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