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