Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
17.96% |
95 / 529 |
|
4.62% |
3 / 65 |
CRAP | |
0.00% |
0 / 1 |
| DatabaseUpdater | |
18.03% |
95 / 527 |
|
4.62% |
3 / 65 |
19656.62 | |
0.00% |
0 / 1 |
| __construct | |
75.00% |
6 / 8 |
|
0.00% |
0 / 1 |
2.06 | |||
| loadExtensionSchemaUpdates | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| loadExtensions | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| newForDB | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| setAutoExtensionHookContainer | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDB | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| outputApplied | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| outputAppliedSummary | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| output | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
3.33 | |||
| addExtensionUpdate | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addExtensionUpdateOnVirtualDomain | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addExtensionTable | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addExtensionIndex | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addExtensionField | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| dropExtensionField | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| dropExtensionIndex | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| dropExtensionTable | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| renameExtensionIndex | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| modifyExtensionField | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| modifyExtensionTable | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| tableExists | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| fieldExists | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addPostDatabaseUpdateMaintenance | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPostDatabaseUpdateMaintenance | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| writeSchemaUpdateFile | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| getSchemaVars | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| doUpdates | |
45.00% |
9 / 20 |
|
0.00% |
0 / 1 |
18.65 | |||
| runUpdates | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
110 | |||
| getLBFactory | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| updateRowExists | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| insertUpdateRow | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 | |||
| insertInitialUpdateKeys | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| doTable | |
50.00% |
5 / 10 |
|
0.00% |
0 / 1 |
10.50 | |||
| checkSchemaAltersAllowed | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| getCoreUpdateList | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getInitialUpdateKeys | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| copyFile | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| appendLine | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| applyPatch | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
| patchPath | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| addTable | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| addField | |
36.36% |
4 / 11 |
|
0.00% |
0 / 1 |
11.44 | |||
| addIndex | |
36.36% |
4 / 11 |
|
0.00% |
0 / 1 |
11.44 | |||
| dropField | |
27.27% |
3 / 11 |
|
0.00% |
0 / 1 |
14.62 | |||
| dropIndex | |
33.33% |
4 / 12 |
|
0.00% |
0 / 1 |
12.41 | |||
| renameIndex | |
16.67% |
4 / 24 |
|
0.00% |
0 / 1 |
45.04 | |||
| dropTable | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
| modifyField | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| modifyPrimaryKey | |
30.77% |
4 / 13 |
|
0.00% |
0 / 1 |
13.30 | |||
| modifyTable | |
26.67% |
4 / 15 |
|
0.00% |
0 / 1 |
20.20 | |||
| modifyTableIfFieldNotExists | |
22.73% |
5 / 22 |
|
0.00% |
0 / 1 |
56.14 | |||
| modifyFieldIfNullable | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
1.00 | |||
| modifyFieldWithCondition | |
16.67% |
4 / 24 |
|
0.00% |
0 / 1 |
45.04 | |||
| runMaintenance | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| setFileAccess | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
| purgeCache | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| checkStats | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| doCollationUpdate | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| doConvertDjvuMetadata | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| rebuildLocalisationCache | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| migratePagelinks | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
| migrateCategorylinks | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
| normalizeCollation | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
| migrateImagelinks | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
| addMissingTalkPageWatchlistLabels | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| ifTableNotExists | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
42 | |||
| ifFieldExists | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\Installer; |
| 8 | |
| 9 | use CleanupEmptyCategories; |
| 10 | use CleanupWatchlistLabelMember; |
| 11 | use DeleteDefaultMessages; |
| 12 | use LogicException; |
| 13 | use MediaWiki\HookContainer\HookContainer; |
| 14 | use MediaWiki\HookContainer\HookRunner; |
| 15 | use MediaWiki\Maintenance\FakeMaintenance; |
| 16 | use MediaWiki\Maintenance\LoggedUpdateMaintenance; |
| 17 | use MediaWiki\Maintenance\Maintenance; |
| 18 | use MediaWiki\MediaWikiServices; |
| 19 | use MediaWiki\ResourceLoader\MessageBlobStore; |
| 20 | use MediaWiki\SiteStats\SiteStatsInit; |
| 21 | use MigrateLinksTable; |
| 22 | use RebuildLocalisationCache; |
| 23 | use RefreshImageMetadata; |
| 24 | use RuntimeException; |
| 25 | use UnexpectedValueException; |
| 26 | use UpdateCollation; |
| 27 | use Wikimedia\Rdbms\IDatabase; |
| 28 | use Wikimedia\Rdbms\IMaintainableDatabase; |
| 29 | use Wikimedia\Rdbms\LBFactory; |
| 30 | use Wikimedia\Rdbms\Platform\ISQLPlatform; |
| 31 | |
| 32 | require_once __DIR__ . '/../../maintenance/Maintenance.php'; |
| 33 | |
| 34 | /** |
| 35 | * Apply database changes after updating MediaWiki. |
| 36 | * |
| 37 | * @ingroup Installer |
| 38 | * @since 1.17 |
| 39 | */ |
| 40 | abstract class DatabaseUpdater { |
| 41 | public const REPLICATION_WAIT_TIMEOUT = 300; |
| 42 | |
| 43 | /** |
| 44 | * Array of updates to perform on the database |
| 45 | * |
| 46 | * @var array |
| 47 | */ |
| 48 | protected $updates = []; |
| 49 | |
| 50 | /** |
| 51 | * Array of updates that were skipped |
| 52 | * |
| 53 | * @var array |
| 54 | */ |
| 55 | protected $updatesSkipped = []; |
| 56 | |
| 57 | /** |
| 58 | * List of extension-provided database updates |
| 59 | * @var array |
| 60 | */ |
| 61 | protected $extensionUpdates = []; |
| 62 | |
| 63 | /** |
| 64 | * List of extension-provided database updates on virtual domain dbs |
| 65 | * @var array |
| 66 | */ |
| 67 | protected $extensionUpdatesWithVirtualDomains = []; |
| 68 | |
| 69 | /** |
| 70 | * Handle to the database subclass |
| 71 | * |
| 72 | * @var IMaintainableDatabase |
| 73 | */ |
| 74 | protected $db; |
| 75 | |
| 76 | /** |
| 77 | * @var Maintenance |
| 78 | */ |
| 79 | protected $maintenance; |
| 80 | |
| 81 | /** @var bool */ |
| 82 | protected $shared = false; |
| 83 | |
| 84 | /** @var HookContainer|null */ |
| 85 | protected $autoExtensionHookContainer; |
| 86 | |
| 87 | /** |
| 88 | * @var class-string<Maintenance>[] Scripts to run after database update |
| 89 | * Should be a subclass of LoggedUpdateMaintenance |
| 90 | */ |
| 91 | protected $postDatabaseUpdateMaintenance = [ |
| 92 | DeleteDefaultMessages::class, |
| 93 | CleanupEmptyCategories::class, |
| 94 | ]; |
| 95 | |
| 96 | /** |
| 97 | * File handle for SQL output. |
| 98 | * |
| 99 | * @var resource|null |
| 100 | */ |
| 101 | protected $fileHandle = null; |
| 102 | |
| 103 | /** |
| 104 | * Flag specifying whether to skip schema (e.g., SQL-only) updates. |
| 105 | * |
| 106 | * @var bool |
| 107 | */ |
| 108 | protected $skipSchema = false; |
| 109 | |
| 110 | /** |
| 111 | * @var bool Flag specifying whether to skip alter schema updates |
| 112 | */ |
| 113 | protected bool $skipSchemaAlters = false; |
| 114 | |
| 115 | /** |
| 116 | * The virtual domain currently being acted on |
| 117 | * @var string|null |
| 118 | */ |
| 119 | private $currentVirtualDomain = null; |
| 120 | |
| 121 | /** |
| 122 | * Flag specifying whether to output notes about updates that were already applied. |
| 123 | * |
| 124 | * @var bool |
| 125 | */ |
| 126 | public $logApplied = false; |
| 127 | |
| 128 | /** |
| 129 | * When not outputting notes about already applied updates, their number if stored here. |
| 130 | * |
| 131 | * @var int |
| 132 | */ |
| 133 | public $appliedUpdateCount = 0; |
| 134 | |
| 135 | /** |
| 136 | * @param IMaintainableDatabase &$db To perform updates on |
| 137 | * @param bool $shared Whether to perform updates on shared tables |
| 138 | * @param Maintenance|null $maintenance Maintenance object which created us |
| 139 | */ |
| 140 | protected function __construct( |
| 141 | IMaintainableDatabase &$db, |
| 142 | $shared, |
| 143 | ?Maintenance $maintenance = null |
| 144 | ) { |
| 145 | $this->db = $db; |
| 146 | $this->db->setFlag( DBO_DDLMODE ); |
| 147 | $this->shared = $shared; |
| 148 | if ( $maintenance ) { |
| 149 | $this->maintenance = $maintenance; |
| 150 | $this->fileHandle = $maintenance->fileHandle; |
| 151 | } else { |
| 152 | $this->maintenance = new FakeMaintenance; |
| 153 | } |
| 154 | $this->maintenance->setDB( $db ); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Cause extensions to register any updates they need to perform. |
| 159 | */ |
| 160 | private function loadExtensionSchemaUpdates() { |
| 161 | $hookContainer = $this->loadExtensions(); |
| 162 | ( new HookRunner( $hookContainer ) )->onLoadExtensionSchemaUpdates( $this ); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Loads LocalSettings.php, if needed, and initialises everything needed for |
| 167 | * LoadExtensionSchemaUpdates hook. |
| 168 | * |
| 169 | * @return HookContainer |
| 170 | */ |
| 171 | private function loadExtensions() { |
| 172 | if ( $this->autoExtensionHookContainer ) { |
| 173 | // Already injected by installer |
| 174 | return $this->autoExtensionHookContainer; |
| 175 | } |
| 176 | if ( defined( 'MW_EXTENSIONS_LOADED' ) ) { |
| 177 | throw new LogicException( __METHOD__ . |
| 178 | ' apparently called from installer but no hook container was injected' ); |
| 179 | } |
| 180 | if ( !defined( 'MEDIAWIKI_INSTALL' ) ) { |
| 181 | // Running under update.php: use the global locator |
| 182 | return MediaWikiServices::getInstance()->getHookContainer(); |
| 183 | } |
| 184 | // Web upgrade used to load extensions here, but it now injects a hook |
| 185 | // container like install |
| 186 | throw new LogicException( __METHOD__ . |
| 187 | ' an extension hook container needs to be injected' ); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * @param IMaintainableDatabase $db |
| 192 | * @param bool $shared |
| 193 | * @param Maintenance|null $maintenance |
| 194 | * @return DatabaseUpdater |
| 195 | */ |
| 196 | public static function newForDB( |
| 197 | IMaintainableDatabase $db, |
| 198 | $shared = false, |
| 199 | ?Maintenance $maintenance = null |
| 200 | ) { |
| 201 | $type = $db->getType(); |
| 202 | if ( in_array( $type, Installer::getDBTypes() ) ) { |
| 203 | $class = '\\MediaWiki\\Installer\\' . ucfirst( $type ) . 'Updater'; |
| 204 | |
| 205 | return new $class( $db, $shared, $maintenance ); |
| 206 | } |
| 207 | |
| 208 | throw new UnexpectedValueException( __METHOD__ . ' called for unsupported DB type' ); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Set the HookContainer to use for loading extension schema updates. |
| 213 | * |
| 214 | * @internal For use by DatabaseInstaller |
| 215 | * @since 1.36 |
| 216 | * @param HookContainer $hookContainer |
| 217 | */ |
| 218 | public function setAutoExtensionHookContainer( HookContainer $hookContainer ) { |
| 219 | $this->autoExtensionHookContainer = $hookContainer; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Get a database connection to run updates |
| 224 | * |
| 225 | * @return IMaintainableDatabase |
| 226 | */ |
| 227 | public function getDB() { |
| 228 | return $this->db; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Output a note about an update that has already been applied. |
| 233 | * These updates may instead be silenced and merely counted. |
| 234 | * |
| 235 | * @param string $str Text to output |
| 236 | */ |
| 237 | public function outputApplied( string $str ): void { |
| 238 | if ( $this->logApplied ) { |
| 239 | $this->output( $str ); |
| 240 | } else { |
| 241 | $this->appliedUpdateCount++; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * If notes about updates that have already been applied are silenced, |
| 247 | * output a message with the count of skipped updates. |
| 248 | */ |
| 249 | public function outputAppliedSummary(): void { |
| 250 | if ( $this->appliedUpdateCount ) { |
| 251 | $this->output( "Skipped {$this->appliedUpdateCount} updates that were already applied.\n" ); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Output some text. If we're running via the web, escape the text first. |
| 257 | * |
| 258 | * @param string $str Text to output |
| 259 | * @param-taint $str escapes_html |
| 260 | */ |
| 261 | public function output( $str ) { |
| 262 | if ( $this->maintenance->isQuiet() ) { |
| 263 | return; |
| 264 | } |
| 265 | if ( MW_ENTRY_POINT !== 'cli' ) { |
| 266 | $str = htmlspecialchars( $str ); |
| 267 | } |
| 268 | echo $str; |
| 269 | flush(); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Add a new update coming from an extension. |
| 274 | * Intended for use in LoadExtensionSchemaUpdates hook handlers. |
| 275 | * |
| 276 | * @since 1.17 |
| 277 | * |
| 278 | * @param array $update The update to run. Format is [ $callback, $params... ] |
| 279 | * $callback is the method to call; either a DatabaseUpdater method name or a callable. |
| 280 | * Must be serializable (i.e., no anonymous functions allowed). The rest of the parameters |
| 281 | * (if any) will be passed to the callback. The first parameter passed to the callback |
| 282 | * is always this object. |
| 283 | */ |
| 284 | public function addExtensionUpdate( array $update ) { |
| 285 | $this->extensionUpdates[] = $update; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Add a new update coming from an extension on virtual domain databases. |
| 290 | * Intended for use in LoadExtensionSchemaUpdates hook handlers. |
| 291 | * |
| 292 | * @since 1.42 |
| 293 | * |
| 294 | * @param array $update The update to run. The format is [ $virtualDomain, $callback, $params... ] |
| 295 | * similarly to addExtensionUpdate() |
| 296 | */ |
| 297 | public function addExtensionUpdateOnVirtualDomain( array $update ) { |
| 298 | $this->extensionUpdatesWithVirtualDomains[] = $update; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Convenience wrapper for addExtensionUpdate() when adding a new table (which |
| 303 | * is the most common usage of updaters in an extension) |
| 304 | * Intended for use in LoadExtensionSchemaUpdates hook handlers. |
| 305 | * |
| 306 | * @since 1.18 |
| 307 | * |
| 308 | * @param string $tableName Name of table to create |
| 309 | * @param string $sqlPath Full path to the schema file |
| 310 | */ |
| 311 | public function addExtensionTable( $tableName, $sqlPath ) { |
| 312 | $this->extensionUpdates[] = [ 'addTable', $tableName, $sqlPath, true ]; |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Add an index to an existing extension table. |
| 317 | * Intended for use in LoadExtensionSchemaUpdates hook handlers. |
| 318 | * |
| 319 | * @since 1.19 |
| 320 | * |
| 321 | * @param string $tableName |
| 322 | * @param string $indexName |
| 323 | * @param string $sqlPath |
| 324 | */ |
| 325 | public function addExtensionIndex( $tableName, $indexName, $sqlPath ) { |
| 326 | $this->extensionUpdates[] = [ 'addIndex', $tableName, $indexName, $sqlPath, true ]; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Add a field to an existing extension table. |
| 331 | * Intended for use in LoadExtensionSchemaUpdates hook handlers. |
| 332 | * |
| 333 | * @since 1.19 |
| 334 | * |
| 335 | * @param string $tableName |
| 336 | * @param string $columnName |
| 337 | * @param string $sqlPath |
| 338 | */ |
| 339 | public function addExtensionField( $tableName, $columnName, $sqlPath ) { |
| 340 | $this->extensionUpdates[] = [ 'addField', $tableName, $columnName, $sqlPath, true ]; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Drop a field from an extension table. |
| 345 | * Intended for use in LoadExtensionSchemaUpdates hook handlers. |
| 346 | * |
| 347 | * @since 1.20 |
| 348 | * |
| 349 | * @param string $tableName |
| 350 | * @param string $columnName |
| 351 | * @param string $sqlPath |
| 352 | */ |
| 353 | public function dropExtensionField( $tableName, $columnName, $sqlPath ) { |
| 354 | $this->extensionUpdates[] = [ 'dropField', $tableName, $columnName, $sqlPath, true ]; |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Drop an index from an extension table |
| 359 | * Intended for use in LoadExtensionSchemaUpdates hook handlers. |
| 360 | * |
| 361 | * @since 1.21 |
| 362 | * |
| 363 | * @param string $tableName |
| 364 | * @param string $indexName |
| 365 | * @param string $sqlPath The path to the SQL change path |
| 366 | */ |
| 367 | public function dropExtensionIndex( $tableName, $indexName, $sqlPath ) { |
| 368 | $this->extensionUpdates[] = [ 'dropIndex', $tableName, $indexName, $sqlPath, true ]; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Drop an extension table. |
| 373 | * Intended for use in LoadExtensionSchemaUpdates hook handlers. |
| 374 | * |
| 375 | * @since 1.20 |
| 376 | * |
| 377 | * @param string $tableName |
| 378 | * @param string|bool $sqlPath |
| 379 | */ |
| 380 | public function dropExtensionTable( $tableName, $sqlPath = false ) { |
| 381 | $this->extensionUpdates[] = [ 'dropTable', $tableName, $sqlPath, true ]; |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Rename an index on an extension table |
| 386 | * Intended for use in LoadExtensionSchemaUpdates hook handlers. |
| 387 | * |
| 388 | * @since 1.21 |
| 389 | * |
| 390 | * @param string $tableName |
| 391 | * @param string $oldIndexName |
| 392 | * @param string $newIndexName |
| 393 | * @param string $sqlPath The path to the SQL change file |
| 394 | * @param bool $skipBothIndexExistWarning Whether to warn if both the old |
| 395 | * and the new indexes exist. [facultative; by default, false] |
| 396 | */ |
| 397 | public function renameExtensionIndex( $tableName, $oldIndexName, $newIndexName, |
| 398 | $sqlPath, $skipBothIndexExistWarning = false |
| 399 | ) { |
| 400 | $this->extensionUpdates[] = [ |
| 401 | 'renameIndex', |
| 402 | $tableName, |
| 403 | $oldIndexName, |
| 404 | $newIndexName, |
| 405 | $skipBothIndexExistWarning, |
| 406 | $sqlPath, |
| 407 | true |
| 408 | ]; |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Modify an existing field in an extension table. |
| 413 | * Intended for use in LoadExtensionSchemaUpdates hook handlers. |
| 414 | * |
| 415 | * @since 1.21 |
| 416 | * |
| 417 | * @param string $tableName |
| 418 | * @param string $fieldName The field to be modified |
| 419 | * @param string $sqlPath The path to the SQL patch |
| 420 | */ |
| 421 | public function modifyExtensionField( $tableName, $fieldName, $sqlPath ) { |
| 422 | $this->extensionUpdates[] = [ 'modifyField', $tableName, $fieldName, $sqlPath, true ]; |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Modify an existing extension table. |
| 427 | * Intended for use in LoadExtensionSchemaUpdates hook handlers. |
| 428 | * |
| 429 | * @since 1.31 |
| 430 | * |
| 431 | * @param string $tableName |
| 432 | * @param string $sqlPath The path to the SQL patch |
| 433 | */ |
| 434 | public function modifyExtensionTable( $tableName, $sqlPath ) { |
| 435 | $this->extensionUpdates[] = [ 'modifyTable', $tableName, $sqlPath, true ]; |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * @since 1.20 |
| 440 | * |
| 441 | * @param string $tableName |
| 442 | * @return bool |
| 443 | */ |
| 444 | public function tableExists( $tableName ) { |
| 445 | return ( $this->db->tableExists( $tableName, __METHOD__ ) ); |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * @since 1.40 |
| 450 | * |
| 451 | * @param string $tableName |
| 452 | * @param string $fieldName |
| 453 | * @return bool |
| 454 | */ |
| 455 | public function fieldExists( $tableName, $fieldName ) { |
| 456 | return ( $this->db->fieldExists( $tableName, $fieldName, __METHOD__ ) ); |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Add a maintenance script to be run after the database updates are complete. |
| 461 | * |
| 462 | * Script should subclass LoggedUpdateMaintenance |
| 463 | * |
| 464 | * @since 1.19 |
| 465 | * |
| 466 | * @param class-string<Maintenance> $class Name of a Maintenance subclass |
| 467 | */ |
| 468 | public function addPostDatabaseUpdateMaintenance( $class ) { |
| 469 | $this->postDatabaseUpdateMaintenance[] = $class; |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * @since 1.17 |
| 474 | * |
| 475 | * @return class-string<Maintenance>[] |
| 476 | */ |
| 477 | public function getPostDatabaseUpdateMaintenance() { |
| 478 | return $this->postDatabaseUpdateMaintenance; |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * @since 1.21 |
| 483 | * |
| 484 | * Writes the schema updates desired to a file for the DB Admin to run. |
| 485 | */ |
| 486 | private function writeSchemaUpdateFile() { |
| 487 | $updates = $this->updatesSkipped; |
| 488 | $this->updatesSkipped = []; |
| 489 | |
| 490 | foreach ( $updates as [ $func, $args, $origParams ] ) { |
| 491 | $func( ...$args ); |
| 492 | flush(); |
| 493 | $this->updatesSkipped[] = $origParams; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Get appropriate schema variables in the current database connection. |
| 499 | * |
| 500 | * This should be called after any request data has been imported, but before |
| 501 | * any write operations to the database. The result should be passed to the DB |
| 502 | * setSchemaVars() method. |
| 503 | * |
| 504 | * @return array |
| 505 | * @since 1.28 |
| 506 | */ |
| 507 | public function getSchemaVars() { |
| 508 | return []; // DB-type specific |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Do all the updates |
| 513 | * |
| 514 | * @param array $what What updates to perform. Supports: |
| 515 | * * 'core' - Run updates for MediaWiki core |
| 516 | * * 'extensions' - Run updates for extensions and skins |
| 517 | * * 'stats' - Check that the site_stats table is populated correctly |
| 518 | * * 'initial' - Inserts the initial update keys from MediaWiki core |
| 519 | * * 'noschema' - Skips all schema updates |
| 520 | * * 'noschema-alters' - Skips all schema updates that involve an ALTER TABLE command, |
| 521 | * intended for use during install.php where the application of schema updates is |
| 522 | * not necessary and may cause permission errors when executing the statements in |
| 523 | * the database |
| 524 | */ |
| 525 | public function doUpdates( array $what = [ 'core', 'extensions', 'stats' ] ) { |
| 526 | $this->db->setSchemaVars( $this->getSchemaVars() ); |
| 527 | |
| 528 | $what = array_fill_keys( $what, true ); |
| 529 | $this->skipSchema = isset( $what['noschema'] ) || $this->fileHandle !== null; |
| 530 | $this->skipSchemaAlters = isset( $what['noschema-alters'] ) || $this->skipSchema; |
| 531 | |
| 532 | if ( isset( $what['initial'] ) ) { |
| 533 | $this->output( 'Inserting initial update keys...' ); |
| 534 | $this->insertInitialUpdateKeys(); |
| 535 | $this->output( "done.\n" ); |
| 536 | } |
| 537 | if ( isset( $what['core'] ) ) { |
| 538 | $this->runUpdates( $this->getCoreUpdateList(), false ); |
| 539 | $this->doCollationUpdate(); |
| 540 | } |
| 541 | if ( isset( $what['extensions'] ) ) { |
| 542 | $this->loadExtensionSchemaUpdates(); |
| 543 | $this->runUpdates( $this->extensionUpdates, true ); |
| 544 | $this->runUpdates( $this->extensionUpdatesWithVirtualDomains, true, true ); |
| 545 | } |
| 546 | |
| 547 | if ( isset( $what['stats'] ) ) { |
| 548 | $this->checkStats(); |
| 549 | } |
| 550 | |
| 551 | if ( $this->fileHandle ) { |
| 552 | $this->skipSchema = false; |
| 553 | $this->writeSchemaUpdateFile(); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * Helper function for doUpdates() |
| 559 | * |
| 560 | * @param array $updates Array of updates to run |
| 561 | * @param bool $passSelf Whether to pass this object when calling external functions |
| 562 | * @param bool $hasVirtualDomain Whether the updates' array include virtual domains |
| 563 | */ |
| 564 | private function runUpdates( array $updates, $passSelf, $hasVirtualDomain = false ) { |
| 565 | $lbFactory = $this->getLBFactory(); |
| 566 | $updatesDone = []; |
| 567 | $updatesSkipped = []; |
| 568 | foreach ( $updates as $params ) { |
| 569 | $origParams = $params; |
| 570 | $oldDb = null; |
| 571 | $this->currentVirtualDomain = null; |
| 572 | if ( $hasVirtualDomain === true ) { |
| 573 | $this->currentVirtualDomain = array_shift( $params ); |
| 574 | $oldDb = $this->db; |
| 575 | $virtualDb = $lbFactory->getPrimaryDatabase( $this->currentVirtualDomain ); |
| 576 | '@phan-var IMaintainableDatabase $virtualDb'; |
| 577 | $this->maintenance->setDB( $virtualDb ); |
| 578 | $this->db = $virtualDb; |
| 579 | } |
| 580 | $func = array_shift( $params ); |
| 581 | if ( !is_array( $func ) && method_exists( $this, $func ) ) { |
| 582 | $func = [ $this, $func ]; |
| 583 | } elseif ( $passSelf ) { |
| 584 | array_unshift( $params, $this ); |
| 585 | } |
| 586 | $ret = $func( ...$params ); |
| 587 | if ( $hasVirtualDomain === true && $oldDb ) { |
| 588 | $this->db = $oldDb; |
| 589 | $this->maintenance->setDB( $oldDb ); |
| 590 | $this->currentVirtualDomain = null; |
| 591 | } |
| 592 | |
| 593 | flush(); |
| 594 | if ( $ret !== false ) { |
| 595 | $updatesDone[] = $origParams; |
| 596 | $lbFactory->waitForReplication( [ 'timeout' => self::REPLICATION_WAIT_TIMEOUT ] ); |
| 597 | } else { |
| 598 | if ( $hasVirtualDomain === true ) { |
| 599 | $params = $origParams; |
| 600 | $func = array_shift( $params ); |
| 601 | } |
| 602 | $updatesSkipped[] = [ $func, $params, $origParams ]; |
| 603 | } |
| 604 | } |
| 605 | $this->updatesSkipped = array_merge( $this->updatesSkipped, $updatesSkipped ); |
| 606 | $this->updates = array_merge( $this->updates, $updatesDone ); |
| 607 | } |
| 608 | |
| 609 | private function getLBFactory(): LBFactory { |
| 610 | return MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * Helper function: check if the given key is present in the updatelog table. |
| 615 | * |
| 616 | * @param string $key Name of the key to check for |
| 617 | * @return bool |
| 618 | */ |
| 619 | public function updateRowExists( $key ) { |
| 620 | // Return false if the updatelog table does not exist. This can occur if performing schema changes for tables |
| 621 | // that are on a virtual database domain. |
| 622 | if ( !$this->db->tableExists( 'updatelog', __METHOD__ ) ) { |
| 623 | return false; |
| 624 | } |
| 625 | |
| 626 | $row = $this->db->newSelectQueryBuilder() |
| 627 | ->select( '1 AS X' ) // T67813 |
| 628 | ->from( 'updatelog' ) |
| 629 | ->where( [ 'ul_key' => $key ] ) |
| 630 | ->caller( __METHOD__ )->fetchRow(); |
| 631 | |
| 632 | return (bool)$row; |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * Helper function: Add a key to the updatelog table |
| 637 | * |
| 638 | * @note Extensions must only use this from within callbacks registered with |
| 639 | * addExtensionUpdate(). In particular, this method must not be called directly |
| 640 | * from a LoadExtensionSchemaUpdates handler. |
| 641 | * |
| 642 | * @param string $key Name of the key to insert |
| 643 | * @param string|null $val [optional] Value to insert along with the key |
| 644 | */ |
| 645 | public function insertUpdateRow( $key, $val = null ) { |
| 646 | // We cannot insert anything to the updatelog table if it does not exist. This can occur for schema changes |
| 647 | // on tables that are on a virtual database domain. |
| 648 | if ( !$this->db->tableExists( 'updatelog', __METHOD__ ) ) { |
| 649 | return; |
| 650 | } |
| 651 | |
| 652 | $this->db->clearFlag( DBO_DDLMODE ); |
| 653 | $values = [ 'ul_key' => $key ]; |
| 654 | if ( $val ) { |
| 655 | $values['ul_value'] = $val; |
| 656 | } |
| 657 | $this->db->newInsertQueryBuilder() |
| 658 | ->insertInto( 'updatelog' ) |
| 659 | ->ignore() |
| 660 | ->row( $values ) |
| 661 | ->caller( __METHOD__ )->execute(); |
| 662 | $this->db->setFlag( DBO_DDLMODE ); |
| 663 | } |
| 664 | |
| 665 | /** |
| 666 | * Add initial keys to the updatelog table. Should be called during installation. |
| 667 | */ |
| 668 | public function insertInitialUpdateKeys() { |
| 669 | $this->db->clearFlag( DBO_DDLMODE ); |
| 670 | $iqb = $this->db->newInsertQueryBuilder() |
| 671 | ->insertInto( 'updatelog' ) |
| 672 | ->ignore() |
| 673 | ->caller( __METHOD__ ); |
| 674 | foreach ( $this->getInitialUpdateKeys() as $key ) { |
| 675 | $iqb->row( [ 'ul_key' => $key ] ); |
| 676 | } |
| 677 | $iqb->execute(); |
| 678 | $this->db->setFlag( DBO_DDLMODE ); |
| 679 | } |
| 680 | |
| 681 | /** |
| 682 | * Returns whether updates should be executed on the database table $name. |
| 683 | * Updates will be prevented if the table is a shared table, and it is not |
| 684 | * specified to run updates on shared tables. |
| 685 | * |
| 686 | * @param string $name Table name |
| 687 | * @return bool |
| 688 | */ |
| 689 | protected function doTable( $name ) { |
| 690 | global $wgSharedDB, $wgSharedTables; |
| 691 | |
| 692 | if ( $this->shared ) { |
| 693 | // Shared updates are enabled |
| 694 | return true; |
| 695 | } |
| 696 | if ( $this->currentVirtualDomain |
| 697 | && $this->getLBFactory()->isSharedVirtualDomain( $this->currentVirtualDomain ) |
| 698 | ) { |
| 699 | $this->output( "...skipping update to table $name in shared virtual domain.\n" ); |
| 700 | return false; |
| 701 | } |
| 702 | if ( $wgSharedDB !== null && in_array( $name, $wgSharedTables ) ) { |
| 703 | $this->output( "...skipping update to shared table $name.\n" ); |
| 704 | return false; |
| 705 | } |
| 706 | |
| 707 | return true; |
| 708 | } |
| 709 | |
| 710 | /** |
| 711 | * Checks whether the DatabaseUpdater is allowed to attempt to apply |
| 712 | * ALTER TABLE commands on the database. If not, then a message |
| 713 | * is printed to say that the schema update is skipped. |
| 714 | * |
| 715 | * @param string $schemaAlterMessage A string describing what the ALTER TABLE command is doing |
| 716 | * @return bool Whether to continue with the ALTER TABLE command |
| 717 | */ |
| 718 | protected function checkSchemaAltersAllowed( string $schemaAlterMessage ): bool { |
| 719 | if ( $this->skipSchemaAlters ) { |
| 720 | $this->output( "...skipping schema change ($schemaAlterMessage).\n" ); |
| 721 | return false; |
| 722 | } |
| 723 | return true; |
| 724 | } |
| 725 | |
| 726 | /** |
| 727 | * Get an array of updates to perform on the database. Should return a |
| 728 | * multidimensional array. The main key is the MediaWiki version (1.12, |
| 729 | * 1.13...) with the values being arrays of updates. |
| 730 | * |
| 731 | * @return array[] |
| 732 | */ |
| 733 | abstract protected function getCoreUpdateList(); |
| 734 | |
| 735 | /** |
| 736 | * Get an array of update keys to insert into the updatelog table after a |
| 737 | * new installation. The named operations will then be skipped by a |
| 738 | * subsequent update. |
| 739 | * |
| 740 | * Add keys here to skip updates that are redundant or harmful on a new |
| 741 | * installation, for example reducing field sizes, adding constraints, etc. |
| 742 | * |
| 743 | * @return string[] |
| 744 | */ |
| 745 | abstract protected function getInitialUpdateKeys(); |
| 746 | |
| 747 | /** |
| 748 | * Append an SQL fragment to the open file handle. |
| 749 | * |
| 750 | * @note protected since 1.35 |
| 751 | * |
| 752 | * @param string $filename File name to open |
| 753 | */ |
| 754 | protected function copyFile( $filename ) { |
| 755 | $this->db->sourceFile( |
| 756 | $filename, |
| 757 | null, |
| 758 | null, |
| 759 | __METHOD__, |
| 760 | $this->appendLine( ... ) |
| 761 | ); |
| 762 | } |
| 763 | |
| 764 | /** |
| 765 | * Append a line to the open file handle. The line is assumed to |
| 766 | * be a complete SQL statement. |
| 767 | * |
| 768 | * This is used as a callback for sourceLine(). |
| 769 | * |
| 770 | * @note protected since 1.35 |
| 771 | * |
| 772 | * @param string $line Text to append to the file |
| 773 | * @return bool False to skip actually executing the file |
| 774 | */ |
| 775 | protected function appendLine( $line ) { |
| 776 | $line = rtrim( $line ) . ";\n"; |
| 777 | if ( fwrite( $this->fileHandle, $line ) === false ) { |
| 778 | throw new RuntimeException( "trouble writing file" ); |
| 779 | } |
| 780 | |
| 781 | return false; |
| 782 | } |
| 783 | |
| 784 | /** |
| 785 | * Applies a SQL patch |
| 786 | * |
| 787 | * @note Do not use this in a LoadExtensionSchemaUpdates handler, |
| 788 | * use addExtensionUpdate instead! |
| 789 | * |
| 790 | * @param string $path Path to the patch file |
| 791 | * @param bool $isFullPath Whether to treat $path as a relative or not |
| 792 | * @param string|null $msg Description of the patch |
| 793 | * @return bool False if the patch was skipped. |
| 794 | */ |
| 795 | protected function applyPatch( $path, $isFullPath = false, $msg = null ) { |
| 796 | $msg ??= "Applying $path patch"; |
| 797 | if ( $this->skipSchema ) { |
| 798 | $this->output( "...skipping schema change ($msg).\n" ); |
| 799 | |
| 800 | return false; |
| 801 | } |
| 802 | |
| 803 | $this->output( "{$msg}..." ); |
| 804 | |
| 805 | if ( !$isFullPath ) { |
| 806 | $path = $this->patchPath( $this->db, $path ); |
| 807 | } |
| 808 | if ( $this->fileHandle !== null ) { |
| 809 | $this->copyFile( $path ); |
| 810 | } else { |
| 811 | $this->db->sourceFile( $path ); |
| 812 | } |
| 813 | $this->output( "done.\n" ); |
| 814 | |
| 815 | return true; |
| 816 | } |
| 817 | |
| 818 | /** |
| 819 | * Get the full path to a patch file. |
| 820 | * |
| 821 | * @param IDatabase $db |
| 822 | * @param string $patch The basename of the patch, like patch-something.sql |
| 823 | * @return string Full path to patch file. It fails back to MySQL |
| 824 | * if no DB-specific patch exists. |
| 825 | */ |
| 826 | public function patchPath( IDatabase $db, $patch ) { |
| 827 | $baseDir = MW_INSTALL_PATH; |
| 828 | |
| 829 | $dbType = $db->getType(); |
| 830 | if ( file_exists( "$baseDir/sql/$dbType/$patch" ) ) { |
| 831 | return "$baseDir/sql/$dbType/$patch"; |
| 832 | } |
| 833 | |
| 834 | // TODO: Is the fallback still needed after the changes from T382030? |
| 835 | return "$baseDir/sql/mysql/$patch"; |
| 836 | } |
| 837 | |
| 838 | /** |
| 839 | * Add a new table to the database |
| 840 | * |
| 841 | * @note Code in a LoadExtensionSchemaUpdates handler should |
| 842 | * use addExtensionTable instead! |
| 843 | * |
| 844 | * @param string $name Name of the new table |
| 845 | * @param string $patch Path to the patch file |
| 846 | * @param bool $fullpath Whether to treat $patch path as a relative or not |
| 847 | * @return bool False if this was skipped because schema changes are skipped |
| 848 | */ |
| 849 | protected function addTable( $name, $patch, $fullpath = false ) { |
| 850 | if ( !$this->doTable( $name ) ) { |
| 851 | return true; |
| 852 | } |
| 853 | |
| 854 | if ( $this->db->tableExists( $name, __METHOD__ ) ) { |
| 855 | $this->outputApplied( "...$name table already exists.\n" ); |
| 856 | return true; |
| 857 | } |
| 858 | |
| 859 | return $this->applyPatch( $patch, $fullpath, "Creating $name table" ); |
| 860 | } |
| 861 | |
| 862 | /** |
| 863 | * Add a new field to an existing table |
| 864 | * |
| 865 | * @note Code in a LoadExtensionSchemaUpdates handler should |
| 866 | * use addExtensionField instead! |
| 867 | * |
| 868 | * @param string $table Name of the table to modify |
| 869 | * @param string $field Name of the new field |
| 870 | * @param string $patch Path to the patch file |
| 871 | * @param bool $fullpath Whether to treat $patch path as a relative or not |
| 872 | * @return bool False if this was skipped because schema changes are skipped |
| 873 | */ |
| 874 | protected function addField( $table, $field, $patch, $fullpath = false ) { |
| 875 | if ( !$this->doTable( $table ) ) { |
| 876 | return true; |
| 877 | } |
| 878 | |
| 879 | $updateMsg = "Adding $field field to table $table"; |
| 880 | if ( !$this->checkSchemaAltersAllowed( $updateMsg ) ) { |
| 881 | return false; |
| 882 | } |
| 883 | |
| 884 | if ( !$this->db->tableExists( $table, __METHOD__ ) ) { |
| 885 | $this->outputApplied( "...$table table does not exist, skipping new field patch.\n" ); |
| 886 | } elseif ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) { |
| 887 | $this->outputApplied( "...have $field field in $table table.\n" ); |
| 888 | } else { |
| 889 | return $this->applyPatch( $patch, $fullpath, $updateMsg ); |
| 890 | } |
| 891 | |
| 892 | return true; |
| 893 | } |
| 894 | |
| 895 | /** |
| 896 | * Add a new index to an existing table |
| 897 | * |
| 898 | * @note Code in a LoadExtensionSchemaUpdates handler should |
| 899 | * use addExtensionIndex instead! |
| 900 | * |
| 901 | * @param string $table Name of the table to modify |
| 902 | * @param string $index Name of the new index |
| 903 | * @param string $patch Path to the patch file |
| 904 | * @param bool $fullpath Whether to treat $patch path as a relative or not |
| 905 | * @return bool False if this was skipped because schema changes are skipped |
| 906 | */ |
| 907 | protected function addIndex( $table, $index, $patch, $fullpath = false ) { |
| 908 | if ( !$this->doTable( $table ) ) { |
| 909 | return true; |
| 910 | } |
| 911 | |
| 912 | $updateMsg = "Adding index $index to table $table"; |
| 913 | if ( !$this->checkSchemaAltersAllowed( $updateMsg ) ) { |
| 914 | return false; |
| 915 | } |
| 916 | |
| 917 | if ( !$this->db->tableExists( $table, __METHOD__ ) ) { |
| 918 | $this->output( "...skipping: '$table' table doesn't exist yet.\n" ); |
| 919 | } elseif ( $this->db->indexExists( $table, $index, __METHOD__ ) ) { |
| 920 | $this->outputApplied( "...index $index already set on $table table.\n" ); |
| 921 | } else { |
| 922 | return $this->applyPatch( $patch, $fullpath, $updateMsg ); |
| 923 | } |
| 924 | |
| 925 | return true; |
| 926 | } |
| 927 | |
| 928 | /** |
| 929 | * Drop a field from an existing table |
| 930 | * |
| 931 | * @note Code in a LoadExtensionSchemaUpdates handler should |
| 932 | * use dropExtensionField instead! |
| 933 | * |
| 934 | * @param string $table Name of the table to modify |
| 935 | * @param string $field Name of the old field |
| 936 | * @param string $patch Path to the patch file |
| 937 | * @param bool $fullpath Whether to treat $patch path as a relative or not |
| 938 | * @return bool False if this was skipped because schema changes are skipped |
| 939 | */ |
| 940 | protected function dropField( $table, $field, $patch, $fullpath = false ) { |
| 941 | if ( !$this->doTable( $table ) ) { |
| 942 | return true; |
| 943 | } |
| 944 | |
| 945 | if ( !$this->checkSchemaAltersAllowed( "Dropping $field from table $table" ) ) { |
| 946 | return false; |
| 947 | } |
| 948 | |
| 949 | if ( !$this->db->tableExists( $table, __METHOD__ ) ) { |
| 950 | $this->outputApplied( "...skipping: '$table' table doesn't exist yet.\n" ); |
| 951 | |
| 952 | return true; |
| 953 | } |
| 954 | |
| 955 | if ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) { |
| 956 | return $this->applyPatch( $patch, $fullpath, "Table $table contains $field field. Dropping" ); |
| 957 | } |
| 958 | |
| 959 | $this->outputApplied( "...$table table does not contain $field field.\n" ); |
| 960 | return true; |
| 961 | } |
| 962 | |
| 963 | /** |
| 964 | * Drop an index from an existing table |
| 965 | * |
| 966 | * @note Code in a LoadExtensionSchemaUpdates handler should |
| 967 | * use dropExtensionIndex instead! |
| 968 | * |
| 969 | * @param string $table Name of the table to modify |
| 970 | * @param string $index Name of the index |
| 971 | * @param string $patch Path to the patch file |
| 972 | * @param bool $fullpath Whether to treat $patch path as a relative or not |
| 973 | * @return bool False if this was skipped because schema changes are skipped |
| 974 | */ |
| 975 | protected function dropIndex( $table, $index, $patch, $fullpath = false ) { |
| 976 | if ( !$this->doTable( $table ) ) { |
| 977 | return true; |
| 978 | } |
| 979 | |
| 980 | $updateMsg = "Dropping $index index from table $table"; |
| 981 | if ( !$this->checkSchemaAltersAllowed( $updateMsg ) ) { |
| 982 | return false; |
| 983 | } |
| 984 | |
| 985 | if ( !$this->db->tableExists( $table, __METHOD__ ) ) { |
| 986 | $this->outputApplied( "...skipping: '$table' table doesn't exist yet.\n" ); |
| 987 | |
| 988 | return true; |
| 989 | } |
| 990 | |
| 991 | if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) { |
| 992 | return $this->applyPatch( $patch, $fullpath, $updateMsg ); |
| 993 | } |
| 994 | |
| 995 | $this->outputApplied( "...$index key doesn't exist.\n" ); |
| 996 | return true; |
| 997 | } |
| 998 | |
| 999 | /** |
| 1000 | * Rename an index from an existing table |
| 1001 | * |
| 1002 | * @note Code in a LoadExtensionSchemaUpdates handler should |
| 1003 | * use renameExtensionIndex instead! |
| 1004 | * |
| 1005 | * @param string $table Name of the table to modify |
| 1006 | * @param string $oldIndex Old name of the index |
| 1007 | * @param string $newIndex New name of the index |
| 1008 | * @param bool $skipBothIndexExistWarning Whether to warn if both the old and new indexes exist. |
| 1009 | * @param string $patch Path to the patch file |
| 1010 | * @param bool $fullpath Whether to treat $patch path as a relative or not |
| 1011 | * @return bool False if this was skipped because schema changes are skipped |
| 1012 | */ |
| 1013 | protected function renameIndex( $table, $oldIndex, $newIndex, |
| 1014 | $skipBothIndexExistWarning, $patch, $fullpath = false |
| 1015 | ) { |
| 1016 | if ( !$this->doTable( $table ) ) { |
| 1017 | return true; |
| 1018 | } |
| 1019 | |
| 1020 | $updateMsg = "Renaming index $oldIndex to $newIndex in table $table"; |
| 1021 | if ( !$this->checkSchemaAltersAllowed( $updateMsg ) ) { |
| 1022 | return false; |
| 1023 | } |
| 1024 | |
| 1025 | // First requirement: the table must exist |
| 1026 | if ( !$this->db->tableExists( $table, __METHOD__ ) ) { |
| 1027 | $this->outputApplied( "...skipping: '$table' table doesn't exist yet.\n" ); |
| 1028 | |
| 1029 | return true; |
| 1030 | } |
| 1031 | |
| 1032 | // Second requirement: the new index must be missing |
| 1033 | if ( $this->db->indexExists( $table, $newIndex, __METHOD__ ) ) { |
| 1034 | $this->outputApplied( "...index $newIndex already set on $table table.\n" ); |
| 1035 | if ( !$skipBothIndexExistWarning && |
| 1036 | $this->db->indexExists( $table, $oldIndex, __METHOD__ ) |
| 1037 | ) { |
| 1038 | $this->output( "...WARNING: $oldIndex still exists, despite it has " . |
| 1039 | "been renamed into $newIndex (which also exists).\n" . |
| 1040 | " $oldIndex should be manually removed if not needed anymore.\n" ); |
| 1041 | } |
| 1042 | |
| 1043 | return true; |
| 1044 | } |
| 1045 | |
| 1046 | // Third requirement: the old index must exist |
| 1047 | if ( !$this->db->indexExists( $table, $oldIndex, __METHOD__ ) ) { |
| 1048 | $this->outputApplied( "...skipping: index $oldIndex doesn't exist.\n" ); |
| 1049 | |
| 1050 | return true; |
| 1051 | } |
| 1052 | |
| 1053 | // Requirements have been satisfied, the patch can be applied |
| 1054 | return $this->applyPatch( |
| 1055 | $patch, |
| 1056 | $fullpath, |
| 1057 | $updateMsg |
| 1058 | ); |
| 1059 | } |
| 1060 | |
| 1061 | /** |
| 1062 | * If the specified table exists, drop it, or execute the |
| 1063 | * patch if one is provided. |
| 1064 | * |
| 1065 | * @note Code in a LoadExtensionSchemaUpdates handler should |
| 1066 | * use dropExtensionTable instead! |
| 1067 | * |
| 1068 | * @note protected since 1.35 |
| 1069 | * |
| 1070 | * @param string $table Table to drop. |
| 1071 | * @param string|false $patch String of patch file that will drop the table. Default: false. |
| 1072 | * @param bool $fullpath Whether $patch is a full path. Default: false. |
| 1073 | * @return bool False if this was skipped because schema changes are skipped |
| 1074 | */ |
| 1075 | protected function dropTable( $table, $patch = false, $fullpath = false ) { |
| 1076 | if ( !$this->doTable( $table ) ) { |
| 1077 | return true; |
| 1078 | } |
| 1079 | |
| 1080 | if ( $this->db->tableExists( $table, __METHOD__ ) ) { |
| 1081 | $msg = "Dropping table $table"; |
| 1082 | |
| 1083 | if ( $patch === false ) { |
| 1084 | $this->output( "$msg ..." ); |
| 1085 | $this->db->dropTable( $table, __METHOD__ ); |
| 1086 | $this->output( "done.\n" ); |
| 1087 | } else { |
| 1088 | return $this->applyPatch( $patch, $fullpath, $msg ); |
| 1089 | } |
| 1090 | } else { |
| 1091 | $this->outputApplied( "...$table doesn't exist.\n" ); |
| 1092 | } |
| 1093 | |
| 1094 | return true; |
| 1095 | } |
| 1096 | |
| 1097 | /** |
| 1098 | * Modify an existing field |
| 1099 | * |
| 1100 | * @note Code in a LoadExtensionSchemaUpdates handler should |
| 1101 | * use modifyExtensionField instead! |
| 1102 | * |
| 1103 | * @note protected since 1.35 |
| 1104 | * |
| 1105 | * @param string $table Name of the table to which the field belongs |
| 1106 | * @param string $field Name of the field to modify |
| 1107 | * @param string $patch Path to the patch file |
| 1108 | * @param bool $fullpath Whether to treat $patch path as a relative or not |
| 1109 | * @return bool False if this was skipped because schema changes are skipped |
| 1110 | */ |
| 1111 | protected function modifyField( $table, $field, $patch, $fullpath = false ) { |
| 1112 | return $this->modifyFieldWithCondition( |
| 1113 | $table, $field, |
| 1114 | static fn () => true, |
| 1115 | $patch, $fullpath |
| 1116 | ); |
| 1117 | } |
| 1118 | |
| 1119 | /** |
| 1120 | * Modify or set a PRIMARY KEY on a table. |
| 1121 | * |
| 1122 | * This checks the current table schema via the database layer to determine the existing |
| 1123 | * PRIMARY KEY columns. If they already match the requested set, the patch is skipped; |
| 1124 | * otherwise the supplied patch is applied. |
| 1125 | * |
| 1126 | * @param string $table Table name |
| 1127 | * @param string[] $columns Desired PRIMARY KEY columns in order |
| 1128 | * @param string $patch SQL patch path |
| 1129 | * @param bool $fullpath Whether $patch is a full path |
| 1130 | * @return bool False if the patch was skipped because schema changes are skipped |
| 1131 | */ |
| 1132 | protected function modifyPrimaryKey( $table, array $columns, $patch, $fullpath = false ) { |
| 1133 | if ( !$this->doTable( $table ) ) { |
| 1134 | return true; |
| 1135 | } |
| 1136 | |
| 1137 | $updateMsg = "Modifying primary key on table $table"; |
| 1138 | if ( !$this->checkSchemaAltersAllowed( $updateMsg ) ) { |
| 1139 | return false; |
| 1140 | } |
| 1141 | |
| 1142 | if ( !$this->db->tableExists( $table, __METHOD__ ) ) { |
| 1143 | $this->outputApplied( "...skipping: '$table' table doesn't exist yet.\n" ); |
| 1144 | return true; |
| 1145 | } |
| 1146 | |
| 1147 | // Compare desired PK to current PK columns from the DB layer |
| 1148 | $current = $this->db->getPrimaryKeyColumns( $table, __METHOD__ ); |
| 1149 | if ( $current === array_values( $columns ) ) { |
| 1150 | $this->outputApplied( "...primary key already set on $table table.\n" ); |
| 1151 | return true; |
| 1152 | } |
| 1153 | |
| 1154 | return $this->applyPatch( $patch, $fullpath, $updateMsg ); |
| 1155 | } |
| 1156 | |
| 1157 | /** |
| 1158 | * Modify an existing table, similar to modifyField. Intended for changes that |
| 1159 | * touch more than one column on a table. |
| 1160 | * |
| 1161 | * @note Code in a LoadExtensionSchemaUpdates handler should |
| 1162 | * use modifyExtensionTable instead! |
| 1163 | * |
| 1164 | * @note protected since 1.35 |
| 1165 | * |
| 1166 | * @param string $table Name of the table to modify |
| 1167 | * @param string $patch Name of the patch file to apply |
| 1168 | * @param string|bool $fullpath Whether to treat $patch path as relative or not, defaults to false |
| 1169 | * @return bool False if this was skipped because of schema changes being skipped |
| 1170 | */ |
| 1171 | protected function modifyTable( $table, $patch, $fullpath = false ) { |
| 1172 | if ( !$this->doTable( $table ) ) { |
| 1173 | return true; |
| 1174 | } |
| 1175 | |
| 1176 | $updateMsg = "Modifying table $table with patch $patch"; |
| 1177 | if ( !$this->checkSchemaAltersAllowed( $updateMsg ) ) { |
| 1178 | return false; |
| 1179 | } |
| 1180 | |
| 1181 | $updateKey = "$table-$patch"; |
| 1182 | if ( !$this->db->tableExists( $table, __METHOD__ ) ) { |
| 1183 | $this->outputApplied( "...$table table does not exist, skipping modify table patch.\n" ); |
| 1184 | } elseif ( $this->updateRowExists( $updateKey ) ) { |
| 1185 | $this->outputApplied( "...table $table already modified by patch $patch.\n" ); |
| 1186 | } else { |
| 1187 | $apply = $this->applyPatch( $patch, $fullpath, $updateMsg ); |
| 1188 | if ( $apply ) { |
| 1189 | $this->insertUpdateRow( $updateKey ); |
| 1190 | } |
| 1191 | return $apply; |
| 1192 | } |
| 1193 | return true; |
| 1194 | } |
| 1195 | |
| 1196 | /** |
| 1197 | * Modify a table if a field doesn't exist. This helps extensions to avoid |
| 1198 | * running updates on SQLite that are destructive because they don't copy |
| 1199 | * new fields. |
| 1200 | * |
| 1201 | * @since 1.44 |
| 1202 | * @param string $table Name of the table to which the field belongs |
| 1203 | * @param string $field Name of the field to check |
| 1204 | * @param string $patch Path to the patch file |
| 1205 | * @param bool $fullpath Whether to treat $patch path as a relative or not |
| 1206 | * @param string|null $fieldBeingModified The field being modified. If this |
| 1207 | * is specified, the updatelog key will match that used by modifyField(), |
| 1208 | * so if the patch was previously applied via modifyField(), it won't be |
| 1209 | * applied again. Also, if the field doesn't exist, the patch will not be |
| 1210 | * applied. If this is null, the updatelog key will match that used by |
| 1211 | * modifyTable(). |
| 1212 | * @return bool False if this was skipped because schema changes are skipped |
| 1213 | */ |
| 1214 | protected function modifyTableIfFieldNotExists( $table, $field, $patch, $fullpath = false, |
| 1215 | $fieldBeingModified = null |
| 1216 | ) { |
| 1217 | if ( !$this->doTable( $table ) ) { |
| 1218 | return true; |
| 1219 | } |
| 1220 | |
| 1221 | if ( $fieldBeingModified === null ) { |
| 1222 | $updateKey = "$table-$patch"; |
| 1223 | } else { |
| 1224 | $updateKey = "$table-$fieldBeingModified-$patch"; |
| 1225 | } |
| 1226 | |
| 1227 | if ( !$this->checkSchemaAltersAllowed( "Modifying table $table with patch $patch" ) ) { |
| 1228 | return false; |
| 1229 | } |
| 1230 | |
| 1231 | if ( !$this->db->tableExists( $table, __METHOD__ ) ) { |
| 1232 | $this->outputApplied( "...$table table does not exist, skipping patch $patch.\n" ); |
| 1233 | } elseif ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) { |
| 1234 | $this->outputApplied( "...$field field exists in $table table, skipping obsolete patch $patch.\n" ); |
| 1235 | } elseif ( $fieldBeingModified !== null |
| 1236 | && !$this->db->fieldExists( $table, $fieldBeingModified, __METHOD__ ) |
| 1237 | ) { |
| 1238 | $this->outputApplied( "...$fieldBeingModified field does not exist in $table table, " . |
| 1239 | "skipping patch $patch.\n" ); |
| 1240 | } elseif ( $this->updateRowExists( $updateKey ) ) { |
| 1241 | $this->outputApplied( "...table $table already modified by patch $patch.\n" ); |
| 1242 | } else { |
| 1243 | $apply = $this->applyPatch( $patch, $fullpath, "Modifying table $table with patch $patch" ); |
| 1244 | if ( $apply ) { |
| 1245 | $this->insertUpdateRow( $updateKey ); |
| 1246 | } |
| 1247 | return $apply; |
| 1248 | } |
| 1249 | return true; |
| 1250 | } |
| 1251 | |
| 1252 | /** |
| 1253 | * Modify a field if the field exists and is nullable |
| 1254 | * |
| 1255 | * @since 1.44 |
| 1256 | * @param string $table Name of the table to which the field belongs |
| 1257 | * @param string $field Name of the field to modify |
| 1258 | * @param string $patch Path to the patch file |
| 1259 | * @param bool $fullpath Whether to treat $patch path as a relative or not |
| 1260 | * @return bool False if this was skipped because schema changes are skipped |
| 1261 | */ |
| 1262 | protected function modifyFieldIfNullable( $table, $field, $patch, $fullpath = false ) { |
| 1263 | return $this->modifyFieldWithCondition( |
| 1264 | $table, $field, |
| 1265 | static function ( $fieldInfo ) { |
| 1266 | return $fieldInfo->isNullable(); |
| 1267 | }, |
| 1268 | $patch, |
| 1269 | $fullpath |
| 1270 | ); |
| 1271 | } |
| 1272 | |
| 1273 | /** |
| 1274 | * Modify a field if a field exists and a callback returns true. The callback |
| 1275 | * is called with the FieldInfo of the field in question. |
| 1276 | * |
| 1277 | * @internal |
| 1278 | * @param string $table Name of the table to modify |
| 1279 | * @param string $field Name of the field to modify |
| 1280 | * @param callable $condCallback A callback which will be called with the |
| 1281 | * \Wikimedia\Rdbms\Field object for the specified field. If the callback returns |
| 1282 | * true, the update will proceed. |
| 1283 | * @param string $patch Name of the patch file to apply |
| 1284 | * @param string|bool $fullpath Whether to treat $patch path as relative or not, defaults to false |
| 1285 | * @return bool False if this was skipped because of schema changes being skipped |
| 1286 | */ |
| 1287 | private function modifyFieldWithCondition( |
| 1288 | $table, $field, $condCallback, $patch, $fullpath = false |
| 1289 | ) { |
| 1290 | if ( !$this->doTable( $table ) ) { |
| 1291 | return true; |
| 1292 | } |
| 1293 | |
| 1294 | $updateMsg = "Modifying $field field of table $table"; |
| 1295 | if ( !$this->checkSchemaAltersAllowed( $updateMsg ) ) { |
| 1296 | return false; |
| 1297 | } |
| 1298 | |
| 1299 | $updateKey = "$table-$field-$patch"; |
| 1300 | if ( !$this->db->tableExists( $table, __METHOD__ ) ) { |
| 1301 | $this->outputApplied( "...$table table does not exist, skipping modify field patch.\n" ); |
| 1302 | return true; |
| 1303 | } |
| 1304 | $fieldInfo = $this->db->fieldInfo( $table, $field ); |
| 1305 | if ( !$fieldInfo ) { |
| 1306 | $this->outputApplied( "...$field field does not exist in $table table, " . |
| 1307 | "skipping modify field patch.\n" ); |
| 1308 | return true; |
| 1309 | } |
| 1310 | if ( $this->updateRowExists( $updateKey ) ) { |
| 1311 | $this->outputApplied( "...$field in table $table already modified by patch $patch.\n" ); |
| 1312 | return true; |
| 1313 | } |
| 1314 | if ( !$condCallback( $fieldInfo ) ) { |
| 1315 | $this->outputApplied( "...$field in table $table already has the required properties.\n" ); |
| 1316 | return true; |
| 1317 | } |
| 1318 | |
| 1319 | $apply = $this->applyPatch( $patch, $fullpath, $updateMsg ); |
| 1320 | if ( $apply ) { |
| 1321 | $this->insertUpdateRow( $updateKey ); |
| 1322 | } |
| 1323 | return $apply; |
| 1324 | } |
| 1325 | |
| 1326 | /** |
| 1327 | * Run a maintenance script |
| 1328 | * |
| 1329 | * This should only be used when the maintenance script must run before |
| 1330 | * later updates. If later updates don't depend on the script, add it to |
| 1331 | * DatabaseUpdater::$postDatabaseUpdateMaintenance instead. |
| 1332 | * |
| 1333 | * The script's execute() method must return true to indicate successful |
| 1334 | * completion, and must return false (or throw an exception) to indicate |
| 1335 | * unsuccessful completion. |
| 1336 | * |
| 1337 | * @note Code in a LoadExtensionSchemaUpdates handler should |
| 1338 | * use addExtensionUpdate instead! |
| 1339 | * |
| 1340 | * @note protected since 1.35 |
| 1341 | * |
| 1342 | * @since 1.32 |
| 1343 | * @param class-string<Maintenance> $class Maintenance subclass |
| 1344 | * @param string $unused Unused, kept for compatibility |
| 1345 | */ |
| 1346 | protected function runMaintenance( $class, $unused = '' ) { |
| 1347 | $task = $this->maintenance->createChild( $class ); |
| 1348 | if ( $task instanceof LoggedUpdateMaintenance && $task->isAlreadyCompleted() ) { |
| 1349 | $this->outputApplied( "..." . $task->updateSkippedMessage() . "\n" ); |
| 1350 | return; |
| 1351 | } |
| 1352 | $this->output( "Running $class...\n" ); |
| 1353 | $ok = $task->execute(); |
| 1354 | if ( !$ok ) { |
| 1355 | throw new RuntimeException( "Execution of $class did not complete successfully." ); |
| 1356 | } |
| 1357 | $this->output( "done.\n" ); |
| 1358 | } |
| 1359 | |
| 1360 | /** |
| 1361 | * Set any .htaccess files or equivalent for storage repos |
| 1362 | * |
| 1363 | * Some zones (e.g. "temp") used to be public and may have been initialized as such |
| 1364 | */ |
| 1365 | public function setFileAccess() { |
| 1366 | $repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo(); |
| 1367 | $zonePath = $repo->getZonePath( 'temp' ); |
| 1368 | if ( $repo->getBackend()->directoryExists( [ 'dir' => $zonePath ] ) ) { |
| 1369 | // If the directory was never made, then it will have the right ACLs when it is made |
| 1370 | $status = $repo->getBackend()->secure( [ |
| 1371 | 'dir' => $zonePath, |
| 1372 | 'noAccess' => true, |
| 1373 | 'noListing' => true |
| 1374 | ] ); |
| 1375 | if ( $status->isOK() ) { |
| 1376 | $this->output( "Set the local repo temp zone container to be private.\n" ); |
| 1377 | } else { |
| 1378 | $this->output( "Failed to set the local repo temp zone container to be private.\n" ); |
| 1379 | } |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | /** |
| 1384 | * Purge various database caches |
| 1385 | */ |
| 1386 | public function purgeCache() { |
| 1387 | global $wgLocalisationCacheConf; |
| 1388 | // We can't guarantee that the user will be able to use TRUNCATE, |
| 1389 | // but we know that DELETE is available to us |
| 1390 | $this->output( "Purging caches..." ); |
| 1391 | |
| 1392 | // ObjectCache |
| 1393 | $this->db->newDeleteQueryBuilder() |
| 1394 | ->deleteFrom( 'objectcache' ) |
| 1395 | ->where( ISQLPlatform::ALL_ROWS ) |
| 1396 | ->caller( __METHOD__ ) |
| 1397 | ->execute(); |
| 1398 | |
| 1399 | // LocalisationCache |
| 1400 | if ( $wgLocalisationCacheConf['manualRecache'] ) { |
| 1401 | $this->rebuildLocalisationCache(); |
| 1402 | } |
| 1403 | |
| 1404 | // ResourceLoader: Message cache |
| 1405 | $services = MediaWikiServices::getInstance(); |
| 1406 | MessageBlobStore::clearGlobalCacheEntry( |
| 1407 | $services->getMainWANObjectCache() |
| 1408 | ); |
| 1409 | } |
| 1410 | |
| 1411 | /** |
| 1412 | * Check the site_stats table is not properly populated. |
| 1413 | */ |
| 1414 | protected function checkStats() { |
| 1415 | $row = $this->db->newSelectQueryBuilder() |
| 1416 | ->select( '*' ) |
| 1417 | ->from( 'site_stats' ) |
| 1418 | ->where( [ 'ss_row_id' => 1 ] ) |
| 1419 | ->caller( __METHOD__ )->fetchRow(); |
| 1420 | if ( $row === false ) { |
| 1421 | $this->output( "...site_stats is populated...data is missing! rebuilding..." ); |
| 1422 | } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) { |
| 1423 | $this->output( "...site_stats is populated...missing ss_total_pages, rebuilding..." ); |
| 1424 | } else { |
| 1425 | $this->outputApplied( "...site_stats is populated.\n" ); |
| 1426 | return; |
| 1427 | } |
| 1428 | SiteStatsInit::doAllAndCommit( $this->db ); |
| 1429 | $this->output( "done.\n" ); |
| 1430 | } |
| 1431 | |
| 1432 | # Common updater functions |
| 1433 | |
| 1434 | /** |
| 1435 | * Update CategoryLinks collation |
| 1436 | */ |
| 1437 | protected function doCollationUpdate() { |
| 1438 | global $wgCategoryCollation; |
| 1439 | if ( $this->updateRowExists( 'UpdateCollation::' . $wgCategoryCollation ) ) { |
| 1440 | $this->outputApplied( "...collations up-to-date.\n" ); |
| 1441 | return; |
| 1442 | } |
| 1443 | $this->output( "Updating category collations...\n" ); |
| 1444 | $task = $this->maintenance->createChild( UpdateCollation::class ); |
| 1445 | $ok = $task->execute(); |
| 1446 | if ( $ok !== false ) { |
| 1447 | $this->output( "...done.\n" ); |
| 1448 | $this->insertUpdateRow( 'UpdateCollation::' . $wgCategoryCollation ); |
| 1449 | } |
| 1450 | } |
| 1451 | |
| 1452 | protected function doConvertDjvuMetadata() { |
| 1453 | if ( $this->updateRowExists( 'ConvertDjvuMetadata' ) ) { |
| 1454 | return; |
| 1455 | } |
| 1456 | $this->output( "Converting djvu metadata..." ); |
| 1457 | $task = $this->maintenance->createChild( RefreshImageMetadata::class ); |
| 1458 | '@phan-var RefreshImageMetadata $task'; |
| 1459 | $task->loadParamsAndArgs( RefreshImageMetadata::class, [ |
| 1460 | 'force' => true, |
| 1461 | 'mediatype' => 'OFFICE', |
| 1462 | 'mime' => 'image/*', |
| 1463 | 'batch-size' => 1, |
| 1464 | 'sleep' => 1 |
| 1465 | ] ); |
| 1466 | $ok = $task->execute(); |
| 1467 | if ( $ok !== false ) { |
| 1468 | $this->output( "...done.\n" ); |
| 1469 | $this->insertUpdateRow( 'ConvertDjvuMetadata' ); |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | /** |
| 1474 | * Rebuilds the localisation cache |
| 1475 | */ |
| 1476 | protected function rebuildLocalisationCache() { |
| 1477 | /** |
| 1478 | * @var RebuildLocalisationCache $cl |
| 1479 | */ |
| 1480 | $cl = $this->maintenance->createChild( |
| 1481 | RebuildLocalisationCache::class, 'rebuildLocalisationCache.php' |
| 1482 | ); |
| 1483 | '@phan-var RebuildLocalisationCache $cl'; |
| 1484 | $this->output( "Rebuilding localisation cache...\n" ); |
| 1485 | $cl->setForce(); |
| 1486 | $cl->execute(); |
| 1487 | $this->output( "done.\n" ); |
| 1488 | } |
| 1489 | |
| 1490 | protected function migratePagelinks() { |
| 1491 | if ( $this->updateRowExists( MigrateLinksTable::class . 'pagelinks' ) ) { |
| 1492 | $this->outputApplied( "...pagelinks table has already been migrated.\n" ); |
| 1493 | return; |
| 1494 | } |
| 1495 | /** |
| 1496 | * @var MigrateLinksTable $task |
| 1497 | */ |
| 1498 | $task = $this->maintenance->createChild( |
| 1499 | MigrateLinksTable::class, 'migrateLinksTable.php' |
| 1500 | ); |
| 1501 | '@phan-var MigrateLinksTable $task'; |
| 1502 | $task->loadParamsAndArgs( MigrateLinksTable::class, [ |
| 1503 | 'force' => true, |
| 1504 | 'table' => 'pagelinks' |
| 1505 | ] ); |
| 1506 | $this->output( "Running migrateLinksTable.php on pagelinks...\n" ); |
| 1507 | $task->execute(); |
| 1508 | $this->output( "done.\n" ); |
| 1509 | } |
| 1510 | |
| 1511 | protected function migrateCategorylinks() { |
| 1512 | if ( $this->updateRowExists( MigrateLinksTable::class . 'categorylinks' ) ) { |
| 1513 | $this->outputApplied( "...categorylinks table has already been migrated.\n" ); |
| 1514 | return; |
| 1515 | } |
| 1516 | /** |
| 1517 | * @var MigrateLinksTable $task |
| 1518 | */ |
| 1519 | $task = $this->maintenance->createChild( |
| 1520 | MigrateLinksTable::class, 'migrateLinksTable.php' |
| 1521 | ); |
| 1522 | '@phan-var MigrateLinksTable $task'; |
| 1523 | $task->loadParamsAndArgs( MigrateLinksTable::class, [ |
| 1524 | 'force' => true, |
| 1525 | 'table' => 'categorylinks' |
| 1526 | ] ); |
| 1527 | $this->output( "Running migrateLinksTable.php on categorylinks...\n" ); |
| 1528 | $task->execute(); |
| 1529 | $this->output( "done.\n" ); |
| 1530 | } |
| 1531 | |
| 1532 | protected function normalizeCollation() { |
| 1533 | if ( $this->updateRowExists( 'normalizeCollation' ) ) { |
| 1534 | $this->outputApplied( "...collation table has already been normalized.\n" ); |
| 1535 | return; |
| 1536 | } |
| 1537 | /** |
| 1538 | * @var UpdateCollation $task |
| 1539 | */ |
| 1540 | $task = $this->maintenance->createChild( |
| 1541 | UpdateCollation::class, 'updateCollation.php' |
| 1542 | ); |
| 1543 | '@phan-var UpdateCollation $task'; |
| 1544 | $task->loadParamsAndArgs( UpdateCollation::class, [ |
| 1545 | 'only-migrate-normalization' => true, |
| 1546 | ] ); |
| 1547 | $this->output( "Running updateCollation.php --only-migrate-normalization...\n" ); |
| 1548 | $task->execute(); |
| 1549 | $this->insertUpdateRow( 'normalizeCollation' ); |
| 1550 | $this->output( "done.\n" ); |
| 1551 | } |
| 1552 | |
| 1553 | protected function migrateImagelinks() { |
| 1554 | if ( $this->updateRowExists( MigrateLinksTable::class . 'imagelinks' ) ) { |
| 1555 | $this->outputApplied( "...imagelinks table has already been migrated.\n" ); |
| 1556 | return; |
| 1557 | } |
| 1558 | /** |
| 1559 | * @var MigrateLinksTable $task |
| 1560 | */ |
| 1561 | $task = $this->maintenance->createChild( |
| 1562 | MigrateLinksTable::class, 'migrateLinksTable.php' |
| 1563 | ); |
| 1564 | '@phan-var MigrateLinksTable $task'; |
| 1565 | $task->loadParamsAndArgs( MigrateLinksTable::class, [ |
| 1566 | 'force' => true, |
| 1567 | 'table' => 'imagelinks' |
| 1568 | ] ); |
| 1569 | $this->output( "Running migrateLinksTable.php on imagelinks...\n" ); |
| 1570 | $task->execute(); |
| 1571 | $this->output( "done.\n" ); |
| 1572 | } |
| 1573 | |
| 1574 | protected function addMissingTalkPageWatchlistLabels(): void { |
| 1575 | if ( $this->updateRowExists( CleanupWatchlistLabelMember::class ) ) { |
| 1576 | $this->outputApplied( "...watchlist_label_member table has already been fixed.\n" ); |
| 1577 | return; |
| 1578 | } |
| 1579 | $task = $this->maintenance->createChild( CleanupWatchlistLabelMember::class ); |
| 1580 | $task->loadParamsAndArgs( CleanupWatchlistLabelMember::class, [ 'force' => true ] ); |
| 1581 | $this->output( "Running cleanupWatchlistLabelMember.php on watchlist_label_member...\n" ); |
| 1582 | $task->execute(); |
| 1583 | $this->output( "done.\n" ); |
| 1584 | } |
| 1585 | |
| 1586 | /** |
| 1587 | * Only run a function if a table does not exist |
| 1588 | * |
| 1589 | * @since 1.35 |
| 1590 | * @param string $table Table to check. |
| 1591 | * If passed $this, it's assumed to be a call from runUpdates() with |
| 1592 | * $passSelf = true: all other parameters are shifted and $this is |
| 1593 | * prepended to the rest of $params. |
| 1594 | * @param string|array|static $func Normally this is the string naming the method on $this to |
| 1595 | * call. It may also be an array style callable. |
| 1596 | * @param mixed ...$params Parameters for `$func` |
| 1597 | * @return mixed Whatever $func returns, or null when skipped. |
| 1598 | */ |
| 1599 | protected function ifTableNotExists( $table, $func, ...$params ) { |
| 1600 | // Handle $passSelf from runUpdates(). |
| 1601 | $passSelf = false; |
| 1602 | if ( $table === $this ) { |
| 1603 | $passSelf = true; |
| 1604 | $table = $func; |
| 1605 | $func = array_shift( $params ); |
| 1606 | } |
| 1607 | |
| 1608 | if ( $this->db->tableExists( $table, __METHOD__ ) ) { |
| 1609 | return null; |
| 1610 | } |
| 1611 | |
| 1612 | if ( !is_array( $func ) && method_exists( $this, $func ) ) { |
| 1613 | $func = [ $this, $func ]; |
| 1614 | } elseif ( $passSelf ) { |
| 1615 | array_unshift( $params, $this ); |
| 1616 | } |
| 1617 | |
| 1618 | return $func( ...$params ); |
| 1619 | } |
| 1620 | |
| 1621 | /** |
| 1622 | * Only run a function if the named field exists |
| 1623 | * |
| 1624 | * @since 1.35 |
| 1625 | * @param string $table Table to check. |
| 1626 | * If passed $this, it's assumed to be a call from runUpdates() with |
| 1627 | * $passSelf = true: all other parameters are shifted and $this is |
| 1628 | * prepended to the rest of $params. |
| 1629 | * @param string $field Field to check |
| 1630 | * @param string|array|static $func Normally this is the string naming the method on $this to |
| 1631 | * call. It may also be an array style callable. |
| 1632 | * @param mixed ...$params Parameters for `$func` |
| 1633 | * @return mixed Whatever $func returns, or null when skipped. |
| 1634 | */ |
| 1635 | protected function ifFieldExists( $table, $field, $func, ...$params ) { |
| 1636 | // Handle $passSelf from runUpdates(). |
| 1637 | $passSelf = false; |
| 1638 | if ( $table === $this ) { |
| 1639 | $passSelf = true; |
| 1640 | $table = $field; |
| 1641 | $field = $func; |
| 1642 | $func = array_shift( $params ); |
| 1643 | } |
| 1644 | |
| 1645 | if ( !$this->db->tableExists( $table, __METHOD__ ) || |
| 1646 | !$this->db->fieldExists( $table, $field, __METHOD__ ) |
| 1647 | ) { |
| 1648 | return null; |
| 1649 | } |
| 1650 | |
| 1651 | if ( !is_array( $func ) && method_exists( $this, $func ) ) { |
| 1652 | $func = [ $this, $func ]; |
| 1653 | } elseif ( $passSelf ) { |
| 1654 | array_unshift( $params, $this ); |
| 1655 | } |
| 1656 | |
| 1657 | return $func( ...$params ); |
| 1658 | } |
| 1659 | |
| 1660 | } |
| 1661 | |
| 1662 | /** @deprecated class alias since 1.42 */ |
| 1663 | class_alias( DatabaseUpdater::class, 'DatabaseUpdater' ); |