MediaWiki REL1_39
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
34
41 public function __construct() {
42 parent::__construct();
43 $this->addDescription( 'MediaWiki database updater' );
44 $this->addOption( 'skip-compat-checks', 'Skips compatibility checks, mostly for developers' );
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 private function compatChecks() {
71 $minimumPcreVersion = Installer::MINIMUM_PCRE_VERSION;
72
73 $pcreVersion = explode( ' ', PCRE_VERSION, 2 )[0];
74 if ( version_compare( $pcreVersion, $minimumPcreVersion, '<' ) ) {
75 $this->fatalError(
76 "PCRE $minimumPcreVersion or later is required.\n" .
77 "Your PHP binary is linked with PCRE $pcreVersion.\n\n" .
78 "More information:\n" .
79 "https://www.mediawiki.org/wiki/Manual:Errors_and_symptoms/PCRE\n\n" .
80 "ABORTING.\n" );
81 }
82 }
83
84 public function setup() {
85 global $wgMessagesDirs;
86
87 parent::setup();
88
89 // T206765: We need to load the installer i18n files as some of errors come installer/updater code
90 // T310378: We have to ensure we do this before execute()
91 $wgMessagesDirs['MediawikiInstaller'] = dirname( __DIR__ ) . '/includes/installer/i18n';
92 }
93
94 public function execute() {
96
98 && !( $this->hasOption( 'force' )
99 || $this->hasOption( 'schema' )
100 || $this->hasOption( 'noschema' ) )
101 ) {
102 $this->fatalError( "Do not run update.php on this wiki. If you're seeing this you should\n"
103 . "probably ask for some help in performing your schema updates or use\n"
104 . "the --noschema and --schema options to get an SQL file for someone\n"
105 . "else to inspect and run.\n\n"
106 . "If you know what you are doing, you can continue with --force\n" );
107 }
108
109 $this->fileHandle = null;
110 if ( substr( $this->getOption( 'schema', '' ), 0, 2 ) === "--" ) {
111 $this->fatalError( "The --schema option requires a file as an argument.\n" );
112 } elseif ( $this->hasOption( 'schema' ) ) {
113 $file = $this->getOption( 'schema' );
114 $this->fileHandle = fopen( $file, "w" );
115 if ( $this->fileHandle === false ) {
116 $err = error_get_last();
117 $this->fatalError( "Problem opening the schema file for writing: $file\n\t{$err['message']}" );
118 }
119 }
120
121 // Check for warnings about settings, and abort if there are any.
122 if ( !$this->hasOption( 'skip-config-validation' ) ) {
123 $this->validateSettings();
124 }
125
126 $lang = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'en' );
127 // Set global language to ensure localised errors are in English (T22633)
128 RequestContext::getMain()->setLanguage( $lang );
129
130 // BackCompat
131 $wgLang = $lang;
132
133 define( 'MW_UPDATER', true );
134
135 $this->output( 'MediaWiki ' . MW_VERSION . " Updater\n\n" );
136
137 MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
138
139 if ( !$this->hasOption( 'skip-compat-checks' ) ) {
140 $this->compatChecks();
141 } else {
142 $this->output( "Skipping compatibility checks, proceed at your own risk (Ctrl+C to abort)\n" );
143 $this->countDown( 5 );
144 }
145
146 // Check external dependencies are up to date
147 if ( !$this->hasOption( 'skip-external-dependencies' ) ) {
148 $composerLockUpToDate = $this->runChild( CheckComposerLockUpToDate::class );
149 $composerLockUpToDate->execute();
150 } else {
151 $this->output(
152 "Skipping checking whether external dependencies are up to date, proceed at your own risk\n"
153 );
154 }
155
156 # Attempt to connect to the database as a privileged user
157 # This will vomit up an error if there are permissions problems
158 $db = $this->getDB( DB_PRIMARY );
159
160 # Check to see whether the database server meets the minimum requirements
162 $dbInstallerClass = Installer::getDBInstallerClass( $db->getType() );
163 $status = $dbInstallerClass::meetsMinimumRequirement( $db );
164 if ( !$status->isOK() ) {
165 // This might output some wikitext like <strong> but it should be comprehensible
166 $text = $status->getWikiText();
167 $this->fatalError( $text );
168 }
169
170 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
171 $this->output( "Going to run database updates for $dbDomain\n" );
172 if ( $db->getType() === 'sqlite' ) {
174 '@phan-var DatabaseSqlite $db';
175 $this->output( "Using SQLite file: '{$db->getDbFilePath()}'\n" );
176 }
177 $this->output( "Depending on the size of your database this may take a while!\n" );
178
179 if ( !$this->hasOption( 'quick' ) ) {
180 $this->output( "Abort with control-c in the next five seconds "
181 . "(skip this countdown with --quick) ..." );
182 $this->countDown( 5 );
183 }
184
185 $time1 = microtime( true );
186
187 $shared = $this->hasOption( 'doshared' );
188
189 $updates = [ 'core', 'extensions' ];
190 if ( !$this->hasOption( 'schema' ) ) {
191 if ( $this->hasOption( 'noschema' ) ) {
192 $updates[] = 'noschema';
193 }
194 $updates[] = 'stats';
195 }
196
197 $updater = DatabaseUpdater::newForDB( $db, $shared, $this );
198
199 // Avoid upgrading from versions older than 1.31
200 // Using an implicit marker (slots table didn't exist until 1.31)
201 // TODO: Use an explicit marker
202 // See T259771
203 if ( !$updater->tableExists( 'slots' ) ) {
204 $this->fatalError(
205 "Can not upgrade from versions older than 1.31, please upgrade to that version or later first."
206 );
207 }
208
209 $updater->doUpdates( $updates );
210
211 foreach ( $updater->getPostDatabaseUpdateMaintenance() as $maint ) {
212 $child = $this->runChild( $maint );
213
214 // LoggedUpdateMaintenance is checking the updatelog itself
215 $isLoggedUpdate = $child instanceof LoggedUpdateMaintenance;
216
217 if ( !$isLoggedUpdate && $updater->updateRowExists( $maint ) ) {
218 continue;
219 }
220
221 $child->execute();
222 if ( !$isLoggedUpdate ) {
223 $updater->insertUpdateRow( $maint );
224 }
225 }
226
227 $updater->setFileAccess();
228
229 $updater->purgeCache();
230
231 $time2 = microtime( true );
232
233 $timeDiff = $lang->formatTimePeriod( $time2 - $time1 );
234 $this->output( "\nDone in $timeDiff.\n" );
235 }
236
237 protected function afterFinalSetup() {
239
240 # Don't try to access the database
241 # This needs to be disabled early since extensions will try to use the l10n
242 # cache from $wgExtensionFunctions (T22471)
244 'class' => LocalisationCache::class,
245 'storeClass' => LCStoreNull::class,
246 'storeDirectory' => false,
247 'manualRecache' => false,
248 ];
249 }
250
256 public function validateParamsAndArgs() {
257 // Allow extensions to add additional params.
258 $params = [];
259 $this->getHookRunner()->onMaintenanceUpdateAddParams( $params );
260
261 // This executes before the PHP version check, so don't use null coalesce (??).
262 // Keeping this compatible with older PHP versions lets us reach the code that
263 // displays a more helpful error.
264 foreach ( $params as $name => $param ) {
265 $this->addOption(
266 $name,
267 $param['desc'],
268 isset( $param['require'] ) ? $param['require'] : false,
269 isset( $param['withArg'] ) ? $param['withArg'] : false,
270 isset( $param['shortName'] ) ? $param['shortName'] : false,
271 isset( $param['multiOccurrence'] ) ? $param['multiOccurrence'] : false
272 );
273 }
274
275 parent::validateParamsAndArgs();
276 }
277
278 private function formatWarnings( array $warnings ) {
279 $text = '';
280 foreach ( $warnings as $warning ) {
281 $warning = wordwrap( $warning, 75, "\n " );
282 $text .= "* $warning\n";
283 }
284 return $text;
285 }
286
287 private function validateSettings() {
288 global $wgSettings;
289
290 $warnings = [];
291 if ( $wgSettings->getWarnings() ) {
292 $warnings = $wgSettings->getWarnings();
293 }
294
295 $status = $wgSettings->validate();
296 if ( !$status->isOk() ) {
297 foreach ( $status->getErrorsByType( 'error' ) as $msg ) {
298 $msg = wfMessage( $msg['message'], ...$msg['params'] );
299 $warnings[] = $msg->text();
300 }
301 }
302
303 $deprecations = $wgSettings->detectDeprecatedConfig();
304 foreach ( $deprecations as $key => $msg ) {
305 $warnings[] = "$key is deprecated: $msg";
306 }
307
308 if ( $warnings ) {
309 $this->fatalError( "Some of your configuration settings caused a warning:\n\n"
310 . $this->formatWarnings( $warnings ) . "\n"
311 . "Please correct the issue before running update.php again.\n"
312 . "If you know what you are doing, you can bypass this check\n"
313 . "using --skip-config-validation.\n" );
314 }
315 }
316}
317
318$maintClass = UpdateMediaWiki::class;
319require_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:497
$wgSettings
Definition Setup.php:143
const MINIMUM_PCRE_VERSION
The oldest version of PCRE we can support.
Definition Installer.php:65
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.
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.
Service locator for MediaWiki core services.
Maintenance script to run database schema updates.
Definition update.php:40
afterFinalSetup()
Override to perform any required operation at the end of initialisation.
Definition update.php:237
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Definition update.php:66
validateParamsAndArgs()
Definition update.php:256
setup()
Do some checking and basic setup.
Definition update.php:84
execute()
Do the actual work.
Definition update.php:94
__construct()
Default constructor.
Definition update.php:41
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
if(!isset( $args[0])) $lang
$maintClass
Definition update.php:318