update.php 27.3 KB
Newer Older
1
<?php
Dries's avatar
   
Dries committed
2
// $Id$
Dries's avatar
   
Dries committed
3
4
5
6
7
8
/*
** USAGE:
**
** - Point your browser to "http://www.site.com/update.php" and follow
**   the instructions.
**
Dries's avatar
   
Dries committed
9
10
11
12
** - If you are not logged in as administrator, you will need to modify the
**   statement below. Change the 1 into a 0 to disable the access check.
**   After finishing the upgrade, open this file and change the 0 back into
**   a 1!
Dries's avatar
   
Dries committed
13
14
*/

Dries's avatar
   
Dries committed
15
16
17
// Disable access checking?
$access_check = 1;

18
if (!ini_get("safe_mode")) {
19
20
21
22
23
  set_time_limit(180);
}

// Define the various updates in an array("date : comment" => "function");
$mysql_updates = array(
Dries's avatar
   
Dries committed
24
  "2002-06-22: first update since Drupal 4.0.0 release" => "update_32",
25
  "2002-07-07" => "update_33",
Dries's avatar
   
Dries committed
26
  "2002-07-31" => "update_34",
Dries's avatar
   
Dries committed
27
  "2002-08-10" => "update_35",
Dries's avatar
   
Dries committed
28
  "2002-08-16" => "update_36",
Dries's avatar
   
Dries committed
29
  "2002-08-19" => "update_37",
Dries's avatar
   
Dries committed
30
  "2002-08-26" => "update_38",
Dries's avatar
   
Dries committed
31
  "2002-09-15" => "update_39",
Dries's avatar
   
Dries committed
32
  "2002-09-17" => "update_40",
Kjartan's avatar
Kjartan committed
33
  "2002-10-13" => "update_41",
Dries's avatar
   
Dries committed
34
  "2002-10-17" => "update_42",
Dries's avatar
   
Dries committed
35
  "2002-10-26" => "update_43",
Dries's avatar
   
Dries committed
36
  "2002-11-08" => "update_44",
Dries's avatar
   
Dries committed
37
  "2002-11-20" => "update_45",
Dries's avatar
   
Dries committed
38
  "2002-12-10: first update since Drupal 4.1.0 release" => "update_46",
Dries's avatar
   
Dries committed
39
40
41
  "2002-12-29" => "update_47",
  "2003-01-03" => "update_48",
  "2003-01-05" => "update_49",
Dries's avatar
   
Dries committed
42
  "2003-01-15" => "update_50",
Dries's avatar
   
Dries committed
43
  "2003-04-19" => "update_51",
Dries's avatar
   
Dries committed
44
  "2003-04-20" => "update_52",
Dries's avatar
   
Dries committed
45
  "2003-05-18" => "update_53",
Dries's avatar
   
Dries committed
46
  "2003-05-24" => "update_54",
Dries's avatar
   
Dries committed
47
  "2003-05-31" => "update_55",
Dries's avatar
   
Dries committed
48
  "2003-06-04" => "update_56",
Dries's avatar
   
Dries committed
49
  "2003-06-08" => "update_57",
Dries's avatar
   
Dries committed
50
  "2003-06-08: first update since Drupal 4.2.0 release" => "update_58",
Dries's avatar
   
Dries committed
51
  "2003-08-05" => "update_59",
Dries's avatar
   
Dries committed
52
  "2003-08-15" => "update_60",
Dries's avatar
   
Dries committed
53
  "2003-08-20" => "update_61",
Dries's avatar
   
Dries committed
54
  "2003-08-27" => "update_62",
Dries's avatar
   
Dries committed
55
  "2003-09-09" => "update_63",
Dries's avatar
   
Dries committed
56
  "2003-09-10" => "update_64",
Dries's avatar
   
Dries committed
57
  "2003-09-29" => "update_65",
Dries's avatar
   
Dries committed
58
  "2003-09-30" => "update_66",
Dries's avatar
   
Dries committed
59
  "2003-10-11" => "update_67",
Dries's avatar
   
Dries committed
60
  "2003-10-20" => "update_68",
Dries's avatar
   
Dries committed
61
  "2003-10-22" => "update_69",
Dries's avatar
   
Dries committed
62
  "2003-10-27" => "update_70",
Dries's avatar
   
Dries committed
63
  "2003-11-17" => "update_71",
Dries's avatar
   
Dries committed
64
  "2003-11-27" => "update_72",
Dries's avatar
   
Dries committed
65
  "2003-12-03" => "update_73",
Dries's avatar
   
Dries committed
66
  "2003-12-06" => "update_74",
Dries's avatar
   
Dries committed
67
  "2004-01-06" => "update_75",
Dries's avatar
Dries committed
68
  "2004-01-11" => "update_76",
Dries's avatar
   
Dries committed
69
  "2004-01-13" => "update_77",
Dries's avatar
   
Dries committed
70
71
  "2004-02-03" => "update_78",
  "2004-02-21" => "update_79"
72
73
);

