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