Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / installer / DatabaseUpdater.php
blobb676f45ccf00795400dc8a972a880b82bd525371
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 = array();
43 /**
44 * Array of updates that were skipped
46 * @var array
48 protected $updatesSkipped = array();
50 /**
51 * List of extension-provided database updates
52 * @var array
54 protected $extensionUpdates = array();
56 /**
57 * Handle to the database subclass
59 * @var DatabaseBase
61 protected $db;
63 protected $shared = false;
65 /**
66 * Scripts to run after database update
67 * Should be a subclass of LoggedUpdateMaintenance
69 protected $postDatabaseUpdateMaintenance = array(
70 'DeleteDefaultMessages',
71 'PopulateRevisionLength',
72 'PopulateRevisionSha1',
73 'PopulateImageSha1',
74 'FixExtLinksProtocolRelative',
75 'PopulateFilearchiveSha1',
76 'PopulateBacklinkNamespace'
79 /**
80 * File handle for SQL output.
82 * @var resource
84 protected $fileHandle = null;
86 /**
87 * Flag specifying whether or not to skip schema (e.g. SQL-only) updates.
89 * @var bool
91 protected $skipSchema = false;
93 /**
94 * Hold the value of $wgContentHandlerUseDB during the upgrade.
96 protected $holdContentHandlerUseDB = true;
98 /**
99 * Constructor
101 * @param DatabaseBase $db To perform updates on
102 * @param bool $shared Whether to perform updates on shared tables
103 * @param Maintenance $maintenance Maintenance object which created us
105 protected function __construct( DatabaseBase &$db, $shared, Maintenance $maintenance = null ) {
106 $this->db = $db;
107 $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
108 $this->shared = $shared;
109 if ( $maintenance ) {
110 $this->maintenance = $maintenance;
111 $this->fileHandle = $maintenance->fileHandle;
112 } else {
113 $this->maintenance = new FakeMaintenance;
115 $this->maintenance->setDB( $db );
116 $this->initOldGlobals();
117 $this->loadExtensions();
118 Hooks::run( 'LoadExtensionSchemaUpdates', array( $this ) );
122 * Initialize all of the old globals. One day this should all become
123 * something much nicer
125 private function initOldGlobals() {
126 global $wgExtNewTables, $wgExtNewFields, $wgExtPGNewFields,
127 $wgExtPGAlteredFields, $wgExtNewIndexes, $wgExtModifiedFields;
129 # For extensions only, should be populated via hooks
130 # $wgDBtype should be checked to specifiy the proper file
131 $wgExtNewTables = array(); // table, dir
132 $wgExtNewFields = array(); // table, column, dir
133 $wgExtPGNewFields = array(); // table, column, column attributes; for PostgreSQL
134 $wgExtPGAlteredFields = array(); // table, column, new type, conversion method; for PostgreSQL
135 $wgExtNewIndexes = array(); // table, index, dir
136 $wgExtModifiedFields = array(); // table, index, dir
140 * Loads LocalSettings.php, if needed, and initialises everything needed for
141 * LoadExtensionSchemaUpdates hook.
143 private function loadExtensions() {
144 if ( !defined( 'MEDIAWIKI_INSTALL' ) ) {
145 return; // already loaded
147 $vars = Installer::getExistingLocalSettings();
148 if ( !$vars ) {
149 return; // no LocalSettings found
151 if ( !isset( $vars['wgHooks'] ) || !isset( $vars['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
152 return;
154 global $wgHooks, $wgAutoloadClasses;
155 $wgHooks['LoadExtensionSchemaUpdates'] = $vars['wgHooks']['LoadExtensionSchemaUpdates'];
156 $wgAutoloadClasses = $wgAutoloadClasses + $vars['wgAutoloadClasses'];
160 * @param DatabaseBase $db
161 * @param bool $shared
162 * @param Maintenance $maintenance
164 * @throws MWException
165 * @return DatabaseUpdater
167 public static function newForDB( &$db, $shared = false, $maintenance = null ) {
168 $type = $db->getType();
169 if ( in_array( $type, Installer::getDBTypes() ) ) {
170 $class = ucfirst( $type ) . 'Updater';
172 return new $class( $db, $shared, $maintenance );
173 } else {
174 throw new MWException( __METHOD__ . ' called for unsupported $wgDBtype' );
179 * Get a database connection to run updates
181 * @return DatabaseBase
183 public function getDB() {
184 return $this->db;
188 * Output some text. If we're running from web, escape the text first.
190 * @param string $str Text to output
192 public function output( $str ) {
193 if ( $this->maintenance->isQuiet() ) {
194 return;
196 global $wgCommandLineMode;
197 if ( !$wgCommandLineMode ) {
198 $str = htmlspecialchars( $str );
200 echo $str;
201 flush();
205 * Add a new update coming from an extension. This should be called by
206 * extensions while executing the LoadExtensionSchemaUpdates hook.
208 * @since 1.17
210 * @param array $update The update to run. Format is the following:
211 * first item is the callback function, it also can be a
212 * simple string with the name of a function in this class,
213 * following elements are parameters to the function.
214 * Note that callback functions will receive this object as
215 * first parameter.
217 public function addExtensionUpdate( array $update ) {
218 $this->extensionUpdates[] = $update;
222 * Convenience wrapper for addExtensionUpdate() when adding a new table (which
223 * is the most common usage of updaters in an extension)
225 * @since 1.18
227 * @param string $tableName Name of table to create
228 * @param string $sqlPath Full path to the schema file
230 public function addExtensionTable( $tableName, $sqlPath ) {
231 $this->extensionUpdates[] = array( 'addTable', $tableName, $sqlPath, true );
235 * @since 1.19
237 * @param string $tableName
238 * @param string $indexName
239 * @param string $sqlPath
241 public function addExtensionIndex( $tableName, $indexName, $sqlPath ) {
242 $this->extensionUpdates[] = array( 'addIndex', $tableName, $indexName, $sqlPath, true );
247 * @since 1.19
249 * @param string $tableName
250 * @param string $columnName
251 * @param string $sqlPath
253 public function addExtensionField( $tableName, $columnName, $sqlPath ) {
254 $this->extensionUpdates[] = array( 'addField', $tableName, $columnName, $sqlPath, true );
259 * @since 1.20
261 * @param string $tableName
262 * @param string $columnName
263 * @param string $sqlPath
265 public function dropExtensionField( $tableName, $columnName, $sqlPath ) {
266 $this->extensionUpdates[] = array( 'dropField', $tableName, $columnName, $sqlPath, true );
270 * Drop an index from an extension table
272 * @since 1.21
274 * @param string $tableName The table name
275 * @param string $indexName The index name
276 * @param string $sqlPath The path to the SQL change path
278 public function dropExtensionIndex( $tableName, $indexName, $sqlPath ) {
279 $this->extensionUpdates[] = array( 'dropIndex', $tableName, $indexName, $sqlPath, true );
284 * @since 1.20
286 * @param string $tableName
287 * @param string $sqlPath
289 public function dropExtensionTable( $tableName, $sqlPath ) {
290 $this->extensionUpdates[] = array( 'dropTable', $tableName, $sqlPath, true );
294 * Rename an index on an extension table
296 * @since 1.21
298 * @param string $tableName The table name
299 * @param string $oldIndexName The old index name
300 * @param string $newIndexName The new index name
301 * @param string $sqlPath The path to the SQL change path
302 * @param bool $skipBothIndexExistWarning Whether to warn if both the old
303 * and the new indexes exist. [facultative; by default, false]
305 public function renameExtensionIndex( $tableName, $oldIndexName, $newIndexName,
306 $sqlPath, $skipBothIndexExistWarning = false
308 $this->extensionUpdates[] = array(
309 'renameIndex',
310 $tableName,
311 $oldIndexName,
312 $newIndexName,
313 $skipBothIndexExistWarning,
314 $sqlPath,
315 true
320 * @since 1.21
322 * @param string $tableName The table name
323 * @param string $fieldName The field to be modified
324 * @param string $sqlPath The path to the SQL change path
326 public function modifyExtensionField( $tableName, $fieldName, $sqlPath ) {
327 $this->extensionUpdates[] = array( 'modifyField', $tableName, $fieldName, $sqlPath, true );
332 * @since 1.20
334 * @param string $tableName
335 * @return bool
337 public function tableExists( $tableName ) {
338 return ( $this->db->tableExists( $tableName, __METHOD__ ) );
342 * Add a maintenance script to be run after the database updates are complete.
344 * Script should subclass LoggedUpdateMaintenance
346 * @since 1.19
348 * @param string $class Name of a Maintenance subclass
350 public function addPostDatabaseUpdateMaintenance( $class ) {
351 $this->postDatabaseUpdateMaintenance[] = $class;
355 * Get the list of extension-defined updates
357 * @return array
359 protected function getExtensionUpdates() {
360 return $this->extensionUpdates;
364 * @since 1.17
366 * @return array
368 public function getPostDatabaseUpdateMaintenance() {
369 return $this->postDatabaseUpdateMaintenance;
373 * @since 1.21
375 * Writes the schema updates desired to a file for the DB Admin to run.
376 * @param array $schemaUpdate
378 private function writeSchemaUpdateFile( $schemaUpdate = array() ) {
379 $updates = $this->updatesSkipped;
380 $this->updatesSkipped = array();
382 foreach ( $updates as $funcList ) {
383 $func = $funcList[0];
384 $arg = $funcList[1];
385 $origParams = $funcList[2];
386 call_user_func_array( $func, $arg );
387 flush();
388 $this->updatesSkipped[] = $origParams;
393 * Do all the updates
395 * @param array $what What updates to perform
397 public function doUpdates( $what = array( 'core', 'extensions', 'stats' ) ) {
398 global $wgVersion;
400 $this->db->begin( __METHOD__ );
401 $what = array_flip( $what );
402 $this->skipSchema = isset( $what['noschema'] ) || $this->fileHandle !== null;
403 if ( isset( $what['core'] ) ) {
404 $this->runUpdates( $this->getCoreUpdateList(), false );
406 if ( isset( $what['extensions'] ) ) {
407 $this->runUpdates( $this->getOldGlobalUpdates(), false );
408 $this->runUpdates( $this->getExtensionUpdates(), true );
411 if ( isset( $what['stats'] ) ) {
412 $this->checkStats();
415 $this->setAppliedUpdates( $wgVersion, $this->updates );
417 if ( $this->fileHandle ) {
418 $this->skipSchema = false;
419 $this->writeSchemaUpdateFile();
420 $this->setAppliedUpdates( "$wgVersion-schema", $this->updatesSkipped );
423 $this->db->commit( __METHOD__ );
427 * Helper function for doUpdates()
429 * @param array $updates Array of updates to run
430 * @param bool $passSelf Whether to pass this object we calling external functions
432 private function runUpdates( array $updates, $passSelf ) {
433 $updatesDone = array();
434 $updatesSkipped = array();
435 foreach ( $updates as $params ) {
436 $origParams = $params;
437 $func = array_shift( $params );
438 if ( !is_array( $func ) && method_exists( $this, $func ) ) {
439 $func = array( $this, $func );
440 } elseif ( $passSelf ) {
441 array_unshift( $params, $this );
443 $ret = call_user_func_array( $func, $params );
444 flush();
445 if ( $ret !== false ) {
446 $updatesDone[] = $origParams;
447 } else {
448 $updatesSkipped[] = array( $func, $params, $origParams );
451 $this->updatesSkipped = array_merge( $this->updatesSkipped, $updatesSkipped );
452 $this->updates = array_merge( $this->updates, $updatesDone );
456 * @param string $version
457 * @param array $updates
459 protected function setAppliedUpdates( $version, $updates = array() ) {
460 $this->db->clearFlag( DBO_DDLMODE );
461 if ( !$this->canUseNewUpdatelog() ) {
462 return;
464 $key = "updatelist-$version-" . time() . self::$updateCounter;
465 self::$updateCounter++;
466 $this->db->insert( 'updatelog',
467 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
468 __METHOD__ );
469 $this->db->setFlag( DBO_DDLMODE );
473 * Helper function: check if the given key is present in the updatelog table.
474 * Obviously, only use this for updates that occur after the updatelog table was
475 * created!
476 * @param string $key Name of the key to check for
477 * @return bool
479 public function updateRowExists( $key ) {
480 $row = $this->db->selectRow(
481 'updatelog',
482 # Bug 65813
483 '1 AS X',
484 array( 'ul_key' => $key ),
485 __METHOD__
488 return (bool)$row;
492 * Helper function: Add a key to the updatelog table
493 * Obviously, only use this for updates that occur after the updatelog table was
494 * created!
495 * @param string $key Name of key to insert
496 * @param string $val [optional] Value to insert along with the key
498 public function insertUpdateRow( $key, $val = null ) {
499 $this->db->clearFlag( DBO_DDLMODE );
500 $values = array( 'ul_key' => $key );
501 if ( $val && $this->canUseNewUpdatelog() ) {
502 $values['ul_value'] = $val;
504 $this->db->insert( 'updatelog', $values, __METHOD__, 'IGNORE' );
505 $this->db->setFlag( DBO_DDLMODE );
509 * Updatelog was changed in 1.17 to have a ul_value column so we can record
510 * more information about what kind of updates we've done (that's what this
511 * class does). Pre-1.17 wikis won't have this column, and really old wikis
512 * might not even have updatelog at all
514 * @return bool
516 protected function canUseNewUpdatelog() {
517 return $this->db->tableExists( 'updatelog', __METHOD__ ) &&
518 $this->db->fieldExists( 'updatelog', 'ul_value', __METHOD__ );
522 * Returns whether updates should be executed on the database table $name.
523 * Updates will be prevented if the table is a shared table and it is not
524 * specified to run updates on shared tables.
526 * @param string $name Table name
527 * @return bool
529 protected function doTable( $name ) {
530 global $wgSharedDB, $wgSharedTables;
532 // Don't bother to check $wgSharedTables if there isn't a shared database
533 // or the user actually also wants to do updates on the shared database.
534 if ( $wgSharedDB === null || $this->shared ) {
535 return true;
538 if ( in_array( $name, $wgSharedTables ) ) {
539 $this->output( "...skipping update to shared table $name.\n" );
540 return false;
541 } else {
542 return true;
547 * Before 1.17, we used to handle updates via stuff like
548 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
549 * of this in 1.17 but we want to remain back-compatible for a while. So
550 * load up these old global-based things into our update list.
552 * @return array
554 protected function getOldGlobalUpdates() {
555 global $wgExtNewFields, $wgExtNewTables, $wgExtModifiedFields,
556 $wgExtNewIndexes;
558 $updates = array();
560 foreach ( $wgExtNewTables as $tableRecord ) {
561 $updates[] = array(
562 'addTable', $tableRecord[0], $tableRecord[1], true
566 foreach ( $wgExtNewFields as $fieldRecord ) {
567 $updates[] = array(
568 'addField', $fieldRecord[0], $fieldRecord[1],
569 $fieldRecord[2], true
573 foreach ( $wgExtNewIndexes as $fieldRecord ) {
574 $updates[] = array(
575 'addIndex', $fieldRecord[0], $fieldRecord[1],
576 $fieldRecord[2], true
580 foreach ( $wgExtModifiedFields as $fieldRecord ) {
581 $updates[] = array(
582 'modifyField', $fieldRecord[0], $fieldRecord[1],
583 $fieldRecord[2], true
587 return $updates;
591 * Get an array of updates to perform on the database. Should return a
592 * multi-dimensional array. The main key is the MediaWiki version (1.12,
593 * 1.13...) with the values being arrays of updates, identical to how
594 * updaters.inc did it (for now)
596 * @return array
598 abstract protected function getCoreUpdateList();
601 * Append an SQL fragment to the open file handle.
603 * @param string $filename File name to open
605 public function copyFile( $filename ) {
606 $this->db->sourceFile( $filename, false, false, false,
607 array( $this, 'appendLine' )
612 * Append a line to the open filehandle. The line is assumed to
613 * be a complete SQL statement.
615 * This is used as a callback for sourceLine().
617 * @param string $line Text to append to the file
618 * @return bool False to skip actually executing the file
619 * @throws MWException
621 public function appendLine( $line ) {
622 $line = rtrim( $line ) . ";\n";
623 if ( fwrite( $this->fileHandle, $line ) === false ) {
624 throw new MWException( "trouble writing file" );
627 return false;
631 * Applies a SQL patch
633 * @param string $path Path to the patch file
634 * @param bool $isFullPath Whether to treat $path as a relative or not
635 * @param string $msg Description of the patch
636 * @return bool False if patch is skipped.
638 protected function applyPatch( $path, $isFullPath = false, $msg = null ) {
639 if ( $msg === null ) {
640 $msg = "Applying $path patch";
642 if ( $this->skipSchema ) {
643 $this->output( "...skipping schema change ($msg).\n" );
645 return false;
648 $this->output( "$msg ..." );
650 if ( !$isFullPath ) {
651 $path = $this->db->patchPath( $path );
653 if ( $this->fileHandle !== null ) {
654 $this->copyFile( $path );
655 } else {
656 $this->db->sourceFile( $path );
658 $this->output( "done.\n" );
660 return true;
664 * Add a new table to the database
666 * @param string $name Name of the new table
667 * @param string $patch Path to the patch file
668 * @param bool $fullpath Whether to treat $patch path as a relative or not
669 * @return bool False if this was skipped because schema changes are skipped
671 protected function addTable( $name, $patch, $fullpath = false ) {
672 if ( !$this->doTable( $name ) ) {
673 return true;
676 if ( $this->db->tableExists( $name, __METHOD__ ) ) {
677 $this->output( "...$name table already exists.\n" );
678 } else {
679 return $this->applyPatch( $patch, $fullpath, "Creating $name table" );
682 return true;
686 * Add a new field to an existing table
688 * @param string $table Name of the table to modify
689 * @param string $field Name of the new field
690 * @param string $patch Path to the patch file
691 * @param bool $fullpath Whether to treat $patch path as a relative or not
692 * @return bool False if this was skipped because schema changes are skipped
694 protected function addField( $table, $field, $patch, $fullpath = false ) {
695 if ( !$this->doTable( $table ) ) {
696 return true;
699 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
700 $this->output( "...$table table does not exist, skipping new field patch.\n" );
701 } elseif ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
702 $this->output( "...have $field field in $table table.\n" );
703 } else {
704 return $this->applyPatch( $patch, $fullpath, "Adding $field field to table $table" );
707 return true;
711 * Add a new index to an existing table
713 * @param string $table Name of the table to modify
714 * @param string $index Name of the new index
715 * @param string $patch Path to the patch file
716 * @param bool $fullpath Whether to treat $patch path as a relative or not
717 * @return bool False if this was skipped because schema changes are skipped
719 protected function addIndex( $table, $index, $patch, $fullpath = false ) {
720 if ( !$this->doTable( $table ) ) {
721 return true;
724 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
725 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
726 } elseif ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
727 $this->output( "...index $index already set on $table table.\n" );
728 } else {
729 return $this->applyPatch( $patch, $fullpath, "Adding index $index to table $table" );
732 return true;
736 * Drop a field from an existing table
738 * @param string $table Name of the table to modify
739 * @param string $field Name of the old field
740 * @param string $patch Path to the patch file
741 * @param bool $fullpath Whether to treat $patch path as a relative or not
742 * @return bool False if this was skipped because schema changes are skipped
744 protected function dropField( $table, $field, $patch, $fullpath = false ) {
745 if ( !$this->doTable( $table ) ) {
746 return true;
749 if ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
750 return $this->applyPatch( $patch, $fullpath, "Table $table contains $field field. Dropping" );
751 } else {
752 $this->output( "...$table table does not contain $field field.\n" );
755 return true;
759 * Drop an index from an existing table
761 * @param string $table Name of the table to modify
762 * @param string $index Name of the index
763 * @param string $patch Path to the patch file
764 * @param bool $fullpath Whether to treat $patch path as a relative or not
765 * @return bool False if this was skipped because schema changes are skipped
767 protected function dropIndex( $table, $index, $patch, $fullpath = false ) {
768 if ( !$this->doTable( $table ) ) {
769 return true;
772 if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
773 return $this->applyPatch( $patch, $fullpath, "Dropping $index index from table $table" );
774 } else {
775 $this->output( "...$index key doesn't exist.\n" );
778 return true;
782 * Rename an index from an existing table
784 * @param string $table Name of the table to modify
785 * @param string $oldIndex Old name of the index
786 * @param string $newIndex New name of the index
787 * @param bool $skipBothIndexExistWarning Whether to warn if both the
788 * old and the new indexes exist.
789 * @param string $patch Path to the patch file
790 * @param bool $fullpath Whether to treat $patch path as a relative or not
791 * @return bool False if this was skipped because schema changes are skipped
793 protected function renameIndex( $table, $oldIndex, $newIndex,
794 $skipBothIndexExistWarning, $patch, $fullpath = false
796 if ( !$this->doTable( $table ) ) {
797 return true;
800 // First requirement: the table must exist
801 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
802 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
804 return true;
807 // Second requirement: the new index must be missing
808 if ( $this->db->indexExists( $table, $newIndex, __METHOD__ ) ) {
809 $this->output( "...index $newIndex already set on $table table.\n" );
810 if ( !$skipBothIndexExistWarning &&
811 $this->db->indexExists( $table, $oldIndex, __METHOD__ )
813 $this->output( "...WARNING: $oldIndex still exists, despite it has " .
814 "been renamed into $newIndex (which also exists).\n" .
815 " $oldIndex should be manually removed if not needed anymore.\n" );
818 return true;
821 // Third requirement: the old index must exist
822 if ( !$this->db->indexExists( $table, $oldIndex, __METHOD__ ) ) {
823 $this->output( "...skipping: index $oldIndex doesn't exist.\n" );
825 return true;
828 // Requirements have been satisfied, patch can be applied
829 return $this->applyPatch(
830 $patch,
831 $fullpath,
832 "Renaming index $oldIndex into $newIndex to table $table"
837 * If the specified table exists, drop it, or execute the
838 * patch if one is provided.
840 * Public @since 1.20
842 * @param string $table Table to drop.
843 * @param string|bool $patch String of patch file that will drop the table. Default: false.
844 * @param bool $fullpath Whether $patch is a full path. Default: false.
845 * @return bool False if this was skipped because schema changes are skipped
847 public function dropTable( $table, $patch = false, $fullpath = false ) {
848 if ( !$this->doTable( $table ) ) {
849 return true;
852 if ( $this->db->tableExists( $table, __METHOD__ ) ) {
853 $msg = "Dropping table $table";
855 if ( $patch === false ) {
856 $this->output( "$msg ..." );
857 $this->db->dropTable( $table, __METHOD__ );
858 $this->output( "done.\n" );
859 } else {
860 return $this->applyPatch( $patch, $fullpath, $msg );
862 } else {
863 $this->output( "...$table doesn't exist.\n" );
866 return true;
870 * Modify an existing field
872 * @param string $table Name of the table to which the field belongs
873 * @param string $field Name of the field to modify
874 * @param string $patch Path to the patch file
875 * @param bool $fullpath Whether to treat $patch path as a relative or not
876 * @return bool False if this was skipped because schema changes are skipped
878 public function modifyField( $table, $field, $patch, $fullpath = false ) {
879 if ( !$this->doTable( $table ) ) {
880 return true;
883 $updateKey = "$table-$field-$patch";
884 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
885 $this->output( "...$table table does not exist, skipping modify field patch.\n" );
886 } elseif ( !$this->db->fieldExists( $table, $field, __METHOD__ ) ) {
887 $this->output( "...$field field does not exist in $table table, " .
888 "skipping modify field patch.\n" );
889 } elseif ( $this->updateRowExists( $updateKey ) ) {
890 $this->output( "...$field in table $table already modified by patch $patch.\n" );
891 } else {
892 $this->insertUpdateRow( $updateKey );
894 return $this->applyPatch( $patch, $fullpath, "Modifying $field field of table $table" );
897 return true;
901 * Set any .htaccess files or equivilent for storage repos
903 * Some zones (e.g. "temp") used to be public and may have been initialized as such
905 public function setFileAccess() {
906 $repo = RepoGroup::singleton()->getLocalRepo();
907 $zonePath = $repo->getZonePath( 'temp' );
908 if ( $repo->getBackend()->directoryExists( array( 'dir' => $zonePath ) ) ) {
909 // If the directory was never made, then it will have the right ACLs when it is made
910 $status = $repo->getBackend()->secure( array(
911 'dir' => $zonePath,
912 'noAccess' => true,
913 'noListing' => true
914 ) );
915 if ( $status->isOK() ) {
916 $this->output( "Set the local repo temp zone container to be private.\n" );
917 } else {
918 $this->output( "Failed to set the local repo temp zone container to be private.\n" );
924 * Purge the objectcache table
926 public function purgeCache() {
927 global $wgLocalisationCacheConf;
928 # We can't guarantee that the user will be able to use TRUNCATE,
929 # but we know that DELETE is available to us
930 $this->output( "Purging caches..." );
931 $this->db->delete( 'objectcache', '*', __METHOD__ );
932 if ( $wgLocalisationCacheConf['manualRecache'] ) {
933 $this->rebuildLocalisationCache();
935 MessageBlobStore::getInstance()->clear();
936 $this->output( "done.\n" );
940 * Check the site_stats table is not properly populated.
942 protected function checkStats() {
943 $this->output( "...site_stats is populated..." );
944 $row = $this->db->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__ );
945 if ( $row === false ) {
946 $this->output( "data is missing! rebuilding...\n" );
947 } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
948 $this->output( "missing ss_total_pages, rebuilding...\n" );
949 } else {
950 $this->output( "done.\n" );
952 return;
954 SiteStatsInit::doAllAndCommit( $this->db );
957 # Common updater functions
960 * Sets the number of active users in the site_stats table
962 protected function doActiveUsersInit() {
963 $activeUsers = $this->db->selectField( 'site_stats', 'ss_active_users', false, __METHOD__ );
964 if ( $activeUsers == -1 ) {
965 $activeUsers = $this->db->selectField( 'recentchanges',
966 'COUNT( DISTINCT rc_user_text )',
967 array( 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ), __METHOD__
969 $this->db->update( 'site_stats',
970 array( 'ss_active_users' => intval( $activeUsers ) ),
971 array( 'ss_row_id' => 1 ), __METHOD__, array( 'LIMIT' => 1 )
974 $this->output( "...ss_active_users user count set...\n" );
978 * Populates the log_user_text field in the logging table
980 protected function doLogUsertextPopulation() {
981 if ( !$this->updateRowExists( 'populate log_usertext' ) ) {
982 $this->output(
983 "Populating log_user_text field, printing progress markers. For large\n" .
984 "databases, you may want to hit Ctrl-C and do this manually with\n" .
985 "maintenance/populateLogUsertext.php.\n"
988 $task = $this->maintenance->runChild( 'PopulateLogUsertext' );
989 $task->execute();
990 $this->output( "done.\n" );
995 * Migrate log params to new table and index for searching
997 protected function doLogSearchPopulation() {
998 if ( !$this->updateRowExists( 'populate log_search' ) ) {
999 $this->output(
1000 "Populating log_search table, printing progress markers. For large\n" .
1001 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1002 "maintenance/populateLogSearch.php.\n" );
1004 $task = $this->maintenance->runChild( 'PopulateLogSearch' );
1005 $task->execute();
1006 $this->output( "done.\n" );
1011 * Updates the timestamps in the transcache table
1012 * @return bool
1014 protected function doUpdateTranscacheField() {
1015 if ( $this->updateRowExists( 'convert transcache field' ) ) {
1016 $this->output( "...transcache tc_time already converted.\n" );
1018 return true;
1021 return $this->applyPatch( 'patch-tc-timestamp.sql', false,
1022 "Converting tc_time from UNIX epoch to MediaWiki timestamp" );
1026 * Update CategoryLinks collation
1028 protected function doCollationUpdate() {
1029 global $wgCategoryCollation;
1030 if ( $this->db->fieldExists( 'categorylinks', 'cl_collation', __METHOD__ ) ) {
1031 if ( $this->db->selectField(
1032 'categorylinks',
1033 'COUNT(*)',
1034 'cl_collation != ' . $this->db->addQuotes( $wgCategoryCollation ),
1035 __METHOD__
1036 ) == 0
1038 $this->output( "...collations up-to-date.\n" );
1040 return;
1043 $this->output( "Updating category collations..." );
1044 $task = $this->maintenance->runChild( 'UpdateCollation' );
1045 $task->execute();
1046 $this->output( "...done.\n" );
1051 * Migrates user options from the user table blob to user_properties
1053 protected function doMigrateUserOptions() {
1054 if ( $this->db->tableExists( 'user_properties' ) ) {
1055 $cl = $this->maintenance->runChild( 'ConvertUserOptions', 'convertUserOptions.php' );
1056 $cl->execute();
1057 $this->output( "done.\n" );
1062 * Enable profiling table when it's turned on
1064 protected function doEnableProfiling() {
1065 global $wgProfiler;
1067 if ( !$this->doTable( 'profiling' ) ) {
1068 return true;
1071 $profileToDb = false;
1072 if ( isset( $wgProfiler['output'] ) ) {
1073 $out = $wgProfiler['output'];
1074 if ( $out === 'db' ) {
1075 $profileToDb = true;
1076 } elseif ( is_array( $out ) && in_array( 'db', $out ) ) {
1077 $profileToDb = true;
1081 if ( $profileToDb && !$this->db->tableExists( 'profiling', __METHOD__ ) ) {
1082 $this->applyPatch( 'patch-profiling.sql', false, 'Add profiling table' );
1087 * Rebuilds the localisation cache
1089 protected function rebuildLocalisationCache() {
1091 * @var $cl RebuildLocalisationCache
1093 $cl = $this->maintenance->runChild( 'RebuildLocalisationCache', 'rebuildLocalisationCache.php' );
1094 $this->output( "Rebuilding localisation cache...\n" );
1095 $cl->setForce();
1096 $cl->execute();
1097 $this->output( "done.\n" );
1101 * Turns off content handler fields during parts of the upgrade
1102 * where they aren't available.
1104 protected function disableContentHandlerUseDB() {
1105 global $wgContentHandlerUseDB;
1107 if ( $wgContentHandlerUseDB ) {
1108 $this->output( "Turning off Content Handler DB fields for this part of upgrade.\n" );
1109 $this->holdContentHandlerUseDB = $wgContentHandlerUseDB;
1110 $wgContentHandlerUseDB = false;
1115 * Turns content handler fields back on.
1117 protected function enableContentHandlerUseDB() {
1118 global $wgContentHandlerUseDB;
1120 if ( $this->holdContentHandlerUseDB ) {
1121 $this->output( "Content Handler DB fields should be usable now.\n" );
1122 $wgContentHandlerUseDB = $this->holdContentHandlerUseDB;