Dries's avatar
   
Dries committed
74
function update_32() {
75
76
  update_sql("ALTER TABLE users ADD index (sid(4))");
  update_sql("ALTER TABLE users ADD index (timestamp)");
Dries's avatar
   
Dries committed
77
  update_sql("ALTER TABLE users ADD UNIQUE KEY name (name)");
Dries's avatar
   
Dries committed
78
79
}

Dries's avatar
   
Dries committed
80
function update_33() {
81
  $result = db_query("SELECT * FROM variable WHERE value NOT LIKE 's:%;'");
82
  // NOTE: the "WHERE"-part of the query above avoids variables to get serialized twice.
Dries's avatar
   
Dries committed
83
84
85
86
87
  while ($variable = db_fetch_object($result)) {
    variable_set($variable->name, $variable->value);
  }
}

88
function update_34() {
89
90
  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'");
Dries's avatar
   
Dries committed
91
  update_sql("ALTER TABLE users CHANGE session session TEXT");
92
93
}

Dries's avatar
   
Dries committed
94
function update_35() {
95
  update_sql("ALTER TABLE poll_choices ADD INDEX (nid)");
Dries's avatar
   
Dries committed
96
97
}

Dries's avatar
   
Dries committed
98
function update_36() {
99
100
  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'");
Dries's avatar
   
Dries committed
101
102
}

Dries's avatar
   
Dries committed
103
104
function update_37() {

105
  update_sql("DROP TABLE IF EXISTS sequences");
Dries's avatar
   
Dries committed
106
107

  update_sql("CREATE TABLE sequences (
Dries's avatar
   
Dries committed
108
    name VARCHAR(255) NOT NULL PRIMARY KEY,
Dries's avatar
   
Dries committed
109
    id INT UNSIGNED NOT NULL
110
  ) TYPE=MyISAM");
Dries's avatar
   
Dries committed
111

112
113
  if ($max = db_result(db_query("SELECT MAX(nid) FROM node"))) {
    update_sql("REPLACE INTO sequences VALUES ('node', $max)");
Dries's avatar
   
Dries committed
114
115
  }

116
117
  if ($max = db_result(db_query("SELECT MAX(cid) FROM comments"))) {
    update_sql("REPLACE INTO sequences VALUES ('comments', $max)");
Dries's avatar
   
Dries committed
118
119
120
  }
  // NOTE: move the comments bit down as soon as we switched to use the new comment module!

121
122
  if ($max = db_result(db_query("SELECT MAX(tid) FROM term_data"))) {
    update_sql("REPLACE INTO sequences VALUES ('term_data', $max)");
Dries's avatar
   
Dries committed
123
124
125
  }
}

Dries's avatar
   
Dries committed
126
function update_38() {
127
  update_sql("ALTER TABLE watchdog CHANGE message message text NOT NULL default ''");
Dries's avatar
   
Dries committed
128
129
}

Dries's avatar
   
Dries committed
130
131
132
function update_39() {
  update_sql("DROP TABLE moderate");

133
134
135
  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");
Dries's avatar
   
Dries committed
136
137
138
139
140

  update_sql("CREATE TABLE moderation_votes (
    mid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    vote VARCHAR(255),
    weight TINYINT NOT NULL
141
  )");
Dries's avatar
   
Dries committed
142
143
144
145
146

  update_sql("CREATE TABLE moderation_roles (
    rid INT UNSIGNED NOT NULL,
    mid INT UNSIGNED NOT NULL,
    value TINYINT NOT NULL
147
  )");
Dries's avatar
   
Dries committed
148

149
150
  update_sql("ALTER TABLE moderation_roles ADD INDEX (rid)");
  update_sql("ALTER TABLE moderation_roles ADD INDEX (mid)");
Dries's avatar
   
Dries committed
151
152
153
154
155

  update_sql("CREATE TABLE moderation_filters (
    fid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    filter VARCHAR(255) NOT NULL,
    minimum SMALLINT NOT NULL
156
  )");
Dries's avatar
   
Dries committed
157

158
159
160
  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)");
Dries's avatar
   
Dries committed
161

162
163
164
  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)");
Dries's avatar
   
Dries committed
165
166
167
168
169

  update_sql("CREATE TABLE forum (
    nid int unsigned not null primary key,
    icon varchar(255) not null,
    shadow int unsigned not null
170
  )");
Dries's avatar
   
Dries committed
171
172
}

