update.php

  1. drupal
    1. 4.6 update.php
    2. 4.7 update.php
    3. 5 update.php
    4. 6 update.php
    5. 7 update.php
    6. 8 core/lib/Drupal/Core/Database/Driver/sqlite/Update.php
    7. 8 core/lib/Drupal/Core/Database/Driver/pgsql/Update.php
    8. 8 core/lib/Drupal/Core/Database/Query/Update.php
    9. 8 core/update.php
    10. 8 core/lib/Drupal/Core/Database/Driver/mysql/Update.php

Administrative page for handling updates from one Drupal version to another.

Point your browser to "http://www.example.com/update.php" and follow the instructions.

If you are not logged in as administrator, you will need to modify the access check statement below. Change the TRUE to a FALSE to disable the access check. After finishing the upgrade, be sure to open this file and change the FALSE back to a TRUE!

Functions & methods

NameDescription
db_add_columnAdd a column to a database using syntax appropriate for PostgreSQL. Save result of SQL commands in $ret array.
db_change_columnChange a column definition using syntax appropriate for PostgreSQL. Save result of SQL commands in $ret array.
update_access_denied_page
update_convert_table_utf8Convert a single MySQL table to UTF-8.
update_dataPerform one update and store the results which will later be displayed on the finished page.
update_do_updatesPerform updates for one second or until finished.
update_do_update_pagePerform updates for the JS version and return progress.
update_finished_page
update_fix_access_table
update_fix_schema_versionIf the schema version for Drupal core is stored in the variables table (4.6.x and earlier) move it to the schema_version column of the system table.
update_fix_sessionsSystem update 130 changes the sessions table, which breaks the update script's ability to use session variables. This changes the table appropriately.
update_fix_system_table
update_fix_watchdogSystem update 142 changes the watchdog table, which breaks the update script's ability to use logging. This changes the table appropriately.
update_fix_watchdog_115System update 115 changes the watchdog table, which breaks the update script's ability to use logging. This changes the table appropriately.
update_info_page
update_progress_page
update_progress_page_nojsPerform updates for the non-JS version and return the status page.
update_selection_page
update_sql
update_update_page

File

