update.php 16.1 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9
/*
** USAGE:
**
** - Point your browser to "http://www.site.com/update.php" and follow
**   the instructions.
**
*/

10 11 12 13 14 15
if (!get_cfg_var("safe_mode")) {
  set_time_limit(180);
}

// Define the various updates in an array("date : comment" => "function");
$mysql_updates = array(
16
  "2002-06-22: first update since Drupal 4.0.0 release" => "update_32",
17
  "2002-07-07" => "update_33",
18
  "2002-07-31" => "update_34",
19
  "2002-08-10" => "update_35",
20
  "2002-08-16" => "update_36",
21
  "2002-08-19" => "update_37",
22
  "2002-08-26" => "update_38",
23
  "2002-09-15" => "update_39",
24
  "2002-09-17" => "update_40",
25
  "2002-10-13" => "update_41",
Dries's avatar
Dries committed
26
  "2002-10-17" => "update_42",
Dries's avatar
Dries committed
27
  "2002-10-26" => "update_43",
28
  "2002-11-08" => "update_44",
Dries's avatar
Dries committed
29
  "2002-11-20" => "update_45",
30
  "2002-12-10: first update since Drupal 4.1.0 release" => "update_46",
31 32 33
  "2002-12-29" => "update_47",
  "2003-01-03" => "update_48",
  "2003-01-05" => "update_49",
34
  "2003-01-15" => "update_50",
35
  "2003-04-19" => "update_51",
36
  "2003-04-20" => "update_52",
37
  "2003-05-18" => "update_53",
38
  "2003-05-24" => "update_54",
39
  "2003-05-31" => "update_55",
40
  "2003-06-04" => "update_56",
41
  "2003-06-08" => "update_57",
42
  "2003-06-08: first update since Drupal 4.2.0 release" => "update_58",
43
  "2003-08-05" => "update_59",
44
  "2003-08-15" => "update_60",
45
  "2003-08-20" => "update_61",
46
  "2003-08-27" => "update_62",
Dries's avatar
Dries committed
47 48
  "2003-09-09" => "update_63",
  "2003-09-10" => "update_64"
49 50
);

Dries's avatar
Dries committed
51
function update_32() {
52 53
  update_sql("ALTER TABLE users ADD index (sid(4))");
  update_sql("ALTER TABLE users ADD index (timestamp)");
Dries's avatar
Dries committed
54
  update_sql("ALTER TABLE users ADD UNIQUE KEY name (name)");
Dries's avatar
Dries committed
55 56
}

Dries's avatar
Dries committed
57
function update_33() {
58
  $result = db_query("SELECT * FROM variable WHERE value NOT LIKE 's:%;'");
59
  // NOTE: the "WHERE"-part of the query above avoids variables to get serialized twice.
Dries's avatar
Dries committed
60 61 62 63 64
  while ($variable = db_fetch_object($result)) {
    variable_set($variable->name, $variable->value);
  }
}

65
function update_34() {
66 67
  update_sql("ALTER TABLE feed MODIFY refresh int(10) NOT NULL default '0'");
  update_sql("ALTER TABLE feed MODIFY timestamp int (10) NOT NULL default '0'");
68
  update_sql("ALTER TABLE users CHANGE session session TEXT");
69 70
}

71
function update_35() {
72
  update_sql("ALTER TABLE poll_choices ADD INDEX (nid)");
73 74
}

75
function update_36() {
76 77
  update_sql("ALTER TABLE rating CHANGE old previous int(6) NOT NULL default '0'");
  update_sql("ALTER TABLE rating CHANGE new current int(6) NOT NULL default '0'");
78 79
}

80 81
function update_37() {

82
  update_sql("DROP TABLE IF EXISTS sequences");
83 84

  update_sql("CREATE TABLE sequences (
85
    name VARCHAR(255) NOT NULL PRIMARY KEY,
86
    id INT UNSIGNED NOT NULL
87
  ) TYPE=MyISAM");
88

89 90
  if ($max = db_result(db_query("SELECT MAX(nid) FROM node"))) {
    update_sql("REPLACE INTO sequences VALUES ('node', $max)");
91 92
  }

93 94
  if ($max = db_result(db_query("SELECT MAX(cid) FROM comments"))) {
    update_sql("REPLACE INTO sequences VALUES ('comments', $max)");
95 96 97
  }
  // NOTE: move the comments bit down as soon as we switched to use the new comment module!

98 99
  if ($max = db_result(db_query("SELECT MAX(tid) FROM term_data"))) {
    update_sql("REPLACE INTO sequences VALUES ('term_data', $max)");
100 101 102
  }
}

