Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / installer / DatabaseUpdater.php
blob3f2e2cb8f8a1c0c475adfa4fecca3a3bdc1392f6
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 * @throws MWException
159 * @param DatabaseBase $db
160 * @param bool $shared
161 * @param null $maintenance
162 * @return DatabaseUpdater
164 public static function newForDB( &$db, $shared = false, $maintenance = null ) {
165 $type = $db->getType();
166 if ( in_array( $type, Installer::getDBTypes() ) ) {
167 $class = ucfirst( $type ) . 'Updater';
169 return new $class( $db, $shared, $maintenance );
170 } else {
171 throw new MWException( __METHOD__ . ' called for unsupported $wgDBtype' );
176 * Get a database connection to run updates
178 * @return DatabaseBase
180 public function getDB() {
181 return $this->db;
185 * Output some text. If we're running from web, escape the text first.
187 * @param string $str Text to output
189 public function output( $str ) {
190 if ( $this->maintenance->isQuiet() ) {
191 return;
193 global $wgCommandLineMode;
194 if ( !$wgCommandLineMode ) {
195 $str = htmlspecialchars( $str );
197 echo $str;
198 flush();
202 * Add a new update coming from an extension. This should be called by
203 * extensions while executing the LoadExtensionSchemaUpdates hook.
205 * @since 1.17
207 * @param array $update The update to run. Format is the following:
208 * first item is the callback function, it also can be a
209 * simple string with the name of a function in this class,
210 * following elements are parameters to the function.
211 * Note that callback functions will receive this object as
212 * first parameter.
214 public function addExtensionUpdate( array $update ) {
215 $this->extensionUpdates[] = $update;
219 * Convenience wrapper for addExtensionUpdate() when adding a new table (which
220 * is the most common usage of updaters in an extension)
222 * @since 1.18
224 * @param string $tableName Name of table to create
225 * @param string $sqlPath Full path to the schema file
227 public function addExtensionTable( $tableName, $sqlPath ) {
228 $this->extensionUpdates[] = array( 'addTable', $tableName, $sqlPath, true );
232 * @since 1.19
234 * @param $tableName string
235 * @param $indexName string
236 * @param $sqlPath string
238 public function addExtensionIndex( $tableName, $indexName, $sqlPath ) {
239 $this->extensionUpdates[] = array( 'addIndex', $tableName, $indexName, $sqlPath, true );
244 * @since 1.19
246 * @param $tableName string
247 * @param $columnName string
248 * @param $sqlPath string
250 public function addExtensionField( $tableName, $columnName, $sqlPath ) {
251 $this->extensionUpdates[] = array( 'addField', $tableName, $columnName, $sqlPath, true );
256 * @since 1.20
258 * @param $tableName string
259 * @param $columnName string
260 * @param $sqlPath string
262 public function dropExtensionField( $tableName, $columnName, $sqlPath ) {
263 $this->extensionUpdates[] = array( 'dropField', $tableName, $columnName, $sqlPath, true );
267 * Drop an index from an extension table
269 * @since 1.21
271 * @param string $tableName The table name
272 * @param string $indexName The index name
273 * @param string $sqlPath The path to the SQL change path
275 public function dropExtensionIndex( $tableName, $indexName, $sqlPath ) {
276 $this->extensionUpdates[] = array( 'dropIndex', $tableName, $indexName, $sqlPath, true );
281 * @since 1.20
283 * @param $tableName string
284 * @param $sqlPath string
286 public function dropExtensionTable( $tableName, $sqlPath ) {
287 $this->extensionUpdates[] = array( 'dropTable', $tableName, $sqlPath, true );
291 * Rename an index on an extension table
293 * @since 1.21
295 * @param string $tableName The table name
296 * @param string $oldIndexName The old index name
297 * @param string $newIndexName The new index name
298 * @param $skipBothIndexExistWarning Boolean: Whether to warn if both the old
299 * and the new indexes exist. [facultative; by default, false]
300 * @param string $sqlPath The path to the SQL change path
302 public function renameExtensionIndex( $tableName, $oldIndexName, $newIndexName,
303 $sqlPath, $skipBothIndexExistWarning = false
305 $this->extensionUpdates[] = array(
306 'renameIndex',
307 $tableName,
308 $oldIndexName,
309 $newIndexName,
310 $skipBothIndexExistWarning,
311 $sqlPath,
312 true
317 * @since 1.21
319 * @param string $tableName The table name
320 * @param string $fieldName The field to be modified
321 * @param string $sqlPath The path to the SQL change path
323 public function modifyExtensionField( $tableName, $fieldName, $sqlPath ) {
324 $this->extensionUpdates[] = array( 'modifyField', $tableName, $fieldName, $sqlPath, true );
329 * @since 1.20
331 * @param $tableName string
332 * @return bool
334 public function tableExists( $tableName ) {
335 return ( $this->db->tableExists( $tableName, __METHOD__ ) );
339 * Add a maintenance script to be run after the database updates are complete.
341 * Script should subclass LoggedUpdateMaintenance
343 * @since 1.19
345 * @param string $class Name of a Maintenance subclass
347 public function addPostDatabaseUpdateMaintenance( $class ) {
348 $this->postDatabaseUpdateMaintenance[] = $class;
352 * Get the list of extension-defined updates
354 * @return Array
356 protected function getExtensionUpdates() {
357 return $this->extensionUpdates;
361 * @since 1.17
363 * @return array
365 public function getPostDatabaseUpdateMaintenance() {
366 return $this->postDatabaseUpdateMaintenance;
370 * @since 1.21
372 * Writes the schema updates desired to a file for the DB Admin to run.
374 private function writeSchemaUpdateFile( $schemaUpdate = array() ) {
375 $updates = $this->updatesSkipped;
376 $this->updatesSkipped = array();
378 foreach ( $updates as $funcList ) {
379 $func = $funcList[0];
380 $arg = $funcList[1];
381 $origParams = $funcList[2];
382 call_user_func_array( $func, $arg );
383 flush();
384 $this->updatesSkipped[] = $origParams;
389 * Do all the updates
391 * @param array $what What updates to perform
393 public function doUpdates( $what = array( 'core', 'extensions', 'stats' ) ) {
394 global $wgVersion;
396 $this->db->begin( __METHOD__ );
397 $what = array_flip( $what );
398 $this->skipSchema = isset( $what['noschema'] ) || $this->fileHandle !== null;
399 if ( isset( $what['core'] ) ) {
400 $this->runUpdates( $this->getCoreUpdateList(), false );
402 if ( isset( $what['extensions'] ) ) {
403 $this->runUpdates( $this->getOldGlobalUpdates(), false );
404 $this->runUpdates( $this->getExtensionUpdates(), true );
407 if ( isset( $what['stats'] ) ) {
408 $this->checkStats();
411 $this->setAppliedUpdates( $wgVersion, $this->updates );
413 if ( $this->fileHandle ) {
414 $this->skipSchema = false;
415 $this->writeSchemaUpdateFile();
416 $this->setAppliedUpdates( "$wgVersion-schema", $this->updatesSkipped );
419 $this->db->commit( __METHOD__ );
423 * Helper function for doUpdates()
425 * @param array $updates of updates to run
426 * @param bool $passSelf Whether to pass this object we calling external
427 * functions
429 private function runUpdates( array $updates, $passSelf ) {
430 $updatesDone = array();
431 $updatesSkipped = array();
432 foreach ( $updates as $params ) {
433 $origParams = $params;
434 $func = array_shift( $params );
435 if ( !is_array( $func ) && method_exists( $this, $func ) ) {
436 $func = array( $this, $func );
437 } elseif ( $passSelf ) {
438 array_unshift( $params, $this );
440 $ret = call_user_func_array( $func, $params );
441 flush();
442 if ( $ret !== false ) {
443 $updatesDone[] = $origParams;
444 } else {
445 $updatesSkipped[] = array( $func, $params, $origParams );
448 $this->updatesSkipped = array_merge( $this->updatesSkipped, $updatesSkipped );
449 $this->updates = array_merge( $this->updates, $updatesDone );
453 * @param string $version
454 * @param array $updates
456 protected function setAppliedUpdates( $version, $updates = array() ) {
457 $this->db->clearFlag( DBO_DDLMODE );
458 if ( !$this->canUseNewUpdatelog() ) {
459 return;
461 $key = "updatelist-$version-" . time();
462 $this->db->insert( 'updatelog',
463 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
464 __METHOD__ );
465 $this->db->setFlag( DBO_DDLMODE );
469 * Helper function: check if the given key is present in the updatelog table.
470 * Obviously, only use this for updates that occur after the updatelog table was
471 * created!
472 * @param string $key Name of the key to check for
473 * @return bool
475 public function updateRowExists( $key ) {
476 $row = $this->db->selectRow(
477 'updatelog',
478 '1',
479 array( 'ul_key' => $key ),
480 __METHOD__
483 return (bool)$row;
487 * Helper function: Add a key to the updatelog table
488 * Obviously, only use this for updates that occur after the updatelog table was
489 * created!
490 * @param string $key Name of key to insert
491 * @param string $val [optional] Value to insert along with the key
493 public function insertUpdateRow( $key, $val = null ) {
494 $this->db->clearFlag( DBO_DDLMODE );
495 $values = array( 'ul_key' => $key );
496 if ( $val && $this->canUseNewUpdatelog() ) {
497 $values['ul_value'] = $val;
499 $this->db->insert( 'updatelog', $values, __METHOD__, 'IGNORE' );
500 $this->db->setFlag( DBO_DDLMODE );
504 * Updatelog was changed in 1.17 to have a ul_value column so we can record
505 * more information about what kind of updates we've done (that's what this
506 * class does). Pre-1.17 wikis won't have this column, and really old wikis
507 * might not even have updatelog at all
509 * @return boolean
511 protected function canUseNewUpdatelog() {
512 return $this->db->tableExists( 'updatelog', __METHOD__ ) &&
513 $this->db->fieldExists( 'updatelog', 'ul_value', __METHOD__ );
517 * Returns whether updates should be executed on the database table $name.
518 * Updates will be prevented if the table is a shared table and it is not
519 * specified to run updates on shared tables.
521 * @param string $name Table name
522 * @return bool
524 protected function doTable( $name ) {
525 global $wgSharedDB, $wgSharedTables;
527 // Don't bother to check $wgSharedTables if there isn't a shared database
528 // or the user actually also wants to do updates on the shared database.
529 if ( $wgSharedDB === null || $this->shared ) {
530 return true;
533 return !in_array( $name, $wgSharedTables );
537 * Before 1.17, we used to handle updates via stuff like
538 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
539 * of this in 1.17 but we want to remain back-compatible for a while. So
540 * load up these old global-based things into our update list.
542 * @return array
544 protected function getOldGlobalUpdates() {
545 global $wgExtNewFields, $wgExtNewTables, $wgExtModifiedFields,
546 $wgExtNewIndexes;
548 $updates = array();
550 foreach ( $wgExtNewTables as $tableRecord ) {
551 $updates[] = array(
552 'addTable', $tableRecord[0], $tableRecord[1], true
556 foreach ( $wgExtNewFields as $fieldRecord ) {
557 $updates[] = array(
558 'addField', $fieldRecord[0], $fieldRecord[1],
559 $fieldRecord[2], true
563 foreach ( $wgExtNewIndexes as $fieldRecord ) {
564 $updates[] = array(
565 'addIndex', $fieldRecord[0], $fieldRecord[1],
566 $fieldRecord[2], true
570 foreach ( $wgExtModifiedFields as $fieldRecord ) {
571 $updates[] = array(
572 'modifyField', $fieldRecord[0], $fieldRecord[1],
573 $fieldRecord[2], true
577 return $updates;
581 * Get an array of updates to perform on the database. Should return a
582 * multi-dimensional array. The main key is the MediaWiki version (1.12,
583 * 1.13...) with the values being arrays of updates, identical to how
584 * updaters.inc did it (for now)
586 * @return array
588 abstract protected function getCoreUpdateList();
591 * Append an SQL fragment to the open file handle.
593 * @param string $filename File name to open
595 public function copyFile( $filename ) {
596 $this->db->sourceFile( $filename, false, false, false,
597 array( $this, 'appendLine' )
602 * Append a line to the open filehandle. The line is assumed to
603 * be a complete SQL statement.
605 * This is used as a callback for for sourceLine().
607 * @param string $line Text to append to the file
608 * @return bool False to skip actually executing the file
609 * @throws MWException
611 public function appendLine( $line ) {
612 $line = rtrim( $line ) . ";\n";
613 if ( fwrite( $this->fileHandle, $line ) === false ) {
614 throw new MWException( "trouble writing file" );
617 return false;
621 * Applies a SQL patch
623 * @param string $path Path to the patch file
624 * @param $isFullPath Boolean Whether to treat $path as a relative or not
625 * @param string $msg Description of the patch
626 * @return bool False if patch is skipped.
628 protected function applyPatch( $path, $isFullPath = false, $msg = null ) {
629 if ( $msg === null ) {
630 $msg = "Applying $path patch";
632 if ( $this->skipSchema ) {
633 $this->output( "...skipping schema change ($msg).\n" );
635 return false;
638 $this->output( "$msg ..." );
640 if ( !$isFullPath ) {
641 $path = $this->db->patchPath( $path );
643 if ( $this->fileHandle !== null ) {
644 $this->copyFile( $path );
645 } else {
646 $this->db->sourceFile( $path );
648 $this->output( "done.\n" );
650 return true;
654 * Add a new table to the database
656 * @param string $name Name of the new table
657 * @param string $patch Path to the patch file
658 * @param bool $fullpath Whether to treat $patch path as a relative or not
659 * @return bool False if this was skipped because schema changes are skipped
661 protected function addTable( $name, $patch, $fullpath = false ) {
662 if ( !$this->doTable( $name ) ) {
663 return true;
666 if ( $this->db->tableExists( $name, __METHOD__ ) ) {
667 $this->output( "...$name table already exists.\n" );
668 } else {
669 return $this->applyPatch( $patch, $fullpath, "Creating $name table" );
672 return true;
676 * Add a new field to an existing table
678 * @param string $table Name of the table to modify
679 * @param string $field Name of the new field
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 addField( $table, $field, $patch, $fullpath = false ) {
685 if ( !$this->doTable( $table ) ) {
686 return true;
689 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
690 $this->output( "...$table table does not exist, skipping new field patch.\n" );
691 } elseif ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
692 $this->output( "...have $field field in $table table.\n" );
693 } else {
694 return $this->applyPatch( $patch, $fullpath, "Adding $field field to table $table" );
697 return true;
701 * Add a new index to an existing table
703 * @param string $table Name of the table to modify
704 * @param string $index Name of the new index
705 * @param string $patch Path to the patch file
706 * @param bool $fullpath Whether to treat $patch path as a relative or not
707 * @return bool False if this was skipped because schema changes are skipped
709 protected function addIndex( $table, $index, $patch, $fullpath = false ) {
710 if ( !$this->doTable( $table ) ) {
711 return true;
714 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
715 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
716 } elseif ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
717 $this->output( "...index $index already set on $table table.\n" );
718 } else {
719 return $this->applyPatch( $patch, $fullpath, "Adding index $index to table $table" );
722 return true;
726 * Drop a field from an existing table
728 * @param string $table Name of the table to modify
729 * @param string $field Name of the old field
730 * @param string $patch Path to the patch file
731 * @param bool $fullpath Whether to treat $patch path as a relative or not
732 * @return bool False if this was skipped because schema changes are skipped
734 protected function dropField( $table, $field, $patch, $fullpath = false ) {
735 if ( !$this->doTable( $table ) ) {
736 return true;
739 if ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
740 return $this->applyPatch( $patch, $fullpath, "Table $table contains $field field. Dropping" );
741 } else {
742 $this->output( "...$table table does not contain $field field.\n" );
745 return true;
749 * Drop an index from an existing table
751 * @param string $table Name of the table to modify
752 * @param string $index Name of the index
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 dropIndex( $table, $index, $patch, $fullpath = false ) {
758 if ( !$this->doTable( $table ) ) {
759 return true;
762 if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
763 return $this->applyPatch( $patch, $fullpath, "Dropping $index index from table $table" );
764 } else {
765 $this->output( "...$index key doesn't exist.\n" );
768 return true;
772 * Rename an index from an existing table
774 * @param string $table Name of the table to modify
775 * @param string $oldIndex Old name of the index
776 * @param string $newIndex New name of the index
777 * @param $skipBothIndexExistWarning Boolean: Whether to warn if both the
778 * old and the new indexes exist.
779 * @param string $patch Path to the patch file
780 * @param bool $fullpath Whether to treat $patch path as a relative or not
781 * @return bool False if this was skipped because schema changes are skipped
783 protected function renameIndex( $table, $oldIndex, $newIndex,
784 $skipBothIndexExistWarning, $patch, $fullpath = false
786 if ( !$this->doTable( $table ) ) {
787 return true;
790 // First requirement: the table must exist
791 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
792 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
794 return true;
797 // Second requirement: the new index must be missing
798 if ( $this->db->indexExists( $table, $newIndex, __METHOD__ ) ) {
799 $this->output( "...index $newIndex already set on $table table.\n" );
800 if ( !$skipBothIndexExistWarning &&
801 $this->db->indexExists( $table, $oldIndex, __METHOD__ )
803 $this->output( "...WARNING: $oldIndex still exists, despite it has " .
804 "been renamed into $newIndex (which also exists).\n" .
805 " $oldIndex should be manually removed if not needed anymore.\n" );
808 return true;
811 // Third requirement: the old index must exist
812 if ( !$this->db->indexExists( $table, $oldIndex, __METHOD__ ) ) {
813 $this->output( "...skipping: index $oldIndex doesn't exist.\n" );
815 return true;
818 // Requirements have been satisfied, patch can be applied
819 return $this->applyPatch(
820 $patch,
821 $fullpath,
822 "Renaming index $oldIndex into $newIndex to table $table"
827 * If the specified table exists, drop it, or execute the
828 * patch if one is provided.
830 * Public @since 1.20
832 * @param string $table Table to drop.
833 * @param string|bool $patch String of patch file that will drop the table. Default: false.
834 * @param bool $fullpath Whether $patch is a full path. Default: false.
835 * @return bool False if this was skipped because schema changes are skipped
837 public function dropTable( $table, $patch = false, $fullpath = false ) {
838 if ( !$this->doTable( $table ) ) {
839 return true;
842 if ( $this->db->tableExists( $table, __METHOD__ ) ) {
843 $msg = "Dropping table $table";
845 if ( $patch === false ) {
846 $this->output( "$msg ..." );
847 $this->db->dropTable( $table, __METHOD__ );
848 $this->output( "done.\n" );
849 } else {
850 return $this->applyPatch( $patch, $fullpath, $msg );
852 } else {
853 $this->output( "...$table doesn't exist.\n" );
856 return true;
860 * Modify an existing field
862 * @param string $table Name of the table to which the field belongs
863 * @param string $field Name of the field to modify
864 * @param string $patch Path to the patch file
865 * @param bool $fullpath Whether to treat $patch path as a relative or not
866 * @return bool False if this was skipped because schema changes are skipped
868 public function modifyField( $table, $field, $patch, $fullpath = false ) {
869 if ( !$this->doTable( $table ) ) {
870 return true;
873 $updateKey = "$table-$field-$patch";
874 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
875 $this->output( "...$table table does not exist, skipping modify field patch.\n" );
876 } elseif ( !$this->db->fieldExists( $table, $field, __METHOD__ ) ) {
877 $this->output( "...$field field does not exist in $table table, " .
878 "skipping modify field patch.\n" );
879 } elseif ( $this->updateRowExists( $updateKey ) ) {
880 $this->output( "...$field in table $table already modified by patch $patch.\n" );
881 } else {
882 $this->insertUpdateRow( $updateKey );
884 return $this->applyPatch( $patch, $fullpath, "Modifying $field field of table $table" );
887 return true;
891 * Purge the objectcache table
893 public function purgeCache() {
894 global $wgLocalisationCacheConf;
895 # We can't guarantee that the user will be able to use TRUNCATE,
896 # but we know that DELETE is available to us
897 $this->output( "Purging caches..." );
898 $this->db->delete( 'objectcache', '*', __METHOD__ );
899 if ( $wgLocalisationCacheConf['manualRecache'] ) {
900 $this->rebuildLocalisationCache();
902 MessageBlobStore::clear();
903 $this->output( "done.\n" );
907 * Check the site_stats table is not properly populated.
909 protected function checkStats() {
910 $this->output( "...site_stats is populated..." );
911 $row = $this->db->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__ );
912 if ( $row === false ) {
913 $this->output( "data is missing! rebuilding...\n" );
914 } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
915 $this->output( "missing ss_total_pages, rebuilding...\n" );
916 } else {
917 $this->output( "done.\n" );
919 return;
921 SiteStatsInit::doAllAndCommit( $this->db );
924 # Common updater functions
927 * Sets the number of active users in the site_stats table
929 protected function doActiveUsersInit() {
930 $activeUsers = $this->db->selectField( 'site_stats', 'ss_active_users', false, __METHOD__ );
931 if ( $activeUsers == -1 ) {
932 $activeUsers = $this->db->selectField( 'recentchanges',
933 'COUNT( DISTINCT rc_user_text )',
934 array( 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ), __METHOD__
936 $this->db->update( 'site_stats',
937 array( 'ss_active_users' => intval( $activeUsers ) ),
938 array( 'ss_row_id' => 1 ), __METHOD__, array( 'LIMIT' => 1 )
941 $this->output( "...ss_active_users user count set...\n" );
945 * Populates the log_user_text field in the logging table
947 protected function doLogUsertextPopulation() {
948 if ( !$this->updateRowExists( 'populate log_usertext' ) ) {
949 $this->output(
950 "Populating log_user_text field, printing progress markers. For large\n" .
951 "databases, you may want to hit Ctrl-C and do this manually with\n" .
952 "maintenance/populateLogUsertext.php.\n"
955 $task = $this->maintenance->runChild( 'PopulateLogUsertext' );
956 $task->execute();
957 $this->output( "done.\n" );
962 * Migrate log params to new table and index for searching
964 protected function doLogSearchPopulation() {
965 if ( !$this->updateRowExists( 'populate log_search' ) ) {
966 $this->output(
967 "Populating log_search table, printing progress markers. For large\n" .
968 "databases, you may want to hit Ctrl-C and do this manually with\n" .
969 "maintenance/populateLogSearch.php.\n" );
971 $task = $this->maintenance->runChild( 'PopulateLogSearch' );
972 $task->execute();
973 $this->output( "done.\n" );
978 * Updates the timestamps in the transcache table
980 protected function doUpdateTranscacheField() {
981 if ( $this->updateRowExists( 'convert transcache field' ) ) {
982 $this->output( "...transcache tc_time already converted.\n" );
984 return true;
987 return $this->applyPatch( 'patch-tc-timestamp.sql', false,
988 "Converting tc_time from UNIX epoch to MediaWiki timestamp" );
992 * Update CategoryLinks collation
994 protected function doCollationUpdate() {
995 global $wgCategoryCollation;
996 if ( $this->db->fieldExists( 'categorylinks', 'cl_collation', __METHOD__ ) ) {
997 if ( $this->db->selectField(
998 'categorylinks',
999 'COUNT(*)',
1000 'cl_collation != ' . $this->db->addQuotes( $wgCategoryCollation ),
1001 __METHOD__
1002 ) == 0
1004 $this->output( "...collations up-to-date.\n" );
1006 return;
1009 $this->output( "Updating category collations..." );
1010 $task = $this->maintenance->runChild( 'UpdateCollation' );
1011 $task->execute();
1012 $this->output( "...done.\n" );
1017 * Migrates user options from the user table blob to user_properties
1019 protected function doMigrateUserOptions() {
1020 if ( $this->db->tableExists( 'user_properties' ) ) {
1021 $cl = $this->maintenance->runChild( 'ConvertUserOptions', 'convertUserOptions.php' );
1022 $cl->execute();
1023 $this->output( "done.\n" );
1028 * Rebuilds the localisation cache
1030 protected function rebuildLocalisationCache() {
1032 * @var $cl RebuildLocalisationCache
1034 $cl = $this->maintenance->runChild( 'RebuildLocalisationCache', 'rebuildLocalisationCache.php' );
1035 $this->output( "Rebuilding localisation cache...\n" );
1036 $cl->setForce();
1037 $cl->execute();
1038 $this->output( "done.\n" );
1042 * Turns off content handler fields during parts of the upgrade
1043 * where they aren't available.
1045 protected function disableContentHandlerUseDB() {
1046 global $wgContentHandlerUseDB;
1048 if ( $wgContentHandlerUseDB ) {
1049 $this->output( "Turning off Content Handler DB fields for this part of upgrade.\n" );
1050 $this->holdContentHandlerUseDB = $wgContentHandlerUseDB;
1051 $wgContentHandlerUseDB = false;
1056 * Turns content handler fields back on.
1058 protected function enableContentHandlerUseDB() {
1059 global $wgContentHandlerUseDB;
1061 if ( $this->holdContentHandlerUseDB ) {
1062 $this->output( "Content Handler DB fields should be usable now.\n" );
1063 $wgContentHandlerUseDB = $this->holdContentHandlerUseDB;