Dries's avatar
   
Dries committed
173
function update_40() {
174
175
  if ($max = db_result(db_query("SELECT MAX(cid) FROM comments"))) {
    update_sql("REPLACE INTO sequences VALUES ('comments', $max)");
Dries's avatar
   
Dries committed
176
177
178
  }
}

Dries's avatar
   
Dries committed
179
function update_41() {
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
  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
  )");
Dries's avatar
   
Dries committed
198
199
}

Kjartan's avatar
Kjartan committed
200
201
202
203
204
205
function update_42() {
  update_sql("DROP TABLE modules");
  update_sql("DROP TABLE layout");
  update_sql("DROP TABLE referrer");
}

Dries's avatar
   
Dries committed
206
207
208
function update_43() {
  update_sql("ALTER TABLE blocks DROP remove");
  update_sql("ALTER TABLE blocks DROP name");
Kjartan's avatar
Kjartan committed
209
210
  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
211
212
}

Dries's avatar
   
Dries committed
213
214
215
216
function update_44() {
  update_sql("UPDATE system SET filename = CONCAT('modules/', filename) WHERE type = 'module'");
}

Dries's avatar
   
Dries committed
217
218
219
220
function update_45() {
  update_sql("ALTER TABLE page ADD description varchar(128) NOT NULL default ''");
}

Dries's avatar
   
Dries committed
221
function update_46() {
222
223
224
225
226
227
  if ($GLOBALS["db_type"] == "pgsql") {
    update_sql("ALTER TABLE cache ADD created integer");
  }
  else {
    update_sql("ALTER TABLE cache ADD created int(11) NOT NULL default '0'");
  }
Dries's avatar
   
Dries committed
228
229
}

Dries's avatar
   
Dries committed
230
function update_47() {
Dries's avatar
   
Dries committed
231
  if ($max = db_result(db_query("SELECT MAX(vid) FROM vocabulary"))) {
Dries's avatar
   
Dries committed
232
233
234
235
    update_sql("REPLACE INTO sequences VALUES ('vocabulary', $max)");
  }
}

Dries's avatar
   
Dries committed
236
function update_48() {
Dries's avatar
   
Dries committed
237
238
239
  update_sql("ALTER TABLE watchdog ADD link varchar(255) DEFAULT '' NULL");
}

Dries's avatar
   
Dries committed
240
241
242
243
244
245
246
247
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
248
249
}

Dries's avatar
   
Dries committed
250
function update_50() {
Dries's avatar
   
Dries committed
251
  update_sql("ALTER TABLE forum ADD tid INT UNSIGNED NOT NULL");
Dries's avatar
Dries committed
252
253
  $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)) {
Dries's avatar
   
Dries committed
254
    db_queryd("UPDATE forum SET tid = %d WHERE nid = %d", $node->tid, $node->nid);
Dries's avatar
Dries committed
255
256
257
258
  }
  update_sql("ALTER TABLE forum ADD INDEX (tid)");
}

Dries's avatar
   
Dries committed
259
260
261
262
function update_51() {
  update_sql("ALTER TABLE blocks CHANGE delta delta varchar(32) NOT NULL default '0'");
}

Dries's avatar
   
Dries committed
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
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'");
}

Dries's avatar
   
Dries committed
282
283
284
285
function update_53() {
  update_sql("CREATE INDEX book_parent ON book(parent);");
}

Dries's avatar
   
Dries committed
286
287
288
289
function update_54() {
  update_sql("ALTER TABLE locales CHANGE string string BLOB DEFAULT '' NOT NULL");
}

Dries's avatar
   
Dries committed
290
291
292
293
294
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;");
}

Dries's avatar
   
Dries committed
295
296
297
298
function update_56() {
  update_sql("ALTER TABLE vocabulary CHANGE types nodes TEXT DEFAULT '' NOT NULL");
}

Dries's avatar
   
Dries committed
299
300
301
302
function update_57() {
  update_sql("DELETE FROM variable WHERE name = 'site_charset'");
}

Dries's avatar
   
Dries committed
303
function update_58() {
Dries's avatar
   
Dries committed
304
305
306
307
308
309
310
  if ($GLOBALS["db_type"] == "pgsql") {
    update_sql("ALTER TABLE {node} ADD path varchar(250) NULL");
    update_sql("ALTER TABLE {node} ALTER COLUMN path SET DEFAULT ''");
  }
  else {
    update_sql("ALTER TABLE {node} ADD path varchar(250) NULL default ''");
  }
Dries's avatar
   
Dries committed
311
312
}

Dries's avatar
   