103
function update_38() {
104
  update_sql("ALTER TABLE watchdog CHANGE message message text NOT NULL default ''");
105 106
}

107 108 109
function update_39() {
  update_sql("DROP TABLE moderate");

110 111 112
  update_sql("ALTER TABLE comments ADD score MEDIUMINT NOT NULL");
  update_sql("ALTER TABLE comments ADD status TINYINT UNSIGNED NOT NULL");
  update_sql("ALTER TABLE comments ADD users MEDIUMTEXT");
113 114 115 116 117

  update_sql("CREATE TABLE moderation_votes (
    mid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    vote VARCHAR(255),
    weight TINYINT NOT NULL
118
  )");
119 120 121 122 123

  update_sql("CREATE TABLE moderation_roles (
    rid INT UNSIGNED NOT NULL,
    mid INT UNSIGNED NOT NULL,
    value TINYINT NOT NULL
124
  )");
125

126 127
  update_sql("ALTER TABLE moderation_roles ADD INDEX (rid)");
  update_sql("ALTER TABLE moderation_roles ADD INDEX (mid)");
128 129 130 131 132

  update_sql("CREATE TABLE moderation_filters (
    fid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    filter VARCHAR(255) NOT NULL,
    minimum SMALLINT NOT NULL
133
  )");
134

135 136 137
  update_sql("DELETE FROM moderation_votes");
  update_sql("INSERT INTO moderation_votes VALUES (1, '+1', 0)");
  update_sql("INSERT INTO moderation_votes VALUES (2, '-1', 1)");
138

139 140 141
  update_sql("DELETE FROM moderation_roles");
  update_sql("INSERT INTO moderation_roles VALUES (2, 1, 1)");
  update_sql("INSERT INTO moderation_roles VALUES (2, 2, -1)");
142 143 144 145 146

  update_sql("CREATE TABLE forum (
    nid int unsigned not null primary key,
    icon varchar(255) not null,
    shadow int unsigned not null
147
  )");
148 149
}

150
function update_40() {
151 152
  if ($max = db_result(db_query("SELECT MAX(cid) FROM comments"))) {
    update_sql("REPLACE INTO sequences VALUES ('comments', $max)");
153 154 155
  }
}

156
function update_41() {
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
  update_sql("CREATE TABLE statistics (
    nid int(11) NOT NULL,
    totalcount bigint UNSIGNED DEFAULT '0' NOT NULL,
    daycount mediumint UNSIGNED DEFAULT '0' NOT NULL,
    timestamp int(11) UNSIGNED DEFAULT '0' NOT NULL,
    PRIMARY KEY (nid),
    INDEX (totalcount),
    INDEX (daycount),
    INDEX (timestamp)
  )");

  update_sql("CREATE TABLE accesslog (
    nid int(11) UNSIGNED DEFAULT '0',
    url varchar(255),
    hostname varchar(128),
    uid int(10) UNSIGNED DEFAULT '0',
    timestamp int(11) UNSIGNED NOT NULL
  )");
175 176
}

177 178 179 180 181 182
function update_42() {
  update_sql("DROP TABLE modules");
  update_sql("DROP TABLE layout");
  update_sql("DROP TABLE referrer");
}

Dries's avatar
Dries committed
183 184 185
function update_43() {
  update_sql("ALTER TABLE blocks DROP remove");
  update_sql("ALTER TABLE blocks DROP name");
Kjartan's avatar
Kjartan committed
186 187
  update_sql("UPDATE boxes SET type = 0 WHERE type = 1");
  update_sql("UPDATE boxes SET type = 1 WHERE type = 2");
Dries's avatar
Dries committed
188 189
}

Dries's avatar
Dries committed
190 191 192 193
function update_44() {
  update_sql("UPDATE system SET filename = CONCAT('modules/', filename) WHERE type = 'module'");
}

194 195 196 197
function update_45() {
  update_sql("ALTER TABLE page ADD description varchar(128) NOT NULL default ''");
}

