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();
31 protected $shared = false;
33 protected $postDatabaseUpdateMaintenance = array(
34 'DeleteDefaultMessages'
40 * @param $db DatabaseBase object to perform updates on
41 * @param $shared bool Whether to perform updates on shared tables
42 * @param $maintenance Maintenance Maintenance object which created us
44 protected function __construct( DatabaseBase
&$db, $shared, Maintenance
$maintenance = null ) {
46 $this->shared
= $shared;
48 $this->maintenance
= $maintenance;
50 $this->maintenance
= new FakeMaintenance
;
52 $this->initOldGlobals();
53 wfRunHooks( 'LoadExtensionSchemaUpdates', array( $this ) );
57 * Initialize all of the old globals. One day this should all become
58 * something much nicer
60 private function initOldGlobals() {
61 global $wgExtNewTables, $wgExtNewFields, $wgExtPGNewFields,
62 $wgExtPGAlteredFields, $wgExtNewIndexes, $wgExtModifiedFields;
64 # For extensions only, should be populated via hooks
65 # $wgDBtype should be checked to specifiy the proper file
66 $wgExtNewTables = array(); // table, dir
67 $wgExtNewFields = array(); // table, column, dir
68 $wgExtPGNewFields = array(); // table, column, column attributes; for PostgreSQL
69 $wgExtPGAlteredFields = array(); // table, column, new type, conversion method; for PostgreSQL
70 $wgExtNewIndexes = array(); // table, index, dir
71 $wgExtModifiedFields = array(); // table, index, dir
74 public static function newForDB( &$db, $shared = false, $maintenance = null ) {
75 $type = $db->getType();
76 if( in_array( $type, Installer
::getDBTypes() ) ) {
77 $class = ucfirst( $type ) . 'Updater';
78 return new $class( $db, $shared, $maintenance );
80 throw new MWException( __METHOD__
. ' called for unsupported $wgDBtype' );
85 * Get a database connection to run updates
87 * @return DatabasBase object
89 public function getDB() {
94 * Output some text. Right now this is a wrapper for wfOut, but hopefully
95 * that function can go away some day :)
97 * @param $str String: Text to output
99 protected function output( $str ) {
100 if ( $this->maintenance
->isQuiet() ) {
107 * Add a new update coming from an extension. This should be called by
108 * extensions while executing the LoadExtensionSchemaUpdates hook.
110 * @param $update Array: the update to run. Format is the following:
111 * first item is the callback function, it also can be a
112 * simple string with the name of a function in this class,
113 * following elements are parameters to the function.
114 * Note that callback functions will recieve this object as
117 public function addExtensionUpdate( Array $update ) {
118 $this->extensionUpdates
[] = $update;
122 * Get the list of extension-defined updates
126 protected function getExtensionUpdates() {
127 return $this->extensionUpdates
;
130 public function getPostDatabaseUpdateMaintenance() {
131 return $this->postDatabaseUpdateMaintenance
;
137 * @param $purge Boolean: whether to clear the objectcache table after updates
139 public function doUpdates( $purge = true ) {
142 if ( !defined( 'MW_NO_SETUP' ) ) {
143 define( 'MW_NO_SETUP', true );
146 $this->runUpdates( $this->getCoreUpdateList(), false );
147 $this->runUpdates( $this->getOldGlobalUpdates(), false );
148 $this->runUpdates( $this->getExtensionUpdates(), true );
150 $this->setAppliedUpdates( $wgVersion, $this->updates
);
159 * Helper function for doUpdates()
161 * @param $updates Array of updates to run
162 * @param $passSelf Boolean: whether to pass this object we calling external
165 private function runUpdates( array $updates, $passSelf ) {
166 foreach ( $updates as $params ) {
167 $func = array_shift( $params );
168 if( !is_array( $func ) && method_exists( $this, $func ) ) {
169 $func = array( $this, $func );
170 } elseif ( $passSelf ) {
171 array_unshift( $params, $this );
173 call_user_func_array( $func, $params );
176 $this->updates
= array_merge( $this->updates
, $updates );
179 protected function setAppliedUpdates( $version, $updates = array() ) {
180 if( !$this->canUseNewUpdatelog() ) {
183 $key = "updatelist-$version-" . time();
184 $this->db
->insert( 'updatelog',
185 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
190 * Helper function: check if the given key is present in the updatelog table.
191 * Obviously, only use this for updates that occur after the updatelog table was
194 public function updateRowExists( $key ) {
195 $row = $this->db
->selectRow(
198 array( 'ul_key' => $key ),
205 * Updatelog was changed in 1.17 to have a ul_value column so we can record
206 * more information about what kind of updates we've done (that's what this
207 * class does). Pre-1.17 wikis won't have this column, and really old wikis
208 * might not even have updatelog at all
212 protected function canUseNewUpdatelog() {
213 return $this->db
->tableExists( 'updatelog' ) &&
214 $this->db
->fieldExists( 'updatelog', 'ul_value' );
218 * Before 1.17, we used to handle updates via stuff like
219 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
220 * of this in 1.17 but we want to remain back-compatible for a while. So
221 * load up these old global-based things into our update list.
223 protected function getOldGlobalUpdates() {
224 global $wgExtNewFields, $wgExtNewTables, $wgExtModifiedFields,
225 $wgExtNewIndexes, $wgSharedDB, $wgSharedTables;
227 $doUser = $this->shared ?
228 $wgSharedDB && in_array( 'user', $wgSharedTables ) :
229 !$wgSharedDB ||
!in_array( 'user', $wgSharedTables );
233 foreach ( $wgExtNewTables as $tableRecord ) {
235 'addTable', $tableRecord[0], $tableRecord[1], true
239 foreach ( $wgExtNewFields as $fieldRecord ) {
240 if ( $fieldRecord[0] != 'user' ||
$doUser ) {
242 'addField', $fieldRecord[0], $fieldRecord[1],
243 $fieldRecord[2], true
248 foreach ( $wgExtNewIndexes as $fieldRecord ) {
250 'addIndex', $fieldRecord[0], $fieldRecord[1],
251 $fieldRecord[2], true
255 foreach ( $wgExtModifiedFields as $fieldRecord ) {
257 'modifyField', $fieldRecord[0], $fieldRecord[1],
258 $fieldRecord[2], true
266 * Get an array of updates to perform on the database. Should return a
267 * multi-dimensional array. The main key is the MediaWiki version (1.12,
268 * 1.13...) with the values being arrays of updates, identical to how
269 * updaters.inc did it (for now)
273 protected abstract function getCoreUpdateList();
276 * Applies a SQL patch
277 * @param $path String Path to the patch file
278 * @param $isFullPath Boolean Whether to treat $path as a relative or not
280 protected function applyPatch( $path, $isFullPath = false ) {
282 $this->db
->sourceFile( $path );
284 $this->db
->sourceFile( $this->db
->patchPath( $path ) );
289 * Add a new table to the database
290 * @param $name String Name of the new table
291 * @param $patch String Path to the patch file
292 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
294 protected function addTable( $name, $patch, $fullpath = false ) {
295 if ( $this->db
->tableExists( $name ) ) {
296 $this->output( "...$name table already exists.\n" );
298 $this->output( "Creating $name table..." );
299 $this->applyPatch( $patch, $fullpath );
300 $this->output( "ok\n" );
305 * Add a new field to an existing table
306 * @param $table String Name of the table to modify
307 * @param $field String Name of the new field
308 * @param $patch String Path to the patch file
309 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
311 protected function addField( $table, $field, $patch, $fullpath = false ) {
312 if ( !$this->db
->tableExists( $table ) ) {
313 $this->output( "...$table table does not exist, skipping new field patch\n" );
314 } elseif ( $this->db
->fieldExists( $table, $field ) ) {
315 $this->output( "...have $field field in $table table.\n" );
317 $this->output( "Adding $field field to table $table..." );
318 $this->applyPatch( $patch, $fullpath );
319 $this->output( "ok\n" );
324 * Add a new index to an existing table
325 * @param $table String Name of the table to modify
326 * @param $index String Name of the new index
327 * @param $patch String Path to the patch file
328 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
330 protected function addIndex( $table, $index, $patch, $fullpath = false ) {
331 if ( $this->db
->indexExists( $table, $index ) ) {
332 $this->output( "...$index key already set on $table table.\n" );
334 $this->output( "Adding $index key to table $table... " );
335 $this->applyPatch( $patch, $fullpath );
336 $this->output( "ok\n" );
341 * Drop a field from an existing table
343 * @param $table String Name of the table to modify
344 * @param $field String Name of the old field
345 * @param $patch String Path to the patch file
346 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
348 protected function dropField( $table, $field, $patch, $fullpath = false ) {
349 if ( $this->db
->fieldExists( $table, $field ) ) {
350 $this->output( "Table $table contains $field field. Dropping... " );
351 $this->applyPatch( $patch, $fullpath );
352 $this->output( "ok\n" );
354 $this->output( "...$table table does not contain $field field.\n" );
359 * Drop an index from an existing table
361 * @param $table String: Name of the table to modify
362 * @param $index String: Name of the old index
363 * @param $patch String: Path to the patch file
364 * @param $fullpath Boolean: Whether to treat $patch path as a relative or not
366 protected function dropIndex( $table, $index, $patch, $fullpath = false ) {
367 if ( $this->db
->indexExists( $table, $index ) ) {
368 $this->output( "Dropping $index from table $table... " );
369 $this->applyPatch( $patch, $fullpath );
370 $this->output( "ok\n" );
372 $this->output( "...$index key doesn't exist.\n" );
377 * Modify an existing field
379 * @param $table String: name of the table to which the field belongs
380 * @param $field String: name of the field to modify
381 * @param $patch String: path to the patch file
382 * @param $fullpath Boolean: whether to treat $patch path as a relative or not
384 public function modifyField( $table, $field, $patch, $fullpath = false ) {
385 if ( !$this->db
->tableExists( $table ) ) {
386 $this->output( "...$table table does not exist, skipping modify field patch\n" );
387 } elseif ( !$this->db
->fieldExists( $table, $field ) ) {
388 $this->output( "...$field field does not exist in $table table, skipping modify field patch\n" );
390 $this->output( "Modifying $field field of table $table..." );
391 $this->applyPatch( $patch, $fullpath );
392 $this->output( "ok\n" );
397 * Purge the objectcache table
399 protected function purgeCache() {
400 # We can't guarantee that the user will be able to use TRUNCATE,
401 # but we know that DELETE is available to us
402 $this->output( "Purging caches..." );
403 $this->db
->delete( 'objectcache', '*', __METHOD__
);
404 $this->output( "done.\n" );
408 * Check the site_stats table is not properly populated.
410 protected function checkStats() {
411 $this->output( "Checking site_stats row..." );
412 $row = $this->db
->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__
);
413 if ( $row === false ) {
414 $this->output( "data is missing! rebuilding...\n" );
415 } elseif ( isset( $row->site_stats
) && $row->ss_total_pages
== -1 ) {
416 $this->output( "missing ss_total_pages, rebuilding...\n" );
418 $this->output( "done.\n" );
421 SiteStatsInit
::doAllAndCommit( false );
424 # Common updater functions
426 protected function doActiveUsersInit() {
427 $activeUsers = $this->db
->selectField( 'site_stats', 'ss_active_users', false, __METHOD__
);
428 if ( $activeUsers == -1 ) {
429 $activeUsers = $this->db
->selectField( 'recentchanges',
430 'COUNT( DISTINCT rc_user_text )',
431 array( 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ), __METHOD__
433 $this->db
->update( 'site_stats',
434 array( 'ss_active_users' => intval( $activeUsers ) ),
435 array( 'ss_row_id' => 1 ), __METHOD__
, array( 'LIMIT' => 1 )
438 $this->output( "...ss_active_users user count set...\n" );
441 protected function doLogSearchPopulation() {
442 if ( $this->updateRowExists( 'populate log_search' ) ) {
443 $this->output( "...log_search table already populated.\n" );
448 "Populating log_search table, printing progress markers. For large\n" .
449 "databases, you may want to hit Ctrl-C and do this manually with\n" .
450 "maintenance/populateLogSearch.php.\n" );
451 $task = new PopulateLogSearch();
453 $this->output( "Done populating log_search table.\n" );
456 protected function doUpdateTranscacheField() {
457 if ( $this->updateRowExists( 'convert transcache field' ) ) {
458 $this->output( "...transcache tc_time already converted.\n" );
462 $this->output( "Converting tc_time from UNIX epoch to MediaWiki timestamp... " );
463 $this->applyPatch( 'patch-tc-timestamp.sql' );
464 $this->output( "ok\n" );
467 protected function doCollationUpdate() {
468 global $wgCategoryCollation;
469 if ( $this->db
->selectField(
472 'cl_collation != ' . $this->db
->addQuotes( $wgCategoryCollation ),
475 $this->output( "...collations up-to-date.\n" );
479 $task = new UpdateCollation();