diff options
Diffstat (limited to 'includes/installer/DatabaseUpdater.php')
-rw-r--r-- | includes/installer/DatabaseUpdater.php | 449 |
1 files changed, 368 insertions, 81 deletions
diff --git a/includes/installer/DatabaseUpdater.php b/includes/installer/DatabaseUpdater.php index ff0a99e9..25f751c7 100644 --- a/includes/installer/DatabaseUpdater.php +++ b/includes/installer/DatabaseUpdater.php @@ -40,6 +40,13 @@ abstract class DatabaseUpdater { protected $updates = array(); /** + * Array of updates that were skipped + * + * @var array + */ + protected $updatesSkipped = array(); + + /** * List of extension-provided database updates * @var array */ @@ -54,19 +61,43 @@ abstract class DatabaseUpdater { protected $shared = false; + /** + * Scripts to run after database update + * Should be a subclass of LoggedUpdateMaintenance + */ protected $postDatabaseUpdateMaintenance = array( 'DeleteDefaultMessages', 'PopulateRevisionLength', 'PopulateRevisionSha1', 'PopulateImageSha1', 'FixExtLinksProtocolRelative', + 'PopulateFilearchiveSha1', ); /** + * File handle for SQL output. + * + * @var Filehandle + */ + protected $fileHandle = null; + + /** + * Flag specifying whether or not to skip schema (e.g. SQL-only) updates. + * + * @var bool + */ + protected $skipSchema = false; + + /** + * Hold the value of $wgContentHandlerUseDB during the upgrade. + */ + protected $wgContentHandlerUseDB = true; + + /** * Constructor * * @param $db DatabaseBase object to perform updates on - * @param $shared bool Whether to perform updates on shared tables + * @param bool $shared Whether to perform updates on shared tables * @param $maintenance Maintenance Maintenance object which created us */ protected function __construct( DatabaseBase &$db, $shared, Maintenance $maintenance = null ) { @@ -75,6 +106,7 @@ abstract class DatabaseUpdater { $this->shared = $shared; if ( $maintenance ) { $this->maintenance = $maintenance; + $this->fileHandle = $maintenance->fileHandle; } else { $this->maintenance = new FakeMaintenance; } @@ -150,7 +182,7 @@ abstract class DatabaseUpdater { /** * Output some text. If we're running from web, escape the text first. * - * @param $str String: Text to output + * @param string $str Text to output */ public function output( $str ) { if ( $this->maintenance->isQuiet() ) { @@ -170,14 +202,14 @@ abstract class DatabaseUpdater { * * @since 1.17 * - * @param $update Array: the update to run. Format is the following: + * @param array $update the update to run. Format is the following: * first item is the callback function, it also can be a * simple string with the name of a function in this class, * following elements are parameters to the function. * Note that callback functions will receive this object as * first parameter. */ - public function addExtensionUpdate( Array $update ) { + public function addExtensionUpdate( array $update ) { $this->extensionUpdates[] = $update; } @@ -187,8 +219,8 @@ abstract class DatabaseUpdater { * * @since 1.18 * - * @param $tableName String Name of table to create - * @param $sqlPath String Full path to the schema file + * @param string $tableName Name of table to create + * @param string $sqlPath Full path to the schema file */ public function addExtensionTable( $tableName, $sqlPath ) { $this->extensionUpdates[] = array( 'addTable', $tableName, $sqlPath, true ); @@ -230,6 +262,19 @@ abstract class DatabaseUpdater { } /** + * Drop an index from an extension table + * + * @since 1.21 + * + * @param string $tableName The table name + * @param string $indexName The index name + * @param string $sqlPath The path to the SQL change path + */ + public function dropExtensionIndex( $tableName, $indexName, $sqlPath ) { + $this->extensionUpdates[] = array( 'dropIndex', $tableName, $indexName, $sqlPath, true ); + } + + /** * * @since 1.20 * @@ -241,6 +286,32 @@ abstract class DatabaseUpdater { } /** + * Rename an index on an extension table + * + * @since 1.21 + * + * @param string $tableName The table name + * @param string $oldIndexName The old index name + * @param string $newIndexName The new index name + * @param $skipBothIndexExistWarning Boolean: Whether to warn if both the old and the new indexes exist. [facultative; by default, false] + * @param string $sqlPath The path to the SQL change path + */ + public function renameExtensionIndex( $tableName, $oldIndexName, $newIndexName, $sqlPath, $skipBothIndexExistWarning = false ) { + $this->extensionUpdates[] = array( 'renameIndex', $tableName, $oldIndexName, $newIndexName, $skipBothIndexExistWarning, $sqlPath, true ); + } + + /** + * @since 1.21 + * + * @param string $tableName The table name + * @param string $fieldName The field to be modified + * @param string $sqlPath The path to the SQL change path + */ + public function modifyExtensionField( $tableName, $fieldName, $sqlPath) { + $this->extensionUpdates[] = array( 'modifyField', $tableName, $fieldName, $sqlPath, true ); + } + + /** * * @since 1.20 * @@ -254,9 +325,11 @@ abstract class DatabaseUpdater { /** * Add a maintenance script to be run after the database updates are complete. * + * Script should subclass LoggedUpdateMaintenance + * * @since 1.19 * - * @param $class string Name of a Maintenance subclass + * @param string $class Name of a Maintenance subclass */ public function addPostDatabaseUpdateMaintenance( $class ) { $this->postDatabaseUpdateMaintenance[] = $class; @@ -281,15 +354,35 @@ abstract class DatabaseUpdater { } /** + * @since 1.21 + * + * Writes the schema updates desired to a file for the DB Admin to run. + */ + private function writeSchemaUpdateFile( $schemaUpdate = array() ) { + $updates = $this->updatesSkipped; + $this->updatesSkipped = array(); + + foreach( $updates as $funcList ) { + $func = $funcList[0]; + $arg = $funcList[1]; + $origParams = $funcList[2]; + call_user_func_array( $func, $arg ); + flush(); + $this->updatesSkipped[] = $origParams; + } + } + + /** * Do all the updates * - * @param $what Array: what updates to perform + * @param array $what what updates to perform */ - public function doUpdates( $what = array( 'core', 'extensions', 'purge', 'stats' ) ) { - global $wgLocalisationCacheConf, $wgVersion; + public function doUpdates( $what = array( 'core', 'extensions', 'stats' ) ) { + global $wgVersion, $wgLocalisationCacheConf; $this->db->begin( __METHOD__ ); $what = array_flip( $what ); + $this->skipSchema = isset( $what['noschema'] ) || $this->fileHandle !== null; if ( isset( $what['core'] ) ) { $this->runUpdates( $this->getCoreUpdateList(), false ); } @@ -298,8 +391,6 @@ abstract class DatabaseUpdater { $this->runUpdates( $this->getExtensionUpdates(), true ); } - $this->setAppliedUpdates( $wgVersion, $this->updates ); - if ( isset( $what['stats'] ) ) { $this->checkStats(); } @@ -311,28 +402,46 @@ abstract class DatabaseUpdater { $this->rebuildLocalisationCache(); } } + + $this->setAppliedUpdates( $wgVersion, $this->updates ); + + if( $this->fileHandle ) { + $this->skipSchema = false; + $this->writeSchemaUpdateFile(); + $this->setAppliedUpdates( "$wgVersion-schema", $this->updatesSkipped ); + } + $this->db->commit( __METHOD__ ); } /** * Helper function for doUpdates() * - * @param $updates Array of updates to run + * @param array $updates of updates to run * @param $passSelf Boolean: whether to pass this object we calling external * functions */ private function runUpdates( array $updates, $passSelf ) { + $updatesDone = array(); + $updatesSkipped = array(); foreach ( $updates as $params ) { + $origParams = $params; $func = array_shift( $params ); if( !is_array( $func ) && method_exists( $this, $func ) ) { $func = array( $this, $func ); } elseif ( $passSelf ) { array_unshift( $params, $this ); } - call_user_func_array( $func, $params ); + $ret = call_user_func_array( $func, $params ); flush(); + if( $ret !== false ) { + $updatesDone[] = $origParams; + } else { + $updatesSkipped[] = array( $func, $params, $origParams ); + } } - $this->updates = array_merge( $this->updates, $updates ); + $this->updatesSkipped = array_merge( $this->updatesSkipped, $updatesSkipped ); + $this->updates = array_merge( $this->updates, $updatesDone ); } /** @@ -347,7 +456,7 @@ abstract class DatabaseUpdater { $key = "updatelist-$version-" . time(); $this->db->insert( 'updatelog', array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ), - __METHOD__ ); + __METHOD__ ); $this->db->setFlag( DBO_DDLMODE ); } @@ -355,7 +464,7 @@ abstract class DatabaseUpdater { * Helper function: check if the given key is present in the updatelog table. * Obviously, only use this for updates that occur after the updatelog table was * created! - * @param $key String Name of the key to check for + * @param string $key Name of the key to check for * * @return bool */ @@ -373,8 +482,8 @@ abstract class DatabaseUpdater { * Helper function: Add a key to the updatelog table * Obviously, only use this for updates that occur after the updatelog table was * created! - * @param $key String Name of key to insert - * @param $val String [optional] value to insert along with the key + * @param string $key Name of key to insert + * @param string $val [optional] value to insert along with the key */ public function insertUpdateRow( $key, $val = null ) { $this->db->clearFlag( DBO_DDLMODE ); @@ -400,6 +509,26 @@ abstract class DatabaseUpdater { } /** + * Returns whether updates should be executed on the database table $name. + * Updates will be prevented if the table is a shared table and it is not + * specified to run updates on shared tables. + * + * @param string $name table name + * @return bool + */ + protected function doTable( $name ) { + global $wgSharedDB, $wgSharedTables; + + // Don't bother to check $wgSharedTables if there isn't a shared database + // or the user actually also wants to do updates on the shared database. + if ( $wgSharedDB === null || $this->shared ) { + return true; + } + + return !in_array( $name, $wgSharedTables ); + } + + /** * Before 1.17, we used to handle updates via stuff like * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot * of this in 1.17 but we want to remain back-compatible for a while. So @@ -409,11 +538,7 @@ abstract class DatabaseUpdater { */ protected function getOldGlobalUpdates() { global $wgExtNewFields, $wgExtNewTables, $wgExtModifiedFields, - $wgExtNewIndexes, $wgSharedDB, $wgSharedTables; - - $doUser = $this->shared ? - $wgSharedDB && in_array( 'user', $wgSharedTables ) : - !$wgSharedDB || !in_array( 'user', $wgSharedTables ); + $wgExtNewIndexes; $updates = array(); @@ -424,12 +549,10 @@ abstract class DatabaseUpdater { } foreach ( $wgExtNewFields as $fieldRecord ) { - if ( $fieldRecord[0] != 'user' || $doUser ) { - $updates[] = array( - 'addField', $fieldRecord[0], $fieldRecord[1], - $fieldRecord[2], true - ); - } + $updates[] = array( + 'addField', $fieldRecord[0], $fieldRecord[1], + $fieldRecord[2], true + ); } foreach ( $wgExtNewIndexes as $fieldRecord ) { @@ -457,104 +580,221 @@ abstract class DatabaseUpdater { * * @return Array */ - protected abstract function getCoreUpdateList(); + abstract protected function getCoreUpdateList(); + + /** + * Append an SQL fragment to the open file handle. + * + * @param string $filename File name to open + */ + public function copyFile( $filename ) { + $this->db->sourceFile( $filename, false, false, false, + array( $this, 'appendLine' ) + ); + } + + /** + * Append a line to the open filehandle. The line is assumed to + * be a complete SQL statement. + * + * This is used as a callback for for sourceLine(). + * + * @param string $line text to append to the file + * @return Boolean false to skip actually executing the file + * @throws MWException + */ + public function appendLine( $line ) { + $line = rtrim( $line ) . ";\n"; + if( fwrite( $this->fileHandle, $line ) === false ) { + throw new MWException( "trouble writing file" ); + } + return false; + } /** * Applies a SQL patch - * @param $path String Path to the patch file + * + * @param string $path Path to the patch file * @param $isFullPath Boolean Whether to treat $path as a relative or not - * @param $msg String Description of the patch + * @param string $msg Description of the patch + * @return boolean false if patch is skipped. */ protected function applyPatch( $path, $isFullPath = false, $msg = null ) { if ( $msg === null ) { $msg = "Applying $path patch"; } + if ( $this->skipSchema ) { + $this->output( "...skipping schema change ($msg).\n" ); + return false; + } + + $this->output( "$msg ..." ); if ( !$isFullPath ) { $path = $this->db->patchPath( $path ); } - - $this->output( "$msg ..." ); - $this->db->sourceFile( $path ); + if( $this->fileHandle !== null ) { + $this->copyFile( $path ); + } else { + $this->db->sourceFile( $path ); + } $this->output( "done.\n" ); + return true; } /** * Add a new table to the database - * @param $name String Name of the new table - * @param $patch String Path to the patch file + * + * @param string $name Name of the new table + * @param string $patch Path to the patch file * @param $fullpath Boolean Whether to treat $patch path as a relative or not + * @return Boolean false if this was skipped because schema changes are skipped */ protected function addTable( $name, $patch, $fullpath = false ) { + if ( !$this->doTable( $name ) ) { + return true; + } + if ( $this->db->tableExists( $name, __METHOD__ ) ) { $this->output( "...$name table already exists.\n" ); } else { - $this->applyPatch( $patch, $fullpath, "Creating $name table" ); + return $this->applyPatch( $patch, $fullpath, "Creating $name table" ); } + return true; } /** * Add a new field to an existing table - * @param $table String Name of the table to modify - * @param $field String Name of the new field - * @param $patch String Path to the patch file + * + * @param string $table Name of the table to modify + * @param string $field Name of the new field + * @param string $patch Path to the patch file * @param $fullpath Boolean Whether to treat $patch path as a relative or not + * @return Boolean false if this was skipped because schema changes are skipped */ protected function addField( $table, $field, $patch, $fullpath = false ) { + if ( !$this->doTable( $table ) ) { + return true; + } + if ( !$this->db->tableExists( $table, __METHOD__ ) ) { $this->output( "...$table table does not exist, skipping new field patch.\n" ); } elseif ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) { $this->output( "...have $field field in $table table.\n" ); } else { - $this->applyPatch( $patch, $fullpath, "Adding $field field to table $table" ); + return $this->applyPatch( $patch, $fullpath, "Adding $field field to table $table" ); } + return true; } /** * Add a new index to an existing table - * @param $table String Name of the table to modify - * @param $index String Name of the new index - * @param $patch String Path to the patch file + * + * @param string $table Name of the table to modify + * @param string $index Name of the new index + * @param string $patch Path to the patch file * @param $fullpath Boolean Whether to treat $patch path as a relative or not + * @return Boolean false if this was skipped because schema changes are skipped */ protected function addIndex( $table, $index, $patch, $fullpath = false ) { - if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) { + if ( !$this->doTable( $table ) ) { + return true; + } + + if ( !$this->db->tableExists( $table, __METHOD__ ) ) { + $this->output( "...skipping: '$table' table doesn't exist yet.\n" ); + } else if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) { $this->output( "...index $index already set on $table table.\n" ); } else { - $this->applyPatch( $patch, $fullpath, "Adding index $index to table $table" ); + return $this->applyPatch( $patch, $fullpath, "Adding index $index to table $table" ); } + return true; } /** * Drop a field from an existing table * - * @param $table String Name of the table to modify - * @param $field String Name of the old field - * @param $patch String Path to the patch file + * @param string $table Name of the table to modify + * @param string $field Name of the old field + * @param string $patch Path to the patch file * @param $fullpath Boolean Whether to treat $patch path as a relative or not + * @return Boolean false if this was skipped because schema changes are skipped */ protected function dropField( $table, $field, $patch, $fullpath = false ) { + if ( !$this->doTable( $table ) ) { + return true; + } + if ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) { - $this->applyPatch( $patch, $fullpath, "Table $table contains $field field. Dropping" ); + return $this->applyPatch( $patch, $fullpath, "Table $table contains $field field. Dropping" ); } else { $this->output( "...$table table does not contain $field field.\n" ); } + return true; } /** * Drop an index from an existing table * - * @param $table String: Name of the table to modify - * @param $index String: Name of the old index - * @param $patch String: Path to the patch file + * @param string $table Name of the table to modify + * @param string $index Name of the index + * @param string $patch Path to the patch file * @param $fullpath Boolean: Whether to treat $patch path as a relative or not + * @return Boolean false if this was skipped because schema changes are skipped */ protected function dropIndex( $table, $index, $patch, $fullpath = false ) { + if ( !$this->doTable( $table ) ) { + return true; + } + if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) { - $this->applyPatch( $patch, $fullpath, "Dropping $index index from table $table" ); + return $this->applyPatch( $patch, $fullpath, "Dropping $index index from table $table" ); } else { $this->output( "...$index key doesn't exist.\n" ); } + return true; + } + + /** + * Rename an index from an existing table + * + * @param string $table Name of the table to modify + * @param string $oldIndex Old name of the index + * @param string $newIndex New name of the index + * @param $skipBothIndexExistWarning Boolean: Whether to warn if both the old and the new indexes exist. + * @param string $patch Path to the patch file + * @param $fullpath Boolean: Whether to treat $patch path as a relative or not + * @return Boolean false if this was skipped because schema changes are skipped + */ + protected function renameIndex( $table, $oldIndex, $newIndex, $skipBothIndexExistWarning, $patch, $fullpath = false ) { + if ( !$this->doTable( $table ) ) { + return true; + } + + // First requirement: the table must exist + if ( !$this->db->tableExists( $table, __METHOD__ ) ) { + $this->output( "...skipping: '$table' table doesn't exist yet.\n" ); + return true; + } + + // Second requirement: the new index must be missing + if ( $this->db->indexExists( $table, $newIndex, __METHOD__ ) ) { + $this->output( "...index $newIndex already set on $table table.\n" ); + if ( !$skipBothIndexExistWarning && $this->db->indexExists( $table, $oldIndex, __METHOD__ ) ) { + $this->output( "...WARNING: $oldIndex still exists, despite it has been renamed into $newIndex (which also exists).\n" . + " $oldIndex should be manually removed if not needed anymore.\n" ); + } + return true; + } + + // Third requirement: the old index must exist + if ( !$this->db->indexExists( $table, $oldIndex, __METHOD__ ) ) { + $this->output( "...skipping: index $oldIndex doesn't exist.\n" ); + return true; + } + + // Requirements have been satisfied, patch can be applied + return $this->applyPatch( $patch, $fullpath, "Renaming index $oldIndex into $newIndex to table $table" ); } /** @@ -566,8 +806,13 @@ abstract class DatabaseUpdater { * @param $table string * @param $patch string|false * @param $fullpath bool + * @return Boolean false if this was skipped because schema changes are skipped */ public function dropTable( $table, $patch = false, $fullpath = false ) { + if ( !$this->doTable( $table ) ) { + return true; + } + if ( $this->db->tableExists( $table, __METHOD__ ) ) { $msg = "Dropping table $table"; @@ -577,23 +822,28 @@ abstract class DatabaseUpdater { $this->output( "done.\n" ); } else { - $this->applyPatch( $patch, $fullpath, $msg ); + return $this->applyPatch( $patch, $fullpath, $msg ); } - } else { $this->output( "...$table doesn't exist.\n" ); } + return true; } /** * Modify an existing field * - * @param $table String: name of the table to which the field belongs - * @param $field String: name of the field to modify - * @param $patch String: path to the patch file + * @param string $table name of the table to which the field belongs + * @param string $field name of the field to modify + * @param string $patch path to the patch file * @param $fullpath Boolean: whether to treat $patch path as a relative or not + * @return Boolean false if this was skipped because schema changes are skipped */ public function modifyField( $table, $field, $patch, $fullpath = false ) { + if ( !$this->doTable( $table ) ) { + return true; + } + $updateKey = "$table-$field-$patch"; if ( !$this->db->tableExists( $table, __METHOD__ ) ) { $this->output( "...$table table does not exist, skipping modify field patch.\n" ); @@ -602,19 +852,25 @@ abstract class DatabaseUpdater { } elseif( $this->updateRowExists( $updateKey ) ) { $this->output( "...$field in table $table already modified by patch $patch.\n" ); } else { - $this->applyPatch( $patch, $fullpath, "Modifying $field field of table $table" ); $this->insertUpdateRow( $updateKey ); + return $this->applyPatch( $patch, $fullpath, "Modifying $field field of table $table" ); } + return true; } /** * Purge the objectcache table */ - protected function purgeCache() { + public function purgeCache() { + global $wgLocalisationCacheConf; # We can't guarantee that the user will be able to use TRUNCATE, # but we know that DELETE is available to us $this->output( "Purging caches..." ); $this->db->delete( 'objectcache', '*', __METHOD__ ); + if ( $wgLocalisationCacheConf['manualRecache'] ) { + $this->rebuildLocalisationCache(); + } + MessageBlobStore::clear(); $this->output( "done.\n" ); } @@ -693,10 +949,11 @@ abstract class DatabaseUpdater { protected function doUpdateTranscacheField() { if ( $this->updateRowExists( 'convert transcache field' ) ) { $this->output( "...transcache tc_time already converted.\n" ); - return; + return true; } - $this->applyPatch( 'patch-tc-timestamp.sql', false, "Converting tc_time from UNIX epoch to MediaWiki timestamp" ); + return $this->applyPatch( 'patch-tc-timestamp.sql', false, + "Converting tc_time from UNIX epoch to MediaWiki timestamp" ); } /** @@ -704,29 +961,33 @@ abstract class DatabaseUpdater { */ protected function doCollationUpdate() { global $wgCategoryCollation; - if ( $this->db->selectField( - 'categorylinks', - 'COUNT(*)', - 'cl_collation != ' . $this->db->addQuotes( $wgCategoryCollation ), - __METHOD__ - ) == 0 ) { - $this->output( "...collations up-to-date.\n" ); - return; - } + if ( $this->db->fieldExists( 'categorylinks', 'cl_collation', __METHOD__ ) ) { + if ( $this->db->selectField( + 'categorylinks', + 'COUNT(*)', + 'cl_collation != ' . $this->db->addQuotes( $wgCategoryCollation ), + __METHOD__ + ) == 0 ) { + $this->output( "...collations up-to-date.\n" ); + return; + } - $this->output( "Updating category collations..." ); - $task = $this->maintenance->runChild( 'UpdateCollation' ); - $task->execute(); - $this->output( "...done.\n" ); + $this->output( "Updating category collations..." ); + $task = $this->maintenance->runChild( 'UpdateCollation' ); + $task->execute(); + $this->output( "...done.\n" ); + } } /** * Migrates user options from the user table blob to user_properties */ protected function doMigrateUserOptions() { - $cl = $this->maintenance->runChild( 'ConvertUserOptions', 'convertUserOptions.php' ); - $cl->execute(); - $this->output( "done.\n" ); + if( $this->db->tableExists( 'user_properties' ) ) { + $cl = $this->maintenance->runChild( 'ConvertUserOptions', 'convertUserOptions.php' ); + $cl->execute(); + $this->output( "done.\n" ); + } } /** @@ -742,4 +1003,30 @@ abstract class DatabaseUpdater { $cl->execute(); $this->output( "done.\n" ); } + + /** + * Turns off content handler fields during parts of the upgrade + * where they aren't available. + */ + protected function disableContentHandlerUseDB() { + global $wgContentHandlerUseDB; + + if( $wgContentHandlerUseDB ) { + $this->output( "Turning off Content Handler DB fields for this part of upgrade.\n" ); + $this->holdContentHandlerUseDB = $wgContentHandlerUseDB; + $wgContentHandlerUseDB = false; + } + } + + /** + * Turns content handler fields back on. + */ + protected function enableContentHandlerUseDB() { + global $wgContentHandlerUseDB; + + if( $this->holdContentHandlerUseDB ) { + $this->output( "Content Handler DB fields should be usable now.\n" ); + $wgContentHandlerUseDB = $this->holdContentHandlerUseDB; + } + } } |