Dries's avatar
Dries committed
198 199 200 201
function update_46() {
  update_sql("ALTER TABLE cache ADD created int(11) NOT NULL default '0'");
}

202
function update_47() {
Dries's avatar
Dries committed
203
  if ($max = db_result(db_query("SELECT MAX(vid) FROM vocabulary"))) {
Dries's avatar
Dries committed
204 205 206 207
    update_sql("REPLACE INTO sequences VALUES ('vocabulary', $max)");
  }
}

208
function update_48() {
209 210 211
  update_sql("ALTER TABLE watchdog ADD link varchar(255) DEFAULT '' NULL");
}

212 213 214 215 216 217 218 219
function update_49() {
  /*
  ** Make sure the admin module is added to the system table or the
  ** admin menus won't show up.
  */

  update_sql("DELETE FROM system WHERE name = 'admin';");
  update_sql("INSERT INTO system VALUES ('modules/admin.module','admin','module','',1)");
Dries's avatar
Dries committed
220 221
}

222
function update_50() {
223
  update_sql("ALTER TABLE forum ADD tid INT UNSIGNED NOT NULL");
Dries's avatar
Dries committed
224 225
  $result = db_queryd("SELECT n.nid, t.tid FROM node n, term_node t WHERE n.nid = t.nid AND type = 'forum'");
  while ($node = db_fetch_object($result)) {
226
    db_queryd("UPDATE forum SET tid = %d WHERE nid = %d", $node->tid, $node->nid);
Dries's avatar
Dries committed
227 228 229 230
  }
  update_sql("ALTER TABLE forum ADD INDEX (tid)");
}

231 232 233 234
function update_51() {
  update_sql("ALTER TABLE blocks CHANGE delta delta varchar(32) NOT NULL default '0'");
}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
function update_52() {
  update_sql("UPDATE sequences SET name = 'comments_cid' WHERE name = 'comments';");
  update_sql("UPDATE sequences SET name = 'node_nid' WHERE name = 'node';");

  update_sql("DELETE FROM sequences WHERE name = 'import'");
  update_sql("DELETE FROM sequences WHERE name = 'bundle_bid'");  // in case we would run this entry twice
  update_sql("DELETE FROM sequences WHERE name = 'feed_fid'");    // in case we would run this entry twice

  $bundles = db_result(db_query("SELECT MAX(bid) FROM bundle;"));
  update_sql("INSERT INTO sequences (name, id) VALUES ('bundle_bid', '$bundles')");

  $feeds = db_result(db_query("SELECT MAX(fid) FROM feed;"));
  update_sql("INSERT INTO sequences (name, id) VALUES ('feed_fid', '$feeds')");

  update_sql("UPDATE sequences SET name = 'vocabulary_vid' WHERE name = 'vocabulary';");

  update_sql("UPDATE sequences SET name = 'term_data_tid' WHERE name = 'term_data'");
}

254 255 256 257
function update_53() {
  update_sql("CREATE INDEX book_parent ON book(parent);");
}

258 259 260 261
function update_54() {
  update_sql("ALTER TABLE locales CHANGE string string BLOB DEFAULT '' NOT NULL");
}

262 263 264 265 266
function update_55() {
  update_sql("ALTER TABLE site ADD checked INT(11) NOT NULL;");
  update_sql("ALTER TABLE site CHANGE timestamp changed INT(11) NOT NULL;");
}

267 268 269 270
function update_56() {
  update_sql("ALTER TABLE vocabulary CHANGE types nodes TEXT DEFAULT '' NOT NULL");
}

271 272 273 274
function update_57() {
  update_sql("DELETE FROM variable WHERE name = 'site_charset'");
}

275 276 277 278
function update_58() {
  update_sql("ALTER TABLE node ADD path varchar(250) NULL default ''");
}

279 280
function update_59() {

281
  update_sql("ALTER TABLE {comments} ADD thread VARCHAR(255) NOT NULL");
282

283
  $result = db_query("SELECT DISTINCT(nid) FROM {comments} WHERE thread = ''");
284 285

  while ($node = db_fetch_object($result)) {
286
    $result2 = db_query("SELECT cid, pid FROM {comments} where nid = '%d' ORDER BY timestamp", $node->nid);
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    $comments = array();
    while ($comment = db_fetch_object($result2)) {
      $comments[$comment->cid] = $comment;
    }

    $structure = array();
    $structure = _update_thread_structure($comments, 0, -1, $structure);

    foreach ($structure as $cid => $thread) {
      $new_parts = array();
      foreach(explode(".", $thread) as $part) {
        if ($part > 9) {
          $start = substr($part, 0, strlen($part) - 1);
          $end = substr($part, -1, 1);

          $new_parts[] = str_repeat("9", $start).$end;
        }
        else {
          $new_parts[] = $part;
        }
      }
      $thread = implode(".", $new_parts);

310
      db_query("UPDATE {comments} SET thread = '%s' WHERE cid = '%d'", $thread."/", $comments[$cid]->cid);
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    }
  }
}

