initial import from onelab svn codebase
[plewww.git] / database / updates.inc
1 <?php
2 // $Id: updates.inc 144 2007-03-28 07:52:20Z thierry $
3
4 function system_update_110() {
5   $ret = array();
6
7   // TODO: needs PGSQL version
8   if ($GLOBALS['db_type'] == 'mysql') {
9     /*
10     ** Search
11     */
12
13     $ret[] = update_sql('DROP TABLE {search_index}');
14     $ret[] = update_sql("CREATE TABLE {search_index} (
15       word varchar(50) NOT NULL default '',
16       sid int(10) unsigned NOT NULL default '0',
17       type varchar(16) default NULL,
18       fromsid int(10) unsigned NOT NULL default '0',
19       fromtype varchar(16) default NULL,
20       score int(10) unsigned default NULL,
21       KEY sid (sid),
22       KEY fromsid (fromsid),
23       KEY word (word)
24       )");
25
26     $ret[] = update_sql("CREATE TABLE {search_total} (
27       word varchar(50) NOT NULL default '',
28       count int(10) unsigned default NULL,
29       PRIMARY KEY word (word)
30       )");
31
32
33     /*
34     ** Blocks
35     */
36
37     $ret[] = update_sql('ALTER TABLE {blocks} DROP path');
38     $ret[] = update_sql('ALTER TABLE {blocks} ADD visibility tinyint(1) NOT NULL');
39     $ret[] = update_sql('ALTER TABLE {blocks} ADD pages text NOT NULL');
40   }
41   elseif ($GLOBALS['db_type'] == 'pgsql') {
42     /*
43     ** Search
44     */
45     $ret[] = update_sql('DROP TABLE {search_index}');
46     $ret[] = update_sql("CREATE TABLE {search_index} (
47       word varchar(50) NOT NULL default '',
48       sid integer NOT NULL default '0',
49       type varchar(16) default NULL,
50       fromsid integer NOT NULL default '0',
51       fromtype varchar(16) default NULL,
52       score integer default NULL
53       )");
54     $ret[] = update_sql("CREATE INDEX {search_index}_sid_idx on {search_index}(sid)");
55     $ret[] = update_sql("CREATE INDEX {search_index}_fromsid_idx on {search_index}(fromsid)");
56     $ret[] = update_sql("CREATE INDEX {search_index}_word_idx on {search_index}(word)");
57
58     $ret[] = update_sql("CREATE TABLE {search_total} (
59       word varchar(50) NOT NULL default '' PRIMARY KEY,
60       count integer default NULL
61       )");
62
63
64     /*
65     ** Blocks
66     */
67     // Postgres can only drop columns since 7.4
68     #$ret[] = update_sql('ALTER TABLE {blocks} DROP path');
69
70     $ret[] = update_sql('ALTER TABLE {blocks} ADD visibility smallint');
71     $ret[] = update_sql("ALTER TABLE {blocks} ALTER COLUMN visibility set default 0");
72     $ret[] = update_sql('UPDATE {blocks} SET visibility = 0');
73     $ret[] = update_sql('ALTER TABLE {blocks} ALTER COLUMN visibility SET NOT NULL');
74     $ret[] = update_sql('ALTER TABLE {blocks} ADD pages text');
75     $ret[] = update_sql("ALTER TABLE {blocks} ALTER COLUMN pages set default ''");
76     $ret[] = update_sql("UPDATE {blocks} SET pages = ''");
77     $ret[] = update_sql('ALTER TABLE {blocks} ALTER COLUMN pages SET NOT NULL');
78
79   }
80
81   $ret[] = update_sql("DELETE FROM {variable} WHERE name = 'node_cron_last'");
82
83   $ret[] = update_sql('UPDATE {blocks} SET status = 1, custom = 2 WHERE status = 0 AND custom = 1');
84
85   return $ret;
86 }
87
88 function system_update_111() {
89   $ret = array();
90
91   $ret[] = update_sql("DELETE FROM {variable} WHERE name LIKE 'throttle_%'");
92
93   if ($GLOBALS['db_type'] == 'mysql') {
94     $ret[] = update_sql('ALTER TABLE {sessions} ADD PRIMARY KEY sid (sid)');
95   }
96   elseif ($GLOBALS['db_type'] == 'pgsql') {
97     $ret[] = update_sql('ALTER TABLE {sessions} ADD UNIQUE(sid)');
98   }
99
100   return $ret;
101 }
102
103 function system_update_112() {
104   $ret = array();
105
106   if ($GLOBALS['db_type'] == 'mysql') {
107     $ret[] = update_sql("CREATE TABLE {flood} (
108       event varchar(64) NOT NULL default '',
109       hostname varchar(128) NOT NULL default '',
110       timestamp int(11) NOT NULL default '0'
111      );");
112   }
113   elseif ($GLOBALS['db_type'] == 'pgsql') {
114     $ret[] = update_sql("CREATE TABLE {flood} (
115       event varchar(64) NOT NULL default '',
116       hostname varchar(128) NOT NULL default '',
117       timestamp integer NOT NULL default 0
118      );");
119   }
120
121   return $ret;
122 }
123
124 function system_update_113() {
125   $ret = array();
126
127   if ($GLOBALS['db_type'] == 'mysql') {
128     $ret[] = update_sql('ALTER TABLE {accesslog} ADD aid int(10) NOT NULL auto_increment, ADD PRIMARY KEY (aid)');
129   }
130   elseif ($GLOBALS['db_type'] == 'pgsql') {
131     $ret[] = update_sql("SELECT * INTO TEMPORARY {accesslog}_t FROM {accesslog}");
132     $ret[] = update_sql("DROP TABLE {accesslog}");
133     $ret[] = update_sql("CREATE TABLE {accesslog} (
134       aid serial,
135       title varchar(255) default NULL,
136       path varchar(255) default NULL,
137       url varchar(255) default NULL,
138       hostname varchar(128) default NULL,
139       uid integer default '0',
140       timestamp integer NOT NULL default '0'
141     )");
142     $ret[] = update_sql("INSERT INTO {accesslog} (title, path, url, hostname, uid, timestamp) SELECT title, path, url, hostname, uid, timestamp FROM {accesslog}_t");
143
144     $ret[] = update_sql("DROP TABLE {accesslog}_t");
145     $ret[] = update_sql("CREATE INDEX {accesslog}_timestamp_idx ON {accesslog} (timestamp);");
146
147   }
148
149   // Flush the menu cache:
150   cache_clear_all('menu:', TRUE);
151
152   return $ret;
153 }
154
155 function system_update_114() {
156   $ret = array();
157   if ($GLOBALS['db_type'] == 'mysql') {
158     $ret[] = update_sql("CREATE TABLE {queue} (
159       nid int(10) unsigned NOT NULL,
160       uid int(10) unsigned NOT NULL,
161       vote int(3) NOT NULL default '0',
162       PRIMARY KEY (nid, uid)
163      )");
164   }
165   else if ($GLOBALS['db_type'] == 'pgsql') {
166     $ret[] = update_sql("CREATE TABLE {queue} (
167       nid integer NOT NULL default '0',
168       uid integer NOT NULL default '0',
169       vote integer NOT NULL default '0',
170       PRIMARY KEY (nid, uid)
171     )");
172     $ret[] = update_sql("CREATE INDEX {queue}_nid_idx ON queue(nid)");
173     $ret[] = update_sql("CREATE INDEX {queue}_uid_idx ON queue(uid)");
174   }
175
176   $result = db_query("SELECT nid, votes, score, users FROM {node}");
177   while ($node = db_fetch_object($result)) {
178     if (isset($node->users)) {
179       $arr = explode(',', $node->users);
180       unset($node->users);
181       foreach ($arr as $value) {
182         $arr2 = explode('=', trim($value));
183         if (isset($arr2[0]) && isset($arr2[1])) {
184           switch ($arr2[1]) {
185             case '+ 1':
186               db_query("INSERT INTO {queue} (nid, uid, vote) VALUES (%d, %d, %d)", $node->nid, (int)$arr2[0], 1);
187               break;
188             case '- 1':
189               db_query("INSERT INTO {queue} (nid, uid, vote) VALUES (%d, %d, %d)", $node->nid, (int)$arr2[0], -1);
190               break;
191             default:
192               db_query("INSERT INTO {queue} (nid, uid, vote) VALUES (%d, %d, %d)", $node->nid, (int)$arr2[0], 0);
193           }
194         }
195       }
196     }
197   }
198
199   if ($GLOBALS['db_type'] == 'mysql') {
200     // Postgres only supports dropping of columns since 7.4
201     $ret[] = update_sql("ALTER TABLE {node} DROP votes");
202     $ret[] = update_sql("ALTER TABLE {node} DROP score");
203     $ret[] = update_sql("ALTER TABLE {node} DROP users");
204   }
205
206   return $ret;
207 }
208
209 function system_update_115() {
210   $ret = array();
211
212   // This update has been moved to update_fix_watchdog_115 in update.php because it
213   // is needed for the basic functioning of the update script.
214
215   return $ret;
216 }
217
218 function system_update_116() {
219   return array(update_sql("DELETE FROM {system} WHERE name = 'admin'"));
220 }
221
222 function system_update_117() {
223   $ret = array();
224   if ($GLOBALS['db_type'] == 'mysql') {
225     $ret[] = update_sql("CREATE TABLE {vocabulary_node_types} (
226                          vid int(10) NOT NULL default '0',
227                          type varchar(16) NOT NULL default '',
228                          PRIMARY KEY (vid, type))");
229   }
230   else if ($GLOBALS['db_type'] == 'pgsql') {
231     $ret[] = update_sql("CREATE TABLE {vocabulary_node_types} (
232                          vid serial,
233                          type varchar(16) NOT NULL default '',
234                           PRIMARY KEY (vid, type)) ");
235   }
236   return $ret;
237 }
238
239 function system_update_118() {
240   $ret = array();
241   $node_types = array();
242   $result = db_query('SELECT vid, nodes FROM {vocabulary}');
243   while ($vocabulary = db_fetch_object($result)) {
244     $node_types[$vocabulary->vid] = explode(',', $vocabulary->nodes);
245   }
246   foreach ($node_types as $vid => $type_array) {
247     foreach ($type_array as $type) {
248       db_query("INSERT INTO {vocabulary_node_types} (vid, type) VALUES (%d, '%s')", $vid, $type);
249     }
250   }
251   if ($GLOBALS['db_type'] == 'mysql') {
252     $ret[] = update_sql("ALTER TABLE {vocabulary} DROP nodes");
253   }
254   return $ret;
255 }
256
257 function system_update_119() {
258   $ret = array();
259
260   foreach (node_get_types() as $type => $name) {
261     $node_options = array();
262     if (variable_get('node_status_'. $type, 1)) {
263       $node_options[] = 'status';
264     }
265     if (variable_get('node_moderate_'. $type, 0)) {
266       $node_options[] = 'moderate';
267     }
268     if (variable_get('node_promote_'. $type, 1)) {
269       $node_options[] = 'promote';
270     }
271     if (variable_get('node_sticky_'. $type, 0)) {
272       $node_options[] = 'sticky';
273     }
274     if (variable_get('node_revision_'. $type, 0)) {
275       $node_options[] = 'revision';
276     }
277     variable_set('node_options_'. $type, $node_options);
278     variable_del('node_status_'. $type);
279     variable_del('node_moderate_'. $type);
280     variable_del('node_promote_'. $type);
281     variable_del('node_sticky_'. $type);
282     variable_del('node_revision_'. $type);
283   }
284
285   return $ret;
286 }
287
288 function system_update_120() {
289   $ret = array();
290
291   // Rewrite old URL aliases. Works for both PostgreSQL and MySQL
292   $result = db_query("SELECT pid, src FROM {url_alias} WHERE src LIKE 'blog/%%'");
293   while ($alias = db_fetch_object($result)) {
294     list(, $page, $op, $uid) = explode('/', $alias->src);
295     if ($page == 'feed') {
296       $new = "blog/$uid/feed";
297       update_sql("UPDATE {url_alias} SET src = '%s' WHERE pid = '%s'", $new, $alias->pid);
298     }
299   }
300
301   return $ret;
302 }
303
304 function system_update_121() {
305   $ret = array();
306
307   // Remove the unused page table.
308   $ret[] = update_sql('DROP TABLE {page}');
309
310   return $ret;
311 }
312
313 function system_update_122() {
314
315   $ret = array();
316   $ret[] = update_sql("ALTER TABLE {blocks} ADD types text");
317   return $ret;
318
319 }
320
321 function system_update_123() {
322   $ret = array();
323
324   if ($GLOBALS['db_type'] == 'mysql') {
325     $ret[] = update_sql("ALTER TABLE {vocabulary} ADD module varchar(255) NOT NULL default ''");
326   }
327   elseif ($GLOBALS['db_type'] == 'pgsql') {
328     $ret[] = update_sql("ALTER TABLE {vocabulary} ADD module varchar(255)");
329     $ret[] = update_sql("UPDATE {vocabulary} SET module = ''");
330     $ret[] = update_sql("ALTER TABLE {vocabulary} ALTER COLUMN module SET NOT NULL");
331     $ret[] = update_sql("ALTER TABLE {vocabulary} ALTER COLUMN module SET DEFAULT ''");
332   }
333
334   $ret[] = update_sql("UPDATE {vocabulary} SET module = 'taxonomy'");
335   $vid = variable_get('forum_nav_vocabulary', '');
336   if (!empty($vid)) {
337     $ret[] = update_sql("UPDATE {vocabulary} SET module = 'forum' WHERE vid = " . $vid);
338   }
339
340   return $ret;
341 }
342
343 function system_update_124() {
344   $ret = array();
345
346   if ($GLOBALS['db_type'] == 'mysql') {
347     // redo update_105, correctly creating node_comment_statistics
348     $ret[] = update_sql("DROP TABLE IF EXISTS {node_comment_statistics}");
349
350     $ret[] = update_sql("CREATE TABLE {node_comment_statistics} (
351       nid int(10) unsigned NOT NULL auto_increment,
352       last_comment_timestamp int(11) NOT NULL default '0',
353       last_comment_name varchar(60) default NULL,
354       last_comment_uid int(10) NOT NULL default '0',
355       comment_count int(10) unsigned NOT NULL default '0',
356       PRIMARY KEY (nid),
357       KEY node_comment_timestamp (last_comment_timestamp)
358       )");
359   }
360
361   else {
362     // also drop incorrectly named table for PostgreSQL
363     $ret[] = update_sql("DROP TABLE {node}_comment_statistics");
364
365     $ret[] = update_sql("CREATE TABLE {node_comment_statistics} (
366       nid integer NOT NULL,
367       last_comment_timestamp integer NOT NULL default '0',
368       last_comment_name varchar(60)  default NULL,
369       last_comment_uid integer NOT NULL default '0',
370       comment_count integer NOT NULL default '0',
371       PRIMARY KEY (nid)
372     )");
373
374     $ret[] = update_sql("CREATE INDEX {node_comment_statistics}_timestamp_idx ON {node_comment_statistics}(last_comment_timestamp);
375 ");
376   }
377
378   // initialize table
379   $ret[] = update_sql("INSERT INTO {node_comment_statistics} (nid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) SELECT n.nid, n.changed, NULL, 0, 0 FROM {node} n");
380
381   // fill table
382   $result = db_query("SELECT c.nid, c.timestamp, c.name, c.uid, COUNT(c.nid) as comment_count FROM {node} n LEFT JOIN {comments} c ON c.nid = n.nid WHERE c.status = 0 GROUP BY c.nid, c.timestamp, c.name, c.uid");
383   while ($comment_record = db_fetch_object($result)) {
384     $count = db_result(db_query('SELECT COUNT(cid) FROM {comments} WHERE nid = %d AND status = 0', $comment_record->nid));
385     db_query("UPDATE {node_comment_statistics} SET comment_count = %d, last_comment_timestamp = %d, last_comment_name = '%s', last_comment_uid = %d WHERE nid = %d", $count, $comment_record->timestamp, $comment_record->name, $comment_record->uid, $comment_record->nid);
386   }
387
388   return $ret;
389 }
390
391 function system_update_125() {
392   // Postgres only update.
393   $ret = array();
394
395   if ($GLOBALS['db_type'] == 'pgsql') {
396
397     $ret[] = update_sql("CREATE OR REPLACE FUNCTION if(boolean, anyelement, anyelement) RETURNS anyelement AS '
398           SELECT CASE WHEN $1 THEN $2 ELSE $3 END;
399         ' LANGUAGE 'sql'");
400
401     $ret[] = update_sql("CREATE FUNCTION greatest(integer, integer, integer) RETURNS integer AS '
402                           SELECT greatest($1, greatest($2, $3));
403                         ' LANGUAGE 'sql'");
404
405   }
406
407   return $ret;
408 }
409
410 function system_update_126() {
411   variable_set('forum_block_num_0', variable_get('forum_block_num', 5));
412   variable_set('forum_block_num_1', variable_get('forum_block_num', 5));
413   variable_del('forum_block_num');
414
415   return array();
416 }
417
418 function system_update_127() {
419   $ret = array();
420   if ($GLOBALS['db_type'] == 'pgsql') {
421     $ret[] = update_sql("ALTER TABLE {poll} RENAME voters TO polled");
422   }
423   else if ($GLOBALS['db_type'] == 'mysql') {
424     $ret[] = update_sql("ALTER TABLE {poll} CHANGE voters polled longtext");
425   }
426   return $ret;
427 }
428
429 function system_update_128() {
430   $ret = array();
431
432   if ($GLOBALS['db_type'] == 'mysql') {
433     $ret[] = update_sql('ALTER TABLE {term_node} ADD PRIMARY KEY (tid,nid)');
434   }
435   elseif ($GLOBALS['db_type'] == 'pgsql') {
436     $ret[] = update_sql('ALTER TABLE {term_node} ADD PRIMARY KEY (tid,nid)');
437   }
438
439   return $ret;
440 }
441
442 function system_update_129() {
443   $ret = array();
444
445   if ($GLOBALS['db_type'] == 'mysql') {
446     $ret[] = update_sql("ALTER TABLE {vocabulary} ADD tags tinyint(3) unsigned default '0' NOT NULL");
447   }
448   elseif ($GLOBALS['db_type'] == 'pgsql') {
449     db_add_column($ret, 'vocabulary', 'tags', 'smallint', array('default' => 0, 'not null' => TRUE));
450   }
451
452   return $ret;
453 }
454
455 function system_update_130() {
456   $ret = array();
457
458   // This update has been moved to update_fix_sessions in update.php because it
459   // is needed for the basic functioning of the update script.
460
461   return $ret;
462 }
463
464 function system_update_131() {
465   $ret = array();
466
467   if ($GLOBALS['db_type'] == 'mysql') {
468     $ret[] = update_sql("ALTER TABLE {boxes} DROP INDEX title");
469     // Removed recreation of the index, which is not present in the db schema
470   }
471   elseif ($GLOBALS['db_type'] == 'pgsql') {
472     $ret[] = update_sql("ALTER TABLE {boxes} DROP CONSTRAINT {boxes}_title_key");
473   }
474
475   return $ret;
476 }
477
478 function system_update_132() {
479   /**
480    * PostgreSQL only update.
481    */
482   $ret = array();
483
484   if (!variable_get('update_132_done', FALSE)) {
485     if ($GLOBALS['db_type'] == 'pgsql') {
486       $ret[] = update_sql('DROP TABLE {search_total}');
487       $ret[] = update_sql("CREATE TABLE {search_total} (
488         word varchar(50) NOT NULL default '',
489              count float default NULL)");
490       $ret[] = update_sql('CREATE INDEX {search_total}_word_idx ON {search_total}(word)');
491
492       /**
493        * Wipe the search index
494        */
495       include_once './modules/search.module';
496       search_wipe();
497     }
498
499     variable_del('update_132_done');
500   }
501
502   return $ret;
503 }
504
505 function system_update_133() {
506   $ret = array();
507
508   if ($GLOBALS['db_type'] == 'mysql') {
509     $ret[] = update_sql("CREATE TABLE {contact} (
510       subject varchar(255) NOT NULL default '',
511       recipients longtext NOT NULL default '',
512       reply longtext NOT NULL default ''
513       )");
514     $ret[] = update_sql("ALTER TABLE {users} ADD login int(11) NOT NULL default '0'");
515   }
516   elseif ($GLOBALS['db_type'] == 'pgsql') {
517     // Table {contact} is changed in update_143() so I have moved it's creation there.
518     // It was never created here for postgres because of errors.
519
520     db_add_column($ret, 'users', 'login', 'int', array('default' => 0, 'not null' => TRUE));
521   }
522
523   return $ret;
524 }
525
526 function system_update_134() {
527   $ret = array();
528   $ret[] = update_sql('ALTER TABLE {blocks} DROP types');
529   return $ret;
530 }
531
532 function system_update_135() {
533   if (!variable_get('update_135_done', FALSE)) {
534     $result = db_query("SELECT delta FROM {blocks} WHERE module = 'aggregator'");
535     while ($block = db_fetch_object($result)) {
536       list($type, $id) = explode(':', $block->delta);
537       db_query("UPDATE {blocks} SET delta = '%s' WHERE module = 'aggregator' AND delta = '%s'", $type .'-'. $id, $block->delta);
538     }
539
540     variable_del('update_135_done');
541   }
542   return array();
543 }
544
545 function system_update_136() {
546   $ret = array();
547
548   switch ($GLOBALS['db_type']) {
549     case 'pgsql':
550       $ret[] = update_sql("DROP INDEX {users}_changed_idx"); // We drop the index first because it won't be renamed
551       $ret[] = update_sql("ALTER TABLE {users} RENAME changed TO access");
552       $ret[] = update_sql("CREATE INDEX {users}_access_idx on {users}(access)"); // Re-add the index
553       break;
554     case 'mysql':
555     case 'mysqli':
556       $ret[] = update_sql("ALTER TABLE {users} CHANGE COLUMN changed access int(11) NOT NULL default '0'");
557       break;
558   }
559
560   $ret[] = update_sql('UPDATE {users} SET access = login WHERE login > created');
561   $ret[] = update_sql('UPDATE {users} SET access = created WHERE access = 0');
562   return $ret;
563 }
564
565 function system_update_137() {
566   $ret = array();
567
568   if (!variable_get('update_137_done', FALSE)) {
569     if ($GLOBALS['db_type'] == 'mysql') {
570       $ret[] = update_sql("ALTER TABLE {locales_source} CHANGE location location varchar(255) NOT NULL default ''");
571     }
572     elseif ($GLOBALS['db_type'] == 'pgsql') {
573       db_change_column($ret, 'locales_source', 'location', 'location', 'varchar(255)', array('not null' => TRUE, 'default' => "''"));
574     }
575     variable_del('update_137_done');
576   }
577
578   return $ret;
579 }
580
581 function system_update_138() {
582   $ret = array();
583   // duplicate of update_97 which never got into the default database.* files.
584   $ret[] = update_sql("INSERT INTO {url_alias} (src, dst) VALUES ('node/feed', 'rss.xml')");
585   return $ret;
586 }
587
588 function system_update_139() {
589   $ret = array();
590   switch ($GLOBALS['db_type']) {
591     case 'pgsql':
592       db_add_column($ret, 'accesslog', 'timer', 'int', array('not null' => TRUE, 'default' => 0));
593       break;
594     case 'mysql':
595     case 'mysqli':
596       $ret[] = update_sql("ALTER TABLE {accesslog} ADD timer int(10) unsigned NOT NULL default '0'");
597       break;
598   }
599
600   return $ret;
601 }
602
603 function system_update_140() {
604   $ret = array();
605
606   if ($GLOBALS['db_type'] == 'mysql') {
607     $ret[] = update_sql("ALTER TABLE {url_alias} ADD INDEX (src)");
608   }
609   elseif ($GLOBALS['db_type'] == 'pgsql') {
610     $ret[] = update_sql("CREATE INDEX {url_alias}_src_idx ON {url_alias}(src)");
611   }
612   return $ret;
613 }
614
615 function system_update_141() {
616   $ret = array();
617
618   variable_del('upload_maxsize_total');
619
620   return $ret;
621 }
622
623 function system_update_142() {
624   $ret = array();
625
626   // This update has been moved to update_fix_sessions in update.php because it
627   // is needed for the basic functioning of the update script.
628
629   return $ret;
630 }
631
632 function system_update_143() {
633   $ret = array();
634
635   if ($GLOBALS['db_type'] == 'mysql') {
636     $ret[] = update_sql("ALTER TABLE {contact} CHANGE subject category VARCHAR(255) NOT NULL ");
637     $ret[] = update_sql("ALTER TABLE {contact} ADD PRIMARY KEY (category)");
638   }
639   elseif ($GLOBALS['db_type'] == 'pgsql') {
640     // Why the table is created here? See update_133().
641     $ret[] = update_sql("CREATE TABLE {contact} (
642       category varchar(255) NOT NULL default '',
643       recipients text NOT NULL default '',
644       reply text NOT NULL default '',
645       PRIMARY KEY (category))");
646   }
647
648   return $ret;
649 }
650
651 function system_update_144() {
652   $ret = array();
653   if ($GLOBALS['db_type'] == 'mysql') {
654     $ret[] = update_sql("ALTER TABLE {node} CHANGE type type VARCHAR(32) NOT NULL");
655   }
656   elseif ($GLOBALS['db_type'] == 'pgsql') {
657     $ret[] = update_sql("DROP INDEX {node}_type_idx"); // Drop indexes using "type" column
658     $ret[] = update_sql("DROP INDEX {node}_title_idx");
659     db_change_column($ret, 'node', 'type', 'type', 'varchar(32)', array('not null' => TRUE, 'default' => "''"));
660     // Let's recreate the indexes
661     $ret[] = update_sql("CREATE INDEX {node}_type_idx ON {node}(type)");
662     $ret[] = update_sql("CREATE INDEX {node}_title_type_idx ON {node}(title,type)");
663     $ret[] = update_sql("CREATE INDEX {node}_status_type_nid_idx ON {node}(status,type,nid)");
664   }
665   return $ret;
666 }
667
668 function system_update_145() {
669   $default_theme = variable_get('theme_default', 'bluemarine');
670
671   $themes = list_themes();
672   if (!array_key_exists($default_theme, $themes)) {
673       variable_set('theme_default', 'bluemarine');
674       $default_theme = 'bluemarine';
675    }
676
677   $ret = array();
678
679   switch ($GLOBALS['db_type']) {
680     case 'pgsql':
681       db_change_column($ret, 'blocks', 'region', 'region', 'varchar(64)', array('default' => "'left'", 'not null' => TRUE));
682       db_add_column($ret, 'blocks', 'theme', 'varchar(255)', array('not null' => TRUE, 'default' => "''"));
683       break;
684     case 'mysql':
685     case 'mysqli':
686       $ret[] = update_sql("ALTER TABLE {blocks} CHANGE region region varchar(64) default 'left' NOT NULL");
687       $ret[] = update_sql("ALTER TABLE {blocks} ADD theme varchar(255) NOT NULL default ''");
688       break;
689   }
690
691   // Intialize block data for default theme
692   $ret[] = update_sql("UPDATE {blocks} SET region = 'left' WHERE region = '0'");
693   $ret[] = update_sql("UPDATE {blocks} SET region = 'right' WHERE region = '1'");
694   db_query("UPDATE {blocks} SET theme = '%s'", $default_theme);
695
696   // Initialze block data for other enabled themes.
697   $themes = list_themes();
698   foreach (array_keys($themes) as $theme) {
699     if (($theme != $default_theme) && $themes[$theme]->status == 1) {
700       system_initialize_theme_blocks($theme);
701     }
702   }
703
704   return $ret;
705 }
706
707 function system_update_146() {
708   $ret = array();
709
710   if ($GLOBALS['db_type'] == 'mysql') {
711     $ret[] = update_sql("CREATE TABLE {node_revisions}
712                                 SELECT nid, nid AS vid, uid, type, title, body, teaser, changed AS timestamp, format
713                                 FROM {node}");
714
715     $ret[] = update_sql("ALTER TABLE {node_revisions} CHANGE nid nid int(10) unsigned NOT NULL default '0'");
716     $ret[] = update_sql("ALTER TABLE {node_revisions} ADD log longtext");
717
718     $ret[] = update_sql("ALTER TABLE {node} ADD vid int(10) unsigned NOT NULL default '0'");
719     $ret[] = update_sql("ALTER TABLE {files} ADD vid int(10) unsigned NOT NULL default '0'");
720     $ret[] = update_sql("ALTER TABLE {book} ADD vid int(10) unsigned NOT NULL default '0'");
721     $ret[] = update_sql("ALTER TABLE {forum} ADD vid int(10) unsigned NOT NULL default '0'");
722
723     $ret[] = update_sql("ALTER TABLE {book} DROP PRIMARY KEY");
724     $ret[] = update_sql("ALTER TABLE {forum} DROP PRIMARY KEY");
725     $ret[] = update_sql("ALTER TABLE {files} DROP PRIMARY KEY");
726
727     $ret[] = update_sql("UPDATE {node} SET vid = nid");
728     $ret[] = update_sql("UPDATE {forum} SET vid = nid");
729     $ret[] = update_sql("UPDATE {book} SET vid = nid");
730     $ret[] = update_sql("UPDATE {files} SET vid = nid");
731
732     $ret[] = update_sql("ALTER TABLE {book} ADD PRIMARY KEY vid (vid)");
733     $ret[] = update_sql("ALTER TABLE {forum} ADD PRIMARY KEY vid (vid)");
734     $ret[] = update_sql("ALTER TABLE {node_revisions} ADD PRIMARY KEY vid (vid)");
735     $ret[] = update_sql("ALTER TABLE {node_revisions} ADD KEY nid (nid)");
736     $ret[] = update_sql("ALTER TABLE {node_revisions} ADD KEY uid (uid)");
737
738     $ret[] = update_sql("CREATE TABLE {old_revisions} SELECT nid, type, revisions FROM {node} WHERE revisions != ''");
739
740     $ret[] = update_sql("ALTER TABLE {book} ADD KEY nid (nid)");
741     $ret[] = update_sql("ALTER TABLE {forum} ADD KEY nid (nid)");
742     $ret[] = update_sql("ALTER TABLE {files} ADD KEY fid (fid)");
743     $ret[] = update_sql("ALTER TABLE {files} ADD KEY vid (vid)");
744     $vid = db_next_id('{node}_nid');
745     $ret[] = update_sql("INSERT INTO {sequences} (name, id) VALUES ('{node_revisions}_vid', $vid)");
746   }
747   else { // pgsql
748     $ret[] = update_sql("CREATE TABLE {node_revisions} (
749       nid integer NOT NULL default '0',
750       vid integer NOT NULL default '0',
751       uid integer NOT NULL default '0',
752       title varchar(128) NOT NULL default '',
753       body text NOT NULL default '',
754       teaser text NOT NULL default '',
755       log text NOT NULL default '',
756       timestamp integer NOT NULL default '0',
757       format int NOT NULL default '0',
758       PRIMARY KEY (vid))");
759     $ret[] = update_sql("INSERT INTO {node_revisions} (nid, vid, uid, title, body, teaser, timestamp, format)
760       SELECT nid, nid AS vid, uid, title, body, teaser, changed AS timestamp, format
761       FROM {node}");
762     $ret[] = update_sql('CREATE INDEX {node_revisions}_nid_idx ON {node_revisions}(nid)');
763     $ret[] = update_sql('CREATE INDEX {node_revisions}_uid_idx ON {node_revisions}(uid)');
764     $vid = db_next_id('{node}_nid');
765     $ret[] = update_sql("CREATE SEQUENCE {node_revisions}_vid_seq INCREMENT 1 START $vid");
766
767     db_add_column($ret, 'node',  'vid', 'int', array('not null' => TRUE, 'default' => 0));
768     db_add_column($ret, 'files', 'vid', 'int', array('not null' => TRUE, 'default' => 0));
769     db_add_column($ret, 'book',  'vid', 'int', array('not null' => TRUE, 'default' => 0));
770     db_add_column($ret, 'forum', 'vid', 'int', array('not null' => TRUE, 'default' => 0));
771
772     $ret[] = update_sql("ALTER TABLE {book} DROP CONSTRAINT {book}_pkey");
773     $ret[] = update_sql("ALTER TABLE {forum} DROP CONSTRAINT {forum}_pkey");
774     $ret[] = update_sql("ALTER TABLE {files} DROP CONSTRAINT {files}_pkey");
775
776     $ret[] = update_sql("UPDATE {node} SET vid = nid");
777     $ret[] = update_sql("UPDATE {forum} SET vid = nid");
778     $ret[] = update_sql("UPDATE {book} SET vid = nid");
779     $ret[] = update_sql("UPDATE {files} SET vid = nid");
780
781     $ret[] = update_sql("ALTER TABLE {book} ADD PRIMARY KEY (vid)");
782     $ret[] = update_sql("ALTER TABLE {forum} ADD PRIMARY KEY (vid)");
783
784     $ret[] = update_sql("CREATE TABLE {old_revisions} AS SELECT nid, type, revisions FROM {node} WHERE revisions != ''");
785
786     $ret[] = update_sql('CREATE INDEX {node}_vid_idx ON {node}(vid)');
787     $ret[] = update_sql('CREATE INDEX {forum}_nid_idx ON {forum}(nid)');
788     $ret[] = update_sql('CREATE INDEX {files}_fid_idx ON {files}(fid)');
789     $ret[] = update_sql('CREATE INDEX {files}_vid_idx ON {files}(vid)');
790   }
791
792   // Move logs too.
793   $result = db_query("SELECT nid, log FROM {book} WHERE log != ''");
794   while ($row = db_fetch_object($result)) {
795     db_query("UPDATE {node_revisions} SET log = '%s' WHERE vid = %d", $row->log, $row->nid);
796   }
797
798   $ret[] = update_sql("ALTER TABLE {book} DROP log");
799   $ret[] = update_sql("ALTER TABLE {node} DROP teaser");
800   $ret[] = update_sql("ALTER TABLE {node} DROP body");
801   $ret[] = update_sql("ALTER TABLE {node} DROP format");
802   $ret[] = update_sql("ALTER TABLE {node} DROP revisions");
803
804   return $ret;
805 }
806
807 function system_update_147() {
808   $ret = array();
809
810   // this update is mysql only, pgsql should get it right in the first try.
811   if ($GLOBALS['db_type'] == 'mysql') {
812     $ret[] = update_sql("ALTER TABLE {node_revisions} DROP type");
813   }
814
815   return $ret;
816 }
817
818 function system_update_148() {
819   $ret = array();
820
821   // Add support for tracking users' session ids (useful for tracking anon users)
822   switch ($GLOBALS['db_type']) {
823     case 'pgsql':
824       db_add_column($ret, 'accesslog', 'sid', 'varchar(32)', array('not null' => TRUE, 'default' => "''"));
825       break;
826     case 'mysql':
827     case 'mysqli':
828       $ret[] = update_sql("ALTER TABLE {accesslog} ADD sid varchar(32) NOT NULL default ''");
829       break;
830   }
831
832   return $ret;
833 }
834
835 function system_update_149() {
836   $ret = array();
837
838   switch ($GLOBALS['db_type']) {
839     case 'pgsql':
840       db_add_column($ret, 'files', 'description', 'varchar(255)', array('not null' => TRUE, 'default' => "''"));
841       break;
842     case 'mysql':
843     case 'mysqli':
844       $ret[] = update_sql("ALTER TABLE {files} ADD COLUMN description VARCHAR(255) NOT NULL DEFAULT ''");
845       break;
846     default:
847       break;
848   }
849
850   return $ret;
851 }
852
853 function system_update_150() {
854   $ret = array();
855
856   $ret[] = update_sql("DELETE FROM {variable} WHERE name = 'node_cron_last'");
857   $ret[] = update_sql("DELETE FROM {variable} WHERE name = 'minimum_word_size'");
858   $ret[] = update_sql("DELETE FROM {variable} WHERE name = 'remove_short'");
859
860   $ret[] = update_sql("DELETE FROM {node_counter} WHERE nid = 0");
861
862   $ret[] = update_sql('DROP TABLE {search_index}');
863   $ret[] = update_sql('DROP TABLE {search_total}');
864
865   switch ($GLOBALS['db_type']) {
866     case 'mysqli':
867     case 'mysql':
868       $ret[] = update_sql("CREATE TABLE {search_dataset} (
869                            sid int(10) unsigned NOT NULL default '0',
870                            type varchar(16) default NULL,
871                            data longtext NOT NULL,
872                            KEY sid_type (sid, type)
873                            )");
874
875       $ret[] = update_sql("CREATE TABLE {search_index} (
876                            word varchar(50) NOT NULL default '',
877                            sid int(10) unsigned NOT NULL default '0',
878                            type varchar(16) default NULL,
879                            fromsid int(10) unsigned NOT NULL default '0',
880                            fromtype varchar(16) default NULL,
881                            score float default NULL,
882                            KEY sid_type (sid, type),
883                            KEY from_sid_type (fromsid, fromtype),
884                            KEY word (word)
885                            )");
886
887       $ret[] = update_sql("CREATE TABLE {search_total} (
888                            word varchar(50) NOT NULL default '',
889                            count float default NULL,
890                            PRIMARY KEY word (word)
891                            )");
892       break;
893     case 'pgsql':
894       $ret[] = update_sql("CREATE TABLE {search_dataset} (
895         sid integer NOT NULL default '0',
896         type varchar(16) default NULL,
897         data text NOT NULL default '')");
898       $ret[] = update_sql("CREATE INDEX {search_dataset}_sid_type_idx ON {search_dataset}(sid, type)");
899
900       $ret[] = update_sql("CREATE TABLE {search_index} (
901         word varchar(50) NOT NULL default '',
902         sid integer NOT NULL default '0',
903         type varchar(16) default NULL,
904         fromsid integer NOT NULL default '0',
905         fromtype varchar(16) default NULL,
906         score float default NULL)");
907       $ret[] = update_sql("CREATE INDEX {search_index}_sid_type_idx ON {search_index}(sid, type)");
908       $ret[] = update_sql("CREATE INDEX {search_index}_fromsid_fromtype_idx ON {search_index}(fromsid, fromtype)");
909       $ret[] = update_sql("CREATE INDEX {search_index}_word_idx ON {search_index}(word)");
910
911       $ret[] = update_sql("CREATE TABLE {search_total} (
912         word varchar(50) NOT NULL default '',
913         count float default NULL,
914         PRIMARY KEY(word))");
915       break;
916     default:
917       break;
918   }
919   return $ret;
920 }
921
922 function system_update_151() {
923   $ret = array();
924
925   $ts = variable_get('theme_settings', null);
926
927   // set up data array so we can loop over both sets of links
928   $menus = array(0 => array('links_var' => 'primary_links',
929                             'toggle_var' => 'toggle_primary_links',
930                             'more_var' => 'primary_links_more',
931                             'menu_name' => 'Primary links',
932                             'menu_var' => 'menu_primary_menu',
933                             'pid' => 0),
934                  1 => array('links_var' => 'secondary_links',
935                             'toggle_var' => 'toggle_secondary_links',
936                             'more_var' => 'secondary_links_more',
937                             'menu_name' => 'Secondary links',
938                             'menu_var' => 'menu_secondary_menu',
939                             'pid' => 0));
940
941   for ($loop = 0; $loop <= 1 ; $loop ++) {
942     // create new Primary and Secondary links menus
943     $menus[$loop]['pid'] = db_next_id('{menu}_mid');
944     $ret[] = update_sql("INSERT INTO {menu} (mid, pid, path, title, description, weight, type) " .
945                          "VALUES ({$menus[$loop]['pid']}, 0, '', '{$menus[$loop]['menu_name']}', '', 0, 115)");
946
947     // Gather links from various settings into a single array.
948     $phptemplate_links = variable_get("phptemplate_". $menus[$loop]['links_var'], array());
949     if (empty($phptemplate_links)) {
950       $phptemplate_links = array('text' => array(), 'link' => array());
951     }
952     if (isset($ts) && is_array($ts)) {
953       if (is_array($ts[$menus[$loop]['links_var']])) {
954         $theme_links = $ts[$menus[$loop]['links_var']];
955       }
956       else {
957         // Convert old xtemplate style links.
958         preg_match_all('/<a\s+.*?href=[\"\'\s]?(.*?)[\"\'\s]?>(.*?)<\/a>/i', $ts[$menus[$loop]['links_var']], $urls);
959         $theme_links['text'] = $urls[2];
960         $theme_links['link'] = $urls[1];
961       }
962     }
963     else {
964       $theme_links = array('text' => array(), 'link' => array());
965     }
966     $links['text'] = array_merge($phptemplate_links['text'], $theme_links['text']);
967     $links['link'] = array_merge($phptemplate_links['link'], $theme_links['link']);
968
969     // insert all entries from theme links into new menus
970     $num_inserted = 0;
971     for ($i = 0; $i < count($links['text']); $i++) {
972       if ($links['text'][$i] != "" && $links['link'][$i] != "") {
973         $num_inserted ++;
974         $node_unalias = db_fetch_array(db_query("SELECT src FROM {url_alias} WHERE dst = '%s'", $links['link'][$i]));
975         if (isset($node_unalias) && is_array($node_unalias)) {
976           $link_path = $node_unalias['src'];
977         }
978         else {
979           $link_path = $links['link'][$i];
980         }
981
982         $mid = db_next_id('{menu}_mid');
983         $ret[] = update_sql("INSERT INTO {menu} (mid, pid, path, title, description, weight, type) " .
984                              "VALUES ($mid, {$menus[$loop]['pid']}, '" . db_escape_string($link_path) .
985                              "', '" . db_escape_string($links['text'][$i]) .
986                              "', '" . db_escape_string($links['description'][$i]) . "', 0, 118)");
987       }
988     }
989     // delete Secondary links if not populated.
990     if ($loop == 1 && $num_inserted == 0) {
991       db_query("DELETE FROM {menu} WHERE mid={$menus[$loop]['pid']}");
992     }
993
994     // Set menu_primary_menu and menu_primary_menu variables if links were
995     // imported. If the user had links but the toggle display was off, they
996     // will need to disable the new links manually in admins/settings/menu.
997     if ($num_inserted == 0) {
998       variable_set($menus[$loop]['menu_var'], 0);
999     }
1000     else {
1001       variable_set($menus[$loop]['menu_var'], $menus[$loop]['pid']);
1002     }
1003     variable_del('phptemplate_' .$menus[$loop]['links_var']);
1004     variable_del('phptemplate_'. $menus[$loop]['links_var'] .'_more');
1005     variable_del($menus[$loop]['toggle_var']);
1006     variable_del($menus[$loop]['more_var']);
1007     // If user has old xtemplate links in a string, leave them in the var.
1008     if (isset($ts) && is_array($ts) && is_array($ts[$menus[$loop]['links_var']])) {
1009       variable_del($menus[$loop]['links_var']);
1010     }
1011   }
1012
1013   if (isset($ts) && is_array($ts)) {
1014     variable_set('theme_settings', $ts);
1015   }
1016
1017   $ret[] = update_sql("UPDATE {system} SET status = 1 WHERE name = 'menu'");
1018
1019   return $ret;
1020 }
1021
1022 function system_update_152() {
1023   $ret = array();
1024
1025   // Postgresql only update
1026   switch ($GLOBALS['db_type']) {
1027     case 'pgsql':
1028       $ret[] = update_sql("ALTER TABLE {forum} DROP shadow");
1029       break;
1030     case 'mysql':
1031     case 'mysqli':
1032       break;
1033   }
1034
1035   return $ret;
1036 }
1037
1038 function system_update_153(){
1039   $ret = array();
1040   switch ($GLOBALS['db_type']) {
1041     case 'pgsql':
1042       $ret[] = update_sql("ALTER TABLE {contact} DROP CONSTRAINT {contact}_pkey");
1043       $ret[] = update_sql("CREATE SEQUENCE {contact}_cid_seq");
1044       db_add_column($ret, 'contact', 'cid', 'int', array('not null' => TRUE, 'default' => "nextval('{contact}_cid_seq')"));
1045       $ret[] = update_sql("ALTER TABLE {contact} ADD PRIMARY KEY (cid)");
1046       $ret[] = update_sql("ALTER TABLE {contact} ADD CONSTRAINT {contact}_category_key UNIQUE (category)");
1047       break;
1048     case 'mysql':
1049     case 'mysqli':
1050       $ret[] = update_sql("ALTER TABLE {contact} DROP PRIMARY KEY");
1051       $ret[] = update_sql("ALTER TABLE {contact} ADD COLUMN cid int(11) NOT NULL PRIMARY KEY auto_increment");
1052       $ret[] = update_sql("ALTER TABLE {contact} ADD UNIQUE KEY category (category)");
1053       break;
1054   }
1055   return $ret;
1056 }
1057
1058 function system_update_154() {
1059   $ret = array();
1060   switch ($GLOBALS['db_type']) {
1061     case 'pgsql':
1062       db_add_column($ret, 'contact', 'weight', 'smallint', array('not null' => TRUE, 'default' => 0));
1063       db_add_column($ret, 'contact', 'selected', 'smallint', array('not null' => TRUE, 'default' => 0));
1064       break;
1065     case 'mysql':
1066     case 'mysqli':
1067       $ret[] = update_sql("ALTER TABLE {contact} ADD COLUMN weight tinyint(3) NOT NULL DEFAULT 0");
1068       $ret[] = update_sql("ALTER TABLE {contact} ADD COLUMN selected tinyint(1) NOT NULL DEFAULT 0");
1069       break;
1070   }
1071   return $ret;
1072 }
1073
1074 function system_update_155() {
1075   $ret = array();
1076
1077   // Postgresql only update
1078   switch ($GLOBALS['db_type']) {
1079     case 'pgsql':
1080       $ret[] = update_sql("DROP TABLE {cache}");
1081       $ret[] = update_sql("CREATE TABLE {cache} (
1082         cid varchar(255) NOT NULL default '',
1083         data bytea default '',
1084         expire integer NOT NULL default '0',
1085         created integer NOT NULL default '0',
1086         headers text default '',
1087         PRIMARY KEY (cid)
1088         )");
1089       $ret[] = update_sql("CREATE INDEX {cache}_expire_idx ON {cache}(expire)");
1090       break;
1091     case 'mysql':
1092     case 'mysqli':
1093       break;
1094   }
1095
1096   return $ret;
1097 }
1098
1099 function system_update_156() {
1100   $ret = array();
1101   $ret[] = update_sql("DELETE FROM {cache}");
1102   system_themes();
1103   return $ret;
1104 }
1105
1106 function system_update_157() {
1107   $ret = array();
1108   $ret[] = update_sql("DELETE FROM {url_alias} WHERE src = 'node/feed' AND dst = 'rss.xml'");
1109   $ret[] = update_sql("INSERT INTO {url_alias} (src, dst) VALUES ('rss.xml', 'node/feed')");
1110   return $ret;
1111 }
1112
1113 function system_update_158() {
1114   $ret = array();
1115
1116   switch ($GLOBALS['db_type']) {
1117     case 'mysqli':
1118     case 'mysql':
1119       $ret[] = update_sql("ALTER TABLE {old_revisions} ADD done tinyint(1) NOT NULL DEFAULT 0");
1120       $ret[] = update_sql("ALTER TABLE {old_revisions} ADD INDEX (done)");
1121       break;
1122
1123     case 'pgsql':
1124       db_add_column($ret, 'old_revisions', 'done', 'smallint', array('not null' => TRUE, 'default' => 0));
1125       $ret[] = update_sql('CREATE INDEX {old_revisions}_done_idx ON {old_revisions}(done)');
1126       break;
1127   }
1128
1129   return $ret;
1130 }
1131
1132 /**
1133  * Retrieve data out of the old_revisions table and put into new revision
1134  * system.
1135  *
1136  * The old_revisions table is not deleted because any data which could not be
1137  * put into the new system is retained.
1138  */
1139 function system_update_159() {
1140   $ret = array();
1141
1142   $result = db_query_range("SELECT * FROM {old_revisions} WHERE done = 0 AND type IN ('page', 'story', 'poll', 'book', 'forum', 'blog') ORDER BY nid DESC", 0, 20);
1143
1144   if (db_num_rows($result)) {
1145     $vid = db_next_id('{node_revisions}_vid');
1146     while ($node = db_fetch_object($result)) {
1147       $revisions = unserialize($node->revisions);
1148       if (isset($revisions) && is_array($revisions) && count($revisions) > 0) {
1149         $revisions_query = array();
1150         $revisions_args = array();
1151         $book_query = array();
1152         $book_args = array();
1153         $forum_query = array();
1154         $forum_args = array();
1155         foreach ($revisions as $version) {
1156           $revision = array();
1157           foreach ($version['node'] as $node_field => $node_value) {
1158             $revision[$node_field] = $node_value;
1159           }
1160           $revision['uid'] = $version['uid'];
1161           $revision['timestamp'] = $version['timestamp'];
1162           $vid++;
1163           $revisions_query[] = "(%d, %d, %d, '%s', '%s', '%s', '%s', %d, %d)";
1164           $revisions_args = array_merge($revisions_args, array($node->nid, $vid, $revision['uid'], $revision['title'], $revision['body'], $revision['teaser'], $revision['log'], $revision['timestamp'], $revision['format']));
1165           switch ($node->type) {
1166             case 'forum':
1167               if ($revision['tid'] > 0) {
1168                 $forum_query[] = "(%d, %d, %d)";
1169                 $forum_args = array_merge($forum_args, array($vid, $node->nid, $revision['tid']));
1170               }
1171               break;
1172
1173             case 'book':
1174               $book_query[] = "(%d, %d, %d, %d)";
1175               $book_args = array_merge($book_args, array($vid, $node->nid, $revision['parent'], $revision['weight']));
1176               break;
1177           }
1178         }
1179         if (count($revisions_query)) {
1180           $revision_status = db_query("INSERT INTO {node_revisions} (nid, vid, uid, title, body, teaser, log, timestamp, format) VALUES ". implode(',', $revisions_query), $revisions_args);
1181         }
1182         if (count($forum_query)) {
1183           $forum_status = db_query("INSERT INTO {forum} (vid, nid, tid) VALUES ". implode(',', $forum_query), $forum_args);
1184         }
1185         if (count($book_query)) {
1186           $book_status = db_query("INSERT INTO {book} (vid, nid, parent, weight) VALUES ". implode(',', $book_query), $book_args);
1187         }
1188         $delete = FALSE;
1189         switch ($node->type) {
1190           case 'forum':
1191             if ($forum_status && $revision_status) {
1192               $delete = TRUE;
1193             }
1194             break;
1195
1196           case 'book':
1197             if ($book_status && $revision_status) {
1198               $delete = TRUE;
1199             }
1200             break;
1201
1202           default:
1203             if ($revision_status) {
1204               $delete = TRUE;
1205             }
1206             break;
1207         }
1208
1209         if ($delete) {
1210           db_query('DELETE FROM {old_revisions} WHERE nid = %d', $node->nid);
1211         }
1212         else {
1213           db_query('UPDATE {old_revisions} SET done = 1 WHERE nid = %d', $node->nid);
1214         }
1215
1216         switch ($GLOBALS['db_type']) {
1217           case 'mysqli':
1218           case 'mysql':
1219             $ret[] = update_sql("UPDATE {sequences} SET id = $vid WHERE name = '{node_revisions}_vid'");
1220             break;
1221
1222           case 'pgsql':
1223             $ret[] = update_sql("SELECT setval('{node_revisions}_vid_seq', $vid)");
1224             break;
1225         }
1226       }
1227       else {
1228         db_query('UPDATE {old_revisions} SET done = 1 WHERE nid = %d', $node->nid);
1229         watchdog('php', "Recovering old revisions for node $node->nid failed.", WATCHDOG_WARNING);
1230       }
1231     }
1232   }
1233
1234   if (db_num_rows($result) < 20) {
1235     $ret[] = update_sql('ALTER TABLE {old_revisions} DROP done');
1236   }
1237   else {
1238     $ret['#finished'] = FALSE;
1239   }
1240
1241   return $ret;
1242 }
1243
1244 function system_update_160() {
1245   $types = module_invoke('node', 'get_types');
1246   if (is_array($types)) {
1247     foreach($types as $type) {
1248       if (!is_array(variable_get("node_options_$type", array()))) {
1249         variable_set("node_options_$type", array());
1250       }
1251     }
1252   }
1253   return array();
1254 }
1255
1256 function system_update_161() {
1257   variable_del('forum_icon_path');
1258   return array();
1259 }
1260
1261 function system_update_162() {
1262   $ret = array();
1263
1264   // PostgreSQL only update
1265   switch ($GLOBALS['db_type']) {
1266     case 'pgsql':
1267
1268       $ret[] = update_sql('DROP INDEX {book}_parent');
1269       $ret[] = update_sql('CREATE INDEX {book}_parent_idx ON {book}(parent)');
1270
1271       $ret[] = update_sql('DROP INDEX {node_comment_statistics}_timestamp_idx');
1272       $ret[] = update_sql('CREATE INDEX {node_comment_statistics}_last_comment_timestamp_idx ON {node_comment_statistics}(last_comment_timestamp)');
1273
1274       $ret[] = update_sql('ALTER TABLE {filters} ALTER delta SET DEFAULT 0');
1275       $ret[] = update_sql('DROP INDEX {filters}_module_idx');
1276
1277       $ret[] = update_sql('DROP INDEX {locales_target}_lid_idx');
1278       $ret[] = update_sql('DROP INDEX {locales_target}_lang_idx');
1279       $ret[] = update_sql('CREATE INDEX {locales_target}_locale_idx ON {locales_target}(locale)');
1280
1281       $ret[] = update_sql('DROP INDEX {node}_created');
1282       $ret[] = update_sql('CREATE INDEX {node}_created_idx ON {node}(created)');
1283       $ret[] = update_sql('DROP INDEX {node}_changed');
1284       $ret[] = update_sql('CREATE INDEX {node}_changed_idx ON {node}(changed)');
1285
1286       $ret[] = update_sql('DROP INDEX {profile_fields}_category');
1287       $ret[] = update_sql('CREATE INDEX {profile_fields}_category_idx ON {profile_fields}(category)');
1288
1289       $ret[] = update_sql('DROP INDEX {url_alias}_dst_idx');
1290       $ret[] = update_sql('CREATE UNIQUE INDEX {url_alias}_dst_idx ON {url_alias}(dst)');
1291
1292       $ret[] = update_sql('CREATE INDEX {sessions}_uid_idx ON {sessions}(uid)');
1293       $ret[] = update_sql('CREATE INDEX {sessions}_timestamp_idx ON {sessions}(timestamp)');
1294
1295       $ret[] = update_sql('ALTER TABLE {accesslog} DROP mask');
1296
1297       db_change_column($ret, 'accesslog', 'path', 'path', 'text');
1298       db_change_column($ret, 'accesslog', 'url', 'url', 'text');
1299       db_change_column($ret, 'watchdog', 'link', 'link', 'text', array('not null' => TRUE, 'default' => "''"));
1300       db_change_column($ret, 'watchdog', 'location', 'location', 'text', array('not null' => TRUE, 'default' => "''"));
1301       db_change_column($ret, 'watchdog', 'referer', 'referer', 'text', array('not null' => TRUE, 'default' => "''"));
1302
1303       break;
1304   }
1305
1306   return $ret;
1307 }
1308
1309 function system_update_163() {
1310   $ret = array();
1311   if ($GLOBALS['db_type'] == 'mysql' || $GLOBALS['db_type'] == 'mysqli') {
1312     $ret[] = update_sql('ALTER TABLE {cache} CHANGE data data LONGBLOB');
1313   }
1314   return $ret;
1315 }
1316
1317 function system_update_164() {
1318   $ret = array();
1319
1320   switch ($GLOBALS['db_type']) {
1321     case 'mysql':
1322     case 'mysqli':
1323       $ret[] = update_sql("CREATE TABLE {poll_votes} (
1324           nid int(10) unsigned NOT NULL,
1325           uid int(10) unsigned NOT NULL default 0,
1326           hostname varchar(128) NOT NULL default '',
1327           INDEX (nid),
1328           INDEX (uid),
1329           INDEX (hostname)
1330       )");
1331       break;
1332
1333     case 'pgsql':
1334       $ret[] = update_sql("CREATE TABLE {poll_votes} (
1335           nid int NOT NULL,
1336           uid int NOT NULL default 0,
1337           hostname varchar(128) NOT NULL default ''
1338       )");
1339       $ret[] = update_sql('CREATE INDEX {poll_votes}_nid_idx ON {poll_votes} (nid)');
1340       $ret[] = update_sql('CREATE INDEX {poll_votes}_uid_idx ON {poll_votes} (uid)');
1341       $ret[] = update_sql('CREATE INDEX {poll_votes}_hostname_idx ON {poll_votes} (hostname)');
1342       break;
1343   }
1344
1345   $result = db_query('SELECT nid, polled FROM {poll}');
1346   while ($poll = db_fetch_object($result)) {
1347     foreach (explode(' ', $poll->polled) as $polled) {
1348       if ($polled[0] == '_') {
1349         // $polled is a user id
1350         db_query('INSERT INTO {poll_votes} (nid, uid) VALUES (%d, %d)', $poll->nid, substr($polled, 1, -1));
1351       }
1352       else {
1353         // $polled is a host
1354         db_query("INSERT INTO {poll_votes} (nid, hostname) VALUES (%d, '%s')", $poll->nid, $polled);
1355       }
1356     }
1357   }
1358
1359   $ret[] = update_sql('ALTER TABLE {poll} DROP polled');
1360
1361   return $ret;
1362 }
1363
1364 function system_update_165() {
1365   $cron_last = max(variable_get('drupal_cron_last', 0), variable_get('ping_cron_last', 0));
1366   variable_set('cron_last', $cron_last);
1367   variable_del('drupal_cron_last');
1368   variable_del('ping_cron_last');
1369   return array();
1370 }
1371
1372 function system_update_166() {
1373   $ret = array();
1374
1375   $ret[] = update_sql("DROP TABLE {directory}");
1376   switch ($GLOBALS['db_type']) {
1377     case 'mysqli':
1378     case 'mysql':
1379       $ret[] = update_sql("CREATE TABLE {client} (
1380         cid int(10) unsigned NOT NULL auto_increment,
1381         link varchar(255) NOT NULL default '',
1382         name varchar(128) NOT NULL default '',
1383         mail varchar(128) NOT NULL default '',
1384         slogan longtext NOT NULL,
1385         mission longtext NOT NULL,
1386         users int(10) NOT NULL default '0',
1387         nodes int(10) NOT NULL default '0',
1388         version varchar(35) NOT NULL default'',
1389         created int(11) NOT NULL default '0',
1390         changed int(11) NOT NULL default '0',
1391         PRIMARY KEY (cid)
1392       )");
1393       $ret[] = update_sql("CREATE TABLE {client_system} (
1394         cid int(10) NOT NULL default '0',
1395         name varchar(255) NOT NULL default '',
1396         type varchar(255) NOT NULL default '',
1397         PRIMARY KEY (cid,name)
1398       )");
1399       break;
1400
1401     case 'pgsql':
1402       $ret[] = update_sql("CREATE TABLE {client} (
1403         cid SERIAL,
1404         link varchar(255) NOT NULL default '',
1405         name varchar(128) NOT NULL default '',
1406         mail varchar(128) NOT NULL default '',
1407         slogan text NOT NULL default '',
1408         mission text NOT NULL default '',
1409         users integer NOT NULL default '0',
1410         nodes integer NOT NULL default '0',
1411         version varchar(35) NOT NULL default'',
1412         created integer NOT NULL default '0',
1413         changed integer NOT NULL default '0',
1414         PRIMARY KEY (cid)
1415       )");
1416       $ret[] = update_sql("CREATE TABLE {client_system} (
1417         cid integer NOT NULL,
1418         name varchar(255) NOT NULL default '',
1419         type varchar(255) NOT NULL default '',
1420         PRIMARY KEY (cid,name)
1421       )");
1422       break;
1423   }
1424
1425   return $ret;
1426 }
1427
1428 function system_update_167() {
1429   $ret = array();
1430
1431   switch ($GLOBALS['db_type']) {
1432     case 'mysqli':
1433     case 'mysql':
1434       $ret[] = update_sql("ALTER TABLE {vocabulary_node_types} CHANGE type type varchar(32) NOT NULL default ''");
1435       break;
1436     case 'pgsql':
1437       db_change_column($ret, 'vocabulary_node_types', 'type', 'type', 'varchar(32)', array('not null' => TRUE, 'default' => "''"));
1438       $ret[] = update_sql("ALTER TABLE {vocabulary_node_types} ADD PRIMARY KEY (vid, type)");
1439       break;
1440   }
1441
1442   return $ret;
1443 }
1444
1445 function system_update_168() {
1446   $ret = array();
1447
1448   $ret[] = update_sql("ALTER TABLE {term_hierarchy} ADD PRIMARY KEY (tid, parent)");
1449
1450   return $ret;
1451 }
1452
1453 function system_update_169() {
1454   // Warn PGSQL admins if their database is set up incorrectly
1455   if ($GLOBALS['db_type'] == 'pgsql') {
1456     $encoding = db_result(db_query('SHOW server_encoding'));
1457     if (!in_array(strtolower($encoding), array('unicode', 'utf8'))) {
1458       $msg = 'Your PostgreSQL database is set up with the wrong character encoding ('. $encoding .'). It is possible it will not work as expected. It is advised to recreate it with UTF-8/Unicode encoding. More information can be found in the <a href="http://www.postgresql.org/docs/7.4/interactive/multibyte.html">PostgreSQL documentation</a>.';
1459       watchdog('php', $msg, WATCHDOG_WARNING);
1460       drupal_set_message($msg, 'status');
1461     }
1462   }
1463
1464   // Note: 'access' table manually updated in update.php
1465   return _system_update_utf8(array(
1466     'accesslog', 'aggregator_category',
1467     'aggregator_category_feed', 'aggregator_category_item',
1468     'aggregator_feed', 'aggregator_item', 'authmap', 'blocks',
1469     'book', 'boxes', 'cache', 'comments', 'contact',
1470     'node_comment_statistics', 'client', 'client_system', 'files',
1471     'filter_formats', 'filters', 'flood', 'forum', 'history',
1472     'locales_meta', 'locales_source', 'locales_target', 'menu',
1473     'node', 'node_access', 'node_revisions', 'profile_fields',
1474     'profile_values', 'url_alias', 'permission', 'poll', 'poll_votes',
1475     'poll_choices', 'role', 'search_dataset', 'search_index',
1476     'search_total', 'sessions', 'sequences', 'node_counter',
1477     'system', 'term_data', 'term_hierarchy', 'term_node',
1478     'term_relation', 'term_synonym', 'users', 'users_roles', 'variable',
1479     'vocabulary', 'vocabulary_node_types', 'watchdog'
1480   ));
1481 }
1482
1483 /**
1484  * Converts a set of tables to UTF-8 encoding.
1485  *
1486  * This update is designed to be re-usable by contrib modules and is
1487  * used by system_update_169().
1488  */
1489 function _system_update_utf8($tables) {
1490   // Are we starting this update for the first time?
1491   if (!isset($_SESSION['update_utf8'])) {
1492     switch ($GLOBALS['db_type']) {
1493       // Only for MySQL 4.1+
1494       case 'mysqli':
1495         break;
1496       case 'mysql':
1497         if (version_compare(mysql_get_server_info($GLOBALS['active_db']), '4.1.0', '<')) {
1498           return array();
1499         }
1500         break;
1501       case 'pgsql':
1502         return array();
1503     }
1504
1505     // See if database uses UTF-8 already
1506     global $db_url;
1507     $url = parse_url(is_array($db_url) ? $db_url['default'] : $db_url);
1508     $db_name = substr($url['path'], 1);
1509     $result = db_fetch_array(db_query('SHOW CREATE DATABASE `%s`', $db_name));
1510     if (preg_match('/utf8/i', array_pop($result))) {
1511       return array();
1512     }
1513
1514     // Make list of tables to convert
1515     $_SESSION['update_utf8'] = $tables;
1516     // Keep track of total for progress bar
1517     $_SESSION['update_utf8_total'] = count($tables);
1518   }
1519
1520   // Fetch remaining tables list and convert next table
1521   $list = &$_SESSION['update_utf8'];
1522
1523   $ret = update_convert_table_utf8(array_shift($list));
1524
1525   // Are we done?
1526   if (count($list) == 0) {
1527     unset($_SESSION['update_utf8']);
1528     unset($_SESSION['update_utf8_total']);
1529     return $ret;
1530   }
1531
1532   // Progress percentage
1533   $ret['#finished'] = 1 - (count($list) / $_SESSION['update_utf8_total']);
1534   return $ret;
1535 }
1536
1537 function system_update_170() {
1538   if (!variable_get('update_170_done', false)) {
1539     switch ($GLOBALS['db_type']) {
1540       case 'pgsql':
1541         $ret = array();
1542         db_change_column($ret, 'system', 'schema_version', 'schema_version', 'smallint', array('not null' => TRUE, 'default' => -1));
1543         break;
1544
1545       case 'mysql':
1546       case 'mysqli':
1547         db_query('ALTER TABLE {system} CHANGE schema_version schema_version smallint(3) not null default -1');
1548         break;
1549     }
1550     // Set schema version -1 (uninstalled) for disabled modules (only affects contrib).
1551     db_query('UPDATE {system} SET schema_version = -1 WHERE status = 0 AND schema_version = 0');
1552   }
1553   return array();
1554 }
1555
1556 function system_update_171() {
1557   $ret = array();
1558   $ret[] = update_sql('DELETE FROM {users_roles} WHERE rid IN ('. DRUPAL_ANONYMOUS_RID. ', '. DRUPAL_AUTHENTICATED_RID. ')');
1559   return $ret;
1560 }
1561
1562 function system_update_172() {
1563   // Multi-part update
1564   if (!isset($_SESSION['system_update_172'])) {
1565     $_SESSION['system_update_172'] = 0;
1566     $_SESSION['system_update_172_max'] = db_result(db_query('SELECT MAX(cid) FROM {comments}'));
1567   }
1568
1569   include_once './modules/comment.module';
1570
1571   $limit = 20;
1572   $result = db_query_range("SELECT cid, thread FROM {comments} WHERE cid > %d ORDER BY cid ASC", $_SESSION['system_update_172'], 0, $limit);
1573   while ($comment = db_fetch_object($result)) {
1574     $_SESSION['system_update_172'] = $comment->cid;
1575     $thread = explode('.', rtrim($comment->thread, '/'));
1576     foreach ($thread as $i => $offset) {
1577       // Decode old-style comment codes: 1,2,...,9,90,91,92,...,99,990,991,...
1578       $thread[$i] = int2vancode((strlen($offset) - 1) * 10 + substr($offset, -1, 1));
1579     }
1580     $thread = implode('.', $thread) .'/';
1581     db_query("UPDATE {comments} SET thread = '%s' WHERE cid = %d", $thread, $comment->cid);
1582   }
1583
1584   if ($_SESSION['system_update_172'] == $_SESSION['system_update_172_max']) {
1585     unset($_SESSION['system_update_172']);
1586     unset($_SESSION['system_update_172_max']);
1587     return array();
1588   }
1589   return array('#finished' => $_SESSION['system_update_172'] / $_SESSION['system_update_172_max']);
1590 }
1591
1592 function system_update_173() {
1593   $ret = array();
1594   // State tracker to determine whether we keep a backup of the files table or not.
1595   $safe = TRUE;
1596
1597   // PostgreSQL needs CREATE TABLE foobar _AS_ SELECT ...
1598   $AS = ($GLOBALS['db_type'] == 'pgsql') ? 'AS' : '';
1599
1600   // Backup the files table.
1601   $ret[] = update_sql("CREATE TABLE {files_backup} $AS SELECT * FROM {files}");
1602
1603   // Do some files table sanity checking and cleanup.
1604   $ret[] = update_sql('DELETE FROM {files} WHERE fid = 0');
1605   $ret[] = update_sql('UPDATE {files} SET vid = nid WHERE vid = 0');
1606
1607   // Create a temporary table to build the new file_revisions and files tables from.
1608   $ret[] = update_sql("CREATE TABLE {files_tmp} $AS SELECT * FROM {files}");
1609   $ret[] = update_sql('DROP TABLE {files}');
1610
1611   switch ($GLOBALS['db_type']) {
1612     case 'pgsql':
1613       // create file_revisions table
1614       $ret[] = update_sql("CREATE TABLE {file_revisions} (
1615         fid integer NOT NULL default 0,
1616         vid integer NOT NULL default 0,
1617         description varchar(255) NOT NULL default '',
1618         list smallint NOT NULL default 0,
1619         PRIMARY KEY (fid, vid))");
1620       $result = update_sql("INSERT INTO {file_revisions} SELECT DISTINCT ON (fid,vid) fid, vid, description, list FROM {files_tmp}");
1621       $ret[] = $result;
1622       if ($result['success'] === FALSE) {
1623         $safe = FALSE;
1624       }
1625
1626       // Create normalized files table
1627       $ret[] = update_sql("CREATE TABLE {files} (
1628         fid SERIAL,
1629         nid integer NOT NULL default 0,
1630         filename varchar(255) NOT NULL default '',
1631         filepath varchar(255) NOT NULL default '',
1632         filemime varchar(255) NOT NULL default '',
1633         filesize integer NOT NULL default 0,
1634         PRIMARY KEY (fid))");
1635       $result = update_sql("INSERT INTO {files} SELECT DISTINCT ON (fid) fid, nid, filename, filepath, filemime, filesize FROM {files_tmp}");
1636       $ret[] = $result;
1637       if ($result['success'] === FALSE) {
1638         $safe = FALSE;
1639       }
1640
1641       $ret[] = update_sql("SELECT setval('{files}_fid_seq', max(fid)) FROM {files}");
1642
1643       break;
1644
1645     case 'mysqli':
1646     case 'mysql':
1647       // create file_revisions table
1648       $ret[] = update_sql("CREATE TABLE {file_revisions} (
1649         fid int(10) unsigned NOT NULL default 0,
1650         vid int(10) unsigned NOT NULL default 0,
1651         description varchar(255) NOT NULL default '',
1652         list tinyint(1) unsigned NOT NULL default 0,
1653         PRIMARY KEY (fid, vid)
1654         ) /*!40100 DEFAULT CHARACTER SET utf8 */");
1655
1656       // Try as you might mysql only does distinct row if you are selecting more than 1 column.
1657       $result = update_sql('INSERT INTO {file_revisions} SELECT DISTINCT fid , vid, description, list FROM {files_tmp}');
1658       $ret[] = $result;
1659       if ($result['success'] === FALSE) {
1660         $safe = FALSE;
1661       }
1662
1663       $ret[] = update_sql("CREATE TABLE {files} (
1664         fid int(10) unsigned NOT NULL default 0,
1665         nid int(10) unsigned NOT NULL default 0,
1666         filename varchar(255) NOT NULL default '',
1667         filepath varchar(255) NOT NULL default '',
1668         filemime varchar(255) NOT NULL default '',
1669         filesize int(10) unsigned NOT NULL default 0,
1670         PRIMARY KEY (fid)
1671         ) /*!40100 DEFAULT CHARACTER SET utf8 */");
1672       $result = update_sql("INSERT INTO {files} SELECT DISTINCT fid, nid, filename, filepath, filemime, filesize FROM {files_tmp}");
1673       $ret[] = $result;
1674       if ($result['success'] === FALSE) {
1675         $safe = FALSE;
1676       }
1677
1678       break;
1679   }
1680
1681   $ret[] = update_sql("DROP TABLE {files_tmp}");
1682
1683   // Remove original files table if all went well. Otherwise preserve it and notify user.
1684   if ($safe) {
1685     $ret[] = update_sql("DROP TABLE {files_backup}");
1686   }
1687   else {
1688     drupal_set_message('Normalizing files table failed. A backup of the original table called {files_backup} remains in your database.');
1689   }
1690
1691   return $ret;
1692 }
1693
1694 function system_update_174() {
1695   // This update (update comments system variables on upgrade) has been removed.
1696   return array();
1697 }
1698
1699 function system_update_175() {
1700   $result = db_query('SELECT * FROM {url_alias}');
1701   while ($path = db_fetch_object($result)) {
1702     $path->src = urldecode($path->src);
1703     $path->dst = urldecode($path->dst);
1704     db_query("UPDATE {url_alias} SET dst = '%s', src = '%s' WHERE pid = %d", $path->dst, $path->src, $path->pid);
1705   }
1706   return array();
1707 }
1708
1709 function system_update_176() {
1710   $ret = array();
1711   $ret[] = update_sql('ALTER TABLE {filter_formats} ADD UNIQUE (name)');
1712   return $ret;
1713 }
1714
1715 function system_update_177() {
1716   $ret = array();
1717   $message_ids = array(
1718     'welcome_subject' => 'Welcome subject',
1719     'welcome_body' => 'Welcome body text',
1720     'approval_subject' => 'Approval subject',
1721     'approval_body' => 'Approval body text',
1722     'pass_subject' => 'Password reset subject',
1723     'pass_body' => 'Password reset body text',
1724   );
1725   foreach ($message_ids as $message_id => $message_text) {
1726     if ($admin_setting = variable_get('user_mail_'. $message_id, FALSE)) {
1727       // Insert newlines and escape for display as HTML
1728       $admin_setting = nl2br(check_plain($message_text ."\n\n". $admin_setting));
1729       watchdog('legacy', $admin_setting);
1730       $last = db_fetch_object(db_query('SELECT max(wid) AS wid FROM {watchdog}'));
1731       // Deleting is required, because _user_mail_text() checks for the existance of the variable.
1732       variable_del('user_mail_'. $message_id);
1733       $ret[] = array(
1734         'query' => strtr('The mail template %message_id has been reset to the default. The old template <a href="%url">has been saved</a>.', array('%message_id' => 'user_mail_'. $message_id, '%url' => url('admin/logs/event/'. $last->wid))),
1735         'success' => TRUE
1736       );
1737     }
1738   }
1739   return $ret;
1740 }
1741
1742 function _update_178_url_fix($text) {
1743   // Key is the attribute to replace.
1744   $urlpatterns['href'] = "/<a[^>]+href=\"([^\"]+)/i";
1745   $urlpatterns['src']  = "/<img[^>]+src=\"([^\"]+)/i";
1746
1747   $old = $text;
1748   foreach ($urlpatterns as $type => $pattern) {
1749     if (preg_match_all($pattern, $text, $matches)) {
1750       foreach ($matches[1] as $url) {
1751         if ($url != '' && !strstr($url, 'mailto:') && !strstr($url, '://') && !strstr($url, '../') && !strstr($url, './') && $url[0] != '/' && $url[0] != '#') {
1752           $text = preg_replace('|'. $type .'\s*=\s*"'. preg_quote($url) .'\s*"|', $type. '="'.base_path(). $url  .'"', $text);
1753         }
1754       }
1755     }
1756   }
1757   return $text != $old ? $text : FALSE;
1758 }
1759
1760 function _update_178_url_formats() {
1761   $formats = array();
1762
1763   // Any format with the HTML filter in it
1764   $result = db_query("SELECT format FROM {filters} WHERE module = 'filter' AND delta = 0");
1765   while ($format = db_fetch_object($result)) {
1766     $formats[$format->format] = true;
1767   }
1768
1769   // Any format with only the linebreak filter in it
1770   $result = db_query("SELECT format FROM {filters} WHERE module = 'filter' AND delta = 2");
1771   while ($format = db_fetch_object($result)) {
1772     if (db_result(db_query('SELECT COUNT(*) FROM {filters} WHERE format = %d', $format->format)) == 1) {
1773       $formats[$format->format] = true;
1774     }
1775   }
1776
1777   // Any format with 'HTML' in its name
1778   $result = db_query("SELECT format FROM {filter_formats} WHERE name LIKE '%HTML%'");
1779   while ($format = db_fetch_object($result)) {
1780     $formats[$format->format] = true;
1781   }
1782
1783   return $formats;
1784 }
1785
1786 /**
1787  * Update base paths for relative URLs in node and comment content.
1788  */
1789 function system_update_178() {
1790
1791   if (variable_get('clean_url', 0) == 1) {
1792     // Multi-part update
1793     if (!isset($_SESSION['system_update_178_comment'])) {
1794       // Check which formats need to be converted
1795       $formats = _update_178_url_formats();
1796       if (count($formats) == 0) {
1797         return array();
1798       }
1799
1800       // Build format query string
1801       $_SESSION['formats'] = array_keys($formats);
1802       $_SESSION['format_string'] = '('. substr(str_repeat('%d, ', count($formats)), 0, -2) .')';
1803
1804       // Begin update
1805       $_SESSION['system_update_178_comment'] = 0;
1806       $_SESSION['system_update_178_node'] = 0;
1807       $_SESSION['system_update_178_comment_max'] = db_result(db_query('SELECT MAX(cid) FROM {comments} WHERE format IN '. $_SESSION['format_string'], $_SESSION['formats']));
1808       $_SESSION['system_update_178_node_max'] = db_result(db_query('SELECT MAX(vid) FROM {node_revisions} WHERE format IN '. $_SESSION['format_string'], $_SESSION['formats']));
1809     }
1810
1811     $limit = 20;
1812
1813     // Comments
1814     if ($_SESSION['system_update_178_comment'] != $_SESSION['system_update_178_comment_max']) {
1815       $args = array_merge(array($_SESSION['system_update_178_comment']), $_SESSION['formats']);
1816       $result = db_query_range("SELECT cid, comment FROM {comments} WHERE cid > %d AND format IN ". $_SESSION['format_string'] .' ORDER BY cid ASC', $args, 0, $limit);
1817       while ($comment = db_fetch_object($result)) {
1818         $_SESSION['system_update_178_comment'] = $comment->cid;
1819         $comment->comment = _update_178_url_fix($comment->comment);
1820         if ($comment->comment !== FALSE) {
1821           db_query("UPDATE {comments} SET comment = '%s' WHERE cid = %d", $comment->comment, $comment->cid);
1822         }
1823       }
1824     }
1825
1826     // Node revisions
1827     $args = array_merge(array($_SESSION['system_update_178_node']), $_SESSION['formats']);
1828     $result = db_query_range("SELECT vid, teaser, body FROM {node_revisions} WHERE vid > %d AND format IN ". $_SESSION['format_string'] .' ORDER BY vid ASC', $args, 0, $limit);
1829     while ($node = db_fetch_object($result)) {
1830       $_SESSION['system_update_178_node'] = $node->vid;
1831       $set = array();
1832       $args = array();
1833
1834       $node->teaser = _update_178_url_fix($node->teaser);
1835       if ($node->teaser !== FALSE) {
1836         $set[] = "teaser = '%s'";
1837         $args[] = $node->teaser;
1838       }
1839
1840       $node->body = _update_178_url_fix($node->body);
1841       if ($node->body !== FALSE) {
1842         $set[] = "body = '%s'";
1843         $args[] = $node->body;
1844       }
1845
1846       if (count($set)) {
1847         $args[] = $node->vid;
1848         db_query('UPDATE {node_revisions} SET '. implode(', ', $set) .' WHERE vid = %d', $args);
1849       }
1850
1851     }
1852
1853     if ($_SESSION['system_update_178_comment'] == $_SESSION['system_update_178_comment_max'] &&
1854         $_SESSION['system_update_178_node'] == $_SESSION['system_update_178_node_max']) {
1855       unset($_SESSION['system_update_178_comment']);
1856       unset($_SESSION['system_update_178_comment_max']);
1857       unset($_SESSION['system_update_178_node']);
1858       unset($_SESSION['system_update_178_node_max']);
1859       return array();
1860     }
1861     else {
1862       // Report percentage finished
1863       return array('#finished' =>
1864         ($_SESSION['system_update_178_comment'] + $_SESSION['system_update_178_node']) /
1865         ($_SESSION['system_update_178_comment_max'] + $_SESSION['system_update_178_node_max'])
1866       );
1867     }
1868   }
1869
1870   return array();
1871 }
1872
1873 /**
1874  * Update base paths for relative URLs in custom blocks, profiles and various variables.
1875  */
1876 function system_update_179() {
1877
1878   if (variable_get('clean_url', 0) == 1) {
1879     // Multi-part update
1880     if (!isset($_SESSION['system_update_179_uid'])) {
1881       // Check which formats need to be converted
1882       $formats = _update_178_url_formats();
1883       if (count($formats) == 0) {
1884         return array();
1885       }
1886
1887       // Custom Blocks (too small for multipart)
1888       $format_string = '('. substr(str_repeat('%d, ', count($formats)), 0, -2) .')';
1889       $result = db_query("SELECT bid, body FROM {boxes} WHERE format IN ". $format_string, array_keys($formats));
1890       while ($block = db_fetch_object($result)) {
1891         $block->body = _update_178_url_fix($block->body);
1892         if ($block->body !== FALSE) {
1893           db_query("UPDATE {boxes} SET body = '%s' WHERE bid = %d", $block->body, $block->bid);
1894         }
1895       }
1896
1897       // Variables (too small for multipart)
1898       $vars = array('site_mission', 'site_footer', 'user_registration_help');
1899       foreach (node_get_types() as $type => $name) {
1900         $vars[] = $type .'_help';
1901       }
1902       foreach ($vars as $var) {
1903         $value = variable_get($var, NULL);
1904         if (!is_null($value)) {
1905           $value = _update_178_url_fix($value);
1906           if ($value !== FALSE) {
1907             variable_set($var, $value);
1908           }
1909         }
1910       }
1911
1912       // See if profiles need to be updated: is the default format HTML?
1913       if (!isset($formats[variable_get('filter_default_format', 1)])) {
1914         return array();
1915       }
1916       $result = db_query("SELECT fid FROM {profile_fields} WHERE type = 'textarea'");
1917       $fields = array();
1918       while ($field = db_fetch_object($result)) {
1919         $fields[] = $field->fid;
1920       }
1921       if (count($fields) == 0) {
1922         return array();
1923       }
1924
1925       // Begin multi-part update for profiles
1926       $_SESSION['system_update_179_fields'] = $fields;
1927       $_SESSION['system_update_179_field_string'] = '('. substr(str_repeat('%d, ', count($fields)), 0, -2) .')';
1928       $_SESSION['system_update_179_uid'] = 0;
1929       $_SESSION['system_update_179_fid'] = 0;
1930       $_SESSION['system_update_179_max'] = db_result(db_query('SELECT MAX(uid) FROM {profile_values} WHERE fid IN '. $_SESSION['system_update_179_field_string'], $_SESSION['system_update_179_fields']));
1931     }
1932
1933     // Fetch next 20 profile values to convert
1934     $limit = 20;
1935     $args = array_merge(array($_SESSION['system_update_179_uid'], $_SESSION['system_update_179_fid'], $_SESSION['system_update_179_uid']), $_SESSION['system_update_179_fields']);
1936     $result = db_query_range("SELECT fid, uid, value FROM {profile_values} WHERE ((uid = %d AND fid > %d) OR uid > %d) AND fid IN ". $_SESSION['system_update_179_field_string'] .' ORDER BY uid ASC, fid ASC', $args, 0, $limit);
1937     while ($field = db_fetch_object($result)) {
1938       $_SESSION['system_update_179_uid'] = $field->uid;
1939       $_SESSION['system_update_179_fid'] = $field->fid;
1940       $field->value = _update_178_url_fix($field->value);
1941       if ($field->value !== FALSE) {
1942         db_query("UPDATE {profile_values} SET value = '%s' WHERE uid = %d AND fid = %d", $field->value, $field->uid, $field->fid);
1943       }
1944
1945     }
1946
1947     // Done?
1948     if (db_num_rows($result) == 0) {
1949       unset($_SESSION['system_update_179_uid']);
1950       unset($_SESSION['system_update_179_fid']);
1951       unset($_SESSION['system_update_179_max']);
1952       return array();
1953     }
1954     else {
1955       // Report percentage finished
1956       // (Note: make sure we complete all fields for the last user by not reporting 100% too early)
1957       return array('#finished' => $_SESSION['system_update_179_uid'] / ($_SESSION['system_update_179_max'] + 1));
1958     }
1959   }
1960
1961   return array();
1962 }
1963
1964 function system_update_180() {
1965   $ret = array();
1966
1967   switch ($GLOBALS['db_type']) {
1968     case 'mysql':
1969     case 'mysqli':
1970       $ret[] = update_sql("ALTER TABLE {node} DROP PRIMARY KEY");
1971       $ret[] = update_sql("ALTER TABLE {node} ADD PRIMARY KEY (nid, vid)");
1972       $ret[] = update_sql("ALTER TABLE {node} ADD UNIQUE (vid)");
1973       $ret[] = update_sql("ALTER TABLE {node} ADD INDEX (nid)");
1974
1975       $ret[] = update_sql("ALTER TABLE {node_counter} CHANGE nid nid INT(10) NOT NULL DEFAULT '0'");
1976       break;
1977     case 'pgsql':
1978       $ret[] = update_sql("ALTER TABLE {node} DROP CONSTRAINT {node}_pkey"); // Change PK
1979       $ret[] = update_sql("ALTER TABLE {node} ADD PRIMARY KEY (nid, vid)");
1980       $ret[] = update_sql('DROP INDEX {node}_vid_idx'); // Change normal index to UNIQUE index
1981       $ret[] = update_sql('CREATE UNIQUE INDEX {node}_vid_idx ON {node}(vid)');
1982       $ret[] = update_sql('CREATE INDEX {node}_nid_idx ON {node}(nid)'); // Add index on nid
1983       break;
1984   }
1985
1986   return $ret;
1987 }
1988
1989 function system_update_181() {
1990   $ret = array();
1991   switch ($GLOBALS['db_type']) {
1992     case 'mysql':
1993     case 'mysqli':
1994       $ret[] = update_sql("ALTER TABLE {profile_fields} ADD autocomplete TINYINT(1) NOT NULL AFTER visibility ;");
1995       break;
1996     case 'pgsql':
1997       db_add_column($ret, 'profile_fields', 'autocomplete', 'smallint', array('not null' => TRUE, 'default' => 0));
1998       break;
1999   }
2000   return $ret;
2001 }
2002
2003 /**
2004  * The lid field in pgSQL should not be UNIQUE, but an INDEX.
2005  */
2006 function system_update_182() {
2007   $ret = array();
2008
2009   if ($GLOBALS['db_type'] == 'pgsql') {
2010     $ret[] = update_sql('ALTER TABLE {locales_target} DROP CONSTRAINT {locales_target}_lid_key');
2011     $ret[] = update_sql('CREATE INDEX {locales_target}_lid_idx ON {locales_target} (lid)');
2012   }
2013
2014   return $ret;
2015 }