From b1191e821b1c3e63ade1af4fba52b0169a9318b3 Mon Sep 17 00:00:00 2001
From: Gordon Heydon <gordon@heydon.com.au>
Date: Fri, 31 Jul 2009 10:52:58 +1000
Subject: [PATCH 2/2] Move of the update.php functions to includes/update.inc

---
 includes/update.inc |   63 +++++++++++++++++++++++++++++++++++++++++++++++++++
 update.php          |   54 +------------------------------------------
 2 files changed, 65 insertions(+), 52 deletions(-)

diff --git includes/update.inc includes/update.inc
index 1aeb165..d4ad221 100644
--- includes/update.inc
+++ includes/update.inc
@@ -339,3 +339,66 @@ function update_do_one($module, $number, &$context) {
   $context['message'] = 'Updating ' . check_plain($module) . ' module';
 }
 
+/**
+ * Start the update batch process
+ *
+ * @param $start
+ *  an array of all the modules and which update to start at.
+ * @param $redirect
+ *  (optional) Path to redirect to when the batch has finished processing.
+ * @param $url
+ *  (optional - should only be used for separate scripts like update.php) URL of the batch processing page.
+ * @param $batch
+ *  (optional) explicitly specify any of the parameters which are going to be passed to the batch api
+ */
+function update_batch($start, $redirect = NULL, $url = NULL, $batch = array()) {
+  global $base_url;
+
+  // During the update, bring the site offline so that schema changes do not
+  // affect visiting users.
+  $_SESSION['site_offline'] = variable_get('site_offline', FALSE);
+  if ($_SESSION['site_offline'] == FALSE) {
+    variable_set('site_offline', TRUE);
+  }
+
+  $operations = array();
+  // Set the installed version so updates start at the correct place.
+  foreach ($start as $module => $version) {
+    drupal_set_installed_schema_version($module, $version - 1);
+    $updates = drupal_get_schema_versions($module);
+    $max_version = max($updates);
+    if ($version <= $max_version) {
+      foreach ($updates as $update) {
+        if ($update >= $version) {
+          $operations[] = array('update_do_one', array($module, $update));
+        }
+      }
+    }
+  }
+  $batch['operations'] = $operations;
+  $batch += array(
+    'title' => 'Updating',
+    'init_message' => 'Starting updates',
+    'error_message' => 'An unrecoverable error has occurred. You can find the error message below. It is advised to copy it to the clipboard for reference.',
+    'finished' => 'update_finished',
+    'file' => '/includes/update.inc',
+  );
+  batch_set($batch);
+  batch_process($redirect, $url);
+}
+
+function update_finished($success, $results, $operations) {
+  // clear the caches in case the data has been updated.
+  drupal_flush_all_caches();
+
+  $_SESSION['update_results'] = $results;
+  $_SESSION['update_success'] = $success;
+  $_SESSION['updates_remaining'] = $operations;
+
+  // Now that the update is done, we can put the site back online if it was
+  // previously turned off.
+  if (isset($_SESSION['site_offline']) && $_SESSION['site_offline'] == FALSE) {
+    variable_set('site_offline', FALSE);
+    unset($_SESSION['site_offline']);
+  }
+}
diff --git update.php update.php
index 6c58ae7..a4914cf 100644
--- update.php
+++ update.php
@@ -120,56 +120,6 @@ function update_script_selection_form() {
   return $form;
 }
 
-function update_batch() {
-  global $base_url;
-
-  // During the update, bring the site offline so that schema changes do not
-  // affect visiting users.
-  $_SESSION['site_offline'] = variable_get('site_offline', FALSE);
-  if ($_SESSION['site_offline'] == FALSE) {
-    variable_set('site_offline', TRUE);
-  }
-
-  $operations = array();
-  // Set the installed version so updates start at the correct place.
-  foreach ($_POST['start'] as $module => $version) {
-    drupal_set_installed_schema_version($module, $version - 1);
-    $updates = drupal_get_schema_versions($module);
-    $max_version = max($updates);
-    if ($version <= $max_version) {
-      foreach ($updates as $update) {
-        if ($update >= $version) {
-          $operations[] = array('update_do_one', array($module, $update));
-        }
-      }
-    }
-  }
-  $batch = array(
-    'operations' => $operations,
-    'title' => 'Updating',
-    'init_message' => 'Starting updates',
-    'error_message' => 'An unrecoverable error has occurred. You can find the error message below. It is advised to copy it to the clipboard for reference.',
-    'finished' => 'update_finished',
-  );
-  batch_set($batch);
-  batch_process($base_url . '/update.php?op=results', $base_url . '/update.php');
-}
-
-function update_finished($success, $results, $operations) {
-  // clear the caches in case the data has been updated.
-  drupal_flush_all_caches();
-
-  $_SESSION['update_results'] = $results;
-  $_SESSION['update_success'] = $success;
-  $_SESSION['updates_remaining'] = $operations;
-
-  // Now that the update is done, we can put the site back online if it was
-  // previously turned off.
-  if (isset($_SESSION['site_offline']) && $_SESSION['site_offline'] == FALSE) {
-    variable_set('site_offline', FALSE);
-    unset($_SESSION['site_offline']);
-  }
-}
 
 function update_helpful_links() {
   // NOTE: we can't use l() here because the URL would point to 'update.php?q=admin'.
@@ -327,7 +277,7 @@ function update_check_requirements() {
 
 // Some unavoidable errors happen because the database is not yet up-to-date.
 // Our custom error handler is not yet installed, so we just suppress them.
-ini_set('display_errors', FALSE);
+//ini_set('display_errors', FALSE);
 
 // We prepare a minimal bootstrap for the update requirements check to avoid
 // reaching the PHP memory limit.
@@ -399,7 +349,7 @@ if ($update_access_allowed) {
 
     case 'Apply pending updates':
       if (isset($_GET['token']) && $_GET['token'] == drupal_get_token('update')) {
-        update_batch();
+        update_batch($_POST['start'], $base_url . '/update.php?op=results', $base_url . '/update.php');
         break;
       }
 
-- 
1.6.4.rc0.17.gd9eb0