update.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Administrative page for handling updates from one Drupal version to another.
  5. *
  6. * Point your browser to "http://www.example.com/update.php" and follow the
  7. * instructions.
  8. *
  9. * If you are not logged in as administrator, you will need to modify the access
  10. * check statement below. Change the TRUE to a FALSE to disable the access
  11. * check. After finishing the upgrade, be sure to open this file and change the
  12. * FALSE back to a TRUE!
  13. */
  14. // Enforce access checking?
  15. $access_check = TRUE;
  16. function update_sql($sql) {
  17. $result = db_query($sql);
  18. return array('success' => $result !== FALSE, 'query' => check_plain($sql));
  19. }
  20. /**
  21. * Add a column to a database using syntax appropriate for PostgreSQL.
  22. * Save result of SQL commands in $ret array.
  23. *
  24. * Note: when you add a column with NOT NULL and you are not sure if there are
  25. * already rows in the table, you MUST also add DEFAULT. Otherwise PostgreSQL won't
  26. * work when the table is not empty. If NOT NULL and DEFAULT are set the
  27. * PostgreSQL version will set values of the added column in old rows to the
  28. * DEFAULT value.
  29. *
  30. * @param $ret
  31. * Array to which results will be added.
  32. * @param $table
  33. * Name of the table, without {}
  34. * @param $column
  35. * Name of the column
  36. * @param $type
  37. * Type of column
  38. * @param $attributes
  39. * Additional optional attributes. Recognized attributes:
  40. * not null => TRUE|FALSE
  41. * default => NULL|FALSE|value (with or without '', it won't be added)
  42. * @return
  43. * nothing, but modifies $ret parameter.
  44. */
  45. function db_add_column(&$ret, $table, $column, $type, $attributes = array()) {
  46. if (array_key_exists('not null', $attributes) and $attributes['not null']) {
  47. $not_null = 'NOT NULL';
  48. }
  49. if (array_key_exists('default', $attributes)) {
  50. if (is_null($attributes['default'])) {
  51. $default_val = 'NULL';
  52. $default = 'default NULL';
  53. }
  54. elseif ($attributes['default'] === FALSE) {
  55. $default = '';
  56. }
  57. else {
  58. $default_val = "$attributes[default]";
  59. $default = "default $attributes[default]";
  60. }
  61. }
  62. $ret[] = update_sql("ALTER TABLE {". $table ."} ADD $column $type");
  63. if ($default) { $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column SET $default"); }
  64. if ($not_null) {
  65. if ($default) { $ret[] = update_sql("UPDATE {". $table ."} SET $column = $default_val"); }
  66. $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column SET NOT NULL");
  67. }
  68. }
  69. /**
  70. * Change a column definition using syntax appropriate for PostgreSQL.
  71. * Save result of SQL commands in $ret array.
  72. *
  73. * Remember that changing a column definition involves adding a new column
  74. * and dropping an old one. This means that any indices, primary keys and
  75. * sequences from serial-type columns are dropped and might need to be
  76. * recreated.
  77. *
  78. * @param $ret
  79. * Array to which results will be added.
  80. * @param $table
  81. * Name of the table, without {}
  82. * @param $column
  83. * Name of the column to change
  84. * @param $column_new
  85. * New name for the column (set to the same as $column if you don't want to change the name)
  86. * @param $type
  87. * Type of column
  88. * @param $attributes
  89. * Additional optional attributes. Recognized attributes:
  90. * not null => TRUE|FALSE
  91. * default => NULL|FALSE|value (with or without '', it won't be added)
  92. * @return
  93. * nothing, but modifies $ret parameter.
  94. */
  95. function db_change_column(&$ret, $table, $column, $column_new, $type, $attributes = array()) {
  96. if (array_key_exists('not null', $attributes) and $attributes['not null']) {
  97. $not_null = 'NOT NULL';
  98. }
  99. if (array_key_exists('default', $attributes)) {
  100. if (is_null($attributes['default'])) {
  101. $default_val = 'NULL';
  102. $default = 'default NULL';
  103. }
  104. elseif ($attributes['default'] === FALSE) {
  105. $default = '';
  106. }
  107. else {
  108. $default_val = "$attributes[default]";
  109. $default = "default $attributes[default]";
  110. }
  111. }
  112. $ret[] = update_sql("ALTER TABLE {". $table ."} RENAME $column TO ". $column ."_old");
  113. $ret[] = update_sql("ALTER TABLE {". $table ."} ADD $column_new $type");
  114. $ret[] = update_sql("UPDATE {". $table ."} SET $column_new = ". $column ."_old");
  115. if ($default) { $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column_new SET $default"); }
  116. if ($not_null) { $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column_new SET NOT NULL"); }
  117. $ret[] = update_sql("ALTER TABLE {". $table ."} DROP ". $column ."_old");
  118. }
  119. /**
  120. * If the schema version for Drupal core is stored in the variables table
  121. * (4.6.x and earlier) move it to the schema_version column of the system
  122. * table.
  123. *
  124. * This function may be removed when update 156 is removed, which is the last
  125. * update in the 4.6 to 4.7 migration.
  126. */
  127. function update_fix_schema_version() {
  128. if ($update_start = variable_get('update_start', FALSE)) {
  129. // Some updates were made to the 4.6 branch and 4.7 branch. This sets
  130. // temporary variables to prevent the updates from being executed twice and
  131. // throwing errors.
  132. switch ($update_start) {
  133. case '2005-04-14':
  134. variable_set('update_132_done', TRUE);
  135. break;
  136. case '2005-05-06':
  137. variable_set('update_132_done', TRUE);
  138. variable_set('update_135_done', TRUE);
  139. break;
  140. case '2005-05-07':
  141. variable_set('update_132_done', TRUE);
  142. variable_set('update_135_done', TRUE);
  143. variable_set('update_137_done', TRUE);
  144. break;
  145. }
  146. // The schema_version column (added below) was changed during 4.7beta.
  147. // Update_170 is only for those beta users.
  148. variable_set('update_170_done', TRUE);
  149. $sql_updates = array(
  150. '2004-10-31: first update since Drupal 4.5.0 release' => 110,
  151. '2004-11-07' => 111, '2004-11-15' => 112, '2004-11-28' => 113,
  152. '2004-12-05' => 114, '2005-01-07' => 115, '2005-01-14' => 116,
  153. '2005-01-18' => 117, '2005-01-19' => 118, '2005-01-20' => 119,
  154. '2005-01-25' => 120, '2005-01-26' => 121, '2005-01-27' => 122,
  155. '2005-01-28' => 123, '2005-02-11' => 124, '2005-02-23' => 125,
  156. '2005-03-03' => 126, '2005-03-18' => 127, '2005-03-21' => 128,
  157. // The following three updates were made on the 4.6 branch
  158. '2005-04-14' => 128, '2005-05-06' => 128, '2005-05-07' => 128,
  159. '2005-04-08: first update since Drupal 4.6.0 release' => 129,
  160. '2005-04-10' => 130, '2005-04-11' => 131, '2005-04-14' => 132,
  161. '2005-04-24' => 133, '2005-04-30' => 134, '2005-05-06' => 135,
  162. '2005-05-08' => 136, '2005-05-09' => 137, '2005-05-10' => 138,
  163. '2005-05-11' => 139, '2005-05-12' => 140, '2005-05-22' => 141,
  164. '2005-07-29' => 142, '2005-07-30' => 143, '2005-08-08' => 144,
  165. '2005-08-15' => 145, '2005-08-25' => 146, '2005-09-07' => 147,
  166. '2005-09-18' => 148, '2005-09-27' => 149, '2005-10-15' => 150,
  167. '2005-10-23' => 151, '2005-10-28' => 152, '2005-11-03' => 153,
  168. '2005-11-14' => 154, '2005-11-27' => 155, '2005-12-03' => 156,
  169. );
  170. // Add schema version column
  171. switch ($GLOBALS['db_type']) {
  172. case 'pgsql':
  173. $ret = array();
  174. db_add_column($ret, 'system', 'schema_version', 'smallint', array('not null' => TRUE, 'default' => -1));
  175. break;
  176. case 'mysql':
  177. case 'mysqli':
  178. db_query('ALTER TABLE {system} ADD schema_version smallint(3) not null default -1');
  179. break;
  180. }
  181. // Set all enabled (contrib) modules to schema version 0 (installed)
  182. db_query('UPDATE {system} SET schema_version = 0 WHERE status = 1');
  183. // Set schema version for core
  184. drupal_set_installed_schema_version('system', $sql_updates[$update_start]);
  185. variable_del('update_start');
  186. }
  187. }
  188. /**
  189. * System update 130 changes the sessions table, which breaks the update
  190. * script's ability to use session variables. This changes the table
  191. * appropriately.
  192. *
  193. * This code, including the 'update_sessions_fixed' variable, may be removed
  194. * when update 130 is removed. It is part of the Drupal 4.6 to 4.7 migration.
  195. */
  196. function update_fix_sessions() {
  197. $ret = array();
  198. if (drupal_get_installed_schema_version('system') < 130 && !variable_get('update_sessions_fixed', FALSE)) {
  199. if ($GLOBALS['db_type'] == 'mysql') {
  200. db_query("ALTER TABLE {sessions} ADD cache int(11) NOT NULL default '0' AFTER timestamp");
  201. }
  202. elseif ($GLOBALS['db_type'] == 'pgsql') {
  203. db_add_column($ret, 'sessions', 'cache', 'int', array('default' => 0, 'not null' => TRUE));
  204. }
  205. variable_set('update_sessions_fixed', TRUE);
  206. }
  207. }
  208. /**
  209. * System update 115 changes the watchdog table, which breaks the update
  210. * script's ability to use logging. This changes the table appropriately.
  211. *
  212. * This code, including the 'update_watchdog_115_fixed' variable, may be removed
  213. * when update 115 is removed. It is part of the Drupal 4.5 to 4.7 migration.
  214. */
  215. function update_fix_watchdog_115() {
  216. if (drupal_get_installed_schema_version('system') < 115 && !variable_get('update_watchdog_115_fixed', FALSE)) {
  217. if ($GLOBALS['db_type'] == 'mysql') {
  218. $ret[] = update_sql("ALTER TABLE {watchdog} ADD severity tinyint(3) unsigned NOT NULL default '0'");
  219. }
  220. else if ($GLOBALS['db_type'] == 'pgsql') {
  221. $ret[] = update_sql('ALTER TABLE {watchdog} ADD severity smallint');
  222. $ret[] = update_sql('UPDATE {watchdog} SET severity = 0');
  223. $ret[] = update_sql('ALTER TABLE {watchdog} ALTER COLUMN severity SET NOT NULL');
  224. $ret[] = update_sql('ALTER TABLE {watchdog} ALTER COLUMN severity SET DEFAULT 0');
  225. }
  226. variable_set('update_watchdog_115_fixed', TRUE);
  227. }
  228. }
  229. /**
  230. * System update 142 changes the watchdog table, which breaks the update
  231. * script's ability to use logging. This changes the table appropriately.
  232. *
  233. * This code, including the 'update_watchdog_fixed' variable, may be removed
  234. * when update 142 is removed. It is part of the Drupal 4.6 to 4.7 migration.
  235. */
  236. function update_fix_watchdog() {
  237. if (drupal_get_installed_schema_version('system') < 142 && !variable_get('update_watchdog_fixed', FALSE)) {
  238. switch ($GLOBALS['db_type']) {
  239. case 'pgsql':
  240. $ret = array();
  241. db_add_column($ret, 'watchdog', 'referer', 'varchar(128)', array('not null' => TRUE, 'default' => "''"));
  242. break;
  243. case 'mysql':
  244. case 'mysqli':
  245. db_query("ALTER TABLE {watchdog} ADD COLUMN referer varchar(128) NOT NULL");
  246. break;
  247. }
  248. variable_set('update_watchdog_fixed', TRUE);
  249. }
  250. }
  251. /**
  252. * Perform one update and store the results which will later be displayed on
  253. * the finished page.
  254. *
  255. * @param $module
  256. * The module whose update will be run.
  257. * @param $number
  258. * The update number to run.
  259. *
  260. * @return
  261. * TRUE if the update was finished. Otherwise, FALSE.
  262. */
  263. function update_data($module, $number) {
  264. $ret = module_invoke($module, 'update_'. $number);
  265. // Assume the update finished unless the update results indicate otherwise.
  266. $finished = 1;
  267. if (isset($ret['#finished'])) {
  268. $finished = $ret['#finished'];
  269. unset($ret['#finished']);
  270. }
  271. // Save the query and results for display by update_finished_page().
  272. if (!isset($_SESSION['update_results'])) {
  273. $_SESSION['update_results'] = array();
  274. }
  275. if (!isset($_SESSION['update_results'][$module])) {
  276. $_SESSION['update_results'][$module] = array();
  277. }
  278. if (!isset($_SESSION['update_results'][$module][$number])) {
  279. $_SESSION['update_results'][$module][$number] = array();
  280. }
  281. $_SESSION['update_results'][$module][$number] = array_merge($_SESSION['update_results'][$module][$number], $ret);
  282. if ($finished == 1) {
  283. // Update the installed version
  284. drupal_set_installed_schema_version($module, $number);
  285. }
  286. return $finished;
  287. }
  288. function update_selection_page() {
  289. $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>';
  290. $output .= '<p>Click Update to start the update process.</p>';
  291. $form = array();
  292. $form['start'] = array(
  293. '#tree' => TRUE,
  294. '#type' => 'fieldset',
  295. '#title' => 'Select versions',
  296. '#collapsible' => TRUE,
  297. '#collapsed' => TRUE,
  298. );
  299. foreach (module_list() as $module) {
  300. $updates = drupal_get_schema_versions($module);
  301. if ($updates !== FALSE) {
  302. $updates = drupal_map_assoc($updates);
  303. $updates[] = 'No updates available';
  304. $form['start'][$module] = array(
  305. '#type' => 'select',
  306. '#title' => $module . ' module',
  307. '#default_value' => array_search(drupal_get_installed_schema_version($module), $updates) + 1,
  308. '#options' => $updates,
  309. );
  310. }
  311. }
  312. $form['has_js'] = array(
  313. '#type' => 'hidden',
  314. '#default_value' => FALSE,
  315. '#attributes' => array('id' => 'edit-has_js'),
  316. );
  317. $form['submit'] = array(
  318. '#type' => 'submit',
  319. '#value' => 'Update',
  320. );
  321. drupal_set_title('Drupal database update');
  322. // Prevent browser from using cached drupal.js or update.js
  323. drupal_add_js('misc/update.js', TRUE);
  324. $output .= drupal_get_form('update_script_selection_form', $form);
  325. return $output;
  326. }
  327. function update_update_page() {
  328. // Set the installed version so updates start at the correct place.
  329. $_SESSION['update_remaining'] = array();
  330. foreach ($_POST['edit']['start'] as $module => $version) {
  331. drupal_set_installed_schema_version($module, $version - 1);
  332. $max_version = max(drupal_get_schema_versions($module));
  333. if ($version <= $max_version) {
  334. foreach (range($version, $max_version) as $update) {
  335. $_SESSION['update_remaining'][] = array('module' => $module, 'version' => $update);
  336. }
  337. }
  338. }
  339. // Keep track of total number of updates
  340. $_SESSION['update_total'] = count($_SESSION['update_remaining']);
  341. if ($_POST['edit']['has_js']) {
  342. return update_progress_page();
  343. }
  344. else {
  345. return update_progress_page_nojs();
  346. }
  347. }
  348. function update_progress_page() {
  349. // Prevent browser from using cached drupal.js or update.js
  350. drupal_add_js('misc/progress.js', TRUE);
  351. drupal_add_js('misc/update.js', TRUE);
  352. drupal_set_title('Updating');
  353. $output = '<div id="progress"></div>';
  354. $output .= '<p id="wait">Please wait while your site is being updated.</p>';
  355. return $output;
  356. }
  357. /**
  358. * Perform updates for one second or until finished.
  359. *
  360. * @return
  361. * An array indicating the status after doing updates. The first element is
  362. * the overall percentage finished. The second element is a status message.
  363. */
  364. function update_do_updates() {
  365. while (($update = reset($_SESSION['update_remaining']))) {
  366. $update_finished = update_data($update['module'], $update['version']);
  367. if ($update_finished == 1) {
  368. // Dequeue the completed update.
  369. unset($_SESSION['update_remaining'][key($_SESSION['update_remaining'])]);
  370. $update_finished = 0; // Make sure this step isn't counted double
  371. }
  372. if (timer_read('page') > 1000) {
  373. break;
  374. }
  375. }
  376. if ($_SESSION['update_total']) {
  377. $percentage = floor(($_SESSION['update_total'] - count($_SESSION['update_remaining']) + $update_finished) / $_SESSION['update_total'] * 100);
  378. }
  379. else {
  380. $percentage = 100;
  381. }
  382. // When no updates remain, clear the cache.
  383. if (!isset($update['module'])) {
  384. db_query('DELETE FROM {cache}');
  385. }
  386. return array($percentage, isset($update['module']) ? 'Updating '. $update['module'] .' module' : 'Updating complete');
  387. }
  388. /**
  389. * Perform updates for the JS version and return progress.
  390. */
  391. function update_do_update_page() {
  392. global $conf;
  393. // HTTP Post required
  394. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  395. drupal_set_message('HTTP Post is required.', 'error');
  396. drupal_set_title('Error');
  397. return '';
  398. }
  399. // Error handling: if PHP dies, the output will fail to parse as JSON, and
  400. // the Javascript will tell the user to continue to the op=error page.
  401. list($percentage, $message) = update_do_updates();
  402. print drupal_to_js(array('status' => TRUE, 'percentage' => $percentage, 'message' => $message));
  403. }
  404. /**
  405. * Perform updates for the non-JS version and return the status page.
  406. */
  407. function update_progress_page_nojs() {
  408. drupal_set_title('Updating');
  409. $new_op = 'do_update_nojs';
  410. if ($_SERVER['REQUEST_METHOD'] == 'GET') {
  411. // Error handling: if PHP dies, it will output whatever is in the output
  412. // buffer, followed by the error message.
  413. ob_start();
  414. $fallback = '<p class="error">An unrecoverable error has occurred. You can find the error message below. It is advised to copy it to the clipboard for reference. Please continue to the <a href="update.php?op=error">update summary</a>.</p><p class="error">';
  415. print theme('maintenance_page', $fallback, FALSE, TRUE);
  416. list($percentage, $message) = update_do_updates();
  417. if ($percentage == 100) {
  418. $new_op = 'finished';
  419. }
  420. // Updates successful; remove fallback
  421. ob_end_clean();
  422. }
  423. else {
  424. // This is the first page so return some output immediately.
  425. $percentage = 0;
  426. $message = 'Starting updates';
  427. }
  428. drupal_set_html_head('<meta http-equiv="Refresh" content="0; URL=update.php?op='. $new_op .'">');
  429. $output = theme('progress_bar', $percentage, $message);
  430. $output .= '<p>Updating your site will take a few seconds.</p>';
  431. // Note: do not output drupal_set_message()s until the summary page.
  432. print theme('maintenance_page', $output, FALSE);
  433. return NULL;
  434. }
  435. function update_finished_page($success) {
  436. drupal_set_title('Drupal database update');
  437. // NOTE: we can't use l() here because the URL would point to 'update.php?q=admin'.
  438. $links[] = '<a href="'. base_path() .'">main page</a>';
  439. $links[] = '<a href="'. base_path() .'?q=admin">administration pages</a>';
  440. // Report end result
  441. if ($success) {
  442. $output = '<p>Updates were attempted. If you see no failures below, you may proceed happily to the <a href="index.php?q=admin">administration pages</a>. Otherwise, you may need to update your database manually. All errors have been <a href="index.php?q=admin/logs">logged</a>.</p>';
  443. }
  444. else {
  445. $update = reset($_SESSION['update_remaining']);
  446. $output = '<p class="error">The update process was aborted prematurely while running <strong>update #'. $update['version'] .' in '. $update['module'] .'.module</strong>. All other errors have been <a href="index.php?q=admin/logs">logged</a>. You may need to check the <code>watchdog</code> database table manually.</p>';
  447. }
  448. if ($GLOBALS['access_check'] == FALSE) {
  449. $output .= "<p><strong>Reminder: don't forget to set the <code>\$access_check</code> value at the top of <code>update.php</code> back to <code>TRUE</code>.</strong></p>";
  450. }
  451. $output .= theme('item_list', $links);
  452. // Output a list of queries executed
  453. if ($_SESSION['update_results']) {
  454. $output .= '<div id="update-results">';
  455. $output .= '<h2>The following queries were executed</h2>';
  456. foreach ($_SESSION['update_results'] as $module => $updates) {
  457. $output .= '<h3>'. $module .' module</h3>';
  458. foreach ($updates as $number => $queries) {
  459. $output .= '<h4>Update #'. $number .'</h4>';
  460. $output .= '<ul>';
  461. foreach ($queries as $query) {
  462. if ($query['success']) {
  463. $output .= '<li class="success">'. $query['query'] .'</li>';
  464. }
  465. else {
  466. $output .= '<li class="failure"><strong>Failed:</strong> '. $query['query'] .'</li>';
  467. }
  468. }
  469. if (!count($queries)) {
  470. $output .= '<li class="none">No queries</li>';
  471. }
  472. $output .= '</ul>';
  473. }
  474. }
  475. $output .= '</div>';
  476. unset($_SESSION['update_results']);
  477. }
  478. return $output;
  479. }
  480. function update_info_page() {
  481. drupal_set_title('Drupal database update');
  482. $output = "<ol>\n";
  483. $output .= "<li>Use this script to <strong>upgrade an existing Drupal installation</strong>. You don't need this script when installing Drupal from scratch.</li>";
  484. $output .= "<li>Before doing anything, backup your database. This process will change your database and its values, and some things might get lost.</li>\n";
  485. $output .= "<li>Update your Drupal sources, check the notes below and <a href=\"update.php?op=selection\">run the database upgrade script</a>. Don't upgrade your database twice as it may cause problems.</li>\n";
  486. $output .= "<li>Go through the various administration pages to change the existing and new settings to your liking.</li>\n";
  487. $output .= "</ol>";
  488. $output .= '<p>For more help, 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>';
  489. return $output;
  490. }
  491. function update_access_denied_page() {
  492. drupal_set_title('Access denied');
  493. 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>update.php</code> to bypass this access check. To do this:</p>
  494. <ol>
  495. <li>With a text editor find the update.php file on your system. It should be in the main Drupal directory that you installed all the files into.</li>
  496. <li>There is a line near top of update.php that says <code>$access_check = TRUE;</code>. Change it to <code>$access_check = FALSE;</code>.</li>
  497. <li>As soon as the script is done, you must change the update.php script back to its original form to <code>$access_check = TRUE;</code>.</li>
  498. <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>
  499. </ol>';
  500. }
  501. // This code may be removed later. It is part of the Drupal 4.5 to 4.7 migration.
  502. function update_fix_system_table() {
  503. drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
  504. $row = db_fetch_object(db_query_range('SELECT * FROM {system}', 0, 1));
  505. if (!isset($row->weight)) {
  506. $ret = array();
  507. switch ($GLOBALS['db_type']) {
  508. case 'pgsql':
  509. db_add_column($ret, 'system', 'weight', 'smallint', array('not null' => TRUE, 'default' => 0));
  510. $ret[] = update_sql('CREATE INDEX {system}_weight_idx ON {system} (weight)');
  511. break;
  512. case 'mysql':
  513. case 'mysqli':
  514. $ret[] = update_sql("ALTER TABLE {system} ADD weight tinyint(2) default '0' NOT NULL, ADD KEY (weight)");
  515. break;
  516. }
  517. }
  518. }
  519. // This code may be removed later. It is part of the Drupal 4.6 to 4.7 migration.
  520. function update_fix_access_table() {
  521. if (variable_get('update_access_fixed', FALSE)) {
  522. return;
  523. }
  524. switch ($GLOBALS['db_type']) {
  525. // Only for MySQL 4.1+
  526. case 'mysqli':
  527. break;
  528. case 'mysql':
  529. if (version_compare(mysql_get_server_info($GLOBALS['active_db']), '4.1.0', '<')) {
  530. return;
  531. }
  532. break;
  533. case 'pgsql':
  534. return;
  535. }
  536. // Convert access table to UTF-8 if needed.
  537. $result = db_fetch_array(db_query('SHOW CREATE TABLE {access}'));
  538. if (!preg_match('/utf8/i', array_pop($result))) {
  539. update_convert_table_utf8('access');
  540. }
  541. // Don't run again
  542. variable_set('update_access_fixed', TRUE);
  543. }
  544. /**
  545. * Convert a single MySQL table to UTF-8.
  546. *
  547. * We change all text columns to their corresponding binary type,
  548. * then back to text, but with a UTF-8 character set.
  549. * See: http://dev.mysql.com/doc/refman/4.1/en/charset-conversion.html
  550. */
  551. function update_convert_table_utf8($table) {
  552. $ret = array();
  553. $types = array('char' => 'binary',
  554. 'varchar' => 'varbinary',
  555. 'tinytext' => 'tinyblob',
  556. 'text' => 'blob',
  557. 'mediumtext' => 'mediumblob',
  558. 'longtext' => 'longblob');
  559. // Get next table in list
  560. $convert_to_binary = array();
  561. $convert_to_utf8 = array();
  562. // Set table default charset
  563. $ret[] = update_sql('ALTER TABLE {'. $table .'} DEFAULT CHARACTER SET utf8');
  564. // Find out which columns need converting and build SQL statements
  565. $result = db_query('SHOW FULL COLUMNS FROM {'. $table .'}');
  566. while ($column = db_fetch_array($result)) {
  567. list($type) = explode('(', $column['Type']);
  568. if (isset($types[$type])) {
  569. $names = 'CHANGE `'. $column['Field'] .'` `'. $column['Field'] .'` ';
  570. $attributes = ' DEFAULT '. ($column['Default'] == 'NULL' ? 'NULL ' :
  571. "'". db_escape_string($column['Default']) ."' ") .
  572. ($column['Null'] == 'YES' ? 'NULL' : 'NOT NULL');
  573. $convert_to_binary[] = $names . preg_replace('/'. $type .'/i', $types[$type], $column['Type']) . $attributes;
  574. $convert_to_utf8[] = $names . $column['Type'] .' CHARACTER SET utf8'. $attributes;
  575. }
  576. }
  577. if (count($convert_to_binary)) {
  578. // Convert text columns to binary
  579. $ret[] = update_sql('ALTER TABLE {'. $table .'} '. implode(', ', $convert_to_binary));
  580. // Convert binary columns to UTF-8
  581. $ret[] = update_sql('ALTER TABLE {'. $table .'} '. implode(', ', $convert_to_utf8));
  582. }
  583. return $ret;
  584. }
  585. // Some unavoidable errors happen because the database is not yet up-to-date.
  586. // Our custom error handler is not yet installed, so we just suppress them.
  587. ini_set('display_errors', FALSE);
  588. include_once './includes/bootstrap.inc';
  589. update_fix_system_table();
  590. update_fix_access_table();
  591. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  592. drupal_maintenance_theme();
  593. // Turn error reporting back on. From now on, only fatal errors (which are
  594. // not passed through the error handler) will cause a message to be printed.
  595. ini_set('display_errors', TRUE);
  596. // Access check:
  597. if (($access_check == FALSE) || ($user->uid == 1)) {
  598. include_once './includes/install.inc';
  599. update_fix_schema_version();
  600. update_fix_watchdog_115();
  601. update_fix_watchdog();
  602. update_fix_sessions();
  603. $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : '';
  604. switch ($op) {
  605. case 'Update':
  606. // Check for a valid form token to protect against cross site request forgeries.
  607. if (drupal_valid_token($_REQUEST['edit']['form_token'], 'update_script_selection_form', TRUE)) {
  608. $output = update_update_page();
  609. }
  610. else {
  611. form_set_error('form_token', t('Validation error, please try again. If this error persists, please contact the site administrator.'));
  612. $output = update_selection_page();
  613. }
  614. break;
  615. case 'finished':
  616. $output = update_finished_page(true);
  617. break;
  618. case 'error':
  619. $output = update_finished_page(false);
  620. break;
  621. case 'do_update':
  622. $output = update_do_update_page();
  623. break;
  624. case 'do_update_nojs':
  625. $output = update_progress_page_nojs();
  626. break;
  627. case 'selection':
  628. $output = update_selection_page();
  629. break;
  630. default:
  631. $output = update_info_page();
  632. break;
  633. }
  634. }
  635. else {
  636. $output = update_access_denied_page();
  637. }
  638. if (isset($output)) {
  639. print theme('maintenance_page', $output);
  640. }
Login or register to post comments