[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/libraries/joomla/installer/adapters/ -> library.php (source)

   1  <?php
   2  /**
   3   * @package     Joomla.Platform
   4   * @subpackage  Installer
   5   *
   6   * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
   7   * @license     GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  defined('JPATH_PLATFORM') or die;
  11  
  12  jimport('joomla.installer.librarymanifest');
  13  jimport('joomla.base.adapterinstance');
  14  
  15  /**
  16   * Library installer
  17   *
  18   * @package     Joomla.Platform
  19   * @subpackage  Installer
  20   * @since       11.1
  21   */
  22  class JInstallerLibrary extends JAdapterInstance
  23  {
  24      /**
  25       * Custom loadLanguage method
  26       *
  27       * @param   string  $path  The path where to find language files.
  28       *
  29       * @return  void
  30       *
  31       * @since   11.1
  32       */
  33  	public function loadLanguage($path = null)
  34      {
  35          $source = $this->parent->getPath('source');
  36          if (!$source)
  37          {
  38              $this->parent->setPath('source', JPATH_PLATFORM . '/' . $this->parent->extension->element);
  39          }
  40          $this->manifest = $this->parent->getManifest();
  41          $extension = 'lib_' . strtolower(JFilterInput::getInstance()->clean((string) $this->manifest->name, 'cmd'));
  42          $name = strtolower((string) $this->manifest->libraryname);
  43          $lang = JFactory::getLanguage();
  44          $source = $path ? $path : JPATH_PLATFORM . "/$name";
  45          $lang->load($extension . '.sys', $source, null, false, false)
  46              || $lang->load($extension . '.sys', JPATH_SITE, null, false, false)
  47              || $lang->load($extension . '.sys', $source, $lang->getDefault(), false, false)
  48              || $lang->load($extension . '.sys', JPATH_SITE, $lang->getDefault(), false, false);
  49      }
  50  
  51      /**
  52       * Custom install method
  53       *
  54       * @return  boolean  True on success
  55       *
  56       * @since   11.1
  57       */
  58  	public function install()
  59      {
  60          // Get the extension manifest object
  61          $this->manifest = $this->parent->getManifest();
  62  
  63          // Manifest Document Setup Section
  64  
  65          // Set the extensions name
  66          $name = JFilterInput::getInstance()->clean((string) $this->manifest->name, 'string');
  67          $element = str_replace('.xml', '', basename($this->parent->getPath('manifest')));
  68          $this->set('name', $name);
  69          $this->set('element', $element);
  70  
  71          $db = $this->parent->getDbo();
  72          $query = $db->getQuery(true);
  73          $query->select($db->quoteName('extension_id'));
  74          $query->from($db->quoteName('#__extensions'));
  75          $query->where($db->quoteName('type') . ' = ' . $db->quote('library'));
  76          $query->where($db->quoteName('element') . ' = ' . $db->quote($element));
  77          $db->setQuery($query);
  78          $result = $db->loadResult();
  79          if ($result)
  80          {
  81              // Already installed, can we upgrade?
  82              if ($this->parent->getOverwrite() || $this->parent->getUpgrade())
  83              {
  84                  // We can upgrade, so uninstall the old one
  85                  $installer = new JInstaller; // we don't want to compromise this instance!
  86                  $installer->uninstall('library', $result);
  87              }
  88              else
  89              {
  90                  // Abort the install, no upgrade possible
  91                  $this->parent->abort(JText::_('JLIB_INSTALLER_ABORT_LIB_INSTALL_ALREADY_INSTALLED'));
  92                  return false;
  93              }
  94          }
  95  
  96          // Get the libraries description
  97          $description = (string) $this->manifest->description;
  98          if ($description)
  99          {
 100              $this->parent->set('message', JText::_($description));
 101          }
 102          else
 103          {
 104              $this->parent->set('message', '');
 105          }
 106  
 107          // Set the installation path
 108          $group = (string) $this->manifest->libraryname;
 109          if (!$group)
 110          {
 111              $this->parent->abort(JText::_('JLIB_INSTALLER_ABORT_LIB_INSTALL_NOFILE'));
 112              return false;
 113          }
 114          else
 115          {
 116              $this->parent->setPath('extension_root', JPATH_PLATFORM . '/' . implode(DIRECTORY_SEPARATOR, explode('/', $group)));
 117          }
 118  
 119          // Filesystem Processing Section
 120  
 121          // If the plugin directory does not exist, let's create it
 122          $created = false;
 123          if (!file_exists($this->parent->getPath('extension_root')))
 124          {
 125              if (!$created = JFolder::create($this->parent->getPath('extension_root')))
 126              {
 127                  $this->parent->abort(
 128                      JText::sprintf('JLIB_INSTALLER_ABORT_LIB_INSTALL_FAILED_TO_CREATE_DIRECTORY', $this->parent->getPath('extension_root'))
 129                  );
 130                  return false;
 131              }
 132          }
 133  
 134          // If we created the plugin directory and will want to remove it if we
 135          // have to roll back the installation, let's add it to the installation
 136          // step stack
 137  
 138          if ($created)
 139          {
 140              $this->parent->pushStep(array('type' => 'folder', 'path' => $this->parent->getPath('extension_root')));
 141          }
 142  
 143          // Copy all necessary files
 144          if ($this->parent->parseFiles($this->manifest->files, -1) === false)
 145          {
 146              // Install failed, roll back changes
 147              $this->parent->abort();
 148              return false;
 149          }
 150  
 151          // Parse optional tags
 152          $this->parent->parseLanguages($this->manifest->languages);
 153          $this->parent->parseMedia($this->manifest->media);
 154  
 155          // Extension Registration
 156          $row = JTable::getInstance('extension');
 157          $row->name = $this->get('name');
 158          $row->type = 'library';
 159          $row->element = $this->get('element');
 160          $row->folder = ''; // There is no folder for modules
 161          $row->enabled = 1;
 162          $row->protected = 0;
 163          $row->access = 1;
 164          $row->client_id = 0;
 165          $row->params = $this->parent->getParams();
 166          $row->custom_data = ''; // custom data
 167          $row->manifest_cache = $this->parent->generateManifestCache();
 168          if (!$row->store())
 169          {
 170              // Install failed, roll back changes
 171              $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT_LIB_INSTALL_ROLLBACK', $db->stderr(true)));
 172              return false;
 173          }
 174  
 175          // Finalization and Cleanup Section
 176  
 177          // Lastly, we will copy the manifest file to its appropriate place.
 178          $manifest = array();
 179          $manifest['src'] = $this->parent->getPath('manifest');
 180          $manifest['dest'] = JPATH_MANIFESTS . '/libraries/' . basename($this->parent->getPath('manifest'));
 181          if (!$this->parent->copyFiles(array($manifest), true))
 182          {
 183              // Install failed, rollback changes
 184              $this->parent->abort(JText::_('JLIB_INSTALLER_ABORT_LIB_INSTALL_COPY_SETUP'));
 185              return false;
 186          }
 187          return $row->get('extension_id');
 188      }
 189  
 190      /**
 191       * Custom update method
 192       *
 193       * @return  boolean  True on success
 194       *
 195       * @since   11.1
 196       */
 197  	public function update()
 198      {
 199          // Since this is just files, an update removes old files
 200          // Get the extension manifest object
 201          $this->manifest = $this->parent->getManifest();
 202  
 203          // Manifest Document Setup Section
 204  
 205          // Set the extensions name
 206          $name = (string) $this->manifest->name;
 207          $name = JFilterInput::getInstance()->clean($name, 'string');
 208          $element = str_replace('.xml', '', basename($this->parent->getPath('manifest')));
 209          $this->set('name', $name);
 210          $this->set('element', $element);
 211          $installer = new JInstaller; // we don't want to compromise this instance!
 212          $db = $this->parent->getDbo();
 213          $query = $db->getQuery(true);
 214          $query->select($db->quoteName('extension_id'));
 215          $query->from($db->quoteName('#__extensions'));
 216          $query->where($db->quoteName('type') . ' = ' . $db->quote('library'));
 217          $query->where($db->quoteName('element') . ' = ' . $db->quote($element));
 218          $db->setQuery($query);
 219          $result = $db->loadResult();
 220          if ($result)
 221          {
 222              // Already installed, which would make sense
 223              $installer->uninstall('library', $result);
 224          }
 225          // Now create the new files
 226          return $this->install();
 227      }
 228  
 229      /**
 230       * Custom uninstall method
 231       *
 232       * @param   string  $id  The id of the library to uninstall.
 233       *
 234       * @return  boolean  True on success
 235       *
 236       * @since   11.1
 237       */
 238  	public function uninstall($id)
 239      {
 240          // Initialise variables.
 241          $retval = true;
 242  
 243          // First order of business will be to load the module object table from the database.
 244          // This should give us the necessary information to proceed.
 245          $row = JTable::getInstance('extension');
 246          if (!$row->load((int) $id) || !strlen($row->element))
 247          {
 248              JError::raiseWarning(100, JText::_('ERRORUNKOWNEXTENSION'));
 249              return false;
 250          }
 251  
 252          // Is the library we are trying to uninstall a core one?
 253          // Because that is not a good idea...
 254          if ($row->protected)
 255          {
 256              JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_LIB_UNINSTALL_WARNCORELIBRARY'));
 257              return false;
 258          }
 259  
 260          $manifestFile = JPATH_MANIFESTS . '/libraries/' . $row->element . '.xml';
 261  
 262          // Because libraries may not have their own folders we cannot use the standard method of finding an installation manifest
 263          if (file_exists($manifestFile))
 264          {
 265              $manifest = new JLibraryManifest($manifestFile);
 266              // Set the plugin root path
 267              $this->parent->setPath('extension_root', JPATH_PLATFORM . '/' . $manifest->libraryname);
 268  
 269              $xml = JFactory::getXML($manifestFile);
 270  
 271              // If we cannot load the XML file return null
 272              if (!$xml)
 273              {
 274                  JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_LIB_UNINSTALL_LOAD_MANIFEST'));
 275                  return false;
 276              }
 277  
 278              // Check for a valid XML root tag.
 279              // TODO: Remove backwards compatibility in a future version
 280              // Should be 'extension', but for backward compatibility we will accept 'install'.
 281  
 282              if ($xml->getName() != 'install' && $xml->getName() != 'extension')
 283              {
 284                  JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_LIB_UNINSTALL_INVALID_MANIFEST'));
 285                  return false;
 286              }
 287  
 288              $this->parent->removeFiles($xml->files, -1);
 289              JFile::delete($manifestFile);
 290  
 291          }
 292          else
 293          {
 294              // Remove this row entry since its invalid
 295              $row->delete($row->extension_id);
 296              unset($row);
 297              JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_LIB_UNINSTALL_INVALID_NOTFOUND_MANIFEST'));
 298              return false;
 299          }
 300  
 301          // TODO: Change this so it walked up the path backwards so we clobber multiple empties
 302          // If the folder is empty, let's delete it
 303          if (JFolder::exists($this->parent->getPath('extension_root')))
 304          {
 305              if (is_dir($this->parent->getPath('extension_root')))
 306              {
 307                  $files = JFolder::files($this->parent->getPath('extension_root'));
 308                  if (!count($files))
 309                  {
 310                      JFolder::delete($this->parent->getPath('extension_root'));
 311                  }
 312              }
 313          }
 314  
 315          $this->parent->removeFiles($xml->media);
 316          $this->parent->removeFiles($xml->languages);
 317  
 318          $row->delete($row->extension_id);
 319          unset($row);
 320  
 321          return $retval;
 322      }
 323  
 324      /**
 325       * Custom discover method
 326       *
 327       * @return  array  JExtension  list of extensions available
 328       *
 329       * @since   11.1
 330       */
 331  	public function discover()
 332      {
 333          $results = array();
 334          $file_list = JFolder::files(JPATH_MANIFESTS . '/libraries', '\.xml$');
 335          foreach ($file_list as $file)
 336          {
 337              $manifest_details = JApplicationHelper::parseXMLInstallFile(JPATH_MANIFESTS . '/libraries/' . $file);
 338              $file = JFile::stripExt($file);
 339              $extension = JTable::getInstance('extension');
 340              $extension->set('type', 'library');
 341              $extension->set('client_id', 0);
 342              $extension->set('element', $file);
 343              $extension->set('name', $file);
 344              $extension->set('state', -1);
 345              $extension->set('manifest_cache', json_encode($manifest_details));
 346              $results[] = $extension;
 347          }
 348          return $results;
 349      }
 350  
 351      /**
 352       * Custom discover_install method
 353       *
 354       * @return  boolean  True on success
 355       *
 356       * @since   11.1
 357       */
 358  	public function discover_install()
 359      {
 360          /* Libraries are a strange beast; they are actually references to files
 361           * There are two parts to a library which are disjunct in their locations
 362           * 1) The manifest file (stored in /JPATH_MANIFESTS/libraries)
 363           * 2) The actual files (stored in /JPATH_PLATFORM/libraryname)
 364           * Thus installation of a library is the process of dumping files
 365           * in two different places. As such it is impossible to perform
 366           * any operation beyond mere registration of a library under the presumption
 367           * that the files exist in the appropriate location so that come uninstall
 368           * time they can be adequately removed.
 369           */
 370  
 371          $manifestPath = JPATH_MANIFESTS . '/libraries/' . $this->parent->extension->element . '.xml';
 372          $this->parent->manifest = $this->parent->isManifest($manifestPath);
 373          $this->parent->setPath('manifest', $manifestPath);
 374          $manifest_details = JApplicationHelper::parseXMLInstallFile($this->parent->getPath('manifest'));
 375          $this->parent->extension->manifest_cache = json_encode($manifest_details);
 376          $this->parent->extension->state = 0;
 377          $this->parent->extension->name = $manifest_details['name'];
 378          $this->parent->extension->enabled = 1;
 379          $this->parent->extension->params = $this->parent->getParams();
 380          if ($this->parent->extension->store())
 381          {
 382              return true;
 383          }
 384          else
 385          {
 386              JError::raiseWarning(101, JText::_('JLIB_INSTALLER_ERROR_LIB_DISCOVER_STORE_DETAILS'));
 387              return false;
 388          }
 389      }
 390  
 391      /**
 392       * Refreshes the extension table cache
 393       *
 394       * @return  boolean  Result of operation, true if updated, false on failure
 395       *
 396       * @since   11.1
 397       */
 398  	public function refreshManifestCache()
 399      {
 400          // Need to find to find where the XML file is since we don't store this normally
 401          $manifestPath = JPATH_MANIFESTS . '/libraries/' . $this->parent->extension->element . '.xml';
 402          $this->parent->manifest = $this->parent->isManifest($manifestPath);
 403          $this->parent->setPath('manifest', $manifestPath);
 404  
 405          $manifest_details = JApplicationHelper::parseXMLInstallFile($this->parent->getPath('manifest'));
 406          $this->parent->extension->manifest_cache = json_encode($manifest_details);
 407          $this->parent->extension->name = $manifest_details['name'];
 408  
 409          try
 410          {
 411              return $this->parent->extension->store();
 412          }
 413          catch (JException $e)
 414          {
 415              JError::raiseWarning(101, JText::_('JLIB_INSTALLER_ERROR_LIB_REFRESH_MANIFEST_CACHE'));
 416              return false;
 417          }
 418      }
 419  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1