4 * @subpackage Maintenance
9 require_once 'convertLinks.inc';
10 require_once 'InitialiseMessages.inc';
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' ),
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', 'user_email_token', 'patch-user_email_token.sql' ),
35 array( 'user_rights', 'ur_user', 'patch-rename-user_groups-and_rights.sql' ),
36 array( 'group', 'group_rights', 'patch-userlevels-rights.sql' ),
37 array( 'logging', 'log_params', 'patch-log_params.sql' ),
38 array( 'archive', 'ar_rev_id', 'patch-archive-rev_id.sql' ),
39 array( 'page', 'page_len', 'patch-page_len.sql' ),
40 array( 'revision', 'rev_deleted', 'patch-rev_deleted.sql' ),
41 array( 'image', 'img_width', 'patch-img_width.sql' ),
42 array( 'image', 'img_metadata', 'patch-img_metadata.sql' ),
45 function add_table( $name, $patch ) {
47 if ( $wgDatabase->tableExists( $name ) ) {
48 echo "...$name table already exists.\n";
50 echo "Creating $name table...";
51 dbsource( "maintenance/archives/$patch", $wgDatabase );
56 function add_field( $table, $field, $patch ) {
58 if ( !$wgDatabase->tableExists( $table ) ) {
59 echo "...$table table does not exist, skipping new field patch\n";
60 } elseif ( $wgDatabase->fieldExists( $table, $field ) ) {
61 echo "...have $field field in $table table.\n";
63 echo "Adding $field field to table $table...";
64 dbsource( "maintenance/archives/$patch" , $wgDatabase );
69 function do_revision_updates() {
70 global $wgSoftwareRevision;
71 if ( $wgSoftwareRevision < 1001 ) {
76 function update_passwords() {
77 wfDebugDieBacktrace( "This function needs to be updated or removed.\n" );
80 $fname = "Update script: update_passwords()";
81 print "\nIt appears that you need to update the user passwords in your\n" .
82 "database. If you have already done this (if you've run this update\n" .
83 "script once before, for example), doing so again will make all your\n" .
84 "user accounts inaccessible, so be sure you only do this once.\n" .
85 "Update user passwords? (yes/no)";
87 $resp = readconsole();
88 if ( ! ( "Y" == $resp{0} || "y" == $resp{0} ) ) { return; }
90 $sql = "SELECT user_id,user_password FROM user";
91 $source = $wgDatabase->query( $sql, $fname );
93 while ( $row = $wgDatabase->fetchObject( $source ) ) {
95 $oldpass = $row->user_password;
96 $newpass = md5( "{$id}-{$oldpass}" );
98 $sql = "UPDATE user SET user_password='{$newpass}' " .
99 "WHERE user_id={$id}";
100 $wgDatabase->query( $sql, $fname );
104 function do_interwiki_update() {
105 # Check that interwiki table exists; if it doesn't source it
107 if( $wgDatabase->tableExists( "interwiki" ) ) {
108 echo "...already have interwiki table\n";
111 echo "Creating interwiki table: ";
112 dbsource( "maintenance/archives/patch-interwiki.sql" );
114 echo "Adding default interwiki definitions: ";
115 dbsource( "maintenance/interwiki.sql" );
119 function do_index_update() {
120 # Check that proper indexes are in place
122 $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
123 if( $meta->multiple_key == 0 ) {
124 echo "Updating indexes to 20031107: ";
125 dbsource( "maintenance/archives/patch-indexes.sql" );
129 echo "...indexes seem up to 20031107 standards\n";
133 function do_linkscc_1_3_update() {
134 // Update linkscc table to 1.3 schema if necessary
135 global $wgDatabase, $wgVersion;
136 if( $wgDatabase->tableExists( "linkscc" )
137 && $wgDatabase->fieldExists( "linkscc", "lcc_title" ) ) {
138 echo "Altering lcc_title field from linkscc table... ";
139 dbsource( "maintenance/archives/patch-linkscc-1.3.sql", $wgDatabase );
142 echo "...linkscc is up to date, or does not exist. Good.\n";
146 function do_image_name_unique_update() {
148 if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
149 echo "...image primary key already set.\n";
151 echo "Making img_name the primary key... ";
152 dbsource( "maintenance/archives/patch-image_name_primary.sql", $wgDatabase );
157 function do_watchlist_update() {
159 if( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
160 echo "ENOTIF: The watchlist table is already set up for email notification.\n";
162 echo "ENOTIF: Adding wl_notificationtimestamp field for email notification management.";
163 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
164 dbsource( "maintenance/archives/patch-email-notification.sql", $wgDatabase );
169 function do_copy_newtalk_to_watchlist() {
171 global $wgCommandLineMode; # this needs to be saved while getID() and getName() are called
173 if ( $wgDatabase->tableExists( 'user_newtalk' ) ) {
174 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
175 $wgDatabase->tableName( 'user_newtalk' ) );
176 $num_newtalks=$wgDatabase->numRows($res);
177 echo "ENOTIF: Now converting ".$num_newtalks." user_newtalk entries to watchlist table entries ... \n";
180 for ( $i = 1; $i <= $num_newtalks; $i++ ) {
181 $wluser = $wgDatabase->fetchObject( $res );
182 echo 'ENOTIF: <= user_newtalk: user_id='.$wluser->user_id.' user_ip='.$wluser->user_ip."\n";
183 if ($wluser->user_id == 0) { # anonymous users ... have IP numbers as "names"
184 if ($user->isIP($wluser->user_ip)) { # do only if it really looks like an IP number (double checked)
185 $wgDatabase->replace( 'watchlist',
186 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
187 array('wl_user' => 0,
188 'wl_namespace' => NS_USER_TALK,
189 'wl_title' => $wluser->user_ip,
190 'wl_notificationtimestamp' => '19700101000000'
191 ), 'updaters.inc::do_watchlist_update2'
193 echo 'ENOTIF: ====> watchlist: user_id=0 '.$wluser->user_ip."\n";
195 } else { # normal users ... have user_ids
196 $user->setID($wluser->user_id);
197 $wgDatabase->replace( 'watchlist',
198 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
199 array('wl_user' => $user->getID(),
200 'wl_namespace' => NS_USER_TALK,
201 'wl_title' => $user->getName(),
202 'wl_notificationtimestamp' => '19700101000000'
203 ), 'updaters.inc::do_watchlist_update3'
205 echo 'ENOTIF: ====> watchlist: user_id='.$user->getID().' '.$user->getName()."\n";
208 echo "ENOTIF: The watchlist table has got the former user_newtalk entries.\n";
209 dbsource( "maintenance/archives/patch-drop-user_newtalk.sql", $wgDatabase );
210 echo "ENOTIF: Deleting the user_newtalk table as its entries are now in the watchlist table.\n";
212 echo "ENOTIF: No user_newtalk table found. Nothing to convert to watchlist table entries.\n";
217 function do_user_update() {
219 if( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
220 echo "User table contains old email authentication field. Dropping... ";
221 dbsource( "maintenance/archives/patch-email-authentication.sql", $wgDatabase );
224 echo "...user table does not contain old email authentication field.\n";
228 # Assumes that the group table has been added.
229 function do_group_update() {
231 $res = $wgDatabase->safeQuery( 'SELECT COUNT(*) AS c FROM !',
232 $wgDatabase->tableName( 'group' ) );
233 $row = $wgDatabase->fetchObject( $res );
234 $wgDatabase->freeResult( $res );
236 echo "Adding default group definitions... ";
237 dbsource( "maintenance/archives/patch-userlevels-defaultgroups.sql", $wgDatabase );
240 echo "...group definitions already in place.\n";
241 $res = $wgDatabase->safeQuery( "SELECT COUNT(*) AS n FROM !
242 WHERE group_name IN ('Sysops','Bureaucrat')
243 AND group_rights NOT LIKE '%sysop%'",
244 $wgDatabase->tableName( 'group' ) );
245 $row = $wgDatabase->fetchObject( $res );
246 $wgDatabase->freeResult( $res );
248 echo "Fixing sysops group permissions and add group editing right... ";
249 dbsource( "maintenance/archives/patch-group-sysopfix.sql", $wgDatabase );
252 echo "...sysop group permissions look ok.\n";
258 * 1.4 betas were missing the 'binary' marker from logging.log_title,
259 * which causes a collation mismatch error on joins in MySQL 4.1.
261 function do_logging_encoding() {
263 $logging = $wgDatabase->tableName( 'logging' );
264 $res = $wgDatabase->query( "SELECT log_title FROM $logging LIMIT 0" );
265 $flags = explode( ' ', mysql_field_flags( $res, 0 ) );
266 $wgDatabase->freeResult( $res );
268 if( in_array( 'binary', $flags ) ) {
269 echo "Logging table has correct title encoding.\n";
271 echo "Fixing title encoding on logging table... ";
272 dbsource( 'maintenance/archives/patch-logging-title.sql', $wgDatabase );
277 function do_schema_restructuring() {
279 $fname="do_schema_restructuring";
280 if ( $wgDatabase->tableExists( 'page' ) ) {
281 echo "...page table already exists.\n";
283 echo "...converting from cur/old to page/revision/text DB structure.\n"; flush();
284 echo "......checking for duplicate entries.\n"; flush();
286 extract( $wgDatabase->tableNames( 'cur', 'old', 'page', 'revision', 'text' ) );
288 $rows = $wgDatabase->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
289 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", $fname );
291 if ( $wgDatabase->numRows( $rows ) > 0 ) {
292 echo "......<b>Found duplicate entries</b>\n";
293 echo ( sprintf( "<b> %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
294 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
295 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
296 $duplicate[$row->cur_namespace] = array();
298 $duplicate[$row->cur_namespace][] = $row->cur_title;
299 echo ( sprintf( " %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
301 $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
303 foreach ( $duplicate as $ns => $titles ) {
309 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
311 foreach ( $titles as $t ) {
313 $sql .= $wgDatabase->addQuotes( $t );
316 $sql .= ', ' . $wgDatabase->addQuotes( $t );
321 # By sorting descending, the most recent entry will be the first in the list.
322 # All following entries will be deleted by the next while-loop.
323 $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
325 $rows = $wgDatabase->query( $sql, $fname );
327 $prev_title = $prev_namespace = false;
330 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
331 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
332 $deleteId[] = $row->cur_id;
334 $prev_title = $row->cur_title;
335 $prev_namespace = $row->cur_namespace;
337 $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
338 $rows = $wgDatabase->query( $sql, $fname );
339 echo "......<b>Deleted</b> ".$wgDatabase->affectedRows()." records.\n";
343 echo "......Creating tables.\n";
344 $wgDatabase->query(" CREATE TABLE $page (
345 page_id int(8) unsigned NOT NULL auto_increment,
346 page_namespace tinyint NOT NULL,
347 page_title varchar(255) binary NOT NULL,
348 page_restrictions tinyblob NOT NULL default '',
349 page_counter bigint(20) unsigned NOT NULL default '0',
350 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
351 page_is_new tinyint(1) unsigned NOT NULL default '0',
352 page_random real unsigned NOT NULL,
353 page_touched char(14) binary NOT NULL default '',
354 page_latest int(8) unsigned NOT NULL,
355 page_len int(8) unsigned NOT NULL,
357 PRIMARY KEY page_id (page_id),
358 UNIQUE INDEX name_title (page_namespace,page_title),
362 $wgDatabase->query("CREATE TABLE $revision (
363 rev_id int(8) unsigned NOT NULL auto_increment,
364 rev_page int(8) unsigned NOT NULL,
365 rev_comment tinyblob NOT NULL default '',
366 rev_user int(5) unsigned NOT NULL default '0',
367 rev_user_text varchar(255) binary NOT NULL default '',
368 rev_timestamp char(14) binary NOT NULL default '',
369 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
370 rev_deleted tinyint(1) unsigned NOT NULL default '0',
372 PRIMARY KEY rev_page_id (rev_page, rev_id),
373 UNIQUE INDEX rev_id (rev_id),
374 INDEX rev_timestamp (rev_timestamp),
375 INDEX page_timestamp (rev_page,rev_timestamp),
376 INDEX user_timestamp (rev_user,rev_timestamp),
377 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
380 echo "......Locking tables.\n";
381 $wgDatabase->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", $fname );
383 $maxold = $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname );
384 echo "......maxold is {$maxold}\n";
386 echo "......Moving text from cur.\n";
387 $wgDatabase->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
388 old_timestamp, old_minor_edit, old_flags)
389 SELECT cur_namespace, cur_title, cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit,''
390 FROM $cur", $fname );
392 echo "......Setting up revision table.\n";
393 $wgDatabase->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
395 SELECT old_id, cur_id, old_comment, old_user, old_user_text,
396 old_timestamp, old_minor_edit
397 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", $fname );
399 echo "......Setting up page table.\n";
400 $wgDatabase->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
401 page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
402 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
403 cur_random, cur_touched, rev_id, LENGTH(cur_text)
405 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
407 echo "......Unlocking tables.\n";
408 $wgDatabase->query( "UNLOCK TABLES", $fname );
410 echo "......Renaming old.\n";
411 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
416 function do_inverse_timestamp() {
418 $fname="do_schema_restructuring";
419 if( $wgDatabase->fieldExists( 'revision', 'inverse_timestamp' ) ) {
420 echo "Removing revision.inverse_timestamp and fixing indexes... ";
421 dbsource( 'maintenance/archives/patch-inverse_timestamp.sql', $wgDatabase );
424 echo "revision timestamp indexes already up to 2005-03-13\n";
428 function do_text_id() {
430 if( $wgDatabase->fieldExists( 'revision', 'rev_text_id' ) ) {
431 echo "...rev_text_id already in place.\n";
433 echo "Adding rev_text_id field... ";
434 dbsource( 'maintenance/archives/patch-rev_text_id.sql', $wgDatabase );
440 function do_all_updates() {
441 global $wgNewTables, $wgNewFields;
444 foreach ( $wgNewTables as $tableRecord ) {
445 add_table( $tableRecord[0], $tableRecord[1] );
450 foreach ( $wgNewFields as $fieldRecord ) {
451 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2] );
455 # Add default group data
456 do_group_update(); flush();
458 # Do schema updates which require special handling
459 do_interwiki_update(); flush();
460 do_index_update(); flush();
461 do_linkscc_1_3_update(); flush();
462 convertLinks(); flush();
463 do_image_name_unique_update(); flush();
464 do_watchlist_update(); flush();
465 do_user_update(); flush();
466 do_copy_newtalk_to_watchlist(); flush();
467 do_logging_encoding(); flush();
469 do_schema_restructuring(); flush();
470 do_inverse_timestamp(); flush();
471 do_text_id(); flush();
473 initialiseMessages(); flush();