Source for file library.php

Documentation is available at library.php

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

Documentation generated on Tue, 19 Nov 2013 15:07:05 +0100 by phpDocumentor 1.4.3