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