aboutsummaryrefslogtreecommitdiffstats
path: root/maintenance/doMaintenance.php
blob: dba57a1b54e4d4242a05e2ba6ba947c35f80757b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
 * We want to make this whole thing as seamless as possible to the
 * end-user. Unfortunately, we can't do _all_ of the work in the class
 * because A) included files are not in global scope, but in the scope
 * of their caller, and B) MediaWiki has way too many globals. So instead
 * we'll kinda fake it, and do the requires() inline. <3 PHP
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Maintenance
 */

use MediaWiki\Maintenance\MaintenanceRunner;
use MediaWiki\Settings\SettingsBuilder;

// No AutoLoader yet
require_once __DIR__ . '/includes/MaintenanceRunner.php';
require_once __DIR__ . '/includes/MaintenanceParameters.php';

if ( !defined( 'RUN_MAINTENANCE_IF_MAIN' ) ) {
	echo "This file must be included after Maintenance.php\n";
	exit( 1 );
}

// Wasn't included from the file scope, halt execution (probably wanted the class).
// This typically happens when a maintenance script is executed using run.php.
// @phan-suppress-next-line PhanSuspiciousValueComparisonInGlobalScope
if ( !MaintenanceRunner::shouldExecute() && $maintClass != CommandLineInc::class ) {
	return;
}

// @phan-suppress-next-line PhanImpossibleConditionInGlobalScope
if ( !$maintClass || !class_exists( $maintClass ) ) {
	echo "\$maintClass is not set or is set to a non-existent class.\n";
	exit( 1 );
}

// Define the MediaWiki entrypoint
define( 'MEDIAWIKI', true );

$IP = wfDetectInstallPath();
require_once "$IP/includes/AutoLoader.php";

$runner = new MaintenanceRunner();
$runner->initForClass( $maintClass, $GLOBALS['argv'] );

// We used to call this variable $self, but it was moved
// to $maintenance->mSelf. Keep that here for b/c
$self = $runner->getName();

$runner->defineSettings();

// Custom setup for Maintenance entry point
if ( !defined( 'MW_FINAL_SETUP_CALLBACK' ) ) {

	// Define a function, since we can't put a closure or object
	// reference into MW_FINAL_SETUP_CALLBACK.
	function wfMaintenanceSetup( SettingsBuilder $settingsBuilder ) {
		global $runner;
		$runner->setup( $settingsBuilder );
	}

	define( 'MW_FINAL_SETUP_CALLBACK', 'wfMaintenanceSetup' );
}

// Initialize MediaWiki (load settings, initialized session,
// enable MediaWikiServices)
require_once "$IP/includes/Setup.php";

// We only get here if the script was invoked directly.
// If it was loaded by MaintenanceRunner, MaintenanceRunner::shouldExecute() would have returned false,
// and we would have returned from this file early.

if ( stream_isatty( STDOUT ) ) {
	echo "\n";
	echo "*******************************************************************************\n";
	echo "NOTE: Do not run maintenance scripts directly, use maintenance/run.php instead!\n";
	echo "      Running scripts directly has been deprecated in MediaWiki 1.40.\n";
	echo "      It may not work for some (or any) scripts in the future.\n";
	echo "*******************************************************************************\n";
	echo "\n";
}

// Do it!
$success = $runner->run();

// Exit with an error status if execute() returned false
if ( !$success ) {
	exit( 1 );
}