MediaWiki master
update.php
Go to the documentation of this file.
1#!/usr/bin/env php
2<?php
28// NO_AUTOLOAD -- due to hashbang above
29
30// @codeCoverageIgnoreStart
31require_once __DIR__ . '/Maintenance.php';
32// @codeCoverageIgnoreEnd
33
43
50 public function __construct() {
51 parent::__construct();
52 $this->addDescription( 'MediaWiki database updater' );
53 $this->addOption( 'quick', 'Skip 5 second countdown before starting' );
54 $this->addOption( 'initial',
55 'Do initial updates required after manual installation using tables-generated.sql' );
56 $this->addOption( 'doshared', 'Also update shared tables' );
57 $this->addOption( 'noschema', 'Only do the updates that are not done during schema updates' );
58 $this->addOption(
59 'schema',
60 'Output SQL to do the schema updates instead of doing them. Works '
61 . 'even when $wgAllowSchemaUpdates is false',
62 false,
63 true
64 );
65 $this->addOption( 'force', 'Override when $wgAllowSchemaUpdates disables this script' );
66 $this->addOption(
67 'skip-external-dependencies',
68 'Skips checking whether external dependencies are up to date, mostly for developers'
69 );
70 $this->addOption(
71 'skip-config-validation',
72 'Skips checking whether the existing configuration is valid'
73 );
74 }
75
76 public function getDbType() {
77 return Maintenance::DB_ADMIN;
78 }
79
80 public function setup() {
81 global $wgMessagesDirs;
82 // T206765: We need to load the installer i18n files as some of errors come installer/updater code
83 // T310378: We have to ensure we do this before execute()
84 $wgMessagesDirs['MediaWikiInstaller'] = dirname( __DIR__ ) . '/includes/installer/i18n';
85 }
86
87 public function execute() {
89
91 && !( $this->hasOption( 'force' )
92 || $this->hasOption( 'schema' )
93 || $this->hasOption( 'noschema' ) )
94 ) {
95 $this->fatalError( "Do not run update.php on this wiki. If you're seeing this you should\n"
96 . "probably ask for some help in performing your schema updates or use\n"
97 . "the --noschema and --schema options to get an SQL file for someone\n"
98 . "else to inspect and run.\n\n"
99 . "If you know what you are doing, you can continue with --force\n" );
100 }
101
102 $this->fileHandle = null;
103 if ( str_starts_with( $this->getOption( 'schema', '' ), '--' ) ) {
104 $this->fatalError( "The --schema option requires a file as an argument.\n" );
105 } elseif ( $this->hasOption( 'schema' ) ) {
106 $file = $this->getOption( 'schema' );
107 $this->fileHandle = fopen( $file, "w" );
108 if ( $this->fileHandle === false ) {
109 $err = error_get_last();
110 $this->fatalError( "Problem opening the schema file for writing: $file\n\t{$err['message']}" );
111 }
112 }
113
114 // Check for warnings about settings, and abort if there are any.
115 if ( !$this->hasOption( 'skip-config-validation' ) ) {
116 $this->validateSettings();
117 }
118
119 $lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' );
120 // Set global language to ensure localised errors are in English (T22633)
121 RequestContext::getMain()->setLanguage( $lang );
122
123 // BackCompat
124 $wgLang = $lang;
125
126 define( 'MW_UPDATER', true );
127
128 $this->output( 'MediaWiki ' . MW_VERSION . " Updater\n\n" );
129
130 $this->waitForReplication();
131
132 // Check external dependencies are up to date
133 if ( !$this->hasOption( 'skip-external-dependencies' ) && !getenv( 'MW_SKIP_EXTERNAL_DEPENDENCIES' ) ) {
134 $composerLockUpToDate = $this->runChild( CheckComposerLockUpToDate::class );
135 $composerLockUpToDate->execute();
136 } else {
137 $this->output(
138 "Skipping checking whether external dependencies are up to date, proceed at your own risk\n"
139 );
140 }
141
142 # Attempt to connect to the database as a privileged user
143 # This will vomit up an error if there are permissions problems
144 $db = $this->getPrimaryDB();
145
146 # Check to see whether the database server meets the minimum requirements
148 $dbInstallerClass = Installer::getDBInstallerClass( $db->getType() );
149 $status = $dbInstallerClass::meetsMinimumRequirement( $db );
150 if ( !$status->isOK() ) {
151 // This might output some wikitext like <strong> but it should be comprehensible
152 $this->fatalError( $status );
153 }
154
155 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
156 $this->output( "Going to run database updates for $dbDomain\n" );
157 if ( $db->getType() === 'sqlite' ) {
159 '@phan-var DatabaseSqlite $db';
160 $this->output( "Using SQLite file: '{$db->getDbFilePath()}'\n" );
161 }
162 $this->output( "Depending on the size of your database this may take a while!\n" );
163
164 if ( !$this->hasOption( 'quick' ) ) {
165 $this->output( "Abort with control-c in the next five seconds "
166 . "(skip this countdown with --quick) ..." );
167 $this->countDown( 5 );
168 }
169
170 $time1 = microtime( true );
171
172 $shared = $this->hasOption( 'doshared' );
173
174 $updates = [ 'core', 'extensions' ];
175 if ( !$this->hasOption( 'schema' ) ) {
176 if ( $this->hasOption( 'noschema' ) ) {
177 $updates[] = 'noschema';
178 }
179 $updates[] = 'stats';
180 }
181 if ( $this->hasOption( 'initial' ) ) {
182 $updates[] = 'initial';
183 }
184
185 $updater = DatabaseUpdater::newForDB( $db, $shared, $this );
186
187 // Avoid upgrading from versions older than 1.35
188 // Using an implicit marker (rev_actor was introduced in 1.34)
189 // TODO: Use an explicit marker
190 // See T259771
191 if ( !$updater->fieldExists( 'revision', 'rev_actor' ) ) {
192 $this->fatalError(
193 "Can not upgrade from versions older than 1.35, please upgrade to that version or later first."
194 );
195 }
196
197 $updater->doUpdates( $updates );
198
199 foreach ( $updater->getPostDatabaseUpdateMaintenance() as $maint ) {
200 $child = $this->runChild( $maint );
201
202 // LoggedUpdateMaintenance is checking the updatelog itself
203 $isLoggedUpdate = $child instanceof LoggedUpdateMaintenance;
204
205 if ( !$isLoggedUpdate && $updater->updateRowExists( $maint ) ) {
206 continue;
207 }
208
209 $child->execute();
210 if ( !$isLoggedUpdate ) {
211 $updater->insertUpdateRow( $maint );
212 }
213 }
214
215 $updater->setFileAccess();
216
217 $updater->purgeCache();
218
219 $time2 = microtime( true );
220
221 $timeDiff = $lang->formatTimePeriod( $time2 - $time1 );
222 $this->output( "\nDone in $timeDiff.\n" );
223 }
224
225 protected function afterFinalSetup() {
227
228 # Don't try to access the database
229 # This needs to be disabled early since extensions will try to use the l10n
230 # cache from $wgExtensionFunctions (T22471)
232 'class' => LocalisationCache::class,
233 'storeClass' => LCStoreNull::class,
234 'storeDirectory' => false,
235 'manualRecache' => false,
236 ];
237 }
238
242 public function validateParamsAndArgs() {
243 // Allow extensions to add additional params.
244 $params = [];
245 $this->getHookRunner()->onMaintenanceUpdateAddParams( $params );
246
247 // This executes before the PHP version check, so don't use null coalesce (??).
248 // Keeping this compatible with older PHP versions lets us reach the code that
249 // displays a more helpful error.
250 foreach ( $params as $name => $param ) {
251 $this->addOption(
252 $name,
253 $param['desc'],
254 isset( $param['require'] ) ? $param['require'] : false,
255 isset( $param['withArg'] ) ? $param['withArg'] : false,
256 isset( $param['shortName'] ) ? $param['shortName'] : false,
257 isset( $param['multiOccurrence'] ) ? $param['multiOccurrence'] : false
258 );
259 }
260
261 parent::validateParamsAndArgs();
262 }
263
264 private function formatWarnings( array $warnings ) {
265 $text = '';
266 foreach ( $warnings as $warning ) {
267 $warning = wordwrap( $warning, 75, "\n " );
268 $text .= "* $warning\n";
269 }
270 return $text;
271 }
272
273 private function validateSettings() {
274 $settings = SettingsBuilder::getInstance();
275
276 $warnings = [];
277 if ( $settings->getWarnings() ) {
278 $warnings = $settings->getWarnings();
279 }
280
281 $status = $settings->validate();
282 if ( !$status->isOK() ) {
283 foreach ( $status->getMessages( 'error' ) as $msg ) {
284 $warnings[] = wfMessage( $msg )->text();
285 }
286 }
287
288 $deprecations = $settings->detectDeprecatedConfig();
289 foreach ( $deprecations as $key => $msg ) {
290 $warnings[] = "$key is deprecated: $msg";
291 }
292
293 $obsolete = $settings->detectObsoleteConfig();
294 foreach ( $obsolete as $key => $msg ) {
295 $warnings[] = "$key is obsolete: $msg";
296 }
297
298 if ( $warnings ) {
299 $this->fatalError( "Some of your configuration settings caused a warning:\n\n"
300 . $this->formatWarnings( $warnings ) . "\n"
301 . "Please correct the issue before running update.php again.\n"
302 . "If you know what you are doing, you can bypass this check\n"
303 . "using --skip-config-validation.\n" );
304 }
305 }
306}
307
308// @codeCoverageIgnoreStart
309$maintClass = UpdateMediaWiki::class;
310require_once RUN_MAINTENANCE_IF_MAIN;
311// @codeCoverageIgnoreEnd
const MW_VERSION
The running version of MediaWiki.
Definition Defines.php:37
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(!defined( 'MW_NO_SESSION') &&MW_ENTRY_POINT !=='cli' $wgLang
Definition Setup.php:570
array $params
The job parameters.
Group all the pieces relevant to the context of a request into one instance.
Base class for DBMS-specific installation helper classes.
Apply database changes after updating MediaWiki.
Base installer class.
Definition Installer.php:85
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
runChild( $maintClass, $classFile=null)
Returns an instance of the given maintenance script, with all of the current arguments passed to it.
waitForReplication()
Wait for replica DB servers to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
getHookRunner()
Get a HookRunner for running core hooks.
getServiceContainer()
Returns the main service container.
countDown( $seconds)
Count down from $seconds to zero on the terminal, with a one-second pause between showing each number...
addDescription( $text)
Set the description text.
Builder class for constructing a Config object from a set of sources during bootstrap.
Tools for dealing with other locally-hosted wikis.
Definition WikiMap.php:31
Maintenance script to run database schema updates.
Definition update.php:49
afterFinalSetup()
Override to perform any required operation at the end of initialisation.
Definition update.php:225
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Definition update.php:76
validateParamsAndArgs()
Run some validation checks on the params, etc.
Definition update.php:242
setup()
Provides subclasses with an opportunity to perform initial checks.
Definition update.php:80
execute()
Do the actual work.
Definition update.php:87
__construct()
Default constructor.
Definition update.php:50
This is the SQLite database abstraction layer.
$wgMessagesDirs
Config variable stub for the MessagesDirs setting, for use by phpdoc and IDEs.
$wgAllowSchemaUpdates
Config variable stub for the AllowSchemaUpdates setting, for use by phpdoc and IDEs.
$wgLocalisationCacheConf
Config variable stub for the LocalisationCacheConf setting, for use by phpdoc and IDEs.
$maintClass
Definition update.php:309