3 * DBMS-specific updater helper.
9 require_once( dirname(__FILE__
) . '/../../maintenance/Maintenance.php' );
12 * Class for handling database updates. Roughly based off of updaters.inc, with
13 * a few improvements :)
18 abstract class DatabaseUpdater
{
21 * Array of updates to perform on the database
25 protected $updates = array();
27 protected $extensionUpdates = array();
30 * Handle to the database subclass
36 protected $shared = false;
38 protected $postDatabaseUpdateMaintenance = array(
39 'DeleteDefaultMessages'
45 * @param $db DatabaseBase object to perform updates on
46 * @param $shared bool Whether to perform updates on shared tables
47 * @param $maintenance Maintenance Maintenance object which created us
49 protected function __construct( DatabaseBase
&$db, $shared, Maintenance
$maintenance = null ) {
51 $this->shared
= $shared;
53 $this->maintenance
= $maintenance;
55 $this->maintenance
= new FakeMaintenance
;
57 $this->initOldGlobals();
58 wfRunHooks( 'LoadExtensionSchemaUpdates', array( $this ) );
62 * Initialize all of the old globals. One day this should all become
63 * something much nicer
65 private function initOldGlobals() {
66 global $wgExtNewTables, $wgExtNewFields, $wgExtPGNewFields,
67 $wgExtPGAlteredFields, $wgExtNewIndexes, $wgExtModifiedFields;
69 # For extensions only, should be populated via hooks
70 # $wgDBtype should be checked to specifiy the proper file
71 $wgExtNewTables = array(); // table, dir
72 $wgExtNewFields = array(); // table, column, dir
73 $wgExtPGNewFields = array(); // table, column, column attributes; for PostgreSQL
74 $wgExtPGAlteredFields = array(); // table, column, new type, conversion method; for PostgreSQL
75 $wgExtNewIndexes = array(); // table, index, dir
76 $wgExtModifiedFields = array(); // table, index, dir
79 public static function newForDB( &$db, $shared = false, $maintenance = null ) {
80 $type = $db->getType();
81 if( in_array( $type, Installer
::getDBTypes() ) ) {
82 $class = ucfirst( $type ) . 'Updater';
83 return new $class( $db, $shared, $maintenance );
85 throw new MWException( __METHOD__
. ' called for unsupported $wgDBtype' );
90 * Get a database connection to run updates
92 * @return DatabasBase object
94 public function getDB() {
99 * Output some text. If we're running from web, escape the text first.
101 * @param $str String: Text to output
103 public function output( $str ) {
104 if ( $this->maintenance
->isQuiet() ) {
107 global $wgCommandLineMode;
108 if( !$wgCommandLineMode ) {
109 $str = htmlspecialchars( $str );
116 * Add a new update coming from an extension. This should be called by
117 * extensions while executing the LoadExtensionSchemaUpdates hook.
119 * @param $update Array: the update to run. Format is the following:
120 * first item is the callback function, it also can be a
121 * simple string with the name of a function in this class,
122 * following elements are parameters to the function.
123 * Note that callback functions will recieve this object as
126 public function addExtensionUpdate( Array $update ) {
127 $this->extensionUpdates
[] = $update;
131 * Get the list of extension-defined updates
135 protected function getExtensionUpdates() {
136 return $this->extensionUpdates
;
139 public function getPostDatabaseUpdateMaintenance() {
140 return $this->postDatabaseUpdateMaintenance
;
146 * @param $purge Boolean: whether to clear the objectcache table after updates
148 public function doUpdates( $purge = true ) {
151 if ( !defined( 'MW_NO_SETUP' ) ) {
152 define( 'MW_NO_SETUP', true );
155 $this->runUpdates( $this->getCoreUpdateList(), false );
156 $this->runUpdates( $this->getOldGlobalUpdates(), false );
157 $this->runUpdates( $this->getExtensionUpdates(), true );
159 $this->setAppliedUpdates( $wgVersion, $this->updates
);
168 * Helper function for doUpdates()
170 * @param $updates Array of updates to run
171 * @param $passSelf Boolean: whether to pass this object we calling external
174 private function runUpdates( array $updates, $passSelf ) {
175 foreach ( $updates as $params ) {
176 $func = array_shift( $params );
177 if( !is_array( $func ) && method_exists( $this, $func ) ) {
178 $func = array( $this, $func );
179 } elseif ( $passSelf ) {
180 array_unshift( $params, $this );
182 call_user_func_array( $func, $params );
185 $this->updates
= array_merge( $this->updates
, $updates );
188 protected function setAppliedUpdates( $version, $updates = array() ) {
189 if( !$this->canUseNewUpdatelog() ) {
192 $key = "updatelist-$version-" . time();
193 $this->db
->insert( 'updatelog',
194 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
199 * Helper function: check if the given key is present in the updatelog table.
200 * Obviously, only use this for updates that occur after the updatelog table was
203 public function updateRowExists( $key ) {
204 $row = $this->db
->selectRow(
207 array( 'ul_key' => $key ),
214 * Updatelog was changed in 1.17 to have a ul_value column so we can record
215 * more information about what kind of updates we've done (that's what this
216 * class does). Pre-1.17 wikis won't have this column, and really old wikis
217 * might not even have updatelog at all
221 protected function canUseNewUpdatelog() {
222 return $this->db
->tableExists( 'updatelog' ) &&
223 $this->db
->fieldExists( 'updatelog', 'ul_value' );
227 * Before 1.17, we used to handle updates via stuff like
228 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
229 * of this in 1.17 but we want to remain back-compatible for a while. So
230 * load up these old global-based things into our update list.
232 protected function getOldGlobalUpdates() {
233 global $wgExtNewFields, $wgExtNewTables, $wgExtModifiedFields,
234 $wgExtNewIndexes, $wgSharedDB, $wgSharedTables;
236 $doUser = $this->shared ?
237 $wgSharedDB && in_array( 'user', $wgSharedTables ) :
238 !$wgSharedDB ||
!in_array( 'user', $wgSharedTables );
242 foreach ( $wgExtNewTables as $tableRecord ) {
244 'addTable', $tableRecord[0], $tableRecord[1], true
248 foreach ( $wgExtNewFields as $fieldRecord ) {
249 if ( $fieldRecord[0] != 'user' ||
$doUser ) {
251 'addField', $fieldRecord[0], $fieldRecord[1],
252 $fieldRecord[2], true
257 foreach ( $wgExtNewIndexes as $fieldRecord ) {
259 'addIndex', $fieldRecord[0], $fieldRecord[1],
260 $fieldRecord[2], true
264 foreach ( $wgExtModifiedFields as $fieldRecord ) {
266 'modifyField', $fieldRecord[0], $fieldRecord[1],
267 $fieldRecord[2], true
275 * Get an array of updates to perform on the database. Should return a
276 * multi-dimensional array. The main key is the MediaWiki version (1.12,
277 * 1.13...) with the values being arrays of updates, identical to how
278 * updaters.inc did it (for now)
282 protected abstract function getCoreUpdateList();
285 * Applies a SQL patch
286 * @param $path String Path to the patch file
287 * @param $isFullPath Boolean Whether to treat $path as a relative or not
289 protected function applyPatch( $path, $isFullPath = false ) {
291 $this->db
->sourceFile( $path );
293 $this->db
->sourceFile( $this->db
->patchPath( $path ) );
298 * Add a new table to the database
299 * @param $name String Name of the new table
300 * @param $patch String Path to the patch file
301 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
303 protected function addTable( $name, $patch, $fullpath = false ) {
304 if ( $this->db
->tableExists( $name ) ) {
305 $this->output( "...$name table already exists.\n" );
307 $this->output( "Creating $name table..." );
308 $this->applyPatch( $patch, $fullpath );
309 $this->output( "ok\n" );
314 * Add a new field to an existing table
315 * @param $table String Name of the table to modify
316 * @param $field String Name of the new field
317 * @param $patch String Path to the patch file
318 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
320 protected function addField( $table, $field, $patch, $fullpath = false ) {
321 if ( !$this->db
->tableExists( $table ) ) {
322 $this->output( "...$table table does not exist, skipping new field patch\n" );
323 } elseif ( $this->db
->fieldExists( $table, $field ) ) {
324 $this->output( "...have $field field in $table table.\n" );
326 $this->output( "Adding $field field to table $table..." );
327 $this->applyPatch( $patch, $fullpath );
328 $this->output( "ok\n" );
333 * Add a new index to an existing table
334 * @param $table String Name of the table to modify
335 * @param $index String Name of the new index
336 * @param $patch String Path to the patch file
337 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
339 protected function addIndex( $table, $index, $patch, $fullpath = false ) {
340 if ( $this->db
->indexExists( $table, $index ) ) {
341 $this->output( "...$index key already set on $table table.\n" );
343 $this->output( "Adding $index key to table $table... " );
344 $this->applyPatch( $patch, $fullpath );
345 $this->output( "ok\n" );
350 * Drop a field from an existing table
352 * @param $table String Name of the table to modify
353 * @param $field String Name of the old field
354 * @param $patch String Path to the patch file
355 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
357 protected function dropField( $table, $field, $patch, $fullpath = false ) {
358 if ( $this->db
->fieldExists( $table, $field ) ) {
359 $this->output( "Table $table contains $field field. Dropping... " );
360 $this->applyPatch( $patch, $fullpath );
361 $this->output( "ok\n" );
363 $this->output( "...$table table does not contain $field field.\n" );
368 * Drop an index from an existing table
370 * @param $table String: Name of the table to modify
371 * @param $index String: Name of the old index
372 * @param $patch String: Path to the patch file
373 * @param $fullpath Boolean: Whether to treat $patch path as a relative or not
375 protected function dropIndex( $table, $index, $patch, $fullpath = false ) {
376 if ( $this->db
->indexExists( $table, $index ) ) {
377 $this->output( "Dropping $index from table $table... " );
378 $this->applyPatch( $patch, $fullpath );
379 $this->output( "ok\n" );
381 $this->output( "...$index key doesn't exist.\n" );
386 * Modify an existing field
388 * @param $table String: name of the table to which the field belongs
389 * @param $field String: name of the field to modify
390 * @param $patch String: path to the patch file
391 * @param $fullpath Boolean: whether to treat $patch path as a relative or not
393 public function modifyField( $table, $field, $patch, $fullpath = false ) {
394 if ( !$this->db
->tableExists( $table ) ) {
395 $this->output( "...$table table does not exist, skipping modify field patch\n" );
396 } elseif ( !$this->db
->fieldExists( $table, $field ) ) {
397 $this->output( "...$field field does not exist in $table table, skipping modify field patch\n" );
399 $this->output( "Modifying $field field of table $table..." );
400 $this->applyPatch( $patch, $fullpath );
401 $this->output( "ok\n" );
406 * Purge the objectcache table
408 protected function purgeCache() {
409 # We can't guarantee that the user will be able to use TRUNCATE,
410 # but we know that DELETE is available to us
411 $this->output( "Purging caches..." );
412 $this->db
->delete( 'objectcache', '*', __METHOD__
);
413 $this->output( "done.\n" );
417 * Check the site_stats table is not properly populated.
419 protected function checkStats() {
420 $this->output( "Checking site_stats row..." );
421 $row = $this->db
->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__
);
422 if ( $row === false ) {
423 $this->output( "data is missing! rebuilding...\n" );
424 } elseif ( isset( $row->site_stats
) && $row->ss_total_pages
== -1 ) {
425 $this->output( "missing ss_total_pages, rebuilding...\n" );
427 $this->output( "done.\n" );
430 SiteStatsInit
::doAllAndCommit( false );
433 # Common updater functions
435 protected function doActiveUsersInit() {
436 $activeUsers = $this->db
->selectField( 'site_stats', 'ss_active_users', false, __METHOD__
);
437 if ( $activeUsers == -1 ) {
438 $activeUsers = $this->db
->selectField( 'recentchanges',
439 'COUNT( DISTINCT rc_user_text )',
440 array( 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ), __METHOD__
442 $this->db
->update( 'site_stats',
443 array( 'ss_active_users' => intval( $activeUsers ) ),
444 array( 'ss_row_id' => 1 ), __METHOD__
, array( 'LIMIT' => 1 )
447 $this->output( "...ss_active_users user count set...\n" );
450 protected function doLogSearchPopulation() {
451 if ( $this->updateRowExists( 'populate log_search' ) ) {
452 $this->output( "...log_search table already populated.\n" );
457 "Populating log_search table, printing progress markers. For large\n" .
458 "databases, you may want to hit Ctrl-C and do this manually with\n" .
459 "maintenance/populateLogSearch.php.\n" );
460 $task = new PopulateLogSearch();
462 $this->output( "Done populating log_search table.\n" );
465 protected function doUpdateTranscacheField() {
466 if ( $this->updateRowExists( 'convert transcache field' ) ) {
467 $this->output( "...transcache tc_time already converted.\n" );
471 $this->output( "Converting tc_time from UNIX epoch to MediaWiki timestamp... " );
472 $this->applyPatch( 'patch-tc-timestamp.sql' );
473 $this->output( "ok\n" );
476 protected function doCollationUpdate() {
477 global $wgCategoryCollation;
478 if ( $this->db
->selectField(
481 'cl_collation != ' . $this->db
->addQuotes( $wgCategoryCollation ),
484 $this->output( "...collations up-to-date.\n" );
488 $task = new UpdateCollation();