Code cleanup: unused globals, empty statements, typos...
[lhc/web/wiklou.git] / includes / installer / DatabaseUpdater.php
1 <?php
2 /**
3 * DBMS-specific updater helper.
4 *
5 * @file
6 * @ingroup Deployment
7 */
8
9 /*
10 * Class for handling database updates. Roughly based off of updaters.inc, with
11 * a few improvements :)
12 *
13 * @ingroup Deployment
14 * @since 1.17
15 */
16 abstract class DatabaseUpdater {
17
18 /**
19 * Array of updates to perform on the database
20 *
21 * @var array
22 */
23 protected $updates = array();
24
25 protected $extensionUpdates = array();
26
27 protected $db;
28
29 protected $shared = false;
30
31 protected $postDatabaseUpdateMaintenance = array(
32 'DeleteDefaultMessages'
33 );
34
35 /**
36 * Constructor
37 *
38 * @param $db DatabaseBase object to perform updates on
39 * @param $shared bool Whether to perform updates on shared tables
40 *
41 * @TODO @FIXME Make $wgDatabase go away.
42 */
43 protected function __construct( DatabaseBase &$db, $shared ) {
44 global $wgDatabase;
45 $wgDatabase = $db;
46 $this->db = $db;
47 $this->shared = $shared;
48 $this->initOldGlobals();
49 wfRunHooks( 'LoadExtensionSchemaUpdates', array( $this ) );
50 }
51
52 /**
53 * Initialize all of the old globals. One day this should all become
54 * something much nicer
55 */
56 private function initOldGlobals() {
57 global $wgUpdates, $wgExtNewTables, $wgExtNewFields, $wgExtPGNewFields,
58 $wgExtPGAlteredFields, $wgExtNewIndexes, $wgExtModifiedFields;
59
60 // Deprecated. Do not use, ever.
61 $wgUpdates = array();
62
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
71 }
72
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 );
78 } else {
79 throw new MWException( __METHOD__ . ' called for unsupported $wgDBtype' );
80 }
81 }
82
83 /**
84 * Get a database connection to run updates
85 *
86 * @return DatabasBase object
87 */
88 public function getDB() {
89 return $this->db;
90 }
91
92 /**
93 * Add a new update coming from an extension. This should be called by
94 * extensions while executing the LoadExtensionSchemaUpdates hook.
95 *
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
101 * first parameter.
102 */
103 public function addExtensionUpdate( $update ) {
104 $this->extensionUpdates[] = $update;
105 }
106
107 /**
108 * Get the list of extension-defined updates
109 *
110 * @return Array
111 */
112 protected function getExtensionUpdates() {
113 return $this->extensionUpdates;
114 }
115
116 public function getPostDatabaseUpdateMaintenance() {
117 return $this->postDatabaseUpdateMaintenance;
118 }
119
120 /**
121 * Do all the updates
122 *
123 * @param $purge Boolean: whether to clear the objectcache table after updates
124 */
125 public function doUpdates( $purge = true ) {
126 global $IP, $wgVersion;
127 require_once( "$IP/maintenance/updaters.inc" );
128
129 $this->runUpdates( $this->getCoreUpdateList(), false );
130 $this->runUpdates( $this->getOldGlobalUpdates(), false );
131 $this->runUpdates( $this->getExtensionUpdates(), true );
132
133 $this->setAppliedUpdates( $wgVersion, $this->updates );
134
135 if( $purge ) {
136 $this->purgeCache();
137 }
138 }
139
140 /**
141 * Helper function for doUpdates()
142 *
143 * @param $updates Array of updates to run
144 * @param $passSelf Boolean: whether to pass this object we calling external
145 * functions
146 */
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 );
154 }
155 call_user_func_array( $func, $params );
156 flush();
157 }
158 $this->updates = array_merge( $this->updates, $updates );
159 }
160
161 protected function setAppliedUpdates( $version, $updates = array() ) {
162 if( !$this->canUseNewUpdatelog() ) {
163 return;
164 }
165 $key = "updatelist-$version-" . time();
166 $this->db->insert( 'updatelog',
167 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
168 __METHOD__ );
169 }
170
171 /**
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
176 *
177 * @return boolean
178 */
179 protected function canUseNewUpdatelog() {
180 return $this->db->tableExists( 'updatelog' ) &&
181 $this->db->fieldExists( 'updatelog', 'ul_value' );
182 }
183
184 /**
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.
189 */
190 protected function getOldGlobalUpdates() {
191 global $wgUpdates, $wgExtNewFields, $wgExtNewTables,
192 $wgExtModifiedFields, $wgExtNewIndexes, $wgSharedDB, $wgSharedTables;
193
194 $doUser = $this->shared ?
195 $wgSharedDB && in_array( 'user', $wgSharedTables ) :
196 !$wgSharedDB || !in_array( 'user', $wgSharedTables );
197
198 $updates = array();
199
200 if( isset( $wgUpdates[ $this->db->getType() ] ) ) {
201 foreach( $wgUpdates[ $this->db->getType() ] as $upd ) {
202 $updates[] = $upd;
203 }
204 }
205
206 foreach ( $wgExtNewTables as $tableRecord ) {
207 $updates[] = array(
208 'addTable', $tableRecord[0], $tableRecord[1], true
209 );
210 }
211
212 foreach ( $wgExtNewFields as $fieldRecord ) {
213 if ( $fieldRecord[0] != 'user' || $doUser ) {
214 $updates[] = array(
215 'addField', $fieldRecord[0], $fieldRecord[1],
216 $fieldRecord[2], true
217 );
218 }
219 }
220
221 foreach ( $wgExtNewIndexes as $fieldRecord ) {
222 $updates[] = array(
223 'addIndex', $fieldRecord[0], $fieldRecord[1],
224 $fieldRecord[2], true
225 );
226 }
227
228 foreach ( $wgExtModifiedFields as $fieldRecord ) {
229 $updates[] = array(
230 'modifyField', $fieldRecord[0], $fieldRecord[1],
231 $fieldRecord[2], true
232 );
233 }
234
235 return $updates;
236 }
237
238 /**
239 * Get an array of updates to perform on the database. Should return a
240 * multi-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)
243 *
244 * @return Array
245 */
246 protected abstract function getCoreUpdateList();
247
248 /**
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
252 */
253 protected function applyPatch( $path, $isFullPath = false ) {
254 if ( $isFullPath ) {
255 $this->db->sourceFile( $path );
256 } else {
257 $this->db->sourceFile( DatabaseBase::patchPath( $path ) );
258 }
259 }
260
261 /**
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
266 */
267 protected function addTable( $name, $patch, $fullpath = false ) {
268 if ( $this->db->tableExists( $name ) ) {
269 wfOut( "...$name table already exists.\n" );
270 } else {
271 wfOut( "Creating $name table..." );
272 $this->applyPatch( $patch, $fullpath );
273 wfOut( "ok\n" );
274 }
275 }
276
277 /**
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
283 */
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" );
289 } else {
290 wfOut( "Adding $field field to table $table..." );
291 $this->applyPatch( $patch, $fullpath );
292 wfOut( "ok\n" );
293 }
294 }
295
296 /**
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
302 */
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" );
306 } else {
307 wfOut( "Adding $index key to table $table... " );
308 $this->applyPatch( $patch, $fullpath );
309 wfOut( "ok\n" );
310 }
311 }
312
313 /**
314 * Drop a field from an existing table
315 *
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
320 */
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 );
325 wfOut( "ok\n" );
326 } else {
327 wfOut( "...$table table does not contain $field field.\n" );
328 }
329 }
330
331 /**
332 * Drop an index from an existing table
333 *
334 * @param $table String: Name of the table to modify
335 * @param $index String: Name of the old index
336 * @param $patch String: Path to the patch file
337 * @param $fullpath Boolean: Whether to treat $patch path as a relative or not
338 */
339 function dropIndex( $table, $index, $patch, $fullpath = false ) {
340 if ( $this->db->indexExists( $table, $index ) ) {
341 wfOut( "Dropping $index from table $table... " );
342 $this->applyPatch( $patch, $fullpath );
343 wfOut( "ok\n" );
344 } else {
345 wfOut( "...$index key doesn't exist.\n" );
346 }
347 }
348
349 /**
350 * Modify an existing field
351 *
352 * @param $table String: name of the table to which the field belongs
353 * @param $field String: name of the field to modify
354 * @param $patch String: path to the patch file
355 * @param $fullpath Boolean: whether to treat $patch path as a relative or not
356 */
357 public function modifyField( $table, $field, $patch, $fullpath = false ) {
358 if ( !$this->db->tableExists( $table ) ) {
359 wfOut( "...$table table does not exist, skipping modify field patch\n" );
360 } elseif ( !$this->db->fieldExists( $table, $field ) ) {
361 wfOut( "...$field field does not exist in $table table, skipping modify field patch\n" );
362 } else {
363 wfOut( "Modifying $field field of table $table..." );
364 $this->applyPatch( $patch, $fullpath );
365 wfOut( "ok\n" );
366 }
367 }
368
369 /**
370 * Purge the objectcache table
371 */
372 protected function purgeCache() {
373 # We can't guarantee that the user will be able to use TRUNCATE,
374 # but we know that DELETE is available to us
375 wfOut( "Purging caches..." );
376 $this->db->delete( 'objectcache', '*', __METHOD__ );
377 wfOut( "done.\n" );
378 }
379 }