* Use Maintenance::runChild() to get the child script instance
[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 $this->checkStats();
139 }
140
141 /**
142 * Helper function for doUpdates()
143 *
144 * @param $updates Array of updates to run
145 * @param $passSelf Boolean: whether to pass this object we calling external
146 * functions
147 */
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 );
155 }
156 call_user_func_array( $func, $params );
157 flush();
158 }
159 $this->updates = array_merge( $this->updates, $updates );
160 }
161
162 protected function setAppliedUpdates( $version, $updates = array() ) {
163 if( !$this->canUseNewUpdatelog() ) {
164 return;
165 }
166 $key = "updatelist-$version-" . time();
167 $this->db->insert( 'updatelog',
168 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
169 __METHOD__ );
170 }
171
172 /**
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
177 *
178 * @return boolean
179 */
180 protected function canUseNewUpdatelog() {
181 return $this->db->tableExists( 'updatelog' ) &&
182 $this->db->fieldExists( 'updatelog', 'ul_value' );
183 }
184
185 /**
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.
190 */
191 protected function getOldGlobalUpdates() {
192 global $wgUpdates, $wgExtNewFields, $wgExtNewTables,
193 $wgExtModifiedFields, $wgExtNewIndexes, $wgSharedDB, $wgSharedTables;
194
195 $doUser = $this->shared ?
196 $wgSharedDB && in_array( 'user', $wgSharedTables ) :
197 !$wgSharedDB || !in_array( 'user', $wgSharedTables );
198
199 $updates = array();
200
201 if( isset( $wgUpdates[ $this->db->getType() ] ) ) {
202 foreach( $wgUpdates[ $this->db->getType() ] as $upd ) {
203 $updates[] = $upd;
204 }
205 }
206
207 foreach ( $wgExtNewTables as $tableRecord ) {
208 $updates[] = array(
209 'addTable', $tableRecord[0], $tableRecord[1], true
210 );
211 }
212
213 foreach ( $wgExtNewFields as $fieldRecord ) {
214 if ( $fieldRecord[0] != 'user' || $doUser ) {
215 $updates[] = array(
216 'addField', $fieldRecord[0], $fieldRecord[1],
217 $fieldRecord[2], true
218 );
219 }
220 }
221
222 foreach ( $wgExtNewIndexes as $fieldRecord ) {
223 $updates[] = array(
224 'addIndex', $fieldRecord[0], $fieldRecord[1],
225 $fieldRecord[2], true
226 );
227 }
228
229 foreach ( $wgExtModifiedFields as $fieldRecord ) {
230 $updates[] = array(
231 'modifyField', $fieldRecord[0], $fieldRecord[1],
232 $fieldRecord[2], true
233 );
234 }
235
236 return $updates;
237 }
238
239 /**
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)
244 *
245 * @return Array
246 */
247 protected abstract function getCoreUpdateList();
248
249 /**
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
253 */
254 protected function applyPatch( $path, $isFullPath = false ) {
255 if ( $isFullPath ) {
256 $this->db->sourceFile( $path );
257 } else {
258 $this->db->sourceFile( DatabaseBase::patchPath( $path ) );
259 }
260 }
261
262 /**
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
267 */
268 protected function addTable( $name, $patch, $fullpath = false ) {
269 if ( $this->db->tableExists( $name ) ) {
270 wfOut( "...$name table already exists.\n" );
271 } else {
272 wfOut( "Creating $name table..." );
273 $this->applyPatch( $patch, $fullpath );
274 wfOut( "ok\n" );
275 }
276 }
277
278 /**
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
284 */
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" );
290 } else {
291 wfOut( "Adding $field field to table $table..." );
292 $this->applyPatch( $patch, $fullpath );
293 wfOut( "ok\n" );
294 }
295 }
296
297 /**
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
303 */
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" );
307 } else {
308 wfOut( "Adding $index key to table $table... " );
309 $this->applyPatch( $patch, $fullpath );
310 wfOut( "ok\n" );
311 }
312 }
313
314 /**
315 * Drop a field from an existing table
316 *
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
321 */
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 );
326 wfOut( "ok\n" );
327 } else {
328 wfOut( "...$table table does not contain $field field.\n" );
329 }
330 }
331
332 /**
333 * Drop an index from an existing table
334 *
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
339 */
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 );
344 wfOut( "ok\n" );
345 } else {
346 wfOut( "...$index key doesn't exist.\n" );
347 }
348 }
349
350 /**
351 * Modify an existing field
352 *
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
357 */
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" );
363 } else {
364 wfOut( "Modifying $field field of table $table..." );
365 $this->applyPatch( $patch, $fullpath );
366 wfOut( "ok\n" );
367 }
368 }
369
370 /**
371 * Purge the objectcache table
372 */
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__ );
378 wfOut( "done.\n" );
379 }
380
381 /**
382 * Check the site_stats table is not properly populated.
383 */
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" );
391 } else {
392 wfOut( "done.\n" );
393 return;
394 }
395 SiteStatsInit::doAllAndCommit( false );
396 }
397
398 }