2425005ff877c70492fc3ebb94df4d17d5fd3243
[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 use MediaWiki\MediaWikiServices;
24
25 require_once __DIR__ . '/../../maintenance/Maintenance.php';
26
27 /**
28 * Class for handling database updates. Roughly based off of updaters.inc, with
29 * a few improvements :)
30 *
31 * @ingroup Deployment
32 * @since 1.17
33 */
34 abstract class DatabaseUpdater {
35 protected static $updateCounter = 0;
36
37 /**
38 * Array of updates to perform on the database
39 *
40 * @var array
41 */
42 protected $updates = [];
43
44 /**
45 * Array of updates that were skipped
46 *
47 * @var array
48 */
49 protected $updatesSkipped = [];
50
51 /**
52 * List of extension-provided database updates
53 * @var array
54 */
55 protected $extensionUpdates = [];
56
57 /**
58 * Handle to the database subclass
59 *
60 * @var DatabaseBase
61 */
62 protected $db;
63
64 protected $shared = false;
65
66 /**
67 * @var string[] Scripts to run after database update
68 * Should be a subclass of LoggedUpdateMaintenance
69 */
70 protected $postDatabaseUpdateMaintenance = [
71 DeleteDefaultMessages::class,
72 PopulateRevisionLength::class,
73 PopulateRevisionSha1::class,
74 PopulateImageSha1::class,
75 FixExtLinksProtocolRelative::class,
76 PopulateFilearchiveSha1::class,
77 PopulateBacklinkNamespace::class,
78 FixDefaultJsonContentPages::class,
79 CleanupEmptyCategories::class,
80 ];
81
82 /**
83 * File handle for SQL output.
84 *
85 * @var resource
86 */
87 protected $fileHandle = null;
88
89 /**
90 * Flag specifying whether or not to skip schema (e.g. SQL-only) updates.
91 *
92 * @var bool
93 */
94 protected $skipSchema = false;
95
96 /**
97 * Hold the value of $wgContentHandlerUseDB during the upgrade.
98 */
99 protected $holdContentHandlerUseDB = true;
100
101 /**
102 * Constructor
103 *
104 * @param DatabaseBase $db To perform updates on
105 * @param bool $shared Whether to perform updates on shared tables
106 * @param Maintenance $maintenance Maintenance object which created us
107 */
108 protected function __construct( DatabaseBase &$db, $shared, Maintenance $maintenance = null ) {
109 $this->db = $db;
110 $this->db->setFlag( DBO_DDLMODE ); // For Oracle's handling of schema files
111 $this->shared = $shared;
112 if ( $maintenance ) {
113 $this->maintenance = $maintenance;
114 $this->fileHandle = $maintenance->fileHandle;
115 } else {
116 $this->maintenance = new FakeMaintenance;
117 }
118 $this->maintenance->setDB( $db );
119 $this->initOldGlobals();
120 $this->loadExtensions();
121 Hooks::run( 'LoadExtensionSchemaUpdates', [ $this ] );
122 }
123
124 /**
125 * Initialize all of the old globals. One day this should all become
126 * something much nicer
127 */
128 private function initOldGlobals() {
129 global $wgExtNewTables, $wgExtNewFields, $wgExtPGNewFields,
130 $wgExtPGAlteredFields, $wgExtNewIndexes, $wgExtModifiedFields;
131
132 # For extensions only, should be populated via hooks
133 # $wgDBtype should be checked to specifiy the proper file
134 $wgExtNewTables = []; // table, dir
135 $wgExtNewFields = []; // table, column, dir
136 $wgExtPGNewFields = []; // table, column, column attributes; for PostgreSQL
137 $wgExtPGAlteredFields = []; // table, column, new type, conversion method; for PostgreSQL
138 $wgExtNewIndexes = []; // table, index, dir
139 $wgExtModifiedFields = []; // table, index, dir
140 }
141
142 /**
143 * Loads LocalSettings.php, if needed, and initialises everything needed for
144 * LoadExtensionSchemaUpdates hook.
145 */
146 private function loadExtensions() {
147 if ( !defined( 'MEDIAWIKI_INSTALL' ) ) {
148 return; // already loaded
149 }
150 $vars = Installer::getExistingLocalSettings();
151
152 $registry = ExtensionRegistry::getInstance();
153 $queue = $registry->getQueue();
154 // Don't accidentally load extensions in the future
155 $registry->clearQueue();
156
157 // This will automatically add "AutoloadClasses" to $wgAutoloadClasses
158 $data = $registry->readFromQueue( $queue );
159 $hooks = [ 'wgHooks' => [ 'LoadExtensionSchemaUpdates' => [] ] ];
160 if ( isset( $data['globals']['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
161 $hooks = $data['globals']['wgHooks']['LoadExtensionSchemaUpdates'];
162 }
163 if ( $vars && isset( $vars['wgHooks']['LoadExtensionSchemaUpdates'] ) ) {
164 $hooks = array_merge_recursive( $hooks, $vars['wgHooks']['LoadExtensionSchemaUpdates'] );
165 }
166 global $wgHooks, $wgAutoloadClasses;
167 $wgHooks['LoadExtensionSchemaUpdates'] = $hooks;
168 if ( $vars && isset( $vars['wgAutoloadClasses'] ) ) {
169 $wgAutoloadClasses += $vars['wgAutoloadClasses'];
170 }
171 }
172
173 /**
174 * @param Database $db
175 * @param bool $shared
176 * @param Maintenance $maintenance
177 *
178 * @throws MWException
179 * @return DatabaseUpdater
180 */
181 public static function newForDB( Database $db, $shared = false, $maintenance = null ) {
182 $type = $db->getType();
183 if ( in_array( $type, Installer::getDBTypes() ) ) {
184 $class = ucfirst( $type ) . 'Updater';
185
186 return new $class( $db, $shared, $maintenance );
187 } else {
188 throw new MWException( __METHOD__ . ' called for unsupported $wgDBtype' );
189 }
190 }
191
192 /**
193 * Get a database connection to run updates
194 *
195 * @return DatabaseBase
196 */
197 public function getDB() {
198 return $this->db;
199 }
200
201 /**
202 * Output some text. If we're running from web, escape the text first.
203 *
204 * @param string $str Text to output
205 */
206 public function output( $str ) {
207 if ( $this->maintenance->isQuiet() ) {
208 return;
209 }
210 global $wgCommandLineMode;
211 if ( !$wgCommandLineMode ) {
212 $str = htmlspecialchars( $str );
213 }
214 echo $str;
215 flush();
216 }
217
218 /**
219 * Add a new update coming from an extension. This should be called by
220 * extensions while executing the LoadExtensionSchemaUpdates hook.
221 *
222 * @since 1.17
223 *
224 * @param array $update The update to run. Format is the following:
225 * first item is the callback function, it also can be a
226 * simple string with the name of a function in this class,
227 * following elements are parameters to the function.
228 * Note that callback functions will receive this object as
229 * first parameter.
230 */
231 public function addExtensionUpdate( array $update ) {
232 $this->extensionUpdates[] = $update;
233 }
234
235 /**
236 * Convenience wrapper for addExtensionUpdate() when adding a new table (which
237 * is the most common usage of updaters in an extension)
238 *
239 * @since 1.18
240 *
241 * @param string $tableName Name of table to create
242 * @param string $sqlPath Full path to the schema file
243 */
244 public function addExtensionTable( $tableName, $sqlPath ) {
245 $this->extensionUpdates[] = [ 'addTable', $tableName, $sqlPath, true ];
246 }
247
248 /**
249 * @since 1.19
250 *
251 * @param string $tableName
252 * @param string $indexName
253 * @param string $sqlPath
254 */
255 public function addExtensionIndex( $tableName, $indexName, $sqlPath ) {
256 $this->extensionUpdates[] = [ 'addIndex', $tableName, $indexName, $sqlPath, true ];
257 }
258
259 /**
260 *
261 * @since 1.19
262 *
263 * @param string $tableName
264 * @param string $columnName
265 * @param string $sqlPath
266 */
267 public function addExtensionField( $tableName, $columnName, $sqlPath ) {
268 $this->extensionUpdates[] = [ 'addField', $tableName, $columnName, $sqlPath, true ];
269 }
270
271 /**
272 *
273 * @since 1.20
274 *
275 * @param string $tableName
276 * @param string $columnName
277 * @param string $sqlPath
278 */
279 public function dropExtensionField( $tableName, $columnName, $sqlPath ) {
280 $this->extensionUpdates[] = [ 'dropField', $tableName, $columnName, $sqlPath, true ];
281 }
282
283 /**
284 * Drop an index from an extension table
285 *
286 * @since 1.21
287 *
288 * @param string $tableName The table name
289 * @param string $indexName The index name
290 * @param string $sqlPath The path to the SQL change path
291 */
292 public function dropExtensionIndex( $tableName, $indexName, $sqlPath ) {
293 $this->extensionUpdates[] = [ 'dropIndex', $tableName, $indexName, $sqlPath, true ];
294 }
295
296 /**
297 *
298 * @since 1.20
299 *
300 * @param string $tableName
301 * @param string $sqlPath
302 */
303 public function dropExtensionTable( $tableName, $sqlPath ) {
304 $this->extensionUpdates[] = [ 'dropTable', $tableName, $sqlPath, true ];
305 }
306
307 /**
308 * Rename an index on an extension table
309 *
310 * @since 1.21
311 *
312 * @param string $tableName The table name
313 * @param string $oldIndexName The old index name
314 * @param string $newIndexName The new index name
315 * @param string $sqlPath The path to the SQL change path
316 * @param bool $skipBothIndexExistWarning Whether to warn if both the old
317 * and the new indexes exist. [facultative; by default, false]
318 */
319 public function renameExtensionIndex( $tableName, $oldIndexName, $newIndexName,
320 $sqlPath, $skipBothIndexExistWarning = false
321 ) {
322 $this->extensionUpdates[] = [
323 'renameIndex',
324 $tableName,
325 $oldIndexName,
326 $newIndexName,
327 $skipBothIndexExistWarning,
328 $sqlPath,
329 true
330 ];
331 }
332
333 /**
334 * @since 1.21
335 *
336 * @param string $tableName The table name
337 * @param string $fieldName The field to be modified
338 * @param string $sqlPath The path to the SQL change path
339 */
340 public function modifyExtensionField( $tableName, $fieldName, $sqlPath ) {
341 $this->extensionUpdates[] = [ 'modifyField', $tableName, $fieldName, $sqlPath, true ];
342 }
343
344 /**
345 *
346 * @since 1.20
347 *
348 * @param string $tableName
349 * @return bool
350 */
351 public function tableExists( $tableName ) {
352 return ( $this->db->tableExists( $tableName, __METHOD__ ) );
353 }
354
355 /**
356 * Add a maintenance script to be run after the database updates are complete.
357 *
358 * Script should subclass LoggedUpdateMaintenance
359 *
360 * @since 1.19
361 *
362 * @param string $class Name of a Maintenance subclass
363 */
364 public function addPostDatabaseUpdateMaintenance( $class ) {
365 $this->postDatabaseUpdateMaintenance[] = $class;
366 }
367
368 /**
369 * Get the list of extension-defined updates
370 *
371 * @return array
372 */
373 protected function getExtensionUpdates() {
374 return $this->extensionUpdates;
375 }
376
377 /**
378 * @since 1.17
379 *
380 * @return string[]
381 */
382 public function getPostDatabaseUpdateMaintenance() {
383 return $this->postDatabaseUpdateMaintenance;
384 }
385
386 /**
387 * @since 1.21
388 *
389 * Writes the schema updates desired to a file for the DB Admin to run.
390 * @param array $schemaUpdate
391 */
392 private function writeSchemaUpdateFile( $schemaUpdate = [] ) {
393 $updates = $this->updatesSkipped;
394 $this->updatesSkipped = [];
395
396 foreach ( $updates as $funcList ) {
397 $func = $funcList[0];
398 $arg = $funcList[1];
399 $origParams = $funcList[2];
400 call_user_func_array( $func, $arg );
401 flush();
402 $this->updatesSkipped[] = $origParams;
403 }
404 }
405
406 /**
407 * Get appropriate schema variables in the current database connection.
408 *
409 * This should be called after any request data has been imported, but before
410 * any write operations to the database. The result should be passed to the DB
411 * setSchemaVars() method.
412 *
413 * @return array
414 * @since 1.28
415 */
416 public function getSchemaVars() {
417 return []; // DB-type specific
418 }
419
420 /**
421 * Do all the updates
422 *
423 * @param array $what What updates to perform
424 */
425 public function doUpdates( $what = [ 'core', 'extensions', 'stats' ] ) {
426 global $wgVersion;
427
428 $this->db->setSchemaVars( $this->getSchemaVars() );
429
430 $what = array_flip( $what );
431 $this->skipSchema = isset( $what['noschema'] ) || $this->fileHandle !== null;
432 if ( isset( $what['core'] ) ) {
433 $this->runUpdates( $this->getCoreUpdateList(), false );
434 }
435 if ( isset( $what['extensions'] ) ) {
436 $this->runUpdates( $this->getOldGlobalUpdates(), false );
437 $this->runUpdates( $this->getExtensionUpdates(), true );
438 }
439
440 if ( isset( $what['stats'] ) ) {
441 $this->checkStats();
442 }
443
444 $this->setAppliedUpdates( $wgVersion, $this->updates );
445
446 if ( $this->fileHandle ) {
447 $this->skipSchema = false;
448 $this->writeSchemaUpdateFile();
449 $this->setAppliedUpdates( "$wgVersion-schema", $this->updatesSkipped );
450 }
451 }
452
453 /**
454 * Helper function for doUpdates()
455 *
456 * @param array $updates Array of updates to run
457 * @param bool $passSelf Whether to pass this object we calling external functions
458 */
459 private function runUpdates( array $updates, $passSelf ) {
460 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
461
462 $updatesDone = [];
463 $updatesSkipped = [];
464 foreach ( $updates as $params ) {
465 $origParams = $params;
466 $func = array_shift( $params );
467 if ( !is_array( $func ) && method_exists( $this, $func ) ) {
468 $func = [ $this, $func ];
469 } elseif ( $passSelf ) {
470 array_unshift( $params, $this );
471 }
472 $ret = call_user_func_array( $func, $params );
473 flush();
474 if ( $ret !== false ) {
475 $updatesDone[] = $origParams;
476 $lbFactory->waitForReplication();
477 } else {
478 $updatesSkipped[] = [ $func, $params, $origParams ];
479 }
480 }
481 $this->updatesSkipped = array_merge( $this->updatesSkipped, $updatesSkipped );
482 $this->updates = array_merge( $this->updates, $updatesDone );
483 }
484
485 /**
486 * @param string $version
487 * @param array $updates
488 */
489 protected function setAppliedUpdates( $version, $updates = [] ) {
490 $this->db->clearFlag( DBO_DDLMODE );
491 if ( !$this->canUseNewUpdatelog() ) {
492 return;
493 }
494 $key = "updatelist-$version-" . time() . self::$updateCounter;
495 self::$updateCounter++;
496 $this->db->insert( 'updatelog',
497 [ 'ul_key' => $key, 'ul_value' => serialize( $updates ) ],
498 __METHOD__ );
499 $this->db->setFlag( DBO_DDLMODE );
500 }
501
502 /**
503 * Helper function: check if the given key is present in the updatelog table.
504 * Obviously, only use this for updates that occur after the updatelog table was
505 * created!
506 * @param string $key Name of the key to check for
507 * @return bool
508 */
509 public function updateRowExists( $key ) {
510 $row = $this->db->selectRow(
511 'updatelog',
512 # Bug 65813
513 '1 AS X',
514 [ 'ul_key' => $key ],
515 __METHOD__
516 );
517
518 return (bool)$row;
519 }
520
521 /**
522 * Helper function: Add a key to the updatelog table
523 * Obviously, only use this for updates that occur after the updatelog table was
524 * created!
525 * @param string $key Name of key to insert
526 * @param string $val [optional] Value to insert along with the key
527 */
528 public function insertUpdateRow( $key, $val = null ) {
529 $this->db->clearFlag( DBO_DDLMODE );
530 $values = [ 'ul_key' => $key ];
531 if ( $val && $this->canUseNewUpdatelog() ) {
532 $values['ul_value'] = $val;
533 }
534 $this->db->insert( 'updatelog', $values, __METHOD__, 'IGNORE' );
535 $this->db->setFlag( DBO_DDLMODE );
536 }
537
538 /**
539 * Updatelog was changed in 1.17 to have a ul_value column so we can record
540 * more information about what kind of updates we've done (that's what this
541 * class does). Pre-1.17 wikis won't have this column, and really old wikis
542 * might not even have updatelog at all
543 *
544 * @return bool
545 */
546 protected function canUseNewUpdatelog() {
547 return $this->db->tableExists( 'updatelog', __METHOD__ ) &&
548 $this->db->fieldExists( 'updatelog', 'ul_value', __METHOD__ );
549 }
550
551 /**
552 * Returns whether updates should be executed on the database table $name.
553 * Updates will be prevented if the table is a shared table and it is not
554 * specified to run updates on shared tables.
555 *
556 * @param string $name Table name
557 * @return bool
558 */
559 protected function doTable( $name ) {
560 global $wgSharedDB, $wgSharedTables;
561
562 // Don't bother to check $wgSharedTables if there isn't a shared database
563 // or the user actually also wants to do updates on the shared database.
564 if ( $wgSharedDB === null || $this->shared ) {
565 return true;
566 }
567
568 if ( in_array( $name, $wgSharedTables ) ) {
569 $this->output( "...skipping update to shared table $name.\n" );
570 return false;
571 } else {
572 return true;
573 }
574 }
575
576 /**
577 * Before 1.17, we used to handle updates via stuff like
578 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
579 * of this in 1.17 but we want to remain back-compatible for a while. So
580 * load up these old global-based things into our update list.
581 *
582 * @return array
583 */
584 protected function getOldGlobalUpdates() {
585 global $wgExtNewFields, $wgExtNewTables, $wgExtModifiedFields,
586 $wgExtNewIndexes;
587
588 $updates = [];
589
590 foreach ( $wgExtNewTables as $tableRecord ) {
591 $updates[] = [
592 'addTable', $tableRecord[0], $tableRecord[1], true
593 ];
594 }
595
596 foreach ( $wgExtNewFields as $fieldRecord ) {
597 $updates[] = [
598 'addField', $fieldRecord[0], $fieldRecord[1],
599 $fieldRecord[2], true
600 ];
601 }
602
603 foreach ( $wgExtNewIndexes as $fieldRecord ) {
604 $updates[] = [
605 'addIndex', $fieldRecord[0], $fieldRecord[1],
606 $fieldRecord[2], true
607 ];
608 }
609
610 foreach ( $wgExtModifiedFields as $fieldRecord ) {
611 $updates[] = [
612 'modifyField', $fieldRecord[0], $fieldRecord[1],
613 $fieldRecord[2], true
614 ];
615 }
616
617 return $updates;
618 }
619
620 /**
621 * Get an array of updates to perform on the database. Should return a
622 * multi-dimensional array. The main key is the MediaWiki version (1.12,
623 * 1.13...) with the values being arrays of updates, identical to how
624 * updaters.inc did it (for now)
625 *
626 * @return array
627 */
628 abstract protected function getCoreUpdateList();
629
630 /**
631 * Append an SQL fragment to the open file handle.
632 *
633 * @param string $filename File name to open
634 */
635 public function copyFile( $filename ) {
636 $this->db->sourceFile( $filename, false, false, false,
637 [ $this, 'appendLine' ]
638 );
639 }
640
641 /**
642 * Append a line to the open filehandle. The line is assumed to
643 * be a complete SQL statement.
644 *
645 * This is used as a callback for sourceLine().
646 *
647 * @param string $line Text to append to the file
648 * @return bool False to skip actually executing the file
649 * @throws MWException
650 */
651 public function appendLine( $line ) {
652 $line = rtrim( $line ) . ";\n";
653 if ( fwrite( $this->fileHandle, $line ) === false ) {
654 throw new MWException( "trouble writing file" );
655 }
656
657 return false;
658 }
659
660 /**
661 * Applies a SQL patch
662 *
663 * @param string $path Path to the patch file
664 * @param bool $isFullPath Whether to treat $path as a relative or not
665 * @param string $msg Description of the patch
666 * @return bool False if patch is skipped.
667 */
668 protected function applyPatch( $path, $isFullPath = false, $msg = null ) {
669 if ( $msg === null ) {
670 $msg = "Applying $path patch";
671 }
672 if ( $this->skipSchema ) {
673 $this->output( "...skipping schema change ($msg).\n" );
674
675 return false;
676 }
677
678 $this->output( "$msg ..." );
679
680 if ( !$isFullPath ) {
681 $path = $this->patchPath( $this->db, $path );
682 }
683 if ( $this->fileHandle !== null ) {
684 $this->copyFile( $path );
685 } else {
686 $this->db->sourceFile( $path );
687 }
688 $this->output( "done.\n" );
689
690 return true;
691 }
692
693 /**
694 * Get the full path of a patch file. Originally based on archive()
695 * from updaters.inc. Keep in mind this always returns a patch, as
696 * it fails back to MySQL if no DB-specific patch can be found
697 *
698 * @param IDatabase $db
699 * @param string $patch The name of the patch, like patch-something.sql
700 * @return string Full path to patch file
701 */
702 public function patchPath( IDatabase $db, $patch ) {
703 global $IP;
704
705 $dbType = $db->getType();
706 if ( file_exists( "$IP/maintenance/$dbType/archives/$patch" ) ) {
707 return "$IP/maintenance/$dbType/archives/$patch";
708 } else {
709 return "$IP/maintenance/archives/$patch";
710 }
711 }
712
713 /**
714 * Add a new table to the database
715 *
716 * @param string $name Name of the new table
717 * @param string $patch Path to the patch file
718 * @param bool $fullpath Whether to treat $patch path as a relative or not
719 * @return bool False if this was skipped because schema changes are skipped
720 */
721 protected function addTable( $name, $patch, $fullpath = false ) {
722 if ( !$this->doTable( $name ) ) {
723 return true;
724 }
725
726 if ( $this->db->tableExists( $name, __METHOD__ ) ) {
727 $this->output( "...$name table already exists.\n" );
728 } else {
729 return $this->applyPatch( $patch, $fullpath, "Creating $name table" );
730 }
731
732 return true;
733 }
734
735 /**
736 * Add a new field to an existing table
737 *
738 * @param string $table Name of the table to modify
739 * @param string $field Name of the new field
740 * @param string $patch Path to the patch file
741 * @param bool $fullpath Whether to treat $patch path as a relative or not
742 * @return bool False if this was skipped because schema changes are skipped
743 */
744 protected function addField( $table, $field, $patch, $fullpath = false ) {
745 if ( !$this->doTable( $table ) ) {
746 return true;
747 }
748
749 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
750 $this->output( "...$table table does not exist, skipping new field patch.\n" );
751 } elseif ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
752 $this->output( "...have $field field in $table table.\n" );
753 } else {
754 return $this->applyPatch( $patch, $fullpath, "Adding $field field to table $table" );
755 }
756
757 return true;
758 }
759
760 /**
761 * Add a new index to an existing table
762 *
763 * @param string $table Name of the table to modify
764 * @param string $index Name of the new index
765 * @param string $patch Path to the patch file
766 * @param bool $fullpath Whether to treat $patch path as a relative or not
767 * @return bool False if this was skipped because schema changes are skipped
768 */
769 protected function addIndex( $table, $index, $patch, $fullpath = false ) {
770 if ( !$this->doTable( $table ) ) {
771 return true;
772 }
773
774 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
775 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
776 } elseif ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
777 $this->output( "...index $index already set on $table table.\n" );
778 } else {
779 return $this->applyPatch( $patch, $fullpath, "Adding index $index to table $table" );
780 }
781
782 return true;
783 }
784
785 /**
786 * Drop a field from an existing table
787 *
788 * @param string $table Name of the table to modify
789 * @param string $field Name of the old field
790 * @param string $patch Path to the patch file
791 * @param bool $fullpath Whether to treat $patch path as a relative or not
792 * @return bool False if this was skipped because schema changes are skipped
793 */
794 protected function dropField( $table, $field, $patch, $fullpath = false ) {
795 if ( !$this->doTable( $table ) ) {
796 return true;
797 }
798
799 if ( $this->db->fieldExists( $table, $field, __METHOD__ ) ) {
800 return $this->applyPatch( $patch, $fullpath, "Table $table contains $field field. Dropping" );
801 } else {
802 $this->output( "...$table table does not contain $field field.\n" );
803 }
804
805 return true;
806 }
807
808 /**
809 * Drop an index from an existing table
810 *
811 * @param string $table Name of the table to modify
812 * @param string $index Name of the index
813 * @param string $patch Path to the patch file
814 * @param bool $fullpath Whether to treat $patch path as a relative or not
815 * @return bool False if this was skipped because schema changes are skipped
816 */
817 protected function dropIndex( $table, $index, $patch, $fullpath = false ) {
818 if ( !$this->doTable( $table ) ) {
819 return true;
820 }
821
822 if ( $this->db->indexExists( $table, $index, __METHOD__ ) ) {
823 return $this->applyPatch( $patch, $fullpath, "Dropping $index index from table $table" );
824 } else {
825 $this->output( "...$index key doesn't exist.\n" );
826 }
827
828 return true;
829 }
830
831 /**
832 * Rename an index from an existing table
833 *
834 * @param string $table Name of the table to modify
835 * @param string $oldIndex Old name of the index
836 * @param string $newIndex New name of the index
837 * @param bool $skipBothIndexExistWarning Whether to warn if both the
838 * old and the new indexes exist.
839 * @param string $patch Path to the patch file
840 * @param bool $fullpath Whether to treat $patch path as a relative or not
841 * @return bool False if this was skipped because schema changes are skipped
842 */
843 protected function renameIndex( $table, $oldIndex, $newIndex,
844 $skipBothIndexExistWarning, $patch, $fullpath = false
845 ) {
846 if ( !$this->doTable( $table ) ) {
847 return true;
848 }
849
850 // First requirement: the table must exist
851 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
852 $this->output( "...skipping: '$table' table doesn't exist yet.\n" );
853
854 return true;
855 }
856
857 // Second requirement: the new index must be missing
858 if ( $this->db->indexExists( $table, $newIndex, __METHOD__ ) ) {
859 $this->output( "...index $newIndex already set on $table table.\n" );
860 if ( !$skipBothIndexExistWarning &&
861 $this->db->indexExists( $table, $oldIndex, __METHOD__ )
862 ) {
863 $this->output( "...WARNING: $oldIndex still exists, despite it has " .
864 "been renamed into $newIndex (which also exists).\n" .
865 " $oldIndex should be manually removed if not needed anymore.\n" );
866 }
867
868 return true;
869 }
870
871 // Third requirement: the old index must exist
872 if ( !$this->db->indexExists( $table, $oldIndex, __METHOD__ ) ) {
873 $this->output( "...skipping: index $oldIndex doesn't exist.\n" );
874
875 return true;
876 }
877
878 // Requirements have been satisfied, patch can be applied
879 return $this->applyPatch(
880 $patch,
881 $fullpath,
882 "Renaming index $oldIndex into $newIndex to table $table"
883 );
884 }
885
886 /**
887 * If the specified table exists, drop it, or execute the
888 * patch if one is provided.
889 *
890 * Public @since 1.20
891 *
892 * @param string $table Table to drop.
893 * @param string|bool $patch String of patch file that will drop the table. Default: false.
894 * @param bool $fullpath Whether $patch is a full path. Default: false.
895 * @return bool False if this was skipped because schema changes are skipped
896 */
897 public function dropTable( $table, $patch = false, $fullpath = false ) {
898 if ( !$this->doTable( $table ) ) {
899 return true;
900 }
901
902 if ( $this->db->tableExists( $table, __METHOD__ ) ) {
903 $msg = "Dropping table $table";
904
905 if ( $patch === false ) {
906 $this->output( "$msg ..." );
907 $this->db->dropTable( $table, __METHOD__ );
908 $this->output( "done.\n" );
909 } else {
910 return $this->applyPatch( $patch, $fullpath, $msg );
911 }
912 } else {
913 $this->output( "...$table doesn't exist.\n" );
914 }
915
916 return true;
917 }
918
919 /**
920 * Modify an existing field
921 *
922 * @param string $table Name of the table to which the field belongs
923 * @param string $field Name of the field to modify
924 * @param string $patch Path to the patch file
925 * @param bool $fullpath Whether to treat $patch path as a relative or not
926 * @return bool False if this was skipped because schema changes are skipped
927 */
928 public function modifyField( $table, $field, $patch, $fullpath = false ) {
929 if ( !$this->doTable( $table ) ) {
930 return true;
931 }
932
933 $updateKey = "$table-$field-$patch";
934 if ( !$this->db->tableExists( $table, __METHOD__ ) ) {
935 $this->output( "...$table table does not exist, skipping modify field patch.\n" );
936 } elseif ( !$this->db->fieldExists( $table, $field, __METHOD__ ) ) {
937 $this->output( "...$field field does not exist in $table table, " .
938 "skipping modify field patch.\n" );
939 } elseif ( $this->updateRowExists( $updateKey ) ) {
940 $this->output( "...$field in table $table already modified by patch $patch.\n" );
941 } else {
942 $this->insertUpdateRow( $updateKey );
943
944 return $this->applyPatch( $patch, $fullpath, "Modifying $field field of table $table" );
945 }
946
947 return true;
948 }
949
950 /**
951 * Set any .htaccess files or equivilent for storage repos
952 *
953 * Some zones (e.g. "temp") used to be public and may have been initialized as such
954 */
955 public function setFileAccess() {
956 $repo = RepoGroup::singleton()->getLocalRepo();
957 $zonePath = $repo->getZonePath( 'temp' );
958 if ( $repo->getBackend()->directoryExists( [ 'dir' => $zonePath ] ) ) {
959 // If the directory was never made, then it will have the right ACLs when it is made
960 $status = $repo->getBackend()->secure( [
961 'dir' => $zonePath,
962 'noAccess' => true,
963 'noListing' => true
964 ] );
965 if ( $status->isOK() ) {
966 $this->output( "Set the local repo temp zone container to be private.\n" );
967 } else {
968 $this->output( "Failed to set the local repo temp zone container to be private.\n" );
969 }
970 }
971 }
972
973 /**
974 * Purge the objectcache table
975 */
976 public function purgeCache() {
977 global $wgLocalisationCacheConf;
978 # We can't guarantee that the user will be able to use TRUNCATE,
979 # but we know that DELETE is available to us
980 $this->output( "Purging caches..." );
981 $this->db->delete( 'objectcache', '*', __METHOD__ );
982 if ( $wgLocalisationCacheConf['manualRecache'] ) {
983 $this->rebuildLocalisationCache();
984 }
985 $blobStore = new MessageBlobStore();
986 $blobStore->clear();
987 $this->db->delete( 'module_deps', '*', __METHOD__ );
988 $this->output( "done.\n" );
989 }
990
991 /**
992 * Check the site_stats table is not properly populated.
993 */
994 protected function checkStats() {
995 $this->output( "...site_stats is populated..." );
996 $row = $this->db->selectRow( 'site_stats', '*', [ 'ss_row_id' => 1 ], __METHOD__ );
997 if ( $row === false ) {
998 $this->output( "data is missing! rebuilding...\n" );
999 } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
1000 $this->output( "missing ss_total_pages, rebuilding...\n" );
1001 } else {
1002 $this->output( "done.\n" );
1003
1004 return;
1005 }
1006 SiteStatsInit::doAllAndCommit( $this->db );
1007 }
1008
1009 # Common updater functions
1010
1011 /**
1012 * Sets the number of active users in the site_stats table
1013 */
1014 protected function doActiveUsersInit() {
1015 $activeUsers = $this->db->selectField( 'site_stats', 'ss_active_users', false, __METHOD__ );
1016 if ( $activeUsers == -1 ) {
1017 $activeUsers = $this->db->selectField( 'recentchanges',
1018 'COUNT( DISTINCT rc_user_text )',
1019 [ 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ], __METHOD__
1020 );
1021 $this->db->update( 'site_stats',
1022 [ 'ss_active_users' => intval( $activeUsers ) ],
1023 [ 'ss_row_id' => 1 ], __METHOD__, [ 'LIMIT' => 1 ]
1024 );
1025 }
1026 $this->output( "...ss_active_users user count set...\n" );
1027 }
1028
1029 /**
1030 * Populates the log_user_text field in the logging table
1031 */
1032 protected function doLogUsertextPopulation() {
1033 if ( !$this->updateRowExists( 'populate log_usertext' ) ) {
1034 $this->output(
1035 "Populating log_user_text field, printing progress markers. For large\n" .
1036 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1037 "maintenance/populateLogUsertext.php.\n"
1038 );
1039
1040 $task = $this->maintenance->runChild( 'PopulateLogUsertext' );
1041 $task->execute();
1042 $this->output( "done.\n" );
1043 }
1044 }
1045
1046 /**
1047 * Migrate log params to new table and index for searching
1048 */
1049 protected function doLogSearchPopulation() {
1050 if ( !$this->updateRowExists( 'populate log_search' ) ) {
1051 $this->output(
1052 "Populating log_search table, printing progress markers. For large\n" .
1053 "databases, you may want to hit Ctrl-C and do this manually with\n" .
1054 "maintenance/populateLogSearch.php.\n" );
1055
1056 $task = $this->maintenance->runChild( 'PopulateLogSearch' );
1057 $task->execute();
1058 $this->output( "done.\n" );
1059 }
1060 }
1061
1062 /**
1063 * Updates the timestamps in the transcache table
1064 * @return bool
1065 */
1066 protected function doUpdateTranscacheField() {
1067 if ( $this->updateRowExists( 'convert transcache field' ) ) {
1068 $this->output( "...transcache tc_time already converted.\n" );
1069
1070 return true;
1071 }
1072
1073 return $this->applyPatch( 'patch-tc-timestamp.sql', false,
1074 "Converting tc_time from UNIX epoch to MediaWiki timestamp" );
1075 }
1076
1077 /**
1078 * Update CategoryLinks collation
1079 */
1080 protected function doCollationUpdate() {
1081 global $wgCategoryCollation;
1082 if ( $this->db->fieldExists( 'categorylinks', 'cl_collation', __METHOD__ ) ) {
1083 if ( $this->db->selectField(
1084 'categorylinks',
1085 'COUNT(*)',
1086 'cl_collation != ' . $this->db->addQuotes( $wgCategoryCollation ),
1087 __METHOD__
1088 ) == 0
1089 ) {
1090 $this->output( "...collations up-to-date.\n" );
1091
1092 return;
1093 }
1094
1095 $this->output( "Updating category collations..." );
1096 $task = $this->maintenance->runChild( 'UpdateCollation' );
1097 $task->execute();
1098 $this->output( "...done.\n" );
1099 }
1100 }
1101
1102 /**
1103 * Migrates user options from the user table blob to user_properties
1104 */
1105 protected function doMigrateUserOptions() {
1106 if ( $this->db->tableExists( 'user_properties' ) ) {
1107 $cl = $this->maintenance->runChild( 'ConvertUserOptions', 'convertUserOptions.php' );
1108 $cl->execute();
1109 $this->output( "done.\n" );
1110 }
1111 }
1112
1113 /**
1114 * Enable profiling table when it's turned on
1115 */
1116 protected function doEnableProfiling() {
1117 global $wgProfiler;
1118
1119 if ( !$this->doTable( 'profiling' ) ) {
1120 return;
1121 }
1122
1123 $profileToDb = false;
1124 if ( isset( $wgProfiler['output'] ) ) {
1125 $out = $wgProfiler['output'];
1126 if ( $out === 'db' ) {
1127 $profileToDb = true;
1128 } elseif ( is_array( $out ) && in_array( 'db', $out ) ) {
1129 $profileToDb = true;
1130 }
1131 }
1132
1133 if ( $profileToDb && !$this->db->tableExists( 'profiling', __METHOD__ ) ) {
1134 $this->applyPatch( 'patch-profiling.sql', false, 'Add profiling table' );
1135 }
1136 }
1137
1138 /**
1139 * Rebuilds the localisation cache
1140 */
1141 protected function rebuildLocalisationCache() {
1142 /**
1143 * @var $cl RebuildLocalisationCache
1144 */
1145 $cl = $this->maintenance->runChild( 'RebuildLocalisationCache', 'rebuildLocalisationCache.php' );
1146 $this->output( "Rebuilding localisation cache...\n" );
1147 $cl->setForce();
1148 $cl->execute();
1149 $this->output( "done.\n" );
1150 }
1151
1152 /**
1153 * Turns off content handler fields during parts of the upgrade
1154 * where they aren't available.
1155 */
1156 protected function disableContentHandlerUseDB() {
1157 global $wgContentHandlerUseDB;
1158
1159 if ( $wgContentHandlerUseDB ) {
1160 $this->output( "Turning off Content Handler DB fields for this part of upgrade.\n" );
1161 $this->holdContentHandlerUseDB = $wgContentHandlerUseDB;
1162 $wgContentHandlerUseDB = false;
1163 }
1164 }
1165
1166 /**
1167 * Turns content handler fields back on.
1168 */
1169 protected function enableContentHandlerUseDB() {
1170 global $wgContentHandlerUseDB;
1171
1172 if ( $this->holdContentHandlerUseDB ) {
1173 $this->output( "Content Handler DB fields should be usable now.\n" );
1174 $wgContentHandlerUseDB = $this->holdContentHandlerUseDB;
1175 }
1176 }
1177 }