| webmaster@1 | 1 <?php | 
| webmaster@15 | 2 // $Id: update.php,v 1.252.2.2 2008/12/10 22:30:13 goba Exp $ | 
| webmaster@1 | 3 | 
| webmaster@1 | 4 /** | 
| webmaster@1 | 5  * @file | 
| webmaster@1 | 6  * Administrative page for handling updates from one Drupal version to another. | 
| webmaster@1 | 7  * | 
| webmaster@1 | 8  * Point your browser to "http://www.example.com/update.php" and follow the | 
| webmaster@1 | 9  * instructions. | 
| webmaster@1 | 10  * | 
| webmaster@1 | 11  * If you are not logged in as administrator, you will need to modify the access | 
| webmaster@1 | 12  * check statement inside your settings.php file. After finishing the upgrade, | 
| webmaster@1 | 13  * be sure to open settings.php again, and change it back to its original state! | 
| webmaster@1 | 14  */ | 
| webmaster@1 | 15 | 
| webmaster@1 | 16 /** | 
| webmaster@1 | 17  * Global flag to identify update.php run, and so avoid various unwanted | 
| webmaster@1 | 18  * operations, such as hook_init() and hook_exit() invokes, css/js preprocessing | 
| webmaster@1 | 19  * and translation, and solve some theming issues. This flag is checked on several | 
| webmaster@1 | 20  * places in Drupal code (not just update.php). | 
| webmaster@1 | 21  */ | 
| webmaster@1 | 22 define('MAINTENANCE_MODE', 'update'); | 
| webmaster@1 | 23 | 
| webmaster@1 | 24 /** | 
| webmaster@1 | 25  * Add a column to a database using syntax appropriate for PostgreSQL. | 
| webmaster@1 | 26  * Save result of SQL commands in $ret array. | 
| webmaster@1 | 27  * | 
| webmaster@1 | 28  * Note: when you add a column with NOT NULL and you are not sure if there are | 
| webmaster@1 | 29  * already rows in the table, you MUST also add DEFAULT. Otherwise PostgreSQL | 
| webmaster@1 | 30  * won't work when the table is not empty, and db_add_column() will fail. | 
| webmaster@1 | 31  * To have an empty string as the default, you must use: 'default' => "''" | 
| webmaster@1 | 32  * in the $attributes array. If NOT NULL and DEFAULT are set the PostgreSQL | 
| webmaster@1 | 33  * version will set values of the added column in old rows to the | 
| webmaster@1 | 34  * DEFAULT value. | 
| webmaster@1 | 35  * | 
| webmaster@1 | 36  * @param $ret | 
| webmaster@1 | 37  *   Array to which results will be added. | 
| webmaster@1 | 38  * @param $table | 
| webmaster@1 | 39  *   Name of the table, without {} | 
| webmaster@1 | 40  * @param $column | 
| webmaster@1 | 41  *   Name of the column | 
| webmaster@1 | 42  * @param $type | 
| webmaster@1 | 43  *   Type of column | 
| webmaster@1 | 44  * @param $attributes | 
| webmaster@1 | 45  *   Additional optional attributes. Recognized attributes: | 
| webmaster@1 | 46  *     not null => TRUE|FALSE | 
| webmaster@1 | 47  *     default  => NULL|FALSE|value (the value must be enclosed in '' marks) | 
| webmaster@1 | 48  * @return | 
| webmaster@1 | 49  *   nothing, but modifies $ret parameter. | 
| webmaster@1 | 50  */ | 
| webmaster@1 | 51 function db_add_column(&$ret, $table, $column, $type, $attributes = array()) { | 
| webmaster@1 | 52   if (array_key_exists('not null', $attributes) and $attributes['not null']) { | 
| webmaster@1 | 53     $not_null = 'NOT NULL'; | 
| webmaster@1 | 54   } | 
| webmaster@1 | 55   if (array_key_exists('default', $attributes)) { | 
| webmaster@1 | 56     if (is_null($attributes['default'])) { | 
| webmaster@1 | 57       $default_val = 'NULL'; | 
| webmaster@1 | 58       $default = 'default NULL'; | 
| webmaster@1 | 59     } | 
| webmaster@1 | 60     elseif ($attributes['default'] === FALSE) { | 
| webmaster@1 | 61       $default = ''; | 
| webmaster@1 | 62     } | 
| webmaster@1 | 63     else { | 
| webmaster@1 | 64       $default_val = "$attributes[default]"; | 
| webmaster@1 | 65       $default = "default $attributes[default]"; | 
| webmaster@1 | 66     } | 
| webmaster@1 | 67   } | 
| webmaster@1 | 68 | 
| webmaster@1 | 69   $ret[] = update_sql("ALTER TABLE {". $table ."} ADD $column $type"); | 
| webmaster@1 | 70   if (!empty($default)) { | 
| webmaster@1 | 71     $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column SET $default"); | 
| webmaster@1 | 72   } | 
| webmaster@1 | 73   if (!empty($not_null)) { | 
| webmaster@1 | 74     if (!empty($default)) { | 
| webmaster@1 | 75       $ret[] = update_sql("UPDATE {". $table ."} SET $column = $default_val"); | 
| webmaster@1 | 76     } | 
| webmaster@1 | 77     $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column SET NOT NULL"); | 
| webmaster@1 | 78   } | 
| webmaster@1 | 79 } | 
| webmaster@1 | 80 | 
| webmaster@1 | 81 /** | 
| webmaster@1 | 82  * Change a column definition using syntax appropriate for PostgreSQL. | 
| webmaster@1 | 83  * Save result of SQL commands in $ret array. | 
| webmaster@1 | 84  * | 
| webmaster@1 | 85  * Remember that changing a column definition involves adding a new column | 
| webmaster@1 | 86  * and dropping an old one. This means that any indices, primary keys and | 
| webmaster@1 | 87  * sequences from serial-type columns are dropped and might need to be | 
| webmaster@1 | 88  * recreated. | 
| webmaster@1 | 89  * | 
| webmaster@1 | 90  * @param $ret | 
| webmaster@1 | 91  *   Array to which results will be added. | 
| webmaster@1 | 92  * @param $table | 
| webmaster@1 | 93  *   Name of the table, without {} | 
| webmaster@1 | 94  * @param $column | 
| webmaster@1 | 95  *   Name of the column to change | 
| webmaster@1 | 96  * @param $column_new | 
| webmaster@1 | 97  *   New name for the column (set to the same as $column if you don't want to change the name) | 
| webmaster@1 | 98  * @param $type | 
| webmaster@1 | 99  *   Type of column | 
| webmaster@1 | 100  * @param $attributes | 
| webmaster@1 | 101  *   Additional optional attributes. Recognized attributes: | 
| webmaster@1 | 102  *     not null => TRUE|FALSE | 
| webmaster@1 | 103  *     default  => NULL|FALSE|value (with or without '', it won't be added) | 
| webmaster@1 | 104  * @return | 
| webmaster@1 | 105  *   nothing, but modifies $ret parameter. | 
| webmaster@1 | 106  */ | 
| webmaster@1 | 107 function db_change_column(&$ret, $table, $column, $column_new, $type, $attributes = array()) { | 
| webmaster@1 | 108   if (array_key_exists('not null', $attributes) and $attributes['not null']) { | 
| webmaster@1 | 109     $not_null = 'NOT NULL'; | 
| webmaster@1 | 110   } | 
| webmaster@1 | 111   if (array_key_exists('default', $attributes)) { | 
| webmaster@1 | 112     if (is_null($attributes['default'])) { | 
| webmaster@1 | 113       $default_val = 'NULL'; | 
| webmaster@1 | 114       $default = 'default NULL'; | 
| webmaster@1 | 115     } | 
| webmaster@1 | 116     elseif ($attributes['default'] === FALSE) { | 
| webmaster@1 | 117       $default = ''; | 
| webmaster@1 | 118     } | 
| webmaster@1 | 119     else { | 
| webmaster@1 | 120       $default_val = "$attributes[default]"; | 
| webmaster@1 | 121       $default = "default $attributes[default]"; | 
| webmaster@1 | 122     } | 
| webmaster@1 | 123   } | 
| webmaster@1 | 124 | 
| webmaster@1 | 125   $ret[] = update_sql("ALTER TABLE {". $table ."} RENAME $column TO ". $column ."_old"); | 
| webmaster@1 | 126   $ret[] = update_sql("ALTER TABLE {". $table ."} ADD $column_new $type"); | 
| webmaster@1 | 127   $ret[] = update_sql("UPDATE {". $table ."} SET $column_new = ". $column ."_old"); | 
| webmaster@1 | 128   if ($default) { $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column_new SET $default"); } | 
| webmaster@1 | 129   if ($not_null) { $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column_new SET NOT NULL"); } | 
| webmaster@1 | 130   $ret[] = update_sql("ALTER TABLE {". $table ."} DROP ". $column ."_old"); | 
| webmaster@1 | 131 } | 
| webmaster@1 | 132 | 
| webmaster@1 | 133 /** | 
| webmaster@1 | 134  * Perform one update and store the results which will later be displayed on | 
| webmaster@1 | 135  * the finished page. | 
| webmaster@1 | 136  * | 
| webmaster@1 | 137  * An update function can force the current and all later updates for this | 
| webmaster@1 | 138  * module to abort by returning a $ret array with an element like: | 
| webmaster@1 | 139  * $ret['#abort'] = array('success' => FALSE, 'query' => 'What went wrong'); | 
| webmaster@1 | 140  * The schema version will not be updated in this case, and all the | 
| webmaster@1 | 141  * aborted updates will continue to appear on update.php as updates that | 
| webmaster@1 | 142  * have not yet been run. | 
| webmaster@1 | 143  * | 
| webmaster@1 | 144  * @param $module | 
| webmaster@1 | 145  *   The module whose update will be run. | 
| webmaster@1 | 146  * @param $number | 
| webmaster@1 | 147  *   The update number to run. | 
| webmaster@1 | 148  * @param $context | 
| webmaster@1 | 149  *   The batch context array | 
| webmaster@1 | 150  */ | 
| webmaster@1 | 151 function update_do_one($module, $number, &$context) { | 
| webmaster@1 | 152   // If updates for this module have been aborted | 
| webmaster@1 | 153   // in a previous step, go no further. | 
| webmaster@1 | 154   if (!empty($context['results'][$module]['#abort'])) { | 
| webmaster@1 | 155     return; | 
| webmaster@1 | 156   } | 
| webmaster@1 | 157 | 
| webmaster@1 | 158   $function = $module .'_update_'. $number; | 
| webmaster@1 | 159   if (function_exists($function)) { | 
| webmaster@1 | 160     $ret = $function($context['sandbox']); | 
| webmaster@1 | 161   } | 
| webmaster@1 | 162 | 
| webmaster@1 | 163   if (isset($ret['#finished'])) { | 
| webmaster@1 | 164     $context['finished'] = $ret['#finished']; | 
| webmaster@1 | 165     unset($ret['#finished']); | 
| webmaster@1 | 166   } | 
| webmaster@1 | 167 | 
| webmaster@1 | 168   if (!isset($context['results'][$module])) { | 
| webmaster@1 | 169     $context['results'][$module] = array(); | 
| webmaster@1 | 170   } | 
| webmaster@1 | 171   if (!isset($context['results'][$module][$number])) { | 
| webmaster@1 | 172     $context['results'][$module][$number] = array(); | 
| webmaster@1 | 173   } | 
| webmaster@1 | 174   $context['results'][$module][$number] = array_merge($context['results'][$module][$number], $ret); | 
| webmaster@1 | 175 | 
| webmaster@1 | 176   if (!empty($ret['#abort'])) { | 
| webmaster@1 | 177     $context['results'][$module]['#abort'] = TRUE; | 
| webmaster@1 | 178   } | 
| webmaster@1 | 179   // Record the schema update if it was completed successfully. | 
| webmaster@1 | 180   if ($context['finished'] == 1 && empty($context['results'][$module]['#abort'])) { | 
| webmaster@1 | 181     drupal_set_installed_schema_version($module, $number); | 
| webmaster@1 | 182   } | 
| webmaster@1 | 183 | 
| webmaster@1 | 184   $context['message'] = 'Updating '. check_plain($module) .' module'; | 
| webmaster@1 | 185 } | 
| webmaster@1 | 186 | 
| webmaster@1 | 187 function update_selection_page() { | 
| webmaster@1 | 188   $output = '<p>The version of Drupal you are updating from has been automatically detected. You can select a different version, but you should not need to.</p>'; | 
| webmaster@1 | 189   $output .= '<p>Click Update to start the update process.</p>'; | 
| webmaster@1 | 190 | 
| webmaster@1 | 191   drupal_set_title('Drupal database update'); | 
| webmaster@1 | 192   $output .= drupal_get_form('update_script_selection_form'); | 
| webmaster@1 | 193 | 
| webmaster@1 | 194   update_task_list('select'); | 
| webmaster@1 | 195 | 
| webmaster@1 | 196   return $output; | 
| webmaster@1 | 197 } | 
| webmaster@1 | 198 | 
| webmaster@1 | 199 function update_script_selection_form() { | 
| webmaster@1 | 200   $form = array(); | 
| webmaster@1 | 201   $form['start'] = array( | 
| webmaster@1 | 202     '#tree' => TRUE, | 
| webmaster@1 | 203     '#type' => 'fieldset', | 
| webmaster@1 | 204     '#title' => 'Select versions', | 
| webmaster@1 | 205     '#collapsible' => TRUE, | 
| webmaster@1 | 206     '#collapsed' => TRUE, | 
| webmaster@1 | 207   ); | 
| webmaster@1 | 208 | 
| webmaster@1 | 209   // Ensure system.module's updates appear first | 
| webmaster@1 | 210   $form['start']['system'] = array(); | 
| webmaster@1 | 211 | 
| webmaster@1 | 212   $modules = drupal_get_installed_schema_version(NULL, FALSE, TRUE); | 
| webmaster@1 | 213   foreach ($modules as $module => $schema_version) { | 
| webmaster@1 | 214     $updates = drupal_get_schema_versions($module); | 
| webmaster@1 | 215     // Skip incompatible module updates completely, otherwise test schema versions. | 
| webmaster@1 | 216     if (!update_check_incompatibility($module) && $updates !== FALSE && $schema_version >= 0) { | 
| webmaster@1 | 217       // module_invoke returns NULL for nonexisting hooks, so if no updates | 
| webmaster@1 | 218       // are removed, it will == 0. | 
| webmaster@1 | 219       $last_removed = module_invoke($module, 'update_last_removed'); | 
| webmaster@1 | 220       if ($schema_version < $last_removed) { | 
| webmaster@1 | 221         $form['start'][$module] = array( | 
| webmaster@1 | 222           '#value'  => '<em>'. $module .'</em> module can not be updated. Its schema version is '. $schema_version .'. Updates up to and including '. $last_removed .' have been removed in this release. In order to update <em>'. $module .'</em> module, you will first <a href="http://drupal.org/upgrade">need to upgrade</a> to the last version in which these updates were available.', | 
| webmaster@1 | 223           '#prefix' => '<div class="warning">', | 
| webmaster@1 | 224           '#suffix' => '</div>', | 
| webmaster@1 | 225         ); | 
| webmaster@1 | 226         $form['start']['#collapsed'] = FALSE; | 
| webmaster@1 | 227         continue; | 
| webmaster@1 | 228       } | 
| webmaster@1 | 229       $updates = drupal_map_assoc($updates); | 
| webmaster@1 | 230       $updates[] = 'No updates available'; | 
| webmaster@1 | 231       $default = $schema_version; | 
| webmaster@1 | 232       foreach (array_keys($updates) as $update) { | 
| webmaster@1 | 233         if ($update > $schema_version) { | 
| webmaster@1 | 234           $default = $update; | 
| webmaster@1 | 235           break; | 
| webmaster@1 | 236         } | 
| webmaster@1 | 237       } | 
| webmaster@1 | 238       $form['start'][$module] = array( | 
| webmaster@1 | 239         '#type' => 'select', | 
| webmaster@1 | 240         '#title' => $module .' module', | 
| webmaster@1 | 241         '#default_value' => $default, | 
| webmaster@1 | 242         '#options' => $updates, | 
| webmaster@1 | 243       ); | 
| webmaster@1 | 244     } | 
| webmaster@1 | 245   } | 
| webmaster@1 | 246 | 
| webmaster@1 | 247   $form['has_js'] = array( | 
| webmaster@1 | 248     '#type' => 'hidden', | 
| webmaster@1 | 249     '#default_value' => FALSE, | 
| webmaster@1 | 250     '#attributes' => array('id' => 'edit-has_js'), | 
| webmaster@1 | 251   ); | 
| webmaster@1 | 252   $form['submit'] = array( | 
| webmaster@1 | 253     '#type' => 'submit', | 
| webmaster@1 | 254     '#value' => 'Update', | 
| webmaster@1 | 255   ); | 
| webmaster@1 | 256   return $form; | 
| webmaster@1 | 257 } | 
| webmaster@1 | 258 | 
| webmaster@1 | 259 function update_batch() { | 
| webmaster@1 | 260   global $base_url; | 
| webmaster@1 | 261 | 
| webmaster@1 | 262   $operations = array(); | 
| webmaster@1 | 263   // Set the installed version so updates start at the correct place. | 
| webmaster@1 | 264   foreach ($_POST['start'] as $module => $version) { | 
| webmaster@1 | 265     drupal_set_installed_schema_version($module, $version - 1); | 
| webmaster@1 | 266     $updates = drupal_get_schema_versions($module); | 
| webmaster@1 | 267     $max_version = max($updates); | 
| webmaster@1 | 268     if ($version <= $max_version) { | 
| webmaster@1 | 269       foreach ($updates as $update) { | 
| webmaster@1 | 270         if ($update >= $version) { | 
| webmaster@1 | 271           $operations[] = array('update_do_one', array($module, $update)); | 
| webmaster@1 | 272         } | 
| webmaster@1 | 273       } | 
| webmaster@1 | 274     } | 
| webmaster@1 | 275   } | 
| webmaster@1 | 276   $batch = array( | 
| webmaster@1 | 277     'operations' => $operations, | 
| webmaster@1 | 278     'title' => 'Updating', | 
| webmaster@1 | 279     'init_message' => 'Starting updates', | 
| webmaster@1 | 280     '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.', | 
| webmaster@1 | 281     'finished' => 'update_finished', | 
| webmaster@1 | 282   ); | 
| webmaster@1 | 283   batch_set($batch); | 
| webmaster@1 | 284   batch_process($base_url .'/update.php?op=results', $base_url .'/update.php'); | 
| webmaster@1 | 285 } | 
| webmaster@1 | 286 | 
| webmaster@1 | 287 function update_finished($success, $results, $operations) { | 
| webmaster@1 | 288   // clear the caches in case the data has been updated. | 
| webmaster@1 | 289   drupal_flush_all_caches(); | 
| webmaster@1 | 290 | 
| webmaster@1 | 291   $_SESSION['update_results'] = $results; | 
| webmaster@1 | 292   $_SESSION['update_success'] = $success; | 
| webmaster@1 | 293   $_SESSION['updates_remaining'] = $operations; | 
| webmaster@1 | 294 } | 
| webmaster@1 | 295 | 
| webmaster@1 | 296 function update_results_page() { | 
| webmaster@1 | 297   drupal_set_title('Drupal database update'); | 
| webmaster@1 | 298   // NOTE: we can't use l() here because the URL would point to 'update.php?q=admin'. | 
| webmaster@1 | 299   $links[] = '<a href="'. base_path() .'">Main page</a>'; | 
| webmaster@1 | 300   $links[] = '<a href="'. base_path() .'?q=admin">Administration pages</a>'; | 
| webmaster@1 | 301 | 
| webmaster@1 | 302   update_task_list(); | 
| webmaster@1 | 303   // Report end result | 
| webmaster@1 | 304   if (module_exists('dblog')) { | 
| webmaster@1 | 305     $log_message = ' All errors have been <a href="'. base_path() .'?q=admin/reports/dblog">logged</a>.'; | 
| webmaster@1 | 306   } | 
| webmaster@1 | 307   else { | 
| webmaster@1 | 308     $log_message = ' All errors have been logged.'; | 
| webmaster@1 | 309   } | 
| webmaster@1 | 310 | 
| webmaster@1 | 311   if ($_SESSION['update_success']) { | 
| webmaster@1 | 312     $output = '<p>Updates were attempted. If you see no failures below, you may proceed happily to the <a href="'. base_path() .'?q=admin">administration pages</a>. Otherwise, you may need to update your database manually.'. $log_message .'</p>'; | 
| webmaster@1 | 313   } | 
| webmaster@1 | 314   else { | 
| webmaster@1 | 315     list($module, $version) = array_pop(reset($_SESSION['updates_remaining'])); | 
| webmaster@1 | 316     $output = '<p class="error">The update process was aborted prematurely while running <strong>update #'. $version .' in '. $module .'.module</strong>.'. $log_message; | 
| webmaster@1 | 317     if (module_exists('dblog')) { | 
| webmaster@1 | 318       $output .= ' You may need to check the <code>watchdog</code> database table manually.'; | 
| webmaster@1 | 319     } | 
| webmaster@1 | 320     $output .= '</p>'; | 
| webmaster@1 | 321   } | 
| webmaster@1 | 322 | 
| webmaster@1 | 323   if (!empty($GLOBALS['update_free_access'])) { | 
| webmaster@1 | 324     $output .= "<p><strong>Reminder: don't forget to set the <code>\$update_free_access</code> value in your <code>settings.php</code> file back to <code>FALSE</code>.</strong></p>"; | 
| webmaster@1 | 325   } | 
| webmaster@1 | 326 | 
| webmaster@1 | 327   $output .= theme('item_list', $links); | 
| webmaster@1 | 328 | 
| webmaster@1 | 329   // Output a list of queries executed | 
| webmaster@1 | 330   if (!empty($_SESSION['update_results'])) { | 
| webmaster@1 | 331     $output .= '<div id="update-results">'; | 
| webmaster@1 | 332     $output .= '<h2>The following queries were executed</h2>'; | 
| webmaster@1 | 333     foreach ($_SESSION['update_results'] as $module => $updates) { | 
| webmaster@1 | 334       $output .= '<h3>'. $module .' module</h3>'; | 
| webmaster@1 | 335       foreach ($updates as $number => $queries) { | 
| webmaster@1 | 336         if ($number != '#abort') { | 
| webmaster@1 | 337           $output .= '<h4>Update #'. $number .'</h4>'; | 
| webmaster@1 | 338           $output .= '<ul>'; | 
| webmaster@1 | 339           foreach ($queries as $query) { | 
| webmaster@1 | 340             if ($query['success']) { | 
| webmaster@1 | 341               $output .= '<li class="success">'. $query['query'] .'</li>'; | 
| webmaster@1 | 342             } | 
| webmaster@1 | 343             else { | 
| webmaster@1 | 344               $output .= '<li class="failure"><strong>Failed:</strong> '. $query['query'] .'</li>'; | 
| webmaster@1 | 345             } | 
| webmaster@1 | 346           } | 
| webmaster@1 | 347           if (!count($queries)) { | 
| webmaster@1 | 348             $output .= '<li class="none">No queries</li>'; | 
| webmaster@1 | 349           } | 
| webmaster@1 | 350         } | 
| webmaster@1 | 351         $output .= '</ul>'; | 
| webmaster@1 | 352       } | 
| webmaster@1 | 353     } | 
| webmaster@1 | 354     $output .= '</div>'; | 
| webmaster@1 | 355   } | 
| webmaster@1 | 356   unset($_SESSION['update_results']); | 
| webmaster@1 | 357   unset($_SESSION['update_success']); | 
| webmaster@1 | 358 | 
| webmaster@1 | 359   return $output; | 
| webmaster@1 | 360 } | 
| webmaster@1 | 361 | 
| webmaster@1 | 362 function update_info_page() { | 
| webmaster@1 | 363   // Change query-strings on css/js files to enforce reload for all users. | 
| webmaster@1 | 364   _drupal_flush_css_js(); | 
| webmaster@1 | 365   // Flush the cache of all data for the update status module. | 
| webmaster@1 | 366   if (db_table_exists('cache_update')) { | 
| webmaster@1 | 367     cache_clear_all('*', 'cache_update', TRUE); | 
| webmaster@1 | 368   } | 
| webmaster@1 | 369 | 
| webmaster@1 | 370   update_task_list('info'); | 
| webmaster@1 | 371   drupal_set_title('Drupal database update'); | 
| webmaster@15 | 372   $token = drupal_get_token('update'); | 
| webmaster@1 | 373   $output = '<p>Use this utility to update your database whenever a new release of Drupal or a module is installed.</p><p>For more detailed information, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.</p>'; | 
| webmaster@1 | 374   $output .= "<ol>\n"; | 
| webmaster@1 | 375   $output .= "<li><strong>Back up your database</strong>. This process will change your database values and in case of emergency you may need to revert to a backup.</li>\n"; | 
| webmaster@1 | 376   $output .= "<li><strong>Back up your code</strong>. Hint: when backing up module code, do not leave that backup in the 'modules' or 'sites/*/modules' directories as this may confuse Drupal's auto-discovery mechanism.</li>\n"; | 
| webmaster@1 | 377   $output .= '<li>Put your site into <a href="'. base_path() .'?q=admin/settings/site-maintenance">maintenance mode</a>.</li>'."\n"; | 
| webmaster@1 | 378   $output .= "<li>Install your new files in the appropriate location, as described in the handbook.</li>\n"; | 
| webmaster@1 | 379   $output .= "</ol>\n"; | 
| webmaster@1 | 380   $output .= "<p>When you have performed the steps above, you may proceed.</p>\n"; | 
| webmaster@15 | 381   $output .= '<form method="post" action="update.php?op=selection&token='. $token .'"><input type="submit" value="Continue" /></form>'; | 
| webmaster@1 | 382   $output .= "\n"; | 
| webmaster@1 | 383   return $output; | 
| webmaster@1 | 384 } | 
| webmaster@1 | 385 | 
| webmaster@1 | 386 function update_access_denied_page() { | 
| webmaster@1 | 387   drupal_set_title('Access denied'); | 
| webmaster@1 | 388   return '<p>Access denied. You are not authorized to access this page. Please log in as the admin user (the first user you created). If you cannot log in, you will have to edit <code>settings.php</code> to bypass this access check. To do this:</p> | 
| webmaster@1 | 389 <ol> | 
| webmaster@1 | 390  <li>With a text editor find the settings.php file on your system. From the main Drupal directory that you installed all the files into, go to <code>sites/your_site_name</code> if such directory exists, or else to <code>sites/default</code> which applies otherwise.</li> | 
| webmaster@1 | 391  <li>There is a line inside your settings.php file that says <code>$update_free_access = FALSE;</code>. Change it to <code>$update_free_access = TRUE;</code>.</li> | 
| webmaster@1 | 392  <li>As soon as the update.php script is done, you must change the settings.php file back to its original form with <code>$update_free_access = FALSE;</code>.</li> | 
| webmaster@1 | 393  <li>To avoid having this problem in future, remember to log in to your website as the admin user (the user you first created) before you backup your database at the beginning of the update process.</li> | 
| webmaster@1 | 394 </ol>'; | 
| webmaster@1 | 395 } | 
| webmaster@1 | 396 | 
| webmaster@1 | 397 /** | 
| webmaster@1 | 398  * Create the batch table. | 
| webmaster@1 | 399  * | 
| webmaster@1 | 400  * This is part of the Drupal 5.x to 6.x migration. | 
| webmaster@1 | 401  */ | 
| webmaster@1 | 402 function update_create_batch_table() { | 
| webmaster@1 | 403 | 
| webmaster@1 | 404   // If batch table exists, update is not necessary | 
| webmaster@1 | 405   if (db_table_exists('batch')) { | 
| webmaster@1 | 406     return; | 
| webmaster@1 | 407   } | 
| webmaster@1 | 408 | 
| webmaster@1 | 409   $schema['batch'] = array( | 
| webmaster@1 | 410     'fields' => array( | 
| webmaster@1 | 411       'bid'       => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE), | 
| webmaster@1 | 412       'token'     => array('type' => 'varchar', 'length' => 64, 'not null' => TRUE), | 
| webmaster@1 | 413       'timestamp' => array('type' => 'int', 'not null' => TRUE), | 
| webmaster@1 | 414       'batch'     => array('type' => 'text', 'not null' => FALSE, 'size' => 'big') | 
| webmaster@1 | 415     ), | 
| webmaster@1 | 416     'primary key' => array('bid'), | 
| webmaster@1 | 417     'indexes' => array('token' => array('token')), | 
| webmaster@1 | 418   ); | 
| webmaster@1 | 419 | 
| webmaster@1 | 420   $ret = array(); | 
| webmaster@1 | 421   db_create_table($ret, 'batch', $schema['batch']); | 
| webmaster@1 | 422   return $ret; | 
| webmaster@1 | 423 } | 
| webmaster@1 | 424 | 
| webmaster@1 | 425 /** | 
| webmaster@1 | 426  * Disable anything in the {system} table that is not compatible with the | 
| webmaster@1 | 427  * current version of Drupal core. | 
| webmaster@1 | 428  */ | 
| webmaster@1 | 429 function update_fix_compatibility() { | 
| webmaster@1 | 430   $ret = array(); | 
| webmaster@1 | 431   $incompatible = array(); | 
| webmaster@1 | 432   $query = db_query("SELECT name, type, status FROM {system} WHERE status = 1 AND type IN ('module','theme')"); | 
| webmaster@1 | 433   while ($result = db_fetch_object($query)) { | 
| webmaster@1 | 434     if (update_check_incompatibility($result->name, $result->type)) { | 
| webmaster@1 | 435       $incompatible[] = $result->name; | 
| webmaster@1 | 436     } | 
| webmaster@1 | 437   } | 
| webmaster@1 | 438   if (!empty($incompatible)) { | 
| webmaster@1 | 439     $ret[] = update_sql("UPDATE {system} SET status = 0 WHERE name IN ('". implode("','", $incompatible) ."')"); | 
| webmaster@1 | 440   } | 
| webmaster@1 | 441   return $ret; | 
| webmaster@1 | 442 } | 
| webmaster@1 | 443 | 
| webmaster@1 | 444 /** | 
| webmaster@1 | 445  * Helper function to test compatibility of a module or theme. | 
| webmaster@1 | 446  */ | 
| webmaster@1 | 447 function update_check_incompatibility($name, $type = 'module') { | 
| webmaster@1 | 448   static $themes, $modules; | 
| webmaster@1 | 449 | 
| webmaster@1 | 450   // Store values of expensive functions for future use. | 
| webmaster@1 | 451   if (empty($themes) || empty($modules)) { | 
| webmaster@15 | 452     $themes = _system_theme_data(); | 
| webmaster@1 | 453     $modules = module_rebuild_cache(); | 
| webmaster@1 | 454   } | 
| webmaster@1 | 455 | 
| webmaster@1 | 456   if ($type == 'module' && isset($modules[$name])) { | 
| webmaster@1 | 457     $file = $modules[$name]; | 
| webmaster@1 | 458   } | 
| webmaster@1 | 459   else if ($type == 'theme' && isset($themes[$name])) { | 
| webmaster@1 | 460     $file = $themes[$name]; | 
| webmaster@1 | 461   } | 
| webmaster@1 | 462   if (!isset($file) | 
| webmaster@1 | 463       || !isset($file->info['core']) | 
| webmaster@1 | 464       || $file->info['core'] != DRUPAL_CORE_COMPATIBILITY | 
| webmaster@1 | 465       || version_compare(phpversion(), $file->info['php']) < 0) { | 
| webmaster@1 | 466     return TRUE; | 
| webmaster@1 | 467   } | 
| webmaster@1 | 468   return FALSE; | 
| webmaster@1 | 469 } | 
| webmaster@1 | 470 | 
| webmaster@1 | 471 /** | 
| webmaster@1 | 472  * Perform Drupal 5.x to 6.x updates that are required for update.php | 
| webmaster@1 | 473  * to function properly. | 
| webmaster@1 | 474  * | 
| webmaster@1 | 475  * This function runs when update.php is run the first time for 6.x, | 
| webmaster@1 | 476  * even before updates are selected or performed.  It is important | 
| webmaster@1 | 477  * that if updates are not ultimately performed that no changes are | 
| webmaster@1 | 478  * made which make it impossible to continue using the prior version. | 
| webmaster@1 | 479  * Just adding columns is safe.  However, renaming the | 
| webmaster@1 | 480  * system.description column to owner is not.  Therefore, we add the | 
| webmaster@1 | 481  * system.owner column and leave it to system_update_6008() to copy | 
| webmaster@1 | 482  * the data from description and remove description. The same for | 
| webmaster@1 | 483  * renaming locales_target.locale to locales_target.language, which | 
| webmaster@1 | 484  * will be finished by locale_update_6002(). | 
| webmaster@1 | 485  */ | 
| webmaster@1 | 486 function update_fix_d6_requirements() { | 
| webmaster@1 | 487   $ret = array(); | 
| webmaster@1 | 488 | 
| webmaster@1 | 489   if (drupal_get_installed_schema_version('system') < 6000 && !variable_get('update_d6_requirements', FALSE)) { | 
| webmaster@1 | 490     $spec = array('type' => 'int', 'size' => 'small', 'default' => 0, 'not null' => TRUE); | 
| webmaster@1 | 491     db_add_field($ret, 'cache', 'serialized', $spec); | 
| webmaster@1 | 492     db_add_field($ret, 'cache_filter', 'serialized', $spec); | 
| webmaster@1 | 493     db_add_field($ret, 'cache_page', 'serialized', $spec); | 
| webmaster@1 | 494     db_add_field($ret, 'cache_menu', 'serialized', $spec); | 
| webmaster@1 | 495 | 
| webmaster@1 | 496     db_add_field($ret, 'system', 'info', array('type' => 'text')); | 
| webmaster@1 | 497     db_add_field($ret, 'system', 'owner', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '')); | 
| webmaster@1 | 498     if (db_table_exists('locales_target')) { | 
| webmaster@1 | 499       db_add_field($ret, 'locales_target', 'language', array('type' => 'varchar', 'length' => 12, 'not null' => TRUE, 'default' => '')); | 
| webmaster@1 | 500     } | 
| webmaster@1 | 501     if (db_table_exists('locales_source')) { | 
| webmaster@1 | 502       db_add_field($ret, 'locales_source', 'textgroup', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => 'default')); | 
| webmaster@1 | 503       db_add_field($ret, 'locales_source', 'version', array('type' => 'varchar', 'length' => 20, 'not null' => TRUE, 'default' => 'none')); | 
| webmaster@1 | 504     } | 
| webmaster@1 | 505     variable_set('update_d6_requirements', TRUE); | 
| webmaster@1 | 506 | 
| webmaster@1 | 507     // Create the cache_block table. See system_update_6027() for more details. | 
| webmaster@1 | 508     $schema['cache_block'] = array( | 
| webmaster@1 | 509       'fields' => array( | 
| webmaster@1 | 510         'cid'        => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''), | 
| webmaster@1 | 511         'data'       => array('type' => 'blob', 'not null' => FALSE, 'size' => 'big'), | 
| webmaster@1 | 512         'expire'     => array('type' => 'int', 'not null' => TRUE, 'default' => 0), | 
| webmaster@1 | 513         'created'    => array('type' => 'int', 'not null' => TRUE, 'default' => 0), | 
| webmaster@1 | 514         'headers'    => array('type' => 'text', 'not null' => FALSE), | 
| webmaster@1 | 515         'serialized' => array('type' => 'int', 'size' => 'small', 'not null' => TRUE, 'default' => 0) | 
| webmaster@1 | 516       ), | 
| webmaster@1 | 517       'indexes' => array('expire' => array('expire')), | 
| webmaster@1 | 518       'primary key' => array('cid'), | 
| webmaster@1 | 519     ); | 
| webmaster@1 | 520     db_create_table($ret, 'cache_block', $schema['cache_block']); | 
| webmaster@1 | 521   } | 
| webmaster@1 | 522 | 
| webmaster@1 | 523   return $ret; | 
| webmaster@1 | 524 } | 
| webmaster@1 | 525 | 
| webmaster@1 | 526 /** | 
| webmaster@1 | 527  * Add the update task list to the current page. | 
| webmaster@1 | 528  */ | 
| webmaster@1 | 529 function update_task_list($active = NULL) { | 
| webmaster@1 | 530   // Default list of tasks. | 
| webmaster@1 | 531   $tasks = array( | 
| webmaster@1 | 532     'info' => 'Overview', | 
| webmaster@1 | 533     'select' => 'Select updates', | 
| webmaster@1 | 534     'run' => 'Run updates', | 
| webmaster@1 | 535     'finished' => 'Review log', | 
| webmaster@1 | 536   ); | 
| webmaster@1 | 537 | 
| webmaster@1 | 538   drupal_set_content('left', theme('task_list', $tasks, $active)); | 
| webmaster@1 | 539 } | 
| webmaster@1 | 540 | 
| webmaster@1 | 541 /** | 
| webmaster@1 | 542  * Check update requirements and report any errors. | 
| webmaster@1 | 543  */ | 
| webmaster@1 | 544 function update_check_requirements() { | 
| webmaster@1 | 545   // Check the system module requirements only. | 
| webmaster@1 | 546   $requirements = module_invoke('system', 'requirements', 'update'); | 
| webmaster@1 | 547   $severity = drupal_requirements_severity($requirements); | 
| webmaster@1 | 548 | 
| webmaster@1 | 549   // If there are issues, report them. | 
| webmaster@1 | 550   if ($severity != REQUIREMENT_OK) { | 
| webmaster@1 | 551     foreach ($requirements as $requirement) { | 
| webmaster@1 | 552       if (isset($requirement['severity']) && $requirement['severity'] != REQUIREMENT_OK) { | 
| webmaster@1 | 553         $message = isset($requirement['description']) ? $requirement['description'] : ''; | 
| webmaster@1 | 554         if (isset($requirement['value']) && $requirement['value']) { | 
| webmaster@1 | 555           $message .= ' (Currently using '. $requirement['title'] .' '. $requirement['value'] .')'; | 
| webmaster@1 | 556         } | 
| webmaster@1 | 557         drupal_set_message($message, 'warning'); | 
| webmaster@1 | 558       } | 
| webmaster@1 | 559     } | 
| webmaster@1 | 560   } | 
| webmaster@1 | 561 } | 
| webmaster@1 | 562 | 
| webmaster@1 | 563 // Some unavoidable errors happen because the database is not yet up-to-date. | 
| webmaster@1 | 564 // Our custom error handler is not yet installed, so we just suppress them. | 
| webmaster@1 | 565 ini_set('display_errors', FALSE); | 
| webmaster@1 | 566 | 
| webmaster@1 | 567 require_once './includes/bootstrap.inc'; | 
| webmaster@1 | 568 | 
| webmaster@1 | 569 // We only load DRUPAL_BOOTSTRAP_CONFIGURATION for the update requirements | 
| webmaster@1 | 570 // check to avoid reaching the PHP memory limit. | 
| webmaster@1 | 571 $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : ''; | 
| webmaster@1 | 572 if (empty($op)) { | 
| webmaster@1 | 573   // Minimum load of components. | 
| webmaster@1 | 574   drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION); | 
| webmaster@1 | 575 | 
| webmaster@1 | 576   require_once './includes/install.inc'; | 
| webmaster@1 | 577   require_once './includes/file.inc'; | 
| webmaster@1 | 578   require_once './modules/system/system.install'; | 
| webmaster@1 | 579 | 
| webmaster@1 | 580   // Load module basics. | 
| webmaster@1 | 581   include_once './includes/module.inc'; | 
| webmaster@1 | 582   $module_list['system']['filename'] = 'modules/system/system.module'; | 
| webmaster@1 | 583   $module_list['filter']['filename'] = 'modules/filter/filter.module'; | 
| webmaster@1 | 584   module_list(TRUE, FALSE, FALSE, $module_list); | 
| webmaster@1 | 585   drupal_load('module', 'system'); | 
| webmaster@1 | 586   drupal_load('module', 'filter'); | 
| webmaster@1 | 587 | 
| webmaster@1 | 588   // Set up $language, since the installer components require it. | 
| webmaster@1 | 589   drupal_init_language(); | 
| webmaster@1 | 590 | 
| webmaster@1 | 591   // Set up theme system for the maintenance page. | 
| webmaster@1 | 592   drupal_maintenance_theme(); | 
| webmaster@1 | 593 | 
| webmaster@1 | 594   // Check the update requirements for Drupal. | 
| webmaster@1 | 595   update_check_requirements(); | 
| webmaster@1 | 596 | 
| webmaster@1 | 597   // Display the warning messages (if any) in a dedicated maintenance page, | 
| webmaster@1 | 598   // or redirect to the update information page if no message. | 
| webmaster@1 | 599   $messages = drupal_set_message(); | 
| webmaster@1 | 600   if (!empty($messages['warning'])) { | 
| webmaster@1 | 601     drupal_maintenance_theme(); | 
| webmaster@1 | 602     print theme('update_page', '<form method="post" action="update.php?op=info"><input type="submit" value="Continue" /></form>', FALSE); | 
| webmaster@1 | 603     exit; | 
| webmaster@1 | 604   } | 
| webmaster@1 | 605   install_goto('update.php?op=info'); | 
| webmaster@1 | 606 } | 
| webmaster@1 | 607 | 
| webmaster@1 | 608 drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); | 
| webmaster@1 | 609 drupal_maintenance_theme(); | 
| webmaster@1 | 610 | 
| webmaster@1 | 611 // This must happen *after* drupal_bootstrap(), since it calls | 
| webmaster@1 | 612 // variable_(get|set), which only works after a full bootstrap. | 
| webmaster@1 | 613 update_create_batch_table(); | 
| webmaster@1 | 614 | 
| webmaster@1 | 615 // Turn error reporting back on. From now on, only fatal errors (which are | 
| webmaster@1 | 616 // not passed through the error handler) will cause a message to be printed. | 
| webmaster@1 | 617 ini_set('display_errors', TRUE); | 
| webmaster@1 | 618 | 
| webmaster@1 | 619 // Access check: | 
| webmaster@1 | 620 if (!empty($update_free_access) || $user->uid == 1) { | 
| webmaster@1 | 621 | 
| webmaster@1 | 622   include_once './includes/install.inc'; | 
| webmaster@1 | 623   include_once './includes/batch.inc'; | 
| webmaster@1 | 624   drupal_load_updates(); | 
| webmaster@1 | 625 | 
| webmaster@1 | 626   update_fix_d6_requirements(); | 
| webmaster@1 | 627   update_fix_compatibility(); | 
| webmaster@1 | 628 | 
| webmaster@1 | 629   $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : ''; | 
| webmaster@1 | 630   switch ($op) { | 
| webmaster@15 | 631     case 'selection': | 
| webmaster@15 | 632       if (isset($_GET['token']) && $_GET['token'] == drupal_get_token('update')) { | 
| webmaster@15 | 633         $output = update_selection_page(); | 
| webmaster@15 | 634         break; | 
| webmaster@15 | 635       } | 
| webmaster@15 | 636 | 
| webmaster@15 | 637     case 'Update': | 
| webmaster@15 | 638       if (isset($_GET['token']) && $_GET['token'] == drupal_get_token('update')) { | 
| webmaster@15 | 639         update_batch(); | 
| webmaster@15 | 640         break; | 
| webmaster@15 | 641       } | 
| webmaster@15 | 642 | 
| webmaster@1 | 643     // update.php ops | 
| webmaster@1 | 644     case 'info': | 
| webmaster@1 | 645       $output = update_info_page(); | 
| webmaster@1 | 646       break; | 
| webmaster@1 | 647 | 
| webmaster@1 | 648     case 'results': | 
| webmaster@1 | 649       $output = update_results_page(); | 
| webmaster@1 | 650       break; | 
| webmaster@1 | 651 | 
| webmaster@1 | 652     // Regular batch ops : defer to batch processing API | 
| webmaster@1 | 653     default: | 
| webmaster@1 | 654       update_task_list('run'); | 
| webmaster@1 | 655       $output = _batch_page(); | 
| webmaster@1 | 656       break; | 
| webmaster@1 | 657   } | 
| webmaster@1 | 658 } | 
| webmaster@1 | 659 else { | 
| webmaster@1 | 660   $output = update_access_denied_page(); | 
| webmaster@1 | 661 } | 
| webmaster@1 | 662 if (isset($output) && $output) { | 
| webmaster@1 | 663   // We defer the display of messages until all updates are done. | 
| webmaster@1 | 664   $progress_page = ($batch = batch_get()) && isset($batch['running']); | 
| webmaster@1 | 665   print theme('update_page', $output, !$progress_page); | 
| webmaster@1 | 666 } |