6 * This is used when the database schema is modified and we need to apply patches.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
25 * @ingroup Maintenance
28 // NO_AUTOLOAD -- due to hashbang above
30 require_once __DIR__
. '/Maintenance.php';
32 use MediaWiki\Settings\SettingsBuilder
;
33 use MediaWiki\WikiMap\WikiMap
;
34 use Wikimedia\Rdbms\DatabaseSqlite
;
37 * Maintenance script to run database schema updates.
39 * @ingroup Maintenance
41 class UpdateMediaWiki
extends Maintenance
{
42 public function __construct() {
43 parent
::__construct();
44 $this->addDescription( 'MediaWiki database updater' );
45 $this->addOption( 'quick', 'Skip 5 second countdown before starting' );
46 $this->addOption( 'doshared', 'Also update shared tables' );
47 $this->addOption( 'noschema', 'Only do the updates that are not done during schema updates' );
50 'Output SQL to do the schema updates instead of doing them. Works '
51 . 'even when $wgAllowSchemaUpdates is false',
55 $this->addOption( 'force', 'Override when $wgAllowSchemaUpdates disables this script' );
57 'skip-external-dependencies',
58 'Skips checking whether external dependencies are up to date, mostly for developers'
61 'skip-config-validation',
62 'Skips checking whether the existing configuration is valid'
66 public function getDbType() {
67 return Maintenance
::DB_ADMIN
;
70 public function setup() {
71 global $wgMessagesDirs;
72 // T206765: We need to load the installer i18n files as some of errors come installer/updater code
73 // T310378: We have to ensure we do this before execute()
74 $wgMessagesDirs['MediawikiInstaller'] = dirname( __DIR__
) . '/includes/installer/i18n';
77 public function execute() {
78 global $wgLang, $wgAllowSchemaUpdates;
80 if ( !$wgAllowSchemaUpdates
81 && !( $this->hasOption( 'force' )
82 ||
$this->hasOption( 'schema' )
83 ||
$this->hasOption( 'noschema' ) )
85 $this->fatalError( "Do not run update.php on this wiki. If you're seeing this you should\n"
86 . "probably ask for some help in performing your schema updates or use\n"
87 . "the --noschema and --schema options to get an SQL file for someone\n"
88 . "else to inspect and run.\n\n"
89 . "If you know what you are doing, you can continue with --force\n" );
92 $this->fileHandle
= null;
93 if ( str_starts_with( $this->getOption( 'schema', '' ), '--' ) ) {
94 $this->fatalError( "The --schema option requires a file as an argument.\n" );
95 } elseif ( $this->hasOption( 'schema' ) ) {
96 $file = $this->getOption( 'schema' );
97 $this->fileHandle
= fopen( $file, "w" );
98 if ( $this->fileHandle
=== false ) {
99 $err = error_get_last();
100 $this->fatalError( "Problem opening the schema file for writing: $file\n\t{$err['message']}" );
104 // Check for warnings about settings, and abort if there are any.
105 if ( !$this->hasOption( 'skip-config-validation' ) ) {
106 $this->validateSettings();
109 $lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' );
110 // Set global language to ensure localised errors are in English (T22633)
111 RequestContext
::getMain()->setLanguage( $lang );
116 define( 'MW_UPDATER', true );
118 $this->output( 'MediaWiki ' . MW_VERSION
. " Updater\n\n" );
120 $this->getServiceContainer()->getDBLoadBalancerFactory()->waitForReplication();
122 // Check external dependencies are up to date
123 if ( !$this->hasOption( 'skip-external-dependencies' ) ) {
124 $composerLockUpToDate = $this->runChild( CheckComposerLockUpToDate
::class );
125 $composerLockUpToDate->execute();
128 "Skipping checking whether external dependencies are up to date, proceed at your own risk\n"
132 # Attempt to connect to the database as a privileged user
133 # This will vomit up an error if there are permissions problems
134 $db = $this->getDB( DB_PRIMARY
);
136 # Check to see whether the database server meets the minimum requirements
137 /** @var DatabaseInstaller $dbInstallerClass */
138 $dbInstallerClass = Installer
::getDBInstallerClass( $db->getType() );
139 $status = $dbInstallerClass::meetsMinimumRequirement( $db );
140 if ( !$status->isOK() ) {
141 // This might output some wikitext like <strong> but it should be comprehensible
142 $text = $status->getWikiText();
143 $this->fatalError( $text );
146 $dbDomain = WikiMap
::getCurrentWikiDbDomain()->getId();
147 $this->output( "Going to run database updates for $dbDomain\n" );
148 if ( $db->getType() === 'sqlite' ) {
149 /** @var DatabaseSqlite $db */
150 '@phan-var DatabaseSqlite $db';
151 $this->output( "Using SQLite file: '{$db->getDbFilePath()}'\n" );
153 $this->output( "Depending on the size of your database this may take a while!\n" );
155 if ( !$this->hasOption( 'quick' ) ) {
156 $this->output( "Abort with control-c in the next five seconds "
157 . "(skip this countdown with --quick) ..." );
158 $this->countDown( 5 );
161 $time1 = microtime( true );
163 $shared = $this->hasOption( 'doshared' );
165 $updates = [ 'core', 'extensions' ];
166 if ( !$this->hasOption( 'schema' ) ) {
167 if ( $this->hasOption( 'noschema' ) ) {
168 $updates[] = 'noschema';
170 $updates[] = 'stats';
173 $updater = DatabaseUpdater
::newForDB( $db, $shared, $this );
175 // Avoid upgrading from versions older than 1.35
176 // Using an implicit marker (ar_user was dropped in 1.34)
177 // TODO: Use an explicit marker
179 if ( $updater->fieldExists( 'archive', 'ar_user' ) ) {
181 "Can not upgrade from versions older than 1.35, please upgrade to that version or later first."
185 $updater->doUpdates( $updates );
187 foreach ( $updater->getPostDatabaseUpdateMaintenance() as $maint ) {
188 $child = $this->runChild( $maint );
190 // LoggedUpdateMaintenance is checking the updatelog itself
191 $isLoggedUpdate = $child instanceof LoggedUpdateMaintenance
;
193 if ( !$isLoggedUpdate && $updater->updateRowExists( $maint ) ) {
198 if ( !$isLoggedUpdate ) {
199 $updater->insertUpdateRow( $maint );
203 $updater->setFileAccess();
205 $updater->purgeCache();
207 $time2 = microtime( true );
209 $timeDiff = $lang->formatTimePeriod( $time2 - $time1 );
210 $this->output( "\nDone in $timeDiff.\n" );
213 protected function afterFinalSetup() {
214 global $wgLocalisationCacheConf;
216 # Don't try to access the database
217 # This needs to be disabled early since extensions will try to use the l10n
218 # cache from $wgExtensionFunctions (T22471)
219 $wgLocalisationCacheConf = [
220 'class' => LocalisationCache
::class,
221 'storeClass' => LCStoreNull
::class,
222 'storeDirectory' => false,
223 'manualRecache' => false,
228 * @suppress PhanPluginDuplicateConditionalNullCoalescing
230 public function validateParamsAndArgs() {
231 // Allow extensions to add additional params.
233 $this->getHookRunner()->onMaintenanceUpdateAddParams( $params );
235 // This executes before the PHP version check, so don't use null coalesce (??).
236 // Keeping this compatible with older PHP versions lets us reach the code that
237 // displays a more helpful error.
238 foreach ( $params as $name => $param ) {
242 isset( $param['require'] ) ?
$param['require'] : false,
243 isset( $param['withArg'] ) ?
$param['withArg'] : false,
244 isset( $param['shortName'] ) ?
$param['shortName'] : false,
245 isset( $param['multiOccurrence'] ) ?
$param['multiOccurrence'] : false
249 parent
::validateParamsAndArgs();
252 private function formatWarnings( array $warnings ) {
254 foreach ( $warnings as $warning ) {
255 $warning = wordwrap( $warning, 75, "\n " );
256 $text .= "* $warning\n";
261 private function validateSettings() {
262 $settings = SettingsBuilder
::getInstance();
265 if ( $settings->getWarnings() ) {
266 $warnings = $settings->getWarnings();
269 $status = $settings->validate();
270 if ( !$status->isOK() ) {
271 foreach ( $status->getErrorsByType( 'error' ) as $msg ) {
272 $msg = wfMessage( $msg['message'], ...$msg['params'] );
273 $warnings[] = $msg->text();
277 $deprecations = $settings->detectDeprecatedConfig();
278 foreach ( $deprecations as $key => $msg ) {
279 $warnings[] = "$key is deprecated: $msg";
282 $obsolete = $settings->detectObsoleteConfig();
283 foreach ( $obsolete as $key => $msg ) {
284 $warnings[] = "$key is obsolete: $msg";
288 $this->fatalError( "Some of your configuration settings caused a warning:\n\n"
289 . $this->formatWarnings( $warnings ) . "\n"
290 . "Please correct the issue before running update.php again.\n"
291 . "If you know what you are doing, you can bypass this check\n"
292 . "using --skip-config-validation.\n" );
297 $maintClass = UpdateMediaWiki
::class;
298 require_once RUN_MAINTENANCE_IF_MAIN
;