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