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