Dries committed
313
function update_59() {
Dries's avatar
   
Dries committed
314
315
316
317
318
319
320
  if ($GLOBALS["db_type"] == "pgsql") {
    update_sql("ALTER TABLE {comments} ADD thread VARCHAR(255)");
    update_sql("ALTER TABLE {comments} ALTER COLUMN thread SET NOT NULL");
  }
  else {
    update_sql("ALTER TABLE {comments} ADD thread VARCHAR(255) NOT NULL");
  }
Dries's avatar
   
Dries committed
321

Dries's avatar
   
Dries committed
322
  $result = db_query("SELECT DISTINCT(nid) FROM {comments} WHERE thread = ''");
Dries's avatar
   
Dries committed
323
324

  while ($node = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
325
    $result2 = db_query("SELECT cid, pid FROM {comments} where nid = '%d' ORDER BY timestamp", $node->nid);
Dries's avatar
   
Dries committed
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
    $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);

Dries's avatar
   
Dries committed
349
      db_query("UPDATE {comments} SET thread = '%s' WHERE cid = '%d'", $thread."/", $comments[$cid]->cid);
Dries's avatar
   
Dries committed
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
    }
  }
}

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;
}

Dries's avatar
   
Dries committed
373
function update_60() {
374
  update_sql("ALTER TABLE {forum} DROP icon");
Dries's avatar
   
Dries committed
375
376
}

Dries's avatar
   
Dries committed
377
function update_61() {
Dries's avatar
   
Dries committed
378
379
380
381
382
383
384
385
386
387
388
389
390
  if ($GLOBALS["db_type"] == "pgsql") {
    update_sql("CREATE TABLE {sessions} (
      uid integer NOT NULL,
      sid varchar(32) NOT NULL default '',
      hostname varchar(128) NOT NULL default '',
      timestamp integer NOT NULL default '0',
      session text,
      PRIMARY KEY (sid)
     );");

    update_sql("ALTER TABLE {users} DROP session;");
    update_sql("ALTER TABLE {users} DROP hostname;");
    update_sql("ALTER TABLE {users} DROP sid;");
Dries's avatar
   
Dries committed
391

Dries's avatar
   
Dries committed
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
  }
  else {
    update_sql("CREATE TABLE IF NOT EXISTS {sessions} (
      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)
    )");

    update_sql("ALTER TABLE {users} DROP session;");
    update_sql("ALTER TABLE {users} DROP hostname;");
    update_sql("ALTER TABLE {users} DROP sid;");
  }
Dries's avatar
   
Dries committed
409
410
}

Dries's avatar
   
Dries committed
411
function update_62() {
Dries's avatar
   
Dries committed
412
413
  if ($GLOBALS["db_type"] == "pgsql") {
    update_sql("CREATE INDEX accesslog_timestamp ON {accesslog} (timestamp)");
Dries's avatar
   
Dries committed
414

Dries's avatar
   
Dries committed
415
416
417
    update_sql("DROP INDEX node_type_idx");
    update_sql("DROP INDEX node_title_idx");
    update_sql("DROP INDEX node_promote_idx");
Dries's avatar
   
Dries committed
418

Dries's avatar
   
Dries committed
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
    update_sql("CREATE INDEX node_type ON {node} (type)");
    update_sql("CREATE INDEX node_title_type ON {node} (title,type)");
    update_sql("CREATE INDEX node_moderate ON {node} (moderate)");
    update_sql("CREATE INDEX node_path ON {node} (path)");
    update_sql("CREATE INDEX node_promote_status ON {node} (promote, status)");

  }
  else {
    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} 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)");
  }
Dries's avatar
   
Dries committed
439
440
}

Dries's avatar
   
