31 require_once __DIR__ .
'/../../maintenance/Maintenance.php';
85 DeleteDefaultMessages::class,
86 PopulateRevisionLength::class,
87 PopulateRevisionSha1::class,
88 PopulateImageSha1::class,
89 FixExtLinksProtocolRelative::class,
90 PopulateFilearchiveSha1::class,
91 PopulateBacklinkNamespace::class,
92 FixDefaultJsonContentPages::class,
93 CleanupEmptyCategories::class,
94 AddRFCandPMIDInterwiki::class,
95 PopulatePPSortKey::class,
96 PopulateIpChanges::class,
97 RefreshExternallinksIndex::class,
142 (
new HookRunner( $hookContainer ) )->onLoadExtensionSchemaUpdates( $this );
152 if ( $this->autoExtensionHookContainer ) {
156 if ( defined(
'MW_EXTENSIONS_LOADED' ) ) {
157 throw new Exception( __METHOD__ .
158 ' apparently called from installer but no hook container was injected' );
160 if ( !defined(
'MEDIAWIKI_INSTALL' ) ) {
162 return MediaWikiServices::getInstance()->getHookContainer();
167 $queue = $registry->getQueue();
169 $registry->clearQueue();
172 $data = $registry->readFromQueue(
$queue );
176 $legacySchemaHooks = $data[
'globals'][
'wgHooks'][
'LoadExtensionSchemaUpdates'] ?? [];
177 if ( $vars && isset( $vars[
'wgHooks'][
'LoadExtensionSchemaUpdates'] ) ) {
178 $legacySchemaHooks = array_merge( $legacySchemaHooks, $vars[
'wgHooks'][
'LoadExtensionSchemaUpdates'] );
183 if ( $vars && isset( $vars[
'wgAutoloadClasses'] ) ) {
189 [
'LoadExtensionSchemaUpdates' => $legacySchemaHooks ],
190 $data[
'attributes'][
'Hooks'] ?? [],
191 $data[
'attributes'][
'DeprecatedHooks'] ?? []
193 MediaWikiServices::getInstance()->getObjectFactory()
212 $class = ucfirst(
$type ) .
'Updater';
216 throw new MWException( __METHOD__ .
' called for unsupported $wgDBtype' );
228 $this->autoExtensionHookContainer = $hookContainer;
247 if ( $this->maintenance->isQuiet() ) {
252 $str = htmlspecialchars( $str );
271 $this->extensionUpdates[] = $update;
285 $this->extensionUpdates[] = [
'addTable', $tableName, $sqlPath,
true ];
299 $this->extensionUpdates[] = [
'addIndex', $tableName, $indexName, $sqlPath,
true ];
313 $this->extensionUpdates[] = [
'addField', $tableName, $columnName, $sqlPath,
true ];
327 $this->extensionUpdates[] = [
'dropField', $tableName, $columnName, $sqlPath,
true ];
341 $this->extensionUpdates[] = [
'dropIndex', $tableName, $indexName, $sqlPath,
true ];
354 $this->extensionUpdates[] = [
'dropTable', $tableName, $sqlPath,
true ];
371 $sqlPath, $skipBothIndexExistWarning =
false
373 $this->extensionUpdates[] = [
378 $skipBothIndexExistWarning,
395 $this->extensionUpdates[] = [
'modifyField', $tableName, $fieldName, $sqlPath,
true ];
408 $this->extensionUpdates[] = [
'modifyTable', $tableName, $sqlPath,
true ];
418 return ( $this->db->tableExists( $tableName, __METHOD__ ) );
431 $this->postDatabaseUpdateMaintenance[] = $class;
460 $this->updatesSkipped = [];
466 $this->updatesSkipped[] = $origParams;
490 public function doUpdates( array $what = [
'core',
'extensions',
'stats' ] ) {
493 $what = array_flip( $what );
494 $this->skipSchema = isset( $what[
'noschema'] ) || $this->fileHandle !==
null;
495 if ( isset( $what[
'core'] ) ) {
498 if ( isset( $what[
'extensions'] ) ) {
503 if ( isset( $what[
'stats'] ) ) {
507 if ( $this->fileHandle ) {
508 $this->skipSchema =
false;
520 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
525 $origParams = $params;
526 $func = array_shift( $params );
527 if ( !is_array( $func ) && method_exists( $this, $func ) ) {
528 $func = [ $this, $func ];
529 } elseif ( $passSelf ) {
530 array_unshift( $params, $this );
532 $ret = $func( ...$params );
534 if ( $ret !==
false ) {
535 $updatesDone[] = $origParams;
536 $lbFactory->waitForReplication( [
'timeout' => self::REPLICATION_WAIT_TIMEOUT ] );
541 $this->updatesSkipped = array_merge( $this->updatesSkipped,
$updatesSkipped );
542 $this->updates = array_merge( $this->updates, $updatesDone );
553 $row = $this->db->selectRow(
557 [
'ul_key' => $key ],
579 $values = [
'ul_key' => $key ];
581 $values[
'ul_value'] = $val;
583 $this->db->insert(
'updatelog', $values, __METHOD__, [
'IGNORE' ] );
596 return $this->db->tableExists(
'updatelog', __METHOD__ ) &&
597 $this->db->fieldExists(
'updatelog',
'ul_value', __METHOD__ );
618 $this->
output(
"...skipping update to shared table $name.\n" );
642 $this->db->sourceFile(
647 [ $this,
'appendLine' ]
665 if ( fwrite( $this->fileHandle,
$line ) ===
false ) {
684 if ( $msg ===
null ) {
685 $msg =
"Applying $path patch";
687 if ( $this->skipSchema ) {
688 $this->
output(
"...skipping schema change ($msg).\n" );
693 $this->
output(
"$msg ..." );
695 if ( !$isFullPath ) {
698 if ( $this->fileHandle !==
null ) {
701 $this->db->sourceFile(
$path );
703 $this->
output(
"done.\n" );
720 if ( file_exists(
"$IP/maintenance/$dbType/archives/$patch" ) ) {
721 return "$IP/maintenance/$dbType/archives/$patch";
723 return "$IP/maintenance/archives/$patch";
738 protected function addTable( $name, $patch, $fullpath =
false ) {
739 if ( !$this->
doTable( $name ) ) {
743 if ( $this->db->tableExists( $name, __METHOD__ ) ) {
744 $this->
output(
"...$name table already exists.\n" );
746 return $this->
applyPatch( $patch, $fullpath,
"Creating $name table" );
764 protected function addField( $table, $field, $patch, $fullpath =
false ) {
765 if ( !$this->
doTable( $table ) ) {
769 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
770 $this->
output(
"...$table table does not exist, skipping new field patch.\n" );
771 } elseif ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
772 $this->
output(
"...have $field field in $table table.\n" );
774 return $this->
applyPatch( $patch, $fullpath,
"Adding $field field to table $table" );
792 protected function addIndex( $table, $index, $patch, $fullpath =
false ) {
793 if ( !$this->
doTable( $table ) ) {
797 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
798 $this->
output(
"...skipping: '$table' table doesn't exist yet.\n" );
799 } elseif ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
800 $this->
output(
"...index $index already set on $table table.\n" );
802 return $this->
applyPatch( $patch, $fullpath,
"Adding index $index to table $table" );
819 if ( !$this->
doTable( $table ) ) {
823 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
824 $this->
output(
"...skipping: '$table' table doesn't exist yet.\n" );
828 $newIndex = $indexes[0];
829 foreach ( $indexes as $index ) {
830 if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
832 "...skipping index $newIndex because index $index already set on $table table.\n"
838 return $this->
applyPatch( $patch, $fullpath,
"Adding index $index to table $table" );
853 protected function dropField( $table, $field, $patch, $fullpath =
false ) {
854 if ( !$this->
doTable( $table ) ) {
858 if ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
859 return $this->
applyPatch( $patch, $fullpath,
"Table $table contains $field field. Dropping" );
861 $this->
output(
"...$table table does not contain $field field.\n" );
879 protected function dropIndex( $table, $index, $patch, $fullpath =
false ) {
880 if ( !$this->
doTable( $table ) ) {
884 if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
885 return $this->
applyPatch( $patch, $fullpath,
"Dropping $index index from table $table" );
887 $this->
output(
"...$index key doesn't exist.\n" );
910 $skipBothIndexExistWarning, $patch, $fullpath =
false
912 if ( !$this->
doTable( $table ) ) {
917 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
918 $this->
output(
"...skipping: '$table' table doesn't exist yet.\n" );
924 if ( $this->db->indexExists( $table, $newIndex, __METHOD__ ) ) {
925 $this->
output(
"...index $newIndex already set on $table table.\n" );
926 if ( !$skipBothIndexExistWarning &&
927 $this->db->indexExists( $table, $oldIndex, __METHOD__ )
929 $this->
output(
"...WARNING: $oldIndex still exists, despite it has " .
930 "been renamed into $newIndex (which also exists).\n" .
931 " $oldIndex should be manually removed if not needed anymore.\n" );
938 if ( !$this->db->indexExists( $table, $oldIndex, __METHOD__ ) ) {
939 $this->
output(
"...skipping: index $oldIndex doesn't exist.\n" );
948 "Renaming index $oldIndex into $newIndex to table $table"
966 protected function dropTable( $table, $patch =
false, $fullpath =
false ) {
967 if ( !$this->
doTable( $table ) ) {
971 if ( $this->db->tableExists( $table, __METHOD__ ) ) {
972 $msg =
"Dropping table $table";
974 if ( $patch ===
false ) {
975 $this->
output(
"$msg ..." );
976 $this->db->dropTable( $table, __METHOD__ );
977 $this->
output(
"done.\n" );
979 return $this->
applyPatch( $patch, $fullpath, $msg );
982 $this->
output(
"...$table doesn't exist.\n" );
1002 protected function modifyField( $table, $field, $patch, $fullpath =
false ) {
1003 if ( !$this->
doTable( $table ) ) {
1007 $updateKey =
"$table-$field-$patch";
1008 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
1009 $this->
output(
"...$table table does not exist, skipping modify field patch.\n" );
1010 } elseif ( !$this->db->fieldExists( $table, $field, __METHOD__ ) ) {
1011 $this->
output(
"...$field field does not exist in $table table, " .
1012 "skipping modify field patch.\n" );
1014 $this->
output(
"...$field in table $table already modified by patch $patch.\n" );
1016 $apply = $this->
applyPatch( $patch, $fullpath,
"Modifying $field field of table $table" );
1040 if ( !$this->
doTable( $table ) ) {
1044 $updateKey =
"$table-$patch";
1045 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
1046 $this->
output(
"...$table table does not exist, skipping modify table patch.\n" );
1048 $this->
output(
"...table $table already modified by patch $patch.\n" );
1050 $apply = $this->
applyPatch( $patch, $fullpath,
"Modifying table $table" );
1080 $this->
output(
"Running $script...\n" );
1081 $task = $this->maintenance->runChild( $class );
1082 $ok = $task->execute();
1084 throw new RuntimeException(
"Execution of $script did not complete successfully." );
1086 $this->
output(
"done.\n" );
1095 $repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
1096 $zonePath = $repo->getZonePath(
'temp' );
1097 if ( $repo->getBackend()->directoryExists( [
'dir' => $zonePath ] ) ) {
1099 $status = $repo->getBackend()->secure( [
1104 if ( $status->isOK() ) {
1105 $this->
output(
"Set the local repo temp zone container to be private.\n" );
1107 $this->
output(
"Failed to set the local repo temp zone container to be private.\n" );
1119 $this->
output(
"Purging caches..." );
1122 $this->db->delete(
'objectcache',
'*', __METHOD__ );
1130 $services = MediaWikiServices::getInstance();
1132 $services->getResourceLoader(),
1134 $services->getMainWANObjectCache()
1136 $blobStore->clear();
1139 $this->db->delete(
'module_deps',
'*', __METHOD__ );
1140 $this->
output(
"done.\n" );
1147 $this->
output(
"...site_stats is populated..." );
1148 $row = $this->db->selectRow(
'site_stats',
'*', [
'ss_row_id' => 1 ], __METHOD__ );
1149 if ( $row ===
false ) {
1150 $this->
output(
"data is missing! rebuilding...\n" );
1151 } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
1152 $this->
output(
"missing ss_total_pages, rebuilding...\n" );
1154 $this->
output(
"done.\n" );
1161 # Common updater functions
1167 $activeUsers = $this->db->selectField(
'site_stats',
'ss_active_users',
'', __METHOD__ );
1168 if ( $activeUsers == -1 ) {
1169 $activeUsers = $this->db->selectField(
'recentchanges',
1170 'COUNT( DISTINCT rc_user_text )',
1171 [
'rc_user != 0',
'rc_bot' => 0,
"rc_log_type != 'newusers'" ], __METHOD__
1173 $this->db->update(
'site_stats',
1174 [
'ss_active_users' => intval( $activeUsers ) ],
1175 [
'ss_row_id' => 1 ], __METHOD__, [
'LIMIT' => 1 ]
1178 $this->
output(
"...ss_active_users user count set...\n" );
1187 "Populating log_user_text field, printing progress markers. For large\n" .
1188 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1189 "maintenance/populateLogUsertext.php.\n"
1192 $task = $this->maintenance->runChild( PopulateLogUsertext::class );
1194 $this->
output(
"done.\n" );
1204 "Populating log_search table, printing progress markers. For large\n" .
1205 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1206 "maintenance/populateLogSearch.php.\n" );
1208 $task = $this->maintenance->runChild( PopulateLogSearch::class );
1210 $this->
output(
"done.\n" );
1219 if ( $this->db->fieldExists(
'categorylinks',
'cl_collation', __METHOD__ ) ) {
1220 if ( $this->db->selectField(
1227 $this->
output(
"...collations up-to-date.\n" );
1232 $this->
output(
"Updating category collations..." );
1233 $task = $this->maintenance->runChild( UpdateCollation::class );
1235 $this->
output(
"...done.\n" );
1243 if ( $this->db->tableExists(
'user_properties', __METHOD__ ) ) {
1244 $cl = $this->maintenance->runChild( ConvertUserOptions::class,
'convertUserOptions.php' );
1246 $this->
output(
"done.\n" );
1257 $cl = $this->maintenance->runChild(
1258 RebuildLocalisationCache::class,
'rebuildLocalisationCache.php'
1260 '@phan-var RebuildLocalisationCache $cl';
1261 $this->
output(
"Rebuilding localisation cache...\n" );
1264 $this->
output(
"done.\n" );
1274 "Migrating comments to the 'comments' table, printing progress markers. For large\n" .
1275 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1276 "maintenance/migrateComments.php.\n"
1278 $task = $this->maintenance->runChild( MigrateComments::class,
'migrateComments.php' );
1279 $ok = $task->execute();
1280 $this->
output( $ok ?
"done.\n" :
"errors were encountered.\n" );
1289 if ( $this->
tableExists(
'image_comment_temp' ) ) {
1290 $this->
output(
"Merging image_comment_temp into the image table\n" );
1291 $task = $this->maintenance->runChild(
1292 MigrateImageCommentTemp::class,
'migrateImageCommentTemp.php'
1296 $ok = $task->execute();
1297 $this->
output( $ok ?
"done.\n" :
"errors were encountered.\n" );
1299 $this->
dropTable(
'image_comment_temp' );
1311 "Migrating actors to the 'actor' table, printing progress markers. For large\n" .
1312 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1313 "maintenance/migrateActors.php.\n"
1315 $task = $this->maintenance->runChild(
'MigrateActors',
'migrateActors.php' );
1316 $ok = $task->execute();
1317 $this->
output( $ok ?
"done.\n" :
"errors were encountered.\n" );
1326 if ( $this->db->fieldExists(
'archive',
'ar_text', __METHOD__ ) ) {
1327 $this->
output(
"Migrating archive ar_text to modern storage.\n" );
1328 $task = $this->maintenance->runChild( MigrateArchiveText::class,
'migrateArchiveText.php' );
1331 if ( $task->execute() ) {
1332 $this->
applyPatch(
'patch-drop-ar_text.sql',
false,
1333 'Dropping ar_text and ar_flags columns' );
1343 $info = $this->db->fieldInfo(
'archive',
'ar_rev_id' );
1345 throw new MWException(
'Missing ar_rev_id field of archive table. Should not happen.' );
1347 if ( $info->isNullable() ) {
1348 $this->
output(
"Populating ar_rev_id.\n" );
1349 $task = $this->maintenance->runChild(
'PopulateArchiveRevId',
'populateArchiveRevId.php' );
1350 if ( $task->execute() ) {
1351 $this->
applyPatch(
'patch-ar_rev_id-not-null.sql',
false,
1352 'Making ar_rev_id not nullable' );
1362 if ( !$this->
updateRowExists(
'populate externallinks.el_index_60' ) ) {
1364 "Populating el_index_60 field, printing progress markers. For large\n" .
1365 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1366 "maintenance/populateExternallinksIndex60.php.\n"
1368 $task = $this->maintenance->runChild(
'PopulateExternallinksIndex60',
1369 'populateExternallinksIndex60.php' );
1371 $this->
output(
"done.\n" );
1382 "Migrating revision data to the MCR 'slot' and 'content' tables, printing progress markers.\n" .
1383 "For large databases, you may want to hit Ctrl-C and do this manually with\n" .
1384 "maintenance/populateContentTables.php.\n"
1386 $task = $this->maintenance->runChild(
1387 PopulateContentTables::class,
'populateContentTables.php'
1389 $ok = $task->execute();
1390 $this->
output( $ok ?
"done.\n" :
"errors were encountered.\n" );
1421 if ( $table === $this ) {
1424 $func = array_shift( $params );
1427 if ( $this->db->tableExists( $table, __METHOD__ ) ) {
1431 if ( !is_array( $func ) && method_exists( $this, $func ) ) {
1432 $func = [ $this, $func ];
1433 } elseif ( $passSelf ) {
1434 array_unshift( $params, $this );
1438 return $func( ...$params );
1458 if ( $table === $this ) {
1462 $func = array_shift( $params );
1465 if ( !$this->db->tableExists( $table, __METHOD__ ) ||
1466 !$this->db->fieldExists( $table, $field, __METHOD__ )
1471 if ( !is_array( $func ) && method_exists( $this, $func ) ) {
1472 $func = [ $this, $func ];
1473 } elseif ( $passSelf ) {
1474 array_unshift( $params, $this );
1478 return $func( ...$params );
1497 if ( !$this->
doTable( $table ) ) {
1501 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
1502 $this->
output(
"...skipping: '$table' table doesn't exist yet.\n" );
1509 "Renaming $count indexes on $table table"