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