Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
updater.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Updater
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.filesystem.file');
13 jimport('joomla.filesystem.folder');
14 jimport('joomla.filesystem.path');
15 jimport('joomla.base.adapter');
16 jimport('joomla.utilities.arrayhelper');
17 
18 /**
19  * Updater Class
20  *
21  * @package Joomla.Platform
22  * @subpackage Updater
23  * @since 11.1
24  */
25 class JUpdater extends JAdapter
26 {
27  /**
28  * @var JUpdater JUpdater instance container.
29  * @since 11.3
30  */
31  protected static $instance;
32 
33  /**
34  * Constructor
35  *
36  * @since 11.1
37  */
38  public function __construct()
39  {
40  // Adapter base path, class prefix
41  parent::__construct(__DIR__, 'JUpdater');
42  }
43 
44  /**
45  * Returns a reference to the global Installer object, only creating it
46  * if it doesn't already exist.
47  *
48  * @return object An installer object
49  *
50  * @since 11.1
51  */
52  public static function getInstance()
53  {
54  if (!isset(self::$instance))
55  {
56  self::$instance = new JUpdater;
57  }
58  return self::$instance;
59  }
60 
61  /**
62  * Finds an update for an extension
63  *
64  * @param integer $eid Extension Identifier; if zero use all sites
65  * @param integer $cacheTimeout How many seconds to cache update information; if zero, force reload the update information
66  *
67  * @return boolean True if there are updates
68  *
69  * @since 11.1
70  */
71  public function findUpdates($eid = 0, $cacheTimeout = 0)
72  {
73 
74  $db = $this->getDBO();
75  $retval = false;
76 
77  // Push it into an array
78  if (!is_array($eid))
79  {
80  $query = 'SELECT DISTINCT update_site_id, type, location, last_check_timestamp FROM #__update_sites WHERE enabled = 1';
81  }
82  else
83  {
84  $query = 'SELECT DISTINCT update_site_id, type, location, last_check_timestamp FROM #__update_sites' .
85  ' WHERE update_site_id IN' .
86  ' (SELECT update_site_id FROM #__update_sites_extensions WHERE extension_id IN (' . implode(',', $eid) . '))';
87  }
88  $db->setQuery($query);
89  $results = $db->loadAssocList();
90  $result_count = count($results);
91  $now = time();
92  for ($i = 0; $i < $result_count; $i++)
93  {
94  $result = &$results[$i];
95  $this->setAdapter($result['type']);
96  if (!isset($this->_adapters[$result['type']]))
97  {
98  // Ignore update sites requiring adapters we don't have installed
99  continue;
100  }
101  if ($cacheTimeout > 0)
102  {
103  if (isset($result['last_check_timestamp']) && ($now - $result['last_check_timestamp'] <= $cacheTimeout))
104  {
105  // Ignore update sites whose information we have fetched within
106  // the cache time limit
107  $retval = true;
108  continue;
109  }
110  }
111  $update_result = $this->_adapters[$result['type']]->findUpdate($result);
112  if (is_array($update_result))
113  {
114  if (array_key_exists('update_sites', $update_result) && count($update_result['update_sites']))
115  {
116  $results = JArrayHelper::arrayUnique(array_merge($results, $update_result['update_sites']));
117  $result_count = count($results);
118  }
119  if (array_key_exists('updates', $update_result) && count($update_result['updates']))
120  {
121  for ($k = 0, $count = count($update_result['updates']); $k < $count; $k++)
122  {
123  $current_update = &$update_result['updates'][$k];
124  $update = JTable::getInstance('update');
125  $extension = JTable::getInstance('extension');
126  $uid = $update
127  ->find(
128  array(
129  'element' => strtolower($current_update->get('element')), 'type' => strtolower($current_update->get('type')),
130  'client_id' => strtolower($current_update->get('client_id')),
131  'folder' => strtolower($current_update->get('folder'))
132  )
133  );
134 
135  $eid = $extension
136  ->find(
137  array(
138  'element' => strtolower($current_update->get('element')), 'type' => strtolower($current_update->get('type')),
139  'client_id' => strtolower($current_update->get('client_id')),
140  'folder' => strtolower($current_update->get('folder'))
141  )
142  );
143  if (!$uid)
144  {
145  // Set the extension id
146  if ($eid)
147  {
148  // We have an installed extension, check the update is actually newer
149  $extension->load($eid);
150  $data = json_decode($extension->manifest_cache, true);
151  if (version_compare($current_update->version, $data['version'], '>') == 1)
152  {
153  $current_update->extension_id = $eid;
154  $current_update->store();
155  }
156  }
157  else
158  {
159  // A potentially new extension to be installed
160  $current_update->store();
161  }
162  }
163  else
164  {
165  $update->load($uid);
166 
167  // If there is an update, check that the version is newer then replaces
168  if (version_compare($current_update->version, $update->version, '>') == 1)
169  {
170  $current_update->store();
171  }
172  }
173  }
174  }
175  }
176 
177  // Finally, update the last update check timestamp
178  $query = $db->getQuery(true)
179  ->update($db->quoteName('#__update_sites'))
180  ->set($db->quoteName('last_check_timestamp') . ' = ' . $db->quote($now))
181  ->where($db->quoteName('update_site_id') . ' = ' . $db->quote($result['update_site_id']));
182  $db->setQuery($query);
183  $db->execute();
184  }
185  return $retval;
186  }
187 
188  /**
189  * Finds an update for an extension
190  *
191  * @param integer $id Id of the extension
192  *
193  * @return mixed
194  *
195  * @since 11.1
196  */
197  public function update($id)
198  {
199  $updaterow = JTable::getInstance('update');
200  $updaterow->load($id);
201  $update = new JUpdate;
202  if ($update->loadFromXML($updaterow->detailsurl))
203  {
204  return $update->install();
205  }
206  return false;
207  }
208 
209 }