Merge "Base class for objects accessign databases."
[lhc/web/wiklou.git] / includes / installer / DatabaseUpdater.php
1 <?php
2 /**
3 * DBMS-specific updater helper.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Deployment
22 */
23
24 require_once( __DIR__ . '/../../maintenance/Maintenance.php' );
25
26 /**
27 * Class for handling database updates. Roughly based off of updaters.inc, with
28 * a few improvements :)
29 *
30 * @ingroup Deployment
31 * @since 1.17
32 */
33 abstract class DatabaseUpdater {
34
35 /**
36 * Array of updates to perform on the database
37 *
38 * @var array
39 */
40 protected $updates = array();
41
42 /**
43 * Array of updates that were skipped
44 *
45 * @var array
46 */
47 protected $updatesSkipped = array();
48
49 /**
50 * List of extension-provided database updates
51 * @var array
52 */
53 protected $extensionUpdates = array();
54
55 /**
56 * Handle to the database subclass
57 *
58 * @var DatabaseBase
59 */
60 protected $db;
61
62 protected $shared = false;
63
64 /**
65 * Scripts to run after database update
66 * Should be a subclass of LoggedUpdateMaintenance
67 */
68 protected $postDatabaseUpdateMaintenance = array(
69 'DeleteDefaultMessages',
70 'PopulateRevisionLength',
71 'PopulateRevisionSha1',
72 'PopulateImageSha1',
73 'FixExtLinksProtocolRelative',
74 'PopulateFilearchiveSha1',
75 );
76
77 /**
78 * File handle for SQL output.
79 *
80 * @var Filehandle
81 */
82 protected $fileHandle = null;
83
84 /**
85 * Flag specifying whether or not to skip schema (e.g. SQL-only) updates.
86 *
87 * @var bool
88 */
89 protected $skipSchema = false;
90
91 /**
92 * Constructor
93 *
94 * @param $db DatabaseBase object to perform updates on
95 * @param $shared bool Whether to perform updates on shared tables
96 * @param $maintenance Maintenance Maintenance object which created us
97 */
98 protected function __construct( DatabaseBase &$db, $shared, Maintenance $maintenance = null ) {
99 $this->db = $db;
100 $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
101 $this->shared = $shared;
102 if ( $maintenance ) {
103 $this->maintenance = $maintenance;
104 $this->fileHandle = $maintenance->fileHandle;
105 } else {
106 $this->maintenance = new FakeMaintenance;
107 }
108 $this->maintenance->setDB( $db );
109 $this->initOldGlobals();
110 $this->loadExtensions();
111 wfRunHooks( 'LoadExtensionSchemaUpdates', array( $this ) );
112 }
113
114 /**
115 * Initialize all of the old globals. One day this should all become
116 * something much nicer
117 */
118 private function initOldGlobals() {
119 global $wgExtNewTables, $wgExtNewFields, $wgExtPGNewFields,
120 $wgExtPGAlteredFields, $wgExtNewIndexes, $wgExtModifiedFields;
121
122 # For extensions only, should be populated via hooks
123 # $wgDBtype should be checked to specifiy the proper file
124 $wgExtNewTables = array(); // table, dir
125 $wgExtNewFields = array(); // table, column, dir
126 $wgExtPGNewFields = array(); // table, column, column attributes; for PostgreSQL
127 $wgExtPGAlteredFields = array(); // table, column, new type, conversion method; for PostgreSQL
128 $wgExtNewIndexes = array(); // table, index, dir
129 $wgExtModifiedFields = array(); // table, index, dir
130 }
131
132 /**
133 * Loads LocalSettings.php, if needed, and initialises everything needed for LoadExtensionSchemaUpdates hook
134 */
135 private function loadExtensions() {
136 if ( !defined( 'MEDIAWIKI_INSTALL' ) ) {
137 return; // already loaded
138 }
139 $vars = Installer::getExistingLocalSettings();
140 if ( !$vars ) {
141 return; // no LocalSettings found
142 }
143 if ( !isset( $vars['wgHooks'] ) || !isset( $vars['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
144 return;
145 }
146 global $wgHooks, $wgAutoloadClasses;
147 $wgHooks['LoadExtensionSchemaUpdates'] = $vars['wgHooks']['LoadExtensionSchemaUpdates'];
148 $wgAutoloadClasses = $wgAutoloadClasses + $vars['wgAutoloadClasses'];
149 }
150
151 /**
152 * @throws MWException
153 * @param DatabaseBase $db
154 * @param bool $shared
155 * @param null $maintenance
156 * @return DatabaseUpdater
157 */
158 public static function newForDB( &$db, $shared = false, $maintenance = null ) {
159 $type = $db->getType();
160 if( in_array( $type, Installer::getDBTypes() ) ) {
161 $class = ucfirst( $type ) . 'Updater';
162 return new $class( $db, $shared, $maintenance );
163 } else {
164 throw new MWException( __METHOD__ . ' called for unsupported $wgDBtype' );
165 }
166 }
167
168 /**
169 * Get a database connection to run updates
170 *
171 * @return DatabaseBase
172 */
173 public function getDB() {
174 return $this->db;
175 }
176
177 /**
178 * Output some text. If we're running from web, escape the text first.
179 *
180 * @param $str String: Text to output
181 */
182 public function output( $str ) {
183 if ( $this->maintenance->isQuiet() ) {
184 return;
185 }
186 global $wgCommandLineMode;
187 if( !$wgCommandLineMode ) {
188 $str = htmlspecialchars( $str );
189 }
190 echo $str;
191 flush();
192 }
193
194 /**
195 * Add a new update coming from an extension. This should be called by
196 * extensions while executing the LoadExtensionSchemaUpdates hook.
197 *
198 * @since 1.17
199 *
200 * @param $update Array: the update to run. Format is the following:
201 * first item is the callback function, it also can be a
202 * simple string with the name of a function in this class,
203 * following elements are parameters to the function.
204 * Note that callback functions will receive this object as
205 * first parameter.
206 */
207 public function addExtensionUpdate( array $update ) {
208 $this->extensionUpdates[] = $update;
209 }
210
211 /**
212 * Convenience wrapper for addExtensionUpdate() when adding a new table (which
213 * is the most common usage of updaters in an extension)
214 *
215 * @since 1.18
216 *
217 * @param $tableName String Name of table to create
218 * @param $sqlPath String Full path to the schema file
219 */
220 public function addExtensionTable( $tableName, $sqlPath ) {
221 $this->extensionUpdates[] = array( 'addTable', $tableName, $sqlPath, true );
222 }
223
224 /**
225 * @since 1.19
226 *
227 * @param $tableName string
228 * @param $indexName string
229 * @param $sqlPath string
230 */
231 public function addExtensionIndex( $tableName, $indexName, $sqlPath ) {
232 $this->extensionUpdates[] = array( 'addIndex', $tableName, $indexName, $sqlPath, true );
233 }
234
235 /**
236 *
237 * @since 1.19
238 *
239 * @param $tableName string
240 * @param $columnName string
241 * @param $sqlPath string
242 */
243 public function addExtensionField( $tableName, $columnName, $sqlPath ) {
244 $this->extensionUpdates[] = array( 'addField', $tableName, $columnName, $sqlPath, true );
245 }
246
247 /**
248 *
249 * @since 1.20
250 *
251 * @param $tableName string
252 * @param $columnName string
253 * @param $sqlPath string
254 */
255 public function dropExtensionField( $tableName, $columnName, $sqlPath ) {
256 $this->extensionUpdates[] = array( 'dropField', $tableName, $columnName, $sqlPath, true );
257 }
258
259 /**
260 *
261 * @since 1.20
262 *
263 * @param $tableName string
264 * @param $sqlPath string
265 */
266 public function dropExtensionTable( $tableName, $sqlPath ) {
267 $this->extensionUpdates[] = array( 'dropTable', $tableName, $sqlPath, true );
268 }
269
270 /**
271 *
272 * @since 1.20
273 *
274 * @param $tableName string
275 * @return bool
276 */
277 public function tableExists( $tableName ) {
278 return ( $this->db->tableExists( $tableName, __METHOD__ ) );
279 }
280
281 /**
282 * Add a maintenance script to be run after the database updates are complete.
283 *
284 * Script should subclass LoggedUpdateMaintenance
285 *
286 * @since 1.19
287 *
288 * @param $class string Name of a Maintenance subclass
289 */
290 public function addPostDatabaseUpdateMaintenance( $class ) {
291 $this->postDatabaseUpdateMaintenance[] = $class;
292 }
293
294 /**
295 * Get the list of extension-defined updates
296 *
297 * @return Array
298 */
299 protected function getExtensionUpdates() {
300 return $this->extensionUpdates;
301 }
302
303 /**
304 * @since 1.17
305 *
306 * @return array
307 */
308 public function getPostDatabaseUpdateMaintenance() {
309 return $this->postDatabaseUpdateMaintenance;
310 }
311
312 /**
313 * @since 1.21
314 *
315 * Writes the schema updates desired to a file for the DB Admin to run.
316 */
317 private function writeSchemaUpdateFile( $schemaUpdate = array() ) {
318 $updates = $this->updatesSkipped;
319 $this->updatesSkipped = array();
320
321 foreach( $updates as $funcList ) {
322 $func = $funcList[0];
323 $arg = $funcList[1];
324 $ret = call_user_func_array( $func, $arg );
325 flush();
326 $this->updatesSkipped[] = $arg;
327 }
328 }
329
330 /**
331 * Do all the updates
332 *
333 * @param $what Array: what updates to perform
334 */
335 public function doUpdates( $what = array( 'core', 'extensions', 'stats' ) ) {
336 global $wgVersion, $wgLocalisationCacheConf;
337
338 $this->db->begin( __METHOD__ );
339 $what = array_flip( $what );
340 $this->skipSchema = isset( $what['noschema'] ) || $this->fileHandle !== null;
341 if ( isset( $what['core'] ) ) {
342 $this->runUpdates( $this->getCoreUpdateList(), false );
343 }
344 if ( isset( $what['extensions'] ) ) {
345 $this->runUpdates( $this->getOldGlobalUpdates(), false );
346 $this->runUpdates( $this->getExtensionUpdates(), true );
347 }
348
349 if ( isset( $what['stats'] ) ) {
350 $this->checkStats();
351 }
352
353 if ( isset( $what['purge'] ) ) {
354 $this->purgeCache();
355
356 if ( $wgLocalisationCacheConf['manualRecache'] ) {
357 $this->rebuildLocalisationCache();
358 }
359 }
360
361 $this->setAppliedUpdates( $wgVersion, $this->updates );
362
363 if( $this->fileHandle ) {
364 $this->skipSchema = false;
365 $this->writeSchemaUpdateFile( );
366 $this->setAppliedUpdates( "$wgVersion-schema", $this->updatesSkipped );
367 }
368
369 $this->db->commit( __METHOD__ );
370 }
371
372 /**
373 * Helper function for doUpdates()
374 *
375 * @param $updates Array of updates to run
376 * @param $passSelf Boolean: whether to pass this object we calling external
377 * functions
378 */
379 private function runUpdates( array $updates, $passSelf ) {
380 $updatesDone = array();
381 $updatesSkipped = array();
382 foreach ( $updates as $params ) {
383 $func = array_shift( $params );
384 if( !is_array( $func ) && method_exists( $this, $func ) ) {
385 $func = array( $this, $func );
386 } elseif ( $passSelf ) {
387 array_unshift( $params, $this );
388 }
389 $ret = call_user_func_array( $func, $params );
390 flush();
391 if( $ret !== false ) {
392 $updatesDone[] = $params;
393 } else {
394 $updatesSkipped[] = array( $func, $params );
395 }
396 }
397 $this->updatesSkipped = array_merge( $this->updatesSkipped, $updatesSkipped );
398 $this->updates = array_merge( $this->updates, $updatesDone );
399 }
400
401 /**
402 * @param $version
403 * @param $updates array
404 */
405 protected function setAppliedUpdates( $version, $updates = array() ) {
406 $this->db->clearFlag( DBO_DDLMODE );
407 if( !$this->canUseNewUpdatelog() ) {
408 return;
409 }
410 $key = "updatelist-$version-" . time();
411 $this->db->insert( 'updatelog',
412 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
413 __METHOD__ );
414 $this->db->setFlag( DBO_DDLMODE );
415 }
416
417 /**
418 * Helper function: check if the given key is present in the updatelog table.
419 * Obviously, only use this for updates that occur after the updatelog table was
420 * created!
421 * @param $key String Name of the key to check for
422 *
423 * @return bool
424 */
425 public function updateRowExists( $key ) {
426 $row = $this->db->selectRow(
427 'updatelog',
428 '1',
429 array( 'ul_key' => $key ),
430 __METHOD__
431 );
432 return (bool)$row;
433 }
434
435 /**
436 * Helper function: Add a key to the updatelog table
437 * Obviously, only use this for updates that occur after the updatelog table was
438 * created!
439 * @param $key String Name of key to insert
440 * @param $val String [optional] value to insert along with the key
441 */
442 public function insertUpdateRow( $key, $val = null ) {
443 $this->db->clearFlag( DBO_DDLMODE );
444 $values = array( 'ul_key' => $key );
445 if( $val && $this->canUseNewUpdatelog() ) {
446 $values['ul_value'] = $val;
447 }
448 $this->db->insert( 'updatelog', $values, __METHOD__, 'IGNORE' );
449 $this->db->setFlag( DBO_DDLMODE );
450 }
451
452 /**
453 * Updatelog was changed in 1.17 to have a ul_value column so we can record
454 * more information about what kind of updates we've done (that's what this
455 * class does). Pre-1.17 wikis won't have this column, and really old wikis
456 * might not even have updatelog at all
457 *
458 * @return boolean
459 */
460 protected function canUseNewUpdatelog() {
461 return $this->db->tableExists( 'updatelog', __METHOD__ ) &&
462 $this->db->fieldExists( 'updatelog', 'ul_value', __METHOD__ );
463 }
464
465 /**
466 * Before 1.17, we used to handle updates via stuff like
467 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
468 * of this in 1.17 but we want to remain back-compatible for a while. So
469 * load up these old global-based things into our update list.
470 *
471 * @return array
472 */
473 protected function getOldGlobalUpdates() {
474 global $wgExtNewFields, $wgExtNewTables, $wgExtModifiedFields,
475 $wgExtNewIndexes, $wgSharedDB, $wgSharedTables;
476
477 $doUser = $this->shared ?
478 $wgSharedDB && in_array( 'user', $wgSharedTables ) :
479 !$wgSharedDB || !in_array( 'user', $wgSharedTables );
480
481 $updates = array();
482
483 foreach ( $wgExtNewTables as $tableRecord ) {
484 $updates[] = array(
485 'addTable', $tableRecord[0], $tableRecord[1], true
486 );
487 }
488
489 foreach ( $wgExtNewFields as $fieldRecord ) {
490 if ( $fieldRecord[0] != 'user' || $doUser ) {
491 $updates[] = array(
492 'addField', $fieldRecord[0], $fieldRecord[1],
493 $fieldRecord[2], true
494 );
495 }
496 }
497
498 foreach ( $wgExtNewIndexes as $fieldRecord ) {
499 $updates[] = array(
500 'addIndex', $fieldRecord[0], $fieldRecord[1],
501 $fieldRecord[2], true
502 );
503 }
504
505 foreach ( $wgExtModifiedFields as $fieldRecord ) {
506 $updates[] = array(
507 'modifyField', $fieldRecord[0], $fieldRecord[1],
508 $fieldRecord[2], true
509 );
510 }
511
512 return $updates;
513 }
514
515 /**
516 * Get an array of updates to perform on the database. Should return a
517 * multi-dimensional array. The main key is the MediaWiki version (1.12,
518 * 1.13...) with the values being arrays of updates, identical to how
519 * updaters.inc did it (for now)
520 *
521 * @return Array
522 */
523 protected abstract function getCoreUpdateList();
524
525 /**
526 * Append an SQL fragment to the open file handle.
527 *
528 * @param $filename String: File name to open
529 */
530 public function copyFile( $filename ) {
531 $this->db->sourceFile( $filename, false, false, false,
532 array( $this, 'appendLine' )
533 );
534 }
535
536 /**
537 * Append a line to the open filehandle. The line is assumed to
538 * be a complete SQL statement.
539 *
540 * This is used as a callback for for sourceLine().
541 *
542 * @param $line String text to append to the file
543 * @return Boolean false to skip actually executing the file
544 * @throws MWException
545 */
546 public function appendLine( $line ) {
547 $line = rtrim( $line ) . ";\n";
548 if( fwrite( $this->fileHandle, $line ) === false ) {
549 throw new MWException( "trouble writing file" );
550 }
551 return false;
552 }
553
554 /**
555 * Applies a SQL patch
556 * @param $path String Path to the patch file
557 * @param $isFullPath Boolean Whether to treat $path as a relative or not
558 * @param $msg String Description of the patch
559 * @return boolean false if patch is skipped.
560 */
561 protected function applyPatch( $path, $isFullPath = false, $msg = null ) {
562 if ( $msg === null ) {
563 $msg = "Applying $path patch";
564 }
565 if ( $this->skipSchema ) {
566 $this->output( "...skipping schema change ($msg).\n" );
567 return false;
568 }
569
570 $this->output( "$msg ..." );
571
572 if ( !$isFullPath ) {
573 $path = $this->db->patchPath( $path );
574 }
575 if( $this->fileHandle !== null ) {
576 $this->copyFile( $path );
577 } else {
578 $this->db->sourceFile( $path );
579 }
580 $this->output( "done.\n" );
581 return true;
582 }
583
584 /**
585 * Add a new table to the database
586 * @param $name String Name of the new table
587 * @param $patch String Path to the patch file
588 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
589 * @return Boolean false if this was skipped because schema changes are skipped
590 */
591 protected function addTable( $name, $patch, $fullpath = false ) {
592 if ( $this->db->tableExists( $name, __METHOD__ ) ) {
593 $this->output( "...$name table already exists.\n" );
594 } else {
595 return $this->applyPatch( $patch, $fullpath, "Creating $name table" );
596 }
597 return true;
598 }
599
600 /**
601 * Add a new field to an existing table
602 * @param $table String Name of the table to modify
603 * @param $field String Name of the new field
604 * @param $patch String Path to the patch file
605 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
606 * @return Boolean false if this was skipped because schema changes are skipped
607 */
608 protected function addField( $table, $field, $patch, $fullpath = false ) {
609 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
610 $this->output( "...$table table does not exist, skipping new field patch.\n" );
611 } elseif ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
612 $this->output( "...have $field field in $table table.\n" );
613 } else {
614 return $this->applyPatch( $patch, $fullpath, "Adding $field field to table $table" );
615 }
616 return true;
617 }
618
619 /**
620 * Add a new index to an existing table
621 * @param $table String Name of the table to modify
622 * @param $index String Name of the new index
623 * @param $patch String Path to the patch file
624 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
625 * @return Boolean false if this was skipped because schema changes are skipped
626 */
627 protected function addIndex( $table, $index, $patch, $fullpath = false ) {
628 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
629 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
630 return false;
631 } else if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
632 $this->output( "...index $index already set on $table table.\n" );
633 } else {
634 return $this->applyPatch( $patch, $fullpath, "Adding index $index to table $table" );
635 }
636 return true;
637 }
638
639 /**
640 * Drop a field from an existing table
641 *
642 * @param $table String Name of the table to modify
643 * @param $field String Name of the old field
644 * @param $patch String Path to the patch file
645 * @param $fullpath Boolean Whether to treat $patch path as a relative or not
646 * @return Boolean false if this was skipped because schema changes are skipped
647 */
648 protected function dropField( $table, $field, $patch, $fullpath = false ) {
649 if ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
650 return $this->applyPatch( $patch, $fullpath, "Table $table contains $field field. Dropping" );
651 } else {
652 $this->output( "...$table table does not contain $field field.\n" );
653 }
654 return true;
655 }
656
657 /**
658 * Drop an index from an existing table
659 *
660 * @param $table String: Name of the table to modify
661 * @param $index String: Name of the old index
662 * @param $patch String: Path to the patch file
663 * @param $fullpath Boolean: Whether to treat $patch path as a relative or not
664 * @return Boolean false if this was skipped because schema changes are skipped
665 */
666 protected function dropIndex( $table, $index, $patch, $fullpath = false ) {
667 if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
668 return $this->applyPatch( $patch, $fullpath, "Dropping $index index from table $table" );
669 } else {
670 $this->output( "...$index key doesn't exist.\n" );
671 }
672 return true;
673 }
674
675 /**
676 * If the specified table exists, drop it, or execute the
677 * patch if one is provided.
678 *
679 * Public @since 1.20
680 *
681 * @param $table string
682 * @param $patch string|false
683 * @param $fullpath bool
684 * @return Boolean false if this was skipped because schema changes are skipped
685 */
686 public function dropTable( $table, $patch = false, $fullpath = false ) {
687 if ( $this->db->tableExists( $table, __METHOD__ ) ) {
688 $msg = "Dropping table $table";
689
690 if ( $patch === false ) {
691 $this->output( "$msg ..." );
692 $this->db->dropTable( $table, __METHOD__ );
693 $this->output( "done.\n" );
694 }
695 else {
696 return $this->applyPatch( $patch, $fullpath, $msg );
697 }
698 } else {
699 $this->output( "...$table doesn't exist.\n" );
700 }
701 return true;
702 }
703
704 /**
705 * Modify an existing field
706 *
707 * @param $table String: name of the table to which the field belongs
708 * @param $field String: name of the field to modify
709 * @param $patch String: path to the patch file
710 * @param $fullpath Boolean: whether to treat $patch path as a relative or not
711 * @return Boolean false if this was skipped because schema changes are skipped
712 */
713 public function modifyField( $table, $field, $patch, $fullpath = false ) {
714 $updateKey = "$table-$field-$patch";
715 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
716 $this->output( "...$table table does not exist, skipping modify field patch.\n" );
717 } elseif ( !$this->db->fieldExists( $table, $field, __METHOD__ ) ) {
718 $this->output( "...$field field does not exist in $table table, skipping modify field patch.\n" );
719 } elseif( $this->updateRowExists( $updateKey ) ) {
720 $this->output( "...$field in table $table already modified by patch $patch.\n" );
721 } else {
722 return $this->applyPatch( $patch, $fullpath, "Modifying $field field of table $table" );
723 $this->insertUpdateRow( $updateKey );
724 }
725 return true;
726 }
727
728 /**
729 * Purge the objectcache table
730 */
731 public function purgeCache() {
732 global $wgLocalisationCacheConf;
733 # We can't guarantee that the user will be able to use TRUNCATE,
734 # but we know that DELETE is available to us
735 $this->output( "Purging caches..." );
736 $this->db->delete( 'objectcache', '*', __METHOD__ );
737 if ( $wgLocalisationCacheConf['manualRecache'] ) {
738 $this->rebuildLocalisationCache();
739 }
740 $this->output( "done.\n" );
741 }
742
743 /**
744 * Check the site_stats table is not properly populated.
745 */
746 protected function checkStats() {
747 $this->output( "...site_stats is populated..." );
748 $row = $this->db->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__ );
749 if ( $row === false ) {
750 $this->output( "data is missing! rebuilding...\n" );
751 } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
752 $this->output( "missing ss_total_pages, rebuilding...\n" );
753 } else {
754 $this->output( "done.\n" );
755 return;
756 }
757 SiteStatsInit::doAllAndCommit( $this->db );
758 }
759
760 # Common updater functions
761
762 /**
763 * Sets the number of active users in the site_stats table
764 */
765 protected function doActiveUsersInit() {
766 $activeUsers = $this->db->selectField( 'site_stats', 'ss_active_users', false, __METHOD__ );
767 if ( $activeUsers == -1 ) {
768 $activeUsers = $this->db->selectField( 'recentchanges',
769 'COUNT( DISTINCT rc_user_text )',
770 array( 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ), __METHOD__
771 );
772 $this->db->update( 'site_stats',
773 array( 'ss_active_users' => intval( $activeUsers ) ),
774 array( 'ss_row_id' => 1 ), __METHOD__, array( 'LIMIT' => 1 )
775 );
776 }
777 $this->output( "...ss_active_users user count set...\n" );
778 }
779
780 /**
781 * Populates the log_user_text field in the logging table
782 */
783 protected function doLogUsertextPopulation() {
784 if ( !$this->updateRowExists( 'populate log_usertext' ) ) {
785 $this->output(
786 "Populating log_user_text field, printing progress markers. For large\n" .
787 "databases, you may want to hit Ctrl-C and do this manually with\n" .
788 "maintenance/populateLogUsertext.php.\n" );
789
790 $task = $this->maintenance->runChild( 'PopulateLogUsertext' );
791 $task->execute();
792 $this->output( "done.\n" );
793 }
794 }
795
796 /**
797 * Migrate log params to new table and index for searching
798 */
799 protected function doLogSearchPopulation() {
800 if ( !$this->updateRowExists( 'populate log_search' ) ) {
801 $this->output(
802 "Populating log_search table, printing progress markers. For large\n" .
803 "databases, you may want to hit Ctrl-C and do this manually with\n" .
804 "maintenance/populateLogSearch.php.\n" );
805
806 $task = $this->maintenance->runChild( 'PopulateLogSearch' );
807 $task->execute();
808 $this->output( "done.\n" );
809 }
810 }
811
812 /**
813 * Updates the timestamps in the transcache table
814 */
815 protected function doUpdateTranscacheField() {
816 if ( $this->updateRowExists( 'convert transcache field' ) ) {
817 $this->output( "...transcache tc_time already converted.\n" );
818 return;
819 }
820
821 return $this->applyPatch( 'patch-tc-timestamp.sql', false,
822 "Converting tc_time from UNIX epoch to MediaWiki timestamp" );
823 }
824
825 /**
826 * Update CategoryLinks collation
827 */
828 protected function doCollationUpdate() {
829 global $wgCategoryCollation;
830 if ( $this->db->fieldExists( 'categorylinks', 'cl_collation', __METHOD__ ) ) {
831 if ( $this->db->selectField(
832 'categorylinks',
833 'COUNT(*)',
834 'cl_collation != ' . $this->db->addQuotes( $wgCategoryCollation ),
835 __METHOD__
836 ) == 0 ) {
837 $this->output( "...collations up-to-date.\n" );
838 return;
839 }
840
841 $this->output( "Updating category collations..." );
842 $task = $this->maintenance->runChild( 'UpdateCollation' );
843 $task->execute();
844 $this->output( "...done.\n" );
845 }
846 }
847
848 /**
849 * Migrates user options from the user table blob to user_properties
850 */
851 protected function doMigrateUserOptions() {
852 if( $this->db->tableExists( 'user_properties' ) ) {
853 $cl = $this->maintenance->runChild( 'ConvertUserOptions', 'convertUserOptions.php' );
854 $cl->execute();
855 $this->output( "done.\n" );
856 }
857 }
858
859 /**
860 * Rebuilds the localisation cache
861 */
862 protected function rebuildLocalisationCache() {
863 /**
864 * @var $cl RebuildLocalisationCache
865 */
866 $cl = $this->maintenance->runChild( 'RebuildLocalisationCache', 'rebuildLocalisationCache.php' );
867 $this->output( "Rebuilding localisation cache...\n" );
868 $cl->setForce();
869 $cl->execute();
870 $this->output( "done.\n" );
871 }
872 }