MediaWiki  1.34.0
update.php
Go to the documentation of this file.
1 #!/usr/bin/env php
2 <?php
28 require_once __DIR__ . '/Maintenance.php';
29 
31 
38  function __construct() {
39  parent::__construct();
40  $this->addDescription( 'MediaWiki database updater' );
41  $this->addOption( 'skip-compat-checks', 'Skips compatibility checks, mostly for developers' );
42  $this->addOption( 'quick', 'Skip 5 second countdown before starting' );
43  $this->addOption( 'doshared', 'Also update shared tables' );
44  $this->addOption( 'nopurge', 'Do not purge the objectcache table after updates' );
45  $this->addOption( 'noschema', 'Only do the updates that are not done during schema updates' );
46  $this->addOption(
47  'schema',
48  'Output SQL to do the schema updates instead of doing them. Works '
49  . 'even when $wgAllowSchemaUpdates is false',
50  false,
51  true
52  );
53  $this->addOption( 'force', 'Override when $wgAllowSchemaUpdates disables this script' );
54  $this->addOption(
55  'skip-external-dependencies',
56  'Skips checking whether external dependencies are up to date, mostly for developers'
57  );
58  }
59 
60  function getDbType() {
61  return Maintenance::DB_ADMIN;
62  }
63 
64  function compatChecks() {
65  $minimumPcreVersion = Installer::MINIMUM_PCRE_VERSION;
66 
67  $pcreVersion = explode( ' ', PCRE_VERSION, 2 )[0];
68  if ( version_compare( $pcreVersion, $minimumPcreVersion, '<' ) ) {
69  $this->fatalError(
70  "PCRE $minimumPcreVersion or later is required.\n" .
71  "Your PHP binary is linked with PCRE $pcreVersion.\n\n" .
72  "More information:\n" .
73  "https://www.mediawiki.org/wiki/Manual:Errors_and_symptoms/PCRE\n\n" .
74  "ABORTING.\n" );
75  }
76 
77  $test = new PhpXmlBugTester();
78  if ( !$test->ok ) {
79  $this->fatalError(
80  "Your system has a combination of PHP and libxml2 versions that is buggy\n" .
81  "and can cause hidden data corruption in MediaWiki and other web apps.\n" .
82  "Upgrade to libxml2 2.7.3 or later.\n" .
83  "ABORTING (see https://bugs.php.net/bug.php?id=45996).\n" );
84  }
85  }
86 
87  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 ( substr( $this->getOption( 'schema' ), 0, 2 ) === "--" ) {
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  // T206765: We need to load the installer i18n files as some of errors come installer/updater code
115  $wgMessagesDirs['MediawikiInstaller'] = dirname( __DIR__ ) . '/includes/installer/i18n';
116 
117  $lang = Language::factory( 'en' );
118  // Set global language to ensure localised errors are in English (T22633)
119  RequestContext::getMain()->setLanguage( $lang );
120  $wgLang = $lang; // BackCompat
121 
122  define( 'MW_UPDATER', true );
123 
124  $this->output( "MediaWiki {$wgVersion} Updater\n\n" );
125 
126  wfWaitForSlaves();
127 
128  if ( !$this->hasOption( 'skip-compat-checks' ) ) {
129  $this->compatChecks();
130  } else {
131  $this->output( "Skipping compatibility checks, proceed at your own risk (Ctrl+C to abort)\n" );
132  $this->countDown( 5 );
133  }
134 
135  // Check external dependencies are up to date
136  if ( !$this->hasOption( 'skip-external-dependencies' ) ) {
137  $composerLockUpToDate = $this->runChild( CheckComposerLockUpToDate::class );
138  $composerLockUpToDate->execute();
139  } else {
140  $this->output(
141  "Skipping checking whether external dependencies are up to date, proceed at your own risk\n"
142  );
143  }
144 
145  # Attempt to connect to the database as a privileged user
146  # This will vomit up an error if there are permissions problems
147  $db = $this->getDB( DB_MASTER );
148 
149  # Check to see whether the database server meets the minimum requirements
150 
151  $dbInstallerClass = Installer::getDBInstallerClass( $db->getType() );
152  $status = $dbInstallerClass::meetsMinimumRequirement( $db->getServerVersion() );
153  if ( !$status->isOK() ) {
154  // This might output some wikitext like <strong> but it should be comprehensible
155  $text = $status->getWikiText();
156  $this->fatalError( $text );
157  }
158 
159  $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
160  $this->output( "Going to run database updates for $dbDomain\n" );
161  if ( $db->getType() === 'sqlite' ) {
163  '@phan-var DatabaseSqlite $db';
164  $this->output( "Using SQLite file: '{$db->getDbFilePath()}'\n" );
165  }
166  $this->output( "Depending on the size of your database this may take a while!\n" );
167 
168  if ( !$this->hasOption( 'quick' ) ) {
169  $this->output( "Abort with control-c in the next five seconds "
170  . "(skip this countdown with --quick) ... " );
171  $this->countDown( 5 );
172  }
173 
174  $time1 = microtime( true );
175 
176  $badPhpUnit = dirname( __DIR__ ) . '/vendor/phpunit/phpunit/src/Util/PHP/eval-stdin.php';
177  if ( file_exists( $badPhpUnit ) ) {
178  // Bad versions of the file are:
179  // https://raw.githubusercontent.com/sebastianbergmann/phpunit/c820f915bfae34e5a836f94967a2a5ea5ef34f21/src/Util/PHP/eval-stdin.php
180  // https://raw.githubusercontent.com/sebastianbergmann/phpunit/3aaddb1c5bd9b9b8d070b4cf120e71c36fd08412/src/Util/PHP/eval-stdin.php
181  $md5 = md5_file( $badPhpUnit );
182  if ( $md5 === '120ac49800671dc383b6f3709c25c099'
183  || $md5 === '28af792cb38fc9a1b236b91c1aad2876'
184  ) {
185  $success = unlink( $badPhpUnit );
186  if ( $success ) {
187  $this->output( "Removed PHPUnit eval-stdin.php to protect against CVE-2017-9841\n" );
188  } else {
189  $this->error( "Unable to remove $badPhpUnit, you should manually. See CVE-2017-9841" );
190  }
191  }
192  }
193 
194  $shared = $this->hasOption( 'doshared' );
195 
196  $updates = [ 'core', 'extensions' ];
197  if ( !$this->hasOption( 'schema' ) ) {
198  if ( $this->hasOption( 'noschema' ) ) {
199  $updates[] = 'noschema';
200  }
201  $updates[] = 'stats';
202  }
203 
204  $updater = DatabaseUpdater::newForDB( $db, $shared, $this );
205  $updater->doUpdates( $updates );
206 
207  foreach ( $updater->getPostDatabaseUpdateMaintenance() as $maint ) {
208  $child = $this->runChild( $maint );
209 
210  // LoggedUpdateMaintenance is checking the updatelog itself
211  $isLoggedUpdate = $child instanceof LoggedUpdateMaintenance;
212 
213  if ( !$isLoggedUpdate && $updater->updateRowExists( $maint ) ) {
214  continue;
215  }
216 
217  $child->execute();
218  if ( !$isLoggedUpdate ) {
219  $updater->insertUpdateRow( $maint );
220  }
221  }
222 
223  $updater->setFileAccess();
224  if ( !$this->hasOption( 'nopurge' ) ) {
225  $updater->purgeCache();
226  }
227 
228  $time2 = microtime( true );
229 
230  $timeDiff = $lang->formatTimePeriod( $time2 - $time1 );
231  $this->output( "\nDone in $timeDiff.\n" );
232  }
233 
234  function afterFinalSetup() {
236 
237  # Don't try to access the database
238  # This needs to be disabled early since extensions will try to use the l10n
239  # cache from $wgExtensionFunctions (T22471)
241  'class' => LocalisationCache::class,
242  'storeClass' => LCStoreNull::class,
243  'storeDirectory' => false,
244  'manualRecache' => false,
245  ];
246  }
247 
253  public function validateParamsAndArgs() {
254  // Allow extensions to add additional params.
255  $params = [];
256  Hooks::run( 'MaintenanceUpdateAddParams', [ &$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 
276 $maintClass = UpdateMediaWiki::class;
277 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
UpdateMediaWiki\getDbType
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Definition: update.php:60
DatabaseUpdater\newForDB
static newForDB(IMaintainableDatabase $db, $shared=false, Maintenance $maintenance=null)
Definition: DatabaseUpdater.php:191
WikiMap\getCurrentWikiDbDomain
static getCurrentWikiDbDomain()
Definition: WikiMap.php:292
Wikimedia\Rdbms\DatabaseSqlite
Definition: DatabaseSqlite.php:38
UpdateMediaWiki
Maintenance script to run database schema updates.
Definition: update.php:37
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:504
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
Maintenance\runChild
runChild( $maintClass, $classFile=null)
Run a child maintenance script.
Definition: Maintenance.php:727
$wgVersion
$wgVersion
MediaWiki version number.
Definition: DefaultSettings.php:75
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
$wgAllowSchemaUpdates
$wgAllowSchemaUpdates
Allow schema updates.
Definition: DefaultSettings.php:2290
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
$success
$success
Definition: NoLocalSettings.php:42
wfWaitForSlaves
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
Definition: GlobalFunctions.php:2718
UpdateMediaWiki\__construct
__construct()
Default constructor.
Definition: update.php:38
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1727
$wgLang
$wgLang
Definition: Setup.php:881
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
$maintClass
$maintClass
Definition: update.php:91
$wgLocalisationCacheConf
$wgLocalisationCacheConf
Localisation cache configuration.
Definition: DefaultSettings.php:2624
DB_MASTER
const DB_MASTER
Definition: defines.php:26
Maintenance\countDown
countDown( $seconds)
Count down from $seconds to zero on the terminal, with a one-second pause between showing each number...
Definition: Maintenance.php:1572
Maintenance\DB_ADMIN
const DB_ADMIN
Definition: Maintenance.php:89
$wgMessagesDirs
$wgMessagesDirs['ReplaceText']
Definition: ReplaceText.php:50
LoggedUpdateMaintenance\execute
execute()
Do the actual work.
Definition: Maintenance.php:1734
UpdateMediaWiki\afterFinalSetup
afterFinalSetup()
Execute a callback function at the end of initialisation.
Definition: update.php:234
UpdateMediaWiki\compatChecks
compatChecks()
Definition: update.php:64
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1396
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:431
PhpXmlBugTester
Test for PHP+libxml2 bug which breaks XML input subtly with certain versions.
Definition: PhpXmlBugTester.php:32
$status
return $status
Definition: SyntaxHighlight.php:347
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
UpdateMediaWiki\execute
execute()
Do the actual work.
Definition: update.php:87
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:481
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
Installer\MINIMUM_PCRE_VERSION
const MINIMUM_PCRE_VERSION
The oldest version of PCRE we can support.
Definition: Installer.php:54
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:217
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:288
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
UpdateMediaWiki\validateParamsAndArgs
validateParamsAndArgs()
Definition: update.php:253
Installer\getDBInstallerClass
static getDBInstallerClass( $type)
Get the DatabaseInstaller class name for this type.
Definition: Installer.php:562