function _update_thread_structure($comments, $pid, $depth, $structure) {
  $depth++;

  foreach ($comments as $key => $comment) {
    if ($comment->pid == $pid) {
      if ($structure[$comment->pid]) {
        $structure[$comment->cid] = $structure[$comment->pid]."."._update_next_thread($structure, $structure[$comment->pid]);
      }
      else {
        $structure[$comment->cid] = _update_next_thread($structure, "");
      }

      $structure = _update_thread_structure($comments, $comment->cid, $depth, $structure);
    }
  }

  return $structure;
}

334
function update_60() {
335
  update_sql("ALTER TABLE {forum} DROP icon");
336 337
}

338
function update_61() {
339
  update_sql("CREATE TABLE IF NOT EXISTS {sessions} (
340 341 342 343 344 345 346 347 348 349
    uid int(10) unsigned NOT NULL,
    sid varchar(32) NOT NULL default '',
    hostname varchar(128) NOT NULL default '',
    timestamp int(11) NOT NULL default '0',
    session text,
    KEY uid (uid),
    KEY sid (sid(4)),
    KEY timestamp (timestamp)
  )");

350 351 352
  update_sql("ALTER TABLE {users} DROP session;");
  update_sql("ALTER TABLE {users} DROP hostname;");
  update_sql("ALTER TABLE {users} DROP sid;");
353 354
}

355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
function update_62() {
  update_sql("ALTER TABLE accesslog ADD INDEX accesslog_timestamp (timestamp)");

  update_sql("ALTER TABLE node DROP INDEX type");
  update_sql("ALTER TABLE node DROP INDEX title");
  update_sql("ALTER TABLE node DROP INDEX promote");
  update_sql("ALTER TABLE node DROP INDEX type");

  update_sql("ALTER TABLE node ADD INDEX node_type (type(4))");
  update_sql("ALTER TABLE node ADD INDEX node_title_type (title,type(4))");
  update_sql("ALTER TABLE node ADD INDEX node_moderate (moderate)");
  update_sql("ALTER TABLE node ADD INDEX node_path (path(5))");
  update_sql("ALTER TABLE node ADD INDEX node_promote_status (promote, status)");
}

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
function _update_next_thread($structure, $parent) {
  do {
    $val++;
    if ($parent) {
      $thread = "$parent.$val";
    }
    else {
      $thread = $val;
    }

  } while (array_search($thread, $structure));

  return $val;
}

385 386 387 388 389 390 391
function update_63() {
  update_sql("ALTER TABLE users CHANGE uid uid int(10) unsigned NOT NULL default '0'");
  update_sql("INSERT INTO users (uid, name, mail, timestamp) VALUES ('0', 'Anonymous', 'root@localhost', '". time() ."')");
  $users = db_result(db_query("SELECT MAX(uid) FROM users;"));
  update_sql("INSERT INTO sequences (name, id) VALUES ('users_uid', '$users')");
}

Dries's avatar
Dries committed
392 393 394 395
function update_64() {
  update_sql("UPDATE users SET rid = 1 WHERE uid = 0");
}

396 397 398 399
/*
** System functions
*/

400
function update_sql($sql) {
401
  $edit = $_POST["edit"];
Kjartan's avatar
Kjartan committed
402
  print nl2br(htmlentities($sql)) ." ";
403 404
  $result = db_query($sql);
  if ($result) {
405
    print "<div style=\"color: green;\">OK</div>\n";
406 407 408
    return 1;
  }
  else {
409
    print "<div style=\"color: red;\">FAILED</div>\n";
410 411 412 413 414 415 416
    return 0;
  }
}

