new graphics
[flowplayerorg.git] / httpdocs / update.php
blob84c4dfe1386ca39c3b8f556dabe0d5cc5f8cac47
1 <?php
2 // $Id: update.php,v 1.211 2006/12/25 21:22:03 drumm Exp $
4 /**
5 * @file
6 * Administrative page for handling updates from one Drupal version to another.
8 * Point your browser to "http://www.example.com/update.php" and follow the
9 * instructions.
11 * If you are not logged in as administrator, you will need to modify the access
12 * check statement below. Change the TRUE to a FALSE to disable the access
13 * check. After finishing the upgrade, be sure to open this file and change the
14 * FALSE back to a TRUE!
17 // Enforce access checking?
18 $access_check = TRUE;
21 function update_sql($sql) {
22 $result = db_query($sql);
23 return array('success' => $result !== FALSE, 'query' => check_plain($sql));
26 /**
27 * Add a column to a database using syntax appropriate for PostgreSQL.
28 * Save result of SQL commands in $ret array.
30 * Note: when you add a column with NOT NULL and you are not sure if there are
31 * already rows in the table, you MUST also add DEFAULT. Otherwise PostgreSQL
32 * won't work when the table is not empty, and db_add_column() will fail.
33 * To have an empty string as the default, you must use: 'default' => "''"
34 * in the $attributes array. If NOT NULL and DEFAULT are set the PostgreSQL
35 * version will set values of the added column in old rows to the
36 * DEFAULT value.
38 * @param $ret
39 * Array to which results will be added.
40 * @param $table
41 * Name of the table, without {}
42 * @param $column
43 * Name of the column
44 * @param $type
45 * Type of column
46 * @param $attributes
47 * Additional optional attributes. Recognized attributes:
48 * not null => TRUE|FALSE
49 * default => NULL|FALSE|value (the value must be enclosed in '' marks)
50 * @return
51 * nothing, but modifies $ret parameter.
53 function db_add_column(&$ret, $table, $column, $type, $attributes = array()) {
54 if (array_key_exists('not null', $attributes) and $attributes['not null']) {
55 $not_null = 'NOT NULL';
57 if (array_key_exists('default', $attributes)) {
58 if (is_null($attributes['default'])) {
59 $default_val = 'NULL';
60 $default = 'default NULL';
62 elseif ($attributes['default'] === FALSE) {
63 $default = '';
65 else {
66 $default_val = "$attributes[default]";
67 $default = "default $attributes[default]";
71 $ret[] = update_sql("ALTER TABLE {". $table ."} ADD $column $type");
72 if ($default) { $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column SET $default"); }
73 if ($not_null) {
74 if ($default) { $ret[] = update_sql("UPDATE {". $table ."} SET $column = $default_val"); }
75 $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column SET NOT NULL");
79 /**
80 * Change a column definition using syntax appropriate for PostgreSQL.
81 * Save result of SQL commands in $ret array.
83 * Remember that changing a column definition involves adding a new column
84 * and dropping an old one. This means that any indices, primary keys and
85 * sequences from serial-type columns are dropped and might need to be
86 * recreated.
88 * @param $ret
89 * Array to which results will be added.
90 * @param $table
91 * Name of the table, without {}
92 * @param $column
93 * Name of the column to change
94 * @param $column_new
95 * New name for the column (set to the same as $column if you don't want to change the name)
96 * @param $type
97 * Type of column
98 * @param $attributes
99 * Additional optional attributes. Recognized attributes:
100 * not null => TRUE|FALSE
101 * default => NULL|FALSE|value (with or without '', it won't be added)
102 * @return
103 * nothing, but modifies $ret parameter.
105 function db_change_column(&$ret, $table, $column, $column_new, $type, $attributes = array()) {
106 if (array_key_exists('not null', $attributes) and $attributes['not null']) {
107 $not_null = 'NOT NULL';
109 if (array_key_exists('default', $attributes)) {
110 if (is_null($attributes['default'])) {
111 $default_val = 'NULL';
112 $default = 'default NULL';
114 elseif ($attributes['default'] === FALSE) {
115 $default = '';
117 else {
118 $default_val = "$attributes[default]";
119 $default = "default $attributes[default]";
123 $ret[] = update_sql("ALTER TABLE {". $table ."} RENAME $column TO ". $column ."_old");
124 $ret[] = update_sql("ALTER TABLE {". $table ."} ADD $column_new $type");
125 $ret[] = update_sql("UPDATE {". $table ."} SET $column_new = ". $column ."_old");
126 if ($default) { $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column_new SET $default"); }
127 if ($not_null) { $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column_new SET NOT NULL"); }
128 $ret[] = update_sql("ALTER TABLE {". $table ."} DROP ". $column ."_old");
132 * If the schema version for Drupal core is stored in the variables table
133 * (4.6.x and earlier) move it to the schema_version column of the system
134 * table.
136 * This function may be removed when update 156 is removed, which is the last
137 * update in the 4.6 to 4.7 migration.
139 function update_fix_schema_version() {
140 if ($update_start = variable_get('update_start', FALSE)) {
141 // Some updates were made to the 4.6 branch and 4.7 branch. This sets
142 // temporary variables to prevent the updates from being executed twice and
143 // throwing errors.
144 switch ($update_start) {
145 case '2005-04-14':
146 variable_set('update_132_done', TRUE);
147 break;
149 case '2005-05-06':
150 variable_set('update_132_done', TRUE);
151 variable_set('update_135_done', TRUE);
152 break;
154 case '2005-05-07':
155 variable_set('update_132_done', TRUE);
156 variable_set('update_135_done', TRUE);
157 variable_set('update_137_done', TRUE);
158 break;
161 // The schema_version column (added below) was changed during 4.7beta.
162 // Update_170 is only for those beta users.
163 variable_set('update_170_done', TRUE);
165 $sql_updates = array(
166 '2004-10-31: first update since Drupal 4.5.0 release' => 110,
167 '2004-11-07' => 111, '2004-11-15' => 112, '2004-11-28' => 113,
168 '2004-12-05' => 114, '2005-01-07' => 115, '2005-01-14' => 116,
169 '2005-01-18' => 117, '2005-01-19' => 118, '2005-01-20' => 119,
170 '2005-01-25' => 120, '2005-01-26' => 121, '2005-01-27' => 122,
171 '2005-01-28' => 123, '2005-02-11' => 124, '2005-02-23' => 125,
172 '2005-03-03' => 126, '2005-03-18' => 127, '2005-03-21' => 128,
173 // The following three updates were made on the 4.6 branch
174 '2005-04-14' => 128, '2005-05-06' => 128, '2005-05-07' => 128,
175 '2005-04-08: first update since Drupal 4.6.0 release' => 129,
176 '2005-04-10' => 130, '2005-04-11' => 131, '2005-04-14' => 132,
177 '2005-04-24' => 133, '2005-04-30' => 134, '2005-05-06' => 135,
178 '2005-05-08' => 136, '2005-05-09' => 137, '2005-05-10' => 138,
179 '2005-05-11' => 139, '2005-05-12' => 140, '2005-05-22' => 141,
180 '2005-07-29' => 142, '2005-07-30' => 143, '2005-08-08' => 144,
181 '2005-08-15' => 145, '2005-08-25' => 146, '2005-09-07' => 147,
182 '2005-09-18' => 148, '2005-09-27' => 149, '2005-10-15' => 150,
183 '2005-10-23' => 151, '2005-10-28' => 152, '2005-11-03' => 153,
184 '2005-11-14' => 154, '2005-11-27' => 155, '2005-12-03' => 156,
187 // Add schema version column
188 switch ($GLOBALS['db_type']) {
189 case 'pgsql':
190 $ret = array();
191 db_add_column($ret, 'system', 'schema_version', 'smallint', array('not null' => TRUE, 'default' => -1));
192 break;
194 case 'mysql':
195 case 'mysqli':
196 db_query('ALTER TABLE {system} ADD schema_version smallint(3) not null default -1');
197 break;
199 // Set all enabled (contrib) modules to schema version 0 (installed)
200 db_query('UPDATE {system} SET schema_version = 0 WHERE status = 1');
202 // Set schema version for core
203 drupal_set_installed_schema_version('system', $sql_updates[$update_start]);
204 variable_del('update_start');
209 * System update 130 changes the sessions table, which breaks the update
210 * script's ability to use session variables. This changes the table
211 * appropriately.
213 * This code, including the 'update_sessions_fixed' variable, may be removed
214 * when update 130 is removed. It is part of the Drupal 4.6 to 4.7 migration.
216 function update_fix_sessions() {
217 $ret = array();
219 if (drupal_get_installed_schema_version('system') < 130 && !variable_get('update_sessions_fixed', FALSE)) {
220 if ($GLOBALS['db_type'] == 'mysql') {
221 db_query("ALTER TABLE {sessions} ADD cache int(11) NOT NULL default '0' AFTER timestamp");
223 elseif ($GLOBALS['db_type'] == 'pgsql') {
224 db_add_column($ret, 'sessions', 'cache', 'int', array('default' => 0, 'not null' => TRUE));
227 variable_set('update_sessions_fixed', TRUE);
232 * System update 115 changes the watchdog table, which breaks the update
233 * script's ability to use logging. This changes the table appropriately.
235 * This code, including the 'update_watchdog_115_fixed' variable, may be removed
236 * when update 115 is removed. It is part of the Drupal 4.5 to 4.7 migration.
238 function update_fix_watchdog_115() {
239 if (drupal_get_installed_schema_version('system') < 115 && !variable_get('update_watchdog_115_fixed', FALSE)) {
240 if ($GLOBALS['db_type'] == 'mysql') {
241 $ret[] = update_sql("ALTER TABLE {watchdog} ADD severity tinyint(3) unsigned NOT NULL default '0'");
243 else if ($GLOBALS['db_type'] == 'pgsql') {
244 $ret[] = update_sql('ALTER TABLE {watchdog} ADD severity smallint');
245 $ret[] = update_sql('UPDATE {watchdog} SET severity = 0');
246 $ret[] = update_sql('ALTER TABLE {watchdog} ALTER COLUMN severity SET NOT NULL');
247 $ret[] = update_sql('ALTER TABLE {watchdog} ALTER COLUMN severity SET DEFAULT 0');
250 variable_set('update_watchdog_115_fixed', TRUE);
255 * System update 142 changes the watchdog table, which breaks the update
256 * script's ability to use logging. This changes the table appropriately.
258 * This code, including the 'update_watchdog_fixed' variable, may be removed
259 * when update 142 is removed. It is part of the Drupal 4.6 to 4.7 migration.
261 function update_fix_watchdog() {
262 if (drupal_get_installed_schema_version('system') < 142 && !variable_get('update_watchdog_fixed', FALSE)) {
263 switch ($GLOBALS['db_type']) {
264 case 'pgsql':
265 $ret = array();
266 db_add_column($ret, 'watchdog', 'referer', 'varchar(128)', array('not null' => TRUE, 'default' => "''"));
267 break;
268 case 'mysql':
269 case 'mysqli':
270 db_query("ALTER TABLE {watchdog} ADD COLUMN referer varchar(128) NOT NULL");
271 break;
274 variable_set('update_watchdog_fixed', TRUE);
279 * Perform one update and store the results which will later be displayed on
280 * the finished page.
282 * @param $module
283 * The module whose update will be run.
284 * @param $number
285 * The update number to run.
287 * @return
288 * TRUE if the update was finished. Otherwise, FALSE.
290 function update_data($module, $number) {
291 $ret = module_invoke($module, 'update_'. $number);
292 // Assume the update finished unless the update results indicate otherwise.
293 $finished = 1;
294 if (isset($ret['#finished'])) {
295 $finished = $ret['#finished'];
296 unset($ret['#finished']);
299 // Save the query and results for display by update_finished_page().
300 if (!isset($_SESSION['update_results'])) {
301 $_SESSION['update_results'] = array();
303 if (!isset($_SESSION['update_results'][$module])) {
304 $_SESSION['update_results'][$module] = array();
306 if (!isset($_SESSION['update_results'][$module][$number])) {
307 $_SESSION['update_results'][$module][$number] = array();
309 $_SESSION['update_results'][$module][$number] = array_merge($_SESSION['update_results'][$module][$number], $ret);
311 if ($finished == 1) {
312 // Update the installed version
313 drupal_set_installed_schema_version($module, $number);
316 return $finished;
319 function update_selection_page() {
320 $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>';
321 $output .= '<p>Click Update to start the update process.</p>';
323 drupal_set_title('Drupal database update');
324 // Prevent browser from using cached drupal.js or update.js
325 drupal_add_js('misc/update.js', 'core', 'header', FALSE, TRUE);
326 $output .= drupal_get_form('update_script_selection_form');
328 return $output;
331 function update_script_selection_form() {
332 $form = array();
333 $form['start'] = array(
334 '#tree' => TRUE,
335 '#type' => 'fieldset',
336 '#title' => 'Select versions',
337 '#collapsible' => TRUE,
338 '#collapsed' => TRUE,
341 // Ensure system.module's updates appear first
342 $form['start']['system'] = array();
344 foreach (module_list() as $module) {
345 $updates = drupal_get_schema_versions($module);
346 if ($updates !== FALSE) {
347 $updates = drupal_map_assoc($updates);
348 $updates[] = 'No updates available';
349 $default = drupal_get_installed_schema_version($module);
350 foreach (array_keys($updates) as $update) {
351 if ($update > $default) {
352 $default = $update;
353 break;
357 $form['start'][$module] = array(
358 '#type' => 'select',
359 '#title' => $module . ' module',
360 '#default_value' => $default,
361 '#options' => $updates,
366 $form['has_js'] = array(
367 '#type' => 'hidden',
368 '#default_value' => FALSE,
369 '#attributes' => array('id' => 'edit-has_js'),
371 $form['submit'] = array(
372 '#type' => 'submit',
373 '#value' => 'Update',
375 return $form;
378 function update_update_page() {
379 // Set the installed version so updates start at the correct place.
380 foreach ($_POST['start'] as $module => $version) {
381 drupal_set_installed_schema_version($module, $version - 1);
382 $updates = drupal_get_schema_versions($module);
383 $max_version = max($updates);
384 if ($version <= $max_version) {
385 foreach ($updates as $update) {
386 if ($update >= $version) {
387 $_SESSION['update_remaining'][] = array('module' => $module, 'version' => $update);
393 // Keep track of total number of updates
394 $_SESSION['update_total'] = count($_SESSION['update_remaining']);
396 if ($_POST['has_js']) {
397 return update_progress_page();
399 else {
400 return update_progress_page_nojs();
404 function update_progress_page() {
405 // Prevent browser from using cached drupal.js or update.js
406 drupal_add_js('misc/progress.js', 'core', 'header', FALSE, TRUE);
407 drupal_add_js('misc/update.js', 'core', 'header', FALSE, TRUE);
409 drupal_set_title('Updating');
410 $output = '<div id="progress"></div>';
411 $output .= '<p id="wait">Please wait while your site is being updated.</p>';
412 return $output;
416 * Perform updates for one second or until finished.
418 * @return
419 * An array indicating the status after doing updates. The first element is
420 * the overall percentage finished. The second element is a status message.
422 function update_do_updates() {
423 while (isset($_SESSION['update_remaining']) && ($update = reset($_SESSION['update_remaining']))) {
424 $update_finished = update_data($update['module'], $update['version']);
425 if ($update_finished == 1) {
426 // Dequeue the completed update.
427 unset($_SESSION['update_remaining'][key($_SESSION['update_remaining'])]);
428 $update_finished = 0; // Make sure this step isn't counted double
430 if (timer_read('page') > 1000) {
431 break;
435 if ($_SESSION['update_total']) {
436 $percentage = floor(($_SESSION['update_total'] - count($_SESSION['update_remaining']) + $update_finished) / $_SESSION['update_total'] * 100);
438 else {
439 $percentage = 100;
442 // When no updates remain, clear the caches in case the data has been updated.
443 if (!isset($update['module'])) {
444 cache_clear_all('*', 'cache', TRUE);
445 cache_clear_all('*', 'cache_page', TRUE);
446 cache_clear_all('*', 'cache_menu', TRUE);
447 cache_clear_all('*', 'cache_filter', TRUE);
448 drupal_clear_css_cache();
451 return array($percentage, isset($update['module']) ? 'Updating '. $update['module'] .' module' : 'Updating complete');
455 * Perform updates for the JS version and return progress.
457 function update_do_update_page() {
458 global $conf;
460 // HTTP Post required
461 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
462 drupal_set_message('HTTP Post is required.', 'error');
463 drupal_set_title('Error');
464 return '';
467 // Error handling: if PHP dies, the output will fail to parse as JSON, and
468 // the Javascript will tell the user to continue to the op=error page.
469 list($percentage, $message) = update_do_updates();
470 print drupal_to_js(array('status' => TRUE, 'percentage' => $percentage, 'message' => $message));
474 * Perform updates for the non-JS version and return the status page.
476 function update_progress_page_nojs() {
477 drupal_set_title('Updating');
479 $new_op = 'do_update_nojs';
480 if ($_SERVER['REQUEST_METHOD'] == 'GET') {
481 // Error handling: if PHP dies, it will output whatever is in the output
482 // buffer, followed by the error message.
483 ob_start();
484 $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>';
485 print theme('maintenance_page', $fallback, FALSE, TRUE);
487 list($percentage, $message) = update_do_updates();
488 if ($percentage == 100) {
489 $new_op = 'finished';
492 // Updates successful; remove fallback
493 ob_end_clean();
495 else {
496 // This is the first page so return some output immediately.
497 $percentage = 0;
498 $message = 'Starting updates';
501 drupal_set_html_head('<meta http-equiv="Refresh" content="0; URL=update.php?op='. $new_op .'">');
502 $output = theme('progress_bar', $percentage, $message);
503 $output .= '<p>Updating your site will take a few seconds.</p>';
505 // Note: do not output drupal_set_message()s until the summary page.
506 print theme('maintenance_page', $output, FALSE);
507 return NULL;
510 function update_finished_page($success) {
511 drupal_set_title('Drupal database update');
512 // NOTE: we can't use l() here because the URL would point to 'update.php?q=admin'.
513 $links[] = '<a href="'. base_path() .'">main page</a>';
514 $links[] = '<a href="'. base_path() .'?q=admin">administration pages</a>';
516 // Report end result
517 if ($success) {
518 $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/watchdog">logged</a>.</p>';
520 else {
521 $update = reset($_SESSION['update_remaining']);
522 $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/watchdog">logged</a>. You may need to check the <code>watchdog</code> database table manually.</p>';
525 if ($GLOBALS['access_check'] == FALSE) {
526 $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>";
529 $output .= theme('item_list', $links);
531 // Output a list of queries executed
532 if ($_SESSION['update_results']) {
533 $output .= '<div id="update-results">';
534 $output .= '<h2>The following queries were executed</h2>';
535 foreach ($_SESSION['update_results'] as $module => $updates) {
536 $output .= '<h3>'. $module .' module</h3>';
537 foreach ($updates as $number => $queries) {
538 $output .= '<h4>Update #'. $number .'</h4>';
539 $output .= '<ul>';
540 foreach ($queries as $query) {
541 if ($query['success']) {
542 $output .= '<li class="success">'. $query['query'] .'</li>';
544 else {
545 $output .= '<li class="failure"><strong>Failed:</strong> '. $query['query'] .'</li>';
548 if (!count($queries)) {
549 $output .= '<li class="none">No queries</li>';
551 $output .= '</ul>';
554 $output .= '</div>';
555 unset($_SESSION['update_results']);
558 return $output;
561 function update_info_page() {
562 drupal_set_title('Drupal database update');
563 $output = "<ol>\n";
564 $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>";
565 $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";
566 $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";
567 $output .= "<li>Go through the various administration pages to change the existing and new settings to your liking.</li>\n";
568 $output .= "</ol>";
569 $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>';
570 return $output;
573 function update_access_denied_page() {
574 drupal_set_title('Access denied');
575 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>
576 <ol>
577 <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>
578 <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>
579 <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>
580 <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>
581 </ol>';
584 // This code may be removed later. It is part of the Drupal 4.5 to 4.8 migration.
585 function update_fix_system_table() {
586 drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
587 $core_modules = array('aggregator', 'archive', 'block', 'blog', 'blogapi', 'book', 'comment', 'contact', 'drupal', 'filter', 'forum', 'help', 'legacy', 'locale', 'menu', 'node', 'page', 'path', 'ping', 'poll', 'profile', 'search', 'statistics', 'story', 'system', 'taxonomy', 'throttle', 'tracker', 'upload', 'user', 'watchdog');
588 foreach ($core_modules as $module) {
589 $old_path = "modules/$module.module";
590 $new_path = "modules/$module/$module.module";
591 db_query("UPDATE {system} SET filename = '%s' WHERE filename = '%s'", $new_path, $old_path);
593 $row = db_fetch_object(db_query_range('SELECT * FROM {system}', 0, 1));
594 if (!isset($row->weight)) {
595 $ret = array();
596 switch ($GLOBALS['db_type']) {
597 case 'pgsql':
598 db_add_column($ret, 'system', 'weight', 'smallint', array('not null' => TRUE, 'default' => 0));
599 $ret[] = update_sql('CREATE INDEX {system}_weight_idx ON {system} (weight)');
600 break;
601 case 'mysql':
602 case 'mysqli':
603 $ret[] = update_sql("ALTER TABLE {system} ADD weight tinyint(2) default '0' NOT NULL, ADD KEY (weight)");
604 break;
609 // This code may be removed later. It is part of the Drupal 4.6 to 4.7 migration.
610 function update_fix_access_table() {
611 if (variable_get('update_access_fixed', FALSE)) {
612 return;
615 switch ($GLOBALS['db_type']) {
616 // Only for MySQL 4.1+
617 case 'mysqli':
618 break;
619 case 'mysql':
620 if (version_compare(mysql_get_server_info($GLOBALS['active_db']), '4.1.0', '<')) {
621 return;
623 break;
624 case 'pgsql':
625 return;
628 // Convert access table to UTF-8 if needed.
629 $result = db_fetch_array(db_query('SHOW CREATE TABLE {access}'));
630 if (!preg_match('/utf8/i', array_pop($result))) {
631 update_convert_table_utf8('access');
634 // Don't run again
635 variable_set('update_access_fixed', TRUE);
639 * Convert a single MySQL table to UTF-8.
641 * We change all text columns to their corresponding binary type,
642 * then back to text, but with a UTF-8 character set.
643 * See: http://dev.mysql.com/doc/refman/4.1/en/charset-conversion.html
645 function update_convert_table_utf8($table) {
646 $ret = array();
647 $types = array('char' => 'binary',
648 'varchar' => 'varbinary',
649 'tinytext' => 'tinyblob',
650 'text' => 'blob',
651 'mediumtext' => 'mediumblob',
652 'longtext' => 'longblob');
654 // Get next table in list
655 $convert_to_binary = array();
656 $convert_to_utf8 = array();
658 // Set table default charset
659 $ret[] = update_sql('ALTER TABLE {'. $table .'} DEFAULT CHARACTER SET utf8');
661 // Find out which columns need converting and build SQL statements
662 $result = db_query('SHOW FULL COLUMNS FROM {'. $table .'}');
663 while ($column = db_fetch_array($result)) {
664 list($type) = explode('(', $column['Type']);
665 if (isset($types[$type])) {
666 $names = 'CHANGE `'. $column['Field'] .'` `'. $column['Field'] .'` ';
667 $attributes = ' DEFAULT '. ($column['Default'] == 'NULL' ? 'NULL ' :
668 "'". db_escape_string($column['Default']) ."' ") .
669 ($column['Null'] == 'YES' ? 'NULL' : 'NOT NULL');
671 $convert_to_binary[] = $names . preg_replace('/'. $type .'/i', $types[$type], $column['Type']) . $attributes;
672 $convert_to_utf8[] = $names . $column['Type'] .' CHARACTER SET utf8'. $attributes;
676 if (count($convert_to_binary)) {
677 // Convert text columns to binary
678 $ret[] = update_sql('ALTER TABLE {'. $table .'} '. implode(', ', $convert_to_binary));
679 // Convert binary columns to UTF-8
680 $ret[] = update_sql('ALTER TABLE {'. $table .'} '. implode(', ', $convert_to_utf8));
682 return $ret;
686 * Create tables for the split cache.
688 * This is part of the Drupal 4.7.x to 5.x migration.
690 function update_create_cache_tables() {
692 // If cache_filter exists, update is not necessary
693 if (db_table_exists('cache_filter')) {
694 return;
697 $ret = array();
698 switch ($GLOBALS['db_type']) {
699 case 'mysql':
700 case 'mysqli':
701 $ret[] = update_sql("CREATE TABLE {cache_filter} (
702 cid varchar(255) NOT NULL default '',
703 data longblob,
704 expire int NOT NULL default '0',
705 created int NOT NULL default '0',
706 headers text,
707 PRIMARY KEY (cid),
708 INDEX expire (expire)
709 ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
710 $ret[] = update_sql("CREATE TABLE {cache_menu} (
711 cid varchar(255) NOT NULL default '',
712 data longblob,
713 expire int NOT NULL default '0',
714 created int NOT NULL default '0',
715 headers text,
716 PRIMARY KEY (cid),
717 INDEX expire (expire)
718 ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
719 $ret[] = update_sql("CREATE TABLE {cache_page} (
720 cid varchar(255) BINARY NOT NULL default '',
721 data longblob,
722 expire int NOT NULL default '0',
723 created int NOT NULL default '0',
724 headers text,
725 PRIMARY KEY (cid),
726 INDEX expire (expire)
727 ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
728 break;
729 case 'pgsql':
730 $ret[] = update_sql("CREATE TABLE {cache_filter} (
731 cid varchar(255) NOT NULL default '',
732 data bytea,
733 expire int NOT NULL default '0',
734 created int NOT NULL default '0',
735 headers text,
736 PRIMARY KEY (cid)
737 )");
738 $ret[] = update_sql("CREATE TABLE {cache_menu} (
739 cid varchar(255) NOT NULL default '',
740 data bytea,
741 expire int NOT NULL default '0',
742 created int NOT NULL default '0',
743 headers text,
744 PRIMARY KEY (cid)
745 )");
746 $ret[] = update_sql("CREATE TABLE {cache_page} (
747 cid varchar(255) NOT NULL default '',
748 data bytea,
749 expire int NOT NULL default '0',
750 created int NOT NULL default '0',
751 headers text,
752 PRIMARY KEY (cid)
753 )");
754 $ret[] = update_sql("CREATE INDEX {cache_filter}_expire_idx ON {cache_filter} (expire)");
755 $ret[] = update_sql("CREATE INDEX {cache_menu}_expire_idx ON {cache_menu} (expire)");
756 $ret[] = update_sql("CREATE INDEX {cache_page}_expire_idx ON {cache_page} (expire)");
757 break;
759 return $ret;
762 // Some unavoidable errors happen because the database is not yet up-to-date.
763 // Our custom error handler is not yet installed, so we just suppress them.
764 ini_set('display_errors', FALSE);
766 include_once './includes/bootstrap.inc';
767 update_fix_system_table();
769 drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
770 drupal_maintenance_theme();
772 // This must happen *after* drupal_bootstrap(), since it calls
773 // variable_(get|set), which only works after a full bootstrap.
774 update_fix_access_table();
775 update_create_cache_tables();
777 // Turn error reporting back on. From now on, only fatal errors (which are
778 // not passed through the error handler) will cause a message to be printed.
779 ini_set('display_errors', TRUE);
781 // Access check:
782 if (($access_check == FALSE) || ($user->uid == 1)) {
784 include_once './includes/install.inc';
785 drupal_load_updates();
787 update_fix_schema_version();
788 update_fix_watchdog_115();
789 update_fix_watchdog();
790 update_fix_sessions();
792 $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : '';
793 switch ($op) {
794 case 'Update':
795 $output = update_update_page();
796 break;
798 case 'finished':
799 $output = update_finished_page(TRUE);
800 break;
802 case 'error':
803 $output = update_finished_page(FALSE);
804 break;
806 case 'do_update':
807 $output = update_do_update_page();
808 break;
810 case 'do_update_nojs':
811 $output = update_progress_page_nojs();
812 break;
814 case 'selection':
815 $output = update_selection_page();
816 break;
818 default:
819 $output = update_info_page();
820 break;
823 else {
824 $output = update_access_denied_page();
827 if (isset($output)) {
828 print theme('maintenance_page', $output);