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_flip( $what );
494  $this->skipSchema = isset( $what['noschema'] ) || $this->fileHandle !== null;
495  if ( isset( $what['core'] ) ) {
496  $this->runUpdates( $this->getCoreUpdateList(), false );
497  }
498  if ( isset( $what['extensions'] ) ) {
500  $this->runUpdates( $this->getExtensionUpdates(), true );
501  }
502 
503  if ( isset( $what['stats'] ) ) {
504  $this->checkStats();
505  }
506 
507  if ( $this->fileHandle ) {
508  $this->skipSchema = false;
509  $this->writeSchemaUpdateFile();
510  }
511  }
512 
519  private function runUpdates( array $updates, $passSelf ) {
520  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
521 
522  $updatesDone = [];
523  $updatesSkipped = [];
524  foreach ( $updates as $params ) {
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 );
531  }
532  $ret = $func( ...$params );
533  flush();
534  if ( $ret !== false ) {
535  $updatesDone[] = $origParams;
536  $lbFactory->waitForReplication( [ 'timeout' => self::REPLICATION_WAIT_TIMEOUT ] );
537  } else {
538  $updatesSkipped[] = [ $func, $params, $origParams ];
539  }
540  }
541  $this->updatesSkipped = array_merge( $this->updatesSkipped, $updatesSkipped );
542  $this->updates = array_merge( $this->updates, $updatesDone );
543  }
544 
552  public function updateRowExists( $key ) {
553  $row = $this->db->selectRow(
554  'updatelog',
555  # T67813
556  '1 AS X',
557  [ 'ul_key' => $key ],
558  __METHOD__
559  );
560 
561  return (bool)$row;
562  }
563 
577  public function insertUpdateRow( $key, $val = null ) {
578  $this->db->clearFlag( DBO_DDLMODE );
579  $values = [ 'ul_key' => $key ];
580  if ( $val && $this->canUseNewUpdatelog() ) {
581  $values['ul_value'] = $val;
582  }
583  $this->db->insert( 'updatelog', $values, __METHOD__, [ 'IGNORE' ] );
584  $this->db->setFlag( DBO_DDLMODE );
585  }
586 
595  protected function canUseNewUpdatelog() {
596  return $this->db->tableExists( 'updatelog', __METHOD__ ) &&
597  $this->db->fieldExists( 'updatelog', 'ul_value', __METHOD__ );
598  }
599 
608  protected function doTable( $name ) {
610 
611  // Don't bother to check $wgSharedTables if there isn't a shared database
612  // or the user actually also wants to do updates on the shared database.
613  if ( $wgSharedDB === null || $this->shared ) {
614  return true;
615  }
616 
617  if ( in_array( $name, $wgSharedTables ) ) {
618  $this->output( "...skipping update to shared table $name.\n" );
619  return false;
620  } else {
621  return true;
622  }
623  }
624 
632  abstract protected function getCoreUpdateList();
633 
641  protected function copyFile( $filename ) {
642  $this->db->sourceFile(
643  $filename,
644  null,
645  null,
646  __METHOD__,
647  [ $this, 'appendLine' ]
648  );
649  }
650 
663  protected function appendLine( $line ) {
664  $line = rtrim( $line ) . ";\n";
665  if ( fwrite( $this->fileHandle, $line ) === false ) {
666  throw new MWException( "trouble writing file" );
667  }
668 
669  return false;
670  }
671 
683  protected function applyPatch( $path, $isFullPath = false, $msg = null ) {
684  if ( $msg === null ) {
685  $msg = "Applying $path patch";
686  }
687  if ( $this->skipSchema ) {
688  $this->output( "...skipping schema change ($msg).\n" );
689 
690  return false;
691  }
692 
693  $this->output( "$msg ..." );
694 
695  if ( !$isFullPath ) {
696  $path = $this->patchPath( $this->db, $path );
697  }
698  if ( $this->fileHandle !== null ) {
699  $this->copyFile( $path );
700  } else {
701  $this->db->sourceFile( $path );
702  }
703  $this->output( "done.\n" );
704 
705  return true;
706  }
707 
716  public function patchPath( IDatabase $db, $patch ) {
717  global $IP;
718 
719  $dbType = $db->getType();
720  if ( file_exists( "$IP/maintenance/$dbType/archives/$patch" ) ) {
721  return "$IP/maintenance/$dbType/archives/$patch";
722  } else {
723  return "$IP/maintenance/archives/$patch";
724  }
725  }
726 
738  protected function addTable( $name, $patch, $fullpath = false ) {
739  if ( !$this->doTable( $name ) ) {
740  return true;
741  }
742 
743  if ( $this->db->tableExists( $name, __METHOD__ ) ) {
744  $this->output( "...$name table already exists.\n" );
745  } else {
746  return $this->applyPatch( $patch, $fullpath, "Creating $name table" );
747  }
748 
749  return true;
750  }
751 
764  protected function addField( $table, $field, $patch, $fullpath = false ) {
765  if ( !$this->doTable( $table ) ) {
766  return true;
767  }
768 
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" );
773  } else {
774  return $this->applyPatch( $patch, $fullpath, "Adding $field field to table $table" );
775  }
776 
777  return true;
778  }
779 
792  protected function addIndex( $table, $index, $patch, $fullpath = false ) {
793  if ( !$this->doTable( $table ) ) {
794  return true;
795  }
796 
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" );
801  } else {
802  return $this->applyPatch( $patch, $fullpath, "Adding index $index to table $table" );
803  }
804 
805  return true;
806  }
807 
818  protected function addIndexIfNoneExist( $table, $indexes, $patch, $fullpath = false ) {
819  if ( !$this->doTable( $table ) ) {
820  return true;
821  }
822 
823  if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
824  $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
825  return true;
826  }
827 
828  $newIndex = $indexes[0];
829  foreach ( $indexes as $index ) {
830  if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
831  $this->output(
832  "...skipping index $newIndex because index $index already set on $table table.\n"
833  );
834  return true;
835  }
836  }
837 
838  return $this->applyPatch( $patch, $fullpath, "Adding index $index to table $table" );
839  }
840 
853  protected function dropField( $table, $field, $patch, $fullpath = false ) {
854  if ( !$this->doTable( $table ) ) {
855  return true;
856  }
857 
858  if ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
859  return $this->applyPatch( $patch, $fullpath, "Table $table contains $field field. Dropping" );
860  } else {
861  $this->output( "...$table table does not contain $field field.\n" );
862  }
863 
864  return true;
865  }
866 
879  protected function dropIndex( $table, $index, $patch, $fullpath = false ) {
880  if ( !$this->doTable( $table ) ) {
881  return true;
882  }
883 
884  if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
885  return $this->applyPatch( $patch, $fullpath, "Dropping $index index from table $table" );
886  } else {
887  $this->output( "...$index key doesn't exist.\n" );
888  }
889 
890  return true;
891  }
892 
909  protected function renameIndex( $table, $oldIndex, $newIndex,
910  $skipBothIndexExistWarning, $patch, $fullpath = false
911  ) {
912  if ( !$this->doTable( $table ) ) {
913  return true;
914  }
915 
916  // First requirement: the table must exist
917  if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
918  $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
919 
920  return true;
921  }
922 
923  // Second requirement: the new index must be missing
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__ )
928  ) {
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" );
932  }
933 
934  return true;
935  }
936 
937  // Third requirement: the old index must exist
938  if ( !$this->db->indexExists( $table, $oldIndex, __METHOD__ ) ) {
939  $this->output( "...skipping: index $oldIndex doesn't exist.\n" );
940 
941  return true;
942  }
943 
944  // Requirements have been satisfied, patch can be applied
945  return $this->applyPatch(
946  $patch,
947  $fullpath,
948  "Renaming index $oldIndex into $newIndex to table $table"
949  );
950  }
951 
966  protected function dropTable( $table, $patch = false, $fullpath = false ) {
967  if ( !$this->doTable( $table ) ) {
968  return true;
969  }
970 
971  if ( $this->db->tableExists( $table, __METHOD__ ) ) {
972  $msg = "Dropping table $table";
973 
974  if ( $patch === false ) {
975  $this->output( "$msg ..." );
976  $this->db->dropTable( $table, __METHOD__ );
977  $this->output( "done.\n" );
978  } else {
979  return $this->applyPatch( $patch, $fullpath, $msg );
980  }
981  } else {
982  $this->output( "...$table doesn't exist.\n" );
983  }
984 
985  return true;
986  }
987 
1002  protected function modifyField( $table, $field, $patch, $fullpath = false ) {
1003  if ( !$this->doTable( $table ) ) {
1004  return true;
1005  }
1006 
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" );
1013  } elseif ( $this->updateRowExists( $updateKey ) ) {
1014  $this->output( "...$field in table $table already modified by patch $patch.\n" );
1015  } else {
1016  $apply = $this->applyPatch( $patch, $fullpath, "Modifying $field field of table $table" );
1017  if ( $apply ) {
1018  $this->insertUpdateRow( $updateKey );
1019  }
1020  return $apply;
1021  }
1022  return true;
1023  }
1024 
1039  protected function modifyTable( $table, $patch, $fullpath = false ) {
1040  if ( !$this->doTable( $table ) ) {
1041  return true;
1042  }
1043 
1044  $updateKey = "$table-$patch";
1045  if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
1046  $this->output( "...$table table does not exist, skipping modify table patch.\n" );
1047  } elseif ( $this->updateRowExists( $updateKey ) ) {
1048  $this->output( "...table $table already modified by patch $patch.\n" );
1049  } else {
1050  $apply = $this->applyPatch( $patch, $fullpath, "Modifying table $table" );
1051  if ( $apply ) {
1052  $this->insertUpdateRow( $updateKey );
1053  }
1054  return $apply;
1055  }
1056  return true;
1057  }
1058 
1079  protected function runMaintenance( $class, $script ) {
1080  $this->output( "Running $script...\n" );
1081  $task = $this->maintenance->runChild( $class );
1082  $ok = $task->execute();
1083  if ( !$ok ) {
1084  throw new RuntimeException( "Execution of $script did not complete successfully." );
1085  }
1086  $this->output( "done.\n" );
1087  }
1088 
1094  public function setFileAccess() {
1095  $repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
1096  $zonePath = $repo->getZonePath( 'temp' );
1097  if ( $repo->getBackend()->directoryExists( [ 'dir' => $zonePath ] ) ) {
1098  // If the directory was never made, then it will have the right ACLs when it is made
1099  $status = $repo->getBackend()->secure( [
1100  'dir' => $zonePath,
1101  'noAccess' => true,
1102  'noListing' => true
1103  ] );
1104  if ( $status->isOK() ) {
1105  $this->output( "Set the local repo temp zone container to be private.\n" );
1106  } else {
1107  $this->output( "Failed to set the local repo temp zone container to be private.\n" );
1108  }
1109  }
1110  }
1111 
1115  public function purgeCache() {
1116  global $wgLocalisationCacheConf;
1117  // We can't guarantee that the user will be able to use TRUNCATE,
1118  // but we know that DELETE is available to us
1119  $this->output( "Purging caches..." );
1120 
1121  // ObjectCache
1122  $this->db->delete( 'objectcache', '*', __METHOD__ );
1123 
1124  // LocalisationCache
1125  if ( $wgLocalisationCacheConf['manualRecache'] ) {
1126  $this->rebuildLocalisationCache();
1127  }
1128 
1129  // ResourceLoader: Message cache
1130  $services = MediaWikiServices::getInstance();
1131  $blobStore = new MessageBlobStore(
1132  $services->getResourceLoader(),
1133  null,
1134  $services->getMainWANObjectCache()
1135  );
1136  $blobStore->clear();
1137 
1138  // ResourceLoader: File-dependency cache
1139  $this->db->delete( 'module_deps', '*', __METHOD__ );
1140  $this->output( "done.\n" );
1141  }
1142 
1146  protected function checkStats() {
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" );
1153  } else {
1154  $this->output( "done.\n" );
1155 
1156  return;
1157  }
1158  SiteStatsInit::doAllAndCommit( $this->db );
1159  }
1160 
1161  # Common updater functions
1162 
1166  protected function doActiveUsersInit() {
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__
1172  );
1173  $this->db->update( 'site_stats',
1174  [ 'ss_active_users' => intval( $activeUsers ) ],
1175  [ 'ss_row_id' => 1 ], __METHOD__, [ 'LIMIT' => 1 ]
1176  );
1177  }
1178  $this->output( "...ss_active_users user count set...\n" );
1179  }
1180 
1184  protected function doLogUsertextPopulation() {
1185  if ( !$this->updateRowExists( 'populate log_usertext' ) ) {
1186  $this->output(
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"
1190  );
1191 
1192  $task = $this->maintenance->runChild( PopulateLogUsertext::class );
1193  $task->execute();
1194  $this->output( "done.\n" );
1195  }
1196  }
1197 
1201  protected function doLogSearchPopulation() {
1202  if ( !$this->updateRowExists( 'populate log_search' ) ) {
1203  $this->output(
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" );
1207 
1208  $task = $this->maintenance->runChild( PopulateLogSearch::class );
1209  $task->execute();
1210  $this->output( "done.\n" );
1211  }
1212  }
1213 
1217  protected function doCollationUpdate() {
1218  global $wgCategoryCollation;
1219  if ( $this->db->fieldExists( 'categorylinks', 'cl_collation', __METHOD__ ) ) {
1220  if ( $this->db->selectField(
1221  'categorylinks',
1222  'COUNT(*)',
1223  'cl_collation != ' . $this->db->addQuotes( $wgCategoryCollation ),
1224  __METHOD__
1225  ) == 0
1226  ) {
1227  $this->output( "...collations up-to-date.\n" );
1228 
1229  return;
1230  }
1231 
1232  $this->output( "Updating category collations..." );
1233  $task = $this->maintenance->runChild( UpdateCollation::class );
1234  $task->execute();
1235  $this->output( "...done.\n" );
1236  }
1237  }
1238 
1242  protected function doMigrateUserOptions() {
1243  if ( $this->db->tableExists( 'user_properties', __METHOD__ ) ) {
1244  $cl = $this->maintenance->runChild( ConvertUserOptions::class, 'convertUserOptions.php' );
1245  $cl->execute();
1246  $this->output( "done.\n" );
1247  }
1248  }
1249 
1253  protected function rebuildLocalisationCache() {
1257  $cl = $this->maintenance->runChild(
1258  RebuildLocalisationCache::class, 'rebuildLocalisationCache.php'
1259  );
1260  '@phan-var RebuildLocalisationCache $cl';
1261  $this->output( "Rebuilding localisation cache...\n" );
1262  $cl->setForce();
1263  $cl->execute();
1264  $this->output( "done.\n" );
1265  }
1266 
1271  protected function migrateComments() {
1272  if ( !$this->updateRowExists( 'MigrateComments' ) ) {
1273  $this->output(
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"
1277  );
1278  $task = $this->maintenance->runChild( MigrateComments::class, 'migrateComments.php' );
1279  $ok = $task->execute();
1280  $this->output( $ok ? "done.\n" : "errors were encountered.\n" );
1281  }
1282  }
1283 
1288  protected function migrateImageCommentTemp() {
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'
1293  );
1294  // @phan-suppress-next-line PhanUndeclaredMethod
1295  $task->setForce();
1296  $ok = $task->execute();
1297  $this->output( $ok ? "done.\n" : "errors were encountered.\n" );
1298  if ( $ok ) {
1299  $this->dropTable( 'image_comment_temp' );
1300  }
1301  }
1302  }
1303 
1308  protected function migrateActors() {
1309  if ( !$this->updateRowExists( 'MigrateActors' ) ) {
1310  $this->output(
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"
1314  );
1315  $task = $this->maintenance->runChild( 'MigrateActors', 'migrateActors.php' );
1316  $ok = $task->execute();
1317  $this->output( $ok ? "done.\n" : "errors were encountered.\n" );
1318  }
1319  }
1320 
1325  protected function migrateArchiveText() {
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' );
1329  // @phan-suppress-next-line PhanUndeclaredMethod
1330  $task->setForce();
1331  if ( $task->execute() ) {
1332  $this->applyPatch( 'patch-drop-ar_text.sql', false,
1333  'Dropping ar_text and ar_flags columns' );
1334  }
1335  }
1336  }
1337 
1342  protected function populateArchiveRevId() {
1343  $info = $this->db->fieldInfo( 'archive', 'ar_rev_id' );
1344  if ( !$info ) {
1345  throw new MWException( 'Missing ar_rev_id field of archive table. Should not happen.' );
1346  }
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' );
1353  }
1354  }
1355  }
1356 
1361  protected function populateExternallinksIndex60() {
1362  if ( !$this->updateRowExists( 'populate externallinks.el_index_60' ) ) {
1363  $this->output(
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"
1367  );
1368  $task = $this->maintenance->runChild( 'PopulateExternallinksIndex60',
1369  'populateExternallinksIndex60.php' );
1370  $task->execute();
1371  $this->output( "done.\n" );
1372  }
1373  }
1374 
1379  protected function populateContentTables() {
1380  if ( !$this->updateRowExists( 'PopulateContentTables' ) ) {
1381  $this->output(
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"
1385  );
1386  $task = $this->maintenance->runChild(
1387  PopulateContentTables::class, 'populateContentTables.php'
1388  );
1389  $ok = $task->execute();
1390  $this->output( $ok ? "done.\n" : "errors were encountered.\n" );
1391  if ( $ok ) {
1392  $this->insertUpdateRow( 'PopulateContentTables' );
1393  }
1394  }
1395  }
1396 
1400  protected function ifNoActorTable( $func, ...$params ) {
1401  wfDeprecated( __METHOD__, '1.35' );
1402  return $this->ifTableNotExists( 'actor', $func, ...$params );
1403  }
1404 
1418  protected function ifTableNotExists( $table, $func, ...$params ) {
1419  // Handle $passSelf from runUpdates().
1420  $passSelf = false;
1421  if ( $table === $this ) {
1422  $passSelf = true;
1423  $table = $func;
1424  $func = array_shift( $params );
1425  }
1426 
1427  if ( $this->db->tableExists( $table, __METHOD__ ) ) {
1428  return null;
1429  }
1430 
1431  if ( !is_array( $func ) && method_exists( $this, $func ) ) {
1432  $func = [ $this, $func ];
1433  } elseif ( $passSelf ) {
1434  array_unshift( $params, $this );
1435  }
1436 
1437  // @phan-suppress-next-line PhanUndeclaredInvokeInCallable Phan is confused
1438  return $func( ...$params );
1439  }
1440 
1455  protected function ifFieldExists( $table, $field, $func, ...$params ) {
1456  // Handle $passSelf from runUpdates().
1457  $passSelf = false;
1458  if ( $table === $this ) {
1459  $passSelf = true;
1460  $table = $field;
1461  $field = $func;
1462  $func = array_shift( $params );
1463  }
1464 
1465  if ( !$this->db->tableExists( $table, __METHOD__ ) ||
1466  !$this->db->fieldExists( $table, $field, __METHOD__ )
1467  ) {
1468  return null;
1469  }
1470 
1471  if ( !is_array( $func ) && method_exists( $this, $func ) ) {
1472  $func = [ $this, $func ];
1473  } elseif ( $passSelf ) {
1474  array_unshift( $params, $this );
1475  }
1476 
1477  // @phan-suppress-next-line PhanUndeclaredInvokeInCallable Phan is confused
1478  return $func( ...$params );
1479  }
1480 
1496  protected function renameIndexes( $table, $patch, $count, $fullpath = false ) {
1497  if ( !$this->doTable( $table ) ) {
1498  return true;
1499  }
1500 
1501  if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
1502  $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
1503  return true;
1504  }
1505 
1506  return $this->applyPatch(
1507  $patch,
1508  $fullpath,
1509  "Renaming $count indexes on $table table"
1510  );
1511  }
1512 
1513 }
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:879
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:966
DatabaseUpdater\copyFile
copyFile( $filename)
Append an SQL fragment to the open file handle.
Definition: DatabaseUpdater.php:641
DatabaseUpdater\$updatesSkipped
array $updatesSkipped
Array of updates that were skipped.
Definition: DatabaseUpdater.php:55
DatabaseUpdater\renameIndexes
renameIndexes( $table, $patch, $count, $fullpath=false)
Rename multiple indexes from an existing table.
Definition: DatabaseUpdater.php:1496
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:683
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:1217
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:2155
DatabaseUpdater
Class for handling database updates.
Definition: DatabaseUpdater.php:40
DatabaseUpdater\populateExternallinksIndex60
populateExternallinksIndex60()
Populates the externallinks.el_index_60 field.
Definition: DatabaseUpdater.php:1361
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:166
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:1146
true
return true
Definition: router.php:90
$wgSharedDB
$wgSharedDB
Shared database for multiple wikis.
Definition: DefaultSettings.php:2144
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:1288
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\doMigrateUserOptions
doMigrateUserOptions()
Migrates user options from the user table blob to user_properties.
Definition: DatabaseUpdater.php:1242
DatabaseUpdater\populateContentTables
populateContentTables()
Populates the MCR content tables.
Definition: DatabaseUpdater.php:1379
DatabaseUpdater\dropField
dropField( $table, $field, $patch, $fullpath=false)
Drop a field from an existing table.
Definition: DatabaseUpdater.php:853
DatabaseUpdater\addField
addField( $table, $field, $patch, $fullpath=false)
Add a new field to an existing table.
Definition: DatabaseUpdater.php:764
DatabaseUpdater\doTable
doTable( $name)
Returns whether updates should be executed on the database table $name.
Definition: DatabaseUpdater.php:608
DatabaseUpdater\getPostDatabaseUpdateMaintenance
getPostDatabaseUpdateMaintenance()
Definition: DatabaseUpdater.php:448
DatabaseUpdater\appendLine
appendLine( $line)
Append a line to the open filehandle.
Definition: DatabaseUpdater.php:663
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
DatabaseUpdater\patchPath
patchPath(IDatabase $db, $patch)
Get the full path of a patch file.
Definition: DatabaseUpdater.php:716
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:1271
DatabaseUpdater\__construct
__construct(IMaintainableDatabase &$db, $shared, Maintenance $maintenance=null)
Stable to call.
Definition: DatabaseUpdater.php:120
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
DatabaseUpdater\doActiveUsersInit
doActiveUsersInit()
Sets the number of active users in the site_stats table.
Definition: DatabaseUpdater.php:1166
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:136
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:1094
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 Stable to override.
Definition: DatabaseUpdater.php:909
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:1325
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:1033
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:1039
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:1253
$queue
$queue
Definition: mergeMessageFileList.php:158
$args
if( $line===false) $args
Definition: mcc.php:124
DatabaseUpdater\ifNoActorTable
ifNoActorTable( $func,... $params)
Definition: DatabaseUpdater.php:1400
Installer\getExistingLocalSettings
static getExistingLocalSettings()
Determine if LocalSettings.php exists.
Definition: Installer.php:643
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:1418
$wgLocalisationCacheConf
$wgLocalisationCacheConf
Localisation cache configuration.
Definition: DefaultSettings.php:2759
DatabaseUpdater\runUpdates
runUpdates(array $updates, $passSelf)
Helper function for doUpdates()
Definition: DatabaseUpdater.php:519
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:552
DatabaseUpdater\doLogSearchPopulation
doLogSearchPopulation()
Migrate log params to new table and index for searching.
Definition: DatabaseUpdater.php:1201
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:1342
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:595
DatabaseUpdater\ifFieldExists
ifFieldExists( $table, $field, $func,... $params)
Only run a function if the named field exists.
Definition: DatabaseUpdater.php:1455
DatabaseUpdater\runMaintenance
runMaintenance( $class, $script)
Run a maintenance script.
Definition: DatabaseUpdater.php:1079
$line
$line
Definition: mcc.php:119
DatabaseUpdater\$fileHandle
resource $fileHandle
File handle for SQL output.
Definition: DatabaseUpdater.php:105
$wgAutoloadClasses
$wgAutoloadClasses
Array mapping class names to filenames, for autoloading.
Definition: DefaultSettings.php:7830
DatabaseUpdater\doLogUsertextPopulation
doLogUsertextPopulation()
Populates the log_user_text field in the logging table.
Definition: DatabaseUpdater.php:1184
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:1002
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:818
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:738
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 type of the DBMS (e.g.
DatabaseUpdater\migrateActors
migrateActors()
Migrate actors to the new 'actor' table.
Definition: DatabaseUpdater.php:1308
Maintenance\setDB
setDB(IMaintainableDatabase $db)
Sets database object to be returned by getDB().
Definition: Maintenance.php:1410
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:1115
$wgCategoryCollation
$wgCategoryCollation
Specify how category names should be sorted, when listed on a category page.
Definition: DefaultSettings.php:8116
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:571
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:524
DatabaseUpdater\addIndex
addIndex( $table, $index, $patch, $fullpath=false)
Add a new index to an existing table.
Definition: DatabaseUpdater.php:792
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:577
$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