update.authorize.inc

Callbacks and related functions invoked by authorize.php to update projects.

We use the Batch API to actually update each individual project on the site. All of the code in this file is run at a low bootstrap level (modules are not loaded), so these functions cannot assume access to the rest of the code of the Update Manager module.

File

drupal-7.x/modules/update/update.authorize.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Callbacks and related functions invoked by authorize.php to update projects.
  5. *
  6. * We use the Batch API to actually update each individual project on the site.
  7. * All of the code in this file is run at a low bootstrap level (modules are not
  8. * loaded), so these functions cannot assume access to the rest of the code of
  9. * the Update Manager module.
  10. */
  11. /**
  12. * Updates existing projects when invoked by authorize.php.
  13. *
  14. * Callback for system_authorized_init() in
  15. * update_manager_update_ready_form_submit().
  16. *
  17. * @param $filetransfer
  18. * The FileTransfer object created by authorize.php for use during this
  19. * operation.
  20. * @param $projects
  21. * A nested array of projects to install into the live webroot, keyed by
  22. * project name. Each subarray contains the following keys:
  23. * - project: The canonical project short name.
  24. * - updater_name: The name of the Updater class to use for this project.
  25. * - local_url: The locally installed location of new code to update with.
  26. */
  27. function update_authorize_run_update($filetransfer, $projects) {
  28. $operations = array();
  29. foreach ($projects as $project => $project_info) {
  30. $operations[] = array(
  31. 'update_authorize_batch_copy_project',
  32. array(
  33. $project_info['project'],
  34. $project_info['updater_name'],
  35. $project_info['local_url'],
  36. $filetransfer,
  37. ),
  38. );
  39. }
  40. $batch = array(
  41. 'title' => t('Installing updates'),
  42. 'init_message' => t('Preparing to update your site'),
  43. 'operations' => $operations,
  44. 'finished' => 'update_authorize_update_batch_finished',
  45. 'file' => drupal_get_path('module', 'update') . '/update.authorize.inc',
  46. );
  47. batch_set($batch);
  48. // Invoke the batch via authorize.php.
  49. system_authorized_batch_process();
  50. }
  51. /**
  52. * Installs a new project when invoked by authorize.php.
  53. *
  54. * Callback for system_authorized_init() in
  55. * update_manager_install_form_submit().
  56. *
  57. * @param FileTransfer $filetransfer
  58. * The FileTransfer object created by authorize.php for use during this
  59. * operation.
  60. * @param string $project
  61. * The canonical project short name (e.g., {system}.name).
  62. * @param string $updater_name
  63. * The name of the Updater class to use for installing this project.
  64. * @param string $local_url
  65. * The URL to the locally installed temp directory where the project has
  66. * already been downloaded and extracted into.
  67. */
  68. function update_authorize_run_install($filetransfer, $project, $updater_name, $local_url) {
  69. $operations[] = array(
  70. 'update_authorize_batch_copy_project',
  71. array(
  72. $project,
  73. $updater_name,
  74. $local_url,
  75. $filetransfer,
  76. ),
  77. );
  78. // @todo Instantiate our Updater to set the human-readable title?
  79. $batch = array(
  80. 'title' => t('Installing %project', array('%project' => $project)),
  81. 'init_message' => t('Preparing to install'),
  82. 'operations' => $operations,
  83. // @todo Use a different finished callback for different messages?
  84. 'finished' => 'update_authorize_install_batch_finished',
  85. 'file' => drupal_get_path('module', 'update') . '/update.authorize.inc',
  86. );
  87. batch_set($batch);
  88. // Invoke the batch via authorize.php.
  89. system_authorized_batch_process();
  90. }
  91. /**
  92. * Batch callback: Copies project to its proper place when authorized to do so.
  93. *
  94. * @param string $project
  95. * The canonical short name of the project being installed.
  96. * @param string $updater_name
  97. * The name of the Updater class to use for installing this project.
  98. * @param string $local_url
  99. * The URL to the locally installed temp directory where the project has
  100. * already been downloaded and extracted into.
  101. * @param FileTransfer $filetransfer
  102. * The FileTransfer object to use for performing this operation.
  103. * @param array $context
  104. * Reference to an array used for Batch API storage.
  105. */
  106. function update_authorize_batch_copy_project($project, $updater_name, $local_url, $filetransfer, &$context) {
  107. // Initialize some variables in the Batch API $context array.
  108. if (!isset($context['results']['log'])) {
  109. $context['results']['log'] = array();
  110. }
  111. if (!isset($context['results']['log'][$project])) {
  112. $context['results']['log'][$project] = array();
  113. }
  114. if (!isset($context['results']['tasks'])) {
  115. $context['results']['tasks'] = array();
  116. }
  117. // The batch API uses a session, and since all the arguments are serialized
  118. // and unserialized between requests, although the FileTransfer object itself
  119. // will be reconstructed, the connection pointer itself will be lost. However,
  120. // the FileTransfer object will still have the connection variable, even
  121. // though the connection itself is now gone. So, although it's ugly, we have
  122. // to unset the connection variable at this point so that the FileTransfer
  123. // object will re-initiate the actual connection.
  124. unset($filetransfer->connection);
  125. if (!empty($context['results']['log'][$project]['#abort'])) {
  126. $context['finished'] = 1;
  127. return;
  128. }
  129. $updater = new $updater_name($local_url);
  130. try {
  131. if ($updater->isInstalled()) {
  132. // This is an update.
  133. $tasks = $updater->update($filetransfer);
  134. }
  135. else {
  136. $tasks = $updater->install($filetransfer);
  137. }
  138. }
  139. catch (UpdaterException $e) {
  140. _update_batch_create_message($context['results']['log'][$project], t('Error installing / updating'), FALSE);
  141. _update_batch_create_message($context['results']['log'][$project], $e->getMessage(), FALSE);
  142. $context['results']['log'][$project]['#abort'] = TRUE;
  143. return;
  144. }
  145. _update_batch_create_message($context['results']['log'][$project], t('Installed %project_name successfully', array('%project_name' => $project)));
  146. if (!empty($tasks)) {
  147. $context['results']['tasks'] += $tasks;
  148. }
  149. // This particular operation is now complete, even though the batch might
  150. // have other operations to perform.
  151. $context['finished'] = 1;
  152. }
  153. /**
  154. * Batch callback: Performs actions when the authorized update batch is done.
  155. *
  156. * This processes the results and stashes them into SESSION such that
  157. * authorize.php will render a report. Also responsible for putting the site
  158. * back online and clearing the update status cache after a successful update.
  159. *
  160. * @param $success
  161. * TRUE if the batch operation was successful; FALSE if there were errors.
  162. * @param $results
  163. * An associative array of results from the batch operation.
  164. */
  165. function update_authorize_update_batch_finished($success, $results) {
  166. foreach ($results['log'] as $project => $messages) {
  167. if (!empty($messages['#abort'])) {
  168. $success = FALSE;
  169. }
  170. }
  171. $offline = variable_get('maintenance_mode', FALSE);
  172. if ($success) {
  173. // Now that the update completed, we need to clear the cache of available
  174. // update data and recompute our status, so prevent show bogus results.
  175. _update_authorize_clear_update_status();
  176. // Take the site out of maintenance mode if it was previously that way.
  177. if ($offline && isset($_SESSION['maintenance_mode']) && $_SESSION['maintenance_mode'] == FALSE) {
  178. variable_set('maintenance_mode', FALSE);
  179. $page_message = array(
  180. 'message' => t('Update was completed successfully. Your site has been taken out of maintenance mode.'),
  181. 'type' => 'status',
  182. );
  183. }
  184. else {
  185. $page_message = array(
  186. 'message' => t('Update was completed successfully.'),
  187. 'type' => 'status',
  188. );
  189. }
  190. }
  191. elseif (!$offline) {
  192. $page_message = array(
  193. 'message' => t('Update failed! See the log below for more information.'),
  194. 'type' => 'error',
  195. );
  196. }
  197. else {
  198. $page_message = array(
  199. 'message' => t('Update failed! See the log below for more information. Your site is still in maintenance mode.'),
  200. 'type' => 'error',
  201. );
  202. }
  203. // Since we're doing an update of existing code, always add a task for
  204. // running update.php.
  205. $results['tasks'][] = t('Your modules have been downloaded and updated.');
  206. $results['tasks'][] = t('<a href="@update">Run database updates</a>', array('@update' => base_path() . 'update.php'));
  207. // Unset the variable since it is no longer needed.
  208. unset($_SESSION['maintenance_mode']);
  209. // Set all these values into the SESSION so authorize.php can display them.
  210. $_SESSION['authorize_results']['success'] = $success;
  211. $_SESSION['authorize_results']['page_message'] = $page_message;
  212. $_SESSION['authorize_results']['messages'] = $results['log'];
  213. $_SESSION['authorize_results']['tasks'] = $results['tasks'];
  214. $_SESSION['authorize_operation']['page_title'] = t('Update manager');
  215. }
  216. /**
  217. * Batch callback: Performs actions when the authorized install batch is done.
  218. *
  219. * This processes the results and stashes them into SESSION such that
  220. * authorize.php will render a report. Also responsible for putting the site
  221. * back online after a successful install if necessary.
  222. *
  223. * @param $success
  224. * TRUE if the batch operation was a success; FALSE if there were errors.
  225. * @param $results
  226. * An associative array of results from the batch operation.
  227. */
  228. function update_authorize_install_batch_finished($success, $results) {
  229. foreach ($results['log'] as $project => $messages) {
  230. if (!empty($messages['#abort'])) {
  231. $success = FALSE;
  232. }
  233. }
  234. $offline = variable_get('maintenance_mode', FALSE);
  235. if ($success) {
  236. // Take the site out of maintenance mode if it was previously that way.
  237. if ($offline && isset($_SESSION['maintenance_mode']) && $_SESSION['maintenance_mode'] == FALSE) {
  238. variable_set('maintenance_mode', FALSE);
  239. $page_message = array(
  240. 'message' => t('Installation was completed successfully. Your site has been taken out of maintenance mode.'),
  241. 'type' => 'status',
  242. );
  243. }
  244. else {
  245. $page_message = array(
  246. 'message' => t('Installation was completed successfully.'),
  247. 'type' => 'status',
  248. );
  249. }
  250. }
  251. elseif (!$success && !$offline) {
  252. $page_message = array(
  253. 'message' => t('Installation failed! See the log below for more information.'),
  254. 'type' => 'error',
  255. );
  256. }
  257. else {
  258. $page_message = array(
  259. 'message' => t('Installation failed! See the log below for more information. Your site is still in maintenance mode.'),
  260. 'type' => 'error',
  261. );
  262. }
  263. // Unset the variable since it is no longer needed.
  264. unset($_SESSION['maintenance_mode']);
  265. // Set all these values into the SESSION so authorize.php can display them.
  266. $_SESSION['authorize_results']['success'] = $success;
  267. $_SESSION['authorize_results']['page_message'] = $page_message;
  268. $_SESSION['authorize_results']['messages'] = $results['log'];
  269. $_SESSION['authorize_results']['tasks'] = $results['tasks'];
  270. $_SESSION['authorize_operation']['page_title'] = t('Update manager');
  271. }
  272. /**
  273. * Creates a structure of log messages.
  274. *
  275. * @param array $project_results
  276. * An associative array of results from the batch operation.
  277. * @param string $message
  278. * A string containing a log message.
  279. * @param bool $success
  280. * (optional) TRUE if the operation the message is about was a success, FALSE
  281. * if there were errors. Defaults to TRUE.
  282. */
  283. function _update_batch_create_message(&$project_results, $message, $success = TRUE) {
  284. $project_results[] = array('message' => $message, 'success' => $success);
  285. }
  286. /**
  287. * Clears cached available update status data.
  288. *
  289. * Since this function is run at such a low bootstrap level, the Update Manager
  290. * module is not loaded. So, we can't just call _update_cache_clear(). However,
  291. * the database is bootstrapped, so we can do a query ourselves to clear out
  292. * what we want to clear.
  293. *
  294. * Note that we do not want to just truncate the table, since that would remove
  295. * items related to currently pending fetch attempts.
  296. *
  297. * @see update_authorize_update_batch_finished()
  298. * @see _update_cache_clear()
  299. */
  300. function _update_authorize_clear_update_status() {
  301. $query = db_delete('cache_update');
  302. $query->condition(
  303. db_or()
  304. ->condition('cid', 'update_project_%', 'LIKE')
  305. ->condition('cid', 'available_releases::%', 'LIKE')
  306. );
  307. $query->execute();
  308. }