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