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