Remove silly 'm' prefix from StubObject/DeprecatedGlobal members
[mediawiki.git] / includes / installer / DatabaseUpdater.php
blob82a358e0bf03a413ed6ca182e43efbd7850a4224
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 {
35 /**
36 * Array of updates to perform on the database
38 * @var array
40 protected $updates = array();
42 /**
43 * Array of updates that were skipped
45 * @var array
47 protected $updatesSkipped = array();
49 /**
50 * List of extension-provided database updates
51 * @var array
53 protected $extensionUpdates = array();
55 /**
56 * Handle to the database subclass
58 * @var DatabaseBase
60 protected $db;
62 protected $shared = false;
64 /**
65 * Scripts to run after database update
66 * Should be a subclass of LoggedUpdateMaintenance
68 protected $postDatabaseUpdateMaintenance = array(
69 'DeleteDefaultMessages',
70 'PopulateRevisionLength',
71 'PopulateRevisionSha1',
72 'PopulateImageSha1',
73 'FixExtLinksProtocolRelative',
74 'PopulateFilearchiveSha1',
77 /**
78 * File handle for SQL output.
80 * @var resource
82 protected $fileHandle = null;
84 /**
85 * Flag specifying whether or not to skip schema (e.g. SQL-only) updates.
87 * @var bool
89 protected $skipSchema = false;
91 /**
92 * Hold the value of $wgContentHandlerUseDB during the upgrade.
94 protected $holdContentHandlerUseDB = true;
96 /**
97 * Constructor
99 * @param DatabaseBase $db To perform updates on
100 * @param bool $shared Whether to perform updates on shared tables
101 * @param Maintenance $maintenance Maintenance object which created us
103 protected function __construct( DatabaseBase &$db, $shared, Maintenance $maintenance = null ) {
104 $this->db = $db;
105 $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
106 $this->shared = $shared;
107 if ( $maintenance ) {
108 $this->maintenance = $maintenance;
109 $this->fileHandle = $maintenance->fileHandle;
110 } else {
111 $this->maintenance = new FakeMaintenance;
113 $this->maintenance->setDB( $db );
114 $this->initOldGlobals();
115 $this->loadExtensions();
116 wfRunHooks( 'LoadExtensionSchemaUpdates', array( $this ) );
120 * Initialize all of the old globals. One day this should all become
121 * something much nicer
123 private function initOldGlobals() {
124 global $wgExtNewTables, $wgExtNewFields, $wgExtPGNewFields,
125 $wgExtPGAlteredFields, $wgExtNewIndexes, $wgExtModifiedFields;
127 # For extensions only, should be populated via hooks
128 # $wgDBtype should be checked to specifiy the proper file
129 $wgExtNewTables = array(); // table, dir
130 $wgExtNewFields = array(); // table, column, dir
131 $wgExtPGNewFields = array(); // table, column, column attributes; for PostgreSQL
132 $wgExtPGAlteredFields = array(); // table, column, new type, conversion method; for PostgreSQL
133 $wgExtNewIndexes = array(); // table, index, dir
134 $wgExtModifiedFields = array(); // table, index, dir
138 * Loads LocalSettings.php, if needed, and initialises everything needed for
139 * LoadExtensionSchemaUpdates hook.
141 private function loadExtensions() {
142 if ( !defined( 'MEDIAWIKI_INSTALL' ) ) {
143 return; // already loaded
145 $vars = Installer::getExistingLocalSettings();
146 if ( !$vars ) {
147 return; // no LocalSettings found
149 if ( !isset( $vars['wgHooks'] ) || !isset( $vars['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
150 return;
152 global $wgHooks, $wgAutoloadClasses;
153 $wgHooks['LoadExtensionSchemaUpdates'] = $vars['wgHooks']['LoadExtensionSchemaUpdates'];
154 $wgAutoloadClasses = $wgAutoloadClasses + $vars['wgAutoloadClasses'];
158 * @param DatabaseBase $db
159 * @param bool $shared
160 * @param Maintenance $maintenance
162 * @throws MWException
163 * @return DatabaseUpdater
165 public static function newForDB( &$db, $shared = false, $maintenance = null ) {
166 $type = $db->getType();
167 if ( in_array( $type, Installer::getDBTypes() ) ) {
168 $class = ucfirst( $type ) . 'Updater';
170 return new $class( $db, $shared, $maintenance );
171 } else {
172 throw new MWException( __METHOD__ . ' called for unsupported $wgDBtype' );
177 * Get a database connection to run updates
179 * @return DatabaseBase
181 public function getDB() {
182 return $this->db;
186 * Output some text. If we're running from web, escape the text first.
188 * @param string $str Text to output
190 public function output( $str ) {
191 if ( $this->maintenance->isQuiet() ) {
192 return;
194 global $wgCommandLineMode;
195 if ( !$wgCommandLineMode ) {
196 $str = htmlspecialchars( $str );
198 echo $str;
199 flush();
203 * Add a new update coming from an extension. This should be called by
204 * extensions while executing the LoadExtensionSchemaUpdates hook.
206 * @since 1.17
208 * @param array $update The update to run. Format is the following:
209 * first item is the callback function, it also can be a
210 * simple string with the name of a function in this class,
211 * following elements are parameters to the function.
212 * Note that callback functions will receive this object as
213 * first parameter.
215 public function addExtensionUpdate( array $update ) {
216 $this->extensionUpdates[] = $update;
220 * Convenience wrapper for addExtensionUpdate() when adding a new table (which
221 * is the most common usage of updaters in an extension)
223 * @since 1.18
225 * @param string $tableName Name of table to create
226 * @param string $sqlPath Full path to the schema file
228 public function addExtensionTable( $tableName, $sqlPath ) {
229 $this->extensionUpdates[] = array( 'addTable', $tableName, $sqlPath, true );
233 * @since 1.19
235 * @param string $tableName
236 * @param string $indexName
237 * @param string $sqlPath
239 public function addExtensionIndex( $tableName, $indexName, $sqlPath ) {
240 $this->extensionUpdates[] = array( 'addIndex', $tableName, $indexName, $sqlPath, true );
245 * @since 1.19
247 * @param string $tableName
248 * @param string $columnName
249 * @param string $sqlPath
251 public function addExtensionField( $tableName, $columnName, $sqlPath ) {
252 $this->extensionUpdates[] = array( 'addField', $tableName, $columnName, $sqlPath, true );
257 * @since 1.20
259 * @param string $tableName
260 * @param string $columnName
261 * @param string $sqlPath
263 public function dropExtensionField( $tableName, $columnName, $sqlPath ) {
264 $this->extensionUpdates[] = array( 'dropField', $tableName, $columnName, $sqlPath, true );
268 * Drop an index from an extension table
270 * @since 1.21
272 * @param string $tableName The table name
273 * @param string $indexName The index name
274 * @param string $sqlPath The path to the SQL change path
276 public function dropExtensionIndex( $tableName, $indexName, $sqlPath ) {
277 $this->extensionUpdates[] = array( 'dropIndex', $tableName, $indexName, $sqlPath, true );
282 * @since 1.20
284 * @param string $tableName
285 * @param string $sqlPath
287 public function dropExtensionTable( $tableName, $sqlPath ) {
288 $this->extensionUpdates[] = array( 'dropTable', $tableName, $sqlPath, true );
292 * Rename an index on an extension table
294 * @since 1.21
296 * @param string $tableName The table name
297 * @param string $oldIndexName The old index name
298 * @param string $newIndexName The new index name
299 * @param bool $skipBothIndexExistWarning Whether to warn if both the old
300 * and the new indexes exist. [facultative; by default, false]
301 * @param string $sqlPath The path to the SQL change path
303 public function renameExtensionIndex( $tableName, $oldIndexName, $newIndexName,
304 $sqlPath, $skipBothIndexExistWarning = false
306 $this->extensionUpdates[] = array(
307 'renameIndex',
308 $tableName,
309 $oldIndexName,
310 $newIndexName,
311 $skipBothIndexExistWarning,
312 $sqlPath,
313 true
318 * @since 1.21
320 * @param string $tableName The table name
321 * @param string $fieldName The field to be modified
322 * @param string $sqlPath The path to the SQL change path
324 public function modifyExtensionField( $tableName, $fieldName, $sqlPath ) {
325 $this->extensionUpdates[] = array( 'modifyField', $tableName, $fieldName, $sqlPath, true );
330 * @since 1.20
332 * @param string $tableName
333 * @return bool
335 public function tableExists( $tableName ) {
336 return ( $this->db->tableExists( $tableName, __METHOD__ ) );
340 * Add a maintenance script to be run after the database updates are complete.
342 * Script should subclass LoggedUpdateMaintenance
344 * @since 1.19
346 * @param string $class Name of a Maintenance subclass
348 public function addPostDatabaseUpdateMaintenance( $class ) {
349 $this->postDatabaseUpdateMaintenance[] = $class;
353 * Get the list of extension-defined updates
355 * @return array
357 protected function getExtensionUpdates() {
358 return $this->extensionUpdates;
362 * @since 1.17
364 * @return array
366 public function getPostDatabaseUpdateMaintenance() {
367 return $this->postDatabaseUpdateMaintenance;
371 * @since 1.21
373 * Writes the schema updates desired to a file for the DB Admin to run.
374 * @param array $schemaUpdate
376 private function writeSchemaUpdateFile( $schemaUpdate = array() ) {
377 $updates = $this->updatesSkipped;
378 $this->updatesSkipped = array();
380 foreach ( $updates as $funcList ) {
381 $func = $funcList[0];
382 $arg = $funcList[1];
383 $origParams = $funcList[2];
384 call_user_func_array( $func, $arg );
385 flush();
386 $this->updatesSkipped[] = $origParams;
391 * Do all the updates
393 * @param array $what What updates to perform
395 public function doUpdates( $what = array( 'core', 'extensions', 'stats' ) ) {
396 global $wgVersion;
398 $this->db->begin( __METHOD__ );
399 $what = array_flip( $what );
400 $this->skipSchema = isset( $what['noschema'] ) || $this->fileHandle !== null;
401 if ( isset( $what['core'] ) ) {
402 $this->runUpdates( $this->getCoreUpdateList(), false );
404 if ( isset( $what['extensions'] ) ) {
405 $this->runUpdates( $this->getOldGlobalUpdates(), false );
406 $this->runUpdates( $this->getExtensionUpdates(), true );
409 if ( isset( $what['stats'] ) ) {
410 $this->checkStats();
413 $this->setAppliedUpdates( $wgVersion, $this->updates );
415 if ( $this->fileHandle ) {
416 $this->skipSchema = false;
417 $this->writeSchemaUpdateFile();
418 $this->setAppliedUpdates( "$wgVersion-schema", $this->updatesSkipped );
421 $this->db->commit( __METHOD__ );
425 * Helper function for doUpdates()
427 * @param array $updates Array of updates to run
428 * @param bool $passSelf Whether to pass this object we calling external functions
430 private function runUpdates( array $updates, $passSelf ) {
431 $updatesDone = array();
432 $updatesSkipped = array();
433 foreach ( $updates as $params ) {
434 $origParams = $params;
435 $func = array_shift( $params );
436 if ( !is_array( $func ) && method_exists( $this, $func ) ) {
437 $func = array( $this, $func );
438 } elseif ( $passSelf ) {
439 array_unshift( $params, $this );
441 $ret = call_user_func_array( $func, $params );
442 flush();
443 if ( $ret !== false ) {
444 $updatesDone[] = $origParams;
445 } else {
446 $updatesSkipped[] = array( $func, $params, $origParams );
449 $this->updatesSkipped = array_merge( $this->updatesSkipped, $updatesSkipped );
450 $this->updates = array_merge( $this->updates, $updatesDone );
454 * @param string $version
455 * @param array $updates
457 protected function setAppliedUpdates( $version, $updates = array() ) {
458 $this->db->clearFlag( DBO_DDLMODE );
459 if ( !$this->canUseNewUpdatelog() ) {
460 return;
462 $key = "updatelist-$version-" . time();
463 $this->db->insert( 'updatelog',
464 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
465 __METHOD__ );
466 $this->db->setFlag( DBO_DDLMODE );
470 * Helper function: check if the given key is present in the updatelog table.
471 * Obviously, only use this for updates that occur after the updatelog table was
472 * created!
473 * @param string $key Name of the key to check for
474 * @return bool
476 public function updateRowExists( $key ) {
477 $row = $this->db->selectRow(
478 'updatelog',
479 '1',
480 array( 'ul_key' => $key ),
481 __METHOD__
484 return (bool)$row;
488 * Helper function: Add a key to the updatelog table
489 * Obviously, only use this for updates that occur after the updatelog table was
490 * created!
491 * @param string $key Name of key to insert
492 * @param string $val [optional] Value to insert along with the key
494 public function insertUpdateRow( $key, $val = null ) {
495 $this->db->clearFlag( DBO_DDLMODE );
496 $values = array( 'ul_key' => $key );
497 if ( $val && $this->canUseNewUpdatelog() ) {
498 $values['ul_value'] = $val;
500 $this->db->insert( 'updatelog', $values, __METHOD__, 'IGNORE' );
501 $this->db->setFlag( DBO_DDLMODE );
505 * Updatelog was changed in 1.17 to have a ul_value column so we can record
506 * more information about what kind of updates we've done (that's what this
507 * class does). Pre-1.17 wikis won't have this column, and really old wikis
508 * might not even have updatelog at all
510 * @return bool
512 protected function canUseNewUpdatelog() {
513 return $this->db->tableExists( 'updatelog', __METHOD__ ) &&
514 $this->db->fieldExists( 'updatelog', 'ul_value', __METHOD__ );
518 * Returns whether updates should be executed on the database table $name.
519 * Updates will be prevented if the table is a shared table and it is not
520 * specified to run updates on shared tables.
522 * @param string $name Table name
523 * @return bool
525 protected function doTable( $name ) {
526 global $wgSharedDB, $wgSharedTables;
528 // Don't bother to check $wgSharedTables if there isn't a shared database
529 // or the user actually also wants to do updates on the shared database.
530 if ( $wgSharedDB === null || $this->shared ) {
531 return true;
534 if ( in_array( $name, $wgSharedTables ) ) {
535 $this->output( "...skipping update to shared table $name.\n" );
536 return false;
537 } else {
538 return true;
543 * Before 1.17, we used to handle updates via stuff like
544 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
545 * of this in 1.17 but we want to remain back-compatible for a while. So
546 * load up these old global-based things into our update list.
548 * @return array
550 protected function getOldGlobalUpdates() {
551 global $wgExtNewFields, $wgExtNewTables, $wgExtModifiedFields,
552 $wgExtNewIndexes;
554 $updates = array();
556 foreach ( $wgExtNewTables as $tableRecord ) {
557 $updates[] = array(
558 'addTable', $tableRecord[0], $tableRecord[1], true
562 foreach ( $wgExtNewFields as $fieldRecord ) {
563 $updates[] = array(
564 'addField', $fieldRecord[0], $fieldRecord[1],
565 $fieldRecord[2], true
569 foreach ( $wgExtNewIndexes as $fieldRecord ) {
570 $updates[] = array(
571 'addIndex', $fieldRecord[0], $fieldRecord[1],
572 $fieldRecord[2], true
576 foreach ( $wgExtModifiedFields as $fieldRecord ) {
577 $updates[] = array(
578 'modifyField', $fieldRecord[0], $fieldRecord[1],
579 $fieldRecord[2], true
583 return $updates;
587 * Get an array of updates to perform on the database. Should return a
588 * multi-dimensional array. The main key is the MediaWiki version (1.12,
589 * 1.13...) with the values being arrays of updates, identical to how
590 * updaters.inc did it (for now)
592 * @return array
594 abstract protected function getCoreUpdateList();
597 * Append an SQL fragment to the open file handle.
599 * @param string $filename File name to open
601 public function copyFile( $filename ) {
602 $this->db->sourceFile( $filename, false, false, false,
603 array( $this, 'appendLine' )
608 * Append a line to the open filehandle. The line is assumed to
609 * be a complete SQL statement.
611 * This is used as a callback for for sourceLine().
613 * @param string $line Text to append to the file
614 * @return bool False to skip actually executing the file
615 * @throws MWException
617 public function appendLine( $line ) {
618 $line = rtrim( $line ) . ";\n";
619 if ( fwrite( $this->fileHandle, $line ) === false ) {
620 throw new MWException( "trouble writing file" );
623 return false;
627 * Applies a SQL patch
629 * @param string $path Path to the patch file
630 * @param bool $isFullPath Whether to treat $path as a relative or not
631 * @param string $msg Description of the patch
632 * @return bool False if patch is skipped.
634 protected function applyPatch( $path, $isFullPath = false, $msg = null ) {
635 if ( $msg === null ) {
636 $msg = "Applying $path patch";
638 if ( $this->skipSchema ) {
639 $this->output( "...skipping schema change ($msg).\n" );
641 return false;
644 $this->output( "$msg ..." );
646 if ( !$isFullPath ) {
647 $path = $this->db->patchPath( $path );
649 if ( $this->fileHandle !== null ) {
650 $this->copyFile( $path );
651 } else {
652 $this->db->sourceFile( $path );
654 $this->output( "done.\n" );
656 return true;
660 * Add a new table to the database
662 * @param string $name Name of the new table
663 * @param string $patch Path to the patch file
664 * @param bool $fullpath Whether to treat $patch path as a relative or not
665 * @return bool False if this was skipped because schema changes are skipped
667 protected function addTable( $name, $patch, $fullpath = false ) {
668 if ( !$this->doTable( $name ) ) {
669 return true;
672 if ( $this->db->tableExists( $name, __METHOD__ ) ) {
673 $this->output( "...$name table already exists.\n" );
674 } else {
675 return $this->applyPatch( $patch, $fullpath, "Creating $name table" );
678 return true;
682 * Add a new field to an existing table
684 * @param string $table Name of the table to modify
685 * @param string $field Name of the new field
686 * @param string $patch Path to the patch file
687 * @param bool $fullpath Whether to treat $patch path as a relative or not
688 * @return bool False if this was skipped because schema changes are skipped
690 protected function addField( $table, $field, $patch, $fullpath = false ) {
691 if ( !$this->doTable( $table ) ) {
692 return true;
695 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
696 $this->output( "...$table table does not exist, skipping new field patch.\n" );
697 } elseif ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
698 $this->output( "...have $field field in $table table.\n" );
699 } else {
700 return $this->applyPatch( $patch, $fullpath, "Adding $field field to table $table" );
703 return true;
707 * Add a new index to an existing table
709 * @param string $table Name of the table to modify
710 * @param string $index Name of the new index
711 * @param string $patch Path to the patch file
712 * @param bool $fullpath Whether to treat $patch path as a relative or not
713 * @return bool False if this was skipped because schema changes are skipped
715 protected function addIndex( $table, $index, $patch, $fullpath = false ) {
716 if ( !$this->doTable( $table ) ) {
717 return true;
720 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
721 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
722 } elseif ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
723 $this->output( "...index $index already set on $table table.\n" );
724 } else {
725 return $this->applyPatch( $patch, $fullpath, "Adding index $index to table $table" );
728 return true;
732 * Drop a field from an existing table
734 * @param string $table Name of the table to modify
735 * @param string $field Name of the old field
736 * @param string $patch Path to the patch file
737 * @param bool $fullpath Whether to treat $patch path as a relative or not
738 * @return bool False if this was skipped because schema changes are skipped
740 protected function dropField( $table, $field, $patch, $fullpath = false ) {
741 if ( !$this->doTable( $table ) ) {
742 return true;
745 if ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
746 return $this->applyPatch( $patch, $fullpath, "Table $table contains $field field. Dropping" );
747 } else {
748 $this->output( "...$table table does not contain $field field.\n" );
751 return true;
755 * Drop an index from an existing table
757 * @param string $table Name of the table to modify
758 * @param string $index Name of the index
759 * @param string $patch Path to the patch file
760 * @param bool $fullpath Whether to treat $patch path as a relative or not
761 * @return bool False if this was skipped because schema changes are skipped
763 protected function dropIndex( $table, $index, $patch, $fullpath = false ) {
764 if ( !$this->doTable( $table ) ) {
765 return true;
768 if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
769 return $this->applyPatch( $patch, $fullpath, "Dropping $index index from table $table" );
770 } else {
771 $this->output( "...$index key doesn't exist.\n" );
774 return true;
778 * Rename an index from an existing table
780 * @param string $table Name of the table to modify
781 * @param string $oldIndex Old name of the index
782 * @param string $newIndex New name of the index
783 * @param bool $skipBothIndexExistWarning Whether to warn if both the
784 * old and the new indexes exist.
785 * @param string $patch Path to the patch file
786 * @param bool $fullpath Whether to treat $patch path as a relative or not
787 * @return bool False if this was skipped because schema changes are skipped
789 protected function renameIndex( $table, $oldIndex, $newIndex,
790 $skipBothIndexExistWarning, $patch, $fullpath = false
792 if ( !$this->doTable( $table ) ) {
793 return true;
796 // First requirement: the table must exist
797 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
798 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
800 return true;
803 // Second requirement: the new index must be missing
804 if ( $this->db->indexExists( $table, $newIndex, __METHOD__ ) ) {
805 $this->output( "...index $newIndex already set on $table table.\n" );
806 if ( !$skipBothIndexExistWarning &&
807 $this->db->indexExists( $table, $oldIndex, __METHOD__ )
809 $this->output( "...WARNING: $oldIndex still exists, despite it has " .
810 "been renamed into $newIndex (which also exists).\n" .
811 " $oldIndex should be manually removed if not needed anymore.\n" );
814 return true;
817 // Third requirement: the old index must exist
818 if ( !$this->db->indexExists( $table, $oldIndex, __METHOD__ ) ) {
819 $this->output( "...skipping: index $oldIndex doesn't exist.\n" );
821 return true;
824 // Requirements have been satisfied, patch can be applied
825 return $this->applyPatch(
826 $patch,
827 $fullpath,
828 "Renaming index $oldIndex into $newIndex to table $table"
833 * If the specified table exists, drop it, or execute the
834 * patch if one is provided.
836 * Public @since 1.20
838 * @param string $table Table to drop.
839 * @param string|bool $patch String of patch file that will drop the table. Default: false.
840 * @param bool $fullpath Whether $patch is a full path. Default: false.
841 * @return bool False if this was skipped because schema changes are skipped
843 public function dropTable( $table, $patch = false, $fullpath = false ) {
844 if ( !$this->doTable( $table ) ) {
845 return true;
848 if ( $this->db->tableExists( $table, __METHOD__ ) ) {
849 $msg = "Dropping table $table";
851 if ( $patch === false ) {
852 $this->output( "$msg ..." );
853 $this->db->dropTable( $table, __METHOD__ );
854 $this->output( "done.\n" );
855 } else {
856 return $this->applyPatch( $patch, $fullpath, $msg );
858 } else {
859 $this->output( "...$table doesn't exist.\n" );
862 return true;
866 * Modify an existing field
868 * @param string $table Name of the table to which the field belongs
869 * @param string $field Name of the field to modify
870 * @param string $patch Path to the patch file
871 * @param bool $fullpath Whether to treat $patch path as a relative or not
872 * @return bool False if this was skipped because schema changes are skipped
874 public function modifyField( $table, $field, $patch, $fullpath = false ) {
875 if ( !$this->doTable( $table ) ) {
876 return true;
879 $updateKey = "$table-$field-$patch";
880 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
881 $this->output( "...$table table does not exist, skipping modify field patch.\n" );
882 } elseif ( !$this->db->fieldExists( $table, $field, __METHOD__ ) ) {
883 $this->output( "...$field field does not exist in $table table, " .
884 "skipping modify field patch.\n" );
885 } elseif ( $this->updateRowExists( $updateKey ) ) {
886 $this->output( "...$field in table $table already modified by patch $patch.\n" );
887 } else {
888 $this->insertUpdateRow( $updateKey );
890 return $this->applyPatch( $patch, $fullpath, "Modifying $field field of table $table" );
893 return true;
897 * Purge the objectcache table
899 public function purgeCache() {
900 global $wgLocalisationCacheConf;
901 # We can't guarantee that the user will be able to use TRUNCATE,
902 # but we know that DELETE is available to us
903 $this->output( "Purging caches..." );
904 $this->db->delete( 'objectcache', '*', __METHOD__ );
905 if ( $wgLocalisationCacheConf['manualRecache'] ) {
906 $this->rebuildLocalisationCache();
908 MessageBlobStore::clear();
909 $this->output( "done.\n" );
913 * Check the site_stats table is not properly populated.
915 protected function checkStats() {
916 $this->output( "...site_stats is populated..." );
917 $row = $this->db->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__ );
918 if ( $row === false ) {
919 $this->output( "data is missing! rebuilding...\n" );
920 } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
921 $this->output( "missing ss_total_pages, rebuilding...\n" );
922 } else {
923 $this->output( "done.\n" );
925 return;
927 SiteStatsInit::doAllAndCommit( $this->db );
930 # Common updater functions
933 * Sets the number of active users in the site_stats table
935 protected function doActiveUsersInit() {
936 $activeUsers = $this->db->selectField( 'site_stats', 'ss_active_users', false, __METHOD__ );
937 if ( $activeUsers == -1 ) {
938 $activeUsers = $this->db->selectField( 'recentchanges',
939 'COUNT( DISTINCT rc_user_text )',
940 array( 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ), __METHOD__
942 $this->db->update( 'site_stats',
943 array( 'ss_active_users' => intval( $activeUsers ) ),
944 array( 'ss_row_id' => 1 ), __METHOD__, array( 'LIMIT' => 1 )
947 $this->output( "...ss_active_users user count set...\n" );
951 * Populates the log_user_text field in the logging table
953 protected function doLogUsertextPopulation() {
954 if ( !$this->updateRowExists( 'populate log_usertext' ) ) {
955 $this->output(
956 "Populating log_user_text field, printing progress markers. For large\n" .
957 "databases, you may want to hit Ctrl-C and do this manually with\n" .
958 "maintenance/populateLogUsertext.php.\n"
961 $task = $this->maintenance->runChild( 'PopulateLogUsertext' );
962 $task->execute();
963 $this->output( "done.\n" );
968 * Migrate log params to new table and index for searching
970 protected function doLogSearchPopulation() {
971 if ( !$this->updateRowExists( 'populate log_search' ) ) {
972 $this->output(
973 "Populating log_search table, printing progress markers. For large\n" .
974 "databases, you may want to hit Ctrl-C and do this manually with\n" .
975 "maintenance/populateLogSearch.php.\n" );
977 $task = $this->maintenance->runChild( 'PopulateLogSearch' );
978 $task->execute();
979 $this->output( "done.\n" );
984 * Updates the timestamps in the transcache table
986 protected function doUpdateTranscacheField() {
987 if ( $this->updateRowExists( 'convert transcache field' ) ) {
988 $this->output( "...transcache tc_time already converted.\n" );
990 return true;
993 return $this->applyPatch( 'patch-tc-timestamp.sql', false,
994 "Converting tc_time from UNIX epoch to MediaWiki timestamp" );
998 * Update CategoryLinks collation
1000 protected function doCollationUpdate() {
1001 global $wgCategoryCollation;
1002 if ( $this->db->fieldExists( 'categorylinks', 'cl_collation', __METHOD__ ) ) {
1003 if ( $this->db->selectField(
1004 'categorylinks',
1005 'COUNT(*)',
1006 'cl_collation != ' . $this->db->addQuotes( $wgCategoryCollation ),
1007 __METHOD__
1008 ) == 0
1010 $this->output( "...collations up-to-date.\n" );
1012 return;
1015 $this->output( "Updating category collations..." );
1016 $task = $this->maintenance->runChild( 'UpdateCollation' );
1017 $task->execute();
1018 $this->output( "...done.\n" );
1023 * Migrates user options from the user table blob to user_properties
1025 protected function doMigrateUserOptions() {
1026 if ( $this->db->tableExists( 'user_properties' ) ) {
1027 $cl = $this->maintenance->runChild( 'ConvertUserOptions', 'convertUserOptions.php' );
1028 $cl->execute();
1029 $this->output( "done.\n" );
1034 * Rebuilds the localisation cache
1036 protected function rebuildLocalisationCache() {
1038 * @var $cl RebuildLocalisationCache
1040 $cl = $this->maintenance->runChild( 'RebuildLocalisationCache', 'rebuildLocalisationCache.php' );
1041 $this->output( "Rebuilding localisation cache...\n" );
1042 $cl->setForce();
1043 $cl->execute();
1044 $this->output( "done.\n" );
1048 * Turns off content handler fields during parts of the upgrade
1049 * where they aren't available.
1051 protected function disableContentHandlerUseDB() {
1052 global $wgContentHandlerUseDB;
1054 if ( $wgContentHandlerUseDB ) {
1055 $this->output( "Turning off Content Handler DB fields for this part of upgrade.\n" );
1056 $this->holdContentHandlerUseDB = $wgContentHandlerUseDB;
1057 $wgContentHandlerUseDB = false;
1062 * Turns content handler fields back on.
1064 protected function enableContentHandlerUseDB() {
1065 global $wgContentHandlerUseDB;
1067 if ( $this->holdContentHandlerUseDB ) {
1068 $this->output( "Content Handler DB fields should be usable now.\n" );
1069 $wgContentHandlerUseDB = $this->holdContentHandlerUseDB;