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