function update_data($start) {
  global $mysql_updates;
  $mysql_updates = array_slice($mysql_updates, ($start-- ? $start : 0));
417
  foreach ($mysql_updates as $date => $func) {
418 419 420 421 422 423 424
    print "<b>$date</b><br />\n<pre>\n";
    $func();
    variable_set("update_start", $date);
    print "</pre>\n";
  }
}

425 426 427 428 429 430 431 432 433
function update_page_header($title) {
  $output = "<html><head><title>$title</title>";
  $output .= <<<EOF
      <link rel="stylesheet" type="text/css" media="print" href="misc/print.css" />
      <style type="text/css" title="layout" media="Screen">
        @import url("misc/admin.css");
      </style>
EOF;
  $output .= "</head><body><a href=\"http://drupal.org/\">";
434
  $output .= "<div id=\"logo\"><a href=\"http://drupal.org/\"><img src=\"misc/druplicon-small.gif\" alt=\"Druplicon - Drupal logo\" title=\"Druplicon - Drupal logo\" /></a></div>";
435 436 437 438 439 440 441 442
  $output .= "<div id=\"update\"><h1>$title</h1>";
  return $output;
}

function update_page_footer() {
  return "</div></body></html>";
}

443
function update_page() {
444 445 446
  global $user, $mysql_updates;

  $edit = $_POST["edit"];
447

448
  switch ($_POST["op"]) {
449 450
    case "Update":
      // make sure we have updates to run.
451
      print update_page_header("Drupal database update");
452 453 454
      print "<b>&raquo; <a href=\"index.php\">main page</a></b><br />\n";
      print "<b>&raquo; <a href=\"index.php?q=admin\">administration pages</a></b><br />\n";
        // NOTE: we can't use l() here because the URL would point to 'update.php?q=admin'.
455 456 457 458 459 460
      if ($edit["start"] == -1) {
        print "No updates to perform.";
      }
      else {
        update_data($edit["start"]);
      }
461
      print "<br />Updates were attempted. If you see no failures above, you may proceed happily to the <a href=\"index.php?q=admin\">administration pages</a>.";
462
      print " Otherwise, you may need to update your database manually.";
463
      print update_page_footer();
464 465 466 467 468
      break;
    default:
      $start = variable_get("update_start", 0);
      $dates[] = "All";
      $i = 1;
469
      foreach ($mysql_updates as $date => $sql) {
470 471 472 473 474
        $dates[$i++] = $date;
        if ($date == $start) {
          $selected = $i;
        }
      }
475
      $dates[$i] = "No updates available";
476 477

      // make update form and output it.
478
      $form .= form_select("Perform updates from", "start", (isset($selected) ? $selected : -1), $dates, "This defaults to the first available update since the last update you peformed.");
479
      $form .= form_submit("Update");
480
      print update_page_header("Drupal database update");
481
      print form($form);
482
      print update_page_footer();
483 484 485 486
      break;
  }
}

487
function update_info() {
488
  print update_page_header("Drupal database update");
Kjartan's avatar
Kjartan committed
489
  print "<ol>\n";
490
  print "<li>Use this script to <b>upgrade an existing Drupal installation</b>.  You don't need this script when installing Drupal from scratch.</li>";
491 492
  print "<li>Before doing anything, backup your database. This process will change your database and its values, and some things might get lost.</li>\n";
  print "<li>Don't run this script twice as it may cause problems.</p></li>\n";
493
  print "<li><a href=\"update.php?op=update\">Upgrade to the latest version.</a></li>\n";
494
  print "<li>Go through the various administration pages to change the existing and new settings to your liking.</li>\n";
Kjartan's avatar
Kjartan committed
495
  print "</ol>";
496
  print update_page_footer();
497
}
498

499
if (isset($_GET["op"])) {
Kjartan's avatar
Kjartan committed
500
  include_once "includes/common.inc";
501

502 503
  // Access check:
  if ($user->uid == 1) {
Kjartan's avatar
Kjartan committed
504 505 506
    update_page();
  }
  else {
507
    print update_page_header("Access denied");
508
    print "Access denied.  You are not authorized to access to this page.  Please log in as the user with user ID #1 or edit <code>update.php</code> to by-pass this access check; search for <code>\$user->uid == 1</code> near the bottom of the file.";
509
    print update_page_footer();
Kjartan's avatar
Kjartan committed
510 511 512 513
  }
}
else {
  update_info();
514
}
515
?>