MediaWiki  REL1_19
MysqlUpdater.php
Go to the documentation of this file.
00001 <?php
00015 class MysqlUpdater extends DatabaseUpdater {
00016 
00017         protected function getCoreUpdateList() {
00018                 return array(
00019                         // 1.2
00020                         array( 'addField', 'ipblocks',      'ipb_id',           'patch-ipblocks.sql' ),
00021                         array( 'addField', 'ipblocks',      'ipb_expiry',       'patch-ipb_expiry.sql' ),
00022                         array( 'doInterwikiUpdate' ),
00023                         array( 'doIndexUpdate' ),
00024                         array( 'addTable', 'hitcounter',                        'patch-hitcounter.sql' ),
00025                         array( 'addField', 'recentchanges', 'rc_type',          'patch-rc_type.sql' ),
00026 
00027                         // 1.3
00028                         array( 'addField', 'user',          'user_real_name',   'patch-user-realname.sql' ),
00029                         array( 'addTable', 'querycache',                        'patch-querycache.sql' ),
00030                         array( 'addTable', 'objectcache',                       'patch-objectcache.sql' ),
00031                         array( 'addTable', 'categorylinks',                     'patch-categorylinks.sql' ),
00032                         array( 'doOldLinksUpdate' ),
00033                         array( 'doFixAncientImagelinks' ),
00034                         array( 'addField', 'recentchanges', 'rc_ip',            'patch-rc_ip.sql' ),
00035 
00036                         // 1.4
00037                         array( 'addIndex', 'image',         'PRIMARY',          'patch-image_name_primary.sql' ),
00038                         array( 'addField', 'recentchanges', 'rc_id',            'patch-rc_id.sql' ),
00039                         array( 'addField', 'recentchanges', 'rc_patrolled',     'patch-rc-patrol.sql' ),
00040                         array( 'addTable', 'logging',                           'patch-logging.sql' ),
00041                         array( 'addField', 'user',          'user_token',       'patch-user_token.sql' ),
00042                         array( 'addField', 'watchlist',     'wl_notificationtimestamp', 'patch-email-notification.sql' ),
00043                         array( 'doWatchlistUpdate' ),
00044                         array( 'dropField', 'user',         'user_emailauthenticationtimestamp', 'patch-email-authentication.sql' ),
00045 
00046                         // 1.5
00047                         array( 'doSchemaRestructuring' ),
00048                         array( 'addField', 'logging',       'log_params',       'patch-log_params.sql' ),
00049                         array( 'checkBin', 'logging',       'log_title',        'patch-logging-title.sql', ),
00050                         array( 'addField', 'archive',       'ar_rev_id',        'patch-archive-rev_id.sql' ),
00051                         array( 'addField', 'page',          'page_len',         'patch-page_len.sql' ),
00052                         array( 'dropField', 'revision',     'inverse_timestamp', 'patch-inverse_timestamp.sql' ),
00053                         array( 'addField', 'revision',      'rev_text_id',      'patch-rev_text_id.sql' ),
00054                         array( 'addField', 'revision',      'rev_deleted',      'patch-rev_deleted.sql' ),
00055                         array( 'addField', 'image',         'img_width',        'patch-img_width.sql' ),
00056                         array( 'addField', 'image',         'img_metadata',     'patch-img_metadata.sql' ),
00057                         array( 'addField', 'user',          'user_email_token', 'patch-user_email_token.sql' ),
00058                         array( 'addField', 'archive',       'ar_text_id',       'patch-archive-text_id.sql' ),
00059                         array( 'doNamespaceSize' ),
00060                         array( 'addField', 'image',         'img_media_type',   'patch-img_media_type.sql' ),
00061                         array( 'doPagelinksUpdate' ),
00062                         array( 'dropField', 'image',        'img_type',         'patch-drop_img_type.sql' ),
00063                         array( 'doUserUniqueUpdate' ),
00064                         array( 'doUserGroupsUpdate' ),
00065                         array( 'addField', 'site_stats',    'ss_total_pages',   'patch-ss_total_articles.sql' ),
00066                         array( 'addTable', 'user_newtalk',                      'patch-usernewtalk2.sql' ),
00067                         array( 'addTable', 'transcache',                        'patch-transcache.sql' ),
00068                         array( 'addField', 'interwiki',     'iw_trans',         'patch-interwiki-trans.sql' ),
00069 
00070                         // 1.6
00071                         array( 'doWatchlistNull' ),
00072                         array( 'addIndex', 'logging',         'times',            'patch-logging-times-index.sql' ),
00073                         array( 'addField', 'ipblocks',        'ipb_range_start',  'patch-ipb_range_start.sql' ),
00074                         array( 'doPageRandomUpdate' ),
00075                         array( 'addField', 'user',            'user_registration', 'patch-user_registration.sql' ),
00076                         array( 'doTemplatelinksUpdate' ),
00077                         array( 'addTable', 'externallinks',                       'patch-externallinks.sql' ),
00078                         array( 'addTable', 'job',                                 'patch-job.sql' ),
00079                         array( 'addField', 'site_stats',      'ss_images',        'patch-ss_images.sql' ),
00080                         array( 'addTable', 'langlinks',                           'patch-langlinks.sql' ),
00081                         array( 'addTable', 'querycache_info',                     'patch-querycacheinfo.sql' ),
00082                         array( 'addTable', 'filearchive',                         'patch-filearchive.sql' ),
00083                         array( 'addField', 'ipblocks',        'ipb_anon_only',    'patch-ipb_anon_only.sql' ),
00084                         array( 'addIndex', 'recentchanges',   'rc_ns_usertext',   'patch-recentchanges-utindex.sql' ),
00085                         array( 'addIndex', 'recentchanges',   'rc_user_text',     'patch-rc_user_text-index.sql' ),
00086 
00087                         // 1.9
00088                         array( 'addField', 'user',          'user_newpass_time', 'patch-user_newpass_time.sql' ),
00089                         array( 'addTable', 'redirect',                           'patch-redirect.sql' ),
00090                         array( 'addTable', 'querycachetwo',                      'patch-querycachetwo.sql' ),
00091                         array( 'addField', 'ipblocks',      'ipb_enable_autoblock', 'patch-ipb_optional_autoblock.sql' ),
00092                         array( 'doBacklinkingIndicesUpdate' ),
00093                         array( 'addField', 'recentchanges', 'rc_old_len',        'patch-rc_len.sql' ),
00094                         array( 'addField', 'user',          'user_editcount',    'patch-user_editcount.sql' ),
00095 
00096                         // 1.10
00097                         array( 'doRestrictionsUpdate' ),
00098                         array( 'addField', 'logging',       'log_id',           'patch-log_id.sql' ),
00099                         array( 'addField', 'revision',      'rev_parent_id',    'patch-rev_parent_id.sql' ),
00100                         array( 'addField', 'page_restrictions', 'pr_id',        'patch-page_restrictions_sortkey.sql' ),
00101                         array( 'addField', 'revision',      'rev_len',          'patch-rev_len.sql' ),
00102                         array( 'addField', 'recentchanges', 'rc_deleted',       'patch-rc_deleted.sql' ),
00103                         array( 'addField', 'logging',       'log_deleted',      'patch-log_deleted.sql' ),
00104                         array( 'addField', 'archive',       'ar_deleted',       'patch-ar_deleted.sql' ),
00105                         array( 'addField', 'ipblocks',      'ipb_deleted',      'patch-ipb_deleted.sql' ),
00106                         array( 'addField', 'filearchive',   'fa_deleted',       'patch-fa_deleted.sql' ),
00107                         array( 'addField', 'archive',       'ar_len',           'patch-ar_len.sql' ),
00108 
00109                         // 1.11
00110                         array( 'addField', 'ipblocks',      'ipb_block_email',  'patch-ipb_emailban.sql' ),
00111                         array( 'doCategorylinksIndicesUpdate' ),
00112                         array( 'addField', 'oldimage',      'oi_metadata',      'patch-oi_metadata.sql' ),
00113                         array( 'addIndex', 'archive',       'usertext_timestamp', 'patch-archive-user-index.sql' ),
00114                         array( 'addIndex', 'image',         'img_usertext_timestamp', 'patch-image-user-index.sql' ),
00115                         array( 'addIndex', 'oldimage',      'oi_usertext_timestamp', 'patch-oldimage-user-index.sql' ),
00116                         array( 'addField', 'archive',       'ar_page_id',       'patch-archive-page_id.sql' ),
00117                         array( 'addField', 'image',         'img_sha1',         'patch-img_sha1.sql' ),
00118 
00119                         // 1.12
00120                         array( 'addTable', 'protected_titles',                  'patch-protected_titles.sql' ),
00121 
00122                         // 1.13
00123                         array( 'addField', 'ipblocks',      'ipb_by_text',      'patch-ipb_by_text.sql' ),
00124                         array( 'addTable', 'page_props',                        'patch-page_props.sql' ),
00125                         array( 'addTable', 'updatelog',                         'patch-updatelog.sql' ),
00126                         array( 'addTable', 'category',                          'patch-category.sql' ),
00127                         array( 'doCategoryPopulation' ),
00128                         array( 'addField', 'archive',       'ar_parent_id',     'patch-ar_parent_id.sql' ),
00129                         array( 'addField', 'user_newtalk',  'user_last_timestamp', 'patch-user_last_timestamp.sql' ),
00130                         array( 'doPopulateParentId' ),
00131                         array( 'checkBin', 'protected_titles', 'pt_title',      'patch-pt_title-encoding.sql', ),
00132                         array( 'doMaybeProfilingMemoryUpdate' ),
00133                         array( 'doFilearchiveIndicesUpdate' ),
00134 
00135                         // 1.14
00136                         array( 'addField', 'site_stats',    'ss_active_users',  'patch-ss_active_users.sql' ),
00137                         array( 'doActiveUsersInit' ),
00138                         array( 'addField', 'ipblocks',      'ipb_allow_usertalk', 'patch-ipb_allow_usertalk.sql' ),
00139 
00140                         // 1.15
00141                         array( 'doUniquePlTlIl' ),
00142                         array( 'addTable', 'change_tag',                        'patch-change_tag.sql' ),
00143                         array( 'addTable', 'tag_summary',                       'patch-change_tag.sql' ),
00144                         array( 'addTable', 'valid_tag',                         'patch-change_tag.sql' ),
00145 
00146                         // 1.16
00147                         array( 'addTable', 'user_properties',                   'patch-user_properties.sql' ),
00148                         array( 'addTable', 'log_search',                        'patch-log_search.sql' ),
00149                         array( 'addField', 'logging',       'log_user_text',    'patch-log_user_text.sql' ),
00150                         array( 'doLogUsertextPopulation' ), # listed separately from the previous update because 1.16 was released without this update
00151                         array( 'doLogSearchPopulation' ),
00152                         array( 'addTable', 'l10n_cache',                        'patch-l10n_cache.sql' ),
00153                         array( 'addTable', 'external_user',                     'patch-external_user.sql' ),
00154                         array( 'addIndex', 'log_search',    'ls_field_val',     'patch-log_search-rename-index.sql' ),
00155                         array( 'addIndex', 'change_tag',    'change_tag_rc_tag', 'patch-change_tag-indexes.sql' ),
00156                         array( 'addField', 'redirect',      'rd_interwiki',     'patch-rd_interwiki.sql' ),
00157                         array( 'doUpdateTranscacheField' ),
00158                         array( 'renameEuWikiId' ),
00159                         array( 'doUpdateMimeMinorField' ),
00160 
00161                         // 1.17
00162                         array( 'addTable', 'iwlinks',                           'patch-iwlinks.sql' ),
00163                         array( 'addIndex', 'iwlinks', 'iwl_prefix_title_from',  'patch-rename-iwl_prefix.sql' ),
00164                         array( 'addField', 'updatelog',     'ul_value',         'patch-ul_value.sql' ),
00165                         array( 'addField', 'interwiki',     'iw_api',           'patch-iw_api_and_wikiid.sql' ),
00166                         array( 'dropIndex', 'iwlinks',      'iwl_prefix',       'patch-kill-iwl_prefix.sql' ),
00167                         array( 'dropIndex', 'iwlinks',      'iwl_prefix_from_title', 'patch-kill-iwl_pft.sql' ),
00168                         array( 'addField', 'categorylinks', 'cl_collation',     'patch-categorylinks-better-collation.sql' ),
00169                         array( 'doClFieldsUpdate' ),
00170                         array( 'doCollationUpdate' ),
00171                         array( 'addTable', 'msg_resource',                      'patch-msg_resource.sql' ),
00172                         array( 'addTable', 'module_deps',                       'patch-module_deps.sql' ),
00173                         array( 'dropIndex', 'archive',      'ar_page_revid',    'patch-archive_kill_ar_page_revid.sql' ),
00174                         array( 'addIndex', 'archive',       'ar_revid',         'patch-archive_ar_revid.sql' ),
00175                         array( 'doLangLinksLengthUpdate' ),
00176 
00177                         // 1.18
00178                         array( 'doUserNewTalkTimestampNotNull' ),
00179                         array( 'addIndex', 'user',          'user_email',       'patch-user_email_index.sql' ),
00180                         array( 'modifyField', 'user_properties', 'up_property', 'patch-up_property.sql' ),
00181                         array( 'addTable', 'uploadstash',                       'patch-uploadstash.sql' ),
00182                         array( 'addTable', 'user_former_groups',                'patch-user_former_groups.sql'),
00183 
00184                         // 1.19
00185                         array( 'addIndex', 'logging',       'type_action',      'patch-logging-type-action-index.sql'),
00186                         array( 'doMigrateUserOptions' ),
00187                         array( 'dropField', 'user',         'user_options', 'patch-drop-user_options.sql' ),
00188                         array( 'addField', 'revision',      'rev_sha1',         'patch-rev_sha1.sql' ),
00189                         array( 'addField', 'archive',       'ar_sha1',          'patch-ar_sha1.sql' ),
00190                         array( 'addIndex', 'page', 'page_redirect_namespace_len', 'patch-page_redirect_namespace_len.sql' ),
00191                         array( 'modifyField', 'user_groups', 'ug_group', 'patch-ug_group-length-increase.sql' ),
00192                         array( 'addField',      'uploadstash',  'us_chunk_inx',         'patch-uploadstash_chunk.sql' ),
00193                         array( 'addfield', 'job',           'job_timestamp',    'patch-jobs-add-timestamp.sql' ),
00194                         array( 'modifyField', 'user_former_groups', 'ufg_group', 'patch-ufg_group-length-increase.sql' ),
00195                 );
00196         }
00197 
00206         protected function checkBin( $table, $field, $patchFile ) {
00207                 $tableName = $this->db->tableName( $table );
00208                 $res = $this->db->query( "SELECT $field FROM $tableName LIMIT 0", __METHOD__ );
00209                 $flags = explode( ' ', mysql_field_flags( $res->result, 0 ) );
00210 
00211                 if ( in_array( 'binary', $flags ) ) {
00212                         $this->output( "...$table table has correct $field encoding.\n" );
00213                 } else {
00214                         $this->output( "Fixing $field encoding on $table table... " );
00215                         $this->applyPatch( $patchFile );
00216                         $this->output( "done.\n" );
00217                 }
00218         }
00219 
00228         protected function indexHasField( $table, $index, $field ) {
00229                 $info = $this->db->indexInfo( $table, $index, __METHOD__ );
00230                 if ( $info ) {
00231                         foreach ( $info as $row ) {
00232                                 if ( $row->Column_name == $field ) {
00233                                         $this->output( "...index $index on table $table includes field $field.\n" );
00234                                         return true;
00235                                 }
00236                         }
00237                 }
00238                 $this->output( "...index $index on table $table has no field $field; added.\n" );
00239                 return false;
00240         }
00241 
00245         protected function doInterwikiUpdate() {
00246                 global $IP;
00247 
00248                 if ( $this->db->tableExists( "interwiki", __METHOD__ ) ) {
00249                         $this->output( "...already have interwiki table\n" );
00250                         return;
00251                 }
00252 
00253                 $this->output( 'Creating interwiki table...' );
00254                 $this->applyPatch( 'patch-interwiki.sql' );
00255                 $this->output( "done.\n" );
00256                 $this->output( 'Adding default interwiki definitions...' );
00257                 $this->applyPatch( "$IP/maintenance/interwiki.sql", true );
00258                 $this->output( "done.\n" );
00259         }
00260 
00264         protected function doIndexUpdate() {
00265                 $meta = $this->db->fieldInfo( 'recentchanges', 'rc_timestamp' );
00266                 if ( $meta === false ) {
00267                         throw new MWException( 'Missing rc_timestamp field of recentchanges table. Should not happen.' );
00268                 }
00269                 if ( $meta->isMultipleKey() ) {
00270                         $this->output( "...indexes seem up to 20031107 standards.\n" );
00271                         return;
00272                 }
00273 
00274                 $this->output( "Updating indexes to 20031107..." );
00275                 $this->applyPatch( 'patch-indexes.sql', true );
00276                 $this->output( "done.\n" );
00277         }
00278 
00279         protected function doOldLinksUpdate() {
00280                 $cl = $this->maintenance->runChild( 'ConvertLinks' );
00281                 $cl->execute();
00282         }
00283 
00284         protected function doFixAncientImagelinks() {
00285                 $info = $this->db->fieldInfo( 'imagelinks', 'il_from' );
00286                 if ( !$info || $info->type() !== 'string' ) {
00287                         $this->output( "...il_from OK\n" );
00288                         return;
00289                 }
00290 
00291                 $this->output( "Fixing ancient broken imagelinks table.\n" );
00292                 $this->output( "NOTE: you will have to run maintenance/refreshLinks.php after this.\n" );
00293                 $this->applyPatch( 'patch-fix-il_from.sql' );
00294                 $this->output( "done.\n" );
00295         }
00296 
00300         function doWatchlistUpdate() {
00301                 $talk = $this->db->selectField( 'watchlist', 'count(*)', 'wl_namespace & 1', __METHOD__ );
00302                 $nontalk = $this->db->selectField( 'watchlist', 'count(*)', 'NOT (wl_namespace & 1)', __METHOD__ );
00303                 if ( $talk == $nontalk ) {
00304                         $this->output( "...watchlist talk page rows already present.\n" );
00305                         return;
00306                 }
00307 
00308                 $this->output( "Adding missing watchlist talk page rows... " );
00309                 $this->db->insertSelect( 'watchlist', 'watchlist',
00310                         array(
00311                                 'wl_user' => 'wl_user',
00312                                 'wl_namespace' => 'wl_namespace | 1',
00313                                 'wl_title' => 'wl_title',
00314                                 'wl_notificationtimestamp' => 'wl_notificationtimestamp'
00315                         ), array( 'NOT (wl_namespace & 1)' ), __METHOD__, 'IGNORE' );
00316                 $this->output( "done.\n" );
00317         }
00318 
00319         function doSchemaRestructuring() {
00320                 if ( $this->db->tableExists( 'page', __METHOD__ ) ) {
00321                         $this->output( "...page table already exists.\n" );
00322                         return;
00323                 }
00324 
00325                 $this->output( "...converting from cur/old to page/revision/text DB structure.\n" );
00326                 $this->output( wfTimestamp( TS_DB ) );
00327                 $this->output( "......checking for duplicate entries.\n" );
00328 
00329                 list ( $cur, $old, $page, $revision, $text ) = $this->db->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
00330 
00331                 $rows = $this->db->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
00332                                 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", __METHOD__ );
00333 
00334                 if ( $rows->numRows() > 0 ) {
00335                         $this->output( wfTimestamp( TS_DB ) );
00336                         $this->output( "......<b>Found duplicate entries</b>\n" );
00337                         $this->output( sprintf( "<b>      %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
00338                         $duplicate = array();
00339                         foreach ( $rows as $row ) {
00340                                 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
00341                                         $duplicate[$row->cur_namespace] = array();
00342                                 }
00343                                 $duplicate[$row->cur_namespace][] = $row->cur_title;
00344                                 $this->output( sprintf( "      %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
00345                         }
00346                         $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
00347                         $firstCond = true;
00348                         foreach ( $duplicate as $ns => $titles ) {
00349                                 if ( $firstCond ) {
00350                                         $firstCond = false;
00351                                 } else {
00352                                         $sql .= ' OR ';
00353                                 }
00354                                 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
00355                                 $first = true;
00356                                 foreach ( $titles as $t ) {
00357                                         if ( $first ) {
00358                                                 $sql .= $this->db->addQuotes( $t );
00359                                                 $first = false;
00360                                         } else {
00361                                                 $sql .= ', ' . $this->db->addQuotes( $t );
00362                                         }
00363                                 }
00364                                 $sql .= ") ) \n";
00365                         }
00366                         # By sorting descending, the most recent entry will be the first in the list.
00367                         # All following entries will be deleted by the next while-loop.
00368                         $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
00369 
00370                         $rows = $this->db->query( $sql, __METHOD__ );
00371 
00372                         $prev_title = $prev_namespace = false;
00373                         $deleteId = array();
00374 
00375                         foreach ( $rows as $row ) {
00376                                 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
00377                                         $deleteId[] = $row->cur_id;
00378                                 }
00379                                 $prev_title     = $row->cur_title;
00380                                 $prev_namespace = $row->cur_namespace;
00381                         }
00382                         $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
00383                         $this->db->query( $sql, __METHOD__ );
00384                         $this->output( wfTimestamp( TS_DB ) );
00385                         $this->output( "......<b>Deleted</b> " . $this->db->affectedRows() . " records.\n" );
00386                 }
00387 
00388                 $this->output( wfTimestamp( TS_DB ) );
00389                 $this->output( "......Creating tables.\n" );
00390                 $this->db->query( "CREATE TABLE $page (
00391                         page_id int(8) unsigned NOT NULL auto_increment,
00392                         page_namespace int NOT NULL,
00393                         page_title varchar(255) binary NOT NULL,
00394                         page_restrictions tinyblob NOT NULL,
00395                         page_counter bigint(20) unsigned NOT NULL default '0',
00396                         page_is_redirect tinyint(1) unsigned NOT NULL default '0',
00397                         page_is_new tinyint(1) unsigned NOT NULL default '0',
00398                         page_random real unsigned NOT NULL,
00399                         page_touched char(14) binary NOT NULL default '',
00400                         page_latest int(8) unsigned NOT NULL,
00401                         page_len int(8) unsigned NOT NULL,
00402 
00403                         PRIMARY KEY page_id (page_id),
00404                         UNIQUE INDEX name_title (page_namespace,page_title),
00405                         INDEX (page_random),
00406                         INDEX (page_len)
00407                         ) ENGINE=InnoDB", __METHOD__ );
00408                 $this->db->query( "CREATE TABLE $revision (
00409                         rev_id int(8) unsigned NOT NULL auto_increment,
00410                         rev_page int(8) unsigned NOT NULL,
00411                         rev_comment tinyblob NOT NULL,
00412                         rev_user int(5) unsigned NOT NULL default '0',
00413                         rev_user_text varchar(255) binary NOT NULL default '',
00414                         rev_timestamp char(14) binary NOT NULL default '',
00415                         rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
00416                         rev_deleted tinyint(1) unsigned NOT NULL default '0',
00417                         rev_len int(8) unsigned,
00418                         rev_parent_id int(8) unsigned default NULL,
00419                         PRIMARY KEY rev_page_id (rev_page, rev_id),
00420                         UNIQUE INDEX rev_id (rev_id),
00421                         INDEX rev_timestamp (rev_timestamp),
00422                         INDEX page_timestamp (rev_page,rev_timestamp),
00423                         INDEX user_timestamp (rev_user,rev_timestamp),
00424                         INDEX usertext_timestamp (rev_user_text,rev_timestamp)
00425                         ) ENGINE=InnoDB", __METHOD__ );
00426 
00427                 $this->output( wfTimestamp( TS_DB ) );
00428                 $this->output( "......Locking tables.\n" );
00429                 $this->db->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", __METHOD__ );
00430 
00431                 $maxold = intval( $this->db->selectField( 'old', 'max(old_id)', '', __METHOD__ ) );
00432                 $this->output( wfTimestamp( TS_DB ) );
00433                 $this->output( "......maxold is {$maxold}\n" );
00434 
00435                 $this->output( wfTimestamp( TS_DB ) );
00436                 global $wgLegacySchemaConversion;
00437                 if ( $wgLegacySchemaConversion ) {
00438                         // Create HistoryBlobCurStub entries.
00439                         // Text will be pulled from the leftover 'cur' table at runtime.
00440                         $this->output( "......Moving metadata from cur; using blob references to text in cur table.\n" );
00441                         $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
00442                         $cur_flags = "'object'";
00443                 } else {
00444                         // Copy all cur text in immediately: this may take longer but avoids
00445                         // having to keep an extra table around.
00446                         $this->output( "......Moving text from cur.\n" );
00447                         $cur_text = 'cur_text';
00448                         $cur_flags = "''";
00449                 }
00450                 $this->db->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
00451                                         old_timestamp, old_minor_edit, old_flags)
00452                         SELECT cur_namespace, cur_title, $cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags
00453                         FROM $cur", __METHOD__ );
00454 
00455                 $this->output( wfTimestamp( TS_DB ) );
00456                 $this->output( "......Setting up revision table.\n" );
00457                 $this->db->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
00458                                 rev_minor_edit)
00459                         SELECT old_id, cur_id, old_comment, old_user, old_user_text,
00460                                 old_timestamp, old_minor_edit
00461                         FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", __METHOD__ );
00462 
00463                 $this->output( wfTimestamp( TS_DB ) );
00464                 $this->output( "......Setting up page table.\n" );
00465                 $this->db->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
00466                         page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
00467                         SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
00468                                 cur_random, cur_touched, rev_id, LENGTH(cur_text)
00469                         FROM $cur,$revision
00470                         WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", __METHOD__ );
00471 
00472                 $this->output( wfTimestamp( TS_DB ) );
00473                 $this->output( "......Unlocking tables.\n" );
00474                 $this->db->query( "UNLOCK TABLES", __METHOD__ );
00475 
00476                 $this->output( wfTimestamp( TS_DB ) );
00477                 $this->output( "......Renaming old.\n" );
00478                 $this->db->query( "ALTER TABLE $old RENAME TO $text", __METHOD__ );
00479 
00480                 $this->output( wfTimestamp( TS_DB ) );
00481                 $this->output( "...done.\n" );
00482         }
00483 
00484         protected function doNamespaceSize() {
00485                 $tables = array(
00486                         'page'          => 'page',
00487                         'archive'       => 'ar',
00488                         'recentchanges' => 'rc',
00489                         'watchlist'     => 'wl',
00490                         'querycache'    => 'qc',
00491                         'logging'       => 'log',
00492                 );
00493                 foreach ( $tables as $table => $prefix ) {
00494                         $field = $prefix . '_namespace';
00495 
00496                         $tablename = $this->db->tableName( $table );
00497                         $result = $this->db->query( "SHOW COLUMNS FROM $tablename LIKE '$field'", __METHOD__ );
00498                         $info = $this->db->fetchObject( $result );
00499 
00500                         if ( substr( $info->Type, 0, 3 ) == 'int' ) {
00501                                 $this->output( "...$field is already a full int ($info->Type).\n" );
00502                         } else {
00503                                 $this->output( "Promoting $field from $info->Type to int... " );
00504                                 $this->db->query( "ALTER TABLE $tablename MODIFY $field int NOT NULL", __METHOD__ );
00505                                 $this->output( "done.\n" );
00506                         }
00507                 }
00508         }
00509 
00510         protected function doPagelinksUpdate() {
00511                 if ( $this->db->tableExists( 'pagelinks', __METHOD__ ) ) {
00512                         $this->output( "...already have pagelinks table.\n" );
00513                         return;
00514                 }
00515 
00516                 $this->output( "Converting links and brokenlinks tables to pagelinks... " );
00517                 $this->applyPatch( 'patch-pagelinks.sql' );
00518                 $this->output( "done.\n" );
00519 
00520                 global $wgContLang;
00521                 foreach ( MWNamespace::getCanonicalNamespaces() as $ns => $name ) {
00522                         if ( $ns == 0 ) {
00523                                 continue;
00524                         }
00525 
00526                         $this->output( "Cleaning up broken links for namespace $ns... " );
00527 
00528                         $pagelinks = $this->db->tableName( 'pagelinks' );
00529                         $name = $wgContLang->getNsText( $ns );
00530                         $prefix = $this->db->strencode( $name );
00531                         $likeprefix = str_replace( '_', '\\_', $prefix );
00532 
00533                         $sql = "UPDATE $pagelinks
00534                                            SET pl_namespace=$ns,
00535                                                    pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
00536                                          WHERE pl_namespace=0
00537                                            AND pl_title LIKE '$likeprefix:%'";
00538 
00539                         $this->db->query( $sql, __METHOD__ );
00540                         $this->output( "done.\n" );
00541                 }
00542         }
00543 
00544         protected function doUserUniqueUpdate() {
00545                 $duper = new UserDupes( $this->db, array( $this, 'output' ) );
00546                 if ( $duper->hasUniqueIndex() ) {
00547                         $this->output( "...already have unique user_name index.\n" );
00548                         return;
00549                 }
00550 
00551                 if ( !$duper->clearDupes() ) {
00552                         $this->output( "WARNING: This next step will probably fail due to unfixed duplicates...\n" );
00553                 }
00554                 $this->output( "Adding unique index on user_name... " );
00555                 $this->applyPatch( 'patch-user_nameindex.sql' );
00556                 $this->output( "done.\n" );
00557         }
00558 
00559         protected function doUserGroupsUpdate() {
00560                 if ( $this->db->tableExists( 'user_groups', __METHOD__ ) ) {
00561                         $info = $this->db->fieldInfo( 'user_groups', 'ug_group' );
00562                         if ( $info->type() == 'int' ) {
00563                                 $oldug = $this->db->tableName( 'user_groups' );
00564                                 $newug = $this->db->tableName( 'user_groups_bogus' );
00565                                 $this->output( "user_groups table exists but is in bogus intermediate format. Renaming to $newug... " );
00566                                 $this->db->query( "ALTER TABLE $oldug RENAME TO $newug", __METHOD__ );
00567                                 $this->output( "done.\n" );
00568 
00569                                 $this->output( "Re-adding fresh user_groups table... " );
00570                                 $this->applyPatch( 'patch-user_groups.sql' );
00571                                 $this->output( "done.\n" );
00572 
00573                                 $this->output( "***\n" );
00574                                 $this->output( "*** WARNING: You will need to manually fix up user permissions in the user_groups\n" );
00575                                 $this->output( "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n" );
00576                                 $this->output( "***\n" );
00577                         } else {
00578                                 $this->output( "...user_groups table exists and is in current format.\n" );
00579                         }
00580                         return;
00581                 }
00582 
00583                 $this->output( "Adding user_groups table... " );
00584                 $this->applyPatch( 'patch-user_groups.sql' );
00585                 $this->output( "done.\n" );
00586 
00587                 if ( !$this->db->tableExists( 'user_rights', __METHOD__ ) ) {
00588                         if ( $this->db->fieldExists( 'user', 'user_rights', __METHOD__ ) ) {
00589                                 $this->output( "Upgrading from a 1.3 or older database? Breaking out user_rights for conversion..." );
00590                                 $this->db->applyPatch( 'patch-user_rights.sql' );
00591                                 $this->output( "done.\n" );
00592                         } else {
00593                                 $this->output( "*** WARNING: couldn't locate user_rights table or field for upgrade.\n" );
00594                                 $this->output( "*** You may need to manually configure some sysops by manipulating\n" );
00595                                 $this->output( "*** the user_groups table.\n" );
00596                                 return;
00597                         }
00598                 }
00599 
00600                 $this->output( "Converting user_rights table to user_groups... " );
00601                 $result = $this->db->select( 'user_rights',
00602                         array( 'ur_user', 'ur_rights' ),
00603                         array( "ur_rights != ''" ),
00604                         __METHOD__ );
00605 
00606                 foreach ( $result as $row ) {
00607                         $groups = array_unique(
00608                                 array_map( 'trim',
00609                                         explode( ',', $row->ur_rights ) ) );
00610 
00611                         foreach ( $groups as $group ) {
00612                                 $this->db->insert( 'user_groups',
00613                                         array(
00614                                                 'ug_user'  => $row->ur_user,
00615                                                 'ug_group' => $group ),
00616                                         __METHOD__ );
00617                         }
00618                 }
00619                 $this->output( "done.\n" );
00620         }
00621 
00626         protected function doWatchlistNull() {
00627                 $info = $this->db->fieldInfo( 'watchlist', 'wl_notificationtimestamp' );
00628                 if ( $info->isNullable() ) {
00629                         $this->output( "...wl_notificationtimestamp is already nullable.\n" );
00630                         return;
00631                 }
00632 
00633                 $this->output( "Making wl_notificationtimestamp nullable... " );
00634                 $this->applyPatch( 'patch-watchlist-null.sql' );
00635                 $this->output( "done.\n" );
00636         }
00637 
00643         protected function doPageRandomUpdate() {
00644                 $page = $this->db->tableName( 'page' );
00645                 $this->db->query( "UPDATE $page SET page_random = RAND() WHERE page_random = 0", __METHOD__ );
00646                 $rows = $this->db->affectedRows();
00647 
00648                 if( $rows ) {
00649                         $this->output( "Set page_random to a random value on $rows rows where it was set to 0\n" );
00650                 } else {
00651                         $this->output( "...no page_random rows needed to be set\n" );
00652                 }
00653         }
00654 
00655         protected function doTemplatelinksUpdate() {
00656                 if ( $this->db->tableExists( 'templatelinks', __METHOD__ ) ) {
00657                         $this->output( "...templatelinks table already exists\n" );
00658                         return;
00659                 }
00660 
00661                 $this->output( "Creating templatelinks table...\n" );
00662                 $this->applyPatch( 'patch-templatelinks.sql' );
00663                 $this->output( "Populating...\n" );
00664                 if ( wfGetLB()->getServerCount() > 1 ) {
00665                         // Slow, replication-friendly update
00666                         $res = $this->db->select( 'pagelinks', array( 'pl_from', 'pl_namespace', 'pl_title' ),
00667                                 array( 'pl_namespace' => NS_TEMPLATE ), __METHOD__ );
00668                         $count = 0;
00669                         foreach ( $res as $row ) {
00670                                 $count = ( $count + 1 ) % 100;
00671                                 if ( $count == 0 ) {
00672                                         wfWaitForSlaves();
00673                                 }
00674                                 $this->db->insert( 'templatelinks',
00675                                         array(
00676                                                 'tl_from' => $row->pl_from,
00677                                                 'tl_namespace' => $row->pl_namespace,
00678                                                 'tl_title' => $row->pl_title,
00679                                         ), __METHOD__
00680                                 );
00681 
00682                         }
00683                 } else {
00684                         // Fast update
00685                         $this->db->insertSelect( 'templatelinks', 'pagelinks',
00686                                 array(
00687                                         'tl_from' => 'pl_from',
00688                                         'tl_namespace' => 'pl_namespace',
00689                                         'tl_title' => 'pl_title'
00690                                 ), array(
00691                                         'pl_namespace' => 10
00692                                 ), __METHOD__
00693                         );
00694                 }
00695                 $this->output( "Done. Please run maintenance/refreshLinks.php for a more thorough templatelinks update.\n" );
00696         }
00697 
00698         protected function doBacklinkingIndicesUpdate() {
00699                 if ( !$this->indexHasField( 'pagelinks', 'pl_namespace', 'pl_from' ) ||
00700                         !$this->indexHasField( 'templatelinks', 'tl_namespace', 'tl_from' ) ||
00701                         !$this->indexHasField( 'imagelinks', 'il_to', 'il_from' ) )
00702                 {
00703                         $this->applyPatch( 'patch-backlinkindexes.sql' );
00704                         $this->output( "...backlinking indices updated\n" );
00705                 }
00706         }
00707 
00713         protected function doRestrictionsUpdate() {
00714                 if ( $this->db->tableExists( 'page_restrictions', __METHOD__ ) ) {
00715                         $this->output( "...page_restrictions table already exists.\n" );
00716                         return;
00717                 }
00718 
00719                 $this->output( "Creating page_restrictions table..." );
00720                 $this->applyPatch( 'patch-page_restrictions.sql' );
00721                 $this->applyPatch( 'patch-page_restrictions_sortkey.sql' );
00722                 $this->output( "done.\n" );
00723 
00724                 $this->output( "Migrating old restrictions to new table...\n" );
00725                 $task = $this->maintenance->runChild( 'UpdateRestrictions' );
00726                 $task->execute();
00727         }
00728 
00729         protected function doCategorylinksIndicesUpdate() {
00730                 if ( !$this->indexHasField( 'categorylinks', 'cl_sortkey', 'cl_from' ) ) {
00731                         $this->applyPatch( 'patch-categorylinksindex.sql' );
00732                         $this->output( "...categorylinks indices updated\n" );
00733                 }
00734         }
00735 
00736         protected function doCategoryPopulation() {
00737                 if ( $this->updateRowExists( 'populate category' ) ) {
00738                         $this->output( "...category table already populated.\n" );
00739                         return;
00740                 }
00741 
00742                 $this->output(
00743                         "Populating category table, printing progress markers. " .
00744                         "For large databases, you\n" .
00745                         "may want to hit Ctrl-C and do this manually with maintenance/\n" .
00746                         "populateCategory.php.\n"
00747                 );
00748                 $task = $this->maintenance->runChild( 'PopulateCategory' );
00749                 $task->execute();
00750                 $this->output( "Done populating category table.\n" );
00751         }
00752 
00753         protected function doPopulateParentId() {
00754                 if ( !$this->updateRowExists( 'populate rev_parent_id' ) ) {
00755                         $this->output(
00756                                 "Populating rev_parent_id fields, printing progress markers. For large\n" .
00757                                 "databases, you may want to hit Ctrl-C and do this manually with\n" .
00758                                 "maintenance/populateParentId.php.\n" );
00759 
00760                         $task = $this->maintenance->runChild( 'PopulateParentId' );
00761                         $task->execute();
00762                 }
00763         }
00764 
00765         protected function doMaybeProfilingMemoryUpdate() {
00766                 if ( !$this->db->tableExists( 'profiling', __METHOD__ ) ) {
00767                         // Simply ignore
00768                 } elseif ( $this->db->fieldExists( 'profiling', 'pf_memory', __METHOD__ ) ) {
00769                         $this->output( "...profiling table has pf_memory field.\n" );
00770                 } else {
00771                         $this->output( "Adding pf_memory field to table profiling..." );
00772                         $this->applyPatch( 'patch-profiling-memory.sql' );
00773                         $this->output( "done.\n" );
00774                 }
00775         }
00776 
00777         protected function doFilearchiveIndicesUpdate() {
00778                 $info = $this->db->indexInfo( 'filearchive', 'fa_user_timestamp', __METHOD__ );
00779                 if ( !$info ) {
00780                         $this->output( "Updating filearchive indices..." );
00781                         $this->applyPatch( 'patch-filearchive-user-index.sql' );
00782                         $this->output( "done.\n" );
00783                 }
00784         }
00785 
00786         protected function doUniquePlTlIl() {
00787                 $info = $this->db->indexInfo( 'pagelinks', 'pl_namespace' );
00788                 if ( is_array( $info ) && !$info[0]->Non_unique ) {
00789                         $this->output( "...pl_namespace, tl_namespace, il_to indices are already UNIQUE.\n" );
00790                         return;
00791                 }
00792 
00793                 $this->output( "Making pl_namespace, tl_namespace and il_to indices UNIQUE... " );
00794                 $this->applyPatch( 'patch-pl-tl-il-unique.sql' );
00795                 $this->output( "done.\n" );
00796         }
00797 
00798         protected function renameEuWikiId() {
00799                 if ( $this->db->fieldExists( 'external_user', 'eu_local_id', __METHOD__ ) ) {
00800                         $this->output( "...eu_wiki_id already renamed to eu_local_id.\n" );
00801                         return;
00802                 }
00803 
00804                 $this->output( "Renaming eu_wiki_id -> eu_local_id... " );
00805                 $this->applyPatch( 'patch-eu_local_id.sql' );
00806                 $this->output( "done.\n" );
00807         }
00808 
00809         protected function doUpdateMimeMinorField() {
00810                 if ( $this->updateRowExists( 'mime_minor_length' ) ) {
00811                         $this->output( "...*_mime_minor fields are already long enough.\n" );
00812                         return;
00813                 }
00814 
00815                 $this->output( "Altering all *_mime_minor fields to 100 bytes in size ... " );
00816                 $this->applyPatch( 'patch-mime_minor_length.sql' );
00817                 $this->output( "done.\n" );
00818         }
00819 
00820         protected function doClFieldsUpdate() {
00821                 if ( $this->updateRowExists( 'cl_fields_update' ) ) {
00822                         $this->output( "...categorylinks up-to-date.\n" );
00823                         return;
00824                 }
00825 
00826                 $this->output( 'Updating categorylinks (again)...' );
00827                 $this->applyPatch( 'patch-categorylinks-better-collation2.sql' );
00828                 $this->output( "done.\n" );
00829         }
00830 
00831         protected function doLangLinksLengthUpdate() {
00832                 $langlinks = $this->db->tableName( 'langlinks' );
00833                 $res = $this->db->query( "SHOW COLUMNS FROM $langlinks LIKE 'll_lang'" );
00834                 $row = $this->db->fetchObject( $res );
00835 
00836                 if ( $row && $row->Type == "varbinary(10)" ) {
00837                         $this->output( 'Updating length of ll_lang in langlinks...' );
00838                         $this->applyPatch( 'patch-langlinks-ll_lang-20.sql' );
00839                         $this->output( "done.\n" );
00840                 } else {
00841                         $this->output( "...ll_lang is up-to-date.\n" );
00842                 }
00843         }
00844 
00845         protected function doUserNewTalkTimestampNotNull() {
00846                 $info = $this->db->fieldInfo( 'user_newtalk', 'user_last_timestamp' );
00847                 if ( $info->isNullable() ) {
00848                         $this->output( "...user_last_timestamp is already nullable.\n" );
00849                         return;
00850                 }
00851 
00852                 $this->output( "Making user_last_timestamp nullable... " );
00853                 $this->applyPatch( 'patch-user-newtalk-timestamp-null.sql' );
00854                 $this->output( "done.\n" );
00855         }
00856 }