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