lessphp: Update to upstream 6e8e724fc7
[mediawiki.git] / includes / installer / DatabaseUpdater.php
blobd4fe53015f1de74cf6a0124b9c3964c65bcc743e
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 Filehandle
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 * Constructor
94 * @param $db DatabaseBase object to perform updates on
95 * @param bool $shared Whether to perform updates on shared tables
96 * @param $maintenance Maintenance Maintenance object which created us
98 protected function __construct( DatabaseBase &$db, $shared, Maintenance $maintenance = null ) {
99 $this->db = $db;
100 $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
101 $this->shared = $shared;
102 if ( $maintenance ) {
103 $this->maintenance = $maintenance;
104 $this->fileHandle = $maintenance->fileHandle;
105 } else {
106 $this->maintenance = new FakeMaintenance;
108 $this->maintenance->setDB( $db );
109 $this->initOldGlobals();
110 $this->loadExtensions();
111 wfRunHooks( 'LoadExtensionSchemaUpdates', array( $this ) );
115 * Initialize all of the old globals. One day this should all become
116 * something much nicer
118 private function initOldGlobals() {
119 global $wgExtNewTables, $wgExtNewFields, $wgExtPGNewFields,
120 $wgExtPGAlteredFields, $wgExtNewIndexes, $wgExtModifiedFields;
122 # For extensions only, should be populated via hooks
123 # $wgDBtype should be checked to specifiy the proper file
124 $wgExtNewTables = array(); // table, dir
125 $wgExtNewFields = array(); // table, column, dir
126 $wgExtPGNewFields = array(); // table, column, column attributes; for PostgreSQL
127 $wgExtPGAlteredFields = array(); // table, column, new type, conversion method; for PostgreSQL
128 $wgExtNewIndexes = array(); // table, index, dir
129 $wgExtModifiedFields = array(); // table, index, dir
133 * Loads LocalSettings.php, if needed, and initialises everything needed for LoadExtensionSchemaUpdates hook
135 private function loadExtensions() {
136 if ( !defined( 'MEDIAWIKI_INSTALL' ) ) {
137 return; // already loaded
139 $vars = Installer::getExistingLocalSettings();
140 if ( !$vars ) {
141 return; // no LocalSettings found
143 if ( !isset( $vars['wgHooks'] ) || !isset( $vars['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
144 return;
146 global $wgHooks, $wgAutoloadClasses;
147 $wgHooks['LoadExtensionSchemaUpdates'] = $vars['wgHooks']['LoadExtensionSchemaUpdates'];
148 $wgAutoloadClasses = $wgAutoloadClasses + $vars['wgAutoloadClasses'];
152 * @throws MWException
153 * @param DatabaseBase $db
154 * @param bool $shared
155 * @param null $maintenance
156 * @return DatabaseUpdater
158 public static function newForDB( &$db, $shared = false, $maintenance = null ) {
159 $type = $db->getType();
160 if ( in_array( $type, Installer::getDBTypes() ) ) {
161 $class = ucfirst( $type ) . 'Updater';
162 return new $class( $db, $shared, $maintenance );
163 } else {
164 throw new MWException( __METHOD__ . ' called for unsupported $wgDBtype' );
169 * Get a database connection to run updates
171 * @return DatabaseBase
173 public function getDB() {
174 return $this->db;
178 * Output some text. If we're running from web, escape the text first.
180 * @param string $str Text to output
182 public function output( $str ) {
183 if ( $this->maintenance->isQuiet() ) {
184 return;
186 global $wgCommandLineMode;
187 if ( !$wgCommandLineMode ) {
188 $str = htmlspecialchars( $str );
190 echo $str;
191 flush();
195 * Add a new update coming from an extension. This should be called by
196 * extensions while executing the LoadExtensionSchemaUpdates hook.
198 * @since 1.17
200 * @param array $update the update to run. Format is the following:
201 * first item is the callback function, it also can be a
202 * simple string with the name of a function in this class,
203 * following elements are parameters to the function.
204 * Note that callback functions will receive this object as
205 * first parameter.
207 public function addExtensionUpdate( array $update ) {
208 $this->extensionUpdates[] = $update;
212 * Convenience wrapper for addExtensionUpdate() when adding a new table (which
213 * is the most common usage of updaters in an extension)
215 * @since 1.18
217 * @param string $tableName Name of table to create
218 * @param string $sqlPath Full path to the schema file
220 public function addExtensionTable( $tableName, $sqlPath ) {
221 $this->extensionUpdates[] = array( 'addTable', $tableName, $sqlPath, true );
225 * @since 1.19
227 * @param $tableName string
228 * @param $indexName string
229 * @param $sqlPath string
231 public function addExtensionIndex( $tableName, $indexName, $sqlPath ) {
232 $this->extensionUpdates[] = array( 'addIndex', $tableName, $indexName, $sqlPath, true );
237 * @since 1.19
239 * @param $tableName string
240 * @param $columnName string
241 * @param $sqlPath string
243 public function addExtensionField( $tableName, $columnName, $sqlPath ) {
244 $this->extensionUpdates[] = array( 'addField', $tableName, $columnName, $sqlPath, true );
249 * @since 1.20
251 * @param $tableName string
252 * @param $columnName string
253 * @param $sqlPath string
255 public function dropExtensionField( $tableName, $columnName, $sqlPath ) {
256 $this->extensionUpdates[] = array( 'dropField', $tableName, $columnName, $sqlPath, true );
260 * Drop an index from an extension table
262 * @since 1.21
264 * @param string $tableName The table name
265 * @param string $indexName The index name
266 * @param string $sqlPath The path to the SQL change path
268 public function dropExtensionIndex( $tableName, $indexName, $sqlPath ) {
269 $this->extensionUpdates[] = array( 'dropIndex', $tableName, $indexName, $sqlPath, true );
274 * @since 1.20
276 * @param $tableName string
277 * @param $sqlPath string
279 public function dropExtensionTable( $tableName, $sqlPath ) {
280 $this->extensionUpdates[] = array( 'dropTable', $tableName, $sqlPath, true );
284 * Rename an index on an extension table
286 * @since 1.21
288 * @param string $tableName The table name
289 * @param string $oldIndexName The old index name
290 * @param string $newIndexName The new index name
291 * @param $skipBothIndexExistWarning Boolean: Whether to warn if both the old and the new indexes exist. [facultative; by default, false]
292 * @param string $sqlPath The path to the SQL change path
294 public function renameExtensionIndex( $tableName, $oldIndexName, $newIndexName, $sqlPath, $skipBothIndexExistWarning = false ) {
295 $this->extensionUpdates[] = array( 'renameIndex', $tableName, $oldIndexName, $newIndexName, $skipBothIndexExistWarning, $sqlPath, true );
299 * @since 1.21
301 * @param string $tableName The table name
302 * @param string $fieldName The field to be modified
303 * @param string $sqlPath The path to the SQL change path
305 public function modifyExtensionField( $tableName, $fieldName, $sqlPath ) {
306 $this->extensionUpdates[] = array( 'modifyField', $tableName, $fieldName, $sqlPath, true );
311 * @since 1.20
313 * @param $tableName string
314 * @return bool
316 public function tableExists( $tableName ) {
317 return ( $this->db->tableExists( $tableName, __METHOD__ ) );
321 * Add a maintenance script to be run after the database updates are complete.
323 * Script should subclass LoggedUpdateMaintenance
325 * @since 1.19
327 * @param string $class Name of a Maintenance subclass
329 public function addPostDatabaseUpdateMaintenance( $class ) {
330 $this->postDatabaseUpdateMaintenance[] = $class;
334 * Get the list of extension-defined updates
336 * @return Array
338 protected function getExtensionUpdates() {
339 return $this->extensionUpdates;
343 * @since 1.17
345 * @return array
347 public function getPostDatabaseUpdateMaintenance() {
348 return $this->postDatabaseUpdateMaintenance;
352 * @since 1.21
354 * Writes the schema updates desired to a file for the DB Admin to run.
356 private function writeSchemaUpdateFile( $schemaUpdate = array() ) {
357 $updates = $this->updatesSkipped;
358 $this->updatesSkipped = array();
360 foreach ( $updates as $funcList ) {
361 $func = $funcList[0];
362 $arg = $funcList[1];
363 $origParams = $funcList[2];
364 call_user_func_array( $func, $arg );
365 flush();
366 $this->updatesSkipped[] = $origParams;
371 * Do all the updates
373 * @param array $what what updates to perform
375 public function doUpdates( $what = array( 'core', 'extensions', 'stats' ) ) {
376 global $wgVersion;
378 $this->db->begin( __METHOD__ );
379 $what = array_flip( $what );
380 $this->skipSchema = isset( $what['noschema'] ) || $this->fileHandle !== null;
381 if ( isset( $what['core'] ) ) {
382 $this->runUpdates( $this->getCoreUpdateList(), false );
384 if ( isset( $what['extensions'] ) ) {
385 $this->runUpdates( $this->getOldGlobalUpdates(), false );
386 $this->runUpdates( $this->getExtensionUpdates(), true );
389 if ( isset( $what['stats'] ) ) {
390 $this->checkStats();
393 $this->setAppliedUpdates( $wgVersion, $this->updates );
395 if ( $this->fileHandle ) {
396 $this->skipSchema = false;
397 $this->writeSchemaUpdateFile();
398 $this->setAppliedUpdates( "$wgVersion-schema", $this->updatesSkipped );
401 $this->db->commit( __METHOD__ );
405 * Helper function for doUpdates()
407 * @param array $updates of updates to run
408 * @param $passSelf Boolean: whether to pass this object we calling external
409 * functions
411 private function runUpdates( array $updates, $passSelf ) {
412 $updatesDone = array();
413 $updatesSkipped = array();
414 foreach ( $updates as $params ) {
415 $origParams = $params;
416 $func = array_shift( $params );
417 if ( !is_array( $func ) && method_exists( $this, $func ) ) {
418 $func = array( $this, $func );
419 } elseif ( $passSelf ) {
420 array_unshift( $params, $this );
422 $ret = call_user_func_array( $func, $params );
423 flush();
424 if ( $ret !== false ) {
425 $updatesDone[] = $origParams;
426 } else {
427 $updatesSkipped[] = array( $func, $params, $origParams );
430 $this->updatesSkipped = array_merge( $this->updatesSkipped, $updatesSkipped );
431 $this->updates = array_merge( $this->updates, $updatesDone );
435 * @param $version
436 * @param $updates array
438 protected function setAppliedUpdates( $version, $updates = array() ) {
439 $this->db->clearFlag( DBO_DDLMODE );
440 if ( !$this->canUseNewUpdatelog() ) {
441 return;
443 $key = "updatelist-$version-" . time();
444 $this->db->insert( 'updatelog',
445 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
446 __METHOD__ );
447 $this->db->setFlag( DBO_DDLMODE );
451 * Helper function: check if the given key is present in the updatelog table.
452 * Obviously, only use this for updates that occur after the updatelog table was
453 * created!
454 * @param string $key Name of the key to check for
456 * @return bool
458 public function updateRowExists( $key ) {
459 $row = $this->db->selectRow(
460 'updatelog',
461 '1',
462 array( 'ul_key' => $key ),
463 __METHOD__
465 return (bool)$row;
469 * Helper function: Add a key to 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 key to insert
473 * @param string $val [optional] value to insert along with the key
475 public function insertUpdateRow( $key, $val = null ) {
476 $this->db->clearFlag( DBO_DDLMODE );
477 $values = array( 'ul_key' => $key );
478 if ( $val && $this->canUseNewUpdatelog() ) {
479 $values['ul_value'] = $val;
481 $this->db->insert( 'updatelog', $values, __METHOD__, 'IGNORE' );
482 $this->db->setFlag( DBO_DDLMODE );
486 * Updatelog was changed in 1.17 to have a ul_value column so we can record
487 * more information about what kind of updates we've done (that's what this
488 * class does). Pre-1.17 wikis won't have this column, and really old wikis
489 * might not even have updatelog at all
491 * @return boolean
493 protected function canUseNewUpdatelog() {
494 return $this->db->tableExists( 'updatelog', __METHOD__ ) &&
495 $this->db->fieldExists( 'updatelog', 'ul_value', __METHOD__ );
499 * Returns whether updates should be executed on the database table $name.
500 * Updates will be prevented if the table is a shared table and it is not
501 * specified to run updates on shared tables.
503 * @param string $name table name
504 * @return bool
506 protected function doTable( $name ) {
507 global $wgSharedDB, $wgSharedTables;
509 // Don't bother to check $wgSharedTables if there isn't a shared database
510 // or the user actually also wants to do updates on the shared database.
511 if ( $wgSharedDB === null || $this->shared ) {
512 return true;
515 return !in_array( $name, $wgSharedTables );
519 * Before 1.17, we used to handle updates via stuff like
520 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
521 * of this in 1.17 but we want to remain back-compatible for a while. So
522 * load up these old global-based things into our update list.
524 * @return array
526 protected function getOldGlobalUpdates() {
527 global $wgExtNewFields, $wgExtNewTables, $wgExtModifiedFields,
528 $wgExtNewIndexes;
530 $updates = array();
532 foreach ( $wgExtNewTables as $tableRecord ) {
533 $updates[] = array(
534 'addTable', $tableRecord[0], $tableRecord[1], true
538 foreach ( $wgExtNewFields as $fieldRecord ) {
539 $updates[] = array(
540 'addField', $fieldRecord[0], $fieldRecord[1],
541 $fieldRecord[2], true
545 foreach ( $wgExtNewIndexes as $fieldRecord ) {
546 $updates[] = array(
547 'addIndex', $fieldRecord[0], $fieldRecord[1],
548 $fieldRecord[2], true
552 foreach ( $wgExtModifiedFields as $fieldRecord ) {
553 $updates[] = array(
554 'modifyField', $fieldRecord[0], $fieldRecord[1],
555 $fieldRecord[2], true
559 return $updates;
563 * Get an array of updates to perform on the database. Should return a
564 * multi-dimensional array. The main key is the MediaWiki version (1.12,
565 * 1.13...) with the values being arrays of updates, identical to how
566 * updaters.inc did it (for now)
568 * @return Array
570 abstract protected function getCoreUpdateList();
573 * Append an SQL fragment to the open file handle.
575 * @param string $filename File name to open
577 public function copyFile( $filename ) {
578 $this->db->sourceFile( $filename, false, false, false,
579 array( $this, 'appendLine' )
584 * Append a line to the open filehandle. The line is assumed to
585 * be a complete SQL statement.
587 * This is used as a callback for for sourceLine().
589 * @param string $line text to append to the file
590 * @return Boolean false to skip actually executing the file
591 * @throws MWException
593 public function appendLine( $line ) {
594 $line = rtrim( $line ) . ";\n";
595 if ( fwrite( $this->fileHandle, $line ) === false ) {
596 throw new MWException( "trouble writing file" );
598 return false;
602 * Applies a SQL patch
604 * @param string $path Path to the patch file
605 * @param $isFullPath Boolean Whether to treat $path as a relative or not
606 * @param string $msg Description of the patch
607 * @return boolean false if patch is skipped.
609 protected function applyPatch( $path, $isFullPath = false, $msg = null ) {
610 if ( $msg === null ) {
611 $msg = "Applying $path patch";
613 if ( $this->skipSchema ) {
614 $this->output( "...skipping schema change ($msg).\n" );
615 return false;
618 $this->output( "$msg ..." );
620 if ( !$isFullPath ) {
621 $path = $this->db->patchPath( $path );
623 if ( $this->fileHandle !== null ) {
624 $this->copyFile( $path );
625 } else {
626 $this->db->sourceFile( $path );
628 $this->output( "done.\n" );
629 return true;
633 * Add a new table to the database
635 * @param string $name Name of the new table
636 * @param string $patch Path to the patch file
637 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
638 * @return Boolean false if this was skipped because schema changes are skipped
640 protected function addTable( $name, $patch, $fullpath = false ) {
641 if ( !$this->doTable( $name ) ) {
642 return true;
645 if ( $this->db->tableExists( $name, __METHOD__ ) ) {
646 $this->output( "...$name table already exists.\n" );
647 } else {
648 return $this->applyPatch( $patch, $fullpath, "Creating $name table" );
650 return true;
654 * Add a new field to an existing table
656 * @param string $table Name of the table to modify
657 * @param string $field Name of the new field
658 * @param string $patch Path to the patch file
659 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
660 * @return Boolean false if this was skipped because schema changes are skipped
662 protected function addField( $table, $field, $patch, $fullpath = false ) {
663 if ( !$this->doTable( $table ) ) {
664 return true;
667 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
668 $this->output( "...$table table does not exist, skipping new field patch.\n" );
669 } elseif ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
670 $this->output( "...have $field field in $table table.\n" );
671 } else {
672 return $this->applyPatch( $patch, $fullpath, "Adding $field field to table $table" );
674 return true;
678 * Add a new index to an existing table
680 * @param string $table Name of the table to modify
681 * @param string $index Name of the new index
682 * @param string $patch Path to the patch file
683 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
684 * @return Boolean false if this was skipped because schema changes are skipped
686 protected function addIndex( $table, $index, $patch, $fullpath = false ) {
687 if ( !$this->doTable( $table ) ) {
688 return true;
691 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
692 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
693 } elseif ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
694 $this->output( "...index $index already set on $table table.\n" );
695 } else {
696 return $this->applyPatch( $patch, $fullpath, "Adding index $index to table $table" );
698 return true;
702 * Drop a field from an existing table
704 * @param string $table Name of the table to modify
705 * @param string $field Name of the old field
706 * @param string $patch Path to the patch file
707 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
708 * @return Boolean false if this was skipped because schema changes are skipped
710 protected function dropField( $table, $field, $patch, $fullpath = false ) {
711 if ( !$this->doTable( $table ) ) {
712 return true;
715 if ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
716 return $this->applyPatch( $patch, $fullpath, "Table $table contains $field field. Dropping" );
717 } else {
718 $this->output( "...$table table does not contain $field field.\n" );
720 return true;
724 * Drop an index from an existing table
726 * @param string $table Name of the table to modify
727 * @param string $index Name of the index
728 * @param string $patch Path to the patch file
729 * @param $fullpath Boolean: Whether to treat $patch path as a relative or not
730 * @return Boolean false if this was skipped because schema changes are skipped
732 protected function dropIndex( $table, $index, $patch, $fullpath = false ) {
733 if ( !$this->doTable( $table ) ) {
734 return true;
737 if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
738 return $this->applyPatch( $patch, $fullpath, "Dropping $index index from table $table" );
739 } else {
740 $this->output( "...$index key doesn't exist.\n" );
742 return true;
746 * Rename an index from an existing table
748 * @param string $table Name of the table to modify
749 * @param string $oldIndex Old name of the index
750 * @param string $newIndex New name of the index
751 * @param $skipBothIndexExistWarning Boolean: Whether to warn if both the old and the new indexes exist.
752 * @param string $patch Path to the patch file
753 * @param $fullpath Boolean: Whether to treat $patch path as a relative or not
754 * @return Boolean false if this was skipped because schema changes are skipped
756 protected function renameIndex( $table, $oldIndex, $newIndex, $skipBothIndexExistWarning, $patch, $fullpath = false ) {
757 if ( !$this->doTable( $table ) ) {
758 return true;
761 // First requirement: the table must exist
762 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
763 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
764 return true;
767 // Second requirement: the new index must be missing
768 if ( $this->db->indexExists( $table, $newIndex, __METHOD__ ) ) {
769 $this->output( "...index $newIndex already set on $table table.\n" );
770 if ( !$skipBothIndexExistWarning && $this->db->indexExists( $table, $oldIndex, __METHOD__ ) ) {
771 $this->output( "...WARNING: $oldIndex still exists, despite it has been renamed into $newIndex (which also exists).\n" .
772 " $oldIndex should be manually removed if not needed anymore.\n" );
774 return true;
777 // Third requirement: the old index must exist
778 if ( !$this->db->indexExists( $table, $oldIndex, __METHOD__ ) ) {
779 $this->output( "...skipping: index $oldIndex doesn't exist.\n" );
780 return true;
783 // Requirements have been satisfied, patch can be applied
784 return $this->applyPatch( $patch, $fullpath, "Renaming index $oldIndex into $newIndex to table $table" );
788 * If the specified table exists, drop it, or execute the
789 * patch if one is provided.
791 * Public @since 1.20
793 * @param $table string
794 * @param $patch string|false
795 * @param $fullpath bool
796 * @return Boolean false if this was skipped because schema changes are skipped
798 public function dropTable( $table, $patch = false, $fullpath = false ) {
799 if ( !$this->doTable( $table ) ) {
800 return true;
803 if ( $this->db->tableExists( $table, __METHOD__ ) ) {
804 $msg = "Dropping table $table";
806 if ( $patch === false ) {
807 $this->output( "$msg ..." );
808 $this->db->dropTable( $table, __METHOD__ );
809 $this->output( "done.\n" );
811 else {
812 return $this->applyPatch( $patch, $fullpath, $msg );
814 } else {
815 $this->output( "...$table doesn't exist.\n" );
817 return true;
821 * Modify an existing field
823 * @param string $table name of the table to which the field belongs
824 * @param string $field name of the field to modify
825 * @param string $patch path to the patch file
826 * @param $fullpath Boolean: whether to treat $patch path as a relative or not
827 * @return Boolean false if this was skipped because schema changes are skipped
829 public function modifyField( $table, $field, $patch, $fullpath = false ) {
830 if ( !$this->doTable( $table ) ) {
831 return true;
834 $updateKey = "$table-$field-$patch";
835 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
836 $this->output( "...$table table does not exist, skipping modify field patch.\n" );
837 } elseif ( !$this->db->fieldExists( $table, $field, __METHOD__ ) ) {
838 $this->output( "...$field field does not exist in $table table, skipping modify field patch.\n" );
839 } elseif ( $this->updateRowExists( $updateKey ) ) {
840 $this->output( "...$field in table $table already modified by patch $patch.\n" );
841 } else {
842 $this->insertUpdateRow( $updateKey );
843 return $this->applyPatch( $patch, $fullpath, "Modifying $field field of table $table" );
845 return true;
849 * Purge the objectcache table
851 public function purgeCache() {
852 global $wgLocalisationCacheConf;
853 # We can't guarantee that the user will be able to use TRUNCATE,
854 # but we know that DELETE is available to us
855 $this->output( "Purging caches..." );
856 $this->db->delete( 'objectcache', '*', __METHOD__ );
857 if ( $wgLocalisationCacheConf['manualRecache'] ) {
858 $this->rebuildLocalisationCache();
860 MessageBlobStore::clear();
861 $this->output( "done.\n" );
865 * Check the site_stats table is not properly populated.
867 protected function checkStats() {
868 $this->output( "...site_stats is populated..." );
869 $row = $this->db->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__ );
870 if ( $row === false ) {
871 $this->output( "data is missing! rebuilding...\n" );
872 } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
873 $this->output( "missing ss_total_pages, rebuilding...\n" );
874 } else {
875 $this->output( "done.\n" );
876 return;
878 SiteStatsInit::doAllAndCommit( $this->db );
881 # Common updater functions
884 * Sets the number of active users in the site_stats table
886 protected function doActiveUsersInit() {
887 $activeUsers = $this->db->selectField( 'site_stats', 'ss_active_users', false, __METHOD__ );
888 if ( $activeUsers == -1 ) {
889 $activeUsers = $this->db->selectField( 'recentchanges',
890 'COUNT( DISTINCT rc_user_text )',
891 array( 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ), __METHOD__
893 $this->db->update( 'site_stats',
894 array( 'ss_active_users' => intval( $activeUsers ) ),
895 array( 'ss_row_id' => 1 ), __METHOD__, array( 'LIMIT' => 1 )
898 $this->output( "...ss_active_users user count set...\n" );
902 * Populates the log_user_text field in the logging table
904 protected function doLogUsertextPopulation() {
905 if ( !$this->updateRowExists( 'populate log_usertext' ) ) {
906 $this->output(
907 "Populating log_user_text field, printing progress markers. For large\n" .
908 "databases, you may want to hit Ctrl-C and do this manually with\n" .
909 "maintenance/populateLogUsertext.php.\n" );
911 $task = $this->maintenance->runChild( 'PopulateLogUsertext' );
912 $task->execute();
913 $this->output( "done.\n" );
918 * Migrate log params to new table and index for searching
920 protected function doLogSearchPopulation() {
921 if ( !$this->updateRowExists( 'populate log_search' ) ) {
922 $this->output(
923 "Populating log_search table, printing progress markers. For large\n" .
924 "databases, you may want to hit Ctrl-C and do this manually with\n" .
925 "maintenance/populateLogSearch.php.\n" );
927 $task = $this->maintenance->runChild( 'PopulateLogSearch' );
928 $task->execute();
929 $this->output( "done.\n" );
934 * Updates the timestamps in the transcache table
936 protected function doUpdateTranscacheField() {
937 if ( $this->updateRowExists( 'convert transcache field' ) ) {
938 $this->output( "...transcache tc_time already converted.\n" );
939 return true;
942 return $this->applyPatch( 'patch-tc-timestamp.sql', false,
943 "Converting tc_time from UNIX epoch to MediaWiki timestamp" );
947 * Update CategoryLinks collation
949 protected function doCollationUpdate() {
950 global $wgCategoryCollation;
951 if ( $this->db->fieldExists( 'categorylinks', 'cl_collation', __METHOD__ ) ) {
952 if ( $this->db->selectField(
953 'categorylinks',
954 'COUNT(*)',
955 'cl_collation != ' . $this->db->addQuotes( $wgCategoryCollation ),
956 __METHOD__
957 ) == 0 ) {
958 $this->output( "...collations up-to-date.\n" );
959 return;
962 $this->output( "Updating category collations..." );
963 $task = $this->maintenance->runChild( 'UpdateCollation' );
964 $task->execute();
965 $this->output( "...done.\n" );
970 * Migrates user options from the user table blob to user_properties
972 protected function doMigrateUserOptions() {
973 if ( $this->db->tableExists( 'user_properties' ) ) {
974 $cl = $this->maintenance->runChild( 'ConvertUserOptions', 'convertUserOptions.php' );
975 $cl->execute();
976 $this->output( "done.\n" );
981 * Rebuilds the localisation cache
983 protected function rebuildLocalisationCache() {
985 * @var $cl RebuildLocalisationCache
987 $cl = $this->maintenance->runChild( 'RebuildLocalisationCache', 'rebuildLocalisationCache.php' );
988 $this->output( "Rebuilding localisation cache...\n" );
989 $cl->setForce();
990 $cl->execute();
991 $this->output( "done.\n" );