MediaWiki 1.41.2
update.php
Go to the documentation of this file.
1#!/usr/bin/env php
2<?php
28// NO_AUTOLOAD -- due to hashbang above
29
30require_once __DIR__ . '/Maintenance.php';
31
35
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' );
48 $this->addOption(
49 'schema',
50 'Output SQL to do the schema updates instead of doing them. Works '
51 . 'even when $wgAllowSchemaUpdates is false',
52 false,
53 true
54 );
55 $this->addOption( 'force', 'Override when $wgAllowSchemaUpdates disables this script' );
56 $this->addOption(
57 'skip-external-dependencies',
58 'Skips checking whether external dependencies are up to date, mostly for developers'
59 );
60 $this->addOption(
61 'skip-config-validation',
62 'Skips checking whether the existing configuration is valid'
63 );
64 }
65
66 public function getDbType() {
68 }
69
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';
75 }
76
77 public function execute() {
79
81 && !( $this->hasOption( 'force' )
82 || $this->hasOption( 'schema' )
83 || $this->hasOption( 'noschema' ) )
84 ) {
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" );
90 }
91
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']}" );
101 }
102 }
103
104 // Check for warnings about settings, and abort if there are any.
105 if ( !$this->hasOption( 'skip-config-validation' ) ) {
106 $this->validateSettings();
107 }
108
109 $lang = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'en' );
110 // Set global language to ensure localised errors are in English (T22633)
111 RequestContext::getMain()->setLanguage( $lang );
112
113 // BackCompat
114 $wgLang = $lang;
115
116 define( 'MW_UPDATER', true );
117
118 $this->output( 'MediaWiki ' . MW_VERSION . " Updater\n\n" );
119
120 $this->getServiceContainer()->getDBLoadBalancerFactory()->waitForReplication();
121
122 // Check external dependencies are up to date
123 if ( !$this->hasOption( 'skip-external-dependencies' ) ) {
124 $composerLockUpToDate = $this->runChild( CheckComposerLockUpToDate::class );
125 $composerLockUpToDate->execute();
126 } else {
127 $this->output(
128 "Skipping checking whether external dependencies are up to date, proceed at your own risk\n"
129 );
130 }
131
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 );
135
136 # Check to see whether the database server meets the minimum requirements
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 );
144 }
145
146 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
147 $this->output( "Going to run database updates for $dbDomain\n" );
148 if ( $db->getType() === 'sqlite' ) {
150 '@phan-var DatabaseSqlite $db';
151 $this->output( "Using SQLite file: '{$db->getDbFilePath()}'\n" );
152 }
153 $this->output( "Depending on the size of your database this may take a while!\n" );
154
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 );
159 }
160
161 $time1 = microtime( true );
162
163 $shared = $this->hasOption( 'doshared' );
164
165 $updates = [ 'core', 'extensions' ];
166 if ( !$this->hasOption( 'schema' ) ) {
167 if ( $this->hasOption( 'noschema' ) ) {
168 $updates[] = 'noschema';
169 }
170 $updates[] = 'stats';
171 }
172
173 $updater = DatabaseUpdater::newForDB( $db, $shared, $this );
174
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
178 // See T259771
179 if ( $updater->fieldExists( 'archive', 'ar_user' ) ) {
180 $this->fatalError(
181 "Can not upgrade from versions older than 1.35, please upgrade to that version or later first."
182 );
183 }
184
185 $updater->doUpdates( $updates );
186
187 foreach ( $updater->getPostDatabaseUpdateMaintenance() as $maint ) {
188 $child = $this->runChild( $maint );
189
190 // LoggedUpdateMaintenance is checking the updatelog itself
191 $isLoggedUpdate = $child instanceof LoggedUpdateMaintenance;
192
193 if ( !$isLoggedUpdate && $updater->updateRowExists( $maint ) ) {
194 continue;
195 }
196
197 $child->execute();
198 if ( !$isLoggedUpdate ) {
199 $updater->insertUpdateRow( $maint );
200 }
201 }
202
203 $updater->setFileAccess();
204
205 $updater->purgeCache();
206
207 $time2 = microtime( true );
208
209 $timeDiff = $lang->formatTimePeriod( $time2 - $time1 );
210 $this->output( "\nDone in $timeDiff.\n" );
211 }
212
213 protected function afterFinalSetup() {
215
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)
220 'class' => LocalisationCache::class,
221 'storeClass' => LCStoreNull::class,
222 'storeDirectory' => false,
223 'manualRecache' => false,
224 ];
225 }
226
230 public function validateParamsAndArgs() {
231 // Allow extensions to add additional params.
232 $params = [];
233 $this->getHookRunner()->onMaintenanceUpdateAddParams( $params );
234
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 ) {
239 $this->addOption(
240 $name,
241 $param['desc'],
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
246 );
247 }
248
249 parent::validateParamsAndArgs();
250 }
251
252 private function formatWarnings( array $warnings ) {
253 $text = '';
254 foreach ( $warnings as $warning ) {
255 $warning = wordwrap( $warning, 75, "\n " );
256 $text .= "* $warning\n";
257 }
258 return $text;
259 }
260
261 private function validateSettings() {
262 $settings = SettingsBuilder::getInstance();
263
264 $warnings = [];
265 if ( $settings->getWarnings() ) {
266 $warnings = $settings->getWarnings();
267 }
268
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();
274 }
275 }
276
277 $deprecations = $settings->detectDeprecatedConfig();
278 foreach ( $deprecations as $key => $msg ) {
279 $warnings[] = "$key is deprecated: $msg";
280 }
281
282 $obsolete = $settings->detectObsoleteConfig();
283 foreach ( $obsolete as $key => $msg ) {
284 $warnings[] = "$key is obsolete: $msg";
285 }
286
287 if ( $warnings ) {
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" );
293 }
294 }
295}
296
297$maintClass = UpdateMediaWiki::class;
298require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
const MW_VERSION
The running version of MediaWiki.
Definition Defines.php:36
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(!defined( 'MW_NO_SESSION') &&! $wgCommandLineMode $wgLang
Definition Setup.php:535
static getDBInstallerClass( $type)
Get the DatabaseInstaller class name for this type.
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.
getHookRunner()
Get a HookRunner for running core hooks.
hasOption( $name)
Checks to see if a particular option was set.
countDown( $seconds)
Count down from $seconds to zero on the terminal, with a one-second pause between showing each number...
runChild( $maintClass, $classFile=null)
Run a child maintenance script.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
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:41
afterFinalSetup()
Override to perform any required operation at the end of initialisation.
Definition update.php:213
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Definition update.php:66
validateParamsAndArgs()
Run some validation checks on the params, etc.
Definition update.php:230
setup()
Provides subclasses with an opportunity to perform initial checks.
Definition update.php:70
execute()
Do the actual work.
Definition update.php:77
__construct()
Default constructor.
Definition update.php:42
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.
const DB_PRIMARY
Definition defines.php:28
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42
$maintClass
Definition update.php:297