* Adding img_metadata to image
[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 array( 'revision', 'rev_deleted', 'patch-rev_deleted.sql' ),
40 array( 'image', 'img_width', 'patch-img_width.sql' ),
41 array( 'image', 'img_metadata', 'patch-img_metadata.sql' ),
42 );
43
44 function add_table( $name, $patch ) {
45 global $wgDatabase;
46 if ( $wgDatabase->tableExists( $name ) ) {
47 echo "...$name table already exists.\n";
48 } else {
49 echo "Creating $name table...";
50 dbsource( "maintenance/archives/$patch", $wgDatabase );
51 echo "ok\n";
52 }
53 }
54
55 function add_field( $table, $field, $patch ) {
56 global $wgDatabase;
57 if ( !$wgDatabase->tableExists( $table ) ) {
58 echo "...$table table does not exist, skipping new field patch\n";
59 } elseif ( $wgDatabase->fieldExists( $table, $field ) ) {
60 echo "...have $field field in $table table.\n";
61 } else {
62 echo "Adding $field field to table $table...";
63 dbsource( "maintenance/archives/$patch" , $wgDatabase );
64 echo "ok\n";
65 }
66 }
67
68 function do_revision_updates() {
69 global $wgSoftwareRevision;
70 if ( $wgSoftwareRevision < 1001 ) {
71 update_passwords();
72 }
73 }
74
75 function update_passwords() {
76 wfDebugDieBacktrace( "This function needs to be updated or removed.\n" );
77
78 global $wgDatabase;
79 $fname = "Update script: update_passwords()";
80 print "\nIt appears that you need to update the user passwords in your\n" .
81 "database. If you have already done this (if you've run this update\n" .
82 "script once before, for example), doing so again will make all your\n" .
83 "user accounts inaccessible, so be sure you only do this once.\n" .
84 "Update user passwords? (yes/no)";
85
86 $resp = readconsole();
87 if ( ! ( "Y" == $resp{0} || "y" == $resp{0} ) ) { return; }
88
89 $sql = "SELECT user_id,user_password FROM user";
90 $source = $wgDatabase->query( $sql, $fname );
91
92 while ( $row = $wgDatabase->fetchObject( $source ) ) {
93 $id = $row->user_id;
94 $oldpass = $row->user_password;
95 $newpass = md5( "{$id}-{$oldpass}" );
96
97 $sql = "UPDATE user SET user_password='{$newpass}' " .
98 "WHERE user_id={$id}";
99 $wgDatabase->query( $sql, $fname );
100 }
101 }
102
103 function do_interwiki_update() {
104 # Check that interwiki table exists; if it doesn't source it
105 global $wgDatabase;
106 if( $wgDatabase->tableExists( "interwiki" ) ) {
107 echo "...already have interwiki table\n";
108 return true;
109 }
110 echo "Creating interwiki table: ";
111 dbsource( "maintenance/archives/patch-interwiki.sql" );
112 echo "ok\n";
113 echo "Adding default interwiki definitions: ";
114 dbsource( "maintenance/interwiki.sql" );
115 echo "ok\n";
116 }
117
118 function do_index_update() {
119 # Check that proper indexes are in place
120 global $wgDatabase;
121 $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
122 if( $meta->multiple_key == 0 ) {
123 echo "Updating indexes to 20031107: ";
124 dbsource( "maintenance/archives/patch-indexes.sql" );
125 echo "ok\n";
126 return true;
127 }
128 echo "...indexes seem up to 20031107 standards\n";
129 return false;
130 }
131
132 function do_linkscc_1_3_update() {
133 // Update linkscc table to 1.3 schema if necessary
134 global $wgDatabase, $wgVersion;
135 if( $wgDatabase->tableExists( "linkscc" )
136 && $wgDatabase->fieldExists( "linkscc", "lcc_title" ) ) {
137 echo "Altering lcc_title field from linkscc table... ";
138 dbsource( "maintenance/archives/patch-linkscc-1.3.sql", $wgDatabase );
139 echo "ok\n";
140 } else {
141 echo "...linkscc is up to date, or does not exist. Good.\n";
142 }
143 }
144
145 function do_image_name_unique_update() {
146 global $wgDatabase;
147 if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
148 echo "...image primary key already set.\n";
149 } else {
150 echo "Making img_name the primary key... ";
151 dbsource( "maintenance/archives/patch-image_name_primary.sql", $wgDatabase );
152 echo "ok\n";
153 }
154 }
155
156 function do_watchlist_update() {
157 global $wgDatabase;
158 if( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
159 echo "ENOTIF: The watchlist table is already set up for email notification.\n";
160 } else {
161 echo "ENOTIF: Adding wl_notificationtimestamp field for email notification management.";
162 /* ALTER TABLE watchlist ADD (wl_notificationtimestamp varchar(14) binary NOT NULL default '0'); */
163 dbsource( "maintenance/archives/patch-email-notification.sql", $wgDatabase );
164 echo "ok\n";
165 }
166 }
167
168 function do_copy_newtalk_to_watchlist() {
169 global $wgDatabase;
170 global $wgCommandLineMode; # this needs to be saved while getID() and getName() are called
171
172 if ( $wgDatabase->tableExists( 'user_newtalk' ) ) {
173 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
174 $wgDatabase->tableName( 'user_newtalk' ) );
175 $num_newtalks=$wgDatabase->numRows($res);
176 echo "ENOTIF: Now converting ".$num_newtalks." user_newtalk entries to watchlist table entries ... \n";
177
178 $user = new User();
179 for ( $i = 1; $i <= $num_newtalks; $i++ ) {
180 $wluser = $wgDatabase->fetchObject( $res );
181 echo 'ENOTIF: <= user_newtalk: user_id='.$wluser->user_id.' user_ip='.$wluser->user_ip."\n";
182 if ($wluser->user_id == 0) { # anonymous users ... have IP numbers as "names"
183 if ($user->isIP($wluser->user_ip)) { # do only if it really looks like an IP number (double checked)
184 $wgDatabase->replace( 'watchlist',
185 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
186 array('wl_user' => 0,
187 'wl_namespace' => NS_USER_TALK,
188 'wl_title' => $wluser->user_ip,
189 'wl_notificationtimestamp' => '19700101000000'
190 ), 'updaters.inc::do_watchlist_update2'
191 );
192 echo 'ENOTIF: ====> watchlist: user_id=0 '.$wluser->user_ip."\n";
193 }
194 } else { # normal users ... have user_ids
195 $user->setID($wluser->user_id);
196 $wgDatabase->replace( 'watchlist',
197 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
198 array('wl_user' => $user->getID(),
199 'wl_namespace' => NS_USER_TALK,
200 'wl_title' => $user->getName(),
201 'wl_notificationtimestamp' => '19700101000000'
202 ), 'updaters.inc::do_watchlist_update3'
203 );
204 echo 'ENOTIF: ====> watchlist: user_id='.$user->getID().' '.$user->getName()."\n";
205 }
206 }
207 echo "ENOTIF: The watchlist table has got the former user_newtalk entries.\n";
208 dbsource( "maintenance/archives/patch-drop-user_newtalk.sql", $wgDatabase );
209 echo "ENOTIF: Deleting the user_newtalk table as its entries are now in the watchlist table.\n";
210 } else {
211 echo "ENOTIF: No user_newtalk table found. Nothing to convert to watchlist table entries.\n";
212 }
213 }
214
215
216 function do_user_update() {
217 global $wgDatabase;
218 if( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
219 echo "EAUTHENT: The user table is already set up for email authentication.\n";
220 } else {
221 echo "EAUTHENT: Adding user_emailauthenticationtimestamp field for email authentication management.";
222 /* ALTER TABLE user ADD (user_emailauthenticationtimestamp varchar(14) binary NOT NULL default '0'); */
223 dbsource( "maintenance/archives/patch-email-authentication.sql", $wgDatabase );
224 echo "ok\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 ?>