Method documentation
[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 require_once( dirname(__FILE__) . '/../../maintenance/Maintenance.php' );
10
11 /*
12 * Class for handling database updates. Roughly based off of updaters.inc, with
13 * a few improvements :)
14 *
15 * @ingroup Deployment
16 * @since 1.17
17 */
18 abstract class DatabaseUpdater {
19
20 /**
21 * Array of updates to perform on the database
22 *
23 * @var array
24 */
25 protected $updates = array();
26
27 protected $extensionUpdates = array();
28
29 /**
30 * Handle to the database subclass
31 *
32 * @var DatabaseBase
33 */
34 protected $db;
35
36 protected $shared = false;
37
38 protected $postDatabaseUpdateMaintenance = array(
39 'DeleteDefaultMessages'
40 );
41
42 /**
43 * Constructor
44 *
45 * @param $db DatabaseBase object to perform updates on
46 * @param $shared bool Whether to perform updates on shared tables
47 * @param $maintenance Maintenance Maintenance object which created us
48 */
49 protected function __construct( DatabaseBase &$db, $shared, Maintenance $maintenance = null ) {
50 $this->db = $db;
51 $this->shared = $shared;
52 if ( $maintenance ) {
53 $this->maintenance = $maintenance;
54 } else {
55 $this->maintenance = new FakeMaintenance;
56 }
57 $this->initOldGlobals();
58 wfRunHooks( 'LoadExtensionSchemaUpdates', array( $this ) );
59 }
60
61 /**
62 * Initialize all of the old globals. One day this should all become
63 * something much nicer
64 */
65 private function initOldGlobals() {
66 global $wgExtNewTables, $wgExtNewFields, $wgExtPGNewFields,
67 $wgExtPGAlteredFields, $wgExtNewIndexes, $wgExtModifiedFields;
68
69 # For extensions only, should be populated via hooks
70 # $wgDBtype should be checked to specifiy the proper file
71 $wgExtNewTables = array(); // table, dir
72 $wgExtNewFields = array(); // table, column, dir
73 $wgExtPGNewFields = array(); // table, column, column attributes; for PostgreSQL
74 $wgExtPGAlteredFields = array(); // table, column, new type, conversion method; for PostgreSQL
75 $wgExtNewIndexes = array(); // table, index, dir
76 $wgExtModifiedFields = array(); // table, index, dir
77 }
78
79 public static function newForDB( &$db, $shared = false, $maintenance = null ) {
80 $type = $db->getType();
81 if( in_array( $type, Installer::getDBTypes() ) ) {
82 $class = ucfirst( $type ) . 'Updater';
83 return new $class( $db, $shared, $maintenance );
84 } else {
85 throw new MWException( __METHOD__ . ' called for unsupported $wgDBtype' );
86 }
87 }
88
89 /**
90 * Get a database connection to run updates
91 *
92 * @return DatabaseBase object
93 */
94 public function getDB() {
95 return $this->db;
96 }
97
98 /**
99 * Output some text. If we're running from web, escape the text first.
100 *
101 * @param $str String: Text to output
102 */
103 public function output( $str ) {
104 if ( $this->maintenance->isQuiet() ) {
105 return;
106 }
107 global $wgCommandLineMode;
108 if( !$wgCommandLineMode ) {
109 $str = htmlspecialchars( $str );
110 }
111 echo $str;
112 flush();
113 }
114
115 /**
116 * Add a new update coming from an extension. This should be called by
117 * extensions while executing the LoadExtensionSchemaUpdates hook.
118 *
119 * @param $update Array: the update to run. Format is the following:
120 * first item is the callback function, it also can be a
121 * simple string with the name of a function in this class,
122 * following elements are parameters to the function.
123 * Note that callback functions will recieve this object as
124 * first parameter.
125 */
126 public function addExtensionUpdate( Array $update ) {
127 $this->extensionUpdates[] = $update;
128 }
129
130 /**
131 * Get the list of extension-defined updates
132 *
133 * @return Array
134 */
135 protected function getExtensionUpdates() {
136 return $this->extensionUpdates;
137 }
138
139 public function getPostDatabaseUpdateMaintenance() {
140 return $this->postDatabaseUpdateMaintenance;
141 }
142
143 /**
144 * Do all the updates
145 *
146 * @param $purge Boolean: whether to clear the objectcache table after updates
147 */
148 public function doUpdates( $purge = true ) {
149 global $wgVersion;
150
151 if ( !defined( 'MW_NO_SETUP' ) ) {
152 define( 'MW_NO_SETUP', true );
153 }
154
155 $this->runUpdates( $this->getCoreUpdateList(), false );
156 $this->runUpdates( $this->getOldGlobalUpdates(), false );
157 $this->runUpdates( $this->getExtensionUpdates(), true );
158
159 $this->setAppliedUpdates( $wgVersion, $this->updates );
160
161 if( $purge ) {
162 $this->purgeCache();
163 }
164 $this->checkStats();
165 }
166
167 /**
168 * Helper function for doUpdates()
169 *
170 * @param $updates Array of updates to run
171 * @param $passSelf Boolean: whether to pass this object we calling external
172 * functions
173 */
174 private function runUpdates( array $updates, $passSelf ) {
175 foreach ( $updates as $params ) {
176 $func = array_shift( $params );
177 if( !is_array( $func ) && method_exists( $this, $func ) ) {
178 $func = array( $this, $func );
179 } elseif ( $passSelf ) {
180 array_unshift( $params, $this );
181 }
182 call_user_func_array( $func, $params );
183 flush();
184 }
185 $this->updates = array_merge( $this->updates, $updates );
186 }
187
188 protected function setAppliedUpdates( $version, $updates = array() ) {
189 if( !$this->canUseNewUpdatelog() ) {
190 return;
191 }
192 $key = "updatelist-$version-" . time();
193 $this->db->insert( 'updatelog',
194 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
195 __METHOD__ );
196 }
197
198 /**
199 * Helper function: check if the given key is present in the updatelog table.
200 * Obviously, only use this for updates that occur after the updatelog table was
201 * created!
202 */
203 public function updateRowExists( $key ) {
204 $row = $this->db->selectRow(
205 'updatelog',
206 '1',
207 array( 'ul_key' => $key ),
208 __METHOD__
209 );
210 return (bool)$row;
211 }
212
213 /**
214 * Updatelog was changed in 1.17 to have a ul_value column so we can record
215 * more information about what kind of updates we've done (that's what this
216 * class does). Pre-1.17 wikis won't have this column, and really old wikis
217 * might not even have updatelog at all
218 *
219 * @return boolean
220 */
221 protected function canUseNewUpdatelog() {
222 return $this->db->tableExists( 'updatelog' ) &&
223 $this->db->fieldExists( 'updatelog', 'ul_value' );
224 }
225
226 /**
227 * Before 1.17, we used to handle updates via stuff like
228 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
229 * of this in 1.17 but we want to remain back-compatible for a while. So
230 * load up these old global-based things into our update list.
231 */
232 protected function getOldGlobalUpdates() {
233 global $wgExtNewFields, $wgExtNewTables, $wgExtModifiedFields,
234 $wgExtNewIndexes, $wgSharedDB, $wgSharedTables;
235
236 $doUser = $this->shared ?
237 $wgSharedDB && in_array( 'user', $wgSharedTables ) :
238 !$wgSharedDB || !in_array( 'user', $wgSharedTables );
239
240 $updates = array();
241
242 foreach ( $wgExtNewTables as $tableRecord ) {
243 $updates[] = array(
244 'addTable', $tableRecord[0], $tableRecord[1], true
245 );
246 }
247
248 foreach ( $wgExtNewFields as $fieldRecord ) {
249 if ( $fieldRecord[0] != 'user' || $doUser ) {
250 $updates[] = array(
251 'addField', $fieldRecord[0], $fieldRecord[1],
252 $fieldRecord[2], true
253 );
254 }
255 }
256
257 foreach ( $wgExtNewIndexes as $fieldRecord ) {
258 $updates[] = array(
259 'addIndex', $fieldRecord[0], $fieldRecord[1],
260 $fieldRecord[2], true
261 );
262 }
263
264 foreach ( $wgExtModifiedFields as $fieldRecord ) {
265 $updates[] = array(
266 'modifyField', $fieldRecord[0], $fieldRecord[1],
267 $fieldRecord[2], true
268 );
269 }
270
271 return $updates;
272 }
273
274 /**
275 * Get an array of updates to perform on the database. Should return a
276 * multi-dimensional array. The main key is the MediaWiki version (1.12,
277 * 1.13...) with the values being arrays of updates, identical to how
278 * updaters.inc did it (for now)
279 *
280 * @return Array
281 */
282 protected abstract function getCoreUpdateList();
283
284 /**
285 * Applies a SQL patch
286 * @param $path String Path to the patch file
287 * @param $isFullPath Boolean Whether to treat $path as a relative or not
288 */
289 protected function applyPatch( $path, $isFullPath = false ) {
290 if ( $isFullPath ) {
291 $this->db->sourceFile( $path );
292 } else {
293 $this->db->sourceFile( $this->db->patchPath( $path ) );
294 }
295 }
296
297 /**
298 * Add a new table to the database
299 * @param $name String Name of the new table
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 protected function addTable( $name, $patch, $fullpath = false ) {
304 if ( $this->db->tableExists( $name ) ) {
305 $this->output( "...$name table already exists.\n" );
306 } else {
307 $this->output( "Creating $name table..." );
308 $this->applyPatch( $patch, $fullpath );
309 $this->output( "ok\n" );
310 }
311 }
312
313 /**
314 * Add a new field to an existing table
315 * @param $table String Name of the table to modify
316 * @param $field String Name of the new field
317 * @param $patch String Path to the patch file
318 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
319 */
320 protected function addField( $table, $field, $patch, $fullpath = false ) {
321 if ( !$this->db->tableExists( $table ) ) {
322 $this->output( "...$table table does not exist, skipping new field patch\n" );
323 } elseif ( $this->db->fieldExists( $table, $field ) ) {
324 $this->output( "...have $field field in $table table.\n" );
325 } else {
326 $this->output( "Adding $field field to table $table..." );
327 $this->applyPatch( $patch, $fullpath );
328 $this->output( "ok\n" );
329 }
330 }
331
332 /**
333 * Add a new index to an existing table
334 * @param $table String Name of the table to modify
335 * @param $index String Name of the new 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 protected function addIndex( $table, $index, $patch, $fullpath = false ) {
340 if ( $this->db->indexExists( $table, $index ) ) {
341 $this->output( "...$index key already set on $table table.\n" );
342 } else {
343 $this->output( "Adding $index key to table $table... " );
344 $this->applyPatch( $patch, $fullpath );
345 $this->output( "ok\n" );
346 }
347 }
348
349 /**
350 * Drop a field from an existing table
351 *
352 * @param $table String Name of the table to modify
353 * @param $field String Name of the old field
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 protected function dropField( $table, $field, $patch, $fullpath = false ) {
358 if ( $this->db->fieldExists( $table, $field ) ) {
359 $this->output( "Table $table contains $field field. Dropping... " );
360 $this->applyPatch( $patch, $fullpath );
361 $this->output( "ok\n" );
362 } else {
363 $this->output( "...$table table does not contain $field field.\n" );
364 }
365 }
366
367 /**
368 * Drop an index from an existing table
369 *
370 * @param $table String: Name of the table to modify
371 * @param $index String: Name of the old index
372 * @param $patch String: Path to the patch file
373 * @param $fullpath Boolean: Whether to treat $patch path as a relative or not
374 */
375 protected function dropIndex( $table, $index, $patch, $fullpath = false ) {
376 if ( $this->db->indexExists( $table, $index ) ) {
377 $this->output( "Dropping $index from table $table... " );
378 $this->applyPatch( $patch, $fullpath );
379 $this->output( "ok\n" );
380 } else {
381 $this->output( "...$index key doesn't exist.\n" );
382 }
383 }
384
385 /**
386 * Modify an existing field
387 *
388 * @param $table String: name of the table to which the field belongs
389 * @param $field String: name of the field to modify
390 * @param $patch String: path to the patch file
391 * @param $fullpath Boolean: whether to treat $patch path as a relative or not
392 */
393 public function modifyField( $table, $field, $patch, $fullpath = false ) {
394 if ( !$this->db->tableExists( $table ) ) {
395 $this->output( "...$table table does not exist, skipping modify field patch\n" );
396 } elseif ( !$this->db->fieldExists( $table, $field ) ) {
397 $this->output( "...$field field does not exist in $table table, skipping modify field patch\n" );
398 } else {
399 $this->output( "Modifying $field field of table $table..." );
400 $this->applyPatch( $patch, $fullpath );
401 $this->output( "ok\n" );
402 }
403 }
404
405 /**
406 * Purge the objectcache table
407 */
408 protected function purgeCache() {
409 # We can't guarantee that the user will be able to use TRUNCATE,
410 # but we know that DELETE is available to us
411 $this->output( "Purging caches..." );
412 $this->db->delete( 'objectcache', '*', __METHOD__ );
413 $this->output( "done.\n" );
414 }
415
416 /**
417 * Check the site_stats table is not properly populated.
418 */
419 protected function checkStats() {
420 $this->output( "Checking site_stats row..." );
421 $row = $this->db->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__ );
422 if ( $row === false ) {
423 $this->output( "data is missing! rebuilding...\n" );
424 } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
425 $this->output( "missing ss_total_pages, rebuilding...\n" );
426 } else {
427 $this->output( "done.\n" );
428 return;
429 }
430 SiteStatsInit::doAllAndCommit( false );
431 }
432
433 # Common updater functions
434
435 protected function doActiveUsersInit() {
436 $activeUsers = $this->db->selectField( 'site_stats', 'ss_active_users', false, __METHOD__ );
437 if ( $activeUsers == -1 ) {
438 $activeUsers = $this->db->selectField( 'recentchanges',
439 'COUNT( DISTINCT rc_user_text )',
440 array( 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ), __METHOD__
441 );
442 $this->db->update( 'site_stats',
443 array( 'ss_active_users' => intval( $activeUsers ) ),
444 array( 'ss_row_id' => 1 ), __METHOD__, array( 'LIMIT' => 1 )
445 );
446 }
447 $this->output( "...ss_active_users user count set...\n" );
448 }
449
450 protected function doLogSearchPopulation() {
451 if ( $this->updateRowExists( 'populate log_search' ) ) {
452 $this->output( "...log_search table already populated.\n" );
453 return;
454 }
455
456 $this->output(
457 "Populating log_search table, printing progress markers. For large\n" .
458 "databases, you may want to hit Ctrl-C and do this manually with\n" .
459 "maintenance/populateLogSearch.php.\n" );
460 $task = new PopulateLogSearch();
461 $task->execute();
462 $this->output( "Done populating log_search table.\n" );
463 }
464
465 protected function doUpdateTranscacheField() {
466 if ( $this->updateRowExists( 'convert transcache field' ) ) {
467 $this->output( "...transcache tc_time already converted.\n" );
468 return;
469 }
470
471 $this->output( "Converting tc_time from UNIX epoch to MediaWiki timestamp... " );
472 $this->applyPatch( 'patch-tc-timestamp.sql' );
473 $this->output( "ok\n" );
474 }
475
476 protected function doCollationUpdate() {
477 global $wgCategoryCollation;
478 if ( $this->db->selectField(
479 'categorylinks',
480 'COUNT(*)',
481 'cl_collation != ' . $this->db->addQuotes( $wgCategoryCollation ),
482 __METHOD__
483 ) == 0 ) {
484 $this->output( "...collations up-to-date.\n" );
485 return;
486 }
487
488 $task = new UpdateCollation();
489 $task->execute();
490 }
491 }