Source for file update.php

Documentation is available at update.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_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.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. jimport('joomla.updater.update');
  13.  
  14. /**
  15.  * Installer Update Model
  16.  *
  17.  * @package     Joomla.Administrator
  18.  * @subpackage  com_installer
  19.  * @since       1.6
  20.  */
  21. {
  22.     /**
  23.      * Constructor.
  24.      *
  25.      * @param   array  $config  An optional associative array of configuration settings.
  26.      *
  27.      * @see     JController
  28.      * @since   1.6
  29.      */
  30.     public function __construct($config array())
  31.     {
  32.         if (empty($config['filter_fields']))
  33.         {
  34.             $config['filter_fields'array(
  35.                 'name',
  36.                 'client_id',
  37.                 'type',
  38.                 'folder',
  39.                 'extension_id',
  40.                 'update_id',
  41.                 'update_site_id',
  42.             );
  43.         }
  44.  
  45.         parent::__construct($config);
  46.     }
  47.  
  48.     /**
  49.      * Method to auto-populate the model state.
  50.      *
  51.      * Note. Calling getState in this method will result in recursion.
  52.      *
  53.      * @param   string  $ordering   An optional ordering field.
  54.      * @param   string  $direction  An optional direction (asc|desc).
  55.      *
  56.      * @return  void 
  57.      *
  58.      * @since   1.6
  59.      */
  60.     protected function populateState($ordering null$direction null)
  61.     {
  62.         $app JFactory::getApplication();
  63.         $value $app->getUserStateFromRequest($this->context . '.filter.search''filter_search');
  64.         $this->setState('filter.search'$value);
  65.  
  66.         $clientId $this->getUserStateFromRequest($this->context . '.filter.client_id''filter_client_id''');
  67.         $this->setState('filter.client_id'$clientId);
  68.  
  69.         $categoryId $this->getUserStateFromRequest($this->context . '.filter.type''filter_type''');
  70.         $this->setState('filter.type'$categoryId);
  71.  
  72.         $group $this->getUserStateFromRequest($this->context . '.filter.group''filter_group''');
  73.         $this->setState('filter.group'$group);
  74.  
  75.         $this->setState('message'$app->getUserState('com_installer.message'));
  76.         $this->setState('extension_message'$app->getUserState('com_installer.extension_message'));
  77.         $app->setUserState('com_installer.message''');
  78.         $app->setUserState('com_installer.extension_message''');
  79.  
  80.         parent::populateState('name''asc');
  81.     }
  82.  
  83.     /**
  84.      * Method to get the database query
  85.      *
  86.      * @return  JDatabaseQuery  The database query
  87.      *
  88.      * @since   1.6
  89.      */
  90.     protected function getListQuery()
  91.     {
  92.         $db $this->getDbo();
  93.         $query $db->getQuery(true);
  94.         $type $this->getState('filter.type');
  95.         $client $this->getState('filter.client_id');
  96.         $group $this->getState('filter.group');
  97.  
  98.         // Grab updates ignoring new installs
  99.         $query->select('*')
  100.             ->from('#__updates')
  101.             ->where('extension_id != 0')
  102.             ->order($this->getState('list.ordering'' ' $this->getState('list.direction'));
  103.  
  104.         if ($type)
  105.         {
  106.             $query->where('type=' $db->quote($type));
  107.         }
  108.         if ($client != '')
  109.         {
  110.             $query->where('client_id = ' intval($client));
  111.         }
  112.         if ($group != '' && in_array($typearray('plugin''library''')))
  113.         {
  114.             $query->where('folder=' $db->quote($group == '*' '' $group));
  115.         }
  116.  
  117.         // Filter by extension_id
  118.         if ($eid $this->getState('filter.extension_id'))
  119.         {
  120.             $query->where($db->quoteName('extension_id'' = ' $db->quote((int) $eid));
  121.         }
  122.         else
  123.         {
  124.             $query->where($db->quoteName('extension_id'' != ' $db->quote(0))
  125.                 ->where($db->quoteName('extension_id'' != ' $db->quote(700));
  126.         }
  127.  
  128.         // Filter by search
  129.         $search $this->getState('filter.search');
  130.         if (!empty($search))
  131.         {
  132.             $query->where('name LIKE ' $db->quote('%' $search '%'));
  133.         }
  134.         return $query;
  135.     }
  136.  
  137.     /**
  138.      * Finds updates for an extension.
  139.      *
  140.      * @param   int  $eid            Extension identifier to look for
  141.      * @param   int  $cache_timeout  Cache timout
  142.      *
  143.      * @return  boolean Result
  144.      *
  145.      * @since   1.6
  146.      */
  147.     public function findUpdates($eid 0$cache_timeout 0)
  148.     {
  149.         // Purge the updates list
  150.         $this->purge();
  151.  
  152.         $updater JUpdater::getInstance();
  153.         $updater->findUpdates($eid$cache_timeout);
  154.         return true;
  155.     }
  156.  
  157.     /**
  158.      * Removes all of the updates from the table.
  159.      *
  160.      * @return  boolean result of operation
  161.      *
  162.      * @since   1.6
  163.      */
  164.     public function purge()
  165.     {
  166.         $db JFactory::getDbo();
  167.  
  168.         // Note: TRUNCATE is a DDL operation
  169.         // This may or may not mean depending on your database
  170.         $db->setQuery('TRUNCATE TABLE #__updates');
  171.         if ($db->execute())
  172.         {
  173.             // Reset the last update check timestamp
  174.             $query $db->getQuery(true)
  175.                 ->update($db->quoteName('#__update_sites'))
  176.                 ->set($db->quoteName('last_check_timestamp'' = ' $db->quote(0));
  177.             $db->setQuery($query);
  178.             $db->execute();
  179.             $this->_message JText::_('COM_INSTALLER_PURGED_UPDATES');
  180.             return true;
  181.         }
  182.         else
  183.         {
  184.             $this->_message JText::_('COM_INSTALLER_FAILED_TO_PURGE_UPDATES');
  185.             return false;
  186.         }
  187.     }
  188.  
  189.     /**
  190.      * Enables any disabled rows in #__update_sites table
  191.      *
  192.      * @return  boolean result of operation
  193.      *
  194.      * @since   1.6
  195.      */
  196.     public function enableSites()
  197.     {
  198.         $db JFactory::getDbo();
  199.         $query $db->getQuery(true)
  200.             ->update('#__update_sites')
  201.             ->set('enabled = 1')
  202.             ->where('enabled = 0');
  203.         $db->setQuery($query);
  204.         if ($db->execute())
  205.         {
  206.             if ($rows $db->getAffectedRows())
  207.             {
  208.                 $this->_message .= JText::plural('COM_INSTALLER_ENABLED_UPDATES'$rows);
  209.             }
  210.             return true;
  211.         }
  212.         else
  213.         {
  214.             $this->_message .= JText::_('COM_INSTALLER_FAILED_TO_ENABLE_UPDATES');
  215.             return false;
  216.         }
  217.     }
  218.  
  219.     /**
  220.      * Update function.
  221.      *
  222.      * Sets the "result" state with the result of the operation.
  223.      *
  224.      * @param   array  $uids  Array[int] List of updates to apply
  225.      *
  226.      * @return  void 
  227.      *
  228.      * @since   1.6
  229.      */
  230.     public function update($uids)
  231.     {
  232.         $result true;
  233.         foreach ($uids as $uid)
  234.         {
  235.             $update new JUpdate;
  236.             $instance JTable::getInstance('update');
  237.             $instance->load($uid);
  238.             $update->loadFromXML($instance->detailsurl);
  239.  
  240.             // Install sets state and enqueues messages
  241.             $res $this->install($update);
  242.  
  243.             if ($res)
  244.             {
  245.                 $instance->delete($uid);
  246.             }
  247.  
  248.             $result $res $result;
  249.         }
  250.  
  251.         // Set the final state
  252.         $this->setState('result'$result);
  253.     }
  254.  
  255.     /**
  256.      * Handles the actual update installation.
  257.      *
  258.      * @param   JUpdate  $update  An update definition
  259.      *
  260.      * @return  boolean   Result of install
  261.      *
  262.      * @since   1.6
  263.      */
  264.     private function install($update)
  265.     {
  266.         $app JFactory::getApplication();
  267.         if (isset($update->get('downloadurl')->_data))
  268.         {
  269.             $url $update->downloadurl->_data;
  270.         }
  271.         else
  272.         {
  273.             JError::raiseWarning(''JText::_('COM_INSTALLER_INVALID_EXTENSION_UPDATE'));
  274.             return false;
  275.         }
  276.  
  277.         $p_file JInstallerHelper::downloadPackage($url);
  278.  
  279.         // Was the package downloaded?
  280.         if (!$p_file)
  281.         {
  282.             JError::raiseWarning(''JText::sprintf('COM_INSTALLER_PACKAGE_DOWNLOAD_FAILED'$url));
  283.             return false;
  284.         }
  285.  
  286.         $config        JFactory::getConfig();
  287.         $tmp_dest    $config->get('tmp_path');
  288.  
  289.         // Unpack the downloaded package file
  290.         $package    JInstallerHelper::unpack($tmp_dest '/' $p_file);
  291.  
  292.         // Get an installer instance
  293.         $installer    JInstaller::getInstance();
  294.         $update->set('type'$package['type']);
  295.  
  296.         // Install the package
  297.         if (!$installer->update($package['dir']))
  298.         {
  299.             // There was an error updating the package
  300.             $msg JText::sprintf('COM_INSTALLER_MSG_UPDATE_ERROR'JText::_('COM_INSTALLER_TYPE_TYPE_' strtoupper($package['type'])));
  301.             $result false;
  302.         }
  303.         else
  304.         {
  305.             // Package updated successfully
  306.             $msg JText::sprintf('COM_INSTALLER_MSG_UPDATE_SUCCESS'JText::_('COM_INSTALLER_TYPE_TYPE_' strtoupper($package['type'])));
  307.             $result true;
  308.         }
  309.  
  310.         // Quick change
  311.         $this->type $package['type'];
  312.  
  313.         // Set some model state values
  314.         $app->enqueueMessage($msg);
  315.  
  316.         // TODO: Reconfigure this code when you have more battery life left
  317.         $this->setState('name'$installer->get('name'));
  318.         $this->setState('result'$result);
  319.         $app->setUserState('com_installer.message'$installer->message);
  320.         $app->setUserState('com_installer.extension_message'$installer->get('extension_message'));
  321.  
  322.         // Cleanup the install files
  323.         if (!is_file($package['packagefile']))
  324.         {
  325.             $config JFactory::getConfig();
  326.             $package['packagefile'$config->get('tmp_path''/' $package['packagefile'];
  327.         }
  328.  
  329.         JInstallerHelper::cleanupInstall($package['packagefile']$package['extractdir']);
  330.  
  331.         return $result;
  332.     }
  333.  
  334.     /**
  335.     * Method to get the row form.
  336.     *
  337.     * @param   array    $data      Data for the form.
  338.     * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
  339.     *
  340.     * @return  mixed  A JForm object on success, false on failure
  341.     *
  342.     * @since    2.5.2
  343.     */
  344.     public function getForm($data array()$loadData true)
  345.     {
  346.         // Get the form.
  347.         JForm::addFormPath(JPATH_COMPONENT '/models/forms');
  348.         JForm::addFieldPath(JPATH_COMPONENT '/models/fields');
  349.         $form JForm::getInstance('com_installer.update''update'array('load_data' => $loadData));
  350.  
  351.         // Check for an error.
  352.         if ($form == false)
  353.         {
  354.             $this->setError($form->getMessage());
  355.             return false;
  356.         }
  357.         // Check the session for previously entered form data.
  358.         $data $this->loadFormData();
  359.  
  360.         // Bind the form data if present.
  361.         if (!empty($data))
  362.         {
  363.             $form->bind($data);
  364.         }
  365.  
  366.         return $form;
  367.     }
  368.  
  369.     /**
  370.      * Method to get the data that should be injected in the form.
  371.      *
  372.      * @return  mixed  The data for the form.
  373.      *
  374.      * @since    2.5.2
  375.      */
  376.     protected function loadFormData()
  377.     {
  378.         // Check the session for previously entered form data.
  379.         $data JFactory::getApplication()->getUserState($this->context . '.data'array());
  380.         return $data;
  381.     }
  382. }

Documentation generated on Tue, 19 Nov 2013 15:16:10 +0100 by phpDocumentor 1.4.3