updatecode.pm.inc

  1. 6.x commands/pm/updatecode.pm.inc
  2. 3.x commands/pm/updatecode.pm.inc
  3. 4.x commands/pm/updatecode.pm.inc
  4. 5.x commands/pm/updatecode.pm.inc
  5. master commands/pm/updatecode.pm.inc

Functions

Namesort descending Description
drush_pm_updatecode Command callback. Displays update status info and allows to update installed projects. Pass specific projects as arguments, otherwise we update all that have candidate releases.
drush_pm_updatecode_rollback
pm_get_release Get the a best release match for a requested update.
pm_project_filter Return an array of updateable projects and fill $rows.
pm_release_recommended Set a release to a recommended version (if available), and set as updateable.
pm_update_complete Run the post-update hooks after updatecode is complete for one project.
pm_update_packages Update projects according to an array of releases and print the release notes for each project, following interactive confirmation from the user.
pm_update_project Update one project -- a module, theme or Drupal core.
_pm_update_core Update drupal core, following interactive confirmation from the user.
_pm_update_move_files Move some files from one location to another

File

commands/pm/updatecode.pm.inc
View source
  1. <?php
  2. /**
  3. * Command callback. Displays update status info and allows to update installed
  4. * projects.
  5. * Pass specific projects as arguments, otherwise we update all that have
  6. * candidate releases.
  7. *
  8. * This command prompts for confirmation before updating, so it is safe to run
  9. * just to check on. In this case, say at the confirmation prompt.
  10. */
  11. function drush_pm_updatecode() {
  12. // We don't provide for other options here, so we supply an explicit path.
  13. drush_include_engine('update_info', 'drupal', NULL, DRUSH_BASE_PATH . '/commands/pm/update_info');
  14. // Find only security updates?
  15. $security_only = drush_get_option('security-only');
  16. // Get specific requests.
  17. $requests = _convert_csv_to_array(func_get_args());
  18. // Parse out project name and version.
  19. $requests = pm_parse_project_version($requests);
  20. // Get installed extensions and projects.
  21. $extensions = drush_get_extensions();
  22. $projects = drush_get_projects($extensions);
  23. // Get update status information.
  24. $update_info = _pm_get_update_info($projects);
  25. // Process locks specified on the command line.
  26. $locked_list = drush_pm_update_lock($update_info, drush_get_option_list('lock'), drush_get_option_list('unlock'), drush_get_option('lock-message'));
  27. foreach ($extensions as $name => $extension) {
  28. // Add an item to $update_info for each enabled extension which was obtained
  29. // from cvs or git and its project is unknown (because of cvs_deploy or
  30. // git_deploy is not enabled).
  31. if (!isset($extension->info['project'])) {
  32. if ((isset($extension->vcs)) && ($extension->status)) {
  33. $update_info[$name] = array(
  34. 'title' => $extension->info['name'],
  35. 'existing_version' => 'Unknown',
  36. 'status' => DRUSH_PM_REQUESTED_PROJECT_NOT_PACKAGED,
  37. 'status_msg' => dt('Project was not packaged by drupal.org but obtained from !vcs. You need to enable !vcs_deploy module', array('!vcs' => $extension->vcs))
  38. );
  39. // The user may have requested to update a project matching this
  40. // extension. If it was by coincidence or error we don't mind as we've
  41. // already added an item to $update_info. Just clean up $requests.
  42. if (isset($requests[$name])) {
  43. unset($requests[$name]);
  44. }
  45. }
  46. }
  47. // Aditionally if the extension name is distinct to the project name and
  48. // the user asked to update the extension, fix the request.
  49. elseif ((isset($requests[$name])) && ($extension->name != $extension->info['project'])) {
  50. $requests[$extension->info['project']] = $requests[$name];
  51. unset($requests[$name]);
  52. }
  53. }
  54. // Add an item to $update_info for each request not present in $update_info.
  55. foreach ($requests as $name => $request) {
  56. if (!isset($update_info[$name])) {
  57. // Disabled projects.
  58. if ((isset($projects[$name])) && ($projects[$name]['status'] == 0)) {
  59. $update_info[$name] = array(
  60. 'title' => $name,
  61. 'existing_version' => $projects[$name]['version'],
  62. 'status' => DRUSH_PM_REQUESTED_PROJECT_NOT_UPDATEABLE
  63. );
  64. unset($requests[$name]);
  65. }
  66. // At this point we are unable to find matching installed project.
  67. // It does not exist at all or it is mispelled,...
  68. else {
  69. $update_info[$name] = array(
  70. 'title' => $name,
  71. 'existing_version' => 'Unknown',
  72. 'status'=> DRUSH_PM_REQUESTED_PROJECT_NOT_FOUND,
  73. );
  74. }
  75. }
  76. }
  77. // If specific versions were requested, match the requested release.
  78. foreach ($requests as $name => $request) {
  79. if (!empty($request['version'])) {
  80. $release = pm_get_release($request, $update_info[$name]);
  81. if (!$release) {
  82. $update_info[$name]['status'] = DRUSH_PM_REQUESTED_VERSION_NOT_FOUND;
  83. }
  84. else if ($release['version'] == $update_info[$name]['existing_version']) {
  85. $update_info[$name]['status'] = DRUSH_PM_REQUESTED_CURRENT;
  86. }
  87. else {
  88. $update_info[$name]['status'] = DRUSH_PM_REQUESTED_UPDATE;
  89. }
  90. // Set the candidate version to the requested release.
  91. $update_info[$name]['candidate_version'] = $release['version'];
  92. }
  93. }
  94. // Table headers.
  95. $rows[] = array(dt('Name'), dt('Installed version'), dt('Proposed version'), dt('Status'));
  96. // Process releases, notifying user of status and
  97. // building a list of proposed updates.
  98. $updateable = pm_project_filter($update_info, $rows, $security_only);
  99. // Pipe preparation.
  100. if (drush_get_context('DRUSH_PIPE')) {
  101. $pipe = "";
  102. foreach($updateable as $project) {
  103. $pipe .= $project['name']. " ";
  104. $pipe .= $project['existing_version']. " ";
  105. $pipe .= $project['candidate_version']. " ";
  106. $pipe .= str_replace(' ', '-', pm_update_filter($project)). "\n";
  107. }
  108. drush_print_pipe($pipe);
  109. // Automatically curtail update process if in pipe mode.
  110. $updateable = array();
  111. }
  112. $tmpfile = drush_tempnam('pm-updatecode.');
  113. $last = pm_update_last_check();
  114. drush_print(dt('Update information last refreshed: ') . ($last ? format_date($last) : dt('Never')));
  115. drush_print();
  116. drush_print(dt("Update status information on all installed and enabled Drupal projects:"));
  117. drush_print_table($rows, TRUE, array(3 => 40), $tmpfile);
  118. $contents = file_get_contents($tmpfile);
  119. drush_print($contents);
  120. drush_print();
  121. // If specific project updates were requested then remove releases for all
  122. // others.
  123. if (!empty($requests)) {
  124. foreach ($updateable as $name => $project) {
  125. if (!isset($requests[$name])) {
  126. unset($updateable[$name]);
  127. }
  128. }
  129. }
  130. // Prevent update of core if --no-core was specified.
  131. if (isset($updateable['drupal']) && drush_get_option('no-core', FALSE)) {
  132. unset($updateable['drupal']);
  133. drush_print(dt('Skipping core update (--no-core specified).'));
  134. }
  135. // If there are any locked projects that were not requested, then remove them.
  136. if (!empty($locked_list)) {
  137. foreach ($updateable as $name => $project) {
  138. if ((isset($locked_list[$name])) && (!isset($requests[$name]))) {
  139. unset($updateable[$name]);
  140. }
  141. }
  142. }
  143. // First check to see if there is a newer drush.
  144. $drush_update_available = NULL;
  145. if (drush_get_option('self-update', TRUE)) {
  146. $drush_update_available = drush_check_self_update();
  147. }
  148. // Do no updates in simulated mode.
  149. if (drush_get_context('DRUSH_SIMULATE')) {
  150. return drush_log(dt('No action taken in simulated mode.'), 'ok');
  151. return TRUE;
  152. }
  153. $core_update_available = FALSE;
  154. if (isset($updateable['drupal'])) {
  155. $drupal_project = $updateable['drupal'];
  156. unset($update_info['drupal']);
  157. unset($updateable['drupal']);
  158. // At present we need to update drupal core after non-core projects
  159. // are updated.
  160. if (empty($updateable)) {
  161. return _pm_update_core($drupal_project, $tmpfile);
  162. }
  163. // If there are modules other than drupal core enabled, then update them
  164. // first.
  165. else {
  166. $core_update_available = TRUE;
  167. if ($drupal_project['status'] == UPDATE_NOT_SECURE) {
  168. drush_print(dt("NOTE: A security update for the Drupal core is available."));
  169. }
  170. else {
  171. drush_print(dt("NOTE: A code update for the Drupal core is available."));
  172. }
  173. drush_print(dt("Drupal core will be updated after all of the non-core modules are updated.\n"));
  174. }
  175. }
  176. // If there are no releases to update, then print a final
  177. // exit message. Supress the message if we already printed
  178. // a message about a drush update being available.
  179. if (empty($updateable)) {
  180. if ($drush_update_available === TRUE) {
  181. return FALSE;
  182. }
  183. if ($security_only) {
  184. return drush_log(dt('No security updates available.'), 'ok');
  185. }
  186. else {
  187. return drush_log(dt('No code updates available.'), 'ok');
  188. }
  189. }
  190. // Offer to update to the identified releases.
  191. if (!pm_update_packages($updateable, $tmpfile)) {
  192. return FALSE;
  193. }
  194. // After projects are updated we can update core.
  195. if ($core_update_available) {
  196. drush_print();
  197. return _pm_update_core($drupal_project, $tmpfile);
  198. }
  199. }
  200. /**
  201. * Update drupal core, following interactive confirmation from the user.
  202. *
  203. * @param $project
  204. * The drupal project information from the drupal.org update service,
  205. * copied from $update_info['drupal']. @see drush_pm_updatecode.
  206. */
  207. function _pm_update_core(&$project, $tmpfile) {
  208. drush_include_engine('package_handler', drush_get_option('package-handler', 'wget'));
  209. $drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT');
  210. drush_print(dt('Code updates will be made to drupal core.'));
  211. drush_print(dt("WARNING: Updating core will discard any modifications made to Drupal core files, most noteworthy among these are .htaccess and robots.txt. If you have made any modifications to these files, please back them up before updating so that you can re-create your modifications in the updated version of the file."));
  212. drush_print(dt("Note: Updating core can potentially break your site. It is NOT recommended to update production sites without prior testing."));
  213. drush_print();
  214. if (drush_get_option('notes', FALSE)) {
  215. drush_print('Obtaining release notes for above projects...');
  216. _drush_pm_releasenotes(array('drupal'), TRUE, $tmpfile);
  217. }
  218. if(!drush_confirm(dt('Do you really want to continue?'))) {
  219. drush_print(dt('Rolling back all changes. Run again with --no-core to update modules only.'));
  220. return drush_user_abort();
  221. }
  222. // We need write permission on $drupal_root.
  223. if (!is_writable($drupal_root)) {
  224. return drush_set_error('DRUSH_PATH_NO_WRITABLE', dt('Drupal root path is not writable.'));
  225. }
  226. // Create a directory 'core' if it does not already exist.
  227. $project['path'] = 'drupal-' . $project['candidate_version'];
  228. $project['full_project_path'] = $drupal_root . '/' . $project['path'];
  229. if (!is_dir($project['full_project_path'])) {
  230. drush_mkdir($project['full_project_path']);
  231. }
  232. // Create a list of directories to exclude from the update process.
  233. $skip_list = array('sites', $project['path']);
  234. // Add non-writable directories: we can't move them around.
  235. // We will also use $items_to_test later for $version_control check.
  236. $items_to_test = drush_scan_directory($drupal_root, '/.*/', array_merge(array('.', '..'), $skip_list), 0, FALSE, 'basename', 0, TRUE);
  237. foreach (array_keys($items_to_test) as $item) {
  238. if (is_dir($item) && !is_writable($item)) {
  239. $skip_list[] = $item;
  240. unset($items_to_test[$item]);
  241. }
  242. elseif (is_link($item)) {
  243. $skip_list[] = $item;
  244. unset($items_to_test[$item]);
  245. }
  246. }
  247. $project['skip_list'] = $skip_list;
  248. // Move all files and folders in $drupal_root to the new 'core' directory
  249. // except for the items in the skip list
  250. _pm_update_move_files($drupal_root, $project['full_project_path'], $project['skip_list']);
  251. // Set a context variable to indicate that rollback should reverse
  252. // the _pm_update_move_files above.
  253. drush_set_context('DRUSH_PM_DRUPAL_CORE', $project);
  254. if (!$version_control = drush_pm_include_version_control($project['full_project_path'])) {
  255. return FALSE;
  256. }
  257. $project['base_project_path'] = dirname($project['full_project_path']);
  258. // Check we have a version control system, and it clears its pre-flight.
  259. if (!$version_control->pre_update($project, $items_to_test)) {
  260. return FALSE;
  261. }
  262. // Package handlers want the project directory in project_dir.
  263. $project['project_dir'] = $project['path'];
  264. // Update core.
  265. if (pm_update_project($project, $version_control) === FALSE) {
  266. return FALSE;
  267. }
  268. // Take the updated files in the 'core' directory that have been updated,
  269. // and move all except for the items in the skip list back to
  270. // the drupal root
  271. _pm_update_move_files($project['full_project_path'], $drupal_root, $project['skip_list']);
  272. drush_delete_dir($project['full_project_path']);
  273. // Version control engines expect full_project_path to exist and be accurate.
  274. $project['full_project_path'] = $project['base_project_path'];
  275. // If there is a backup target, then find items
  276. // in the backup target that do not exist at the
  277. // drupal root. These are to be moved back.
  278. if (array_key_exists('backup_target', $project)) {
  279. _pm_update_move_files($project['backup_target'], $drupal_root, $project['skip_list'], FALSE);
  280. _pm_update_move_files($project['backup_target'] . '/profiles', $drupal_root . '/profiles', array('default'), FALSE);
  281. }
  282. pm_update_complete($project, $version_control);
  283. return TRUE;
  284. }
  285. /**
  286. * Move some files from one location to another
  287. */
  288. function _pm_update_move_files($src_dir, $dest_dir, $skip_list, $remove_conflicts = TRUE) {
  289. $items_to_move = drush_scan_directory($src_dir, '/.*/', array_merge(array('.', '..'), $skip_list), 0, FALSE, 'filename', 0, TRUE);
  290. foreach ($items_to_move as $filename => $info) {
  291. if ($remove_conflicts) {
  292. drush_delete_dir($dest_dir . '/' . basename($filename));
  293. }
  294. if (!file_exists($dest_dir . '/' . basename($filename))) {
  295. $move_result = drush_move_dir($filename, $dest_dir . '/' . basename($filename));
  296. }
  297. }
  298. return TRUE;
  299. }
  300. /**
  301. * Update projects according to an array of releases and print the release notes
  302. * for each project, following interactive confirmation from the user.
  303. *
  304. * @param $update_info
  305. * An array of projects from the drupal.org update service, with an additional
  306. * array key candidate_version that specifies the version to be installed.
  307. */
  308. function pm_update_packages($update_info, $tmpfile) {
  309. drush_include_engine('package_handler', drush_get_option('package-handler', 'wget'));
  310. $drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT');
  311. $print = '';
  312. $status = array();
  313. foreach($update_info as $project) {
  314. $print .= $project['title'] . " [" . $project['name'] . '-' . $project['candidate_version'] . "], ";
  315. $status[$project['status']] = $project['status'];
  316. }
  317. // We print the list of the projects that need to be updated.
  318. if (isset($status[UPDATE_NOT_SECURE])) {
  319. if (isset($status[UPDATE_NOT_CURRENT])) {
  320. $title = (dt('Security and code updates will be made to the following projects:'));
  321. }
  322. else {
  323. $title = (dt('Security updates will be made to the following projects:'));
  324. }
  325. }
  326. else {
  327. $title = (dt('Code updates will be made to the following projects:'));
  328. }
  329. $print = "$title " . (substr($print, 0, strlen($print)-2));
  330. drush_print($print);
  331. file_put_contents($tmpfile, "\n\n$print\n\n", FILE_APPEND);
  332. // Print the release notes for projects to be updated.
  333. if (drush_get_option('notes', FALSE)) {
  334. drush_print('Obtaining release notes for above projects...');
  335. _drush_pm_releasenotes(array_keys($update_info), TRUE, $tmpfile);
  336. }
  337. // We print some warnings before the user confirms the update.
  338. drush_print();
  339. if (drush_get_option('no-backup', FALSE)) {
  340. drush_print(dt("Note: You have selected to not store backups."));
  341. }
  342. else {
  343. drush_print(dt("Note: A backup of your project will be stored to backups directory if it is not managed by a supported version control system."));
  344. drush_print(dt('Note: If you have made any modifications to any file that belongs to one of these projects, you will have to migrate those modifications after updating.'));
  345. }
  346. if(!drush_confirm(dt('Do you really want to continue with the update process?'))) {
  347. return drush_user_abort();
  348. }
  349. // Now we start the actual updating.
  350. foreach($update_info as $project) {
  351. if (empty($project['path'])) {
  352. return drush_set_error('DRUSH_PM_UPDATING_NO_PROJECT_PATH', dt('The !project project path is not available, perhaps the !type is enabled but has been deleted from disk.', array('!project' => $project['name'], '!type' => $project['project_type'])));
  353. }
  354. drush_log(dt('Starting to update !project code at !dir...', array('!project' => $project['title'], '!dir' => $project['path'])));
  355. // Create the projects directory and base (parent) directory.
  356. $project['full_project_path'] = $drupal_root . '/' . $project['path'];
  357. // Check that the directory exists, and is where we expect it to be.
  358. if (stripos($project['path'], $project['project_type']) === FALSE || !is_dir($project['full_project_path'])) {
  359. return drush_set_error('DRUSH_PM_UPDATING_PATH_NOT_FOUND', dt('The !project directory could not be found within the !types directory at !full_project_path, perhaps the project is enabled but has been deleted from disk.', array('!project' => $project['name'], '!type' => $project['project_type'], '!full_project_path' => $project['full_project_path'])));
  360. }
  361. if (!$version_control = drush_pm_include_version_control($project['full_project_path'])) {
  362. return FALSE;
  363. }
  364. $project['base_project_path'] = dirname($project['full_project_path']);
  365. // Check we have a version control system, and it clears its pre-flight.
  366. if (!$version_control->pre_update($project)) {
  367. return FALSE;
  368. }
  369. // Package handlers want the name of the directory in project_dir.
  370. // It may be different to the project name for pm-download.
  371. // Perhaps we want here filename($project['full_project_path']).
  372. $project['project_dir'] = $project['name'];
  373. // Run update on one project.
  374. if (pm_update_project($project, $version_control) === FALSE) {
  375. return FALSE;
  376. }
  377. pm_update_complete($project, $version_control);
  378. }
  379. return TRUE;
  380. }
  381. /**
  382. * Update one project -- a module, theme or Drupal core.
  383. *
  384. * @param $project
  385. * The project to upgrade. $project['full_project_path'] must be set
  386. * to the location where this project is stored.
  387. */
  388. function pm_update_project($project, $version_control) {
  389. // 1. If the version control engine is a proper vcs we need to remove project
  390. // files in order to not have orphan files after update.
  391. // 2. If the package-handler is cvs or git, it will remove upstream removed
  392. // files and no orphans will exist after update.
  393. // So, we must remove all files previous update if the directory is not a
  394. // working copy of cvs or git but we don't need to remove them if the version
  395. // control engine is backup, as it did already move the project out to the
  396. // backup directory.
  397. if (($version_control->engine != 'backup') && (drush_get_option('package-handler', 'wget') == 'wget')) {
  398. // Find and unlink all files but the ones in the vcs control directories.
  399. $skip_list = array('.', '..');
  400. $skip_list = array_merge($skip_list, drush_version_control_reserved_files());
  401. drush_scan_directory($project['full_project_path'], '/.*/', $skip_list, 'unlink', TRUE, 'filename', 0, TRUE);
  402. }
  403. // Add the project to a context so we can roll back if needed.
  404. $updated = drush_get_context('DRUSH_PM_UPDATED');
  405. $updated[] = $project;
  406. drush_set_context('DRUSH_PM_UPDATED', $updated);
  407. if (!package_handler_update_project($project, $project['releases'][$project['candidate_version']])) {
  408. return drush_set_error('DRUSH_PM_UPDATING_FAILED', dt('Updating project !project failed. Attempting to roll back to previously installed version.', array('!project' => $project['name'])));
  409. }
  410. // If the version control engine is a proper vcs we also need to remove
  411. // orphan directories.
  412. if (($version_control->engine != 'backup') && (drush_get_option('package-handler', 'wget') == 'wget')) {
  413. $files = drush_find_empty_directories($project['full_project_path'], $version_control->reserved_files());
  414. array_map('drush_delete_dir', $files);
  415. }
  416. return TRUE;
  417. }
  418. /**
  419. * Run the post-update hooks after updatecode is complete for one project.
  420. */
  421. function pm_update_complete($project, $version_control) {
  422. drush_print(dt('Project !project was updated successfully. Installed version is now !version.', array('!project' => $project['name'], '!version' => $project['candidate_version'])));
  423. drush_command_invoke_all('pm_post_update', $project['name'], $project['releases'][$project['candidate_version']]);
  424. $version_control->post_update($project);
  425. }
  426. function drush_pm_updatecode_rollback() {
  427. $projects = array_reverse(drush_get_context('DRUSH_PM_UPDATED', array()));
  428. foreach($projects as $project) {
  429. drush_log(dt('Rolling back update of !project code ...', array('!project' => $project['title'])));
  430. // Check we have a version control system, and it clears it's pre-flight.
  431. if (!$version_control = drush_pm_include_version_control($project['path'])) {
  432. return FALSE;
  433. }
  434. $version_control->rollback($project);
  435. }
  436. // Post rollback, we will do additional repair if the project is drupal core.
  437. $drupal_core = drush_get_context('DRUSH_PM_DRUPAL_CORE', FALSE);
  438. if ($drupal_core) {
  439. $drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT');
  440. _pm_update_move_files($drupal_core['full_project_path'], $drupal_root, $drupal_core['skip_list']);
  441. drush_delete_dir($drupal_core['full_project_path']);
  442. }
  443. }
  444. /**
  445. * Return an array of updateable projects and fill $rows.
  446. *
  447. * Array of updateable projects is obtained from calculated project update
  448. * status and $security_only flag.
  449. */
  450. function pm_project_filter(&$update_info, &$rows, $security_only) {
  451. $updateable = array();
  452. foreach ($update_info as $key => $project) {
  453. if (empty($project['title'])) {
  454. continue;
  455. }
  456. switch($project['status']) {
  457. case DRUSH_PM_REQUESTED_UPDATE:
  458. $status = dt('Specified version available');
  459. $project['updateable'] = TRUE;
  460. break;
  461. case DRUSH_PM_REQUESTED_CURRENT:
  462. $status = dt('Specified version already installed');
  463. break;
  464. case DRUSH_PM_REQUESTED_PROJECT_NOT_PACKAGED:
  465. $status = $project['status_msg'];
  466. break;
  467. case DRUSH_PM_REQUESTED_VERSION_NOT_FOUND:
  468. $status = dt('Specified version not found');
  469. break;
  470. case DRUSH_PM_REQUESTED_PROJECT_NOT_FOUND:
  471. $status = dt('Specified project not found');
  472. break;
  473. case DRUSH_PM_REQUESTED_PROJECT_NOT_UPDATEABLE:
  474. $status = dt('Project has no enabled extensions and can\'t be updated');
  475. break;
  476. default:
  477. $status = pm_update_filter($project);
  478. break;
  479. }
  480. // Special checking: if drush decides that the candidate version is older
  481. // than the installed version, then we will set the candidate version to
  482. // the installed version.
  483. if (isset($project['candidate_version'], $project['releases'][$project['candidate_version']], $project['releases'][$project['existing_version']])) {
  484. if ($project['releases'][$project['candidate_version']]['date'] < $project['releases'][$project['existing_version']]['date']) {
  485. $project['candidate_version'] = $project['existing_version'];
  486. }
  487. }
  488. if (isset($project['locked'])) {
  489. $status = $project['locked'] . " ($status)";
  490. }
  491. // Persist candidate_version in $update_info (plural).
  492. if (empty($project['candidate_version'])) {
  493. $update_info[$key]['candidate_version'] = $project['existing_version']; // Default to no change
  494. }
  495. else {
  496. $update_info[$key]['candidate_version'] = $project['candidate_version'];
  497. }
  498. if (!empty($project['updateable'])) {
  499. $updateable[$key] = $project;
  500. // Find only security updates
  501. if ($security_only && ($project['status'] != UPDATE_NOT_SECURE)) {
  502. unset($updateable[$key]);
  503. }
  504. }
  505. $rows[] = array($project['title'], $project['existing_version'], $update_info[$key]['candidate_version'], $status);
  506. }
  507. return $updateable;
  508. }
  509. /**
  510. * Set a release to a recommended version (if available), and set as updateable.
  511. */
  512. function pm_release_recommended(&$project) {
  513. if (isset($project['recommended'])) {
  514. $project['candidate_version'] = $project['recommended'];
  515. $project['updateable'] = TRUE;
  516. }
  517. }
  518. /**
  519. * Get the a best release match for a requested update.
  520. *
  521. * @param $request A information array for the requested project
  522. * @param $project A project information array for this project, as returned by an update service from pm_get_extensions()
  523. */
  524. function pm_get_release($request, $project) {
  525. $minor = '';
  526. $version_patch_changed = '';
  527. if ($request['version']) {
  528. // The user specified a specific version - try to find that exact version
  529. foreach($project['releases'] as $version => $release) {
  530. // Ignore unpublished releases.
  531. if ($release['status'] != 'published') {
  532. continue;
  533. }
  534. // Straight match
  535. if (!isset($recommended_version) && $release['version'] == $request['version']) {
  536. $recommended_version = $version;
  537. }
  538. }
  539. }
  540. else {
  541. // No version specified - try to find the best version we can
  542. foreach($project['releases'] as $version => $release) {
  543. // Ignore unpublished releases.
  544. if ($release['status'] != 'published') {
  545. continue;
  546. }
  547. // If we haven't found a recommended version yet, put the dev
  548. // version as recommended and hope it gets overwritten later.
  549. // Look for the 'latest version' if we haven't found it yet.
  550. // Latest version is defined as the most recent version for the
  551. // default major version.
  552. if (!isset($latest_version) && $release['version_major'] == $project['default_major']) {
  553. $latest_version = $version;
  554. }
  555. if (!isset($recommended_version) && $release['version_major'] == $project['default_major']) {
  556. if ($minor != $release['version_patch']) {
  557. $minor = $release['version_patch'];
  558. $version_patch_changed = $version;
  559. }
  560. if (empty($release['version_extra']) && $minor == $release['version_patch']) {
  561. $recommended_version = $version_patch_changed;
  562. }
  563. continue;
  564. }
  565. }
  566. }
  567. if (isset($recommended_version)) {
  568. return $project['releases'][$recommended_version];
  569. }
  570. else if (isset($latest_version)) {
  571. return $project['releases'][$latest_version];
  572. }
  573. else {
  574. return false;
  575. }
  576. }