update.compare.inc

  1. drupal
    1. 6 modules/update/update.compare.inc
    2. 7 modules/update/update.compare.inc
    3. 8 core/modules/update/update.compare.inc

Code required only when comparing available updates to existing data.

Functions & methods

NameDescription
update_calculate_project_dataCalculate the current update status of all projects on the site.
update_calculate_project_update_statusCalculate the current update status of a specific project.
update_filter_project_infoFilter the project .info data to only save attributes we need.
update_get_projectsFetch an array of installed and enabled projects.
update_get_project_nameGiven a $file object (as returned by system_get_files_database()), figure out what project it belongs to.
update_process_project_infoProcess the list of projects on the system to figure out the currently installed versions, and other information that is required before we can compare against the available releases to produce the status report.
update_project_cacheRetrieve data from {cache_update} or empty the cache when necessary.
_update_process_info_listPopulate an array of project data.

File

modules/update/update.compare.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Code required only when comparing available updates to existing data.
  5. */
  6. /**
  7. * Fetch an array of installed and enabled projects.
  8. *
  9. * This is only responsible for generating an array of projects (taking into
  10. * account projects that include more than one module or theme). Other
  11. * information like the specific version and install type (official release,
  12. * dev snapshot, etc) is handled later in update_process_project_info() since
  13. * that logic is only required when preparing the status report, not for
  14. * fetching the available release data.
  15. *
  16. * This array is fairly expensive to construct, since it involves a lot of
  17. * disk I/O, so we cache the results into the {cache_update} table using the
  18. * 'update_project_projects' cache ID. However, since this is not the data
  19. * about available updates fetched from the network, it is ok to invalidate it
  20. * somewhat quickly. If we keep this data for very long, site administrators
  21. * are more likely to see incorrect results if they upgrade to a newer version
  22. * of a module or theme but do not visit certain pages that automatically
  23. * clear this cache.
  24. *
  25. * @see update_process_project_info()
  26. * @see update_calculate_project_data()
  27. * @see update_project_cache()
  28. */
  29. function update_get_projects() {
  30. $projects = &drupal_static(__FUNCTION__, array());
  31. if (empty($projects)) {
  32. // Retrieve the projects from cache, if present.
  33. $projects = update_project_cache('update_project_projects');
  34. if (empty($projects)) {
  35. // Still empty, so we have to rebuild the cache.
  36. $module_data = system_rebuild_module_data();
  37. $theme_data = system_rebuild_theme_data();
  38. _update_process_info_list($projects, $module_data, 'module', TRUE);
  39. _update_process_info_list($projects, $theme_data, 'theme', TRUE);
  40. if (variable_get('update_check_disabled', FALSE)) {
  41. _update_process_info_list($projects, $module_data, 'module', FALSE);
  42. _update_process_info_list($projects, $theme_data, 'theme', FALSE);
  43. }
  44. // Allow other modules to alter projects before fetching and comparing.
  45. drupal_alter('update_projects', $projects);
  46. // Cache the site's project data for at most 1 hour.
  47. _update_cache_set('update_project_projects', $projects, REQUEST_TIME + 3600);
  48. }
  49. }
  50. return $projects;
  51. }
  52. /**
  53. * Populate an array of project data.
  54. *
  55. * This iterates over a list of the installed modules or themes and groups
  56. * them by project and status. A few parts of this function assume that
  57. * enabled modules and themes are always processed first, and if disabled
  58. * modules or themes are being processed (there is a setting to control if
  59. * disabled code should be included in the Available updates report or not),
  60. * those are only processed after $projects has been populated with
  61. * information about the enabled code. 'Hidden' modules and themes are always
  62. * ignored. This function also records the latest change time on the .info
  63. * files for each module or theme, which is important data which is used when
  64. * deciding if the cached available update data should be invalidated.
  65. *
  66. * @param $projects
  67. * Reference to the array of project data of what's installed on this site.
  68. * @param $list
  69. * Array of data to process to add the relevant info to the $projects array.
  70. * @param $project_type
  71. * The kind of data in the list (can be 'module' or 'theme').
  72. * @param $status
  73. * Boolean that controls what status (enabled or disabled) to process out of
  74. * the $list and add to the $projects array.
  75. *
  76. * @see update_get_projects()
  77. */
  78. function _update_process_info_list(&$projects, $list, $project_type, $status) {
  79. foreach ($list as $file) {
  80. // A disabled base theme of an enabled sub-theme still has all of its code
  81. // run by the sub-theme, so we include it in our "enabled" projects list.
  82. if ($status && !$file->status && !empty($file->sub_themes)) {
  83. foreach ($file->sub_themes as $key => $name) {
  84. // Build a list of enabled sub-themes.
  85. if ($list[$key]->status) {
  86. $file->enabled_sub_themes[$key] = $name;
  87. }
  88. }
  89. // If there are no enabled subthemes, we should ignore this base theme
  90. // for the enabled case. If the site is trying to display disabled
  91. // themes, we'll catch it then.
  92. if (empty($file->enabled_sub_themes)) {
  93. continue;
  94. }
  95. }
  96. // Otherwise, just add projects of the proper status to our list.
  97. elseif ($file->status != $status) {
  98. continue;
  99. }
  100. // Skip if the .info file is broken.
  101. if (empty($file->info)) {
  102. continue;
  103. }
  104. // Skip if it's a hidden module or theme.
  105. if (!empty($file->info['hidden'])) {
  106. continue;
  107. }
  108. // If the .info doesn't define the 'project', try to figure it out.
  109. if (!isset($file->info['project'])) {
  110. $file->info['project'] = update_get_project_name($file);
  111. }
  112. // If we still don't know the 'project', give up.
  113. if (empty($file->info['project'])) {
  114. continue;
  115. }
  116. // If we don't already know it, grab the change time on the .info file
  117. // itself. Note: we need to use the ctime, not the mtime (modification
  118. // time) since many (all?) tar implementations will go out of their way to
  119. // set the mtime on the files it creates to the timestamps recorded in the
  120. // tarball. We want to see the last time the file was changed on disk,
  121. // which is left alone by tar and correctly set to the time the .info file
  122. // was unpacked.
  123. if (!isset($file->info['_info_file_ctime'])) {
  124. $info_filename = dirname($file->uri) . '/' . $file->name . '.info';
  125. $file->info['_info_file_ctime'] = filectime($info_filename);
  126. }
  127. if (!isset($file->info['datestamp'])) {
  128. $file->info['datestamp'] = 0;
  129. }
  130. $project_name = $file->info['project'];
  131. // Figure out what project type we're going to use to display this module
  132. // or theme. If the project name is 'drupal', we don't want it to show up
  133. // under the usual "Modules" section, we put it at a special "Drupal Core"
  134. // section at the top of the report.
  135. if ($project_name == 'drupal') {
  136. $project_display_type = 'core';
  137. }
  138. else {
  139. $project_display_type = $project_type;
  140. }
  141. if (empty($status) && empty($file->enabled_sub_themes)) {
  142. // If we're processing disabled modules or themes, append a suffix.
  143. // However, we don't do this to a a base theme with enabled
  144. // subthemes, since we treat that case as if it is enabled.
  145. $project_display_type .= '-disabled';
  146. }
  147. // Add a list of sub-themes that "depend on" the project and a list of base
  148. // themes that are "required by" the project.
  149. if ($project_name == 'drupal') {
  150. // Drupal core is always required, so this extra info would be noise.
  151. $sub_themes = array();
  152. $base_themes = array();
  153. }
  154. else {
  155. // Add list of enabled sub-themes.
  156. $sub_themes = !empty($file->enabled_sub_themes) ? $file->enabled_sub_themes : array();
  157. // Add list of base themes.
  158. $base_themes = !empty($file->base_themes) ? $file->base_themes : array();
  159. }
  160. if (!isset($projects[$project_name])) {
  161. // Only process this if we haven't done this project, since a single
  162. // project can have multiple modules or themes.
  163. $projects[$project_name] = array(
  164. 'name' => $project_name,
  165. // Only save attributes from the .info file we care about so we do not
  166. // bloat our RAM usage needlessly.
  167. 'info' => update_filter_project_info($file->info),
  168. 'datestamp' => $file->info['datestamp'],
  169. 'includes' => array($file->name => $file->info['name']),
  170. 'project_type' => $project_display_type,
  171. 'project_status' => $status,
  172. 'sub_themes' => $sub_themes,
  173. 'base_themes' => $base_themes,
  174. );
  175. }
  176. elseif ($projects[$project_name]['project_type'] == $project_display_type) {
  177. // Only add the file we're processing to the 'includes' array for this
  178. // project if it is of the same type and status (which is encoded in the
  179. // $project_display_type). This prevents listing all the disabled
  180. // modules included with an enabled project if we happen to be checking
  181. // for disabled modules, too.
  182. $projects[$project_name]['includes'][$file->name] = $file->info['name'];
  183. $projects[$project_name]['info']['_info_file_ctime'] = max($projects[$project_name]['info']['_info_file_ctime'], $file->info['_info_file_ctime']);
  184. $projects[$project_name]['datestamp'] = max($projects[$project_name]['datestamp'], $file->info['datestamp']);
  185. if (!empty($sub_themes)) {
  186. $projects[$project_name]['sub_themes'] += $sub_themes;
  187. }
  188. if (!empty($base_themes)) {
  189. $projects[$project_name]['base_themes'] += $base_themes;
  190. }
  191. }
  192. elseif (empty($status)) {
  193. // If we have a project_name that matches, but the project_display_type
  194. // does not, it means we're processing a disabled module or theme that
  195. // belongs to a project that has some enabled code. In this case, we add
  196. // the disabled thing into a separate array for separate display.
  197. $projects[$project_name]['disabled'][$file->name] = $file->info['name'];
  198. }
  199. }
  200. }
  201. /**
  202. * Given a $file object (as returned by system_get_files_database()), figure
  203. * out what project it belongs to.
  204. *
  205. * @see system_get_files_database()
  206. */
  207. function update_get_project_name($file) {
  208. $project_name = '';
  209. if (isset($file->info['project'])) {
  210. $project_name = $file->info['project'];
  211. }
  212. elseif (isset($file->info['package']) && (strpos($file->info['package'], 'Core') === 0)) {
  213. $project_name = 'drupal';
  214. }
  215. return $project_name;
  216. }
  217. /**
  218. * Process the list of projects on the system to figure out the currently
  219. * installed versions, and other information that is required before we can
  220. * compare against the available releases to produce the status report.
  221. *
  222. * @param $projects
  223. * Array of project information from update_get_projects().
  224. */
  225. function update_process_project_info(&$projects) {
  226. foreach ($projects as $key => $project) {
  227. // Assume an official release until we see otherwise.
  228. $install_type = 'official';
  229. $info = $project['info'];
  230. if (isset($info['version'])) {
  231. // Check for development snapshots
  232. if (preg_match('@(dev|HEAD)@', $info['version'])) {
  233. $install_type = 'dev';
  234. }
  235. // Figure out what the currently installed major version is. We need
  236. // to handle both contribution (e.g. "5.x-1.3", major = 1) and core
  237. // (e.g. "5.1", major = 5) version strings.
  238. $matches = array();
  239. if (preg_match('/^(\d+\.x-)?(\d+)\..*$/', $info['version'], $matches)) {
  240. $info['major'] = $matches[2];
  241. }
  242. elseif (!isset($info['major'])) {
  243. // This would only happen for version strings that don't follow the
  244. // drupal.org convention. We let contribs define "major" in their
  245. // .info in this case, and only if that's missing would we hit this.
  246. $info['major'] = -1;
  247. }
  248. }
  249. else {
  250. // No version info available at all.
  251. $install_type = 'unknown';
  252. $info['version'] = t('Unknown');
  253. $info['major'] = -1;
  254. }
  255. // Finally, save the results we care about into the $projects array.
  256. $projects[$key]['existing_version'] = $info['version'];
  257. $projects[$key]['existing_major'] = $info['major'];
  258. $projects[$key]['install_type'] = $install_type;
  259. }
  260. }
  261. /**
  262. * Calculate the current update status of all projects on the site.
  263. *
  264. * The results of this function are expensive to compute, especially on sites
  265. * with lots of modules or themes, since it involves a lot of comparisons and
  266. * other operations. Therefore, we cache the results into the {cache_update}
  267. * table using the 'update_project_data' cache ID. However, since this is not
  268. * the data about available updates fetched from the network, it is ok to
  269. * invalidate it somewhat quickly. If we keep this data for very long, site
  270. * administrators are more likely to see incorrect results if they upgrade to
  271. * a newer version of a module or theme but do not visit certain pages that
  272. * automatically clear this cache.
  273. *
  274. * @param array $available
  275. * Data about available project releases.
  276. *
  277. * @see update_get_available()
  278. * @see update_get_projects()
  279. * @see update_process_project_info()
  280. * @see update_project_cache()
  281. */
  282. function update_calculate_project_data($available) {
  283. // Retrieve the projects from cache, if present.
  284. $projects = update_project_cache('update_project_data');
  285. // If $projects is empty, then the cache must be rebuilt.
  286. // Otherwise, return the cached data and skip the rest of the function.
  287. if (!empty($projects)) {
  288. return $projects;
  289. }
  290. $projects = update_get_projects();
  291. update_process_project_info($projects);
  292. foreach ($projects as $project => $project_info) {
  293. if (isset($available[$project])) {
  294. update_calculate_project_update_status($project, $projects[$project], $available[$project]);
  295. }
  296. else {
  297. $projects[$project]['status'] = UPDATE_UNKNOWN;
  298. $projects[$project]['reason'] = t('No available releases found');
  299. }
  300. }
  301. // Give other modules a chance to alter the status (for example, to allow a
  302. // contrib module to provide fine-grained settings to ignore specific
  303. // projects or releases).
  304. drupal_alter('update_status', $projects);
  305. // Cache the site's update status for at most 1 hour.
  306. _update_cache_set('update_project_data', $projects, REQUEST_TIME + 3600);
  307. return $projects;
  308. }
  309. /**
  310. * Calculate the current update status of a specific project.
  311. *
  312. * This function is the heart of the update status feature. For each project
  313. * it is invoked with, it first checks if the project has been flagged with a
  314. * special status like "unsupported" or "insecure", or if the project node
  315. * itself has been unpublished. In any of those cases, the project is marked
  316. * with an error and the next project is considered.
  317. *
  318. * If the project itself is valid, the function decides what major release
  319. * series to consider. The project defines what the currently supported major
  320. * versions are for each version of core, so the first step is to make sure
  321. * the current version is still supported. If so, that's the target version.
  322. * If the current version is unsupported, the project maintainer's recommended
  323. * major version is used. There's also a check to make sure that this function
  324. * never recommends an earlier release than the currently installed major
  325. * version.
  326. *
  327. * Given a target major version, it scans the available releases looking for
  328. * the specific release to recommend (avoiding beta releases and development
  329. * snapshots if possible). This is complicated to describe, but an example
  330. * will help clarify. For the target major version, find the highest patch
  331. * level. If there is a release at that patch level with no extra ("beta",
  332. * etc), then we recommend the release at that patch level with the most
  333. * recent release date. If every release at that patch level has extra (only
  334. * betas), then recommend the latest release from the previous patch
  335. * level. For example:
  336. *
  337. * 1.6-bugfix <-- recommended version because 1.6 already exists.
  338. * 1.6
  339. *
  340. * or
  341. *
  342. * 1.6-beta
  343. * 1.5 <-- recommended version because no 1.6 exists.
  344. * 1.4
  345. *
  346. * It also looks for the latest release from the same major version, even a
  347. * beta release, to display to the user as the "Latest version" option.
  348. * Additionally, it finds the latest official release from any higher major
  349. * versions that have been released to provide a set of "Also available"
  350. * options.
  351. *
  352. * Finally, and most importantly, it keeps scanning the release history until
  353. * it gets to the currently installed release, searching for anything marked
  354. * as a security update. If any security updates have been found between the
  355. * recommended release and the installed version, all of the releases that
  356. * included a security fix are recorded so that the site administrator can be
  357. * warned their site is insecure, and links pointing to the release notes for
  358. * each security update can be included (which, in turn, will link to the
  359. * official security announcements for each vulnerability).
  360. *
  361. * This function relies on the fact that the .xml release history data comes
  362. * sorted based on major version and patch level, then finally by release date
  363. * if there are multiple releases such as betas from the same major.patch
  364. * version (e.g. 5.x-1.5-beta1, 5.x-1.5-beta2, and 5.x-1.5). Development
  365. * snapshots for a given major version are always listed last.
  366. *
  367. */
  368. function update_calculate_project_update_status($project, &$project_data, $available) {
  369. foreach (array('title', 'link') as $attribute) {
  370. if (!isset($project_data[$attribute]) && isset($available[$attribute])) {
  371. $project_data[$attribute] = $available[$attribute];
  372. }
  373. }
  374. // If the project status is marked as something bad, there's nothing else
  375. // to consider.
  376. if (isset($available['project_status'])) {
  377. switch ($available['project_status']) {
  378. case 'insecure':
  379. $project_data['status'] = UPDATE_NOT_SECURE;
  380. if (empty($project_data['extra'])) {
  381. $project_data['extra'] = array();
  382. }
  383. $project_data['extra'][] = array(
  384. 'class' => array('project-not-secure'),
  385. 'label' => t('Project not secure'),
  386. 'data' => t('This project has been labeled insecure by the Drupal security team, and is no longer available for download. Immediately disabling everything included by this project is strongly recommended!'),
  387. );
  388. break;
  389. case 'unpublished':
  390. case 'revoked':
  391. $project_data['status'] = UPDATE_REVOKED;
  392. if (empty($project_data['extra'])) {
  393. $project_data['extra'] = array();
  394. }
  395. $project_data['extra'][] = array(
  396. 'class' => array('project-revoked'),
  397. 'label' => t('Project revoked'),
  398. 'data' => t('This project has been revoked, and is no longer available for download. Disabling everything included by this project is strongly recommended!'),
  399. );
  400. break;
  401. case 'unsupported':
  402. $project_data['status'] = UPDATE_NOT_SUPPORTED;
  403. if (empty($project_data['extra'])) {
  404. $project_data['extra'] = array();
  405. }
  406. $project_data['extra'][] = array(
  407. 'class' => array('project-not-supported'),
  408. 'label' => t('Project not supported'),
  409. 'data' => t('This project is no longer supported, and is no longer available for download. Disabling everything included by this project is strongly recommended!'),
  410. );
  411. break;
  412. case 'not-fetched':
  413. $project_data['status'] = UPDATE_NOT_FETCHED;
  414. $project_data['reason'] = t('Failed to get available update data.');
  415. break;
  416. default:
  417. // Assume anything else (e.g. 'published') is valid and we should
  418. // perform the rest of the logic in this function.
  419. break;
  420. }
  421. }
  422. if (!empty($project_data['status'])) {
  423. // We already know the status for this project, so there's nothing else to
  424. // compute. Record the project status into $project_data and we're done.
  425. $project_data['project_status'] = $available['project_status'];
  426. return;
  427. }
  428. // Figure out the target major version.
  429. $existing_major = $project_data['existing_major'];
  430. $supported_majors = array();
  431. if (isset($available['supported_majors'])) {
  432. $supported_majors = explode(',', $available['supported_majors']);
  433. }
  434. elseif (isset($available['default_major'])) {
  435. // Older release history XML file without supported or recommended.
  436. $supported_majors[] = $available['default_major'];
  437. }
  438. if (in_array($existing_major, $supported_majors)) {
  439. // Still supported, stay at the current major version.
  440. $target_major = $existing_major;
  441. }
  442. elseif (isset($available['recommended_major'])) {
  443. // Since 'recommended_major' is defined, we know this is the new XML
  444. // format. Therefore, we know the current release is unsupported since
  445. // its major version was not in the 'supported_majors' list. We should
  446. // find the best release from the recommended major version.
  447. $target_major = $available['recommended_major'];
  448. $project_data['status'] = UPDATE_NOT_SUPPORTED;
  449. }
  450. elseif (isset($available['default_major'])) {
  451. // Older release history XML file without recommended, so recommend
  452. // the currently defined "default_major" version.
  453. $target_major = $available['default_major'];
  454. }
  455. else {
  456. // Malformed XML file? Stick with the current version.
  457. $target_major = $existing_major;
  458. }
  459. // Make sure we never tell the admin to downgrade. If we recommended an
  460. // earlier version than the one they're running, they'd face an
  461. // impossible data migration problem, since Drupal never supports a DB
  462. // downgrade path. In the unfortunate case that what they're running is
  463. // unsupported, and there's nothing newer for them to upgrade to, we
  464. // can't print out a "Recommended version", but just have to tell them
  465. // what they have is unsupported and let them figure it out.
  466. $target_major = max($existing_major, $target_major);
  467. $release_patch_changed = '';
  468. $patch = '';
  469. // If the project is marked as UPDATE_FETCH_PENDING, it means that the
  470. // data we currently have (if any) is stale, and we've got a task queued
  471. // up to (re)fetch the data. In that case, we mark it as such, merge in
  472. // whatever data we have (e.g. project title and link), and move on.
  473. if (!empty($available['fetch_status']) && $available['fetch_status'] == UPDATE_FETCH_PENDING) {
  474. $project_data['status'] = UPDATE_FETCH_PENDING;
  475. $project_data['reason'] = t('No available update data');
  476. $project_data['fetch_status'] = $available['fetch_status'];
  477. return;
  478. }
  479. // Defend ourselves from XML history files that contain no releases.
  480. if (empty($available['releases'])) {
  481. $project_data['status'] = UPDATE_UNKNOWN;
  482. $project_data['reason'] = t('No available releases found');
  483. return;
  484. }
  485. foreach ($available['releases'] as $version => $release) {
  486. // First, if this is the existing release, check a few conditions.
  487. if ($project_data['existing_version'] === $version) {
  488. if (isset($release['terms']['Release type']) &&
  489. in_array('Insecure', $release['terms']['Release type'])) {
  490. $project_data['status'] = UPDATE_NOT_SECURE;
  491. }
  492. elseif ($release['status'] == 'unpublished') {
  493. $project_data['status'] = UPDATE_REVOKED;
  494. if (empty($project_data['extra'])) {
  495. $project_data['extra'] = array();
  496. }
  497. $project_data['extra'][] = array(
  498. 'class' => array('release-revoked'),
  499. 'label' => t('Release revoked'),
  500. 'data' => t('Your currently installed release has been revoked, and is no longer available for download. Disabling everything included in this release or upgrading is strongly recommended!'),
  501. );
  502. }
  503. elseif (isset($release['terms']['Release type']) &&
  504. in_array('Unsupported', $release['terms']['Release type'])) {
  505. $project_data['status'] = UPDATE_NOT_SUPPORTED;
  506. if (empty($project_data['extra'])) {
  507. $project_data['extra'] = array();
  508. }
  509. $project_data['extra'][] = array(
  510. 'class' => array('release-not-supported'),
  511. 'label' => t('Release not supported'),
  512. 'data' => t('Your currently installed release is now unsupported, and is no longer available for download. Disabling everything included in this release or upgrading is strongly recommended!'),
  513. );
  514. }
  515. }
  516. // Otherwise, ignore unpublished, insecure, or unsupported releases.
  517. if ($release['status'] == 'unpublished' ||
  518. (isset($release['terms']['Release type']) &&
  519. (in_array('Insecure', $release['terms']['Release type']) ||
  520. in_array('Unsupported', $release['terms']['Release type'])))) {
  521. continue;
  522. }
  523. // See if this is a higher major version than our target and yet still
  524. // supported. If so, record it as an "Also available" release.
  525. if ($release['version_major'] > $target_major) {
  526. if (in_array($release['version_major'], $supported_majors)) {
  527. if (!isset($project_data['also'])) {
  528. $project_data['also'] = array();
  529. }
  530. if (!isset($project_data['also'][$release['version_major']])) {
  531. $project_data['also'][$release['version_major']] = $version;
  532. $project_data['releases'][$version] = $release;
  533. }
  534. }
  535. // Otherwise, this release can't matter to us, since it's neither
  536. // from the release series we're currently using nor the recommended
  537. // release. We don't even care about security updates for this
  538. // branch, since if a project maintainer puts out a security release
  539. // at a higher major version and not at the lower major version,
  540. // they must remove the lower version from the supported major
  541. // versions at the same time, in which case we won't hit this code.
  542. continue;
  543. }
  544. // Look for the 'latest version' if we haven't found it yet. Latest is
  545. // defined as the most recent version for the target major version.
  546. if (!isset($project_data['latest_version'])
  547. && $release['version_major'] == $target_major) {
  548. $project_data['latest_version'] = $version;
  549. $project_data['releases'][$version] = $release;
  550. }
  551. // Look for the development snapshot release for this branch.
  552. if (!isset($project_data['dev_version'])
  553. && $release['version_major'] == $target_major
  554. && isset($release['version_extra'])
  555. && $release['version_extra'] == 'dev') {
  556. $project_data['dev_version'] = $version;
  557. $project_data['releases'][$version] = $release;
  558. }
  559. // Look for the 'recommended' version if we haven't found it yet (see
  560. // phpdoc at the top of this function for the definition).
  561. if (!isset($project_data['recommended'])
  562. && $release['version_major'] == $target_major
  563. && isset($release['version_patch'])) {
  564. if ($patch != $release['version_patch']) {
  565. $patch = $release['version_patch'];
  566. $release_patch_changed = $release;
  567. }
  568. if (empty($release['version_extra']) && $patch == $release['version_patch']) {
  569. $project_data['recommended'] = $release_patch_changed['version'];
  570. $project_data['releases'][$release_patch_changed['version']] = $release_patch_changed;
  571. }
  572. }
  573. // Stop searching once we hit the currently installed version.
  574. if ($project_data['existing_version'] === $version) {
  575. break;
  576. }
  577. // If we're running a dev snapshot and have a timestamp, stop
  578. // searching for security updates once we hit an official release
  579. // older than what we've got. Allow 100 seconds of leeway to handle
  580. // differences between the datestamp in the .info file and the
  581. // timestamp of the tarball itself (which are usually off by 1 or 2
  582. // seconds) so that we don't flag that as a new release.
  583. if ($project_data['install_type'] == 'dev') {
  584. if (empty($project_data['datestamp'])) {
  585. // We don't have current timestamp info, so we can't know.
  586. continue;
  587. }
  588. elseif (isset($release['date']) && ($project_data['datestamp'] + 100 > $release['date'])) {
  589. // We're newer than this, so we can skip it.
  590. continue;
  591. }
  592. }
  593. // See if this release is a security update.
  594. if (isset($release['terms']['Release type'])
  595. && in_array('Security update', $release['terms']['Release type'])) {
  596. $project_data['security updates'][] = $release;
  597. }
  598. }
  599. // If we were unable to find a recommended version, then make the latest
  600. // version the recommended version if possible.
  601. if (!isset($project_data['recommended']) && isset($project_data['latest_version'])) {
  602. $project_data['recommended'] = $project_data['latest_version'];
  603. }
  604. //
  605. // Check to see if we need an update or not.
  606. //
  607. if (!empty($project_data['security updates'])) {
  608. // If we found security updates, that always trumps any other status.
  609. $project_data['status'] = UPDATE_NOT_SECURE;
  610. }
  611. if (isset($project_data['status'])) {
  612. // If we already know the status, we're done.
  613. return;
  614. }
  615. // If we don't know what to recommend, there's nothing we can report.
  616. // Bail out early.
  617. if (!isset($project_data['recommended'])) {
  618. $project_data['status'] = UPDATE_UNKNOWN;
  619. $project_data['reason'] = t('No available releases found');
  620. return;
  621. }
  622. // If we're running a dev snapshot, compare the date of the dev snapshot
  623. // with the latest official version, and record the absolute latest in
  624. // 'latest_dev' so we can correctly decide if there's a newer release
  625. // than our current snapshot.
  626. if ($project_data['install_type'] == 'dev') {
  627. if (isset($project_data['dev_version']) && $available['releases'][$project_data['dev_version']]['date'] > $available['releases'][$project_data['latest_version']]['date']) {
  628. $project_data['latest_dev'] = $project_data['dev_version'];
  629. }
  630. else {
  631. $project_data['latest_dev'] = $project_data['latest_version'];
  632. }
  633. }
  634. // Figure out the status, based on what we've seen and the install type.
  635. switch ($project_data['install_type']) {
  636. case 'official':
  637. if ($project_data['existing_version'] === $project_data['recommended'] || $project_data['existing_version'] === $project_data['latest_version']) {
  638. $project_data['status'] = UPDATE_CURRENT;
  639. }
  640. else {
  641. $project_data['status'] = UPDATE_NOT_CURRENT;
  642. }
  643. break;
  644. case 'dev':
  645. $latest = $available['releases'][$project_data['latest_dev']];
  646. if (empty($project_data['datestamp'])) {
  647. $project_data['status'] = UPDATE_NOT_CHECKED;
  648. $project_data['reason'] = t('Unknown release date');
  649. }
  650. elseif (($project_data['datestamp'] + 100 > $latest['date'])) {
  651. $project_data['status'] = UPDATE_CURRENT;
  652. }
  653. else {
  654. $project_data['status'] = UPDATE_NOT_CURRENT;
  655. }
  656. break;
  657. default:
  658. $project_data['status'] = UPDATE_UNKNOWN;
  659. $project_data['reason'] = t('Invalid info');
  660. }
  661. }
  662. /**
  663. * Retrieve data from {cache_update} or empty the cache when necessary.
  664. *
  665. * Two very expensive arrays computed by this module are the list of all
  666. * installed modules and themes (and .info data, project associations, etc),
  667. * and the current status of the site relative to the currently available
  668. * releases. These two arrays are cached in the {cache_update} table and used
  669. * whenever possible. The cache is cleared whenever the administrator visits
  670. * the status report, available updates report, or the module or theme
  671. * administration pages, since we should always recompute the most current
  672. * values on any of those pages.
  673. *
  674. * Note: while both of these arrays are expensive to compute (in terms of disk
  675. * I/O and some fairly heavy CPU processing), neither of these is the actual
  676. * data about available updates that we have to fetch over the network from
  677. * updates.drupal.org. That information is stored with the
  678. * 'update_available_releases' cache ID -- it needs to persist longer than 1
  679. * hour and never get invalidated just by visiting a page on the site.
  680. *
  681. * @param $cid
  682. * The cache id of data to return from the cache. Valid options are
  683. * 'update_project_data' and 'update_project_projects'.
  684. *
  685. * @return
  686. * The cached value of the $projects array generated by
  687. * update_calculate_project_data() or update_get_projects(), or an empty
  688. * array when the cache is cleared.
  689. */
  690. function update_project_cache($cid) {
  691. $projects = array();
  692. // On certain paths, we should clear the cache and recompute the projects for
  693. // update status of the site to avoid presenting stale information.
  694. $q = $_GET['q'];
  695. $paths = array(
  696. 'admin/modules',
  697. 'admin/modules/update',
  698. 'admin/appearance',
  699. 'admin/appearance/update',
  700. 'admin/reports',
  701. 'admin/reports/updates',
  702. 'admin/reports/updates/update',
  703. 'admin/reports/status',
  704. 'admin/reports/updates/check',
  705. );
  706. if (in_array($q, $paths)) {
  707. _update_cache_clear($cid);
  708. }
  709. else {
  710. $cache = _update_cache_get($cid);
  711. if (!empty($cache->data) && $cache->expire > REQUEST_TIME) {
  712. $projects = $cache->data;
  713. }
  714. }
  715. return $projects;
  716. }
  717. /**
  718. * Filter the project .info data to only save attributes we need.
  719. *
  720. * @param array $info
  721. * Array of .info file data as returned by drupal_parse_info_file().
  722. *
  723. * @return
  724. * Array of .info file data we need for the Update manager.
  725. *
  726. * @see _update_process_info_list()
  727. */
  728. function update_filter_project_info($info) {
  729. $whitelist = array(
  730. '_info_file_ctime',
  731. 'datestamp',
  732. 'major',
  733. 'name',
  734. 'package',
  735. 'project',
  736. 'project status url',
  737. 'version',
  738. );
  739. return array_intersect_key($info, drupal_map_assoc($whitelist));
  740. }
Login or register to post comments