]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/installer/MysqlUpdater.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.git] / includes / installer / MysqlUpdater.php
1 <?php
2 /**
3  * MySQL-specific updater.
4  *
5  * @file
6  * @ingroup Deployment
7  */
8
9 /**
10  * Mysql update list and mysql-specific update functions.
11  *
12  * @ingroup Deployment
13  * @since 1.17
14  */
15 class MysqlUpdater extends DatabaseUpdater {
16
17         protected function getCoreUpdateList() {
18                 return array(
19                         // 1.2
20                         array( 'addField', 'ipblocks',      'ipb_id',           'patch-ipblocks.sql' ),
21                         array( 'addField', 'ipblocks',      'ipb_expiry',       'patch-ipb_expiry.sql' ),
22                         array( 'doInterwikiUpdate' ),
23                         array( 'doIndexUpdate' ),
24                         array( 'addTable', 'hitcounter',                        'patch-hitcounter.sql' ),
25                         array( 'addField', 'recentchanges', 'rc_type',          'patch-rc_type.sql' ),
26
27                         // 1.3
28                         array( 'addField', 'user',          'user_real_name',   'patch-user-realname.sql' ),
29                         array( 'addTable', 'querycache',                        'patch-querycache.sql' ),
30                         array( 'addTable', 'objectcache',                       'patch-objectcache.sql' ),
31                         array( 'addTable', 'categorylinks',                     'patch-categorylinks.sql' ),
32                         array( 'doOldLinksUpdate' ),
33                         array( 'doFixAncientImagelinks' ),
34                         array( 'addField', 'recentchanges', 'rc_ip',            'patch-rc_ip.sql' ),
35
36                         // 1.4
37                         array( 'addIndex', 'image',         'PRIMARY',          'patch-image_name_primary.sql' ),
38                         array( 'addField', 'recentchanges', 'rc_id',            'patch-rc_id.sql' ),
39                         array( 'addField', 'recentchanges', 'rc_patrolled',     'patch-rc-patrol.sql' ),
40                         array( 'addTable', 'logging',                           'patch-logging.sql' ),
41                         array( 'addField', 'user',          'user_token',       'patch-user_token.sql' ),
42                         array( 'addField', 'watchlist',     'wl_notificationtimestamp', 'patch-email-notification.sql' ),
43                         array( 'doWatchlistUpdate' ),
44                         array( 'dropField', 'user',         'user_emailauthenticationtimestamp', 'patch-email-authentication.sql' ),
45
46                         // 1.5
47                         array( 'doSchemaRestructuring' ),
48                         array( 'addField', 'logging',       'log_params',       'patch-log_params.sql' ),
49                         array( 'checkBin', 'logging',       'log_title',        'patch-logging-title.sql', ),
50                         array( 'addField', 'archive',       'ar_rev_id',        'patch-archive-rev_id.sql' ),
51                         array( 'addField', 'page',          'page_len',         'patch-page_len.sql' ),
52                         array( 'dropField', 'revision',     'inverse_timestamp', 'patch-inverse_timestamp.sql' ),
53                         array( 'addField', 'revision',      'rev_text_id',      'patch-rev_text_id.sql' ),
54                         array( 'addField', 'revision',      'rev_deleted',      'patch-rev_deleted.sql' ),
55                         array( 'addField', 'image',         'img_width',        'patch-img_width.sql' ),
56                         array( 'addField', 'image',         'img_metadata',     'patch-img_metadata.sql' ),
57                         array( 'addField', 'user',          'user_email_token', 'patch-user_email_token.sql' ),
58                         array( 'addField', 'archive',       'ar_text_id',       'patch-archive-text_id.sql' ),
59                         array( 'doNamespaceSize' ),
60                         array( 'addField', 'image',         'img_media_type',   'patch-img_media_type.sql' ),
61                         array( 'doPagelinksUpdate' ),
62                         array( 'dropField', 'image',        'img_type',         'patch-drop_img_type.sql' ),
63                         array( 'doUserUniqueUpdate' ),
64                         array( 'doUserGroupsUpdate' ),
65                         array( 'addField', 'site_stats',    'ss_total_pages',   'patch-ss_total_articles.sql' ),
66                         array( 'addTable', 'user_newtalk',                      'patch-usernewtalk2.sql' ),
67                         array( 'addTable', 'transcache',                        'patch-transcache.sql' ),
68                         array( 'addField', 'interwiki',     'iw_trans',         'patch-interwiki-trans.sql' ),
69                         array( 'addTable', 'trackbacks',                        'patch-trackbacks.sql' ),
70
71                         // 1.6
72                         array( 'doWatchlistNull' ),
73                         array( 'addIndex', 'logging',         'times',            'patch-logging-times-index.sql' ),
74                         array( 'addField', 'ipblocks',        'ipb_range_start',  'patch-ipb_range_start.sql' ),
75                         array( 'doPageRandomUpdate' ),
76                         array( 'addField', 'user',            'user_registration', 'patch-user_registration.sql' ),
77                         array( 'doTemplatelinksUpdate' ),
78                         array( 'addTable', 'externallinks',                       'patch-externallinks.sql' ),
79                         array( 'addTable', 'job',                                 'patch-job.sql' ),
80                         array( 'addField', 'site_stats',      'ss_images',        'patch-ss_images.sql' ),
81                         array( 'addTable', 'langlinks',                           'patch-langlinks.sql' ),
82                         array( 'addTable', 'querycache_info',                     'patch-querycacheinfo.sql' ),
83                         array( 'addTable', 'filearchive',                         'patch-filearchive.sql' ),
84                         array( 'addField', 'ipblocks',        'ipb_anon_only',    'patch-ipb_anon_only.sql' ),
85                         array( 'addIndex', 'recentchanges',   'rc_ns_usertext',   'patch-recentchanges-utindex.sql' ),
86                         array( 'addIndex', 'recentchanges',   'rc_user_text',     'patch-rc_user_text-index.sql' ),
87
88                         // 1.9
89                         array( 'addField', 'user',          'user_newpass_time', 'patch-user_newpass_time.sql' ),
90                         array( 'addTable', 'redirect',                           'patch-redirect.sql' ),
91                         array( 'addTable', 'querycachetwo',                      'patch-querycachetwo.sql' ),
92                         array( 'addField', 'ipblocks',      'ipb_enable_autoblock', 'patch-ipb_optional_autoblock.sql' ),
93                         array( 'doBacklinkingIndicesUpdate' ),
94                         array( 'addField', 'recentchanges', 'rc_old_len',        'patch-rc_len.sql' ),
95                         array( 'addField', 'user',          'user_editcount',    'patch-user_editcount.sql' ),
96
97                         // 1.10
98                         array( 'doRestrictionsUpdate' ),
99                         array( 'addField', 'logging',       'log_id',           'patch-log_id.sql' ),
100                         array( 'addField', 'revision',      'rev_parent_id',    'patch-rev_parent_id.sql' ),
101                         array( 'addField', 'page_restrictions', 'pr_id',        'patch-page_restrictions_sortkey.sql' ),
102                         array( 'addField', 'revision',      'rev_len',          'patch-rev_len.sql' ),
103                         array( 'addField', 'recentchanges', 'rc_deleted',       'patch-rc_deleted.sql' ),
104                         array( 'addField', 'logging',       'log_deleted',      'patch-log_deleted.sql' ),
105                         array( 'addField', 'archive',       'ar_deleted',       'patch-ar_deleted.sql' ),
106                         array( 'addField', 'ipblocks',      'ipb_deleted',      'patch-ipb_deleted.sql' ),
107                         array( 'addField', 'filearchive',   'fa_deleted',       'patch-fa_deleted.sql' ),
108                         array( 'addField', 'archive',       'ar_len',           'patch-ar_len.sql' ),
109
110                         // 1.11
111                         array( 'addField', 'ipblocks',      'ipb_block_email',  'patch-ipb_emailban.sql' ),
112                         array( 'doCategorylinksIndicesUpdate' ),
113                         array( 'addField', 'oldimage',      'oi_metadata',      'patch-oi_metadata.sql' ),
114                         array( 'addIndex', 'archive',       'usertext_timestamp', 'patch-archive-user-index.sql' ),
115                         array( 'addIndex', 'image',         'img_usertext_timestamp', 'patch-image-user-index.sql' ),
116                         array( 'addIndex', 'oldimage',      'oi_usertext_timestamp', 'patch-oldimage-user-index.sql' ),
117                         array( 'addField', 'archive',       'ar_page_id',       'patch-archive-page_id.sql' ),
118                         array( 'addField', 'image',         'img_sha1',         'patch-img_sha1.sql' ),
119
120                         // 1.12
121                         array( 'addTable', 'protected_titles',                  'patch-protected_titles.sql' ),
122
123                         // 1.13
124                         array( 'addField', 'ipblocks',      'ipb_by_text',      'patch-ipb_by_text.sql' ),
125                         array( 'addTable', 'page_props',                        'patch-page_props.sql' ),
126                         array( 'addTable', 'updatelog',                         'patch-updatelog.sql' ),
127                         array( 'addTable', 'category',                          'patch-category.sql' ),
128                         array( 'doCategoryPopulation' ),
129                         array( 'addField', 'archive',       'ar_parent_id',     'patch-ar_parent_id.sql' ),
130                         array( 'addField', 'user_newtalk',  'user_last_timestamp', 'patch-user_last_timestamp.sql' ),
131                         array( 'doPopulateParentId' ),
132                         array( 'checkBin', 'protected_titles', 'pt_title',      'patch-pt_title-encoding.sql', ),
133                         array( 'doMaybeProfilingMemoryUpdate' ),
134                         array( 'doFilearchiveIndicesUpdate' ),
135
136                         // 1.14
137                         array( 'addField', 'site_stats',    'ss_active_users',  'patch-ss_active_users.sql' ),
138                         array( 'doActiveUsersInit' ),
139                         array( 'addField', 'ipblocks',      'ipb_allow_usertalk', 'patch-ipb_allow_usertalk.sql' ),
140
141                         // 1.15
142                         array( 'doUniquePlTlIl' ),
143                         array( 'addTable', 'change_tag',                        'patch-change_tag.sql' ),
144                         array( 'addTable', 'tag_summary',                       'patch-change_tag.sql' ),
145                         array( 'addTable', 'valid_tag',                         'patch-change_tag.sql' ),
146
147                         // 1.16
148                         array( 'addTable', 'user_properties',                   'patch-user_properties.sql' ),
149                         array( 'addTable', 'log_search',                        'patch-log_search.sql' ),
150                         array( 'addField', 'logging',       'log_user_text',    'patch-log_user_text.sql' ),
151                         array( 'doLogUsertextPopulation' ), # listed separately from the previous update because 1.16 was released without this update
152                         array( 'doLogSearchPopulation' ),
153                         array( 'addTable', 'l10n_cache',                        'patch-l10n_cache.sql' ),
154                         array( 'addTable', 'external_user',                     'patch-external_user.sql' ),
155                         array( 'addIndex', 'log_search',    'ls_field_val',     'patch-log_search-rename-index.sql' ),
156                         array( 'addIndex', 'change_tag',    'change_tag_rc_tag', 'patch-change_tag-indexes.sql' ),
157                         array( 'addField', 'redirect',      'rd_interwiki',     'patch-rd_interwiki.sql' ),
158                         array( 'doUpdateTranscacheField' ),
159                         array( 'renameEuWikiId' ),
160                         array( 'doUpdateMimeMinorField' ),
161                         array( 'doPopulateRevLen' ),
162
163                         // 1.17
164                         array( 'addTable', 'iwlinks',                           'patch-iwlinks.sql' ),
165                         array( 'addIndex', 'iwlinks', 'iwl_prefix_title_from',  'patch-rename-iwl_prefix.sql' ),
166                         array( 'addField', 'updatelog', 'ul_value',              'patch-ul_value.sql' ),
167                         array( 'addField', 'interwiki',     'iw_api',           'patch-iw_api_and_wikiid.sql' ),
168                         array( 'dropIndex', 'iwlinks', 'iwl_prefix',  'patch-kill-iwl_prefix.sql' ),
169                         array( 'dropIndex', 'iwlinks', 'iwl_prefix_from_title', 'patch-kill-iwl_pft.sql' ),
170                         array( 'addField', 'categorylinks', 'cl_collation', 'patch-categorylinks-better-collation.sql' ),
171                         array( 'doClFieldsUpdate' ),
172                         array( 'doCollationUpdate' ),
173                         array( 'addTable', 'msg_resource',                      'patch-msg_resource.sql' ),
174                         array( 'addTable', 'module_deps',                       'patch-module_deps.sql' ),
175                         array( 'dropIndex', 'archive', 'ar_page_revid',         'patch-archive_kill_ar_page_revid.sql' ),
176                         array( 'addIndex', 'archive', 'ar_revid',               'patch-archive_ar_revid.sql' ),
177                         array( 'doLangLinksLengthUpdate' ),
178                 );
179         }
180
181         /**
182          * 1.4 betas were missing the 'binary' marker from logging.log_title,
183          * which causes a collation mismatch error on joins in MySQL 4.1.
184          *
185          * @param $table String: table name
186          * @param $field String: field name to check
187          * @param $patchFile String: path to the patch to correct the field
188          */
189         protected function checkBin( $table, $field, $patchFile ) {
190                 $tableName = $this->db->tableName( $table );
191                 $res = $this->db->query( "SELECT $field FROM $tableName LIMIT 0", __METHOD__ );
192                 $flags = explode( ' ', mysql_field_flags( $res->result, 0 ) );
193
194                 if ( in_array( 'binary', $flags ) ) {
195                         $this->output( "...$table table has correct $field encoding.\n" );
196                 } else {
197                         $this->output( "Fixing $field encoding on $table table... " );
198                         $this->applyPatch( $patchFile );
199                         $this->output( "done.\n" );
200                 }
201         }
202
203         /**
204          * Check whether an index contain a field
205          *
206          * @param $table String: table name
207          * @param $index String: index name to check
208          * @param $field String: field that should be in the index
209          * @return Boolean
210          */
211         protected function indexHasField( $table, $index, $field ) {
212                 $info = $this->db->indexInfo( $table, $index, __METHOD__ );
213                 if ( $info ) {
214                         foreach ( $info as $row ) {
215                                 if ( $row->Column_name == $field ) {
216                                         $this->output( "...index $index on table $table includes field $field\n" );
217                                         return true;
218                                 }
219                         }
220                 }
221                 $this->output( "...index $index on table $table has no field $field; adding\n" );
222                 return false;
223         }
224
225         /**
226          * Check that interwiki table exists; if it doesn't source it
227          */
228         protected function doInterwikiUpdate() {
229                 global $IP;
230
231                 if ( $this->db->tableExists( "interwiki" ) ) {
232                         $this->output( "...already have interwiki table\n" );
233                         return;
234                 }
235
236                 $this->output( 'Creating interwiki table...' );
237                 $this->applyPatch( 'patch-interwiki.sql' );
238                 $this->output( "ok\n" );
239                 $this->output( 'Adding default interwiki definitions...' );
240                 $this->applyPatch( "$IP/maintenance/interwiki.sql", true );
241                 $this->output( "done.\n" );
242         }
243
244         /**
245          * Check that proper indexes are in place
246          */
247         protected function doIndexUpdate() {
248                 $meta = $this->db->fieldInfo( 'recentchanges', 'rc_timestamp' );
249                 if ( $meta->isMultipleKey() ) {
250                         $this->output( "...indexes seem up to 20031107 standards\n" );
251                         return;
252                 }
253
254                 $this->output( "Updating indexes to 20031107..." );
255                 $this->applyPatch( 'patch-indexes.sql', true );
256                 $this->output( "done.\n" );
257         }
258
259         protected function doOldLinksUpdate() {
260                 $cl = $this->maintenance->runChild( 'ConvertLinks' );
261                 $cl->execute();
262         }
263
264         protected function doFixAncientImagelinks() {
265                 $info = $this->db->fieldInfo( 'imagelinks', 'il_from' );
266                 if ( !$info || $info->type() !== 'string' ) {
267                         $this->output( "...il_from OK\n" );
268                         return;
269                 }
270
271                 $this->output( "Fixing ancient broken imagelinks table.\n" );
272                 $this->output( "NOTE: you will have to run maintenance/refreshLinks.php after this.\n" );
273                 $this->applyPatch( 'patch-fix-il_from.sql' );
274                 $this->output( "done.\n" );
275         }
276
277         /**
278          * Check if we need to add talk page rows to the watchlist
279          */
280         function doWatchlistUpdate() {
281                 $talk = $this->db->selectField( 'watchlist', 'count(*)', 'wl_namespace & 1', __METHOD__ );
282                 $nontalk = $this->db->selectField( 'watchlist', 'count(*)', 'NOT (wl_namespace & 1)', __METHOD__ );
283                 if ( $talk == $nontalk ) {
284                         $this->output( "...watchlist talk page rows already present\n" );
285                         return;
286                 }
287
288                 $this->output( "Adding missing watchlist talk page rows... " );
289                 $this->db->insertSelect( 'watchlist', 'watchlist',
290                         array(
291                                 'wl_user' => 'wl_user',
292                                 'wl_namespace' => 'wl_namespace | 1',
293                                 'wl_title' => 'wl_title',
294                                 'wl_notificationtimestamp' => 'wl_notificationtimestamp'
295                         ), array( 'NOT (wl_namespace & 1)' ), __METHOD__, 'IGNORE' );
296                 $this->output( "done.\n" );
297         }
298
299         function doSchemaRestructuring() {
300                 if ( $this->db->tableExists( 'page' ) ) {
301                         $this->output( "...page table already exists.\n" );
302                         return;
303                 }
304
305                 $this->output( "...converting from cur/old to page/revision/text DB structure.\n" );
306                 $this->output( wfTimestamp( TS_DB ) );
307                 $this->output( "......checking for duplicate entries.\n" );
308
309                 list ( $cur, $old, $page, $revision, $text ) = $this->db->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
310
311                 $rows = $this->db->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
312                                 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", __METHOD__ );
313
314                 if ( $rows->numRows() > 0 ) {
315                         $this->output( wfTimestamp( TS_DB ) );
316                         $this->output( "......<b>Found duplicate entries</b>\n" );
317                         $this->output( sprintf( "<b>      %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
318                         $duplicate = array();
319                         foreach ( $rows as $row ) {
320                                 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
321                                         $duplicate[$row->cur_namespace] = array();
322                                 }
323                                 $duplicate[$row->cur_namespace][] = $row->cur_title;
324                                 $this->output( sprintf( "      %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
325                         }
326                         $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
327                         $firstCond = true;
328                         foreach ( $duplicate as $ns => $titles ) {
329                                 if ( $firstCond ) {
330                                         $firstCond = false;
331                                 } else {
332                                         $sql .= ' OR ';
333                                 }
334                                 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
335                                 $first = true;
336                                 foreach ( $titles as $t ) {
337                                         if ( $first ) {
338                                                 $sql .= $this->db->addQuotes( $t );
339                                                 $first = false;
340                                         } else {
341                                                 $sql .= ', ' . $this->db->addQuotes( $t );
342                                         }
343                                 }
344                                 $sql .= ") ) \n";
345                         }
346                         # By sorting descending, the most recent entry will be the first in the list.
347                         # All following entries will be deleted by the next while-loop.
348                         $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
349
350                         $rows = $this->db->query( $sql, __METHOD__ );
351
352                         $prev_title = $prev_namespace = false;
353                         $deleteId = array();
354
355                         foreach ( $rows as $row ) {
356                                 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
357                                         $deleteId[] = $row->cur_id;
358                                 }
359                                 $prev_title     = $row->cur_title;
360                                 $prev_namespace = $row->cur_namespace;
361                         }
362                         $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
363                         $this->db->query( $sql, __METHOD__ );
364                         $this->output( wfTimestamp( TS_DB ) );
365                         $this->output( "......<b>Deleted</b> " . $this->db->affectedRows() . " records.\n" );
366                 }
367
368                 $this->output( wfTimestamp( TS_DB ) );
369                 $this->output( "......Creating tables.\n" );
370                 $this->db->query( "CREATE TABLE $page (
371                         page_id int(8) unsigned NOT NULL auto_increment,
372                         page_namespace int NOT NULL,
373                         page_title varchar(255) binary NOT NULL,
374                         page_restrictions tinyblob NOT NULL,
375                         page_counter bigint(20) unsigned NOT NULL default '0',
376                         page_is_redirect tinyint(1) unsigned NOT NULL default '0',
377                         page_is_new tinyint(1) unsigned NOT NULL default '0',
378                         page_random real unsigned NOT NULL,
379                         page_touched char(14) binary NOT NULL default '',
380                         page_latest int(8) unsigned NOT NULL,
381                         page_len int(8) unsigned NOT NULL,
382
383                         PRIMARY KEY page_id (page_id),
384                         UNIQUE INDEX name_title (page_namespace,page_title),
385                         INDEX (page_random),
386                         INDEX (page_len)
387                         ) ENGINE=InnoDB", __METHOD__ );
388                 $this->db->query( "CREATE TABLE $revision (
389                         rev_id int(8) unsigned NOT NULL auto_increment,
390                         rev_page int(8) unsigned NOT NULL,
391                         rev_comment tinyblob NOT NULL,
392                         rev_user int(5) unsigned NOT NULL default '0',
393                         rev_user_text varchar(255) binary NOT NULL default '',
394                         rev_timestamp char(14) binary NOT NULL default '',
395                         rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
396                         rev_deleted tinyint(1) unsigned NOT NULL default '0',
397                         rev_len int(8) unsigned,
398                         rev_parent_id int(8) unsigned default NULL,
399                         PRIMARY KEY rev_page_id (rev_page, rev_id),
400                         UNIQUE INDEX rev_id (rev_id),
401                         INDEX rev_timestamp (rev_timestamp),
402                         INDEX page_timestamp (rev_page,rev_timestamp),
403                         INDEX user_timestamp (rev_user,rev_timestamp),
404                         INDEX usertext_timestamp (rev_user_text,rev_timestamp)
405                         ) ENGINE=InnoDB", __METHOD__ );
406
407                 $this->output( wfTimestamp( TS_DB ) );
408                 $this->output( "......Locking tables.\n" );
409                 $this->db->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", __METHOD__ );
410
411                 $maxold = intval( $this->db->selectField( 'old', 'max(old_id)', '', __METHOD__ ) );
412                 $this->output( wfTimestamp( TS_DB ) );
413                 $this->output( "......maxold is {$maxold}\n" );
414
415                 $this->output( wfTimestamp( TS_DB ) );
416                 global $wgLegacySchemaConversion;
417                 if ( $wgLegacySchemaConversion ) {
418                         // Create HistoryBlobCurStub entries.
419                         // Text will be pulled from the leftover 'cur' table at runtime.
420                         $this->output( "......Moving metadata from cur; using blob references to text in cur table.\n" );
421                         $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
422                         $cur_flags = "'object'";
423                 } else {
424                         // Copy all cur text in immediately: this may take longer but avoids
425                         // having to keep an extra table around.
426                         $this->output( "......Moving text from cur.\n" );
427                         $cur_text = 'cur_text';
428                         $cur_flags = "''";
429                 }
430                 $this->db->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
431                                         old_timestamp, old_minor_edit, old_flags)
432                         SELECT cur_namespace, cur_title, $cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags
433                         FROM $cur", __METHOD__ );
434
435                 $this->output( wfTimestamp( TS_DB ) );
436                 $this->output( "......Setting up revision table.\n" );
437                 $this->db->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
438                                 rev_minor_edit)
439                         SELECT old_id, cur_id, old_comment, old_user, old_user_text,
440                                 old_timestamp, old_minor_edit
441                         FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", __METHOD__ );
442
443                 $this->output( wfTimestamp( TS_DB ) );
444                 $this->output( "......Setting up page table.\n" );
445                 $this->db->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
446                         page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
447                         SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
448                                 cur_random, cur_touched, rev_id, LENGTH(cur_text)
449                         FROM $cur,$revision
450                         WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", __METHOD__ );
451
452                 $this->output( wfTimestamp( TS_DB ) );
453                 $this->output( "......Unlocking tables.\n" );
454                 $this->db->query( "UNLOCK TABLES", __METHOD__ );
455
456                 $this->output( wfTimestamp( TS_DB ) );
457                 $this->output( "......Renaming old.\n" );
458                 $this->db->query( "ALTER TABLE $old RENAME TO $text", __METHOD__ );
459
460                 $this->output( wfTimestamp( TS_DB ) );
461                 $this->output( "...done.\n" );
462         }
463
464         protected function doNamespaceSize() {
465                 $tables = array(
466                         'page'          => 'page',
467                         'archive'       => 'ar',
468                         'recentchanges' => 'rc',
469                         'watchlist'     => 'wl',
470                         'querycache'    => 'qc',
471                         'logging'       => 'log',
472                 );
473                 foreach ( $tables as $table => $prefix ) {
474                         $field = $prefix . '_namespace';
475
476                         $tablename = $this->db->tableName( $table );
477                         $result = $this->db->query( "SHOW COLUMNS FROM $tablename LIKE '$field'", __METHOD__ );
478                         $info = $this->db->fetchObject( $result );
479
480                         if ( substr( $info->Type, 0, 3 ) == 'int' ) {
481                                 $this->output( "...$field is already a full int ($info->Type).\n" );
482                         } else {
483                                 $this->output( "Promoting $field from $info->Type to int... " );
484                                 $this->db->query( "ALTER TABLE $tablename MODIFY $field int NOT NULL", __METHOD__ );
485                                 $this->output( "done.\n" );
486                         }
487                 }
488         }
489
490         protected function doPagelinksUpdate() {
491                 if ( $this->db->tableExists( 'pagelinks' ) ) {
492                         $this->output( "...already have pagelinks table.\n" );
493                         return;
494                 }
495
496                 $this->output( "Converting links and brokenlinks tables to pagelinks... " );
497                 $this->applyPatch( 'patch-pagelinks.sql' );
498                 $this->output( "done.\n" );
499
500                 global $wgContLang;
501                 foreach ( MWNamespace::getCanonicalNamespaces() as $ns => $name ) {
502                         if ( $ns == 0 ) {
503                                 continue;
504                         }
505
506                         $this->output( "Cleaning up broken links for namespace $ns... " );
507
508                         $pagelinks = $this->db->tableName( 'pagelinks' );
509                         $name = $wgContLang->getNsText( $ns );
510                         $prefix = $this->db->strencode( $name );
511                         $likeprefix = str_replace( '_', '\\_', $prefix );
512
513                         $sql = "UPDATE $pagelinks
514                                            SET pl_namespace=$ns,
515                                                    pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
516                                          WHERE pl_namespace=0
517                                            AND pl_title LIKE '$likeprefix:%'";
518
519                         $this->db->query( $sql, __METHOD__ );
520                         $this->output( "done.\n" );
521                 }
522         }
523
524         protected function doUserUniqueUpdate() {
525                 $duper = new UserDupes( $this->db, array( $this, 'output' ) );
526                 if ( $duper->hasUniqueIndex() ) {
527                         $this->output( "...already have unique user_name index.\n" );
528                         return;
529                 }
530
531                 if ( !$duper->clearDupes() ) {
532                         $this->output( "WARNING: This next step will probably fail due to unfixed duplicates...\n" );
533                 }
534                 $this->output( "Adding unique index on user_name... " );
535                 $this->applyPatch( 'patch-user_nameindex.sql' );
536                 $this->output( "done.\n" );
537         }
538
539         protected function doUserGroupsUpdate() {
540                 if ( $this->db->tableExists( 'user_groups' ) ) {
541                         $info = $this->db->fieldInfo( 'user_groups', 'ug_group' );
542                         if ( $info->type() == 'int' ) {
543                                 $oldug = $this->db->tableName( 'user_groups' );
544                                 $newug = $this->db->tableName( 'user_groups_bogus' );
545                                 $this->output( "user_groups table exists but is in bogus intermediate format. Renaming to $newug... " );
546                                 $this->db->query( "ALTER TABLE $oldug RENAME TO $newug", __METHOD__ );
547                                 $this->output( "ok\n" );
548
549                                 $this->output( "Re-adding fresh user_groups table... " );
550                                 $this->applyPatch( 'patch-user_groups.sql' );
551                                 $this->output( "ok\n" );
552
553                                 $this->output( "***\n" );
554                                 $this->output( "*** WARNING: You will need to manually fix up user permissions in the user_groups\n" );
555                                 $this->output( "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n" );
556                                 $this->output( "***\n" );
557                         } else {
558                                 $this->output( "...user_groups table exists and is in current format.\n" );
559                         }
560                         return;
561                 }
562
563                 $this->output( "Adding user_groups table... " );
564                 $this->applyPatch( 'patch-user_groups.sql' );
565                 $this->output( "ok\n" );
566
567                 if ( !$this->db->tableExists( 'user_rights' ) ) {
568                         if ( $this->db->fieldExists( 'user', 'user_rights' ) ) {
569                                 $this->output( "Upgrading from a 1.3 or older database? Breaking out user_rights for conversion..." );
570                                 $this->db->applyPatch( 'patch-user_rights.sql' );
571                                 $this->output( "ok\n" );
572                         } else {
573                                 $this->output( "*** WARNING: couldn't locate user_rights table or field for upgrade.\n" );
574                                 $this->output( "*** You may need to manually configure some sysops by manipulating\n" );
575                                 $this->output( "*** the user_groups table.\n" );
576                                 return;
577                         }
578                 }
579
580                 $this->output( "Converting user_rights table to user_groups... " );
581                 $result = $this->db->select( 'user_rights',
582                         array( 'ur_user', 'ur_rights' ),
583                         array( "ur_rights != ''" ),
584                         __METHOD__ );
585
586                 foreach ( $result as $row ) {
587                         $groups = array_unique(
588                                 array_map( 'trim',
589                                         explode( ',', $row->ur_rights ) ) );
590
591                         foreach ( $groups as $group ) {
592                                 $this->db->insert( 'user_groups',
593                                         array(
594                                                 'ug_user'  => $row->ur_user,
595                                                 'ug_group' => $group ),
596                                         __METHOD__ );
597                         }
598                 }
599                 $this->output( "done.\n" );
600         }
601
602         /**
603          * Make sure wl_notificationtimestamp can be NULL,
604          * and update old broken items.
605          */
606         protected function doWatchlistNull() {
607                 $info = $this->db->fieldInfo( 'watchlist', 'wl_notificationtimestamp' );
608                 if ( $info->isNullable() ) {
609                         $this->output( "...wl_notificationtimestamp is already nullable.\n" );
610                         return;
611                 }
612
613                 $this->output( "Making wl_notificationtimestamp nullable... " );
614                 $this->applyPatch( 'patch-watchlist-null.sql' );
615                 $this->output( "done.\n" );
616         }
617
618         /**
619          * Set page_random field to a random value where it is equals to 0.
620          *
621          * @see bug 3946
622          */
623         protected function doPageRandomUpdate() {
624                 $page = $this->db->tableName( 'page' );
625                 $this->db->query( "UPDATE $page SET page_random = RAND() WHERE page_random = 0", __METHOD__ );
626                 $rows = $this->db->affectedRows();
627
628                 if( $rows ) {
629                         $this->output( "Set page_random to a random value on $rows rows where it was set to 0\n" );
630                 } else {
631                         $this->output( "...no page_random rows needed to be set\n" );
632                 }
633         }
634
635         protected function doTemplatelinksUpdate() {
636                 if ( $this->db->tableExists( 'templatelinks' ) ) {
637                         $this->output( "...templatelinks table already exists\n" );
638                         return;
639                 }
640
641                 $this->output( "Creating templatelinks table...\n" );
642                 $this->applyPatch( 'patch-templatelinks.sql' );
643                 $this->output( "Populating...\n" );
644                 if ( wfGetLB()->getServerCount() > 1 ) {
645                         // Slow, replication-friendly update
646                         $res = $this->db->select( 'pagelinks', array( 'pl_from', 'pl_namespace', 'pl_title' ),
647                                 array( 'pl_namespace' => NS_TEMPLATE ), __METHOD__ );
648                         $count = 0;
649                         foreach ( $res as $row ) {
650                                 $count = ( $count + 1 ) % 100;
651                                 if ( $count == 0 ) {
652                                         wfWaitForSlaves( 10 );
653                                 }
654                                 $this->db->insert( 'templatelinks',
655                                         array(
656                                                 'tl_from' => $row->pl_from,
657                                                 'tl_namespace' => $row->pl_namespace,
658                                                 'tl_title' => $row->pl_title,
659                                         ), __METHOD__
660                                 );
661
662                         }
663                 } else {
664                         // Fast update
665                         $this->db->insertSelect( 'templatelinks', 'pagelinks',
666                                 array(
667                                         'tl_from' => 'pl_from',
668                                         'tl_namespace' => 'pl_namespace',
669                                         'tl_title' => 'pl_title'
670                                 ), array(
671                                         'pl_namespace' => 10
672                                 ), __METHOD__
673                         );
674                 }
675                 $this->output( "Done. Please run maintenance/refreshLinks.php for a more thorough templatelinks update.\n" );
676         }
677
678         protected function doBacklinkingIndicesUpdate() {
679                 if ( !$this->indexHasField( 'pagelinks', 'pl_namespace', 'pl_from' ) ||
680                         !$this->indexHasField( 'templatelinks', 'tl_namespace', 'tl_from' ) ||
681                         !$this->indexHasField( 'imagelinks', 'il_to', 'il_from' ) )
682                 {
683                         $this->applyPatch( 'patch-backlinkindexes.sql' );
684                         $this->output( "...backlinking indices updated\n" );
685                 }
686         }
687
688         /**
689          * Adding page_restrictions table, obsoleting page.page_restrictions.
690          * Migrating old restrictions to new table
691          * -- Andrew Garrett, January 2007.
692          */
693         protected function doRestrictionsUpdate() {
694                 if ( $this->db->tableExists( 'page_restrictions' ) ) {
695                         $this->output( "...page_restrictions table already exists.\n" );
696                         return;
697                 }
698
699                 $this->output( "Creating page_restrictions table..." );
700                 $this->applyPatch( 'patch-page_restrictions.sql' );
701                 $this->applyPatch( 'patch-page_restrictions_sortkey.sql' );
702                 $this->output( "done.\n" );
703
704                 $this->output( "Migrating old restrictions to new table...\n" );
705                 $task = $this->maintenance->runChild( 'UpdateRestrictions' );
706                 $task->execute();
707         }
708
709         protected function doCategorylinksIndicesUpdate() {
710                 if ( !$this->indexHasField( 'categorylinks', 'cl_sortkey', 'cl_from' ) ) {
711                         $this->applyPatch( 'patch-categorylinksindex.sql' );
712                         $this->output( "...categorylinks indices updated\n" );
713                 }
714         }
715
716         protected function doCategoryPopulation() {
717                 if ( $this->updateRowExists( 'populate category' ) ) {
718                         $this->output( "...category table already populated.\n" );
719                         return;
720                 }
721
722                 $this->output(
723                         "Populating category table, printing progress markers. " .
724                         "For large databases, you\n" .
725                         "may want to hit Ctrl-C and do this manually with maintenance/\n" .
726                         "populateCategory.php.\n"
727                 );
728                 $task = $this->maintenance->runChild( 'PopulateCategory' );
729                 $task->execute();
730                 $this->output( "Done populating category table.\n" );
731         }
732
733         protected function doPopulateParentId() {
734                 if ( $this->updateRowExists( 'populate rev_parent_id' ) ) {
735                         $this->output( "...rev_parent_id column already populated.\n" );
736                         return;
737                 }
738
739                 $task = $this->maintenance->runChild( 'PopulateParentId' );
740                 $task->execute();
741         }
742
743         protected function doMaybeProfilingMemoryUpdate() {
744                 if ( !$this->db->tableExists( 'profiling' ) ) {
745                         // Simply ignore
746                 } elseif ( $this->db->fieldExists( 'profiling', 'pf_memory' ) ) {
747                         $this->output( "...profiling table has pf_memory field.\n" );
748                 } else {
749                         $this->output( "Adding pf_memory field to table profiling..." );
750                         $this->applyPatch( 'patch-profiling-memory.sql' );
751                         $this->output( "done.\n" );
752                 }
753         }
754
755         protected function doFilearchiveIndicesUpdate() {
756                 $info = $this->db->indexInfo( 'filearchive', 'fa_user_timestamp', __METHOD__ );
757                 if ( !$info ) {
758                         $this->output( "Updating filearchive indices..." );
759                         $this->applyPatch( 'patch-filearchive-user-index.sql' );
760                         $this->output( "done.\n" );
761                 }
762         }
763
764         protected function doUniquePlTlIl() {
765                 $info = $this->db->indexInfo( 'pagelinks', 'pl_namespace' );
766                 if ( is_array( $info ) && !$info[0]->Non_unique ) {
767                         $this->output( "...pl_namespace, tl_namespace, il_to indices are already UNIQUE.\n" );
768                         return;
769                 }
770
771                 $this->output( "Making pl_namespace, tl_namespace and il_to indices UNIQUE... " );
772                 $this->applyPatch( 'patch-pl-tl-il-unique.sql' );
773                 $this->output( "done.\n" );
774         }
775
776         protected function renameEuWikiId() {
777                 if ( $this->db->fieldExists( 'external_user', 'eu_local_id' ) ) {
778                         $this->output( "...eu_wiki_id already renamed to eu_local_id.\n" );
779                         return;
780                 }
781
782                 $this->output( "Renaming eu_wiki_id -> eu_local_id... " );
783                 $this->applyPatch( 'patch-eu_local_id.sql' );
784                 $this->output( "done.\n" );
785         }
786
787         protected function doUpdateMimeMinorField() {
788                 if ( $this->updateRowExists( 'mime_minor_length' ) ) {
789                         $this->output( "...*_mime_minor fields are already long enough.\n" );
790                         return;
791                 }
792
793                 $this->output( "Altering all *_mime_minor fields to 100 bytes in size ... " );
794                 $this->applyPatch( 'patch-mime_minor_length.sql' );
795                 $this->output( "done.\n" );
796         }
797
798         protected function doPopulateRevLen() {
799                 if ( $this->updateRowExists( 'populate rev_len' ) ) {
800                         $this->output( "...rev_len column already populated.\n" );
801                         return;
802                 }
803
804                 $task = $this->maintenance->runChild( 'PopulateRevisionLength' );
805                 $task->execute();
806         }
807
808         protected function doClFieldsUpdate() {
809                 if ( $this->updateRowExists( 'cl_fields_update' ) ) {
810                         $this->output( "...categorylinks up-to-date.\n" );
811                         return;
812                 }
813
814                 $this->output( 'Updating categorylinks (again)...' );
815                 $this->applyPatch( 'patch-categorylinks-better-collation2.sql' );
816                 $this->output( "done.\n" );
817         }
818
819         protected function doLangLinksLengthUpdate() {
820                 $langlinks = $this->db->tableName( 'langlinks' );
821                 $res = $this->db->query( "SHOW COLUMNS FROM $langlinks LIKE 'll_lang'" );
822                 $row = $this->db->fetchObject( $res );
823
824                 if ( $row && $row->Type == "varbinary(10)" ) {
825                         $this->output( 'Updating length of ll_lang in langlinks...' );
826                         $this->applyPatch( 'patch-langlinks-ll_lang-20.sql' );
827                         $this->output( "done.\n" );
828                 } else {
829                         $this->output( "...ll_lang is up-to-date.\n" );
830                 }
831         }
832 }