4 * @subpackage Maintenance
9 require_once 'convertLinks.inc';
10 require_once 'InitialiseMessages.inc';
11 require_once 'archives/moveCustomMessages.inc';
14 # table patch file (in maintenance/archives)
15 array( 'linkscc', 'patch-linkscc.sql' ),
16 array( 'hitcounter', 'patch-hitcounter.sql' ),
17 array( 'querycache', 'patch-querycache.sql' ),
18 array( 'objectcache', 'patch-objectcache.sql' ),
19 array( 'categorylinks', 'patch-categorylinks.sql' ),
20 array( 'logging', 'patch-logging.sql' ),
21 array( 'user_rights', 'patch-user_rights.sql' ),
22 array( 'user_groups', 'patch-userlevels.sql' ),
26 # table field patch file (in maintenance/archives)
27 array( 'ipblocks', 'ipb_id', 'patch-ipblocks.sql' ),
28 array( 'ipblocks', 'ipb_expiry', 'patch-ipb_expiry.sql' ),
29 array( 'recentchanges', 'rc_type', 'patch-rc_type.sql' ),
30 array( 'recentchanges', 'rc_ip', 'patch-rc_ip.sql' ),
31 array( 'recentchanges', 'rc_id', 'patch-rc_id.sql' ),
32 array( 'recentchanges', 'rc_patrolled', 'patch-rc-patrol.sql' ),
33 array( 'user', 'user_real_name', 'patch-user-realname.sql' ),
34 array( 'user', 'user_token', 'patch-user_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' ),
42 function add_table( $name, $patch ) {
44 if ( $wgDatabase->tableExists( $name ) ) {
45 echo "...$name table already exists.\n";
47 echo "Creating $name table...";
48 dbsource( "maintenance/archives/$patch", $wgDatabase );
53 function add_field( $table, $field, $patch ) {
55 if ( $wgDatabase->fieldExists( $table, $field ) ) {
56 echo "...have $field field in $table table.\n";
58 echo "Adding $field field to table $table...";
59 dbsource( "maintenance/archives/$patch" , $wgDatabase );
64 function do_revision_updates() {
65 global $wgSoftwareRevision;
66 if ( $wgSoftwareRevision < 1001 ) {
71 function update_passwords() {
72 wfDebugDieBacktrace( "This function needs to be updated or removed.\n" );
75 $fname = "Update script: update_passwords()";
76 print "\nIt appears that you need to update the user passwords in your\n" .
77 "database. If you have already done this (if you've run this update\n" .
78 "script once before, for example), doing so again will make all your\n" .
79 "user accounts inaccessible, so be sure you only do this once.\n" .
80 "Update user passwords? (yes/no)";
82 $resp = readconsole();
83 if ( ! ( "Y" == $resp{0} || "y" == $resp{0} ) ) { return; }
85 $sql = "SELECT user_id,user_password FROM user";
86 $source = $wgDatabase->query( $sql, $fname );
88 while ( $row = $wgDatabase->fetchObject( $source ) ) {
90 $oldpass = $row->user_password;
91 $newpass = md5( "{$id}-{$oldpass}" );
93 $sql = "UPDATE user SET user_password='{$newpass}' " .
94 "WHERE user_id={$id}";
95 $wgDatabase->query( $sql, $fname );
99 function do_interwiki_update() {
100 # Check that interwiki table exists; if it doesn't source it
102 if( $wgDatabase->tableExists( "interwiki" ) ) {
103 echo "...already have interwiki table\n";
106 echo "Creating interwiki table: ";
107 dbsource( "maintenance/archives/patch-interwiki.sql" );
109 echo "Adding default interwiki definitions: ";
110 dbsource( "maintenance/interwiki.sql" );
114 function do_index_update() {
115 # Check that proper indexes are in place
117 $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
118 if( $meta->multiple_key == 0 ) {
119 echo "Updating indexes to 20031107: ";
120 dbsource( "maintenance/archives/patch-indexes.sql" );
124 echo "...indexes seem up to 20031107 standards\n";
128 function do_linkscc_1_3_update() {
129 // Update linkscc table to 1.3 schema if necessary
130 global $wgDatabase, $wgVersion;
131 if( $wgDatabase->tableExists( "linkscc" )
132 && $wgDatabase->fieldExists( "linkscc", "lcc_title" ) ) {
133 echo "Altering lcc_title field from linkscc table... ";
134 dbsource( "maintenance/archives/patch-linkscc-1.3.sql", $wgDatabase );
137 echo "...linkscc is up to date, or does not exist. Good.\n";
141 function do_image_name_unique_update() {
143 if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
144 echo "...image primary key already set.\n";
146 echo "Making img_name the primary key... ";
147 dbsource( "maintenance/archives/patch-image_name_primary.sql", $wgDatabase );
152 function do_watchlist_update() {
154 if( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
155 echo "ENOTIF: The watchlist table is already set up for email notification.\n";
157 echo "ENOTIF: Adding wl_notificationtimestamp field for email notification management.";
158 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
159 dbsource( "maintenance/archives/patch-email-notification.sql", $wgDatabase );
164 function do_copy_newtalk_to_watchlist() {
166 global $wgCommandLineMode; # this needs to be saved while getID() and getName() are called
168 if ( $wgDatabase->tableExists( 'user_newtalk' ) ) {
169 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
170 $wgDatabase->tableName( 'user_newtalk' ) );
171 $num_newtalks=$wgDatabase->numRows($res);
172 echo "ENOTIF: Now converting ".$num_newtalks." user_newtalk entries to watchlist table entries ... \n";
175 for ( $i = 1; $i <= $num_newtalks; $i++ ) {
176 $wluser = $wgDatabase->fetchObject( $res );
177 echo 'ENOTIF: <= user_newtalk: user_id='.$wluser->user_id.' user_ip='.$wluser->user_ip."\n";
178 if ($wluser->user_id == 0) { # anonymous users ... have IP numbers as "names"
179 if ($user->isIP($wluser->user_ip)) { # do only if it really looks like an IP number (double checked)
180 $wgDatabase->replace( 'watchlist',
181 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
182 array('wl_user' => 0,
183 'wl_namespace' => NS_USER_TALK,
184 'wl_title' => $wluser->user_ip,
185 'wl_notificationtimestamp' => '19700101000000'
186 ), 'updaters.inc::do_watchlist_update2'
188 echo 'ENOTIF: ====> watchlist: user_id=0 '.$wluser->user_ip."\n";
190 } else { # normal users ... have user_ids
191 $user->setID($wluser->user_id);
192 $wgDatabase->replace( 'watchlist',
193 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
194 array('wl_user' => $user->getID(),
195 'wl_namespace' => NS_USER_TALK,
196 'wl_title' => $user->getName(),
197 'wl_notificationtimestamp' => '19700101000000'
198 ), 'updaters.inc::do_watchlist_update3'
200 echo 'ENOTIF: ====> watchlist: user_id='.$user->getID().' '.$user->getName()."\n";
203 echo "ENOTIF: The watchlist table has got the former user_newtalk entries.\n";
204 dbsource( "maintenance/archives/patch-drop-user_newtalk.sql", $wgDatabase );
205 echo "ENOTIF: Deleting the user_newtalk table as its entries are now in the watchlist table.\n";
207 echo "ENOTIF: No user_newtalk table found. Nothing to convert to watchlist table entries.\n";
212 function do_user_update() {
214 if( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
215 echo "EAUTHENT: The user table is already set up for email authentication.\n";
217 echo "EAUTHENT: Adding user_emailauthenticationtimestamp field for email authentication management.";
218 /* ALTER TABLE user ADD (user_emailauthenticationtimestamp varchar(14) binary NOT NULL default '0'); */
219 dbsource( "maintenance/archives/patch-email-authentication.sql", $wgDatabase );
224 # Assumes that the group table has been added.
225 function do_group_update() {
227 $res = $wgDatabase->safeQuery( 'SELECT COUNT(*) AS c FROM !',
228 $wgDatabase->tableName( 'group' ) );
229 $row = $wgDatabase->fetchObject( $res );
230 $wgDatabase->freeResult( $res );
232 echo "Adding default group definitions... ";
233 dbsource( "maintenance/archives/patch-userlevels-defaultgroups.sql", $wgDatabase );
236 echo "...group definitions already in place.\n";
237 $res = $wgDatabase->safeQuery( "SELECT COUNT(*) AS n FROM !
238 WHERE group_name IN ('Sysops','Bureaucrat')
239 AND group_rights NOT LIKE 'sysop'",
240 $wgDatabase->tableName( 'group' ) );
241 $row = $wgDatabase->fetchObject( $res );
242 $wgDatabase->freeResult( $res );
244 echo "Fixing sysops group permissions and add group editing right... ";
245 dbsource( "maintenance/archives/patch-group-sysopfix.sql", $wgDatabase );
248 echo "...sysop group permissions look ok.\n";
254 * 1.4 betas were missing the 'binary' marker from logging.log_title,
255 * which causes a collation mismatch error on joins in MySQL 4.1.
257 function do_logging_encoding() {
259 $logging = $wgDatabase->tableName( 'logging' );
260 $res = $wgDatabase->query( "SELECT log_title FROM $logging LIMIT 0" );
261 $flags = explode( ' ', mysql_field_flags( $res, 0 ) );
262 $wgDatabase->freeResult( $res );
264 if( in_array( 'binary', $flags ) ) {
265 echo "Logging table has correct title encoding.\n";
267 echo "Fixing title encoding on logging table... ";
268 dbsource( 'maintenance/archives/patch-logging-title.sql', $wgDatabase );
273 function do_schema_restructuring() {
275 $fname="do_schema_restructuring";
276 if ( $wgDatabase->tableExists( 'page' ) ) {
277 echo "...page table already exists.\n";
279 echo "...converting from cur/old to page/revision/text DB structure.\n"; flush();
280 echo "......checking for duplicate entries.\n"; flush();
282 extract( $wgDatabase->tableNames( 'cur', 'old', 'page', 'revision', 'text' ) );
284 $rows = $wgDatabase->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
285 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", $fname );
287 if ( $wgDatabase->numRows( $rows ) > 0 ) {
288 echo "......<b>Found duplicate entries</b>\n";
289 echo ( sprintf( "<b> %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
290 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
291 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
292 $duplicate[$row->cur_namespace] = array();
294 $duplicate[$row->cur_namespace][] = $row->cur_title;
295 echo ( sprintf( " %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
297 $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
299 foreach ( $duplicate as $ns => $titles ) {
305 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
307 foreach ( $titles as $t ) {
309 $sql .= $wgDatabase->addQuotes( $t );
312 $sql .= ', ' . $wgDatabase->addQuotes( $t );
317 # By sorting descending, the most recent entry will be the first in the list.
318 # All following entries will be deleted by the next while-loop.
319 $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
321 $rows = $wgDatabase->query( $sql, $fname );
323 $prev_title = $prev_namespace = false;
326 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
327 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
328 $deleteId[] = $row->cur_id;
330 $prev_title = $row->cur_title;
331 $prev_namespace = $row->cur_namespace;
333 $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
334 $rows = $wgDatabase->query( $sql, $fname );
335 echo "......<b>Deleted</b> ".$wgDatabase->affectedRows()." records.\n";
339 echo "......Creating tables.\n";
340 $wgDatabase->query(" CREATE TABLE $page (
341 page_id int(8) unsigned NOT NULL auto_increment,
342 page_namespace tinyint NOT NULL,
343 page_title varchar(255) binary NOT NULL,
344 page_restrictions tinyblob NOT NULL default '',
345 page_counter bigint(20) unsigned NOT NULL default '0',
346 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
347 page_is_new tinyint(1) unsigned NOT NULL default '0',
348 page_random real unsigned NOT NULL,
349 page_touched char(14) binary NOT NULL default '',
350 page_latest int(8) unsigned NOT NULL,
351 page_len int(8) unsigned NOT NULL,
353 PRIMARY KEY page_id (page_id),
354 UNIQUE INDEX name_title (page_namespace,page_title),
358 $wgDatabase->query("CREATE TABLE $revision (
359 rev_id int(8) unsigned NOT NULL auto_increment,
360 rev_page int(8) unsigned NOT NULL,
361 rev_comment tinyblob NOT NULL default '',
362 rev_user int(5) unsigned NOT NULL default '0',
363 rev_user_text varchar(255) binary NOT NULL default '',
364 rev_timestamp char(14) binary NOT NULL default '',
365 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
367 PRIMARY KEY rev_page_id (rev_page, rev_id),
368 UNIQUE INDEX rev_id (rev_id),
369 INDEX rev_timestamp (rev_timestamp),
370 INDEX page_timestamp (rev_page,rev_timestamp),
371 INDEX user_timestamp (rev_user,rev_timestamp),
372 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
375 echo "......Locking tables.\n";
376 $wgDatabase->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", $fname );
378 $maxold = $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname );
379 echo "......maxold is {$maxold}\n";
381 echo "......Moving text from cur.\n";
382 $wgDatabase->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
383 old_timestamp, old_minor_edit, old_flags)
384 SELECT cur_namespace, cur_title, cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit,''
385 FROM $cur", $fname );
387 echo "......Setting up revision table.\n";
388 $wgDatabase->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
390 SELECT old_id, cur_id, old_comment, old_user, old_user_text,
391 old_timestamp, old_minor_edit
392 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", $fname );
394 echo "......Setting up page table.\n";
395 $wgDatabase->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
396 page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
397 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
398 cur_random, cur_touched, rev_id, LENGTH(cur_text)
400 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
402 echo "......Unlocking tables.\n";
403 $wgDatabase->query( "UNLOCK TABLES", $fname );
405 echo "......Renaming old.\n";
406 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
411 function do_all_updates() {
412 global $wgNewTables, $wgNewFields;
415 foreach ( $wgNewTables as $tableRecord ) {
416 add_table( $tableRecord[0], $tableRecord[1] );
421 foreach ( $wgNewFields as $fieldRecord ) {
422 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2] );
426 # Add default group data
427 do_group_update(); flush();
429 # Do schema updates which require special handling
430 do_interwiki_update(); flush();
431 do_index_update(); flush();
432 do_linkscc_1_3_update(); flush();
433 convertLinks(); flush();
434 do_image_name_unique_update(); flush();
435 do_watchlist_update(); flush();
436 do_user_update(); flush();
437 do_copy_newtalk_to_watchlist(); flush();
438 do_logging_encoding(); flush();
440 do_schema_restructuring(); flush();
442 if ( isTemplateInitialised() ) {
443 print "Template namespace already initialised\n";
445 moveCustomMessages( 1 ); flush();
446 moveCustomMessages( 2 ); flush();
447 moveCustomMessages( 3 ); flush();
450 initialiseMessages(); flush();