MediaWiki  master
DatabaseUpdater.php
Go to the documentation of this file.
1 <?php
33 
34 require_once __DIR__ . '/../../maintenance/Maintenance.php';
35 
43 abstract class DatabaseUpdater {
44  public const REPLICATION_WAIT_TIMEOUT = 300;
45 
51  protected $updates = [];
52 
58  protected $updatesSkipped = [];
59 
64  protected $extensionUpdates = [];
65 
71  protected $db;
72 
76  protected $maintenance;
77 
78  protected $shared = false;
79 
82 
88  DeleteDefaultMessages::class,
89  PopulateRevisionLength::class,
90  PopulateRevisionSha1::class,
91  PopulateImageSha1::class,
92  PopulateFilearchiveSha1::class,
93  PopulateBacklinkNamespace::class,
94  FixDefaultJsonContentPages::class,
95  CleanupEmptyCategories::class,
96  AddRFCandPMIDInterwiki::class,
97  PopulatePPSortKey::class,
98  PopulateIpChanges::class,
99  ];
100 
106  protected $fileHandle = null;
107 
113  protected $skipSchema = false;
114 
121  protected function __construct(
123  $shared,
125  ) {
126  $this->db = $db;
127  $this->db->setFlag( DBO_DDLMODE );
128  $this->shared = $shared;
129  if ( $maintenance ) {
130  $this->maintenance = $maintenance;
131  $this->fileHandle = $maintenance->fileHandle;
132  } else {
133  $this->maintenance = new FakeMaintenance;
134  }
135  $this->maintenance->setDB( $db );
136  }
137 
141  private function loadExtensionSchemaUpdates() {
142  $hookContainer = $this->loadExtensions();
143  ( new HookRunner( $hookContainer ) )->onLoadExtensionSchemaUpdates( $this );
144  }
145 
152  private function loadExtensions() {
153  if ( $this->autoExtensionHookContainer ) {
154  // Already injected by installer
156  }
157  if ( defined( 'MW_EXTENSIONS_LOADED' ) ) {
158  throw new Exception( __METHOD__ .
159  ' apparently called from installer but no hook container was injected' );
160  }
161  if ( !defined( 'MEDIAWIKI_INSTALL' ) ) {
162  // Running under update.php: just use global locator
163  return MediaWikiServices::getInstance()->getHookContainer();
164  }
166 
167  $registry = ExtensionRegistry::getInstance();
168  $queue = $registry->getQueue();
169  // Don't accidentally load extensions in the future
170  $registry->clearQueue();
171 
172  // Read extension.json files
173  $extInfo = $registry->readFromQueue( $queue );
174 
175  // Merge extension attribute hooks with hooks defined by a .php
176  // registration file included from LocalSettings.php
177  $legacySchemaHooks = $extInfo['globals']['wgHooks']['LoadExtensionSchemaUpdates'] ?? [];
178  if ( $vars && isset( $vars['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
179  $legacySchemaHooks = array_merge( $legacySchemaHooks, $vars['wgHooks']['LoadExtensionSchemaUpdates'] );
180  }
181 
182  // Register classes defined by extensions that are loaded by including of a file that
183  // updates global variables, rather than having an extension.json manifest.
184  if ( $vars && isset( $vars['wgAutoloadClasses'] ) ) {
185  AutoLoader::registerClasses( $vars['wgAutoloadClasses'] );
186  }
187 
188  // Register class definitions from extension.json files
189  if ( !isset( $extInfo['autoloaderPaths'] )
190  || !isset( $extInfo['autoloaderClasses'] )
191  || !isset( $extInfo['autoloaderNS'] )
192  ) {
193  // NOTE: protect against changes to the structure of $extInfo. It's volatile, and this usage easy to miss.
194  throw new LogicException( 'Missing autoloader keys from extracted extension info' );
195  }
196  AutoLoader::loadFiles( $extInfo['autoloaderPaths'] );
197  AutoLoader::registerClasses( $extInfo['autoloaderClasses'] );
198  AutoLoader::registerNamespaces( $extInfo['autoloaderNS'] );
199 
200  return new HookContainer(
201  new StaticHookRegistry(
202  [ 'LoadExtensionSchemaUpdates' => $legacySchemaHooks ],
203  $extInfo['attributes']['Hooks'] ?? [],
204  $extInfo['attributes']['DeprecatedHooks'] ?? []
205  ),
206  MediaWikiServices::getInstance()->getObjectFactory()
207  );
208  }
209 
218  public static function newForDB(
220  $shared = false,
222  ) {
223  $type = $db->getType();
224  if ( in_array( $type, Installer::getDBTypes() ) ) {
225  $class = ucfirst( $type ) . 'Updater';
226 
227  return new $class( $db, $shared, $maintenance );
228  } else {
229  throw new MWException( __METHOD__ . ' called for unsupported $wgDBtype' );
230  }
231  }
232 
240  public function setAutoExtensionHookContainer( HookContainer $hookContainer ) {
241  $this->autoExtensionHookContainer = $hookContainer;
242  }
243 
249  public function getDB() {
250  return $this->db;
251  }
252 
259  public function output( $str ) {
260  if ( $this->maintenance->isQuiet() ) {
261  return;
262  }
263  global $wgCommandLineMode;
264  if ( !$wgCommandLineMode ) {
265  $str = htmlspecialchars( $str );
266  }
267  echo $str;
268  flush();
269  }
270 
283  public function addExtensionUpdate( array $update ) {
284  $this->extensionUpdates[] = $update;
285  }
286 
297  public function addExtensionTable( $tableName, $sqlPath ) {
298  $this->extensionUpdates[] = [ 'addTable', $tableName, $sqlPath, true ];
299  }
300 
311  public function addExtensionIndex( $tableName, $indexName, $sqlPath ) {
312  $this->extensionUpdates[] = [ 'addIndex', $tableName, $indexName, $sqlPath, true ];
313  }
314 
325  public function addExtensionField( $tableName, $columnName, $sqlPath ) {
326  $this->extensionUpdates[] = [ 'addField', $tableName, $columnName, $sqlPath, true ];
327  }
328 
339  public function dropExtensionField( $tableName, $columnName, $sqlPath ) {
340  $this->extensionUpdates[] = [ 'dropField', $tableName, $columnName, $sqlPath, true ];
341  }
342 
353  public function dropExtensionIndex( $tableName, $indexName, $sqlPath ) {
354  $this->extensionUpdates[] = [ 'dropIndex', $tableName, $indexName, $sqlPath, true ];
355  }
356 
366  public function dropExtensionTable( $tableName, $sqlPath = false ) {
367  $this->extensionUpdates[] = [ 'dropTable', $tableName, $sqlPath, true ];
368  }
369 
383  public function renameExtensionIndex( $tableName, $oldIndexName, $newIndexName,
384  $sqlPath, $skipBothIndexExistWarning = false
385  ) {
386  $this->extensionUpdates[] = [
387  'renameIndex',
388  $tableName,
389  $oldIndexName,
390  $newIndexName,
391  $skipBothIndexExistWarning,
392  $sqlPath,
393  true
394  ];
395  }
396 
407  public function modifyExtensionField( $tableName, $fieldName, $sqlPath ) {
408  $this->extensionUpdates[] = [ 'modifyField', $tableName, $fieldName, $sqlPath, true ];
409  }
410 
420  public function modifyExtensionTable( $tableName, $sqlPath ) {
421  $this->extensionUpdates[] = [ 'modifyTable', $tableName, $sqlPath, true ];
422  }
423 
430  public function tableExists( $tableName ) {
431  return ( $this->db->tableExists( $tableName, __METHOD__ ) );
432  }
433 
441  public function fieldExists( $tableName, $fieldName ) {
442  return ( $this->db->fieldExists( $tableName, $fieldName, __METHOD__ ) );
443  }
444 
454  public function addPostDatabaseUpdateMaintenance( $class ) {
455  $this->postDatabaseUpdateMaintenance[] = $class;
456  }
457 
463  protected function getExtensionUpdates() {
465  }
466 
474  }
475 
481  private function writeSchemaUpdateFile() {
483  $this->updatesSkipped = [];
484 
485  foreach ( $updates as [ $func, $args, $origParams ] ) {
486  // @phan-suppress-next-line PhanUndeclaredInvokeInCallable
487  $func( ...$args );
488  flush();
489  $this->updatesSkipped[] = $origParams;
490  }
491  }
492 
504  public function getSchemaVars() {
505  return []; // DB-type specific
506  }
507 
513  public function doUpdates( array $what = [ 'core', 'extensions', 'stats' ] ) {
514  $this->db->setSchemaVars( $this->getSchemaVars() );
515 
516  $what = array_fill_keys( $what, true );
517  $this->skipSchema = isset( $what['noschema'] ) || $this->fileHandle !== null;
518  if ( isset( $what['core'] ) ) {
519  $this->doCollationUpdate();
520  $this->runUpdates( $this->getCoreUpdateList(), false );
521  }
522  if ( isset( $what['extensions'] ) ) {
523  $this->loadExtensionSchemaUpdates();
524  $this->runUpdates( $this->getExtensionUpdates(), true );
525  }
526 
527  if ( isset( $what['stats'] ) ) {
528  $this->checkStats();
529  }
530 
531  if ( $this->fileHandle ) {
532  $this->skipSchema = false;
533  $this->writeSchemaUpdateFile();
534  }
535  }
536 
543  private function runUpdates( array $updates, $passSelf ) {
544  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
545 
546  $updatesDone = [];
547  $updatesSkipped = [];
548  foreach ( $updates as $params ) {
549  $origParams = $params;
550  $func = array_shift( $params );
551  if ( !is_array( $func ) && method_exists( $this, $func ) ) {
552  $func = [ $this, $func ];
553  } elseif ( $passSelf ) {
554  array_unshift( $params, $this );
555  }
556  $ret = $func( ...$params );
557  flush();
558  if ( $ret !== false ) {
559  $updatesDone[] = $origParams;
560  $lbFactory->waitForReplication( [ 'timeout' => self::REPLICATION_WAIT_TIMEOUT ] );
561  } else {
562  $updatesSkipped[] = [ $func, $params, $origParams ];
563  }
564  }
565  $this->updatesSkipped = array_merge( $this->updatesSkipped, $updatesSkipped );
566  $this->updates = array_merge( $this->updates, $updatesDone );
567  }
568 
576  public function updateRowExists( $key ) {
577  $row = $this->db->selectRow(
578  'updatelog',
579  # T67813
580  '1 AS X',
581  [ 'ul_key' => $key ],
582  __METHOD__
583  );
584 
585  return (bool)$row;
586  }
587 
601  public function insertUpdateRow( $key, $val = null ) {
602  $this->db->clearFlag( DBO_DDLMODE );
603  $values = [ 'ul_key' => $key ];
604  if ( $val && $this->canUseNewUpdatelog() ) {
605  $values['ul_value'] = $val;
606  }
607  $this->db->insert( 'updatelog', $values, __METHOD__, [ 'IGNORE' ] );
608  $this->db->setFlag( DBO_DDLMODE );
609  }
610 
619  protected function canUseNewUpdatelog() {
620  return $this->db->tableExists( 'updatelog', __METHOD__ ) &&
621  $this->db->fieldExists( 'updatelog', 'ul_value', __METHOD__ );
622  }
623 
632  protected function doTable( $name ) {
634 
635  // Don't bother to check $wgSharedTables if there isn't a shared database
636  // or the user actually also wants to do updates on the shared database.
637  if ( $wgSharedDB === null || $this->shared ) {
638  return true;
639  }
640 
641  if ( in_array( $name, $wgSharedTables ) ) {
642  $this->output( "...skipping update to shared table $name.\n" );
643  return false;
644  } else {
645  return true;
646  }
647  }
648 
656  abstract protected function getCoreUpdateList();
657 
665  protected function copyFile( $filename ) {
666  $this->db->sourceFile(
667  $filename,
668  null,
669  null,
670  __METHOD__,
671  function ( $line ) {
672  return $this->appendLine( $line );
673  }
674  );
675  }
676 
689  protected function appendLine( $line ) {
690  $line = rtrim( $line ) . ";\n";
691  if ( fwrite( $this->fileHandle, $line ) === false ) {
692  throw new MWException( "trouble writing file" );
693  }
694 
695  return false;
696  }
697 
709  protected function applyPatch( $path, $isFullPath = false, $msg = null ) {
710  $msg ??= "Applying $path patch";
711  if ( $this->skipSchema ) {
712  $this->output( "...skipping schema change ($msg).\n" );
713 
714  return false;
715  }
716 
717  $this->output( "{$msg}..." );
718 
719  if ( !$isFullPath ) {
720  $path = $this->patchPath( $this->db, $path );
721  }
722  if ( $this->fileHandle !== null ) {
723  $this->copyFile( $path );
724  } else {
725  $this->db->sourceFile( $path );
726  }
727  $this->output( "done.\n" );
728 
729  return true;
730  }
731 
740  public function patchPath( IDatabase $db, $patch ) {
741  $baseDir = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::BaseDirectory );
742 
743  $dbType = $db->getType();
744  if ( file_exists( "$baseDir/maintenance/$dbType/archives/$patch" ) ) {
745  return "$baseDir/maintenance/$dbType/archives/$patch";
746  } else {
747  return "$baseDir/maintenance/archives/$patch";
748  }
749  }
750 
762  protected function addTable( $name, $patch, $fullpath = false ) {
763  if ( !$this->doTable( $name ) ) {
764  return true;
765  }
766 
767  if ( $this->db->tableExists( $name, __METHOD__ ) ) {
768  $this->output( "...$name table already exists.\n" );
769  } else {
770  return $this->applyPatch( $patch, $fullpath, "Creating $name table" );
771  }
772 
773  return true;
774  }
775 
788  protected function addField( $table, $field, $patch, $fullpath = false ) {
789  if ( !$this->doTable( $table ) ) {
790  return true;
791  }
792 
793  if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
794  $this->output( "...$table table does not exist, skipping new field patch.\n" );
795  } elseif ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
796  $this->output( "...have $field field in $table table.\n" );
797  } else {
798  return $this->applyPatch( $patch, $fullpath, "Adding $field field to table $table" );
799  }
800 
801  return true;
802  }
803 
816  protected function addIndex( $table, $index, $patch, $fullpath = false ) {
817  if ( !$this->doTable( $table ) ) {
818  return true;
819  }
820 
821  if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
822  $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
823  } elseif ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
824  $this->output( "...index $index already set on $table table.\n" );
825  } else {
826  return $this->applyPatch( $patch, $fullpath, "Adding index $index to table $table" );
827  }
828 
829  return true;
830  }
831 
844  protected function dropField( $table, $field, $patch, $fullpath = false ) {
845  if ( !$this->doTable( $table ) ) {
846  return true;
847  }
848 
849  if ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
850  return $this->applyPatch( $patch, $fullpath, "Table $table contains $field field. Dropping" );
851  } else {
852  $this->output( "...$table table does not contain $field field.\n" );
853  }
854 
855  return true;
856  }
857 
870  protected function dropIndex( $table, $index, $patch, $fullpath = false ) {
871  if ( !$this->doTable( $table ) ) {
872  return true;
873  }
874 
875  if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
876  return $this->applyPatch( $patch, $fullpath, "Dropping $index index from table $table" );
877  } else {
878  $this->output( "...$index key doesn't exist.\n" );
879  }
880 
881  return true;
882  }
883 
900  protected function renameIndex( $table, $oldIndex, $newIndex,
901  $skipBothIndexExistWarning, $patch, $fullpath = false
902  ) {
903  if ( !$this->doTable( $table ) ) {
904  return true;
905  }
906 
907  // First requirement: the table must exist
908  if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
909  $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
910 
911  return true;
912  }
913 
914  // Second requirement: the new index must be missing
915  if ( $this->db->indexExists( $table, $newIndex, __METHOD__ ) ) {
916  $this->output( "...index $newIndex already set on $table table.\n" );
917  if ( !$skipBothIndexExistWarning &&
918  $this->db->indexExists( $table, $oldIndex, __METHOD__ )
919  ) {
920  $this->output( "...WARNING: $oldIndex still exists, despite it has " .
921  "been renamed into $newIndex (which also exists).\n" .
922  " $oldIndex should be manually removed if not needed anymore.\n" );
923  }
924 
925  return true;
926  }
927 
928  // Third requirement: the old index must exist
929  if ( !$this->db->indexExists( $table, $oldIndex, __METHOD__ ) ) {
930  $this->output( "...skipping: index $oldIndex doesn't exist.\n" );
931 
932  return true;
933  }
934 
935  // Requirements have been satisfied, patch can be applied
936  return $this->applyPatch(
937  $patch,
938  $fullpath,
939  "Renaming index $oldIndex into $newIndex to table $table"
940  );
941  }
942 
957  protected function dropTable( $table, $patch = false, $fullpath = false ) {
958  if ( !$this->doTable( $table ) ) {
959  return true;
960  }
961 
962  if ( $this->db->tableExists( $table, __METHOD__ ) ) {
963  $msg = "Dropping table $table";
964 
965  if ( $patch === false ) {
966  $this->output( "$msg ..." );
967  $this->db->dropTable( $table, __METHOD__ );
968  $this->output( "done.\n" );
969  } else {
970  return $this->applyPatch( $patch, $fullpath, $msg );
971  }
972  } else {
973  $this->output( "...$table doesn't exist.\n" );
974  }
975 
976  return true;
977  }
978 
993  protected function modifyField( $table, $field, $patch, $fullpath = false ) {
994  if ( !$this->doTable( $table ) ) {
995  return true;
996  }
997 
998  $updateKey = "$table-$field-$patch";
999  if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
1000  $this->output( "...$table table does not exist, skipping modify field patch.\n" );
1001  } elseif ( !$this->db->fieldExists( $table, $field, __METHOD__ ) ) {
1002  $this->output( "...$field field does not exist in $table table, " .
1003  "skipping modify field patch.\n" );
1004  } elseif ( $this->updateRowExists( $updateKey ) ) {
1005  $this->output( "...$field in table $table already modified by patch $patch.\n" );
1006  } else {
1007  $apply = $this->applyPatch( $patch, $fullpath, "Modifying $field field of table $table" );
1008  if ( $apply ) {
1009  $this->insertUpdateRow( $updateKey );
1010  }
1011  return $apply;
1012  }
1013  return true;
1014  }
1015 
1030  protected function modifyTable( $table, $patch, $fullpath = false ) {
1031  if ( !$this->doTable( $table ) ) {
1032  return true;
1033  }
1034 
1035  $updateKey = "$table-$patch";
1036  if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
1037  $this->output( "...$table table does not exist, skipping modify table patch.\n" );
1038  } elseif ( $this->updateRowExists( $updateKey ) ) {
1039  $this->output( "...table $table already modified by patch $patch.\n" );
1040  } else {
1041  $apply = $this->applyPatch( $patch, $fullpath, "Modifying table $table" );
1042  if ( $apply ) {
1043  $this->insertUpdateRow( $updateKey );
1044  }
1045  return $apply;
1046  }
1047  return true;
1048  }
1049 
1070  protected function runMaintenance( $class, $script ) {
1071  $this->output( "Running $script...\n" );
1072  $task = $this->maintenance->runChild( $class );
1073  $ok = $task->execute();
1074  if ( !$ok ) {
1075  throw new RuntimeException( "Execution of $script did not complete successfully." );
1076  }
1077  $this->output( "done.\n" );
1078  }
1079 
1085  public function setFileAccess() {
1086  $repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
1087  $zonePath = $repo->getZonePath( 'temp' );
1088  if ( $repo->getBackend()->directoryExists( [ 'dir' => $zonePath ] ) ) {
1089  // If the directory was never made, then it will have the right ACLs when it is made
1090  $status = $repo->getBackend()->secure( [
1091  'dir' => $zonePath,
1092  'noAccess' => true,
1093  'noListing' => true
1094  ] );
1095  if ( $status->isOK() ) {
1096  $this->output( "Set the local repo temp zone container to be private.\n" );
1097  } else {
1098  $this->output( "Failed to set the local repo temp zone container to be private.\n" );
1099  }
1100  }
1101  }
1102 
1106  public function purgeCache() {
1107  global $wgLocalisationCacheConf;
1108  // We can't guarantee that the user will be able to use TRUNCATE,
1109  // but we know that DELETE is available to us
1110  $this->output( "Purging caches..." );
1111 
1112  // ObjectCache
1113  $this->db->delete( 'objectcache', '*', __METHOD__ );
1114 
1115  // LocalisationCache
1116  if ( $wgLocalisationCacheConf['manualRecache'] ) {
1117  $this->rebuildLocalisationCache();
1118  }
1119 
1120  // ResourceLoader: Message cache
1121  $services = MediaWikiServices::getInstance();
1122  MessageBlobStore::clearGlobalCacheEntry(
1123  $services->getMainWANObjectCache()
1124  );
1125 
1126  // ResourceLoader: File-dependency cache
1127  $this->db->delete( 'module_deps', '*', __METHOD__ );
1128  $this->output( "done.\n" );
1129  }
1130 
1134  protected function checkStats() {
1135  $this->output( "...site_stats is populated..." );
1136  $row = $this->db->newSelectQueryBuilder()
1137  ->select( '*' )
1138  ->from( 'site_stats' )
1139  ->where( [ 'ss_row_id' => 1 ] )
1140  ->caller( __METHOD__ )->fetchRow();
1141  if ( $row === false ) {
1142  $this->output( "data is missing! rebuilding...\n" );
1143  } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
1144  $this->output( "missing ss_total_pages, rebuilding...\n" );
1145  } else {
1146  $this->output( "done.\n" );
1147 
1148  return;
1149  }
1150  SiteStatsInit::doAllAndCommit( $this->db );
1151  }
1152 
1153  # Common updater functions
1154 
1158  protected function doCollationUpdate() {
1159  global $wgCategoryCollation;
1160  $collationNeeded = $this->db->newSelectQueryBuilder()
1161  ->select( 'COUNT(*)' )
1162  ->from( 'categorylinks' )
1163  ->where( 'cl_collation != ' . $this->db->addQuotes( $wgCategoryCollation ) )
1164  ->caller( __METHOD__ )->fetchField();
1165  if ( $collationNeeded == 0 ) {
1166  $this->output( "...collations up-to-date.\n" );
1167 
1168  return;
1169  }
1170 
1171  $this->output( "Updating category collations..." );
1172  $task = $this->maintenance->runChild( UpdateCollation::class );
1173  $task->execute();
1174  $this->output( "...done.\n" );
1175  }
1176 
1177  protected function doConvertDjvuMetadata() {
1178  if ( $this->updateRowExists( 'ConvertDjvuMetadata' ) ) {
1179  return;
1180  }
1181  $this->output( "Converting djvu metadata..." );
1182  $task = $this->maintenance->runChild( RefreshImageMetadata::class );
1183  '@phan-var RefreshImageMetadata $task';
1184  $task->loadParamsAndArgs( RefreshImageMetadata::class, [
1185  'force' => true,
1186  'mediatype' => 'OFFICE',
1187  'mime' => 'image/*',
1188  'batch-size' => 1,
1189  'sleep' => 1
1190  ] );
1191  $ok = $task->execute();
1192  if ( $ok !== false ) {
1193  $this->output( "...done.\n" );
1194  $this->insertUpdateRow( 'ConvertDjvuMetadata' );
1195  }
1196  }
1197 
1201  protected function rebuildLocalisationCache() {
1205  $cl = $this->maintenance->runChild(
1206  RebuildLocalisationCache::class, 'rebuildLocalisationCache.php'
1207  );
1208  '@phan-var RebuildLocalisationCache $cl';
1209  $this->output( "Rebuilding localisation cache...\n" );
1210  $cl->setForce();
1211  $cl->execute();
1212  $this->output( "done.\n" );
1213  }
1214 
1215  protected function migrateTemplatelinks() {
1216  if ( $this->updateRowExists( MigrateLinksTable::class . 'templatelinks' ) ) {
1217  $this->output( "...templatelinks table has already been migrated.\n" );
1218  return;
1219  }
1223  $task = $this->maintenance->runChild(
1224  MigrateLinksTable::class, 'migrateLinksTable.php'
1225  );
1226  '@phan-var MigrateLinksTable $task';
1227  $task->loadParamsAndArgs( MigrateLinksTable::class, [
1228  'force' => true,
1229  'table' => 'templatelinks'
1230  ] );
1231  $this->output( "Running migrateLinksTable.php on templatelinks...\n" );
1232  $task->execute();
1233  $this->output( "done.\n" );
1234  }
1235 
1249  protected function ifTableNotExists( $table, $func, ...$params ) {
1250  // Handle $passSelf from runUpdates().
1251  $passSelf = false;
1252  if ( $table === $this ) {
1253  $passSelf = true;
1254  $table = $func;
1255  $func = array_shift( $params );
1256  }
1257 
1258  if ( $this->db->tableExists( $table, __METHOD__ ) ) {
1259  return null;
1260  }
1261 
1262  if ( !is_array( $func ) && method_exists( $this, $func ) ) {
1263  $func = [ $this, $func ];
1264  } elseif ( $passSelf ) {
1265  array_unshift( $params, $this );
1266  }
1267 
1268  // @phan-suppress-next-line PhanUndeclaredInvokeInCallable Phan is confused
1269  return $func( ...$params );
1270  }
1271 
1286  protected function ifFieldExists( $table, $field, $func, ...$params ) {
1287  // Handle $passSelf from runUpdates().
1288  $passSelf = false;
1289  if ( $table === $this ) {
1290  $passSelf = true;
1291  $table = $field;
1292  $field = $func;
1293  $func = array_shift( $params );
1294  }
1295 
1296  if ( !$this->db->tableExists( $table, __METHOD__ ) ||
1297  !$this->db->fieldExists( $table, $field, __METHOD__ )
1298  ) {
1299  return null;
1300  }
1301 
1302  if ( !is_array( $func ) && method_exists( $this, $func ) ) {
1303  $func = [ $this, $func ];
1304  } elseif ( $passSelf ) {
1305  array_unshift( $params, $this );
1306  }
1307 
1308  // @phan-suppress-next-line PhanUndeclaredInvokeInCallable Phan is confused
1309  return $func( ...$params );
1310  }
1311 
1312 }
if(MW_ENTRY_POINT==='index') global $wgCommandLineMode
Class for handling database updates.
purgeCache()
Purge various database caches.
addExtensionTable( $tableName, $sqlPath)
Convenience wrapper for addExtensionUpdate() when adding a new table (which is the most common usage ...
getDB()
Get a database connection to run updates.
setFileAccess()
Set any .htaccess files or equivalent for storage repos.
addIndex( $table, $index, $patch, $fullpath=false)
Add a new index to an existing table.
array $updates
Array of updates to perform on the database.
modifyExtensionTable( $tableName, $sqlPath)
Modify an existing extension table.
bool $skipSchema
Flag specifying whether or not to skip schema (e.g.
runMaintenance( $class, $script)
Run a maintenance script.
addField( $table, $field, $patch, $fullpath=false)
Add a new field to an existing table.
addExtensionIndex( $tableName, $indexName, $sqlPath)
Add an index to an existing extension table.
modifyTable( $table, $patch, $fullpath=false)
Modify an existing table, similar to modifyField.
doUpdates(array $what=[ 'core', 'extensions', 'stats'])
Do all the updates.
Maintenance $maintenance
ifTableNotExists( $table, $func,... $params)
Only run a function if a table does not exist.
updateRowExists( $key)
Helper function: check if the given key is present in the updatelog table.
const REPLICATION_WAIT_TIMEOUT
getCoreUpdateList()
Get an array of updates to perform on the database.
output( $str)
Output some text.
getExtensionUpdates()
Get the list of extension-defined updates.
insertUpdateRow( $key, $val=null)
Helper function: Add a key to the updatelog table.
setAutoExtensionHookContainer(HookContainer $hookContainer)
Set the HookContainer to use for loading extension schema updates.
canUseNewUpdatelog()
Updatelog was changed in 1.17 to have a ul_value column so we can record more information about what ...
patchPath(IDatabase $db, $patch)
Get the full path of a patch file.
copyFile( $filename)
Append an SQL fragment to the open file handle.
tableExists( $tableName)
string[] $postDatabaseUpdateMaintenance
Scripts to run after database update Should be a subclass of LoggedUpdateMaintenance.
HookContainer null $autoExtensionHookContainer
dropExtensionTable( $tableName, $sqlPath=false)
Drop an extension table.
resource null $fileHandle
File handle for SQL output.
renameIndex( $table, $oldIndex, $newIndex, $skipBothIndexExistWarning, $patch, $fullpath=false)
Rename an index from an existing table.
rebuildLocalisationCache()
Rebuilds the localisation cache.
addExtensionField( $tableName, $columnName, $sqlPath)
Add a field to an existing extension table.
IMaintainableDatabase $db
Handle to the database subclass.
ifFieldExists( $table, $field, $func,... $params)
Only run a function if the named field exists.
addTable( $name, $patch, $fullpath=false)
Add a new table to the database.
fieldExists( $tableName, $fieldName)
modifyField( $table, $field, $patch, $fullpath=false)
Modify an existing field.
dropExtensionField( $tableName, $columnName, $sqlPath)
Drop a field from an extension table.
static newForDB(IMaintainableDatabase $db, $shared=false, Maintenance $maintenance=null)
addPostDatabaseUpdateMaintenance( $class)
Add a maintenance script to be run after the database updates are complete.
dropField( $table, $field, $patch, $fullpath=false)
Drop a field from an existing table.
doCollationUpdate()
Update CategoryLinks collation.
renameExtensionIndex( $tableName, $oldIndexName, $newIndexName, $sqlPath, $skipBothIndexExistWarning=false)
Rename an index on an extension table Intended for use in LoadExtensionSchemaUpdates hook handlers.
addExtensionUpdate(array $update)
Add a new update coming from an extension.
array $updatesSkipped
Array of updates that were skipped.
appendLine( $line)
Append a line to the open filehandle.
dropExtensionIndex( $tableName, $indexName, $sqlPath)
Drop an index from an extension table Intended for use in LoadExtensionSchemaUpdates hook handlers.
array $extensionUpdates
List of extension-provided database updates.
checkStats()
Check the site_stats table is not properly populated.
modifyExtensionField( $tableName, $fieldName, $sqlPath)
Modify an existing field in an extension table.
applyPatch( $path, $isFullPath=false, $msg=null)
Applies a SQL patch.
dropIndex( $table, $index, $patch, $fullpath=false)
Drop an index from an existing table.
__construct(IMaintainableDatabase &$db, $shared, Maintenance $maintenance=null)
dropTable( $table, $patch=false, $fullpath=false)
If the specified table exists, drop it, or execute the patch if one is provided.
getSchemaVars()
Get appropriate schema variables in the current database connection.
doTable( $name)
Returns whether updates should be executed on the database table $name.
Fake maintenance wrapper, mostly used for the web installer/updater.
static getExistingLocalSettings()
Determine if LocalSettings.php exists.
Definition: Installer.php:663
static getDBTypes()
Get a list of known DB types.
Definition: Installer.php:551
MediaWiki exception.
Definition: MWException.php:33
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:66
setDB(IMaintainableDatabase $db)
Sets database object to be returned by getDB().
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:567
This is a simple immutable HookRegistry which can be used to set up a local HookContainer in tests an...
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
This class generates message blobs for use by ResourceLoader.
Class designed for counting of stats.
$wgSharedTables
Config variable stub for the SharedTables setting, for use by phpdoc and IDEs.
$wgCategoryCollation
Config variable stub for the CategoryCollation setting, for use by phpdoc and IDEs.
$wgSharedDB
Config variable stub for the SharedDB setting, for use by phpdoc and IDEs.
$wgLocalisationCacheConf
Config variable stub for the LocalisationCacheConf setting, for use by phpdoc and IDEs.
setFlag( $flag, $remember=self::REMEMBER_NOTHING)
Set a flag for this connection.
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:36
Advanced database interface for IDatabase handles that include maintenance methods.
getType()
Get the RDBMS type of the server (e.g.
const DBO_DDLMODE
Definition: defines.php:16
return true
Definition: router.php:90