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();
27 protected $shared = false;
29 protected $postDatabaseUpdateMaintenance = array(
30 'DeleteDefaultMessages'
36 * @param $db DatabaseBase object to perform updates on
37 * @param $shared bool Whether to perform updates on shared tables
39 * @TODO @FIXME Make $wgDatabase go away.
41 protected function __construct( DatabaseBase
&$db, $shared ) {
45 $this->shared
= $shared;
46 $this->initOldGlobals();
47 wfRunHooks( 'LoadExtensionSchemaUpdates', array( $this ) );
51 * Initialize all of the old globals. One day this should all become
52 * something much nicer
54 private function initOldGlobals() {
55 global $wgUpdates, $wgExtNewTables, $wgExtNewFields, $wgExtPGNewFields,
56 $wgExtPGAlteredFields, $wgExtNewIndexes, $wgExtModifiedFields;
58 // Deprecated. Do not use, ever.
61 # For extensions only, should be populated via hooks
62 # $wgDBtype should be checked to specifiy the proper file
63 $wgExtNewTables = array(); // table, dir
64 $wgExtNewFields = array(); // table, column, dir
65 $wgExtPGNewFields = array(); // table, column, column attributes; for PostgreSQL
66 $wgExtPGAlteredFields = array(); // table, column, new type, conversion method; for PostgreSQL
67 $wgExtNewIndexes = array(); // table, index, dir
68 $wgExtModifiedFields = array(); // table, index, dir
71 public static function newForDB( &$db, $shared = false ) {
72 $type = $db->getType();
73 if( in_array( $type, Installer
::getDBTypes() ) ) {
74 $class = ucfirst( $type ) . 'Updater';
75 return new $class( $db, $shared );
77 throw new MWException( __METHOD__
. ' called for unsupported $wgDBtype' );
81 public function getDB() { return $this->db
; }
83 public function getPostDatabaseUpdateMaintenance() {
84 return $this->postDatabaseUpdateMaintenance
;
89 * @param $purge bool Whether to clear the objectcache table after updates
91 public function doUpdates( $purge = true ) {
92 global $IP, $wgVersion;
93 require_once( "$IP/maintenance/updaters.inc" );
94 $this->updates
= array_merge( $this->getCoreUpdateList(),
95 $this->getOldGlobalUpdates() );
96 foreach ( $this->updates
as $params ) {
97 $func = array_shift( $params );
98 if( !is_array( $func ) && method_exists( $this, $func ) ) {
99 $func = array( $this, $func );
101 call_user_func_array( $func, $params );
104 $this->setAppliedUpdates( $wgVersion, $this->updates
);
111 protected function setAppliedUpdates( $version, $updates = array() ) {
112 if( !$this->canUseNewUpdatelog() ) {
115 $key = "updatelist-$version-" . time();
116 $this->db
->insert( 'updatelog',
117 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
122 * Updatelog was changed in 1.17 to have a ul_value column so we can record
123 * more information about what kind of updates we've done (that's what this
124 * class does). Pre-1.17 wikis won't have this column, and really old wikis
125 * might not even have updatelog at all
129 protected function canUseNewUpdatelog() {
130 return $this->db
->tableExists( 'updatelog' ) &&
131 $this->db
->fieldExists( 'updatelog', 'ul_value' );
135 * Before 1.17, we used to handle updates via stuff like $wgUpdates,
136 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
137 * of this in 1.17 but we want to remain back-compatible for awhile. So
138 * load up these old global-based things into our update list.
140 protected function getOldGlobalUpdates() {
141 global $wgUpdates, $wgExtNewFields, $wgExtNewTables,
142 $wgExtModifiedFields, $wgExtNewIndexes, $wgSharedDB, $wgSharedTables;
144 $doUser = $this->shared ?
145 $wgSharedDB && in_array( 'user', $wgSharedTables ) :
146 !$wgSharedDB ||
!in_array( 'user', $wgSharedTables );
150 if( isset( $wgUpdates[ $this->db
->getType() ] ) ) {
151 foreach( $wgUpdates[ $this->db
->getType() ] as $upd ) {
156 foreach ( $wgExtNewTables as $tableRecord ) {
158 'addTable', $tableRecord[0], $tableRecord[1], true
162 foreach ( $wgExtNewFields as $fieldRecord ) {
163 if ( $fieldRecord[0] != 'user' ||
$doUser ) {
165 'addField', $fieldRecord[0], $fieldRecord[1],
166 $fieldRecord[2], true
171 foreach ( $wgExtNewIndexes as $fieldRecord ) {
173 'addIndex', $fieldRecord[0], $fieldRecord[1],
174 $fieldRecord[2], true
178 foreach ( $wgExtModifiedFields as $fieldRecord ) {
180 'modify_field', $fieldRecord[0], $fieldRecord[1],
181 $fieldRecord[2], true
189 * Get an array of updates to perform on the database. Should return a
190 * mutli-dimensional array. The main key is the MediaWiki version (1.12,
191 * 1.13...) with the values being arrays of updates, identical to how
192 * updaters.inc did it (for now)
196 protected abstract function getCoreUpdateList();
199 * Applies a SQL patch
200 * @param $path String Path to the patch file
201 * @param $isFullPath Boolean Whether to treat $path as a relative or not
203 protected function applyPatch( $path, $isFullPath = false ) {
205 $this->db
->sourceFile( $path );
207 $this->db
->sourceFile( DatabaseBase
::patchPath( $path ) );
212 * Add a new table to the database
213 * @param $name String Name of the new table
214 * @param $patch String Path to the patch file
215 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
217 protected function addTable( $name, $patch, $fullpath = false ) {
218 if ( $this->db
->tableExists( $name ) ) {
219 wfOut( "...$name table already exists.\n" );
221 wfOut( "Creating $name table..." );
222 $this->applyPatch( $patch, $fullpath );
228 * Add a new field to an existing table
229 * @param $table String Name of the table to modify
230 * @param $field String Name of the new field
231 * @param $patch String Path to the patch file
232 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
234 protected function addField( $table, $field, $patch, $fullpath = false ) {
235 if ( !$this->db
->tableExists( $table ) ) {
236 wfOut( "...$table table does not exist, skipping new field patch\n" );
237 } elseif ( $this->db
->fieldExists( $table, $field ) ) {
238 wfOut( "...have $field field in $table table.\n" );
240 wfOut( "Adding $field field to table $table..." );
241 $this->applyPatch( $patch, $fullpath );
247 * Add a new index to an existing table
248 * @param $table String Name of the table to modify
249 * @param $index String Name of the new index
250 * @param $patch String Path to the patch file
251 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
253 function addIndex( $table, $index, $patch, $fullpath = false ) {
254 if ( $this->db
->indexExists( $table, $index ) ) {
255 wfOut( "...$index key already set on $table table.\n" );
257 wfOut( "Adding $index key to table $table... " );
258 $this->applyPatch( $patch, $fullpath );
264 * Drop a field from an existing table
266 * @param $table String Name of the table to modify
267 * @param $field String Name of the old field
268 * @param $patch String Path to the patch file
269 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
271 function dropField( $table, $field, $patch, $fullpath = false ) {
272 if ( $this->db
->fieldExists( $table, $field ) ) {
273 wfOut( "Table $table contains $field field. Dropping... " );
274 $this->applyPatch( $patch, $fullpath );
277 wfOut( "...$table table does not contain $field field.\n" );
282 * Purge the objectcache table
284 protected function purgeCache() {
285 # We can't guarantee that the user will be able to use TRUNCATE,
286 # but we know that DELETE is available to us
287 wfOut( "Purging caches..." );
288 $this->db
->delete( 'objectcache', '*', __METHOD__
);