added /*$wgDBprefix*/ in various places, to support upgrading from old prefixed datab...
[mediawiki.git] / maintenance / updaters.inc
blob47238c07e03133646fda7507d4c750c28f01a4b3
1 <?php
2 /**
3  * @package MediaWiki
4  * @subpackage Maintenance
5  */
6  
7  /** */
9 require_once 'convertLinks.inc';
10 require_once 'InitialiseMessages.inc';
11 require_once 'archives/moveCustomMessages.inc';
13 $wgNewTables = array(
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' ),
25 $wgNewFields = array(
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' ),
39 function add_table( $name, $patch ) {
40         global $wgDatabase;
41         if ( $wgDatabase->tableExists( $name ) ) {
42                 echo "...$name table already exists.\n";
43         } else {
44                 echo "Creating $name table...";
45                 dbsource( "maintenance/archives/$patch", $wgDatabase );
46                 echo "ok\n";
47         }
50 function add_field( $table, $field, $patch ) {
51         global $wgDatabase;
52         if ( $wgDatabase->fieldExists( $table, $field ) ) {
53                 echo "...have $field field in $table table.\n";
54         } else {
55                 echo "Adding $field field to table $table...";
56                 dbsource( "maintenance/archives/$patch" , $wgDatabase );
57                 echo "ok\n";
58         }
61 function do_revision_updates() {
62         global $wgSoftwareRevision;
63         if ( $wgSoftwareRevision < 1001 ) {
64                 update_passwords();
65         }
68 function update_passwords() {
69         wfDebugDieBacktrace( "This function needs to be updated or removed.\n" );
70         
71         global $wgDatabase;
72         $fname = "Update script: update_passwords()";
73         print "\nIt appears that you need to update the user passwords in your\n" .
74           "database. If you have already done this (if you've run this update\n" .
75           "script once before, for example), doing so again will make all your\n" .
76           "user accounts inaccessible, so be sure you only do this once.\n" .
77           "Update user passwords? (yes/no)";
79         $resp = readconsole();
80     if ( ! ( "Y" == $resp{0} || "y" == $resp{0} ) ) { return; }
82         $sql = "SELECT user_id,user_password FROM user";
83         $source = $wgDatabase->query( $sql, $fname );
85         while ( $row = $wgDatabase->fetchObject( $source ) ) {
86                 $id = $row->user_id;
87                 $oldpass = $row->user_password;
88                 $newpass = md5( "{$id}-{$oldpass}" );
90                 $sql = "UPDATE user SET user_password='{$newpass}' " .
91                   "WHERE user_id={$id}";
92                 $wgDatabase->query( $sql, $fname );
93         }
96 function do_interwiki_update() {
97         # Check that interwiki table exists; if it doesn't source it
98         global $wgDatabase;
99         if( $wgDatabase->tableExists( "interwiki" ) ) {
100                 echo "...already have interwiki table\n";
101                 return true;
102         }
103         echo "Creating interwiki table: ";
104         dbsource( "maintenance/archives/patch-interwiki.sql" );
105         echo "ok\n";
106         echo "Adding default interwiki definitions: ";
107         dbsource( "maintenance/interwiki.sql" );
108         echo "ok\n";
111 function do_index_update() {
112         # Check that proper indexes are in place
113         global $wgDatabase;
114         $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
115         if( $meta->multiple_key == 0 ) {
116                 echo "Updating indexes to 20031107: ";
117                 dbsource( "maintenance/archives/patch-indexes.sql" );
118                 echo "ok\n";
119                 return true;
120         }
121         echo "...indexes seem up to 20031107 standards\n";
122         return false;
125 function do_linkscc_1_3_update() {
126         // Update linkscc table to 1.3 schema if necessary
127         global $wgDatabase, $wgVersion;
128         if( $wgDatabase->tableExists( "linkscc" )
129                 && $wgDatabase->fieldExists( "linkscc", "lcc_title" ) ) {
130                 echo "Altering lcc_title field from linkscc table... ";
131                 dbsource( "maintenance/archives/patch-linkscc-1.3.sql", $wgDatabase );
132                 echo "ok\n";
133         } else {
134                 echo "...linkscc is up to date, or does not exist. Good.\n";
135         }
138 function do_image_name_unique_update() {
139         global $wgDatabase;
140         if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
141                 echo "...image primary key already set.\n";
142         } else {
143                 echo "Making img_name the primary key... ";
144                 dbsource( "maintenance/archives/patch-image_name_primary.sql", $wgDatabase );
145                 echo "ok\n";
146         }
149 function do_watchlist_update() {
150         global $wgDatabase;
151         if( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
152                 echo "ENOTIF: The watchlist table is already set up for email notification.\n";
153         } else {
154                 echo "ENOTIF: Adding wl_notificationtimestamp field for email notification management.";
155                 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
156                 dbsource( "maintenance/archives/patch-email-notification.sql", $wgDatabase );
157                 echo "ok\n";
158         }
161 function do_copy_newtalk_to_watchlist() {
162         global $wgDatabase;
163         global $wgCommandLineMode;      # this needs to be saved while getID() and getName() are called
165         if ( $wgDatabase->tableExists( 'user_newtalk' ) ) {
166                 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
167                         $wgDatabase->tableName( 'user_newtalk' ) );
168                 $num_newtalks=$wgDatabase->numRows($res);
169                 echo "ENOTIF: Now converting ".$num_newtalks." user_newtalk entries to watchlist table entries ... \n";
171                 $user = new User();
172                 for ( $i = 1; $i <= $num_newtalks; $i++ ) {
173                         $wluser = $wgDatabase->fetchObject( $res );
174                         echo 'ENOTIF: <= user_newtalk: user_id='.$wluser->user_id.' user_ip='.$wluser->user_ip."\n";
175                         if ($wluser->user_id == 0) { # anonymous users ... have IP numbers as "names"
176                                 if ($user->isIP($wluser->user_ip)) { # do only if it really looks like an IP number (double checked)
177                                         $wgDatabase->replace( 'watchlist',
178                                                 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
179                                                   array('wl_user'                       => 0,
180                                                         'wl_namespace'                  => NS_USER_TALK,
181                                                         'wl_title'                      => $wluser->user_ip,
182                                                         'wl_notificationtimestamp'      => '19700101000000'
183                                                         ), 'updaters.inc::do_watchlist_update2'
184                                                 );
185                                         echo 'ENOTIF: ====> watchlist: user_id=0 '.$wluser->user_ip."\n";
186                                 }
187                         } else { # normal users ... have user_ids
188                                 $user->setID($wluser->user_id);
189                                 $wgDatabase->replace( 'watchlist',
190                                         array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
191                                           array('wl_user'                       => $user->getID(),
192                                                 'wl_namespace'                  => NS_USER_TALK,
193                                                 'wl_title'                      => $user->getName(),
194                                                 'wl_notificationtimestamp'      => '19700101000000'
195                                                 ), 'updaters.inc::do_watchlist_update3'
196                                         );
197                                 echo 'ENOTIF: ====> watchlist: user_id='.$user->getID().' '.$user->getName()."\n";
198                         }
199                 }
200                 echo "ENOTIF: The watchlist table has got the former user_newtalk entries.\n";
201                 dbsource( "maintenance/archives/patch-drop-user_newtalk.sql", $wgDatabase );
202                 echo "ENOTIF: Deleting the user_newtalk table as its entries are now in the watchlist table.\n";
203         } else {
204                 echo "ENOTIF: No user_newtalk table found. Nothing to convert to watchlist table entries.\n";
205         }
209 function do_user_update() {
210         global $wgDatabase;
211         if( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
212                 echo "EAUTHENT: The user table is already set up for email authentication.\n";
213         } else {
214                 echo "EAUTHENT: Adding user_emailauthenticationtimestamp field for email authentication management.";
215                 /* ALTER TABLE user ADD (user_emailauthenticationtimestamp varchar(14) binary NOT NULL default '0'); */
216                 dbsource( "maintenance/archives/patch-email-authentication.sql", $wgDatabase );
217                 echo "ok\n";
218         }
221 # Assumes that the group table has been added.
222 function do_group_update() {
223         global $wgDatabase;
224         $res = $wgDatabase->safeQuery( 'SELECT COUNT(*) AS c FROM !',
225                 $wgDatabase->tableName( 'group' ) );
226         $row = $wgDatabase->fetchObject( $res );
227         $wgDatabase->freeResult( $res );
228         if( $row->c == 0 ) {
229                 echo "Adding default group definitions... ";
230                 dbsource( "maintenance/archives/patch-userlevels-defaultgroups.sql", $wgDatabase );
231                 echo "ok\n";
232         } else {
233                 echo "...group definitions already in place.\n";
234                 $res = $wgDatabase->safeQuery( "SELECT COUNT(*) AS n FROM !
235                                                  WHERE group_name IN ('Sysops','Bureaucrat')
236                                                    AND group_rights NOT LIKE 'sysop'",
237                                                $wgDatabase->tableName( 'group' ) );
238                 $row = $wgDatabase->fetchObject( $res );
239                 $wgDatabase->freeResult( $res );
240                 if( $row->n ) {
241                         echo "Fixing sysops group permissions... ";
242                         dbsource( "maintenance/archives/patch-group-sysopfix.sql", $wgDatabase );
243                         echo "ok\n";
244                 } else {
245                         echo "...sysop group permissions look ok.\n";
246                 }
247         }
250 function do_schema_restructuring() {
251         global $wgDatabase;
252         $fname="do_schema_restructuring";
253         if ( $wgDatabase->tableExists( 'page' ) ) {
254                 echo "...page table already exists.\n";
255         } else {
256                 echo "...converting from cur/old to page/revision/text DB structure.\n"; flush();
257                 echo "......checking for duplicate entries.\n"; flush();
258                 $rows = $wgDatabase->query( 'SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
259                                 FROM cur GROUP BY cur_title, cur_namespace HAVING c>1', $fname );
261                 if ( $wgDatabase->numRows( $rows ) > 0 ) {
262                         echo "......<b>Found duplicate entries</b>\n";
263                         echo ( sprintf( "<b>      %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
264                         while ( $row = $wgDatabase->fetchObject( $rows ) ) {
265                                 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
266                                         $duplicate[$row->cur_namespace] = array();
267                                 }
268                                 $duplicate[$row->cur_namespace][] = $row->cur_title;
269                                 echo ( sprintf( "      %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
270                         }
271                         $sql = 'SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM cur WHERE ';
272                         $firstCond = true; 
273                         foreach ( $duplicate as $ns => $titles ) {
274                                 if ( $firstCond ) {
275                                         $firstCond = false;
276                                 } else {
277                                         $sql .= ' OR ';
278                                 }
279                                 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
280                                 $first = true;
281                                 foreach ( $titles as $t ) {
282                                         if ( $first ) {
283                                                 $sql .= $wgDatabase->addQuotes( $t );
284                                                 $first = false;
285                                         } else {
286                                                 $sql .= ', ' . $wgDatabase->addQuotes( $t );
287                                         }
288                                 }
289                                 $sql .= ") ) \n";
290                         }
291                         # By sorting descending, the most recent entry will be the first in the list.
292                         # All following entries will be deleted by the next while-loop.
293                         $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
295                         $rows = $wgDatabase->query( $sql, $fname );
297                         $prev_title = $prev_namespace = false;
298                         $deleteId = array();
300                         while ( $row = $wgDatabase->fetchObject( $rows ) ) {
301                                 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
302                                         $deleteId[] = $row->cur_id;
303                                 }
304                                 $prev_title     = $row->cur_title;
305                                 $prev_namespace = $row->cur_namespace;
306                         }
307                         $sql = 'DELETE FROM cur WHERE cur_id IN ( ' . join( ',', $deleteId ) . ')';
308                         $rows = $wgDatabase->query( $sql, $fname );
309                         echo "......<b>Deleted</b> ".$wgDatabase->affectedRows()." records.\n";
310                 }
311                 
313                 echo "......Creating tables.\n";
314                 $wgDatabase->query(" CREATE TABLE page (
315                         page_id int(8) unsigned NOT NULL auto_increment,
316                         page_namespace tinyint NOT NULL,
317                         page_title varchar(255) binary NOT NULL,
318                         page_restrictions tinyblob NOT NULL default '',
319                         page_counter bigint(20) unsigned NOT NULL default '0',
320                         page_is_redirect tinyint(1) unsigned NOT NULL default '0',
321                         page_is_new tinyint(1) unsigned NOT NULL default '0',
322                         page_random real unsigned NOT NULL,
323                         page_touched char(14) binary NOT NULL default '',
324                         page_latest int(8) unsigned NOT NULL,
326                         PRIMARY KEY page_id (page_id),
327                         UNIQUE INDEX name_title (page_namespace,page_title),
328                         INDEX (page_random)
329                         )", $fname );
330                 $wgDatabase->query("CREATE TABLE revision (
331                         rev_id int(8) unsigned NOT NULL auto_increment,
332                         rev_page int(8) unsigned NOT NULL,
333                         rev_comment tinyblob NOT NULL default '',
334                         rev_user int(5) unsigned NOT NULL default '0',
335                         rev_user_text varchar(255) binary NOT NULL default '',
336                         rev_timestamp char(14) binary NOT NULL default '',
337                         rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
338                         inverse_timestamp char(14) binary NOT NULL default '',
339   
340                         PRIMARY KEY rev_page_id (rev_page, rev_id),
341                         UNIQUE INDEX rev_id (rev_id),
342                         INDEX rev_timestamp (rev_timestamp),
343                         INDEX page_timestamp (rev_page,inverse_timestamp),
344                         INDEX user_timestamp (rev_user,inverse_timestamp),
345                         INDEX usertext_timestamp (rev_user_text,inverse_timestamp)
346                         )", $fname );
348                 echo "......Locking tables.\n";
349                 $wgDatabase->query( 'LOCK TABLES page WRITE, revision WRITE, old WRITE, cur WRITE', $fname );
351                 $maxold = $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname );
352                 echo "......maxold is {$maxold}\n";
354                 echo "......Moving text from cur.\n";
355                 $wgDatabase->query( "INSERT INTO old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
356                                 old_timestamp, old_minor_edit, old_flags, inverse_timestamp)
357                         SELECT cur_namespace, cur_title, cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit,
358                                 '', inverse_timestamp
359                         FROM cur", $fname );
361                 echo "......Setting up revision table.\n";
362                 $wgDatabase->query( "INSERT INTO revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
363                                 inverse_timestamp, rev_minor_edit)
364                         SELECT old_id, cur_id, old_comment, old_user, old_user_text,
365                                 old_timestamp, old.inverse_timestamp, old_minor_edit
366                         FROM old,cur WHERE old_namespace=cur_namespace AND old_title=cur_title", $fname );
368                 echo "......Setting up page table.\n";
369                 $wgDatabase->query( "INSERT INTO page (page_id, page_namespace, page_title, page_restrictions, page_counter,
370                                 page_is_redirect, page_is_new, page_random, page_touched, page_latest)
371                         SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
372                                 cur_random, cur_touched, rev_id
373                         FROM cur,revision
374                         WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
376                 echo "......Unlocking tables.\n";
377                 $wgDatabase->query( "UNLOCK TABLES", $fname );
379                 echo "......Renaming old.\n";
380                 $wgDatabase->query( "ALTER TABLE old RENAME TO text", $fname );
381                 echo "...done.\n";
382         }
385 function do_all_updates() {
386         global $wgNewTables, $wgNewFields;
387         
388         # Add missing tables
389         foreach ( $wgNewTables as $tableRecord ) {
390                 add_table( $tableRecord[0], $tableRecord[1] );
391                 flush();
392         }
394         # Add missing fields
395         foreach ( $wgNewFields as $fieldRecord ) {
396                 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2] );
397                 flush();
398         }
399         
400         # Add default group data
401         do_group_update(); flush();
403         # Do schema updates which require special handling
404         do_interwiki_update(); flush();
405         do_index_update(); flush();
406         do_linkscc_1_3_update(); flush();
407         convertLinks(); flush();
408         do_image_name_unique_update(); flush();
409         do_watchlist_update(); flush();
410         do_copy_newtalk_to_watchlist(); flush();
411         do_user_update(); flush();
413         do_schema_restructuring(); flush();
415         if ( isTemplateInitialised() ) {
416                 print "Template namespace already initialised\n";
417         } else {
418                 moveCustomMessages( 1 ); flush();
419                 moveCustomMessages( 2 ); flush();
420                 moveCustomMessages( 3 ); flush();
421         }
423         initialiseMessages(); flush();