Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / installer / DatabaseUpdater.php
blob6a20abc9024aeec946c3f15165aacafdd6f1e089
1 <?php
2 /**
3 * DBMS-specific updater helper.
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.
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.
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
20 * @file
21 * @ingroup Deployment
24 require_once __DIR__ . '/../../maintenance/Maintenance.php';
26 /**
27 * Class for handling database updates. Roughly based off of updaters.inc, with
28 * a few improvements :)
30 * @ingroup Deployment
31 * @since 1.17
33 abstract class DatabaseUpdater {
34 protected static $updateCounter = 0;
36 /**
37 * Array of updates to perform on the database
39 * @var array
41 protected $updates = [];
43 /**
44 * Array of updates that were skipped
46 * @var array
48 protected $updatesSkipped = [];
50 /**
51 * List of extension-provided database updates
52 * @var array
54 protected $extensionUpdates = [];
56 /**
57 * Handle to the database subclass
59 * @var DatabaseBase
61 protected $db;
63 protected $shared = false;
65 /**
66 * @var string[] Scripts to run after database update
67 * Should be a subclass of LoggedUpdateMaintenance
69 protected $postDatabaseUpdateMaintenance = [
70 DeleteDefaultMessages::class,
71 PopulateRevisionLength::class,
72 PopulateRevisionSha1::class,
73 PopulateImageSha1::class,
74 FixExtLinksProtocolRelative::class,
75 PopulateFilearchiveSha1::class,
76 PopulateBacklinkNamespace::class,
77 FixDefaultJsonContentPages::class,
80 /**
81 * File handle for SQL output.
83 * @var resource
85 protected $fileHandle = null;
87 /**
88 * Flag specifying whether or not to skip schema (e.g. SQL-only) updates.
90 * @var bool
92 protected $skipSchema = false;
94 /**
95 * Hold the value of $wgContentHandlerUseDB during the upgrade.
97 protected $holdContentHandlerUseDB = true;
99 /**
100 * Constructor
102 * @param DatabaseBase $db To perform updates on
103 * @param bool $shared Whether to perform updates on shared tables
104 * @param Maintenance $maintenance Maintenance object which created us
106 protected function __construct( DatabaseBase &$db, $shared, Maintenance $maintenance = null ) {
107 $this->db = $db;
108 $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
109 $this->shared = $shared;
110 if ( $maintenance ) {
111 $this->maintenance = $maintenance;
112 $this->fileHandle = $maintenance->fileHandle;
113 } else {
114 $this->maintenance = new FakeMaintenance;
116 $this->maintenance->setDB( $db );
117 $this->initOldGlobals();
118 $this->loadExtensions();
119 Hooks::run( 'LoadExtensionSchemaUpdates', [ $this ] );
123 * Initialize all of the old globals. One day this should all become
124 * something much nicer
126 private function initOldGlobals() {
127 global $wgExtNewTables, $wgExtNewFields, $wgExtPGNewFields,
128 $wgExtPGAlteredFields, $wgExtNewIndexes, $wgExtModifiedFields;
130 # For extensions only, should be populated via hooks
131 # $wgDBtype should be checked to specifiy the proper file
132 $wgExtNewTables = []; // table, dir
133 $wgExtNewFields = []; // table, column, dir
134 $wgExtPGNewFields = []; // table, column, column attributes; for PostgreSQL
135 $wgExtPGAlteredFields = []; // table, column, new type, conversion method; for PostgreSQL
136 $wgExtNewIndexes = []; // table, index, dir
137 $wgExtModifiedFields = []; // table, index, dir
141 * Loads LocalSettings.php, if needed, and initialises everything needed for
142 * LoadExtensionSchemaUpdates hook.
144 private function loadExtensions() {
145 if ( !defined( 'MEDIAWIKI_INSTALL' ) ) {
146 return; // already loaded
148 $vars = Installer::getExistingLocalSettings();
150 $registry = ExtensionRegistry::getInstance();
151 $queue = $registry->getQueue();
152 // Don't accidentally load extensions in the future
153 $registry->clearQueue();
155 // This will automatically add "AutoloadClasses" to $wgAutoloadClasses
156 $data = $registry->readFromQueue( $queue );
157 $hooks = [ 'wgHooks' => [ 'LoadExtensionSchemaUpdates' => [] ] ];
158 if ( isset( $data['globals']['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
159 $hooks = $data['globals']['wgHooks']['LoadExtensionSchemaUpdates'];
161 if ( $vars && isset( $vars['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
162 $hooks = array_merge_recursive( $hooks, $vars['wgHooks']['LoadExtensionSchemaUpdates'] );
164 global $wgHooks, $wgAutoloadClasses;
165 $wgHooks['LoadExtensionSchemaUpdates'] = $hooks;
166 if ( $vars && isset( $vars['wgAutoloadClasses'] ) ) {
167 $wgAutoloadClasses += $vars['wgAutoloadClasses'];
172 * @param DatabaseBase $db
173 * @param bool $shared
174 * @param Maintenance $maintenance
176 * @throws MWException
177 * @return DatabaseUpdater
179 public static function newForDB( &$db, $shared = false, $maintenance = null ) {
180 $type = $db->getType();
181 if ( in_array( $type, Installer::getDBTypes() ) ) {
182 $class = ucfirst( $type ) . 'Updater';
184 return new $class( $db, $shared, $maintenance );
185 } else {
186 throw new MWException( __METHOD__ . ' called for unsupported $wgDBtype' );
191 * Get a database connection to run updates
193 * @return DatabaseBase
195 public function getDB() {
196 return $this->db;
200 * Output some text. If we're running from web, escape the text first.
202 * @param string $str Text to output
204 public function output( $str ) {
205 if ( $this->maintenance->isQuiet() ) {
206 return;
208 global $wgCommandLineMode;
209 if ( !$wgCommandLineMode ) {
210 $str = htmlspecialchars( $str );
212 echo $str;
213 flush();
217 * Add a new update coming from an extension. This should be called by
218 * extensions while executing the LoadExtensionSchemaUpdates hook.
220 * @since 1.17
222 * @param array $update The update to run. Format is the following:
223 * first item is the callback function, it also can be a
224 * simple string with the name of a function in this class,
225 * following elements are parameters to the function.
226 * Note that callback functions will receive this object as
227 * first parameter.
229 public function addExtensionUpdate( array $update ) {
230 $this->extensionUpdates[] = $update;
234 * Convenience wrapper for addExtensionUpdate() when adding a new table (which
235 * is the most common usage of updaters in an extension)
237 * @since 1.18
239 * @param string $tableName Name of table to create
240 * @param string $sqlPath Full path to the schema file
242 public function addExtensionTable( $tableName, $sqlPath ) {
243 $this->extensionUpdates[] = [ 'addTable', $tableName, $sqlPath, true ];
247 * @since 1.19
249 * @param string $tableName
250 * @param string $indexName
251 * @param string $sqlPath
253 public function addExtensionIndex( $tableName, $indexName, $sqlPath ) {
254 $this->extensionUpdates[] = [ 'addIndex', $tableName, $indexName, $sqlPath, true ];
259 * @since 1.19
261 * @param string $tableName
262 * @param string $columnName
263 * @param string $sqlPath
265 public function addExtensionField( $tableName, $columnName, $sqlPath ) {
266 $this->extensionUpdates[] = [ 'addField', $tableName, $columnName, $sqlPath, true ];
271 * @since 1.20
273 * @param string $tableName
274 * @param string $columnName
275 * @param string $sqlPath
277 public function dropExtensionField( $tableName, $columnName, $sqlPath ) {
278 $this->extensionUpdates[] = [ 'dropField', $tableName, $columnName, $sqlPath, true ];
282 * Drop an index from an extension table
284 * @since 1.21
286 * @param string $tableName The table name
287 * @param string $indexName The index name
288 * @param string $sqlPath The path to the SQL change path
290 public function dropExtensionIndex( $tableName, $indexName, $sqlPath ) {
291 $this->extensionUpdates[] = [ 'dropIndex', $tableName, $indexName, $sqlPath, true ];
296 * @since 1.20
298 * @param string $tableName
299 * @param string $sqlPath
301 public function dropExtensionTable( $tableName, $sqlPath ) {
302 $this->extensionUpdates[] = [ 'dropTable', $tableName, $sqlPath, true ];
306 * Rename an index on an extension table
308 * @since 1.21
310 * @param string $tableName The table name
311 * @param string $oldIndexName The old index name
312 * @param string $newIndexName The new index name
313 * @param string $sqlPath The path to the SQL change path
314 * @param bool $skipBothIndexExistWarning Whether to warn if both the old
315 * and the new indexes exist. [facultative; by default, false]
317 public function renameExtensionIndex( $tableName, $oldIndexName, $newIndexName,
318 $sqlPath, $skipBothIndexExistWarning = false
320 $this->extensionUpdates[] = [
321 'renameIndex',
322 $tableName,
323 $oldIndexName,
324 $newIndexName,
325 $skipBothIndexExistWarning,
326 $sqlPath,
327 true
332 * @since 1.21
334 * @param string $tableName The table name
335 * @param string $fieldName The field to be modified
336 * @param string $sqlPath The path to the SQL change path
338 public function modifyExtensionField( $tableName, $fieldName, $sqlPath ) {
339 $this->extensionUpdates[] = [ 'modifyField', $tableName, $fieldName, $sqlPath, true ];
344 * @since 1.20
346 * @param string $tableName
347 * @return bool
349 public function tableExists( $tableName ) {
350 return ( $this->db->tableExists( $tableName, __METHOD__ ) );
354 * Add a maintenance script to be run after the database updates are complete.
356 * Script should subclass LoggedUpdateMaintenance
358 * @since 1.19
360 * @param string $class Name of a Maintenance subclass
362 public function addPostDatabaseUpdateMaintenance( $class ) {
363 $this->postDatabaseUpdateMaintenance[] = $class;
367 * Get the list of extension-defined updates
369 * @return array
371 protected function getExtensionUpdates() {
372 return $this->extensionUpdates;
376 * @since 1.17
378 * @return string[]
380 public function getPostDatabaseUpdateMaintenance() {
381 return $this->postDatabaseUpdateMaintenance;
385 * @since 1.21
387 * Writes the schema updates desired to a file for the DB Admin to run.
388 * @param array $schemaUpdate
390 private function writeSchemaUpdateFile( $schemaUpdate = [] ) {
391 $updates = $this->updatesSkipped;
392 $this->updatesSkipped = [];
394 foreach ( $updates as $funcList ) {
395 $func = $funcList[0];
396 $arg = $funcList[1];
397 $origParams = $funcList[2];
398 call_user_func_array( $func, $arg );
399 flush();
400 $this->updatesSkipped[] = $origParams;
405 * Do all the updates
407 * @param array $what What updates to perform
409 public function doUpdates( $what = [ 'core', 'extensions', 'stats' ] ) {
410 global $wgVersion;
412 $this->db->begin( __METHOD__ );
413 $what = array_flip( $what );
414 $this->skipSchema = isset( $what['noschema'] ) || $this->fileHandle !== null;
415 if ( isset( $what['core'] ) ) {
416 $this->runUpdates( $this->getCoreUpdateList(), false );
418 if ( isset( $what['extensions'] ) ) {
419 $this->runUpdates( $this->getOldGlobalUpdates(), false );
420 $this->runUpdates( $this->getExtensionUpdates(), true );
423 if ( isset( $what['stats'] ) ) {
424 $this->checkStats();
427 $this->setAppliedUpdates( $wgVersion, $this->updates );
429 if ( $this->fileHandle ) {
430 $this->skipSchema = false;
431 $this->writeSchemaUpdateFile();
432 $this->setAppliedUpdates( "$wgVersion-schema", $this->updatesSkipped );
435 $this->db->commit( __METHOD__ );
439 * Helper function for doUpdates()
441 * @param array $updates Array of updates to run
442 * @param bool $passSelf Whether to pass this object we calling external functions
444 private function runUpdates( array $updates, $passSelf ) {
445 $updatesDone = [];
446 $updatesSkipped = [];
447 foreach ( $updates as $params ) {
448 $origParams = $params;
449 $func = array_shift( $params );
450 if ( !is_array( $func ) && method_exists( $this, $func ) ) {
451 $func = [ $this, $func ];
452 } elseif ( $passSelf ) {
453 array_unshift( $params, $this );
455 $ret = call_user_func_array( $func, $params );
456 flush();
457 if ( $ret !== false ) {
458 $updatesDone[] = $origParams;
459 wfGetLBFactory()->waitForReplication();
460 } else {
461 $updatesSkipped[] = [ $func, $params, $origParams ];
464 $this->updatesSkipped = array_merge( $this->updatesSkipped, $updatesSkipped );
465 $this->updates = array_merge( $this->updates, $updatesDone );
469 * @param string $version
470 * @param array $updates
472 protected function setAppliedUpdates( $version, $updates = [] ) {
473 $this->db->clearFlag( DBO_DDLMODE );
474 if ( !$this->canUseNewUpdatelog() ) {
475 return;
477 $key = "updatelist-$version-" . time() . self::$updateCounter;
478 self::$updateCounter++;
479 $this->db->insert( 'updatelog',
480 [ 'ul_key' => $key, 'ul_value' => serialize( $updates ) ],
481 __METHOD__ );
482 $this->db->setFlag( DBO_DDLMODE );
486 * Helper function: check if the given key is present in the updatelog table.
487 * Obviously, only use this for updates that occur after the updatelog table was
488 * created!
489 * @param string $key Name of the key to check for
490 * @return bool
492 public function updateRowExists( $key ) {
493 $row = $this->db->selectRow(
494 'updatelog',
495 # Bug 65813
496 '1 AS X',
497 [ 'ul_key' => $key ],
498 __METHOD__
501 return (bool)$row;
505 * Helper function: Add a key to the updatelog table
506 * Obviously, only use this for updates that occur after the updatelog table was
507 * created!
508 * @param string $key Name of key to insert
509 * @param string $val [optional] Value to insert along with the key
511 public function insertUpdateRow( $key, $val = null ) {
512 $this->db->clearFlag( DBO_DDLMODE );
513 $values = [ 'ul_key' => $key ];
514 if ( $val && $this->canUseNewUpdatelog() ) {
515 $values['ul_value'] = $val;
517 $this->db->insert( 'updatelog', $values, __METHOD__, 'IGNORE' );
518 $this->db->setFlag( DBO_DDLMODE );
522 * Updatelog was changed in 1.17 to have a ul_value column so we can record
523 * more information about what kind of updates we've done (that's what this
524 * class does). Pre-1.17 wikis won't have this column, and really old wikis
525 * might not even have updatelog at all
527 * @return bool
529 protected function canUseNewUpdatelog() {
530 return $this->db->tableExists( 'updatelog', __METHOD__ ) &&
531 $this->db->fieldExists( 'updatelog', 'ul_value', __METHOD__ );
535 * Returns whether updates should be executed on the database table $name.
536 * Updates will be prevented if the table is a shared table and it is not
537 * specified to run updates on shared tables.
539 * @param string $name Table name
540 * @return bool
542 protected function doTable( $name ) {
543 global $wgSharedDB, $wgSharedTables;
545 // Don't bother to check $wgSharedTables if there isn't a shared database
546 // or the user actually also wants to do updates on the shared database.
547 if ( $wgSharedDB === null || $this->shared ) {
548 return true;
551 if ( in_array( $name, $wgSharedTables ) ) {
552 $this->output( "...skipping update to shared table $name.\n" );
553 return false;
554 } else {
555 return true;
560 * Before 1.17, we used to handle updates via stuff like
561 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
562 * of this in 1.17 but we want to remain back-compatible for a while. So
563 * load up these old global-based things into our update list.
565 * @return array
567 protected function getOldGlobalUpdates() {
568 global $wgExtNewFields, $wgExtNewTables, $wgExtModifiedFields,
569 $wgExtNewIndexes;
571 $updates = [];
573 foreach ( $wgExtNewTables as $tableRecord ) {
574 $updates[] = [
575 'addTable', $tableRecord[0], $tableRecord[1], true
579 foreach ( $wgExtNewFields as $fieldRecord ) {
580 $updates[] = [
581 'addField', $fieldRecord[0], $fieldRecord[1],
582 $fieldRecord[2], true
586 foreach ( $wgExtNewIndexes as $fieldRecord ) {
587 $updates[] = [
588 'addIndex', $fieldRecord[0], $fieldRecord[1],
589 $fieldRecord[2], true
593 foreach ( $wgExtModifiedFields as $fieldRecord ) {
594 $updates[] = [
595 'modifyField', $fieldRecord[0], $fieldRecord[1],
596 $fieldRecord[2], true
600 return $updates;
604 * Get an array of updates to perform on the database. Should return a
605 * multi-dimensional array. The main key is the MediaWiki version (1.12,
606 * 1.13...) with the values being arrays of updates, identical to how
607 * updaters.inc did it (for now)
609 * @return array
611 abstract protected function getCoreUpdateList();
614 * Append an SQL fragment to the open file handle.
616 * @param string $filename File name to open
618 public function copyFile( $filename ) {
619 $this->db->sourceFile( $filename, false, false, false,
620 [ $this, 'appendLine' ]
625 * Append a line to the open filehandle. The line is assumed to
626 * be a complete SQL statement.
628 * This is used as a callback for sourceLine().
630 * @param string $line Text to append to the file
631 * @return bool False to skip actually executing the file
632 * @throws MWException
634 public function appendLine( $line ) {
635 $line = rtrim( $line ) . ";\n";
636 if ( fwrite( $this->fileHandle, $line ) === false ) {
637 throw new MWException( "trouble writing file" );
640 return false;
644 * Applies a SQL patch
646 * @param string $path Path to the patch file
647 * @param bool $isFullPath Whether to treat $path as a relative or not
648 * @param string $msg Description of the patch
649 * @return bool False if patch is skipped.
651 protected function applyPatch( $path, $isFullPath = false, $msg = null ) {
652 if ( $msg === null ) {
653 $msg = "Applying $path patch";
655 if ( $this->skipSchema ) {
656 $this->output( "...skipping schema change ($msg).\n" );
658 return false;
661 $this->output( "$msg ..." );
663 if ( !$isFullPath ) {
664 $path = $this->db->patchPath( $path );
666 if ( $this->fileHandle !== null ) {
667 $this->copyFile( $path );
668 } else {
669 $this->db->sourceFile( $path );
671 $this->output( "done.\n" );
673 return true;
677 * Add a new table to the database
679 * @param string $name Name of the new table
680 * @param string $patch Path to the patch file
681 * @param bool $fullpath Whether to treat $patch path as a relative or not
682 * @return bool False if this was skipped because schema changes are skipped
684 protected function addTable( $name, $patch, $fullpath = false ) {
685 if ( !$this->doTable( $name ) ) {
686 return true;
689 if ( $this->db->tableExists( $name, __METHOD__ ) ) {
690 $this->output( "...$name table already exists.\n" );
691 } else {
692 return $this->applyPatch( $patch, $fullpath, "Creating $name table" );
695 return true;
699 * Add a new field to an existing table
701 * @param string $table Name of the table to modify
702 * @param string $field Name of the new field
703 * @param string $patch Path to the patch file
704 * @param bool $fullpath Whether to treat $patch path as a relative or not
705 * @return bool False if this was skipped because schema changes are skipped
707 protected function addField( $table, $field, $patch, $fullpath = false ) {
708 if ( !$this->doTable( $table ) ) {
709 return true;
712 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
713 $this->output( "...$table table does not exist, skipping new field patch.\n" );
714 } elseif ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
715 $this->output( "...have $field field in $table table.\n" );
716 } else {
717 return $this->applyPatch( $patch, $fullpath, "Adding $field field to table $table" );
720 return true;
724 * Add a new index to an existing table
726 * @param string $table Name of the table to modify
727 * @param string $index Name of the new index
728 * @param string $patch Path to the patch file
729 * @param bool $fullpath Whether to treat $patch path as a relative or not
730 * @return bool False if this was skipped because schema changes are skipped
732 protected function addIndex( $table, $index, $patch, $fullpath = false ) {
733 if ( !$this->doTable( $table ) ) {
734 return true;
737 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
738 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
739 } elseif ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
740 $this->output( "...index $index already set on $table table.\n" );
741 } else {
742 return $this->applyPatch( $patch, $fullpath, "Adding index $index to table $table" );
745 return true;
749 * Drop a field from an existing table
751 * @param string $table Name of the table to modify
752 * @param string $field Name of the old field
753 * @param string $patch Path to the patch file
754 * @param bool $fullpath Whether to treat $patch path as a relative or not
755 * @return bool False if this was skipped because schema changes are skipped
757 protected function dropField( $table, $field, $patch, $fullpath = false ) {
758 if ( !$this->doTable( $table ) ) {
759 return true;
762 if ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
763 return $this->applyPatch( $patch, $fullpath, "Table $table contains $field field. Dropping" );
764 } else {
765 $this->output( "...$table table does not contain $field field.\n" );
768 return true;
772 * Drop an index from an existing table
774 * @param string $table Name of the table to modify
775 * @param string $index Name of the index
776 * @param string $patch Path to the patch file
777 * @param bool $fullpath Whether to treat $patch path as a relative or not
778 * @return bool False if this was skipped because schema changes are skipped
780 protected function dropIndex( $table, $index, $patch, $fullpath = false ) {
781 if ( !$this->doTable( $table ) ) {
782 return true;
785 if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
786 return $this->applyPatch( $patch, $fullpath, "Dropping $index index from table $table" );
787 } else {
788 $this->output( "...$index key doesn't exist.\n" );
791 return true;
795 * Rename an index from an existing table
797 * @param string $table Name of the table to modify
798 * @param string $oldIndex Old name of the index
799 * @param string $newIndex New name of the index
800 * @param bool $skipBothIndexExistWarning Whether to warn if both the
801 * old and the new indexes exist.
802 * @param string $patch Path to the patch file
803 * @param bool $fullpath Whether to treat $patch path as a relative or not
804 * @return bool False if this was skipped because schema changes are skipped
806 protected function renameIndex( $table, $oldIndex, $newIndex,
807 $skipBothIndexExistWarning, $patch, $fullpath = false
809 if ( !$this->doTable( $table ) ) {
810 return true;
813 // First requirement: the table must exist
814 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
815 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
817 return true;
820 // Second requirement: the new index must be missing
821 if ( $this->db->indexExists( $table, $newIndex, __METHOD__ ) ) {
822 $this->output( "...index $newIndex already set on $table table.\n" );
823 if ( !$skipBothIndexExistWarning &&
824 $this->db->indexExists( $table, $oldIndex, __METHOD__ )
826 $this->output( "...WARNING: $oldIndex still exists, despite it has " .
827 "been renamed into $newIndex (which also exists).\n" .
828 " $oldIndex should be manually removed if not needed anymore.\n" );
831 return true;
834 // Third requirement: the old index must exist
835 if ( !$this->db->indexExists( $table, $oldIndex, __METHOD__ ) ) {
836 $this->output( "...skipping: index $oldIndex doesn't exist.\n" );
838 return true;
841 // Requirements have been satisfied, patch can be applied
842 return $this->applyPatch(
843 $patch,
844 $fullpath,
845 "Renaming index $oldIndex into $newIndex to table $table"
850 * If the specified table exists, drop it, or execute the
851 * patch if one is provided.
853 * Public @since 1.20
855 * @param string $table Table to drop.
856 * @param string|bool $patch String of patch file that will drop the table. Default: false.
857 * @param bool $fullpath Whether $patch is a full path. Default: false.
858 * @return bool False if this was skipped because schema changes are skipped
860 public function dropTable( $table, $patch = false, $fullpath = false ) {
861 if ( !$this->doTable( $table ) ) {
862 return true;
865 if ( $this->db->tableExists( $table, __METHOD__ ) ) {
866 $msg = "Dropping table $table";
868 if ( $patch === false ) {
869 $this->output( "$msg ..." );
870 $this->db->dropTable( $table, __METHOD__ );
871 $this->output( "done.\n" );
872 } else {
873 return $this->applyPatch( $patch, $fullpath, $msg );
875 } else {
876 $this->output( "...$table doesn't exist.\n" );
879 return true;
883 * Modify an existing field
885 * @param string $table Name of the table to which the field belongs
886 * @param string $field Name of the field to modify
887 * @param string $patch Path to the patch file
888 * @param bool $fullpath Whether to treat $patch path as a relative or not
889 * @return bool False if this was skipped because schema changes are skipped
891 public function modifyField( $table, $field, $patch, $fullpath = false ) {
892 if ( !$this->doTable( $table ) ) {
893 return true;
896 $updateKey = "$table-$field-$patch";
897 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
898 $this->output( "...$table table does not exist, skipping modify field patch.\n" );
899 } elseif ( !$this->db->fieldExists( $table, $field, __METHOD__ ) ) {
900 $this->output( "...$field field does not exist in $table table, " .
901 "skipping modify field patch.\n" );
902 } elseif ( $this->updateRowExists( $updateKey ) ) {
903 $this->output( "...$field in table $table already modified by patch $patch.\n" );
904 } else {
905 $this->insertUpdateRow( $updateKey );
907 return $this->applyPatch( $patch, $fullpath, "Modifying $field field of table $table" );
910 return true;
914 * Set any .htaccess files or equivilent for storage repos
916 * Some zones (e.g. "temp") used to be public and may have been initialized as such
918 public function setFileAccess() {
919 $repo = RepoGroup::singleton()->getLocalRepo();
920 $zonePath = $repo->getZonePath( 'temp' );
921 if ( $repo->getBackend()->directoryExists( [ 'dir' => $zonePath ] ) ) {
922 // If the directory was never made, then it will have the right ACLs when it is made
923 $status = $repo->getBackend()->secure( [
924 'dir' => $zonePath,
925 'noAccess' => true,
926 'noListing' => true
927 ] );
928 if ( $status->isOK() ) {
929 $this->output( "Set the local repo temp zone container to be private.\n" );
930 } else {
931 $this->output( "Failed to set the local repo temp zone container to be private.\n" );
937 * Purge the objectcache table
939 public function purgeCache() {
940 global $wgLocalisationCacheConf;
941 # We can't guarantee that the user will be able to use TRUNCATE,
942 # but we know that DELETE is available to us
943 $this->output( "Purging caches..." );
944 $this->db->delete( 'objectcache', '*', __METHOD__ );
945 if ( $wgLocalisationCacheConf['manualRecache'] ) {
946 $this->rebuildLocalisationCache();
948 $blobStore = new MessageBlobStore();
949 $blobStore->clear();
950 $this->db->delete( 'module_deps', '*', __METHOD__ );
951 $this->output( "done.\n" );
955 * Check the site_stats table is not properly populated.
957 protected function checkStats() {
958 $this->output( "...site_stats is populated..." );
959 $row = $this->db->selectRow( 'site_stats', '*', [ 'ss_row_id' => 1 ], __METHOD__ );
960 if ( $row === false ) {
961 $this->output( "data is missing! rebuilding...\n" );
962 } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
963 $this->output( "missing ss_total_pages, rebuilding...\n" );
964 } else {
965 $this->output( "done.\n" );
967 return;
969 SiteStatsInit::doAllAndCommit( $this->db );
972 # Common updater functions
975 * Sets the number of active users in the site_stats table
977 protected function doActiveUsersInit() {
978 $activeUsers = $this->db->selectField( 'site_stats', 'ss_active_users', false, __METHOD__ );
979 if ( $activeUsers == -1 ) {
980 $activeUsers = $this->db->selectField( 'recentchanges',
981 'COUNT( DISTINCT rc_user_text )',
982 [ 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ], __METHOD__
984 $this->db->update( 'site_stats',
985 [ 'ss_active_users' => intval( $activeUsers ) ],
986 [ 'ss_row_id' => 1 ], __METHOD__, [ 'LIMIT' => 1 ]
989 $this->output( "...ss_active_users user count set...\n" );
993 * Populates the log_user_text field in the logging table
995 protected function doLogUsertextPopulation() {
996 if ( !$this->updateRowExists( 'populate log_usertext' ) ) {
997 $this->output(
998 "Populating log_user_text field, printing progress markers. For large\n" .
999 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1000 "maintenance/populateLogUsertext.php.\n"
1003 $task = $this->maintenance->runChild( 'PopulateLogUsertext' );
1004 $task->execute();
1005 $this->output( "done.\n" );
1010 * Migrate log params to new table and index for searching
1012 protected function doLogSearchPopulation() {
1013 if ( !$this->updateRowExists( 'populate log_search' ) ) {
1014 $this->output(
1015 "Populating log_search table, printing progress markers. For large\n" .
1016 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1017 "maintenance/populateLogSearch.php.\n" );
1019 $task = $this->maintenance->runChild( 'PopulateLogSearch' );
1020 $task->execute();
1021 $this->output( "done.\n" );
1026 * Updates the timestamps in the transcache table
1027 * @return bool
1029 protected function doUpdateTranscacheField() {
1030 if ( $this->updateRowExists( 'convert transcache field' ) ) {
1031 $this->output( "...transcache tc_time already converted.\n" );
1033 return true;
1036 return $this->applyPatch( 'patch-tc-timestamp.sql', false,
1037 "Converting tc_time from UNIX epoch to MediaWiki timestamp" );
1041 * Update CategoryLinks collation
1043 protected function doCollationUpdate() {
1044 global $wgCategoryCollation;
1045 if ( $this->db->fieldExists( 'categorylinks', 'cl_collation', __METHOD__ ) ) {
1046 if ( $this->db->selectField(
1047 'categorylinks',
1048 'COUNT(*)',
1049 'cl_collation != ' . $this->db->addQuotes( $wgCategoryCollation ),
1050 __METHOD__
1051 ) == 0
1053 $this->output( "...collations up-to-date.\n" );
1055 return;
1058 $this->output( "Updating category collations..." );
1059 $task = $this->maintenance->runChild( 'UpdateCollation' );
1060 $task->execute();
1061 $this->output( "...done.\n" );
1066 * Migrates user options from the user table blob to user_properties
1068 protected function doMigrateUserOptions() {
1069 if ( $this->db->tableExists( 'user_properties' ) ) {
1070 $cl = $this->maintenance->runChild( 'ConvertUserOptions', 'convertUserOptions.php' );
1071 $cl->execute();
1072 $this->output( "done.\n" );
1077 * Enable profiling table when it's turned on
1079 protected function doEnableProfiling() {
1080 global $wgProfiler;
1082 if ( !$this->doTable( 'profiling' ) ) {
1083 return true;
1086 $profileToDb = false;
1087 if ( isset( $wgProfiler['output'] ) ) {
1088 $out = $wgProfiler['output'];
1089 if ( $out === 'db' ) {
1090 $profileToDb = true;
1091 } elseif ( is_array( $out ) && in_array( 'db', $out ) ) {
1092 $profileToDb = true;
1096 if ( $profileToDb && !$this->db->tableExists( 'profiling', __METHOD__ ) ) {
1097 $this->applyPatch( 'patch-profiling.sql', false, 'Add profiling table' );
1102 * Rebuilds the localisation cache
1104 protected function rebuildLocalisationCache() {
1106 * @var $cl RebuildLocalisationCache
1108 $cl = $this->maintenance->runChild( 'RebuildLocalisationCache', 'rebuildLocalisationCache.php' );
1109 $this->output( "Rebuilding localisation cache...\n" );
1110 $cl->setForce();
1111 $cl->execute();
1112 $this->output( "done.\n" );
1116 * Turns off content handler fields during parts of the upgrade
1117 * where they aren't available.
1119 protected function disableContentHandlerUseDB() {
1120 global $wgContentHandlerUseDB;
1122 if ( $wgContentHandlerUseDB ) {
1123 $this->output( "Turning off Content Handler DB fields for this part of upgrade.\n" );
1124 $this->holdContentHandlerUseDB = $wgContentHandlerUseDB;
1125 $wgContentHandlerUseDB = false;
1130 * Turns content handler fields back on.
1132 protected function enableContentHandlerUseDB() {
1133 global $wgContentHandlerUseDB;
1135 if ( $this->holdContentHandlerUseDB ) {
1136 $this->output( "Content Handler DB fields should be usable now.\n" );
1137 $wgContentHandlerUseDB = $this->holdContentHandlerUseDB;