MediaWiki  REL1_20
DatabaseUpdater.php
Go to the documentation of this file.
00001 <?php
00024 require_once( __DIR__ . '/../../maintenance/Maintenance.php' );
00025 
00033 abstract class DatabaseUpdater {
00034 
00040         protected $updates = array();
00041 
00046         protected $extensionUpdates = array();
00047 
00053         protected $db;
00054 
00055         protected $shared = false;
00056 
00057         protected $postDatabaseUpdateMaintenance = array(
00058                 'DeleteDefaultMessages',
00059                 'PopulateRevisionLength',
00060                 'PopulateRevisionSha1',
00061                 'PopulateImageSha1',
00062                 'FixExtLinksProtocolRelative',
00063         );
00064 
00072         protected function __construct( DatabaseBase &$db, $shared, Maintenance $maintenance = null ) {
00073                 $this->db = $db;
00074                 $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
00075                 $this->shared = $shared;
00076                 if ( $maintenance ) {
00077                         $this->maintenance = $maintenance;
00078                 } else {
00079                         $this->maintenance = new FakeMaintenance;
00080                 }
00081                 $this->maintenance->setDB( $db );
00082                 $this->initOldGlobals();
00083                 $this->loadExtensions();
00084                 wfRunHooks( 'LoadExtensionSchemaUpdates', array( $this ) );
00085         }
00086 
00091         private function initOldGlobals() {
00092                 global $wgExtNewTables, $wgExtNewFields, $wgExtPGNewFields,
00093                         $wgExtPGAlteredFields, $wgExtNewIndexes, $wgExtModifiedFields;
00094 
00095                 # For extensions only, should be populated via hooks
00096                 # $wgDBtype should be checked to specifiy the proper file
00097                 $wgExtNewTables = array(); // table, dir
00098                 $wgExtNewFields = array(); // table, column, dir
00099                 $wgExtPGNewFields = array(); // table, column, column attributes; for PostgreSQL
00100                 $wgExtPGAlteredFields = array(); // table, column, new type, conversion method; for PostgreSQL
00101                 $wgExtNewIndexes = array(); // table, index, dir
00102                 $wgExtModifiedFields = array(); // table, index, dir
00103         }
00104 
00108         private function loadExtensions() {
00109                 if ( !defined( 'MEDIAWIKI_INSTALL' ) ) {
00110                         return; // already loaded
00111                 }
00112                 $vars = Installer::getExistingLocalSettings();
00113                 if ( !$vars ) {
00114                         return; // no LocalSettings found
00115                 }
00116                 if ( !isset( $vars['wgHooks'] ) || !isset( $vars['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
00117                         return;
00118                 }
00119                 global $wgHooks, $wgAutoloadClasses;
00120                 $wgHooks['LoadExtensionSchemaUpdates'] = $vars['wgHooks']['LoadExtensionSchemaUpdates'];
00121                 $wgAutoloadClasses = $wgAutoloadClasses + $vars['wgAutoloadClasses'];
00122         }
00123 
00131         public static function newForDB( &$db, $shared = false, $maintenance = null ) {
00132                 $type = $db->getType();
00133                 if( in_array( $type, Installer::getDBTypes() ) ) {
00134                         $class = ucfirst( $type ) . 'Updater';
00135                         return new $class( $db, $shared, $maintenance );
00136                 } else {
00137                         throw new MWException( __METHOD__ . ' called for unsupported $wgDBtype' );
00138                 }
00139         }
00140 
00146         public function getDB() {
00147                 return $this->db;
00148         }
00149 
00155         public function output( $str ) {
00156                 if ( $this->maintenance->isQuiet() ) {
00157                         return;
00158                 }
00159                 global $wgCommandLineMode;
00160                 if( !$wgCommandLineMode ) {
00161                         $str = htmlspecialchars( $str );
00162                 }
00163                 echo $str;
00164                 flush();
00165         }
00166 
00180         public function addExtensionUpdate( Array $update ) {
00181                 $this->extensionUpdates[] = $update;
00182         }
00183 
00193         public function addExtensionTable( $tableName, $sqlPath ) {
00194                 $this->extensionUpdates[] = array( 'addTable', $tableName, $sqlPath, true );
00195         }
00196 
00204         public function addExtensionIndex( $tableName, $indexName, $sqlPath ) {
00205                 $this->extensionUpdates[] = array( 'addIndex', $tableName, $indexName, $sqlPath, true );
00206         }
00207 
00216         public function addExtensionField( $tableName, $columnName, $sqlPath ) {
00217                 $this->extensionUpdates[] = array( 'addField', $tableName, $columnName, $sqlPath, true );
00218         }
00219 
00228         public function dropExtensionField( $tableName, $columnName, $sqlPath ) {
00229                 $this->extensionUpdates[] = array( 'dropField', $tableName, $columnName, $sqlPath, true );
00230         }
00231 
00239         public function dropExtensionTable( $tableName, $sqlPath ) {
00240                 $this->extensionUpdates[] = array( 'dropTable', $tableName, $sqlPath, true );
00241         }
00242 
00250         public function tableExists( $tableName ) {
00251                 return ( $this->db->tableExists( $tableName, __METHOD__ ) );
00252         }
00253 
00261         public function addPostDatabaseUpdateMaintenance( $class ) {
00262                 $this->postDatabaseUpdateMaintenance[] = $class;
00263         }
00264 
00270         protected function getExtensionUpdates() {
00271                 return $this->extensionUpdates;
00272         }
00273 
00279         public function getPostDatabaseUpdateMaintenance() {
00280                 return $this->postDatabaseUpdateMaintenance;
00281         }
00282 
00288         public function doUpdates( $what = array( 'core', 'extensions', 'purge', 'stats' ) ) {
00289                 global $wgLocalisationCacheConf, $wgVersion;
00290 
00291                 $this->db->begin( __METHOD__ );
00292                 $what = array_flip( $what );
00293                 if ( isset( $what['core'] ) ) {
00294                         $this->runUpdates( $this->getCoreUpdateList(), false );
00295                 }
00296                 if ( isset( $what['extensions'] ) ) {
00297                         $this->runUpdates( $this->getOldGlobalUpdates(), false );
00298                         $this->runUpdates( $this->getExtensionUpdates(), true );
00299                 }
00300 
00301                 $this->setAppliedUpdates( $wgVersion, $this->updates );
00302 
00303                 if ( isset( $what['stats'] ) ) {
00304                         $this->checkStats();
00305                 }
00306 
00307                 if ( isset( $what['purge'] ) ) {
00308                         $this->purgeCache();
00309 
00310                         if ( $wgLocalisationCacheConf['manualRecache'] ) {
00311                                 $this->rebuildLocalisationCache();
00312                         }
00313                 }
00314                 $this->db->commit( __METHOD__ );
00315         }
00316 
00324         private function runUpdates( array $updates, $passSelf ) {
00325                 foreach ( $updates as $params ) {
00326                         $func = array_shift( $params );
00327                         if( !is_array( $func ) && method_exists( $this, $func ) ) {
00328                                 $func = array( $this, $func );
00329                         } elseif ( $passSelf ) {
00330                                 array_unshift( $params, $this );
00331                         }
00332                         call_user_func_array( $func, $params );
00333                         flush();
00334                 }
00335                 $this->updates = array_merge( $this->updates, $updates );
00336         }
00337 
00342         protected function setAppliedUpdates( $version, $updates = array() ) {
00343                 $this->db->clearFlag( DBO_DDLMODE );
00344                 if( !$this->canUseNewUpdatelog() ) {
00345                         return;
00346                 }
00347                 $key = "updatelist-$version-" . time();
00348                 $this->db->insert( 'updatelog',
00349                         array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
00350                          __METHOD__ );
00351                 $this->db->setFlag( DBO_DDLMODE );
00352         }
00353 
00362         public function updateRowExists( $key ) {
00363                 $row = $this->db->selectRow(
00364                         'updatelog',
00365                         '1',
00366                         array( 'ul_key' => $key ),
00367                         __METHOD__
00368                 );
00369                 return (bool)$row;
00370         }
00371 
00379         public function insertUpdateRow( $key, $val = null ) {
00380                 $this->db->clearFlag( DBO_DDLMODE );
00381                 $values = array( 'ul_key' => $key );
00382                 if( $val && $this->canUseNewUpdatelog() ) {
00383                         $values['ul_value'] = $val;
00384                 }
00385                 $this->db->insert( 'updatelog', $values, __METHOD__, 'IGNORE' );
00386                 $this->db->setFlag( DBO_DDLMODE );
00387         }
00388 
00397         protected function canUseNewUpdatelog() {
00398                 return $this->db->tableExists( 'updatelog', __METHOD__ ) &&
00399                         $this->db->fieldExists( 'updatelog', 'ul_value', __METHOD__ );
00400         }
00401 
00410         protected function getOldGlobalUpdates() {
00411                 global $wgExtNewFields, $wgExtNewTables, $wgExtModifiedFields,
00412                         $wgExtNewIndexes, $wgSharedDB, $wgSharedTables;
00413 
00414                 $doUser = $this->shared ?
00415                         $wgSharedDB && in_array( 'user', $wgSharedTables ) :
00416                         !$wgSharedDB || !in_array( 'user', $wgSharedTables );
00417 
00418                 $updates = array();
00419 
00420                 foreach ( $wgExtNewTables as $tableRecord ) {
00421                         $updates[] = array(
00422                                 'addTable', $tableRecord[0], $tableRecord[1], true
00423                         );
00424                 }
00425 
00426                 foreach ( $wgExtNewFields as $fieldRecord ) {
00427                         if ( $fieldRecord[0] != 'user' || $doUser ) {
00428                                 $updates[] = array(
00429                                         'addField', $fieldRecord[0], $fieldRecord[1],
00430                                                 $fieldRecord[2], true
00431                                 );
00432                         }
00433                 }
00434 
00435                 foreach ( $wgExtNewIndexes as $fieldRecord ) {
00436                         $updates[] = array(
00437                                 'addIndex', $fieldRecord[0], $fieldRecord[1],
00438                                         $fieldRecord[2], true
00439                         );
00440                 }
00441 
00442                 foreach ( $wgExtModifiedFields as $fieldRecord ) {
00443                         $updates[] = array(
00444                                 'modifyField', $fieldRecord[0], $fieldRecord[1],
00445                                         $fieldRecord[2], true
00446                         );
00447                 }
00448 
00449                 return $updates;
00450         }
00451 
00460         protected abstract function getCoreUpdateList();
00461 
00468         protected function applyPatch( $path, $isFullPath = false, $msg = null ) {
00469                 if ( $msg === null ) {
00470                         $msg = "Applying $path patch";
00471                 }
00472 
00473                 if ( !$isFullPath ) {
00474                         $path = $this->db->patchPath( $path );
00475                 }
00476 
00477                 $this->output( "$msg ..." );
00478                 $this->db->sourceFile( $path );
00479                 $this->output( "done.\n" );
00480         }
00481 
00488         protected function addTable( $name, $patch, $fullpath = false ) {
00489                 if ( $this->db->tableExists( $name, __METHOD__ ) ) {
00490                         $this->output( "...$name table already exists.\n" );
00491                 } else {
00492                         $this->applyPatch( $patch, $fullpath, "Creating $name table" );
00493                 }
00494         }
00495 
00503         protected function addField( $table, $field, $patch, $fullpath = false ) {
00504                 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
00505                         $this->output( "...$table table does not exist, skipping new field patch.\n" );
00506                 } elseif ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
00507                         $this->output( "...have $field field in $table table.\n" );
00508                 } else {
00509                         $this->applyPatch( $patch, $fullpath, "Adding $field field to table $table" );
00510                 }
00511         }
00512 
00520         protected function addIndex( $table, $index, $patch, $fullpath = false ) {
00521                 if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
00522                         $this->output( "...index $index already set on $table table.\n" );
00523                 } else {
00524                         $this->applyPatch( $patch, $fullpath, "Adding index $index to table $table" );
00525                 }
00526         }
00527 
00536         protected function dropField( $table, $field, $patch, $fullpath = false ) {
00537                 if ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
00538                         $this->applyPatch( $patch, $fullpath, "Table $table contains $field field. Dropping" );
00539                 } else {
00540                         $this->output( "...$table table does not contain $field field.\n" );
00541                 }
00542         }
00543 
00552         protected function dropIndex( $table, $index, $patch, $fullpath = false ) {
00553                 if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
00554                         $this->applyPatch( $patch, $fullpath, "Dropping $index index from table $table" );
00555                 } else {
00556                         $this->output( "...$index key doesn't exist.\n" );
00557                 }
00558         }
00559 
00570         public function dropTable( $table, $patch = false, $fullpath = false ) {
00571                 if ( $this->db->tableExists( $table, __METHOD__ ) ) {
00572                         $msg = "Dropping table $table";
00573 
00574                         if ( $patch === false ) {
00575                                 $this->output( "$msg ..." );
00576                                 $this->db->dropTable( $table, __METHOD__ );
00577                                 $this->output( "done.\n" );
00578                         }
00579                         else {
00580                                 $this->applyPatch( $patch, $fullpath, $msg );
00581                         }
00582 
00583                 } else {
00584                         $this->output( "...$table doesn't exist.\n" );
00585                 }
00586         }
00587 
00596         public function modifyField( $table, $field, $patch, $fullpath = false ) {
00597                 $updateKey = "$table-$field-$patch";
00598                 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
00599                         $this->output( "...$table table does not exist, skipping modify field patch.\n" );
00600                 } elseif ( !$this->db->fieldExists( $table, $field, __METHOD__ ) ) {
00601                         $this->output( "...$field field does not exist in $table table, skipping modify field patch.\n" );
00602                 } elseif( $this->updateRowExists( $updateKey ) ) {
00603                         $this->output( "...$field in table $table already modified by patch $patch.\n" );
00604                 } else {
00605                         $this->applyPatch( $patch, $fullpath, "Modifying $field field of table $table" );
00606                         $this->insertUpdateRow( $updateKey );
00607                 }
00608         }
00609 
00613         protected function purgeCache() {
00614                 # We can't guarantee that the user will be able to use TRUNCATE,
00615                 # but we know that DELETE is available to us
00616                 $this->output( "Purging caches..." );
00617                 $this->db->delete( 'objectcache', '*', __METHOD__ );
00618                 $this->output( "done.\n" );
00619         }
00620 
00624         protected function checkStats() {
00625                 $this->output( "...site_stats is populated..." );
00626                 $row = $this->db->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__ );
00627                 if ( $row === false ) {
00628                         $this->output( "data is missing! rebuilding...\n" );
00629                 } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
00630                         $this->output( "missing ss_total_pages, rebuilding...\n" );
00631                 } else {
00632                         $this->output( "done.\n" );
00633                         return;
00634                 }
00635                 SiteStatsInit::doAllAndCommit( $this->db );
00636         }
00637 
00638         # Common updater functions
00639 
00643         protected function doActiveUsersInit() {
00644                 $activeUsers = $this->db->selectField( 'site_stats', 'ss_active_users', false, __METHOD__ );
00645                 if ( $activeUsers == -1 ) {
00646                         $activeUsers = $this->db->selectField( 'recentchanges',
00647                                 'COUNT( DISTINCT rc_user_text )',
00648                                 array( 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ), __METHOD__
00649                         );
00650                         $this->db->update( 'site_stats',
00651                                 array( 'ss_active_users' => intval( $activeUsers ) ),
00652                                 array( 'ss_row_id' => 1 ), __METHOD__, array( 'LIMIT' => 1 )
00653                         );
00654                 }
00655                 $this->output( "...ss_active_users user count set...\n" );
00656         }
00657 
00661         protected function doLogUsertextPopulation() {
00662                 if ( !$this->updateRowExists( 'populate log_usertext' ) ) {
00663                         $this->output(
00664                         "Populating log_user_text field, printing progress markers. For large\n" .
00665                         "databases, you may want to hit Ctrl-C and do this manually with\n" .
00666                         "maintenance/populateLogUsertext.php.\n" );
00667 
00668                         $task = $this->maintenance->runChild( 'PopulateLogUsertext' );
00669                         $task->execute();
00670                         $this->output( "done.\n" );
00671                 }
00672         }
00673 
00677         protected function doLogSearchPopulation() {
00678                 if ( !$this->updateRowExists( 'populate log_search' ) ) {
00679                         $this->output(
00680                                 "Populating log_search table, printing progress markers. For large\n" .
00681                                 "databases, you may want to hit Ctrl-C and do this manually with\n" .
00682                                 "maintenance/populateLogSearch.php.\n" );
00683 
00684                         $task = $this->maintenance->runChild( 'PopulateLogSearch' );
00685                         $task->execute();
00686                         $this->output( "done.\n" );
00687                 }
00688         }
00689 
00693         protected function doUpdateTranscacheField() {
00694                 if ( $this->updateRowExists( 'convert transcache field' ) ) {
00695                         $this->output( "...transcache tc_time already converted.\n" );
00696                         return;
00697                 }
00698 
00699                 $this->applyPatch( 'patch-tc-timestamp.sql', false, "Converting tc_time from UNIX epoch to MediaWiki timestamp" );
00700         }
00701 
00705         protected function doCollationUpdate() {
00706                 global $wgCategoryCollation;
00707                 if ( $this->db->selectField(
00708                         'categorylinks',
00709                         'COUNT(*)',
00710                         'cl_collation != ' . $this->db->addQuotes( $wgCategoryCollation ),
00711                         __METHOD__
00712                 ) == 0 ) {
00713                         $this->output( "...collations up-to-date.\n" );
00714                         return;
00715                 }
00716 
00717                 $this->output( "Updating category collations..." );
00718                 $task = $this->maintenance->runChild( 'UpdateCollation' );
00719                 $task->execute();
00720                 $this->output( "...done.\n" );
00721         }
00722 
00726         protected function doMigrateUserOptions() {
00727                 $cl = $this->maintenance->runChild( 'ConvertUserOptions', 'convertUserOptions.php' );
00728                 $cl->execute();
00729                 $this->output( "done.\n" );
00730         }
00731 
00735         protected function rebuildLocalisationCache() {
00739                 $cl = $this->maintenance->runChild( 'RebuildLocalisationCache', 'rebuildLocalisationCache.php' );
00740                 $this->output( "Rebuilding localisation cache...\n" );
00741                 $cl->setForce();
00742                 $cl->execute();
00743                 $this->output( "done.\n" );
00744         }
00745 }