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
);
141 * Helper function for doUpdates()
143 * @param $updates Array of updates to run
144 * @param $passSelf Boolean: whether to pass this object we calling external
147 private function runUpdates( array $updates, $passSelf ) {
148 foreach ( $updates as $params ) {
149 $func = array_shift( $params );
150 if( !is_array( $func ) && method_exists( $this, $func ) ) {
151 $func = array( $this, $func );
152 } elseif ( $passSelf ) {
153 array_unshift( $params, $this );
155 call_user_func_array( $func, $params );
158 $this->updates
= array_merge( $this->updates
, $updates );
161 protected function setAppliedUpdates( $version, $updates = array() ) {
162 if( !$this->canUseNewUpdatelog() ) {
165 $key = "updatelist-$version-" . time();
166 $this->db
->insert( 'updatelog',
167 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
172 * Updatelog was changed in 1.17 to have a ul_value column so we can record
173 * more information about what kind of updates we've done (that's what this
174 * class does). Pre-1.17 wikis won't have this column, and really old wikis
175 * might not even have updatelog at all
179 protected function canUseNewUpdatelog() {
180 return $this->db
->tableExists( 'updatelog' ) &&
181 $this->db
->fieldExists( 'updatelog', 'ul_value' );
185 * Before 1.17, we used to handle updates via stuff like $wgUpdates,
186 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
187 * of this in 1.17 but we want to remain back-compatible for awhile. So
188 * load up these old global-based things into our update list.
190 protected function getOldGlobalUpdates() {
191 global $wgUpdates, $wgExtNewFields, $wgExtNewTables,
192 $wgExtModifiedFields, $wgExtNewIndexes, $wgSharedDB, $wgSharedTables;
194 $doUser = $this->shared ?
195 $wgSharedDB && in_array( 'user', $wgSharedTables ) :
196 !$wgSharedDB ||
!in_array( 'user', $wgSharedTables );
200 if( isset( $wgUpdates[ $this->db
->getType() ] ) ) {
201 foreach( $wgUpdates[ $this->db
->getType() ] as $upd ) {
206 foreach ( $wgExtNewTables as $tableRecord ) {
208 'addTable', $tableRecord[0], $tableRecord[1], true
212 foreach ( $wgExtNewFields as $fieldRecord ) {
213 if ( $fieldRecord[0] != 'user' ||
$doUser ) {
215 'addField', $fieldRecord[0], $fieldRecord[1],
216 $fieldRecord[2], true
221 foreach ( $wgExtNewIndexes as $fieldRecord ) {
223 'addIndex', $fieldRecord[0], $fieldRecord[1],
224 $fieldRecord[2], true
228 foreach ( $wgExtModifiedFields as $fieldRecord ) {
230 'modifyField', $fieldRecord[0], $fieldRecord[1],
231 $fieldRecord[2], true
239 * Get an array of updates to perform on the database. Should return a
240 * mutli-dimensional array. The main key is the MediaWiki version (1.12,
241 * 1.13...) with the values being arrays of updates, identical to how
242 * updaters.inc did it (for now)
246 protected abstract function getCoreUpdateList();
249 * Applies a SQL patch
250 * @param $path String Path to the patch file
251 * @param $isFullPath Boolean Whether to treat $path as a relative or not
253 protected function applyPatch( $path, $isFullPath = false ) {
255 $this->db
->sourceFile( $path );
257 $this->db
->sourceFile( DatabaseBase
::patchPath( $path ) );
262 * Add a new table to the database
263 * @param $name String Name of the new table
264 * @param $patch String Path to the patch file
265 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
267 protected function addTable( $name, $patch, $fullpath = false ) {
268 if ( $this->db
->tableExists( $name ) ) {
269 wfOut( "...$name table already exists.\n" );
271 wfOut( "Creating $name table..." );
272 $this->applyPatch( $patch, $fullpath );
278 * Add a new field to an existing table
279 * @param $table String Name of the table to modify
280 * @param $field String Name of the new field
281 * @param $patch String Path to the patch file
282 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
284 protected function addField( $table, $field, $patch, $fullpath = false ) {
285 if ( !$this->db
->tableExists( $table ) ) {
286 wfOut( "...$table table does not exist, skipping new field patch\n" );
287 } elseif ( $this->db
->fieldExists( $table, $field ) ) {
288 wfOut( "...have $field field in $table table.\n" );
290 wfOut( "Adding $field field to table $table..." );
291 $this->applyPatch( $patch, $fullpath );
297 * Add a new index to an existing table
298 * @param $table String Name of the table to modify
299 * @param $index String Name of the new index
300 * @param $patch String Path to the patch file
301 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
303 function addIndex( $table, $index, $patch, $fullpath = false ) {
304 if ( $this->db
->indexExists( $table, $index ) ) {
305 wfOut( "...$index key already set on $table table.\n" );
307 wfOut( "Adding $index key to table $table... " );
308 $this->applyPatch( $patch, $fullpath );
314 * Drop a field from an existing table
316 * @param $table String Name of the table to modify
317 * @param $field String Name of the old field
318 * @param $patch String Path to the patch file
319 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
321 function dropField( $table, $field, $patch, $fullpath = false ) {
322 if ( $this->db
->fieldExists( $table, $field ) ) {
323 wfOut( "Table $table contains $field field. Dropping... " );
324 $this->applyPatch( $patch, $fullpath );
327 wfOut( "...$table table does not contain $field field.\n" );
332 * Modify an existing field
334 * @param $table String: name of the table to which the field belongs
335 * @param $field String: name of the field to modify
336 * @param $patch String: path to the patch file
337 * @param $fullpath Boolean: whether to treat $patch path as a relative or not
339 public function modifyField( $table, $field, $patch, $fullpath = false ) {
340 if ( !$this->db
->tableExists( $table ) ) {
341 wfOut( "...$table table does not exist, skipping modify field patch\n" );
342 } elseif ( !$this->db
->fieldExists( $table, $field ) ) {
343 wfOut( "...$field field does not exist in $table table, skipping modify field patch\n" );
345 wfOut( "Modifying $field field of table $table..." );
346 $this->applyPatch( $patch, $fullpath );
352 * Purge the objectcache table
354 protected function purgeCache() {
355 # We can't guarantee that the user will be able to use TRUNCATE,
356 # but we know that DELETE is available to us
357 wfOut( "Purging caches..." );
358 $this->db
->delete( 'objectcache', '*', __METHOD__
);