MediaWiki  master
DatabaseUpdater.php
Go to the documentation of this file.
1 <?php
30 
31 require_once __DIR__ . '/../../maintenance/Maintenance.php';
32 
40 abstract class DatabaseUpdater {
41  public const REPLICATION_WAIT_TIMEOUT = 300;
42 
48  protected $updates = [];
49 
55  protected $updatesSkipped = [];
56 
61  protected $extensionUpdates = [];
62 
68  protected $db;
69 
73  protected $maintenance;
74 
75  protected $shared = false;
76 
79 
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,
98  ];
99 
105  protected $fileHandle = null;
106 
112  protected $skipSchema = false;
113 
120  protected function __construct(
122  $shared,
124  ) {
125  $this->db = $db;
126  $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
127  $this->shared = $shared;
128  if ( $maintenance ) {
129  $this->maintenance = $maintenance;
130  $this->fileHandle = $maintenance->fileHandle;
131  } else {
132  $this->maintenance = new FakeMaintenance;
133  }
134  $this->maintenance->setDB( $db );
135  }
136 
140  private function loadExtensionSchemaUpdates() {
141  $hookContainer = $this->loadExtensions();
142  ( new HookRunner( $hookContainer ) )->onLoadExtensionSchemaUpdates( $this );
143  }
144 
151  private function loadExtensions() {
152  if ( $this->autoExtensionHookContainer ) {
153  // Already injected by installer
155  }
156  if ( defined( 'MW_EXTENSIONS_LOADED' ) ) {
157  throw new Exception( __METHOD__ .
158  ' apparently called from installer but no hook container was injected' );
159  }
160  if ( !defined( 'MEDIAWIKI_INSTALL' ) ) {
161  // Running under update.php: just use global locator
162  return MediaWikiServices::getInstance()->getHookContainer();
163  }
165 
166  $registry = ExtensionRegistry::getInstance();
167  $queue = $registry->getQueue();
168  // Don't accidentally load extensions in the future
169  $registry->clearQueue();
170 
171  // Read extension.json files
172  $data = $registry->readFromQueue( $queue );
173 
174  // Merge extension attribute hooks with hooks defined by a .php
175  // registration file included from LocalSettings.php
176  $legacySchemaHooks = $data['globals']['wgHooks']['LoadExtensionSchemaUpdates'] ?? [];
177  if ( $vars && isset( $vars['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
178  $legacySchemaHooks = array_merge( $legacySchemaHooks, $vars['wgHooks']['LoadExtensionSchemaUpdates'] );
179  }
180 
181  // Merge classes from extension.json
182  global $wgAutoloadClasses;
183  if ( $vars && isset( $vars['wgAutoloadClasses'] ) ) {
184  $wgAutoloadClasses += $vars['wgAutoloadClasses'];
185  }
186 
187  return new HookContainer(
188  new StaticHookRegistry(
189  [ 'LoadExtensionSchemaUpdates' => $legacySchemaHooks ],
190  $data['attributes']['Hooks'] ?? [],
191  $data['attributes']['DeprecatedHooks'] ?? []
192  ),
193  MediaWikiServices::getInstance()->getObjectFactory()
194  );
195  }
196 
205  public static function newForDB(
207  $shared = false,
209  ) {
210  $type = $db->getType();
211  if ( in_array( $type, Installer::getDBTypes() ) ) {
212  $class = ucfirst( $type ) . 'Updater';
213 
214  return new $class( $db, $shared, $maintenance );
215  } else {
216  throw new MWException( __METHOD__ . ' called for unsupported $wgDBtype' );
217  }
218  }
219 
227  public function setAutoExtensionHookContainer( HookContainer $hookContainer ) {
228  $this->autoExtensionHookContainer = $hookContainer;
229  }
230 
236  public function getDB() {
237  return $this->db;
238  }
239 
246  public function output( $str ) {
247  if ( $this->maintenance->isQuiet() ) {
248  return;
249  }
250  global $wgCommandLineMode;
251  if ( !$wgCommandLineMode ) {
252  $str = htmlspecialchars( $str );
253  }
254  echo $str;
255  flush();
256  }
257 
270  public function addExtensionUpdate( array $update ) {
271  $this->extensionUpdates[] = $update;
272  }
273 
284  public function addExtensionTable( $tableName, $sqlPath ) {
285  $this->extensionUpdates[] = [ 'addTable', $tableName, $sqlPath, true ];
286  }
287 
298  public function addExtensionIndex( $tableName, $indexName, $sqlPath ) {
299  $this->extensionUpdates[] = [ 'addIndex', $tableName, $indexName, $sqlPath, true ];
300  }
301 
312  public function addExtensionField( $tableName, $columnName, $sqlPath ) {
313  $this->extensionUpdates[] = [ 'addField', $tableName, $columnName, $sqlPath, true ];
314  }
315 
326  public function dropExtensionField( $tableName, $columnName, $sqlPath ) {
327  $this->extensionUpdates[] = [ 'dropField', $tableName, $columnName, $sqlPath, true ];
328  }
329 
340  public function dropExtensionIndex( $tableName, $indexName, $sqlPath ) {
341  $this->extensionUpdates[] = [ 'dropIndex', $tableName, $indexName, $sqlPath, true ];
342  }
343 
353  public function dropExtensionTable( $tableName, $sqlPath = false ) {
354  $this->extensionUpdates[] = [ 'dropTable', $tableName, $sqlPath, true ];
355  }
356 
370  public function renameExtensionIndex( $tableName, $oldIndexName, $newIndexName,
371  $sqlPath, $skipBothIndexExistWarning = false
372  ) {
373  $this->extensionUpdates[] = [
374  'renameIndex',
375  $tableName,
376  $oldIndexName,
377  $newIndexName,
378  $skipBothIndexExistWarning,
379  $sqlPath,
380  true
381  ];
382  }
383 
394  public function modifyExtensionField( $tableName, $fieldName, $sqlPath ) {
395  $this->extensionUpdates[] = [ 'modifyField', $tableName, $fieldName, $sqlPath, true ];
396  }
397 
407  public function modifyExtensionTable( $tableName, $sqlPath ) {
408  $this->extensionUpdates[] = [ 'modifyTable', $tableName, $sqlPath, true ];
409  }
410 
417  public function tableExists( $tableName ) {
418  return ( $this->db->tableExists( $tableName, __METHOD__ ) );
419  }
420 
430  public function addPostDatabaseUpdateMaintenance( $class ) {
431  $this->postDatabaseUpdateMaintenance[] = $class;
432  }
433 
439  protected function getExtensionUpdates() {
441  }
442 
450  }
451 
458  private function writeSchemaUpdateFile( array $schemaUpdate = [] ) {
460  $this->updatesSkipped = [];
461 
462  foreach ( $updates as [ $func, $args, $origParams ] ) {
463  // @phan-suppress-next-line PhanUndeclaredInvokeInCallable
464  $func( ...$args );
465  flush();
466  $this->updatesSkipped[] = $origParams;
467  }
468  }
469 
481  public function getSchemaVars() {
482  return []; // DB-type specific
483  }
484 
490  public function doUpdates( array $what = [ 'core', 'extensions', 'stats' ] ) {
491  $this->db->setSchemaVars( $this->getSchemaVars() );
492 
493  $what = array_fill_keys( $what, true );
494  $this->skipSchema = isset( $what['noschema'] ) || $this->fileHandle !== null;
495  if ( isset( $what['core'] ) ) {
496  $this->doCollationUpdate();
497  $this->runUpdates( $this->getCoreUpdateList(), false );
498  }
499  if ( isset( $what['extensions'] ) ) {
501  $this->runUpdates( $this->getExtensionUpdates(), true );
502  }
503 
504  if ( isset( $what['stats'] ) ) {
505  $this->checkStats();
506  }
507 
508  if ( $this->fileHandle ) {
509  $this->skipSchema = false;
510  $this->writeSchemaUpdateFile();
511  }
512  }
513 
520  private function runUpdates( array $updates, $passSelf ) {
521  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
522 
523  $updatesDone = [];
524  $updatesSkipped = [];
525  foreach ( $updates as $params ) {
526  $origParams = $params;
527  $func = array_shift( $params );
528  if ( !is_array( $func ) && method_exists( $this, $func ) ) {
529  $func = [ $this, $func ];
530  } elseif ( $passSelf ) {
531  array_unshift( $params, $this );
532  }
533  $ret = $func( ...$params );
534  flush();
535  if ( $ret !== false ) {
536  $updatesDone[] = $origParams;
537  $lbFactory->waitForReplication( [ 'timeout' => self::REPLICATION_WAIT_TIMEOUT ] );
538  } else {
539  $updatesSkipped[] = [ $func, $params, $origParams ];
540  }
541  }
542  $this->updatesSkipped = array_merge( $this->updatesSkipped, $updatesSkipped );
543  $this->updates = array_merge( $this->updates, $updatesDone );
544  }
545 
553  public function updateRowExists( $key ) {
554  $row = $this->db->selectRow(
555  'updatelog',
556  # T67813
557  '1 AS X',
558  [ 'ul_key' => $key ],
559  __METHOD__
560  );
561 
562  return (bool)$row;
563  }
564 
578  public function insertUpdateRow( $key, $val = null ) {
579  $this->db->clearFlag( DBO_DDLMODE );
580  $values = [ 'ul_key' => $key ];
581  if ( $val && $this->canUseNewUpdatelog() ) {
582  $values['ul_value'] = $val;
583  }
584  $this->db->insert( 'updatelog', $values, __METHOD__, [ 'IGNORE' ] );
585  $this->db->setFlag( DBO_DDLMODE );
586  }
587 
596  protected function canUseNewUpdatelog() {
597  return $this->db->tableExists( 'updatelog', __METHOD__ ) &&
598  $this->db->fieldExists( 'updatelog', 'ul_value', __METHOD__ );
599  }
600 
609  protected function doTable( $name ) {
611 
612  // Don't bother to check $wgSharedTables if there isn't a shared database
613  // or the user actually also wants to do updates on the shared database.
614  if ( $wgSharedDB === null || $this->shared ) {
615  return true;
616  }
617 
618  if ( in_array( $name, $wgSharedTables ) ) {
619  $this->output( "...skipping update to shared table $name.\n" );
620  return false;
621  } else {
622  return true;
623  }
624  }
625 
633  abstract protected function getCoreUpdateList();
634 
642  protected function copyFile( $filename ) {
643  $this->db->sourceFile(
644  $filename,
645  null,
646  null,
647  __METHOD__,
648  [ $this, 'appendLine' ]
649  );
650  }
651 
664  protected function appendLine( $line ) {
665  $line = rtrim( $line ) . ";\n";
666  if ( fwrite( $this->fileHandle, $line ) === false ) {
667  throw new MWException( "trouble writing file" );
668  }
669 
670  return false;
671  }
672 
684  protected function applyPatch( $path, $isFullPath = false, $msg = null ) {
685  if ( $msg === null ) {
686  $msg = "Applying $path patch";
687  }
688  if ( $this->skipSchema ) {
689  $this->output( "...skipping schema change ($msg).\n" );
690 
691  return false;
692  }
693 
694  $this->output( "$msg ..." );
695 
696  if ( !$isFullPath ) {
697  $path = $this->patchPath( $this->db, $path );
698  }
699  if ( $this->fileHandle !== null ) {
700  $this->copyFile( $path );
701  } else {
702  $this->db->sourceFile( $path );
703  }
704  $this->output( "done.\n" );
705 
706  return true;
707  }
708 
717  public function patchPath( IDatabase $db, $patch ) {
718  global $IP;
719 
720  $dbType = $db->getType();
721  if ( file_exists( "$IP/maintenance/$dbType/archives/$patch" ) ) {
722  return "$IP/maintenance/$dbType/archives/$patch";
723  } else {
724  return "$IP/maintenance/archives/$patch";
725  }
726  }
727 
739  protected function addTable( $name, $patch, $fullpath = false ) {
740  if ( !$this->doTable( $name ) ) {
741  return true;
742  }
743 
744  if ( $this->db->tableExists( $name, __METHOD__ ) ) {
745  $this->output( "...$name table already exists.\n" );
746  } else {
747  return $this->applyPatch( $patch, $fullpath, "Creating $name table" );
748  }
749 
750  return true;
751  }
752 
765  protected function addField( $table, $field, $patch, $fullpath = false ) {
766  if ( !$this->doTable( $table ) ) {
767  return true;
768  }
769 
770  if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
771  $this->output( "...$table table does not exist, skipping new field patch.\n" );
772  } elseif ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
773  $this->output( "...have $field field in $table table.\n" );
774  } else {
775  return $this->applyPatch( $patch, $fullpath, "Adding $field field to table $table" );
776  }
777 
778  return true;
779  }
780 
793  protected function addIndex( $table, $index, $patch, $fullpath = false ) {
794  if ( !$this->doTable( $table ) ) {
795  return true;
796  }
797 
798  if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
799  $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
800  } elseif ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
801  $this->output( "...index $index already set on $table table.\n" );
802  } else {
803  return $this->applyPatch( $patch, $fullpath, "Adding index $index to table $table" );
804  }
805 
806  return true;
807  }
808 
819  protected function addIndexIfNoneExist( $table, $indexes, $patch, $fullpath = false ) {
820  if ( !$this->doTable( $table ) ) {
821  return true;
822  }
823 
824  if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
825  $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
826  return true;
827  }
828 
829  $newIndex = $indexes[0];
830  foreach ( $indexes as $index ) {
831  if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
832  $this->output(
833  "...skipping index $newIndex because index $index already set on $table table.\n"
834  );
835  return true;
836  }
837  }
838 
839  return $this->applyPatch( $patch, $fullpath, "Adding index $index to table $table" );
840  }
841 
854  protected function dropField( $table, $field, $patch, $fullpath = false ) {
855  if ( !$this->doTable( $table ) ) {
856  return true;
857  }
858 
859  if ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
860  return $this->applyPatch( $patch, $fullpath, "Table $table contains $field field. Dropping" );
861  } else {
862  $this->output( "...$table table does not contain $field field.\n" );
863  }
864 
865  return true;
866  }
867 
880  protected function dropIndex( $table, $index, $patch, $fullpath = false ) {
881  if ( !$this->doTable( $table ) ) {
882  return true;
883  }
884 
885  if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
886  return $this->applyPatch( $patch, $fullpath, "Dropping $index index from table $table" );
887  } else {
888  $this->output( "...$index key doesn't exist.\n" );
889  }
890 
891  return true;
892  }
893 
910  protected function renameIndex( $table, $oldIndex, $newIndex,
911  $skipBothIndexExistWarning, $patch, $fullpath = false
912  ) {
913  if ( !$this->doTable( $table ) ) {
914  return true;
915  }
916 
917  // First requirement: the table must exist
918  if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
919  $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
920 
921  return true;
922  }
923 
924  // Second requirement: the new index must be missing
925  if ( $this->db->indexExists( $table, $newIndex, __METHOD__ ) ) {
926  $this->output( "...index $newIndex already set on $table table.\n" );
927  if ( !$skipBothIndexExistWarning &&
928  $this->db->indexExists( $table, $oldIndex, __METHOD__ )
929  ) {
930  $this->output( "...WARNING: $oldIndex still exists, despite it has " .
931  "been renamed into $newIndex (which also exists).\n" .
932  " $oldIndex should be manually removed if not needed anymore.\n" );
933  }
934 
935  return true;
936  }
937 
938  // Third requirement: the old index must exist
939  if ( !$this->db->indexExists( $table, $oldIndex, __METHOD__ ) ) {
940  $this->output( "...skipping: index $oldIndex doesn't exist.\n" );
941 
942  return true;
943  }
944 
945  // Requirements have been satisfied, patch can be applied
946  return $this->applyPatch(
947  $patch,
948  $fullpath,
949  "Renaming index $oldIndex into $newIndex to table $table"
950  );
951  }
952 
967  protected function dropTable( $table, $patch = false, $fullpath = false ) {
968  if ( !$this->doTable( $table ) ) {
969  return true;
970  }
971 
972  if ( $this->db->tableExists( $table, __METHOD__ ) ) {
973  $msg = "Dropping table $table";
974 
975  if ( $patch === false ) {
976  $this->output( "$msg ..." );
977  $this->db->dropTable( $table, __METHOD__ );
978  $this->output( "done.\n" );
979  } else {
980  return $this->applyPatch( $patch, $fullpath, $msg );
981  }
982  } else {
983  $this->output( "...$table doesn't exist.\n" );
984  }
985 
986  return true;
987  }
988 
1003  protected function modifyField( $table, $field, $patch, $fullpath = false ) {
1004  if ( !$this->doTable( $table ) ) {
1005  return true;
1006  }
1007 
1008  $updateKey = "$table-$field-$patch";
1009  if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
1010  $this->output( "...$table table does not exist, skipping modify field patch.\n" );
1011  } elseif ( !$this->db->fieldExists( $table, $field, __METHOD__ ) ) {
1012  $this->output( "...$field field does not exist in $table table, " .
1013  "skipping modify field patch.\n" );
1014  } elseif ( $this->updateRowExists( $updateKey ) ) {
1015  $this->output( "...$field in table $table already modified by patch $patch.\n" );
1016  } else {
1017  $apply = $this->applyPatch( $patch, $fullpath, "Modifying $field field of table $table" );
1018  if ( $apply ) {
1019  $this->insertUpdateRow( $updateKey );
1020  }
1021  return $apply;
1022  }
1023  return true;
1024  }
1025 
1040  protected function modifyTable( $table, $patch, $fullpath = false ) {
1041  if ( !$this->doTable( $table ) ) {
1042  return true;
1043  }
1044 
1045  $updateKey = "$table-$patch";
1046  if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
1047  $this->output( "...$table table does not exist, skipping modify table patch.\n" );
1048  } elseif ( $this->updateRowExists( $updateKey ) ) {
1049  $this->output( "...table $table already modified by patch $patch.\n" );
1050  } else {
1051  $apply = $this->applyPatch( $patch, $fullpath, "Modifying table $table" );
1052  if ( $apply ) {
1053  $this->insertUpdateRow( $updateKey );
1054  }
1055  return $apply;
1056  }
1057  return true;
1058  }
1059 
1080  protected function runMaintenance( $class, $script ) {
1081  $this->output( "Running $script...\n" );
1082  $task = $this->maintenance->runChild( $class );
1083  $ok = $task->execute();
1084  if ( !$ok ) {
1085  throw new RuntimeException( "Execution of $script did not complete successfully." );
1086  }
1087  $this->output( "done.\n" );
1088  }
1089 
1095  public function setFileAccess() {
1096  $repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
1097  $zonePath = $repo->getZonePath( 'temp' );
1098  if ( $repo->getBackend()->directoryExists( [ 'dir' => $zonePath ] ) ) {
1099  // If the directory was never made, then it will have the right ACLs when it is made
1100  $status = $repo->getBackend()->secure( [
1101  'dir' => $zonePath,
1102  'noAccess' => true,
1103  'noListing' => true
1104  ] );
1105  if ( $status->isOK() ) {
1106  $this->output( "Set the local repo temp zone container to be private.\n" );
1107  } else {
1108  $this->output( "Failed to set the local repo temp zone container to be private.\n" );
1109  }
1110  }
1111  }
1112 
1116  public function purgeCache() {
1117  global $wgLocalisationCacheConf;
1118  // We can't guarantee that the user will be able to use TRUNCATE,
1119  // but we know that DELETE is available to us
1120  $this->output( "Purging caches..." );
1121 
1122  // ObjectCache
1123  $this->db->delete( 'objectcache', '*', __METHOD__ );
1124 
1125  // LocalisationCache
1126  if ( $wgLocalisationCacheConf['manualRecache'] ) {
1127  $this->rebuildLocalisationCache();
1128  }
1129 
1130  // ResourceLoader: Message cache
1131  $services = MediaWikiServices::getInstance();
1132  $blobStore = new MessageBlobStore(
1133  $services->getResourceLoader(),
1134  null,
1135  $services->getMainWANObjectCache()
1136  );
1137  $blobStore->clear();
1138 
1139  // ResourceLoader: File-dependency cache
1140  $this->db->delete( 'module_deps', '*', __METHOD__ );
1141  $this->output( "done.\n" );
1142  }
1143 
1147  protected function checkStats() {
1148  $this->output( "...site_stats is populated..." );
1149  $row = $this->db->selectRow( 'site_stats', '*', [ 'ss_row_id' => 1 ], __METHOD__ );
1150  if ( $row === false ) {
1151  $this->output( "data is missing! rebuilding...\n" );
1152  } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
1153  $this->output( "missing ss_total_pages, rebuilding...\n" );
1154  } else {
1155  $this->output( "done.\n" );
1156 
1157  return;
1158  }
1159  SiteStatsInit::doAllAndCommit( $this->db );
1160  }
1161 
1162  # Common updater functions
1163 
1167  protected function doCollationUpdate() {
1168  global $wgCategoryCollation;
1169  if ( $this->db->selectField(
1170  'categorylinks',
1171  'COUNT(*)',
1172  'cl_collation != ' . $this->db->addQuotes( $wgCategoryCollation ),
1173  __METHOD__
1174  ) == 0
1175  ) {
1176  $this->output( "...collations up-to-date.\n" );
1177 
1178  return;
1179  }
1180 
1181  $this->output( "Updating category collations..." );
1182  $task = $this->maintenance->runChild( UpdateCollation::class );
1183  $task->execute();
1184  $this->output( "...done.\n" );
1185  }
1186 
1190  protected function rebuildLocalisationCache() {
1194  $cl = $this->maintenance->runChild(
1195  RebuildLocalisationCache::class, 'rebuildLocalisationCache.php'
1196  );
1197  '@phan-var RebuildLocalisationCache $cl';
1198  $this->output( "Rebuilding localisation cache...\n" );
1199  $cl->setForce();
1200  $cl->execute();
1201  $this->output( "done.\n" );
1202  }
1203 
1208  protected function migrateComments() {
1209  if ( !$this->updateRowExists( 'MigrateComments' ) ) {
1210  $this->output(
1211  "Migrating comments to the 'comments' table, printing progress markers. For large\n" .
1212  "databases, you may want to hit Ctrl-C and do this manually with\n" .
1213  "maintenance/migrateComments.php.\n"
1214  );
1215  $task = $this->maintenance->runChild( MigrateComments::class, 'migrateComments.php' );
1216  $ok = $task->execute();
1217  $this->output( $ok ? "done.\n" : "errors were encountered.\n" );
1218  }
1219  }
1220 
1225  protected function migrateImageCommentTemp() {
1226  if ( $this->tableExists( 'image_comment_temp' ) ) {
1227  $this->output( "Merging image_comment_temp into the image table\n" );
1228  $task = $this->maintenance->runChild(
1229  MigrateImageCommentTemp::class, 'migrateImageCommentTemp.php'
1230  );
1231  // @phan-suppress-next-line PhanUndeclaredMethod
1232  $task->setForce();
1233  $ok = $task->execute();
1234  $this->output( $ok ? "done.\n" : "errors were encountered.\n" );
1235  if ( $ok ) {
1236  $this->dropTable( 'image_comment_temp' );
1237  }
1238  }
1239  }
1240 
1245  protected function migrateActors() {
1246  if ( !$this->updateRowExists( 'MigrateActors' ) ) {
1247  $this->output(
1248  "Migrating actors to the 'actor' table, printing progress markers. For large\n" .
1249  "databases, you may want to hit Ctrl-C and do this manually with\n" .
1250  "maintenance/migrateActors.php.\n"
1251  );
1252  $task = $this->maintenance->runChild( 'MigrateActors', 'migrateActors.php' );
1253  $ok = $task->execute();
1254  $this->output( $ok ? "done.\n" : "errors were encountered.\n" );
1255  }
1256  }
1257 
1262  protected function migrateArchiveText() {
1263  if ( $this->db->fieldExists( 'archive', 'ar_text', __METHOD__ ) ) {
1264  $this->output( "Migrating archive ar_text to modern storage.\n" );
1265  $task = $this->maintenance->runChild( MigrateArchiveText::class, 'migrateArchiveText.php' );
1266  // @phan-suppress-next-line PhanUndeclaredMethod
1267  $task->setForce();
1268  if ( $task->execute() ) {
1269  $this->applyPatch( 'patch-drop-ar_text.sql', false,
1270  'Dropping ar_text and ar_flags columns' );
1271  }
1272  }
1273  }
1274 
1279  protected function populateArchiveRevId() {
1280  $info = $this->db->fieldInfo( 'archive', 'ar_rev_id' );
1281  if ( !$info ) {
1282  throw new MWException( 'Missing ar_rev_id field of archive table. Should not happen.' );
1283  }
1284  if ( $info->isNullable() ) {
1285  $this->output( "Populating ar_rev_id.\n" );
1286  $task = $this->maintenance->runChild( 'PopulateArchiveRevId', 'populateArchiveRevId.php' );
1287  if ( $task->execute() ) {
1288  $this->applyPatch( 'patch-ar_rev_id-not-null.sql', false,
1289  'Making ar_rev_id not nullable' );
1290  }
1291  }
1292  }
1293 
1298  protected function populateExternallinksIndex60() {
1299  if ( !$this->updateRowExists( 'populate externallinks.el_index_60' ) ) {
1300  $this->output(
1301  "Populating el_index_60 field, printing progress markers. For large\n" .
1302  "databases, you may want to hit Ctrl-C and do this manually with\n" .
1303  "maintenance/populateExternallinksIndex60.php.\n"
1304  );
1305  $task = $this->maintenance->runChild( 'PopulateExternallinksIndex60',
1306  'populateExternallinksIndex60.php' );
1307  $task->execute();
1308  $this->output( "done.\n" );
1309  }
1310  }
1311 
1316  protected function populateContentTables() {
1317  if ( !$this->updateRowExists( 'PopulateContentTables' ) ) {
1318  $this->output(
1319  "Migrating revision data to the MCR 'slot' and 'content' tables, printing progress markers.\n" .
1320  "For large databases, you may want to hit Ctrl-C and do this manually with\n" .
1321  "maintenance/populateContentTables.php.\n"
1322  );
1323  $task = $this->maintenance->runChild(
1324  PopulateContentTables::class, 'populateContentTables.php'
1325  );
1326  $ok = $task->execute();
1327  $this->output( $ok ? "done.\n" : "errors were encountered.\n" );
1328  if ( $ok ) {
1329  $this->insertUpdateRow( 'PopulateContentTables' );
1330  }
1331  }
1332  }
1333 
1337  protected function ifNoActorTable( $func, ...$params ) {
1338  wfDeprecated( __METHOD__, '1.35' );
1339  return $this->ifTableNotExists( 'actor', $func, ...$params );
1340  }
1341 
1355  protected function ifTableNotExists( $table, $func, ...$params ) {
1356  // Handle $passSelf from runUpdates().
1357  $passSelf = false;
1358  if ( $table === $this ) {
1359  $passSelf = true;
1360  $table = $func;
1361  $func = array_shift( $params );
1362  }
1363 
1364  if ( $this->db->tableExists( $table, __METHOD__ ) ) {
1365  return null;
1366  }
1367 
1368  if ( !is_array( $func ) && method_exists( $this, $func ) ) {
1369  $func = [ $this, $func ];
1370  } elseif ( $passSelf ) {
1371  array_unshift( $params, $this );
1372  }
1373 
1374  // @phan-suppress-next-line PhanUndeclaredInvokeInCallable Phan is confused
1375  return $func( ...$params );
1376  }
1377 
1392  protected function ifFieldExists( $table, $field, $func, ...$params ) {
1393  // Handle $passSelf from runUpdates().
1394  $passSelf = false;
1395  if ( $table === $this ) {
1396  $passSelf = true;
1397  $table = $field;
1398  $field = $func;
1399  $func = array_shift( $params );
1400  }
1401 
1402  if ( !$this->db->tableExists( $table, __METHOD__ ) ||
1403  !$this->db->fieldExists( $table, $field, __METHOD__ )
1404  ) {
1405  return null;
1406  }
1407 
1408  if ( !is_array( $func ) && method_exists( $this, $func ) ) {
1409  $func = [ $this, $func ];
1410  } elseif ( $passSelf ) {
1411  array_unshift( $params, $this );
1412  }
1413 
1414  // @phan-suppress-next-line PhanUndeclaredInvokeInCallable Phan is confused
1415  return $func( ...$params );
1416  }
1417 
1418 }
DatabaseUpdater\output
output( $str)
Output some text.
Definition: DatabaseUpdater.php:246
DatabaseUpdater\dropIndex
dropIndex( $table, $index, $patch, $fullpath=false)
Drop an index from an existing table.
Definition: DatabaseUpdater.php:880
DatabaseUpdater\modifyExtensionTable
modifyExtensionTable( $tableName, $sqlPath)
Modify an existing extension table.
Definition: DatabaseUpdater.php:407
DatabaseUpdater\dropTable
dropTable( $table, $patch=false, $fullpath=false)
If the specified table exists, drop it, or execute the patch if one is provided.
Definition: DatabaseUpdater.php:967
DatabaseUpdater\copyFile
copyFile( $filename)
Append an SQL fragment to the open file handle.
Definition: DatabaseUpdater.php:642
DatabaseUpdater\$updatesSkipped
array $updatesSkipped
Array of updates that were skipped.
Definition: DatabaseUpdater.php:55
DatabaseUpdater\newForDB
static newForDB(IMaintainableDatabase $db, $shared=false, Maintenance $maintenance=null)
Definition: DatabaseUpdater.php:205
DatabaseUpdater\applyPatch
applyPatch( $path, $isFullPath=false, $msg=null)
Applies a SQL patch.
Definition: DatabaseUpdater.php:684
DatabaseUpdater\setAutoExtensionHookContainer
setAutoExtensionHookContainer(HookContainer $hookContainer)
Set the HookContainer to use for loading extension schema updates.
Definition: DatabaseUpdater.php:227
DatabaseUpdater\doCollationUpdate
doCollationUpdate()
Update CategoryLinks collation.
Definition: DatabaseUpdater.php:1167
DatabaseUpdater\dropExtensionTable
dropExtensionTable( $tableName, $sqlPath=false)
Drop an extension table.
Definition: DatabaseUpdater.php:353
SiteStatsInit\doAllAndCommit
static doAllAndCommit( $database, array $options=[])
Do all updates and commit them.
Definition: SiteStatsInit.php:134
$wgSharedTables
$wgSharedTables
Definition: DefaultSettings.php:2225
DatabaseUpdater
Class for handling database updates.
Definition: DatabaseUpdater.php:40
DatabaseUpdater\populateExternallinksIndex60
populateExternallinksIndex60()
Populates the externallinks.el_index_60 field.
Definition: DatabaseUpdater.php:1298
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:193
DatabaseUpdater\addPostDatabaseUpdateMaintenance
addPostDatabaseUpdateMaintenance( $class)
Add a maintenance script to be run after the database updates are complete.
Definition: DatabaseUpdater.php:430
DatabaseUpdater\checkStats
checkStats()
Check the site_stats table is not properly populated.
Definition: DatabaseUpdater.php:1147
true
return true
Definition: router.php:90
$wgSharedDB
$wgSharedDB
Shared database for multiple wikis.
Definition: DefaultSettings.php:2214
DatabaseUpdater\getCoreUpdateList
getCoreUpdateList()
Get an array of updates to perform on the database.
DatabaseUpdater\migrateImageCommentTemp
migrateImageCommentTemp()
Merge image_comment_temp into the image table.
Definition: DatabaseUpdater.php:1225
MediaWiki\HookContainer\StaticHookRegistry
This is a simple immutable HookRegistry which can be used to set up a local HookContainer in tests an...
Definition: StaticHookRegistry.php:9
DatabaseUpdater\populateContentTables
populateContentTables()
Populates the MCR content tables.
Definition: DatabaseUpdater.php:1316
DatabaseUpdater\dropField
dropField( $table, $field, $patch, $fullpath=false)
Drop a field from an existing table.
Definition: DatabaseUpdater.php:854
DatabaseUpdater\addField
addField( $table, $field, $patch, $fullpath=false)
Add a new field to an existing table.
Definition: DatabaseUpdater.php:765
DatabaseUpdater\doTable
doTable( $name)
Returns whether updates should be executed on the database table $name.
Definition: DatabaseUpdater.php:609
DatabaseUpdater\getPostDatabaseUpdateMaintenance
getPostDatabaseUpdateMaintenance()
Definition: DatabaseUpdater.php:448
DatabaseUpdater\appendLine
appendLine( $line)
Append a line to the open filehandle.
Definition: DatabaseUpdater.php:664
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
DatabaseUpdater\patchPath
patchPath(IDatabase $db, $patch)
Get the full path of a patch file.
Definition: DatabaseUpdater.php:717
DatabaseUpdater\getSchemaVars
getSchemaVars()
Get appropriate schema variables in the current database connection.
Definition: DatabaseUpdater.php:481
DatabaseUpdater\REPLICATION_WAIT_TIMEOUT
const REPLICATION_WAIT_TIMEOUT
Definition: DatabaseUpdater.php:41
DatabaseUpdater\addExtensionIndex
addExtensionIndex( $tableName, $indexName, $sqlPath)
Add an index to an existing extension table.
Definition: DatabaseUpdater.php:298
DatabaseUpdater\migrateComments
migrateComments()
Migrate comments to the new 'comment' table.
Definition: DatabaseUpdater.php:1208
DatabaseUpdater\__construct
__construct(IMaintainableDatabase &$db, $shared, Maintenance $maintenance=null)
Definition: DatabaseUpdater.php:120
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
DatabaseUpdater\dropExtensionIndex
dropExtensionIndex( $tableName, $indexName, $sqlPath)
Drop an index from an extension table Intended for use in LoadExtensionSchemaUpdates hook handlers.
Definition: DatabaseUpdater.php:340
DatabaseUpdater\$updates
array $updates
Array of updates to perform on the database.
Definition: DatabaseUpdater.php:48
ExtensionRegistry\getInstance
static getInstance()
Definition: ExtensionRegistry.php:134
DatabaseUpdater\addExtensionUpdate
addExtensionUpdate(array $update)
Add a new update coming from an extension.
Definition: DatabaseUpdater.php:270
DatabaseUpdater\setFileAccess
setFileAccess()
Set any .htaccess files or equivilent for storage repos.
Definition: DatabaseUpdater.php:1095
DatabaseUpdater\loadExtensionSchemaUpdates
loadExtensionSchemaUpdates()
Cause extensions to register any updates they need to perform.
Definition: DatabaseUpdater.php:140
MWException
MediaWiki exception.
Definition: MWException.php:29
DatabaseUpdater\renameIndex
renameIndex( $table, $oldIndex, $newIndex, $skipBothIndexExistWarning, $patch, $fullpath=false)
Rename an index from an existing table.
Definition: DatabaseUpdater.php:910
DatabaseUpdater\getExtensionUpdates
getExtensionUpdates()
Get the list of extension-defined updates.
Definition: DatabaseUpdater.php:439
DatabaseUpdater\migrateArchiveText
migrateArchiveText()
Migrate ar_text to modern storage.
Definition: DatabaseUpdater.php:1262
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Definition: GlobalFunctions.php:997
DatabaseUpdater\$maintenance
Maintenance $maintenance
Definition: DatabaseUpdater.php:73
DatabaseUpdater\modifyTable
modifyTable( $table, $patch, $fullpath=false)
Modify an existing table, similar to modifyField.
Definition: DatabaseUpdater.php:1040
DatabaseUpdater\$db
IMaintainableDatabase $db
Handle to the database subclass.
Definition: DatabaseUpdater.php:68
$wgCommandLineMode
global $wgCommandLineMode
Definition: DevelopmentSettings.php:29
DatabaseUpdater\modifyExtensionField
modifyExtensionField( $tableName, $fieldName, $sqlPath)
Modify an existing field in an extension table.
Definition: DatabaseUpdater.php:394
DatabaseUpdater\rebuildLocalisationCache
rebuildLocalisationCache()
Rebuilds the localisation cache.
Definition: DatabaseUpdater.php:1190
$queue
$queue
Definition: mergeMessageFileList.php:176
$args
if( $line===false) $args
Definition: mcc.php:124
DatabaseUpdater\ifNoActorTable
ifNoActorTable( $func,... $params)
Definition: DatabaseUpdater.php:1337
Installer\getExistingLocalSettings
static getExistingLocalSettings()
Determine if LocalSettings.php exists.
Definition: Installer.php:629
DatabaseUpdater\getDB
getDB()
Get a database connection to run updates.
Definition: DatabaseUpdater.php:236
DatabaseUpdater\ifTableNotExists
ifTableNotExists( $table, $func,... $params)
Only run a function if a table does not exist.
Definition: DatabaseUpdater.php:1355
DatabaseUpdater\$fileHandle
resource null $fileHandle
File handle for SQL output.
Definition: DatabaseUpdater.php:105
$wgLocalisationCacheConf
$wgLocalisationCacheConf
Localisation cache configuration.
Definition: DefaultSettings.php:3081
DatabaseUpdater\runUpdates
runUpdates(array $updates, $passSelf)
Helper function for doUpdates()
Definition: DatabaseUpdater.php:520
DatabaseUpdater\doUpdates
doUpdates(array $what=[ 'core', 'extensions', 'stats'])
Do all the updates.
Definition: DatabaseUpdater.php:490
FakeMaintenance
Fake maintenance wrapper, mostly used for the web installer/updater.
Definition: FakeMaintenance.php:25
DatabaseUpdater\updateRowExists
updateRowExists( $key)
Helper function: check if the given key is present in the updatelog table.
Definition: DatabaseUpdater.php:553
DatabaseUpdater\$autoExtensionHookContainer
HookContainer null $autoExtensionHookContainer
Definition: DatabaseUpdater.php:78
DBO_DDLMODE
const DBO_DDLMODE
Definition: defines.php:16
DatabaseUpdater\populateArchiveRevId
populateArchiveRevId()
Populate ar_rev_id, then make it not nullable.
Definition: DatabaseUpdater.php:1279
DatabaseUpdater\canUseNewUpdatelog
canUseNewUpdatelog()
Updatelog was changed in 1.17 to have a ul_value column so we can record more information about what ...
Definition: DatabaseUpdater.php:596
DatabaseUpdater\ifFieldExists
ifFieldExists( $table, $field, $func,... $params)
Only run a function if the named field exists.
Definition: DatabaseUpdater.php:1392
DatabaseUpdater\runMaintenance
runMaintenance( $class, $script)
Run a maintenance script.
Definition: DatabaseUpdater.php:1080
$line
$line
Definition: mcc.php:119
$wgAutoloadClasses
$wgAutoloadClasses
Array mapping class names to filenames, for autoloading.
Definition: DefaultSettings.php:8577
DatabaseUpdater\tableExists
tableExists( $tableName)
Definition: DatabaseUpdater.php:417
DatabaseUpdater\addExtensionField
addExtensionField( $tableName, $columnName, $sqlPath)
Add a field to an existing extension table.
Definition: DatabaseUpdater.php:312
DatabaseUpdater\modifyField
modifyField( $table, $field, $patch, $fullpath=false)
Modify an existing field.
Definition: DatabaseUpdater.php:1003
DatabaseUpdater\writeSchemaUpdateFile
writeSchemaUpdateFile(array $schemaUpdate=[])
Definition: DatabaseUpdater.php:458
DatabaseUpdater\addIndexIfNoneExist
addIndexIfNoneExist( $table, $indexes, $patch, $fullpath=false)
Add a new index to an existing table if none of the given indexes exist.
Definition: DatabaseUpdater.php:819
DatabaseUpdater\loadExtensions
loadExtensions()
Loads LocalSettings.php, if needed, and initialises everything needed for LoadExtensionSchemaUpdates ...
Definition: DatabaseUpdater.php:151
$path
$path
Definition: NoLocalSettings.php:25
DatabaseUpdater\renameExtensionIndex
renameExtensionIndex( $tableName, $oldIndexName, $newIndexName, $sqlPath, $skipBothIndexExistWarning=false)
Rename an index on an extension table Intended for use in LoadExtensionSchemaUpdates hook handlers.
Definition: DatabaseUpdater.php:370
DatabaseUpdater\addTable
addTable( $name, $patch, $fullpath=false)
Add a new table to the database.
Definition: DatabaseUpdater.php:739
DatabaseUpdater\$shared
$shared
Definition: DatabaseUpdater.php:75
MessageBlobStore
This class generates message blobs for use by ResourceLoader.
Definition: MessageBlobStore.php:38
Wikimedia\Rdbms\IDatabase\getType
getType()
Get the RDBMS type of the server (e.g.
DatabaseUpdater\migrateActors
migrateActors()
Migrate actors to the new 'actor' table.
Definition: DatabaseUpdater.php:1245
Maintenance\setDB
setDB(IMaintainableDatabase $db)
Sets database object to be returned by getDB().
Definition: Maintenance.php:1379
DatabaseUpdater\dropExtensionField
dropExtensionField( $tableName, $columnName, $sqlPath)
Drop a field from an extension table.
Definition: DatabaseUpdater.php:326
DatabaseUpdater\purgeCache
purgeCache()
Purge various database caches.
Definition: DatabaseUpdater.php:1116
$wgCategoryCollation
$wgCategoryCollation
Specify how category names should be sorted, when listed on a category page.
Definition: DefaultSettings.php:8863
MediaWiki\HookContainer\HookContainer
HookContainer class.
Definition: HookContainer.php:45
DatabaseUpdater\$skipSchema
bool $skipSchema
Flag specifying whether or not to skip schema (e.g.
Definition: DatabaseUpdater.php:112
MediaWiki\HookContainer\HookRunner
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:556
DatabaseUpdater\addExtensionTable
addExtensionTable( $tableName, $sqlPath)
Convenience wrapper for addExtensionUpdate() when adding a new table (which is the most common usage ...
Definition: DatabaseUpdater.php:284
Installer\getDBTypes
static getDBTypes()
Get a list of known DB types.
Definition: Installer.php:510
DatabaseUpdater\addIndex
addIndex( $table, $index, $patch, $fullpath=false)
Add a new index to an existing table.
Definition: DatabaseUpdater.php:793
DatabaseUpdater\$extensionUpdates
array $extensionUpdates
List of extension-provided database updates.
Definition: DatabaseUpdater.php:61
DatabaseUpdater\insertUpdateRow
insertUpdateRow( $key, $val=null)
Helper function: Add a key to the updatelog table.
Definition: DatabaseUpdater.php:578
$IP
$IP
Definition: WebStart.php:49
Wikimedia\Rdbms\IDatabase\setFlag
setFlag( $flag, $remember=self::REMEMBER_NOTHING)
Set a flag for this connection.
DatabaseUpdater\$postDatabaseUpdateMaintenance
string[] $postDatabaseUpdateMaintenance
Scripts to run after database update Should be a subclass of LoggedUpdateMaintenance.
Definition: DatabaseUpdater.php:84
Wikimedia\Rdbms\IMaintainableDatabase
Advanced database interface for IDatabase handles that include maintenance methods.
Definition: IMaintainableDatabase.php:38
$type
$type
Definition: testCompression.php:52