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