update.authorize.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. * Implements callback_batch_operation().
  93. *
  94. * Copies project to its proper place when authorized to do so.
  95. *
  96. * @param string $project
  97. * The canonical short name of the project being installed.
  98. * @param string $updater_name
  99. * The name of the Updater class to use for installing this project.
  100. * @param string $local_url
  101. * The URL to the locally installed temp directory where the project has
  102. * already been downloaded and extracted into.
  103. * @param FileTransfer $filetransfer
  104. * The FileTransfer object to use for performing this operation.
  105. * @param array $context
  106. * Reference to an array used for Batch API storage.
  107. */
  108. function update_authorize_batch_copy_project($project, $updater_name, $local_url, $filetransfer, &$context) {
  109. // Initialize some variables in the Batch API $context array.
  110. if (!isset($context['results']['log'])) {
  111. $context['results']['log'] = array();
  112. }
  113. if (!isset($context['results']['log'][$project])) {
  114. $context['results']['log'][$project] = array();
  115. }
  116. if (!isset($context['results']['tasks'])) {
  117. $context['results']['tasks'] = array();
  118. }
  119. // The batch API uses a session, and since all the arguments are serialized
  120. // and unserialized between requests, although the FileTransfer object itself
  121. // will be reconstructed, the connection pointer itself will be lost. However,
  122. // the FileTransfer object will still have the connection variable, even
  123. // though the connection itself is now gone. So, although it's ugly, we have
  124. // to unset the connection variable at this point so that the FileTransfer
  125. // object will re-initiate the actual connection.
  126. unset($filetransfer->connection);
  127. if (!empty($context['results']['log'][$project]['#abort'])) {
  128. $context['finished'] = 1;
  129. return;
  130. }
  131. $updater = new $updater_name($local_url);
  132. try {
  133. if ($updater->isInstalled()) {
  134. // This is an update.
  135. $tasks = $updater->update($filetransfer);
  136. }
  137. else {
  138. $tasks = $updater->install($filetransfer);
  139. }
  140. }
  141. catch (UpdaterException $e) {
  142. _update_batch_create_message($context['results']['log'][$project], t('Error installing / updating'), FALSE);
  143. _update_batch_create_message($context['results']['log'][$project], $e->getMessage(), FALSE);
  144. $context['results']['log'][$project]['#abort'] = TRUE;
  145. return;
  146. }
  147. _update_batch_create_message($context['results']['log'][$project], t('Installed %project_name successfully', array('%project_name' => $project)));
  148. if (!empty($tasks)) {
  149. $context['results']['tasks'] += $tasks;
  150. }
  151. // This particular operation is now complete, even though the batch might
  152. // have other operations to perform.
  153. $context['finished'] = 1;
  154. }
  155. /**
  156. * Implements callback_batch_finished().
  157. *
  158. * Performs actions when the authorized update batch is done.
  159. *
  160. * This processes the results and stashes them into SESSION such that
  161. * authorize.php will render a report. Also responsible for putting the site
  162. * back online and clearing the update status cache after a successful update.
  163. *
  164. * @param $success
  165. * TRUE if the batch operation was successful; FALSE if there were errors.
  166. * @param $results
  167. * An associative array of results from the batch operation.
  168. */
  169. function update_authorize_update_batch_finished($success, $results) {
  170. foreach ($results['log'] as $project => $messages) {
  171. if (!empty($messages['#abort'])) {
  172. $success = FALSE;
  173. }
  174. }
  175. $offline = variable_get('maintenance_mode', FALSE);
  176. if ($success) {
  177. // Now that the update completed, we need to clear the cache of available
  178. // update data and recompute our status, so prevent show bogus results.
  179. _update_authorize_clear_update_status();
  180. // Take the site out of maintenance mode if it was previously that way.
  181. if ($offline && isset($_SESSION['maintenance_mode']) && $_SESSION['maintenance_mode'] == FALSE) {
  182. variable_set('maintenance_mode', FALSE);
  183. $page_message = array(
  184. 'message' => t('Update was completed successfully. Your site has been taken out of maintenance mode.'),
  185. 'type' => 'status',
  186. );
  187. }
  188. else {
  189. $page_message = array(
  190. 'message' => t('Update was completed successfully.'),
  191. 'type' => 'status',
  192. );
  193. }
  194. }
  195. elseif (!$offline) {
  196. $page_message = array(
  197. 'message' => t('Update failed! See the log below for more information.'),
  198. 'type' => 'error',
  199. );
  200. }
  201. else {
  202. $page_message = array(
  203. 'message' => t('Update failed! See the log below for more information. Your site is still in maintenance mode.'),
  204. 'type' => 'error',
  205. );
  206. }
  207. // Since we're doing an update of existing code, always add a task for
  208. // running update.php.
  209. $results['tasks'][] = t('Your modules have been downloaded and updated.');
  210. $results['tasks'][] = t('<a href="@update">Run database updates</a>', array('@update' => base_path() . 'update.php'));
  211. // Unset the variable since it is no longer needed.
  212. unset($_SESSION['maintenance_mode']);
  213. // Set all these values into the SESSION so authorize.php can display them.
  214. $_SESSION['authorize_results']['success'] = $success;
  215. $_SESSION['authorize_results']['page_message'] = $page_message;
  216. $_SESSION['authorize_results']['messages'] = $results['log'];
  217. $_SESSION['authorize_results']['tasks'] = $results['tasks'];
  218. $_SESSION['authorize_operation']['page_title'] = t('Update manager');
  219. }
  220. /**
  221. * Implements callback_batch_finished().
  222. *
  223. * Performs actions when the authorized install batch is done.
  224. *
  225. * This processes the results and stashes them into SESSION such that
  226. * authorize.php will render a report. Also responsible for putting the site
  227. * back online after a successful install if necessary.
  228. *
  229. * @param $success
  230. * TRUE if the batch operation was a success; FALSE if there were errors.
  231. * @param $results
  232. * An associative array of results from the batch operation.
  233. */
  234. function update_authorize_install_batch_finished($success, $results) {
  235. foreach ($results['log'] as $project => $messages) {
  236. if (!empty($messages['#abort'])) {
  237. $success = FALSE;
  238. }
  239. }
  240. $offline = variable_get('maintenance_mode', FALSE);
  241. if ($success) {
  242. // Take the site out of maintenance mode if it was previously that way.
  243. if ($offline && isset($_SESSION['maintenance_mode']) && $_SESSION['maintenance_mode'] == FALSE) {
  244. variable_set('maintenance_mode', FALSE);
  245. $page_message = array(
  246. 'message' => t('Installation was completed successfully. Your site has been taken out of maintenance mode.'),
  247. 'type' => 'status',
  248. );
  249. }
  250. else {
  251. $page_message = array(
  252. 'message' => t('Installation was completed successfully.'),
  253. 'type' => 'status',
  254. );
  255. }
  256. }
  257. elseif (!$success && !$offline) {
  258. $page_message = array(
  259. 'message' => t('Installation failed! See the log below for more information.'),
  260. 'type' => 'error',
  261. );
  262. }
  263. else {
  264. $page_message = array(
  265. 'message' => t('Installation failed! See the log below for more information. Your site is still in maintenance mode.'),
  266. 'type' => 'error',
  267. );
  268. }
  269. // Unset the variable since it is no longer needed.
  270. unset($_SESSION['maintenance_mode']);
  271. // Set all these values into the SESSION so authorize.php can display them.
  272. $_SESSION['authorize_results']['success'] = $success;
  273. $_SESSION['authorize_results']['page_message'] = $page_message;
  274. $_SESSION['authorize_results']['messages'] = $results['log'];
  275. $_SESSION['authorize_results']['tasks'] = $results['tasks'];
  276. $_SESSION['authorize_operation']['page_title'] = t('Update manager');
  277. }
  278. /**
  279. * Creates a structure of log messages.
  280. *
  281. * @param array $project_results
  282. * An associative array of results from the batch operation.
  283. * @param string $message
  284. * A string containing a log message.
  285. * @param bool $success
  286. * (optional) TRUE if the operation the message is about was a success, FALSE
  287. * if there were errors. Defaults to TRUE.
  288. */
  289. function _update_batch_create_message(&$project_results, $message, $success = TRUE) {
  290. $project_results[] = array('message' => $message, 'success' => $success);
  291. }
  292. /**
  293. * Clears cached available update status data.
  294. *
  295. * Since this function is run at such a low bootstrap level, the Update Manager
  296. * module is not loaded. So, we can't just call _update_cache_clear(). However,
  297. * the database is bootstrapped, so we can do a query ourselves to clear out
  298. * what we want to clear.
  299. *
  300. * Note that we do not want to just truncate the table, since that would remove
  301. * items related to currently pending fetch attempts.
  302. *
  303. * @see update_authorize_update_batch_finished()
  304. * @see _update_cache_clear()
  305. */
  306. function _update_authorize_clear_update_status() {
  307. $query = db_delete('cache_update');
  308. $query->condition(
  309. db_or()
  310. ->condition('cid', 'update_project_%', 'LIKE')
  311. ->condition('cid', 'available_releases::%', 'LIKE')
  312. );
  313. $query->execute();
  314. }