Add partial support for running Parsoid selser tests
[mediawiki.git] / maintenance / doMaintenance.php
blob66c21dfd57eaf62faaec709d12fc692e2e1c0f30
1 <?php
2 /**
3 * We want to make this whole thing as seamless as possible to the
4 * end-user. Unfortunately, we can't do _all_ of the work in the class
5 * because A) included files are not in global scope, but in the scope
6 * of their caller, and B) MediaWiki has way too many globals. So instead
7 * we'll kinda fake it, and do the requires() inline. <3 PHP
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
24 * @file
25 * @ingroup Maintenance
27 use MediaWiki\MediaWikiServices;
28 use MediaWiki\Settings\SettingsBuilder;
30 if ( !defined( 'RUN_MAINTENANCE_IF_MAIN' ) ) {
31 echo "This file must be included after Maintenance.php\n";
32 exit( 1 );
35 // Wasn't included from the file scope, halt execution (probably wanted the class)
36 // If a class is using CommandLineInc (old school maintenance), they definitely
37 // cannot be included and will proceed with execution
38 // @phan-suppress-next-line PhanSuspiciousValueComparisonInGlobalScope
39 if ( !Maintenance::shouldExecute() && $maintClass != CommandLineInc::class ) {
40 return;
43 // @phan-suppress-next-line PhanImpossibleConditionInGlobalScope
44 if ( !$maintClass || !class_exists( $maintClass ) ) {
45 echo "\$maintClass is not set or is set to a non-existent class.\n";
46 exit( 1 );
49 // Define the MediaWiki entrypoint
50 define( 'MEDIAWIKI', true );
52 // This environment variable is ensured present by Maintenance.php.
53 $IP = getenv( 'MW_INSTALL_PATH' );
55 // Get an object to start us off
56 /** @var Maintenance $maintenance */
57 $maintenance = new $maintClass();
59 // Basic checks and such
60 $maintenance->setup();
62 // We used to call this variable $self, but it was moved
63 // to $maintenance->mSelf. Keep that here for b/c
64 $self = $maintenance->getName();
66 // Define how settings are loaded (e.g. LocalSettings.php)
67 if ( !defined( 'MW_CONFIG_CALLBACK' ) ) {
68 $maintenance->loadSettings();
71 // Custom setup for Maintenance entry point
72 if ( !defined( 'MW_SETUP_CALLBACK' ) ) {
74 function wfMaintenanceSetup( SettingsBuilder $settingsBuilder ) {
75 global $maintenance;
76 $config = $settingsBuilder->getConfig();
78 if ( $maintenance->getDbType() === Maintenance::DB_NONE ) {
79 $cacheConf = $config->get( 'LocalisationCacheConf' );
80 if ( $cacheConf['storeClass'] === false
81 && ( $cacheConf['store'] == 'db'
82 || ( $cacheConf['store'] == 'detect'
83 && !$config->get( 'CacheDirectory' ) ) )
84 ) {
85 $cacheConf['storeClass'] = LCStoreNull::class;
86 $settingsBuilder->putConfigValue( 'LocalisationCacheConf', $cacheConf );
90 $maintenance->finalSetup( $settingsBuilder );
93 define( 'MW_SETUP_CALLBACK', 'wfMaintenanceSetup' );
96 require_once "$IP/includes/Setup.php";
98 // Initialize main config instance
99 $maintenance->setConfig( MediaWikiServices::getInstance()->getMainConfig() );
101 // Double check required extensions are installed
102 $maintenance->checkRequiredExtensions();
104 if ( $maintenance->getDbType() == Maintenance::DB_NONE ) {
105 // Be strict with maintenance tasks that claim to not need a database by
106 // disabling the storage backend.
107 MediaWikiServices::disableStorageBackend();
110 $maintenance->validateParamsAndArgs();
112 // Do the work
113 try {
114 $success = $maintenance->execute();
115 } catch ( Exception $ex ) {
116 $success = false;
117 $exReportMessage = '';
118 while ( $ex ) {
119 $cls = get_class( $ex );
120 $exReportMessage .= "$cls from line {$ex->getLine()} of {$ex->getFile()}: {$ex->getMessage()}\n";
121 $exReportMessage .= $ex->getTraceAsString() . "\n";
122 $ex = $ex->getPrevious();
124 // Print the exception to stderr if possible, don't mix it in
125 // with stdout output.
126 if ( defined( 'STDERR' ) ) {
127 fwrite( STDERR, $exReportMessage );
128 } else {
129 echo $exReportMessage;
133 // Potentially debug globals
134 $maintenance->globals();
136 $maintenance->shutdown();
138 // Exit with an error status if execute() returned false
139 if ( $success === false ) {
140 exit( 1 );