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