Dries committed
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
function _update_next_thread($structure, $parent) {
  do {
    $val++;
    if ($parent) {
      $thread = "$parent.$val";
    }
    else {
      $thread = $val;
    }

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

  return $val;
}

Dries's avatar
   
Dries committed
456
function update_63() {
Dries's avatar
   
Dries committed
457
  if ($GLOBALS["db_type"] == "pgsql") {
Dries's avatar
   
Dries committed
458
    update_sql("INSERT INTO {users} (uid, name, mail, timestamp) VALUES ('0', '', '', '". time() ."')");
Dries's avatar
   
Dries committed
459
460
461
  }
  else {
    update_sql("ALTER TABLE {users} CHANGE uid uid int(10) unsigned NOT NULL default '0'");
Dries's avatar
   
Dries committed
462
    update_sql("INSERT INTO {users} (uid, name, mail, timestamp) VALUES ('0', '', '', '". time() ."')");
Dries's avatar
   
Dries committed
463
464
465
    $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
466
467
}

Dries's avatar
   
Dries committed
468
function update_64() {
Dries's avatar
   
Dries committed
469
  update_sql("UPDATE {users} SET rid = 1 WHERE uid = 0");
Dries's avatar
   
Dries committed
470
471
}

Dries's avatar
   
Dries committed
472
function update_65() {
Dries's avatar
   
Dries committed
473
  // PostgreSQL-only update.
Dries's avatar
   
Dries committed
474
475
476
477
  if ($GLOBALS["db_type"] == "pgsql") {
    update_sql("CREATE FUNCTION \"rand\"() RETURNS float AS '
      BEGIN
        RETURN random();
Dries's avatar
   
Dries committed
478
      END;' LANGUAGE 'plpgsql'");
Dries's avatar
   
Dries committed
479
480
481
  }
}

Dries's avatar
   
Dries committed
482
function update_66() {
Dries's avatar
   
Dries committed
483
484
  if ($GLOBALS["db_type"] == "pgsql") {
    update_sql("CREATE TABLE {path} (
Dries's avatar
   
Dries committed
485
      pid serial,
Dries's avatar
   
Dries committed
486
487
488
489
490
491
492
493
494
495
      src varchar(128) NOT NULL default '',
      dst varchar(128) NOT NULL default '',
      PRIMARY KEY  (pid)
    )");
    update_sql("CREATE INDEX path_src_idx ON {path}(src)");
    update_sql("CREATE INDEX path_dst_idx ON {path}(dst)");
    $result = db_query("SELECT nid, path FROM {node} WHERE path != ''");
    while ($node = db_fetch_object($result)) {
      update_sql("INSERT INTO {path} (src, dst) VALUES ('node/view/$node->nid', '". check_query($node->path) ."')");
    }
Dries's avatar
   
Dries committed
496
  }
Dries's avatar
   
Dries committed
497
498
499
500
501
502
503
504
505
506
507
508
509
510
  else {
    update_sql("CREATE TABLE {path} (
      pid int(10) unsigned NOT NULL auto_increment,
      src varchar(128) NOT NULL default '',
      dst varchar(128) NOT NULL default '',
      PRIMARY KEY  (pid),
      UNIQUE KEY src (src),
      UNIQUE KEY dst (dst)
    )");
    // Migrate the existing paths:
    $result = db_query("SELECT nid, path FROM {node} WHERE path != ''");
    while ($node = db_fetch_object($result)) {
      update_sql("INSERT INTO {path} (src, dst) VALUES ('node/view/$node->nid', '". check_query($node->path) ."')");
    }
Dries's avatar
   
Dries committed
511

Dries's avatar
   
Dries committed
512
513
514
    update_sql("ALTER TABLE {node} DROP path");
  }
}
Dries's avatar
   
Dries committed
515

Dries's avatar
   
Dries committed
516
function update_67() {
Dries's avatar
   
Dries committed
517
518
519
520
  if ($GLOBALS["db_type"] == "pgsql") {
    // Taking no action.  PostgreSQL is not always capable of dropping columns.
  }
  else {
Dries's avatar
   
Dries committed
521
    update_sql("ALTER TABLE {users} DROP homepage");
Dries's avatar
   
Dries committed
522
  }
Dries's avatar
   
Dries committed
523
524
}

Dries's avatar
   
Dries committed
525
function update_68() {
Dries's avatar
   
Dries committed
526
527
528
529
530
531
  if ($GLOBALS["db_type"] == "pgsql") {
    // Unneccesary. The PostgreSQL port was already using a sequence.
  }
  else {
    $max = db_result(db_query("SELECT MAX(aid) FROM {access};"));
    update_sql("INSERT INTO {sequences} (name, id) VALUES ('access_aid', '$max')");
Dries's avatar
   
Dries committed
532
    update_sql("ALTER TABLE {access} CHANGE aid aid tinyint(10) NOT NULL ");
Dries's avatar
   
Dries committed
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
  }
}

function update_69() {
  if ($GLOBALS["db_type"] == "pgsql") {
    /* Rename the statistics table to node_counter */
    update_sql("ALTER TABLE {statistics} RENAME TO {node_counter}");
    update_sql("DROP INDEX  {statistics}_totalcount_idx");
    update_sql("DROP INDEX  {statistics}_daycount_idx");
    update_sql("DROP INDEX  {statistics}_timestamp_idx");
    update_sql("CREATE INDEX {node_counter}_totalcount_idx ON {node_counter}(totalcount)");
    update_sql("CREATE INDEX {node_counter}_daycount_idx ON {node_counter}(daycount)");
    update_sql("CREATE INDEX {node_counter}_timestamp_idx ON {node_counter}(timestamp)");

    /* Rename the path table to url_alias */
    update_sql("ALTER TABLE {path} RENAME TO {url_alias}");
    update_sql("ALTER TABLE {path}_pid_seq RENAME TO {url_alias}_pid_seq");
  }
  else {
    update_sql("ALTER TABLE {statistics} RENAME TO {node_counter}");
    update_sql("ALTER TABLE {path} RENAME TO {url_alias}");
Dries's avatar
   
Dries committed
554
    update_sql("UPDATE {sequences} SET name = '{url_alias}_pid' WHERE name = '{path}_pid'");
Dries's avatar
   
Dries committed
555
  }
Dries's avatar
   
Dries committed
556
557

  update_sql("UPDATE {users} SET name = '' WHERE uid = 0;");
Dries's avatar
   
Dries committed
558
559
}

Dries's avatar
   
Dries committed
560
561
562
563
function update_70() {
  update_sql("ALTER TABLE {variable} CHANGE name name varchar(48) NOT NULL");
}

Dries's avatar
   
Dries committed
564
function update_71() {
Dries's avatar
   
Dries committed
565
566
567
568
569
570
  if ($GLOBALS["db_type"] == "pgsql") {
    update_sql("ALTER TABLE {system} ADD bootstrap integer");
  }
  else {
    update_sql("ALTER TABLE {system} ADD bootstrap int(2)");
  }
Dries's avatar
   
Dries committed
571
572
}

Dries's avatar
   
Dries committed
573
function update_72() {
Dries's avatar
   
Dries committed
574
575
576
577
578
579
580
581
582
  if ($GLOBALS["db_type"] == "pgsql") {
    update_sql("ALTER TABLE {blocks} ADD throttle smallint");
    update_sql("ALTER TABLE {blocks} ALTER COLUMN throttle SET DEFAULT '0'");
    update_sql("UPDATE {blocks} SET throttle = 0");
    update_sql("ALTER TABLE {blocks} ALTER COLUMN throttle SET NOT NULL");
  }
  else {
    update_sql("ALTER TABLE {blocks} ADD throttle tinyint(1) NOT NULL DEFAULT '0'");
  }
Dries's avatar
   
Dries committed
583
584
}

Dries's avatar
   
Dries committed
585
function update_73() {
Dries's avatar
   
Dries committed
586
  /* MySQL only update */
Dries's avatar
   
Dries committed
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
  if ($GLOBALS["db_type"] == "mysql") {
    update_sql("ALTER TABLE {book} CHANGE log log longtext");
    update_sql("ALTER TABLE {boxes} CHANGE body body longtext");
    update_sql("ALTER TABLE {cache} CHANGE data data longtext");
    update_sql("ALTER TABLE {comments} CHANGE comment comment longtext");
    update_sql("ALTER TABLE {comments} CHANGE users users longtext");
    update_sql("ALTER TABLE {directory} CHANGE slogan slogan longtext");
    update_sql("ALTER TABLE {directory} CHANGE mission mission longtext");
    update_sql("ALTER TABLE {feed} CHANGE description description longtext");
    update_sql("ALTER TABLE {item} CHANGE description description longtext");
    update_sql("ALTER TABLE {node} CHANGE users users longtext");
    update_sql("ALTER TABLE {node} CHANGE teaser teaser longtext");
    update_sql("ALTER TABLE {node} CHANGE body body longtext");
    update_sql("ALTER TABLE {node} CHANGE revisions revisions longtext");
    update_sql("ALTER TABLE {permission} CHANGE perm perm longtext");
    update_sql("ALTER TABLE {poll} CHANGE voters voters longtext");
    update_sql("ALTER TABLE {sessions} CHANGE session session longtext");
    update_sql("ALTER TABLE {term_data} CHANGE description description longtext");
    update_sql("ALTER TABLE {users} CHANGE data data longtext");
    update_sql("ALTER TABLE {variable} CHANGE value value longtext");
    update_sql("ALTER TABLE {vocabulary} CHANGE description description longtext");
    update_sql("ALTER TABLE {vocabulary} CHANGE nodes nodes longtext");
    update_sql("ALTER TABLE {watchdog} CHANGE message message longtext");
  }
}

Dries's avatar
   
Dries committed
613
function update_74() {
Dries's avatar
   
Dries committed
614
615
616
617
618
619
620
621
622
  if ($GLOBALS["db_type"] == "pgsql") {
    update_sql("ALTER TABLE {system} ADD throttle smallint");
    update_sql("ALTER TABLE {system} ALTER COLUMN throttle SET DEFAULT '0'");
    update_sql("UPDATE {system} SET throttle = 0");
    update_sql("ALTER TABLE {system} ALTER COLUMN throttle SET NOT NULL");
  }
  else {
    update_sql("ALTER TABLE {system} ADD throttle tinyint(1) NOT NULL DEFAULT '0'");
  }
Dries's avatar
   
Dries committed
623
624
}

Dries's avatar
   
Dries committed
625
function update_75() {
Dries's avatar
   
Dries committed
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
  if ($GLOBALS["db_type"] == "pgsql") {
    update_sql("ALTER TABLE {feed} ADD etag text");
    update_sql("ALTER TABLE {feed} ALTER COLUMN etag SET DEFAULT ''");
    update_sql("ALTER TABLE {feed} ALTER COLUMN etag SET NOT NULL");

    update_sql("ALTER TABLE {feed} ADD modified integer");
    update_sql("ALTER TABLE {feed} ALTER COLUMN modified SET DEFAULT '0'");
    update_sql("UPDATE {feed} SET modified = 0");
    update_sql("ALTER TABLE {feed} ALTER COLUMN modified SET NOT NULL");

    update_sql("ALTER TABLE {feed} RENAME timestamp TO checked");

    update_sql("UPDATE {blocks} SET module = 'aggregator' WHERE module = 'import'");
    update_sql("UPDATE {system} SET filename = 'modules/aggregator.module', name = 'aggregator' WHERE filename = 'modules/import.module'");
  }
  else {
    update_sql("ALTER TABLE {feed} ADD etag varchar(255) NOT NULL DEFAULT ''");
    update_sql("ALTER TABLE {feed} ADD modified int(10) NOT NULL DEFAULT 0");
    update_sql("ALTER TABLE {feed} CHANGE timestamp checked int(10) NOT NULL DEFAULT 0");
    update_sql("UPDATE {blocks} SET module = 'aggregator' WHERE module = 'import'");
    update_sql("UPDATE {system} SET filename = 'modules/aggregator.module', name = 'aggregator' WHERE filename = 'modules/import.module'");
  }
Dries's avatar
   
Dries committed
648
649
}

Dries's avatar
   
Dries committed
650
function update_76() {
Dries's avatar
   
Dries committed
651
652
653
654
655
  if ($GLOBALS["db_type"] == "pgsql") {
    update_sql("ALTER TABLE {feed} ADD image text");
  } else {
    update_sql("ALTER TABLE {feed} ADD image longtext");
  }
Dries's avatar
   
Dries committed
656
657
}

Dries's avatar
Dries committed
658
659
660
661
function update_77() {
  update_sql("ALTER TABLE {cache} ADD headers text");
}

Dries's avatar
   
Dries committed
662
663
function update_78() {
  if ($GLOBALS["db_type"] == "mysql") {
664
    update_sql("CREATE TABLE {filters} (
Dries's avatar
   
Dries committed
665
666
667
668
669
670
671
672
673
674
      module varchar(64) NOT NULL default '',
      weight tinyint(2) DEFAULT '0' NOT NULL,
      KEY weight (weight)
    )");
  }
  else {
    /* Needs PGSQL/MSSQL equivalent */
  }
}

Dries's avatar
   
Dries committed
675
676
677
678
679
680
function update_79() {
  if ($GLOBALS["db_type"] == "pgsql") {
    // Taking no action.  PostgreSQL is not always capable of dropping columns.
  }
  else {
    update_sql("ALTER TABLE {node} DROP attributes");
Kjartan's avatar
- Typo.    
Kjartan committed
681
    update_sql("ALTER TABLE {comment} DROP link");
Dries's avatar
   
Dries committed
682
683
684
  }
}

Dries's avatar
   
Dries committed
685
686
687
688
/*
** System functions
*/

689
function update_sql($sql) {
Dries's avatar
   
Dries committed
690
  $edit = $_POST["edit"];
Kjartan's avatar
Kjartan committed
691
  print nl2br(htmlentities($sql)) ." ";
692
693
  $result = db_query($sql);
  if ($result) {
Dries's avatar
   
Dries committed
694
    print "<div style=\"color: green;\">OK</div>\n";
695
696
697
    return 1;
  }
  else {
Dries's avatar
   
Dries committed
698
    print "<div style=\"color: red;\">FAILED</div>\n";
699
700
701
702
703
704
705
    return 0;
  }
}

function update_data($start) {
  global $mysql_updates;
  $mysql_updates = array_slice($mysql_updates, ($start-- ? $start : 0));
706
  foreach ($mysql_updates as $date => $func) {
707
    print "<strong>$date</strong><br />\n<pre>\n";
708
709
710
711
712
713
    $func();
    variable_set("update_start", $date);
    print "</pre>\n";
  }
}

Dries's avatar
   
Dries committed
714
715
716
717
718
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">
Dries's avatar
   
Dries committed
719
        @import url("misc/drupal.css");
Dries's avatar
   
Dries committed
720
721
      </style>
EOF;
Dries's avatar
   
Dries committed
722
  $output .= "</head><body>";
Dries's avatar
   
Dries committed
723
  $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>";
Dries's avatar
   
Dries committed
724
725
726
727
728
729
730
731
  $output .= "<div id=\"update\"><h1>$title</h1>";
  return $output;
}

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

732
function update_page() {
Dries's avatar
   
Dries committed
733
734
735
  global $user, $mysql_updates;

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

737
  switch ($_POST["op"]) {
738
739
    case "Update":
      // make sure we have updates to run.
Dries's avatar
   
Dries committed
740
      print update_page_header("Drupal database update");
Dries's avatar
   
Dries committed
741
742
743
      $links[] = "<a href=\"index.php\">main page</a>";
      $links[] = "<a href=\"index.php?q=admin\">administration pages</a>";
      print theme("item_list", $links);
Dries's avatar
   
Dries committed
744
        // NOTE: we can't use l() here because the URL would point to 'update.php?q=admin'.
745
746
747
748
749
750
      if ($edit["start"] == -1) {
        print "No updates to perform.";
      }
      else {
        update_data($edit["start"]);
      }
Dries's avatar
   
Dries committed
751
      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>.";
Dries's avatar
   
Dries committed
752
      print " Otherwise, you may need to update your database manually.";
Dries's avatar
   
Dries committed
753
      print update_page_footer();
754
755
756
757
758
      break;
    default:
      $start = variable_get("update_start", 0);
      $dates[] = "All";
      $i = 1;
759
      foreach ($mysql_updates as $date => $sql) {
760
761
762
763
764
        $dates[$i++] = $date;
        if ($date == $start) {
          $selected = $i;
        }
      }
Dries's avatar
   
Dries committed
765
      $dates[$i] = "No updates available";
766
767

      // make update form and output it.
Dries's avatar
   
Dries committed
768
      $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.");
769
      $form .= form_submit("Update");
Dries's avatar
   
Dries committed
770
      print update_page_header("Drupal database update");
771
      print form($form);
Dries's avatar
   
Dries committed
772
      print update_page_footer();
773
774
775
776
      break;
  }
}

777
function update_info() {
Dries's avatar
   
Dries committed
778
  print update_page_header("Drupal database update");
Kjartan's avatar
Kjartan committed
779
  print "<ol>\n";
780
  print "<li>Use this script to <strong>upgrade an existing Drupal installation</strong>.  You don't need this script when installing Drupal from scratch.</li>";
Dries's avatar
   
Dries committed
781
  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";
Dries's avatar
   
Dries committed
782
  print "<li>Update your Drupal sources, check the notes below and <a href=\"update.php?op=update\">run the database upgrade script</a>.  Don't upgrade your database twice as it may cause problems.</p></li>\n";
Dries's avatar
   
Dries committed
783
  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
784
  print "</ol>";
Dries's avatar
   
Dries committed
785
786
  print "Notes:";
  print "<ol>";
Dries's avatar
   
Dries committed
787
  print " <li>If you upgrade from Drupal 4.2.0, you have to create the <code>sessions</code> table manually before upgrading.  After you created the table, you'll want to log in and immediately continue the upgrade.  To create the <code>sessions</code> table, issue the following SQL command (MySQL specific example):<pre>CREATE TABLE sessions (
Dries's avatar
   
Dries committed
788
789
790
791
792
793
794
795
796
  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));</pre></li>";
  print "</ol>";
Dries's avatar
   
Dries committed
797
  print update_page_footer();
798
}
799

800
if (isset($_GET["op"])) {
Dries's avatar
   
Dries committed
801
  include_once "includes/bootstrap.inc";
Kjartan's avatar
Kjartan committed
802
  include_once "includes/common.inc";
Dries's avatar
   
Dries committed
803

Dries's avatar
   
Dries committed
804
  // Access check:
Dries's avatar
   
Dries committed
805
  if (($access_check == 0) || ($user->uid == 1)) {
Kjartan's avatar
Kjartan committed
806
807
808
    update_page();
  }
  else {
Dries's avatar
   
Dries committed
809
    print update_page_header("Access denied");
Dries's avatar
   
Dries committed
810
    print "Access denied.  You are not authorized to access to this page.  Please log in as the user with user ID #1. If you cannot log-in, you will have to edit <code>update.php</code> to by-pass this access check; in that case, open <code>update.php</code> in a text editor and follow the instructions at the top.";
Dries's avatar
   
Dries committed
811
    print update_page_footer();
Kjartan's avatar
Kjartan committed
812
813
814
815
  }
}
else {
  update_info();
816
}
Dries's avatar
   
Dries committed
817
?>