Merge "Mark InnoDB as recomended in the installer."
[lhc/web/wiklou.git] / includes / installer / DatabaseUpdater.php
1 <?php
2 /**
3 * DBMS-specific updater helper.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Deployment
22 */
23 use Wikimedia\Rdbms\Database;
24 use Wikimedia\Rdbms\IDatabase;
25 use MediaWiki\MediaWikiServices;
26
27 require_once __DIR__ . '/../../maintenance/Maintenance.php';
28
29 /**
30 * Class for handling database updates. Roughly based off of updaters.inc, with
31 * a few improvements :)
32 *
33 * @ingroup Deployment
34 * @since 1.17
35 */
36 abstract class DatabaseUpdater {
37 /**
38 * Array of updates to perform on the database
39 *
40 * @var array
41 */
42 protected $updates = [];
43
44 /**
45 * Array of updates that were skipped
46 *
47 * @var array
48 */
49 protected $updatesSkipped = [];
50
51 /**
52 * List of extension-provided database updates
53 * @var array
54 */
55 protected $extensionUpdates = [];
56
57 /**
58 * Handle to the database subclass
59 *
60 * @var Database
61 */
62 protected $db;
63
64 /**
65 * @var Maintenance
66 */
67 protected $maintenance;
68
69 protected $shared = false;
70
71 /**
72 * @var string[] Scripts to run after database update
73 * Should be a subclass of LoggedUpdateMaintenance
74 */
75 protected $postDatabaseUpdateMaintenance = [
76 DeleteDefaultMessages::class,
77 PopulateRevisionLength::class,
78 PopulateRevisionSha1::class,
79 PopulateImageSha1::class,
80 FixExtLinksProtocolRelative::class,
81 PopulateFilearchiveSha1::class,
82 PopulateBacklinkNamespace::class,
83 FixDefaultJsonContentPages::class,
84 CleanupEmptyCategories::class,
85 AddRFCandPMIDInterwiki::class,
86 PopulatePPSortKey::class,
87 PopulateIpChanges::class,
88 ];
89
90 /**
91 * File handle for SQL output.
92 *
93 * @var resource
94 */
95 protected $fileHandle = null;
96
97 /**
98 * Flag specifying whether or not to skip schema (e.g. SQL-only) updates.
99 *
100 * @var bool
101 */
102 protected $skipSchema = false;
103
104 /**
105 * Hold the value of $wgContentHandlerUseDB during the upgrade.
106 */
107 protected $holdContentHandlerUseDB = true;
108
109 /**
110 * @param Database &$db To perform updates on
111 * @param bool $shared Whether to perform updates on shared tables
112 * @param Maintenance|null $maintenance Maintenance object which created us
113 */
114 protected function __construct( Database &$db, $shared, Maintenance $maintenance = null ) {
115 $this->db = $db;
116 $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
117 $this->shared = $shared;
118 if ( $maintenance ) {
119 $this->maintenance = $maintenance;
120 $this->fileHandle = $maintenance->fileHandle;
121 } else {
122 $this->maintenance = new FakeMaintenance;
123 }
124 $this->maintenance->setDB( $db );
125 $this->initOldGlobals();
126 $this->loadExtensions();
127 Hooks::run( 'LoadExtensionSchemaUpdates', [ $this ] );
128 }
129
130 /**
131 * Initialize all of the old globals. One day this should all become
132 * something much nicer
133 */
134 private function initOldGlobals() {
135 global $wgExtNewTables, $wgExtNewFields, $wgExtPGNewFields,
136 $wgExtPGAlteredFields, $wgExtNewIndexes, $wgExtModifiedFields;
137
138 # For extensions only, should be populated via hooks
139 # $wgDBtype should be checked to specify the proper file
140 $wgExtNewTables = []; // table, dir
141 $wgExtNewFields = []; // table, column, dir
142 $wgExtPGNewFields = []; // table, column, column attributes; for PostgreSQL
143 $wgExtPGAlteredFields = []; // table, column, new type, conversion method; for PostgreSQL
144 $wgExtNewIndexes = []; // table, index, dir
145 $wgExtModifiedFields = []; // table, index, dir
146 }
147
148 /**
149 * Loads LocalSettings.php, if needed, and initialises everything needed for
150 * LoadExtensionSchemaUpdates hook.
151 */
152 private function loadExtensions() {
153 if ( !defined( 'MEDIAWIKI_INSTALL' ) || defined( 'MW_EXTENSIONS_LOADED' ) ) {
154 return; // already loaded
155 }
156 $vars = Installer::getExistingLocalSettings();
157
158 $registry = ExtensionRegistry::getInstance();
159 $queue = $registry->getQueue();
160 // Don't accidentally load extensions in the future
161 $registry->clearQueue();
162
163 // This will automatically add "AutoloadClasses" to $wgAutoloadClasses
164 $data = $registry->readFromQueue( $queue );
165 $hooks = [];
166 if ( isset( $data['globals']['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
167 $hooks = $data['globals']['wgHooks']['LoadExtensionSchemaUpdates'];
168 }
169 if ( $vars && isset( $vars['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
170 $hooks = array_merge_recursive( $hooks, $vars['wgHooks']['LoadExtensionSchemaUpdates'] );
171 }
172 global $wgHooks, $wgAutoloadClasses;
173 $wgHooks['LoadExtensionSchemaUpdates'] = $hooks;
174 if ( $vars && isset( $vars['wgAutoloadClasses'] ) ) {
175 $wgAutoloadClasses += $vars['wgAutoloadClasses'];
176 }
177 }
178
179 /**
180 * @param Database $db
181 * @param bool $shared
182 * @param Maintenance|null $maintenance
183 *
184 * @throws MWException
185 * @return DatabaseUpdater
186 */
187 public static function newForDB( Database $db, $shared = false, Maintenance $maintenance = null ) {
188 $type = $db->getType();
189 if ( in_array( $type, Installer::getDBTypes() ) ) {
190 $class = ucfirst( $type ) . 'Updater';
191
192 return new $class( $db, $shared, $maintenance );
193 } else {
194 throw new MWException( __METHOD__ . ' called for unsupported $wgDBtype' );
195 }
196 }
197
198 /**
199 * Get a database connection to run updates
200 *
201 * @return Database
202 */
203 public function getDB() {
204 return $this->db;
205 }
206
207 /**
208 * Output some text. If we're running from web, escape the text first.
209 *
210 * @param string $str Text to output
211 */
212 public function output( $str ) {
213 if ( $this->maintenance->isQuiet() ) {
214 return;
215 }
216 global $wgCommandLineMode;
217 if ( !$wgCommandLineMode ) {
218 $str = htmlspecialchars( $str );
219 }
220 echo $str;
221 flush();
222 }
223
224 /**
225 * Add a new update coming from an extension. This should be called by
226 * extensions while executing the LoadExtensionSchemaUpdates hook.
227 *
228 * @since 1.17
229 *
230 * @param array $update The update to run. Format is [ $callback, $params... ]
231 * $callback is the method to call; either a DatabaseUpdater method name or a callable.
232 * Must be serializable (ie. no anonymous functions allowed). The rest of the parameters
233 * (if any) will be passed to the callback. The first parameter passed to the callback
234 * is always this object.
235 */
236 public function addExtensionUpdate( array $update ) {
237 $this->extensionUpdates[] = $update;
238 }
239
240 /**
241 * Convenience wrapper for addExtensionUpdate() when adding a new table (which
242 * is the most common usage of updaters in an extension)
243 *
244 * @since 1.18
245 *
246 * @param string $tableName Name of table to create
247 * @param string $sqlPath Full path to the schema file
248 */
249 public function addExtensionTable( $tableName, $sqlPath ) {
250 $this->extensionUpdates[] = [ 'addTable', $tableName, $sqlPath, true ];
251 }
252
253 /**
254 * @since 1.19
255 *
256 * @param string $tableName
257 * @param string $indexName
258 * @param string $sqlPath
259 */
260 public function addExtensionIndex( $tableName, $indexName, $sqlPath ) {
261 $this->extensionUpdates[] = [ 'addIndex', $tableName, $indexName, $sqlPath, true ];
262 }
263
264 /**
265 *
266 * @since 1.19
267 *
268 * @param string $tableName
269 * @param string $columnName
270 * @param string $sqlPath
271 */
272 public function addExtensionField( $tableName, $columnName, $sqlPath ) {
273 $this->extensionUpdates[] = [ 'addField', $tableName, $columnName, $sqlPath, true ];
274 }
275
276 /**
277 *
278 * @since 1.20
279 *
280 * @param string $tableName
281 * @param string $columnName
282 * @param string $sqlPath
283 */
284 public function dropExtensionField( $tableName, $columnName, $sqlPath ) {
285 $this->extensionUpdates[] = [ 'dropField', $tableName, $columnName, $sqlPath, true ];
286 }
287
288 /**
289 * Drop an index from an extension table
290 *
291 * @since 1.21
292 *
293 * @param string $tableName The table name
294 * @param string $indexName The index name
295 * @param string $sqlPath The path to the SQL change path
296 */
297 public function dropExtensionIndex( $tableName, $indexName, $sqlPath ) {
298 $this->extensionUpdates[] = [ 'dropIndex', $tableName, $indexName, $sqlPath, true ];
299 }
300
301 /**
302 *
303 * @since 1.20
304 *
305 * @param string $tableName
306 * @param string $sqlPath
307 */
308 public function dropExtensionTable( $tableName, $sqlPath ) {
309 $this->extensionUpdates[] = [ 'dropTable', $tableName, $sqlPath, true ];
310 }
311
312 /**
313 * Rename an index on an extension table
314 *
315 * @since 1.21
316 *
317 * @param string $tableName The table name
318 * @param string $oldIndexName The old index name
319 * @param string $newIndexName The new index name
320 * @param string $sqlPath The path to the SQL change path
321 * @param bool $skipBothIndexExistWarning Whether to warn if both the old
322 * and the new indexes exist. [facultative; by default, false]
323 */
324 public function renameExtensionIndex( $tableName, $oldIndexName, $newIndexName,
325 $sqlPath, $skipBothIndexExistWarning = false
326 ) {
327 $this->extensionUpdates[] = [
328 'renameIndex',
329 $tableName,
330 $oldIndexName,
331 $newIndexName,
332 $skipBothIndexExistWarning,
333 $sqlPath,
334 true
335 ];
336 }
337
338 /**
339 * @since 1.21
340 *
341 * @param string $tableName The table name
342 * @param string $fieldName The field to be modified
343 * @param string $sqlPath The path to the SQL patch
344 */
345 public function modifyExtensionField( $tableName, $fieldName, $sqlPath ) {
346 $this->extensionUpdates[] = [ 'modifyField', $tableName, $fieldName, $sqlPath, true ];
347 }
348
349 /**
350 * @since 1.31
351 *
352 * @param string $tableName The table name
353 * @param string $sqlPath The path to the SQL patch
354 */
355 public function modifyExtensionTable( $tableName, $sqlPath ) {
356 $this->extensionUpdates[] = [ 'modifyTable', $tableName, $sqlPath, true ];
357 }
358
359 /**
360 *
361 * @since 1.20
362 *
363 * @param string $tableName
364 * @return bool
365 */
366 public function tableExists( $tableName ) {
367 return ( $this->db->tableExists( $tableName, __METHOD__ ) );
368 }
369
370 /**
371 * Add a maintenance script to be run after the database updates are complete.
372 *
373 * Script should subclass LoggedUpdateMaintenance
374 *
375 * @since 1.19
376 *
377 * @param string $class Name of a Maintenance subclass
378 */
379 public function addPostDatabaseUpdateMaintenance( $class ) {
380 $this->postDatabaseUpdateMaintenance[] = $class;
381 }
382
383 /**
384 * Get the list of extension-defined updates
385 *
386 * @return array
387 */
388 protected function getExtensionUpdates() {
389 return $this->extensionUpdates;
390 }
391
392 /**
393 * @since 1.17
394 *
395 * @return string[]
396 */
397 public function getPostDatabaseUpdateMaintenance() {
398 return $this->postDatabaseUpdateMaintenance;
399 }
400
401 /**
402 * @since 1.21
403 *
404 * Writes the schema updates desired to a file for the DB Admin to run.
405 * @param array $schemaUpdate
406 */
407 private function writeSchemaUpdateFile( array $schemaUpdate = [] ) {
408 $updates = $this->updatesSkipped;
409 $this->updatesSkipped = [];
410
411 foreach ( $updates as $funcList ) {
412 $func = $funcList[0];
413 $args = $funcList[1];
414 $origParams = $funcList[2];
415 $func( ...$args );
416 flush();
417 $this->updatesSkipped[] = $origParams;
418 }
419 }
420
421 /**
422 * Get appropriate schema variables in the current database connection.
423 *
424 * This should be called after any request data has been imported, but before
425 * any write operations to the database. The result should be passed to the DB
426 * setSchemaVars() method.
427 *
428 * @return array
429 * @since 1.28
430 */
431 public function getSchemaVars() {
432 return []; // DB-type specific
433 }
434
435 /**
436 * Do all the updates
437 *
438 * @param array $what What updates to perform
439 */
440 public function doUpdates( array $what = [ 'core', 'extensions', 'stats' ] ) {
441 $this->db->setSchemaVars( $this->getSchemaVars() );
442
443 $what = array_flip( $what );
444 $this->skipSchema = isset( $what['noschema'] ) || $this->fileHandle !== null;
445 if ( isset( $what['core'] ) ) {
446 $this->runUpdates( $this->getCoreUpdateList(), false );
447 }
448 if ( isset( $what['extensions'] ) ) {
449 $this->runUpdates( $this->getOldGlobalUpdates(), false );
450 $this->runUpdates( $this->getExtensionUpdates(), true );
451 }
452
453 if ( isset( $what['stats'] ) ) {
454 $this->checkStats();
455 }
456
457 if ( $this->fileHandle ) {
458 $this->skipSchema = false;
459 $this->writeSchemaUpdateFile();
460 }
461 }
462
463 /**
464 * Helper function for doUpdates()
465 *
466 * @param array $updates Array of updates to run
467 * @param bool $passSelf Whether to pass this object we calling external functions
468 */
469 private function runUpdates( array $updates, $passSelf ) {
470 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
471
472 $updatesDone = [];
473 $updatesSkipped = [];
474 foreach ( $updates as $params ) {
475 $origParams = $params;
476 $func = array_shift( $params );
477 if ( !is_array( $func ) && method_exists( $this, $func ) ) {
478 $func = [ $this, $func ];
479 } elseif ( $passSelf ) {
480 array_unshift( $params, $this );
481 }
482 $ret = $func( ...$params );
483 flush();
484 if ( $ret !== false ) {
485 $updatesDone[] = $origParams;
486 $lbFactory->waitForReplication();
487 } else {
488 $updatesSkipped[] = [ $func, $params, $origParams ];
489 }
490 }
491 $this->updatesSkipped = array_merge( $this->updatesSkipped, $updatesSkipped );
492 $this->updates = array_merge( $this->updates, $updatesDone );
493 }
494
495 /**
496 * Helper function: check if the given key is present in the updatelog table.
497 * Obviously, only use this for updates that occur after the updatelog table was
498 * created!
499 * @param string $key Name of the key to check for
500 * @return bool
501 */
502 public function updateRowExists( $key ) {
503 $row = $this->db->selectRow(
504 'updatelog',
505 # T67813
506 '1 AS X',
507 [ 'ul_key' => $key ],
508 __METHOD__
509 );
510
511 return (bool)$row;
512 }
513
514 /**
515 * Helper function: Add a key to the updatelog table
516 * Obviously, only use this for updates that occur after the updatelog table was
517 * created!
518 * @param string $key Name of key to insert
519 * @param string|null $val [optional] Value to insert along with the key
520 */
521 public function insertUpdateRow( $key, $val = null ) {
522 $this->db->clearFlag( DBO_DDLMODE );
523 $values = [ 'ul_key' => $key ];
524 if ( $val && $this->canUseNewUpdatelog() ) {
525 $values['ul_value'] = $val;
526 }
527 $this->db->insert( 'updatelog', $values, __METHOD__, 'IGNORE' );
528 $this->db->setFlag( DBO_DDLMODE );
529 }
530
531 /**
532 * Updatelog was changed in 1.17 to have a ul_value column so we can record
533 * more information about what kind of updates we've done (that's what this
534 * class does). Pre-1.17 wikis won't have this column, and really old wikis
535 * might not even have updatelog at all
536 *
537 * @return bool
538 */
539 protected function canUseNewUpdatelog() {
540 return $this->db->tableExists( 'updatelog', __METHOD__ ) &&
541 $this->db->fieldExists( 'updatelog', 'ul_value', __METHOD__ );
542 }
543
544 /**
545 * Returns whether updates should be executed on the database table $name.
546 * Updates will be prevented if the table is a shared table and it is not
547 * specified to run updates on shared tables.
548 *
549 * @param string $name Table name
550 * @return bool
551 */
552 protected function doTable( $name ) {
553 global $wgSharedDB, $wgSharedTables;
554
555 // Don't bother to check $wgSharedTables if there isn't a shared database
556 // or the user actually also wants to do updates on the shared database.
557 if ( $wgSharedDB === null || $this->shared ) {
558 return true;
559 }
560
561 if ( in_array( $name, $wgSharedTables ) ) {
562 $this->output( "...skipping update to shared table $name.\n" );
563 return false;
564 } else {
565 return true;
566 }
567 }
568
569 /**
570 * Before 1.17, we used to handle updates via stuff like
571 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
572 * of this in 1.17 but we want to remain back-compatible for a while. So
573 * load up these old global-based things into our update list.
574 *
575 * @return array
576 */
577 protected function getOldGlobalUpdates() {
578 global $wgExtNewFields, $wgExtNewTables, $wgExtModifiedFields,
579 $wgExtNewIndexes;
580
581 $updates = [];
582
583 foreach ( $wgExtNewTables as $tableRecord ) {
584 $updates[] = [
585 'addTable', $tableRecord[0], $tableRecord[1], true
586 ];
587 }
588
589 foreach ( $wgExtNewFields as $fieldRecord ) {
590 $updates[] = [
591 'addField', $fieldRecord[0], $fieldRecord[1],
592 $fieldRecord[2], true
593 ];
594 }
595
596 foreach ( $wgExtNewIndexes as $fieldRecord ) {
597 $updates[] = [
598 'addIndex', $fieldRecord[0], $fieldRecord[1],
599 $fieldRecord[2], true
600 ];
601 }
602
603 foreach ( $wgExtModifiedFields as $fieldRecord ) {
604 $updates[] = [
605 'modifyField', $fieldRecord[0], $fieldRecord[1],
606 $fieldRecord[2], true
607 ];
608 }
609
610 return $updates;
611 }
612
613 /**
614 * Get an array of updates to perform on the database. Should return a
615 * multi-dimensional array. The main key is the MediaWiki version (1.12,
616 * 1.13...) with the values being arrays of updates, identical to how
617 * updaters.inc did it (for now)
618 *
619 * @return array[]
620 */
621 abstract protected function getCoreUpdateList();
622
623 /**
624 * Append an SQL fragment to the open file handle.
625 *
626 * @param string $filename File name to open
627 */
628 public function copyFile( $filename ) {
629 $this->db->sourceFile(
630 $filename,
631 null,
632 null,
633 __METHOD__,
634 [ $this, 'appendLine' ]
635 );
636 }
637
638 /**
639 * Append a line to the open filehandle. The line is assumed to
640 * be a complete SQL statement.
641 *
642 * This is used as a callback for sourceLine().
643 *
644 * @param string $line Text to append to the file
645 * @return bool False to skip actually executing the file
646 * @throws MWException
647 */
648 public function appendLine( $line ) {
649 $line = rtrim( $line ) . ";\n";
650 if ( fwrite( $this->fileHandle, $line ) === false ) {
651 throw new MWException( "trouble writing file" );
652 }
653
654 return false;
655 }
656
657 /**
658 * Applies a SQL patch
659 *
660 * @param string $path Path to the patch file
661 * @param bool $isFullPath Whether to treat $path as a relative or not
662 * @param string|null $msg Description of the patch
663 * @return bool False if patch is skipped.
664 */
665 protected function applyPatch( $path, $isFullPath = false, $msg = null ) {
666 if ( $msg === null ) {
667 $msg = "Applying $path patch";
668 }
669 if ( $this->skipSchema ) {
670 $this->output( "...skipping schema change ($msg).\n" );
671
672 return false;
673 }
674
675 $this->output( "$msg ..." );
676
677 if ( !$isFullPath ) {
678 $path = $this->patchPath( $this->db, $path );
679 }
680 if ( $this->fileHandle !== null ) {
681 $this->copyFile( $path );
682 } else {
683 $this->db->sourceFile( $path );
684 }
685 $this->output( "done.\n" );
686
687 return true;
688 }
689
690 /**
691 * Get the full path of a patch file. Originally based on archive()
692 * from updaters.inc. Keep in mind this always returns a patch, as
693 * it fails back to MySQL if no DB-specific patch can be found
694 *
695 * @param IDatabase $db
696 * @param string $patch The name of the patch, like patch-something.sql
697 * @return string Full path to patch file
698 */
699 public function patchPath( IDatabase $db, $patch ) {
700 global $IP;
701
702 $dbType = $db->getType();
703 if ( file_exists( "$IP/maintenance/$dbType/archives/$patch" ) ) {
704 return "$IP/maintenance/$dbType/archives/$patch";
705 } else {
706 return "$IP/maintenance/archives/$patch";
707 }
708 }
709
710 /**
711 * Add a new table to the database
712 *
713 * @param string $name Name of the new table
714 * @param string $patch Path to the patch file
715 * @param bool $fullpath Whether to treat $patch path as a relative or not
716 * @return bool False if this was skipped because schema changes are skipped
717 */
718 protected function addTable( $name, $patch, $fullpath = false ) {
719 if ( !$this->doTable( $name ) ) {
720 return true;
721 }
722
723 if ( $this->db->tableExists( $name, __METHOD__ ) ) {
724 $this->output( "...$name table already exists.\n" );
725 } else {
726 return $this->applyPatch( $patch, $fullpath, "Creating $name table" );
727 }
728
729 return true;
730 }
731
732 /**
733 * Add a new field to an existing table
734 *
735 * @param string $table Name of the table to modify
736 * @param string $field Name of the new field
737 * @param string $patch Path to the patch file
738 * @param bool $fullpath Whether to treat $patch path as a relative or not
739 * @return bool False if this was skipped because schema changes are skipped
740 */
741 protected function addField( $table, $field, $patch, $fullpath = false ) {
742 if ( !$this->doTable( $table ) ) {
743 return true;
744 }
745
746 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
747 $this->output( "...$table table does not exist, skipping new field patch.\n" );
748 } elseif ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
749 $this->output( "...have $field field in $table table.\n" );
750 } else {
751 return $this->applyPatch( $patch, $fullpath, "Adding $field field to table $table" );
752 }
753
754 return true;
755 }
756
757 /**
758 * Add a new index to an existing table
759 *
760 * @param string $table Name of the table to modify
761 * @param string $index Name of the new index
762 * @param string $patch Path to the patch file
763 * @param bool $fullpath Whether to treat $patch path as a relative or not
764 * @return bool False if this was skipped because schema changes are skipped
765 */
766 protected function addIndex( $table, $index, $patch, $fullpath = false ) {
767 if ( !$this->doTable( $table ) ) {
768 return true;
769 }
770
771 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
772 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
773 } elseif ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
774 $this->output( "...index $index already set on $table table.\n" );
775 } else {
776 return $this->applyPatch( $patch, $fullpath, "Adding index $index to table $table" );
777 }
778
779 return true;
780 }
781
782 /**
783 * Add a new index to an existing table if none of the given indexes exist
784 *
785 * @param string $table Name of the table to modify
786 * @param string[] $indexes Name of the indexes to check. $indexes[0] should
787 * be the one actually being added.
788 * @param string $patch Path to the patch file
789 * @param bool $fullpath Whether to treat $patch path as a relative or not
790 * @return bool False if this was skipped because schema changes are skipped
791 */
792 protected function addIndexIfNoneExist( $table, $indexes, $patch, $fullpath = false ) {
793 if ( !$this->doTable( $table ) ) {
794 return true;
795 }
796
797 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
798 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
799 return true;
800 }
801
802 $newIndex = $indexes[0];
803 foreach ( $indexes as $index ) {
804 if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
805 $this->output(
806 "...skipping index $newIndex because index $index already set on $table table.\n"
807 );
808 return true;
809 }
810 }
811
812 return $this->applyPatch( $patch, $fullpath, "Adding index $index to table $table" );
813 }
814
815 /**
816 * Drop a field from an existing table
817 *
818 * @param string $table Name of the table to modify
819 * @param string $field Name of the old field
820 * @param string $patch Path to the patch file
821 * @param bool $fullpath Whether to treat $patch path as a relative or not
822 * @return bool False if this was skipped because schema changes are skipped
823 */
824 protected function dropField( $table, $field, $patch, $fullpath = false ) {
825 if ( !$this->doTable( $table ) ) {
826 return true;
827 }
828
829 if ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
830 return $this->applyPatch( $patch, $fullpath, "Table $table contains $field field. Dropping" );
831 } else {
832 $this->output( "...$table table does not contain $field field.\n" );
833 }
834
835 return true;
836 }
837
838 /**
839 * Drop an index from an existing table
840 *
841 * @param string $table Name of the table to modify
842 * @param string $index Name of the index
843 * @param string $patch Path to the patch file
844 * @param bool $fullpath Whether to treat $patch path as a relative or not
845 * @return bool False if this was skipped because schema changes are skipped
846 */
847 protected function dropIndex( $table, $index, $patch, $fullpath = false ) {
848 if ( !$this->doTable( $table ) ) {
849 return true;
850 }
851
852 if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
853 return $this->applyPatch( $patch, $fullpath, "Dropping $index index from table $table" );
854 } else {
855 $this->output( "...$index key doesn't exist.\n" );
856 }
857
858 return true;
859 }
860
861 /**
862 * Rename an index from an existing table
863 *
864 * @param string $table Name of the table to modify
865 * @param string $oldIndex Old name of the index
866 * @param string $newIndex New name of the index
867 * @param bool $skipBothIndexExistWarning Whether to warn if both the
868 * old and the new indexes exist.
869 * @param string $patch Path to the patch file
870 * @param bool $fullpath Whether to treat $patch path as a relative or not
871 * @return bool False if this was skipped because schema changes are skipped
872 */
873 protected function renameIndex( $table, $oldIndex, $newIndex,
874 $skipBothIndexExistWarning, $patch, $fullpath = false
875 ) {
876 if ( !$this->doTable( $table ) ) {
877 return true;
878 }
879
880 // First requirement: the table must exist
881 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
882 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
883
884 return true;
885 }
886
887 // Second requirement: the new index must be missing
888 if ( $this->db->indexExists( $table, $newIndex, __METHOD__ ) ) {
889 $this->output( "...index $newIndex already set on $table table.\n" );
890 if ( !$skipBothIndexExistWarning &&
891 $this->db->indexExists( $table, $oldIndex, __METHOD__ )
892 ) {
893 $this->output( "...WARNING: $oldIndex still exists, despite it has " .
894 "been renamed into $newIndex (which also exists).\n" .
895 " $oldIndex should be manually removed if not needed anymore.\n" );
896 }
897
898 return true;
899 }
900
901 // Third requirement: the old index must exist
902 if ( !$this->db->indexExists( $table, $oldIndex, __METHOD__ ) ) {
903 $this->output( "...skipping: index $oldIndex doesn't exist.\n" );
904
905 return true;
906 }
907
908 // Requirements have been satisfied, patch can be applied
909 return $this->applyPatch(
910 $patch,
911 $fullpath,
912 "Renaming index $oldIndex into $newIndex to table $table"
913 );
914 }
915
916 /**
917 * If the specified table exists, drop it, or execute the
918 * patch if one is provided.
919 *
920 * Public @since 1.20
921 *
922 * @param string $table Table to drop.
923 * @param string|bool $patch String of patch file that will drop the table. Default: false.
924 * @param bool $fullpath Whether $patch is a full path. Default: false.
925 * @return bool False if this was skipped because schema changes are skipped
926 */
927 public function dropTable( $table, $patch = false, $fullpath = false ) {
928 if ( !$this->doTable( $table ) ) {
929 return true;
930 }
931
932 if ( $this->db->tableExists( $table, __METHOD__ ) ) {
933 $msg = "Dropping table $table";
934
935 if ( $patch === false ) {
936 $this->output( "$msg ..." );
937 $this->db->dropTable( $table, __METHOD__ );
938 $this->output( "done.\n" );
939 } else {
940 return $this->applyPatch( $patch, $fullpath, $msg );
941 }
942 } else {
943 $this->output( "...$table doesn't exist.\n" );
944 }
945
946 return true;
947 }
948
949 /**
950 * Modify an existing field
951 *
952 * @param string $table Name of the table to which the field belongs
953 * @param string $field Name of the field to modify
954 * @param string $patch Path to the patch file
955 * @param bool $fullpath Whether to treat $patch path as a relative or not
956 * @return bool False if this was skipped because schema changes are skipped
957 */
958 public function modifyField( $table, $field, $patch, $fullpath = false ) {
959 if ( !$this->doTable( $table ) ) {
960 return true;
961 }
962
963 $updateKey = "$table-$field-$patch";
964 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
965 $this->output( "...$table table does not exist, skipping modify field patch.\n" );
966 } elseif ( !$this->db->fieldExists( $table, $field, __METHOD__ ) ) {
967 $this->output( "...$field field does not exist in $table table, " .
968 "skipping modify field patch.\n" );
969 } elseif ( $this->updateRowExists( $updateKey ) ) {
970 $this->output( "...$field in table $table already modified by patch $patch.\n" );
971 } else {
972 $apply = $this->applyPatch( $patch, $fullpath, "Modifying $field field of table $table" );
973 if ( $apply ) {
974 $this->insertUpdateRow( $updateKey );
975 }
976 return $apply;
977 }
978 return true;
979 }
980
981 /**
982 * Modify an existing table, similar to modifyField. Intended for changes that
983 * touch more than one column on a table.
984 *
985 * @param string $table Name of the table to modify
986 * @param string $patch Name of the patch file to apply
987 * @param string|bool $fullpath Whether to treat $patch path as relative or not, defaults to false
988 * @return bool False if this was skipped because of schema changes being skipped
989 */
990 public function modifyTable( $table, $patch, $fullpath = false ) {
991 if ( !$this->doTable( $table ) ) {
992 return true;
993 }
994
995 $updateKey = "$table-$patch";
996 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
997 $this->output( "...$table table does not exist, skipping modify table patch.\n" );
998 } elseif ( $this->updateRowExists( $updateKey ) ) {
999 $this->output( "...table $table already modified by patch $patch.\n" );
1000 } else {
1001 $apply = $this->applyPatch( $patch, $fullpath, "Modifying table $table" );
1002 if ( $apply ) {
1003 $this->insertUpdateRow( $updateKey );
1004 }
1005 return $apply;
1006 }
1007 return true;
1008 }
1009
1010 /**
1011 * Run a maintenance script
1012 *
1013 * This should only be used when the maintenance script must run before
1014 * later updates. If later updates don't depend on the script, add it to
1015 * DatabaseUpdater::$postDatabaseUpdateMaintenance instead.
1016 *
1017 * The script's execute() method must return true to indicate successful
1018 * completion, and must return false (or throw an exception) to indicate
1019 * unsuccessful completion.
1020 *
1021 * @since 1.32
1022 * @param string $class Maintenance subclass
1023 * @param string $script Script path and filename, usually "maintenance/fooBar.php"
1024 */
1025 public function runMaintenance( $class, $script ) {
1026 $this->output( "Running $script...\n" );
1027 $task = $this->maintenance->runChild( $class );
1028 $ok = $task->execute();
1029 if ( !$ok ) {
1030 throw new RuntimeException( "Execution of $script did not complete successfully." );
1031 }
1032 $this->output( "done.\n" );
1033 }
1034
1035 /**
1036 * Set any .htaccess files or equivilent for storage repos
1037 *
1038 * Some zones (e.g. "temp") used to be public and may have been initialized as such
1039 */
1040 public function setFileAccess() {
1041 $repo = RepoGroup::singleton()->getLocalRepo();
1042 $zonePath = $repo->getZonePath( 'temp' );
1043 if ( $repo->getBackend()->directoryExists( [ 'dir' => $zonePath ] ) ) {
1044 // If the directory was never made, then it will have the right ACLs when it is made
1045 $status = $repo->getBackend()->secure( [
1046 'dir' => $zonePath,
1047 'noAccess' => true,
1048 'noListing' => true
1049 ] );
1050 if ( $status->isOK() ) {
1051 $this->output( "Set the local repo temp zone container to be private.\n" );
1052 } else {
1053 $this->output( "Failed to set the local repo temp zone container to be private.\n" );
1054 }
1055 }
1056 }
1057
1058 /**
1059 * Purge various database caches
1060 */
1061 public function purgeCache() {
1062 global $wgLocalisationCacheConf;
1063 // We can't guarantee that the user will be able to use TRUNCATE,
1064 // but we know that DELETE is available to us
1065 $this->output( "Purging caches..." );
1066
1067 // ObjectCache
1068 $this->db->delete( 'objectcache', '*', __METHOD__ );
1069
1070 // LocalisationCache
1071 if ( $wgLocalisationCacheConf['manualRecache'] ) {
1072 $this->rebuildLocalisationCache();
1073 }
1074
1075 // ResourceLoader: Message cache
1076 $blobStore = new MessageBlobStore();
1077 $blobStore->clear();
1078
1079 // ResourceLoader: File-dependency cache
1080 $this->db->delete( 'module_deps', '*', __METHOD__ );
1081 $this->output( "done.\n" );
1082 }
1083
1084 /**
1085 * Check the site_stats table is not properly populated.
1086 */
1087 protected function checkStats() {
1088 $this->output( "...site_stats is populated..." );
1089 $row = $this->db->selectRow( 'site_stats', '*', [ 'ss_row_id' => 1 ], __METHOD__ );
1090 if ( $row === false ) {
1091 $this->output( "data is missing! rebuilding...\n" );
1092 } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
1093 $this->output( "missing ss_total_pages, rebuilding...\n" );
1094 } else {
1095 $this->output( "done.\n" );
1096
1097 return;
1098 }
1099 SiteStatsInit::doAllAndCommit( $this->db );
1100 }
1101
1102 # Common updater functions
1103
1104 /**
1105 * Sets the number of active users in the site_stats table
1106 */
1107 protected function doActiveUsersInit() {
1108 $activeUsers = $this->db->selectField( 'site_stats', 'ss_active_users', '', __METHOD__ );
1109 if ( $activeUsers == -1 ) {
1110 $activeUsers = $this->db->selectField( 'recentchanges',
1111 'COUNT( DISTINCT rc_user_text )',
1112 [ 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ], __METHOD__
1113 );
1114 $this->db->update( 'site_stats',
1115 [ 'ss_active_users' => intval( $activeUsers ) ],
1116 [ 'ss_row_id' => 1 ], __METHOD__, [ 'LIMIT' => 1 ]
1117 );
1118 }
1119 $this->output( "...ss_active_users user count set...\n" );
1120 }
1121
1122 /**
1123 * Populates the log_user_text field in the logging table
1124 */
1125 protected function doLogUsertextPopulation() {
1126 if ( !$this->updateRowExists( 'populate log_usertext' ) ) {
1127 $this->output(
1128 "Populating log_user_text field, printing progress markers. For large\n" .
1129 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1130 "maintenance/populateLogUsertext.php.\n"
1131 );
1132
1133 $task = $this->maintenance->runChild( PopulateLogUsertext::class );
1134 $task->execute();
1135 $this->output( "done.\n" );
1136 }
1137 }
1138
1139 /**
1140 * Migrate log params to new table and index for searching
1141 */
1142 protected function doLogSearchPopulation() {
1143 if ( !$this->updateRowExists( 'populate log_search' ) ) {
1144 $this->output(
1145 "Populating log_search table, printing progress markers. For large\n" .
1146 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1147 "maintenance/populateLogSearch.php.\n" );
1148
1149 $task = $this->maintenance->runChild( PopulateLogSearch::class );
1150 $task->execute();
1151 $this->output( "done.\n" );
1152 }
1153 }
1154
1155 /**
1156 * Updates the timestamps in the transcache table
1157 * @return bool
1158 */
1159 protected function doUpdateTranscacheField() {
1160 if ( $this->updateRowExists( 'convert transcache field' ) ) {
1161 $this->output( "...transcache tc_time already converted.\n" );
1162
1163 return true;
1164 }
1165
1166 return $this->applyPatch( 'patch-tc-timestamp.sql', false,
1167 "Converting tc_time from UNIX epoch to MediaWiki timestamp" );
1168 }
1169
1170 /**
1171 * Update CategoryLinks collation
1172 */
1173 protected function doCollationUpdate() {
1174 global $wgCategoryCollation;
1175 if ( $this->db->fieldExists( 'categorylinks', 'cl_collation', __METHOD__ ) ) {
1176 if ( $this->db->selectField(
1177 'categorylinks',
1178 'COUNT(*)',
1179 'cl_collation != ' . $this->db->addQuotes( $wgCategoryCollation ),
1180 __METHOD__
1181 ) == 0
1182 ) {
1183 $this->output( "...collations up-to-date.\n" );
1184
1185 return;
1186 }
1187
1188 $this->output( "Updating category collations..." );
1189 $task = $this->maintenance->runChild( UpdateCollation::class );
1190 $task->execute();
1191 $this->output( "...done.\n" );
1192 }
1193 }
1194
1195 /**
1196 * Migrates user options from the user table blob to user_properties
1197 */
1198 protected function doMigrateUserOptions() {
1199 if ( $this->db->tableExists( 'user_properties' ) ) {
1200 $cl = $this->maintenance->runChild( ConvertUserOptions::class, 'convertUserOptions.php' );
1201 $cl->execute();
1202 $this->output( "done.\n" );
1203 }
1204 }
1205
1206 /**
1207 * Enable profiling table when it's turned on
1208 */
1209 protected function doEnableProfiling() {
1210 global $wgProfiler;
1211
1212 if ( !$this->doTable( 'profiling' ) ) {
1213 return;
1214 }
1215
1216 $profileToDb = false;
1217 if ( isset( $wgProfiler['output'] ) ) {
1218 $out = $wgProfiler['output'];
1219 if ( $out === 'db' ) {
1220 $profileToDb = true;
1221 } elseif ( is_array( $out ) && in_array( 'db', $out ) ) {
1222 $profileToDb = true;
1223 }
1224 }
1225
1226 if ( $profileToDb && !$this->db->tableExists( 'profiling', __METHOD__ ) ) {
1227 $this->applyPatch( 'patch-profiling.sql', false, 'Add profiling table' );
1228 }
1229 }
1230
1231 /**
1232 * Rebuilds the localisation cache
1233 */
1234 protected function rebuildLocalisationCache() {
1235 /**
1236 * @var $cl RebuildLocalisationCache
1237 */
1238 $cl = $this->maintenance->runChild(
1239 RebuildLocalisationCache::class, 'rebuildLocalisationCache.php'
1240 );
1241 $this->output( "Rebuilding localisation cache...\n" );
1242 $cl->setForce();
1243 $cl->execute();
1244 $this->output( "done.\n" );
1245 }
1246
1247 /**
1248 * Turns off content handler fields during parts of the upgrade
1249 * where they aren't available.
1250 */
1251 protected function disableContentHandlerUseDB() {
1252 global $wgContentHandlerUseDB;
1253
1254 if ( $wgContentHandlerUseDB ) {
1255 $this->output( "Turning off Content Handler DB fields for this part of upgrade.\n" );
1256 $this->holdContentHandlerUseDB = $wgContentHandlerUseDB;
1257 $wgContentHandlerUseDB = false;
1258 }
1259 }
1260
1261 /**
1262 * Turns content handler fields back on.
1263 */
1264 protected function enableContentHandlerUseDB() {
1265 global $wgContentHandlerUseDB;
1266
1267 if ( $this->holdContentHandlerUseDB ) {
1268 $this->output( "Content Handler DB fields should be usable now.\n" );
1269 $wgContentHandlerUseDB = $this->holdContentHandlerUseDB;
1270 }
1271 }
1272
1273 /**
1274 * Migrate comments to the new 'comment' table
1275 * @since 1.30
1276 */
1277 protected function migrateComments() {
1278 global $wgCommentTableSchemaMigrationStage;
1279 if ( $wgCommentTableSchemaMigrationStage >= MIGRATION_WRITE_NEW &&
1280 !$this->updateRowExists( 'MigrateComments' )
1281 ) {
1282 $this->output(
1283 "Migrating comments to the 'comments' table, printing progress markers. For large\n" .
1284 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1285 "maintenance/migrateComments.php.\n"
1286 );
1287 $task = $this->maintenance->runChild( MigrateComments::class, 'migrateComments.php' );
1288 $ok = $task->execute();
1289 $this->output( $ok ? "done.\n" : "errors were encountered.\n" );
1290 }
1291 }
1292
1293 /**
1294 * Migrate actors to the new 'actor' table
1295 * @since 1.31
1296 */
1297 protected function migrateActors() {
1298 global $wgActorTableSchemaMigrationStage;
1299 if ( $wgActorTableSchemaMigrationStage >= MIGRATION_WRITE_NEW &&
1300 !$this->updateRowExists( 'MigrateActors' )
1301 ) {
1302 $this->output(
1303 "Migrating actors to the 'actor' table, printing progress markers. For large\n" .
1304 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1305 "maintenance/migrateActors.php.\n"
1306 );
1307 $task = $this->maintenance->runChild( 'MigrateActors', 'migrateActors.php' );
1308 $ok = $task->execute();
1309 $this->output( $ok ? "done.\n" : "errors were encountered.\n" );
1310 }
1311 }
1312
1313 /**
1314 * Migrate ar_text to modern storage
1315 * @since 1.31
1316 */
1317 protected function migrateArchiveText() {
1318 if ( $this->db->fieldExists( 'archive', 'ar_text', __METHOD__ ) ) {
1319 $this->output( "Migrating archive ar_text to modern storage.\n" );
1320 $task = $this->maintenance->runChild( MigrateArchiveText::class, 'migrateArchiveText.php' );
1321 $task->setForce();
1322 if ( $task->execute() ) {
1323 $this->applyPatch( 'patch-drop-ar_text.sql', false,
1324 'Dropping ar_text and ar_flags columns' );
1325 }
1326 }
1327 }
1328
1329 /**
1330 * Populate ar_rev_id, then make it not nullable
1331 * @since 1.31
1332 */
1333 protected function populateArchiveRevId() {
1334 $info = $this->db->fieldInfo( 'archive', 'ar_rev_id', __METHOD__ );
1335 if ( !$info ) {
1336 throw new MWException( 'Missing ar_rev_id field of archive table. Should not happen.' );
1337 }
1338 if ( $info->isNullable() ) {
1339 $this->output( "Populating ar_rev_id.\n" );
1340 $task = $this->maintenance->runChild( 'PopulateArchiveRevId', 'populateArchiveRevId.php' );
1341 if ( $task->execute() ) {
1342 $this->applyPatch( 'patch-ar_rev_id-not-null.sql', false,
1343 'Making ar_rev_id not nullable' );
1344 }
1345 }
1346 }
1347
1348 /**
1349 * Populates the externallinks.el_index_60 field
1350 * @since 1.32
1351 */
1352 protected function populateExternallinksIndex60() {
1353 if ( !$this->updateRowExists( 'populate externallinks.el_index_60' ) ) {
1354 $this->output(
1355 "Populating el_index_60 field, printing progress markers. For large\n" .
1356 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1357 "maintenance/populateExternallinksIndex60.php.\n"
1358 );
1359 $task = $this->maintenance->runChild( 'PopulateExternallinksIndex60',
1360 'populateExternallinksIndex60.php' );
1361 $task->execute();
1362 $this->output( "done.\n" );
1363 }
1364 }
1365
1366 /**
1367 * Populates the MCR content tables
1368 * @since 1.32
1369 */
1370 protected function populateContentTables() {
1371 global $wgMultiContentRevisionSchemaMigrationStage;
1372 if ( ( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) &&
1373 !$this->updateRowExists( 'PopulateContentTables' )
1374 ) {
1375 $this->output(
1376 "Migrating revision data to the MCR 'slot' and 'content' tables, printing progress markers.\n" .
1377 "For large databases, you may want to hit Ctrl-C and do this manually with\n" .
1378 "maintenance/populateContentTables.php.\n"
1379 );
1380 $task = $this->maintenance->runChild(
1381 PopulateContentTables::class, 'populateContentTables.php'
1382 );
1383 $ok = $task->execute();
1384 $this->output( $ok ? "done.\n" : "errors were encountered.\n" );
1385 if ( $ok ) {
1386 $this->insertUpdateRow( 'PopulateContentTables' );
1387 }
1388 }
1389 }
1390 }