formatting
[lhc/web/wiklou.git] / maintenance / updaters.inc
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage Maintenance
5 */
6
7 /** */
8
9 require_once 'convertLinks.inc';
10 require_once 'InitialiseMessages.inc';
11
12 $wgNewTables = array(
13 # table patch file (in maintenance/archives)
14 array( 'linkscc', 'patch-linkscc.sql' ),
15 array( 'hitcounter', 'patch-hitcounter.sql' ),
16 array( 'querycache', 'patch-querycache.sql' ),
17 array( 'objectcache', 'patch-objectcache.sql' ),
18 array( 'categorylinks', 'patch-categorylinks.sql' ),
19 array( 'logging', 'patch-logging.sql' ),
20 array( 'user_rights', 'patch-user_rights.sql' ),
21 array( 'group', 'patch-userlevels.sql' ),
22 );
23
24 $wgNewFields = array(
25 # table field patch file (in maintenance/archives)
26 array( 'ipblocks', 'ipb_id', 'patch-ipblocks.sql' ),
27 array( 'ipblocks', 'ipb_expiry', 'patch-ipb_expiry.sql' ),
28 array( 'recentchanges', 'rc_type', 'patch-rc_type.sql' ),
29 array( 'recentchanges', 'rc_ip', 'patch-rc_ip.sql' ),
30 array( 'recentchanges', 'rc_id', 'patch-rc_id.sql' ),
31 array( 'recentchanges', 'rc_patrolled', 'patch-rc-patrol.sql' ),
32 array( 'user', 'user_real_name', 'patch-user-realname.sql' ),
33 array( 'user', 'user_token', 'patch-user_token.sql' ),
34 array( 'user_rights', 'ur_user', 'patch-rename-user_groups-and_rights.sql' ),
35 array( 'group', 'group_rights', 'patch-userlevels-rights.sql' ),
36 array( 'logging', 'log_params', 'patch-log_params.sql' ),
37 array( 'archive', 'ar_rev_id', 'patch-archive-rev_id.sql' ),
38 array( 'page', 'page_len', 'patch-page_len.sql' ),
39 array( 'revision', 'rev_deleted', 'patch-rev_deleted.sql' ),
40 );
41
42 function add_table( $name, $patch ) {
43 global $wgDatabase;
44 if ( $wgDatabase->tableExists( $name ) ) {
45 echo "...$name table already exists.\n";
46 } else {
47 echo "Creating $name table...";
48 dbsource( "maintenance/archives/$patch", $wgDatabase );
49 echo "ok\n";
50 }
51 }
52
53 function add_field( $table, $field, $patch ) {
54 global $wgDatabase;
55 if ( !$wgDatabase->tableExists( $table ) ) {
56 echo "...$table table does not exist, skipping new field patch\n";
57 } elseif ( $wgDatabase->fieldExists( $table, $field ) ) {
58 echo "...have $field field in $table table.\n";
59 } else {
60 echo "Adding $field field to table $table...";
61 dbsource( "maintenance/archives/$patch" , $wgDatabase );
62 echo "ok\n";
63 }
64 }
65
66 function do_revision_updates() {
67 global $wgSoftwareRevision;
68 if ( $wgSoftwareRevision < 1001 ) {
69 update_passwords();
70 }
71 }
72
73 function update_passwords() {
74 wfDebugDieBacktrace( "This function needs to be updated or removed.\n" );
75
76 global $wgDatabase;
77 $fname = "Update script: update_passwords()";
78 print "\nIt appears that you need to update the user passwords in your\n" .
79 "database. If you have already done this (if you've run this update\n" .
80 "script once before, for example), doing so again will make all your\n" .
81 "user accounts inaccessible, so be sure you only do this once.\n" .
82 "Update user passwords? (yes/no)";
83
84 $resp = readconsole();
85 if ( ! ( "Y" == $resp{0} || "y" == $resp{0} ) ) { return; }
86
87 $sql = "SELECT user_id,user_password FROM user";
88 $source = $wgDatabase->query( $sql, $fname );
89
90 while ( $row = $wgDatabase->fetchObject( $source ) ) {
91 $id = $row->user_id;
92 $oldpass = $row->user_password;
93 $newpass = md5( "{$id}-{$oldpass}" );
94
95 $sql = "UPDATE user SET user_password='{$newpass}' " .
96 "WHERE user_id={$id}";
97 $wgDatabase->query( $sql, $fname );
98 }
99 }
100
101 function do_interwiki_update() {
102 # Check that interwiki table exists; if it doesn't source it
103 global $wgDatabase;
104 if( $wgDatabase->tableExists( "interwiki" ) ) {
105 echo "...already have interwiki table\n";
106 return true;
107 }
108 echo "Creating interwiki table: ";
109 dbsource( "maintenance/archives/patch-interwiki.sql" );
110 echo "ok\n";
111 echo "Adding default interwiki definitions: ";
112 dbsource( "maintenance/interwiki.sql" );
113 echo "ok\n";
114 }
115
116 function do_index_update() {
117 # Check that proper indexes are in place
118 global $wgDatabase;
119 $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
120 if( $meta->multiple_key == 0 ) {
121 echo "Updating indexes to 20031107: ";
122 dbsource( "maintenance/archives/patch-indexes.sql" );
123 echo "ok\n";
124 return true;
125 }
126 echo "...indexes seem up to 20031107 standards\n";
127 return false;
128 }
129
130 function do_linkscc_1_3_update() {
131 // Update linkscc table to 1.3 schema if necessary
132 global $wgDatabase, $wgVersion;
133 if( $wgDatabase->tableExists( "linkscc" )
134 && $wgDatabase->fieldExists( "linkscc", "lcc_title" ) ) {
135 echo "Altering lcc_title field from linkscc table... ";
136 dbsource( "maintenance/archives/patch-linkscc-1.3.sql", $wgDatabase );
137 echo "ok\n";
138 } else {
139 echo "...linkscc is up to date, or does not exist. Good.\n";
140 }
141 }
142
143 function do_image_name_unique_update() {
144 global $wgDatabase;
145 if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
146 echo "...image primary key already set.\n";
147 } else {
148 echo "Making img_name the primary key... ";
149 dbsource( "maintenance/archives/patch-image_name_primary.sql", $wgDatabase );
150 echo "ok\n";
151 }
152 }
153
154 function do_watchlist_update() {
155 global $wgDatabase;
156 if( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
157 echo "ENOTIF: The watchlist table is already set up for email notification.\n";
158 } else {
159 echo "ENOTIF: Adding wl_notificationtimestamp field for email notification management.";
160 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
161 dbsource( "maintenance/archives/patch-email-notification.sql", $wgDatabase );
162 echo "ok\n";
163 }
164 }
165
166 function do_copy_newtalk_to_watchlist() {
167 global $wgDatabase;
168 global $wgCommandLineMode; # this needs to be saved while getID() and getName() are called
169
170 if ( $wgDatabase->tableExists( 'user_newtalk' ) ) {
171 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
172 $wgDatabase->tableName( 'user_newtalk' ) );
173 $num_newtalks=$wgDatabase->numRows($res);
174 echo "ENOTIF: Now converting ".$num_newtalks." user_newtalk entries to watchlist table entries ... \n";
175
176 $user = new User();
177 for ( $i = 1; $i <= $num_newtalks; $i++ ) {
178 $wluser = $wgDatabase->fetchObject( $res );
179 echo 'ENOTIF: <= user_newtalk: user_id='.$wluser->user_id.' user_ip='.$wluser->user_ip."\n";
180 if ($wluser->user_id == 0) { # anonymous users ... have IP numbers as "names"
181 if ($user->isIP($wluser->user_ip)) { # do only if it really looks like an IP number (double checked)
182 $wgDatabase->replace( 'watchlist',
183 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
184 array('wl_user' => 0,
185 'wl_namespace' => NS_USER_TALK,
186 'wl_title' => $wluser->user_ip,
187 'wl_notificationtimestamp' => '19700101000000'
188 ), 'updaters.inc::do_watchlist_update2'
189 );
190 echo 'ENOTIF: ====> watchlist: user_id=0 '.$wluser->user_ip."\n";
191 }
192 } else { # normal users ... have user_ids
193 $user->setID($wluser->user_id);
194 $wgDatabase->replace( 'watchlist',
195 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
196 array('wl_user' => $user->getID(),
197 'wl_namespace' => NS_USER_TALK,
198 'wl_title' => $user->getName(),
199 'wl_notificationtimestamp' => '19700101000000'
200 ), 'updaters.inc::do_watchlist_update3'
201 );
202 echo 'ENOTIF: ====> watchlist: user_id='.$user->getID().' '.$user->getName()."\n";
203 }
204 }
205 echo "ENOTIF: The watchlist table has got the former user_newtalk entries.\n";
206 dbsource( "maintenance/archives/patch-drop-user_newtalk.sql", $wgDatabase );
207 echo "ENOTIF: Deleting the user_newtalk table as its entries are now in the watchlist table.\n";
208 } else {
209 echo "ENOTIF: No user_newtalk table found. Nothing to convert to watchlist table entries.\n";
210 }
211 }
212
213
214 function do_user_update() {
215 global $wgDatabase;
216 if( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
217 echo "EAUTHENT: The user table is already set up for email authentication.\n";
218 } else {
219 echo "EAUTHENT: Adding user_emailauthenticationtimestamp field for email authentication management.";
220 /* ALTER TABLE user ADD (user_emailauthenticationtimestamp varchar(14) binary NOT NULL default '0'); */
221 dbsource( "maintenance/archives/patch-email-authentication.sql", $wgDatabase );
222 echo "ok\n";
223 }
224 }
225
226 # Assumes that the group table has been added.
227 function do_group_update() {
228 global $wgDatabase;
229 $res = $wgDatabase->safeQuery( 'SELECT COUNT(*) AS c FROM !',
230 $wgDatabase->tableName( 'group' ) );
231 $row = $wgDatabase->fetchObject( $res );
232 $wgDatabase->freeResult( $res );
233 if( $row->c == 0 ) {
234 echo "Adding default group definitions... ";
235 dbsource( "maintenance/archives/patch-userlevels-defaultgroups.sql", $wgDatabase );
236 echo "ok\n";
237 } else {
238 echo "...group definitions already in place.\n";
239 $res = $wgDatabase->safeQuery( "SELECT COUNT(*) AS n FROM !
240 WHERE group_name IN ('Sysops','Bureaucrat')
241 AND group_rights NOT LIKE 'sysop'",
242 $wgDatabase->tableName( 'group' ) );
243 $row = $wgDatabase->fetchObject( $res );
244 $wgDatabase->freeResult( $res );
245 if( $row->n ) {
246 echo "Fixing sysops group permissions and add group editing right... ";
247 dbsource( "maintenance/archives/patch-group-sysopfix.sql", $wgDatabase );
248 echo "ok\n";
249 } else {
250 echo "...sysop group permissions look ok.\n";
251 }
252 }
253 }
254
255 /**
256 * 1.4 betas were missing the 'binary' marker from logging.log_title,
257 * which causes a collation mismatch error on joins in MySQL 4.1.
258 */
259 function do_logging_encoding() {
260 global $wgDatabase;
261 $logging = $wgDatabase->tableName( 'logging' );
262 $res = $wgDatabase->query( "SELECT log_title FROM $logging LIMIT 0" );
263 $flags = explode( ' ', mysql_field_flags( $res, 0 ) );
264 $wgDatabase->freeResult( $res );
265
266 if( in_array( 'binary', $flags ) ) {
267 echo "Logging table has correct title encoding.\n";
268 } else {
269 echo "Fixing title encoding on logging table... ";
270 dbsource( 'maintenance/archives/patch-logging-title.sql', $wgDatabase );
271 echo "ok\n";
272 }
273 }
274
275 function do_schema_restructuring() {
276 global $wgDatabase;
277 $fname="do_schema_restructuring";
278 if ( $wgDatabase->tableExists( 'page' ) ) {
279 echo "...page table already exists.\n";
280 } else {
281 echo "...converting from cur/old to page/revision/text DB structure.\n"; flush();
282 echo "......checking for duplicate entries.\n"; flush();
283
284 extract( $wgDatabase->tableNames( 'cur', 'old', 'page', 'revision', 'text' ) );
285
286 $rows = $wgDatabase->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
287 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", $fname );
288
289 if ( $wgDatabase->numRows( $rows ) > 0 ) {
290 echo "......<b>Found duplicate entries</b>\n";
291 echo ( sprintf( "<b> %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
292 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
293 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
294 $duplicate[$row->cur_namespace] = array();
295 }
296 $duplicate[$row->cur_namespace][] = $row->cur_title;
297 echo ( sprintf( " %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
298 }
299 $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
300 $firstCond = true;
301 foreach ( $duplicate as $ns => $titles ) {
302 if ( $firstCond ) {
303 $firstCond = false;
304 } else {
305 $sql .= ' OR ';
306 }
307 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
308 $first = true;
309 foreach ( $titles as $t ) {
310 if ( $first ) {
311 $sql .= $wgDatabase->addQuotes( $t );
312 $first = false;
313 } else {
314 $sql .= ', ' . $wgDatabase->addQuotes( $t );
315 }
316 }
317 $sql .= ") ) \n";
318 }
319 # By sorting descending, the most recent entry will be the first in the list.
320 # All following entries will be deleted by the next while-loop.
321 $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
322
323 $rows = $wgDatabase->query( $sql, $fname );
324
325 $prev_title = $prev_namespace = false;
326 $deleteId = array();
327
328 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
329 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
330 $deleteId[] = $row->cur_id;
331 }
332 $prev_title = $row->cur_title;
333 $prev_namespace = $row->cur_namespace;
334 }
335 $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
336 $rows = $wgDatabase->query( $sql, $fname );
337 echo "......<b>Deleted</b> ".$wgDatabase->affectedRows()." records.\n";
338 }
339
340
341 echo "......Creating tables.\n";
342 $wgDatabase->query(" CREATE TABLE $page (
343 page_id int(8) unsigned NOT NULL auto_increment,
344 page_namespace tinyint NOT NULL,
345 page_title varchar(255) binary NOT NULL,
346 page_restrictions tinyblob NOT NULL default '',
347 page_counter bigint(20) unsigned NOT NULL default '0',
348 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
349 page_is_new tinyint(1) unsigned NOT NULL default '0',
350 page_random real unsigned NOT NULL,
351 page_touched char(14) binary NOT NULL default '',
352 page_latest int(8) unsigned NOT NULL,
353 page_len int(8) unsigned NOT NULL,
354
355 PRIMARY KEY page_id (page_id),
356 UNIQUE INDEX name_title (page_namespace,page_title),
357 INDEX (page_random),
358 INDEX (page_len)
359 )", $fname );
360 $wgDatabase->query("CREATE TABLE $revision (
361 rev_id int(8) unsigned NOT NULL auto_increment,
362 rev_page int(8) unsigned NOT NULL,
363 rev_comment tinyblob NOT NULL default '',
364 rev_user int(5) unsigned NOT NULL default '0',
365 rev_user_text varchar(255) binary NOT NULL default '',
366 rev_timestamp char(14) binary NOT NULL default '',
367 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
368
369 PRIMARY KEY rev_page_id (rev_page, rev_id),
370 UNIQUE INDEX rev_id (rev_id),
371 INDEX rev_timestamp (rev_timestamp),
372 INDEX page_timestamp (rev_page,rev_timestamp),
373 INDEX user_timestamp (rev_user,rev_timestamp),
374 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
375 )", $fname );
376
377 echo "......Locking tables.\n";
378 $wgDatabase->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", $fname );
379
380 $maxold = $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname );
381 echo "......maxold is {$maxold}\n";
382
383 echo "......Moving text from cur.\n";
384 $wgDatabase->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
385 old_timestamp, old_minor_edit, old_flags)
386 SELECT cur_namespace, cur_title, cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit,''
387 FROM $cur", $fname );
388
389 echo "......Setting up revision table.\n";
390 $wgDatabase->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
391 rev_minor_edit)
392 SELECT old_id, cur_id, old_comment, old_user, old_user_text,
393 old_timestamp, old_minor_edit
394 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", $fname );
395
396 echo "......Setting up page table.\n";
397 $wgDatabase->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
398 page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
399 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
400 cur_random, cur_touched, rev_id, LENGTH(cur_text)
401 FROM $cur,$revision
402 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
403
404 echo "......Unlocking tables.\n";
405 $wgDatabase->query( "UNLOCK TABLES", $fname );
406
407 echo "......Renaming old.\n";
408 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
409 echo "...done.\n";
410 }
411 }
412
413 function do_inverse_timestamp() {
414 global $wgDatabase;
415 $fname="do_schema_restructuring";
416 if( $wgDatabase->fieldExists( 'revision', 'inverse_timestamp' ) ) {
417 echo "Removing revision.inverse_timestamp and fixing indexes... ";
418 dbsource( 'maintenance/archives/patch-inverse_timestamp.sql', $wgDatabase );
419 echo "ok\n";
420 } else {
421 echo "revision timestamp indexes already up to 2005-03-13\n";
422 }
423 }
424
425 function do_text_id() {
426 global $wgDatabase;
427 if( $wgDatabase->fieldExists( 'revision', 'rev_text_id' ) ) {
428 echo "...rev_text_id already in place.\n";
429 } else {
430 echo "Adding rev_text_id field... ";
431 dbsource( 'maintenance/archives/patch-rev_text_id.sql', $wgDatabase );
432 echo "ok\n";
433 }
434 }
435
436
437 function do_all_updates() {
438 global $wgNewTables, $wgNewFields;
439
440 # Add missing tables
441 foreach ( $wgNewTables as $tableRecord ) {
442 add_table( $tableRecord[0], $tableRecord[1] );
443 flush();
444 }
445
446 # Add missing fields
447 foreach ( $wgNewFields as $fieldRecord ) {
448 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2] );
449 flush();
450 }
451
452 # Add default group data
453 do_group_update(); flush();
454
455 # Do schema updates which require special handling
456 do_interwiki_update(); flush();
457 do_index_update(); flush();
458 do_linkscc_1_3_update(); flush();
459 convertLinks(); flush();
460 do_image_name_unique_update(); flush();
461 do_watchlist_update(); flush();
462 do_user_update(); flush();
463 do_copy_newtalk_to_watchlist(); flush();
464 do_logging_encoding(); flush();
465
466 do_schema_restructuring(); flush();
467 do_inverse_timestamp(); flush();
468 do_text_id(); flush();
469
470 initialiseMessages(); flush();
471 }
472
473 ?>