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