3 * DBMS-specific updater helper.
10 * Class for handling database updates. Roughly based off of updaters.inc, with
11 * a few improvements :)
16 abstract class DatabaseUpdater
{
19 * Array of updates to perform on the database
23 protected $updates = array();
25 protected $extensionUpdates = array();
29 protected $shared = false;
31 protected $postDatabaseUpdateMaintenance = array(
32 'DeleteDefaultMessages'
38 * @param $db DatabaseBase object to perform updates on
39 * @param $shared bool Whether to perform updates on shared tables
41 * @TODO @FIXME Make $wgDatabase go away.
43 protected function __construct( DatabaseBase
&$db, $shared ) {
47 $this->shared
= $shared;
48 $this->initOldGlobals();
49 wfRunHooks( 'LoadExtensionSchemaUpdates', array( $this ) );
53 * Initialize all of the old globals. One day this should all become
54 * something much nicer
56 private function initOldGlobals() {
57 global $wgUpdates, $wgExtNewTables, $wgExtNewFields, $wgExtPGNewFields,
58 $wgExtPGAlteredFields, $wgExtNewIndexes, $wgExtModifiedFields;
60 // Deprecated. Do not use, ever.
63 # For extensions only, should be populated via hooks
64 # $wgDBtype should be checked to specifiy the proper file
65 $wgExtNewTables = array(); // table, dir
66 $wgExtNewFields = array(); // table, column, dir
67 $wgExtPGNewFields = array(); // table, column, column attributes; for PostgreSQL
68 $wgExtPGAlteredFields = array(); // table, column, new type, conversion method; for PostgreSQL
69 $wgExtNewIndexes = array(); // table, index, dir
70 $wgExtModifiedFields = array(); // table, index, dir
73 public static function newForDB( &$db, $shared = false ) {
74 $type = $db->getType();
75 if( in_array( $type, Installer
::getDBTypes() ) ) {
76 $class = ucfirst( $type ) . 'Updater';
77 return new $class( $db, $shared );
79 throw new MWException( __METHOD__
. ' called for unsupported $wgDBtype' );
84 * Get a database connection to run updates
86 * @return DatabasBase object
88 public function getDB() {
93 * Add a new update coming from an extension. This should be called by
94 * extensions while executing the LoadExtensionSchemaUpdates hook.
96 * @param $update Array: the update to run. Format is the following:
97 * first item is the callback function, it also can be a
98 * simple string with the name of a function in this class,
99 * following elements are parameters to the function.
100 * Note that callback functions will recieve this object as
103 public function addExtensionUpdate( $update ) {
104 $this->extensionUpdates
[] = $update;
108 * Get the list of extension-defined updates
112 protected function getExtensionUpdates() {
113 return $this->extensionUpdates
;
116 public function getPostDatabaseUpdateMaintenance() {
117 return $this->postDatabaseUpdateMaintenance
;
123 * @param $purge Boolean: whether to clear the objectcache table after updates
125 public function doUpdates( $purge = true ) {
126 global $IP, $wgVersion;
127 require_once( "$IP/maintenance/updaters.inc" );
129 $this->runUpdates( $this->getCoreUpdateList(), false );
130 $this->runUpdates( $this->getOldGlobalUpdates(), false );
131 $this->runUpdates( $this->getExtensionUpdates(), true );
133 $this->setAppliedUpdates( $wgVersion, $this->updates
);
142 * Helper function for doUpdates()
144 * @param $updates Array of updates to run
145 * @param $passSelf Boolean: whether to pass this object we calling external
148 private function runUpdates( array $updates, $passSelf ) {
149 foreach ( $updates as $params ) {
150 $func = array_shift( $params );
151 if( !is_array( $func ) && method_exists( $this, $func ) ) {
152 $func = array( $this, $func );
153 } elseif ( $passSelf ) {
154 array_unshift( $params, $this );
156 call_user_func_array( $func, $params );
159 $this->updates
= array_merge( $this->updates
, $updates );
162 protected function setAppliedUpdates( $version, $updates = array() ) {
163 if( !$this->canUseNewUpdatelog() ) {
166 $key = "updatelist-$version-" . time();
167 $this->db
->insert( 'updatelog',
168 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
173 * Updatelog was changed in 1.17 to have a ul_value column so we can record
174 * more information about what kind of updates we've done (that's what this
175 * class does). Pre-1.17 wikis won't have this column, and really old wikis
176 * might not even have updatelog at all
180 protected function canUseNewUpdatelog() {
181 return $this->db
->tableExists( 'updatelog' ) &&
182 $this->db
->fieldExists( 'updatelog', 'ul_value' );
186 * Before 1.17, we used to handle updates via stuff like $wgUpdates,
187 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
188 * of this in 1.17 but we want to remain back-compatible for awhile. So
189 * load up these old global-based things into our update list.
191 protected function getOldGlobalUpdates() {
192 global $wgUpdates, $wgExtNewFields, $wgExtNewTables,
193 $wgExtModifiedFields, $wgExtNewIndexes, $wgSharedDB, $wgSharedTables;
195 $doUser = $this->shared ?
196 $wgSharedDB && in_array( 'user', $wgSharedTables ) :
197 !$wgSharedDB ||
!in_array( 'user', $wgSharedTables );
201 if( isset( $wgUpdates[ $this->db
->getType() ] ) ) {
202 foreach( $wgUpdates[ $this->db
->getType() ] as $upd ) {
207 foreach ( $wgExtNewTables as $tableRecord ) {
209 'addTable', $tableRecord[0], $tableRecord[1], true
213 foreach ( $wgExtNewFields as $fieldRecord ) {
214 if ( $fieldRecord[0] != 'user' ||
$doUser ) {
216 'addField', $fieldRecord[0], $fieldRecord[1],
217 $fieldRecord[2], true
222 foreach ( $wgExtNewIndexes as $fieldRecord ) {
224 'addIndex', $fieldRecord[0], $fieldRecord[1],
225 $fieldRecord[2], true
229 foreach ( $wgExtModifiedFields as $fieldRecord ) {
231 'modifyField', $fieldRecord[0], $fieldRecord[1],
232 $fieldRecord[2], true
240 * Get an array of updates to perform on the database. Should return a
241 * multi-dimensional array. The main key is the MediaWiki version (1.12,
242 * 1.13...) with the values being arrays of updates, identical to how
243 * updaters.inc did it (for now)
247 protected abstract function getCoreUpdateList();
250 * Applies a SQL patch
251 * @param $path String Path to the patch file
252 * @param $isFullPath Boolean Whether to treat $path as a relative or not
254 protected function applyPatch( $path, $isFullPath = false ) {
256 $this->db
->sourceFile( $path );
258 $this->db
->sourceFile( DatabaseBase
::patchPath( $path ) );
263 * Add a new table to the database
264 * @param $name String Name of the new table
265 * @param $patch String Path to the patch file
266 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
268 protected function addTable( $name, $patch, $fullpath = false ) {
269 if ( $this->db
->tableExists( $name ) ) {
270 wfOut( "...$name table already exists.\n" );
272 wfOut( "Creating $name table..." );
273 $this->applyPatch( $patch, $fullpath );
279 * Add a new field to an existing table
280 * @param $table String Name of the table to modify
281 * @param $field String Name of the new field
282 * @param $patch String Path to the patch file
283 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
285 protected function addField( $table, $field, $patch, $fullpath = false ) {
286 if ( !$this->db
->tableExists( $table ) ) {
287 wfOut( "...$table table does not exist, skipping new field patch\n" );
288 } elseif ( $this->db
->fieldExists( $table, $field ) ) {
289 wfOut( "...have $field field in $table table.\n" );
291 wfOut( "Adding $field field to table $table..." );
292 $this->applyPatch( $patch, $fullpath );
298 * Add a new index to an existing table
299 * @param $table String Name of the table to modify
300 * @param $index String Name of the new index
301 * @param $patch String Path to the patch file
302 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
304 function addIndex( $table, $index, $patch, $fullpath = false ) {
305 if ( $this->db
->indexExists( $table, $index ) ) {
306 wfOut( "...$index key already set on $table table.\n" );
308 wfOut( "Adding $index key to table $table... " );
309 $this->applyPatch( $patch, $fullpath );
315 * Drop a field from an existing table
317 * @param $table String Name of the table to modify
318 * @param $field String Name of the old field
319 * @param $patch String Path to the patch file
320 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
322 function dropField( $table, $field, $patch, $fullpath = false ) {
323 if ( $this->db
->fieldExists( $table, $field ) ) {
324 wfOut( "Table $table contains $field field. Dropping... " );
325 $this->applyPatch( $patch, $fullpath );
328 wfOut( "...$table table does not contain $field field.\n" );
333 * Drop an index from an existing table
335 * @param $table String: Name of the table to modify
336 * @param $index String: Name of the old index
337 * @param $patch String: Path to the patch file
338 * @param $fullpath Boolean: Whether to treat $patch path as a relative or not
340 function dropIndex( $table, $index, $patch, $fullpath = false ) {
341 if ( $this->db
->indexExists( $table, $index ) ) {
342 wfOut( "Dropping $index from table $table... " );
343 $this->applyPatch( $patch, $fullpath );
346 wfOut( "...$index key doesn't exist.\n" );
351 * Modify an existing field
353 * @param $table String: name of the table to which the field belongs
354 * @param $field String: name of the field to modify
355 * @param $patch String: path to the patch file
356 * @param $fullpath Boolean: whether to treat $patch path as a relative or not
358 public function modifyField( $table, $field, $patch, $fullpath = false ) {
359 if ( !$this->db
->tableExists( $table ) ) {
360 wfOut( "...$table table does not exist, skipping modify field patch\n" );
361 } elseif ( !$this->db
->fieldExists( $table, $field ) ) {
362 wfOut( "...$field field does not exist in $table table, skipping modify field patch\n" );
364 wfOut( "Modifying $field field of table $table..." );
365 $this->applyPatch( $patch, $fullpath );
371 * Purge the objectcache table
373 protected function purgeCache() {
374 # We can't guarantee that the user will be able to use TRUNCATE,
375 # but we know that DELETE is available to us
376 wfOut( "Purging caches..." );
377 $this->db
->delete( 'objectcache', '*', __METHOD__
);
382 * Check the site_stats table is not properly populated.
384 protected function checkStats() {
385 wfOut( "Checking site_stats row..." );
386 $row = $this->db
->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__
);
387 if ( $row === false ) {
388 wfOut( "data is missing! rebuilding...\n" );
389 } elseif ( isset( $row->site_stats
) && $row->ss_total_pages
== -1 ) {
390 wfOut( "missing ss_total_pages, rebuilding...\n" );
395 SiteStatsInit
::doAllAndCommit( false );