MediaWiki REL1_35
update.php
Go to the documentation of this file.
1#!/usr/bin/env php
2<?php
28require_once __DIR__ . '/Maintenance.php';
29
32
39 public function __construct() {
40 parent::__construct();
41 $this->addDescription( 'MediaWiki database updater' );
42 $this->addOption( 'skip-compat-checks', 'Skips compatibility checks, mostly for developers' );
43 $this->addOption( 'quick', 'Skip 5 second countdown before starting' );
44 $this->addOption( 'doshared', 'Also update shared tables' );
45 $this->addOption( 'nopurge', 'Do not purge the objectcache table after updates' );
46 $this->addOption( 'noschema', 'Only do the updates that are not done during schema updates' );
47 $this->addOption(
48 'schema',
49 'Output SQL to do the schema updates instead of doing them. Works '
50 . 'even when $wgAllowSchemaUpdates is false',
51 false,
52 true
53 );
54 $this->addOption( 'force', 'Override when $wgAllowSchemaUpdates disables this script' );
55 $this->addOption(
56 'skip-external-dependencies',
57 'Skips checking whether external dependencies are up to date, mostly for developers'
58 );
59 }
60
61 public function getDbType() {
63 }
64
65 private function compatChecks() {
66 $minimumPcreVersion = Installer::MINIMUM_PCRE_VERSION;
67
68 $pcreVersion = explode( ' ', PCRE_VERSION, 2 )[0];
69 if ( version_compare( $pcreVersion, $minimumPcreVersion, '<' ) ) {
70 $this->fatalError(
71 "PCRE $minimumPcreVersion or later is required.\n" .
72 "Your PHP binary is linked with PCRE $pcreVersion.\n\n" .
73 "More information:\n" .
74 "https://www.mediawiki.org/wiki/Manual:Errors_and_symptoms/PCRE\n\n" .
75 "ABORTING.\n" );
76 }
77 }
78
79 public function execute() {
81
83 && !( $this->hasOption( 'force' )
84 || $this->hasOption( 'schema' )
85 || $this->hasOption( 'noschema' ) )
86 ) {
87 $this->fatalError( "Do not run update.php on this wiki. If you're seeing this you should\n"
88 . "probably ask for some help in performing your schema updates or use\n"
89 . "the --noschema and --schema options to get an SQL file for someone\n"
90 . "else to inspect and run.\n\n"
91 . "If you know what you are doing, you can continue with --force\n" );
92 }
93
94 $this->fileHandle = null;
95 if ( substr( $this->getOption( 'schema', '' ), 0, 2 ) === "--" ) {
96 $this->fatalError( "The --schema option requires a file as an argument.\n" );
97 } elseif ( $this->hasOption( 'schema' ) ) {
98 $file = $this->getOption( 'schema' );
99 $this->fileHandle = fopen( $file, "w" );
100 if ( $this->fileHandle === false ) {
101 $err = error_get_last();
102 $this->fatalError( "Problem opening the schema file for writing: $file\n\t{$err['message']}" );
103 }
104 }
105
106 // T206765: We need to load the installer i18n files as some of errors come installer/updater code
107 $wgMessagesDirs['MediawikiInstaller'] = dirname( __DIR__ ) . '/includes/installer/i18n';
108
109 $lang = MediaWikiServices::getInstance()->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 MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
121
122 if ( !$this->hasOption( 'skip-compat-checks' ) ) {
123 $this->compatChecks();
124 } else {
125 $this->output( "Skipping compatibility checks, proceed at your own risk (Ctrl+C to abort)\n" );
126 $this->countDown( 5 );
127 }
128
129 // Check external dependencies are up to date
130 if ( !$this->hasOption( 'skip-external-dependencies' ) ) {
131 $composerLockUpToDate = $this->runChild( CheckComposerLockUpToDate::class );
132 $composerLockUpToDate->execute();
133 } else {
134 $this->output(
135 "Skipping checking whether external dependencies are up to date, proceed at your own risk\n"
136 );
137 }
138
139 # Attempt to connect to the database as a privileged user
140 # This will vomit up an error if there are permissions problems
141 $db = $this->getDB( DB_MASTER );
142
143 # Check to see whether the database server meets the minimum requirements
145 $dbInstallerClass = Installer::getDBInstallerClass( $db->getType() );
146 $status = $dbInstallerClass::meetsMinimumRequirement( $db->getServerVersion() );
147 if ( !$status->isOK() ) {
148 // This might output some wikitext like <strong> but it should be comprehensible
149 $text = $status->getWikiText();
150 $this->fatalError( $text );
151 }
152
153 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
154 $this->output( "Going to run database updates for $dbDomain\n" );
155 if ( $db->getType() === 'sqlite' ) {
157 '@phan-var DatabaseSqlite $db';
158 $this->output( "Using SQLite file: '{$db->getDbFilePath()}'\n" );
159 }
160 $this->output( "Depending on the size of your database this may take a while!\n" );
161
162 if ( !$this->hasOption( 'quick' ) ) {
163 $this->output( "Abort with control-c in the next five seconds "
164 . "(skip this countdown with --quick) ... " );
165 $this->countDown( 5 );
166 }
167
168 $time1 = microtime( true );
169
170 $shared = $this->hasOption( 'doshared' );
171
172 $updates = [ 'core', 'extensions' ];
173 if ( !$this->hasOption( 'schema' ) ) {
174 if ( $this->hasOption( 'noschema' ) ) {
175 $updates[] = 'noschema';
176 }
177 $updates[] = 'stats';
178 }
179
180 $updater = DatabaseUpdater::newForDB( $db, $shared, $this );
181 $updater->doUpdates( $updates );
182
183 foreach ( $updater->getPostDatabaseUpdateMaintenance() as $maint ) {
184 $child = $this->runChild( $maint );
185
186 // LoggedUpdateMaintenance is checking the updatelog itself
187 $isLoggedUpdate = $child instanceof LoggedUpdateMaintenance;
188
189 if ( !$isLoggedUpdate && $updater->updateRowExists( $maint ) ) {
190 continue;
191 }
192
193 $child->execute();
194 if ( !$isLoggedUpdate ) {
195 $updater->insertUpdateRow( $maint );
196 }
197 }
198
199 $updater->setFileAccess();
200 if ( !$this->hasOption( 'nopurge' ) ) {
201 $updater->purgeCache();
202 }
203
204 $time2 = microtime( true );
205
206 $timeDiff = $lang->formatTimePeriod( $time2 - $time1 );
207 $this->output( "\nDone in $timeDiff.\n" );
208 }
209
210 protected function afterFinalSetup() {
212
213 # Don't try to access the database
214 # This needs to be disabled early since extensions will try to use the l10n
215 # cache from $wgExtensionFunctions (T22471)
217 'class' => LocalisationCache::class,
218 'storeClass' => LCStoreNull::class,
219 'storeDirectory' => false,
220 'manualRecache' => false,
221 ];
222 }
223
229 public function validateParamsAndArgs() {
230 // Allow extensions to add additional params.
231 $params = [];
232 $this->getHookRunner()->onMaintenanceUpdateAddParams( $params );
233
234 // This executes before the PHP version check, so don't use null coalesce (??).
235 // Keeping this compatible with older PHP versions lets us reach the code that
236 // displays a more helpful error.
237 foreach ( $params as $name => $param ) {
238 $this->addOption(
239 $name,
240 $param['desc'],
241 isset( $param['require'] ) ? $param['require'] : false,
242 isset( $param['withArg'] ) ? $param['withArg'] : false,
243 isset( $param['shortName'] ) ? $param['shortName'] : false,
244 isset( $param['multiOccurrence'] ) ? $param['multiOccurrence'] : false
245 );
246 }
247
248 parent::validateParamsAndArgs();
249 }
250}
251
252$maintClass = UpdateMediaWiki::class;
253require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
$wgMessagesDirs
Extension messages directories.
$wgAllowSchemaUpdates
Allow schema updates.
$wgLocalisationCacheConf
Localisation cache configuration.
const MW_VERSION
The running version of MediaWiki.
Definition Defines.php:40
const RUN_MAINTENANCE_IF_MAIN
$wgLang
Definition Setup.php:781
const MINIMUM_PCRE_VERSION
The oldest version of PCRE we can support.
Definition Installer.php:62
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.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Maintenance script to run database schema updates.
Definition update.php:38
afterFinalSetup()
Execute a callback function at the end of initialisation Stable for overriding.
Definition update.php:210
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Definition update.php:61
validateParamsAndArgs()
Definition update.php:229
execute()
Do the actual work.
Definition update.php:79
__construct()
Default constructor.
Definition update.php:39
const DB_MASTER
Definition defines.php:29
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:252