aboutsummaryrefslogtreecommitdiffstats
path: root/maintenance/checkDependencies.php
blob: 69a646382d71d43cfccbba1821b3d14ccb616d73 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/**
 * (C) 2019 Kunal Mehta <legoktm@debian.org>
 *
 * 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
 */

use MediaWiki\MainConfigNames;
use MediaWiki\Maintenance\Maintenance;
use MediaWiki\Registration\ExtensionDependencyError;
use MediaWiki\Registration\ExtensionRegistry;

// @codeCoverageIgnoreStart
require_once __DIR__ . '/Maintenance.php';
// @codeCoverageIgnoreEnd

/**
 * Checks dependencies for extensions, mostly without loading them
 *
 * @since 1.34
 */
class CheckDependencies extends Maintenance {

	/** @var bool */
	private $checkDev;

	public function __construct() {
		parent::__construct();
		$this->addDescription( 'Check dependencies for extensions' );
		$this->addOption( 'extensions', 'Comma separated list of extensions to check', false, true );
		$this->addOption( 'skins', 'Comma separated list of skins to check', false, true );
		$this->addOption( 'json', 'Output in JSON' );
		$this->addOption( 'dev', 'Check development dependencies too' );
	}

	public function execute() {
		$this->checkDev = $this->hasOption( 'dev' );
		$extensions = $this->hasOption( 'extensions' )
			? explode( ',', $this->getOption( 'extensions' ) )
			: [];
		$skins = $this->hasOption( 'skins' )
			? explode( ',', $this->getOption( 'skins' ) )
			: [];

		$dependencies = [];
		// Note that we can only use the main registry if we are
		// not checking development dependencies.
		$registry = ExtensionRegistry::getInstance();
		foreach ( $extensions as $extension ) {
			if ( !$this->checkDev && $registry->isLoaded( $extension ) ) {
				// If it's already loaded, we know all the dependencies resolved.
				$this->addToDependencies( $dependencies, [ $extension ], [] );
				continue;
			}
			$this->loadThing( $dependencies, $extension, [ $extension ], [] );
		}

		foreach ( $skins as $skin ) {
			if ( !$this->checkDev && $registry->isLoaded( $skin ) ) {
				// If it's already loaded, we know all the dependencies resolved.
				$this->addToDependencies( $dependencies, [], [ $skin ] );
				continue;
			}
			$this->loadThing( $dependencies, $skin, [], [ $skin ] );
		}

		if ( $this->hasOption( 'json' ) ) {
			$this->output( json_encode( $dependencies ) . "\n" );
		} else {
			$this->output( $this->formatForHumans( $dependencies ) . "\n" );
		}
	}

	private function loadThing( &$dependencies, $name, $extensions, $skins ) {
		$extDir = $this->getConfig()->get( MainConfigNames::ExtensionDirectory );
		$styleDir = $this->getConfig()->get( MainConfigNames::StyleDirectory );
		$queue = [];
		$missing = false;
		foreach ( $extensions as $extension ) {
			$path = "$extDir/$extension/extension.json";
			if ( file_exists( $path ) ) {
				// 1 is ignored
				$queue[$path] = 1;
				$this->addToDependencies( $dependencies, [ $extension ], [], $name );
			} else {
				$missing = true;
				$this->addToDependencies( $dependencies, [ $extension ], [], $name, 'missing' );
			}
		}

		foreach ( $skins as $skin ) {
			$path = "$styleDir/$skin/skin.json";
			if ( file_exists( $path ) ) {
				$queue[$path] = 1;
				$this->addToDependencies( $dependencies, [], [ $skin ], $name );
			} else {
				$missing = true;
				$this->addToDependencies( $dependencies, [], [ $skin ], $name, 'missing' );
			}
		}

		if ( $missing ) {
			// Stuff is missing, give up
			return;
		}

		$registry = new ExtensionRegistry();
		$registry->setCheckDevRequires( $this->checkDev );
		try {
			$registry->readFromQueue( $queue );
		} catch ( ExtensionDependencyError $e ) {
			$reason = false;
			if ( $e->incompatibleCore ) {
				$reason = 'incompatible-core';
			} elseif ( $e->incompatibleSkins ) {
				$reason = 'incompatible-skins';
			} elseif ( $e->incompatibleExtensions ) {
				$reason = 'incompatible-extensions';
			} elseif ( $e->missingExtensions || $e->missingSkins ) {
				// There's an extension missing in the dependency tree,
				// so add those to the dependency list and try again
				$this->loadThing(
					$dependencies,
					$name,
					array_merge( $extensions, $e->missingExtensions ),
					array_merge( $skins, $e->missingSkins )
				);
				return;
			} else {
				// missing-phpExtension
				// missing-ability
				// XXX: ???
				$this->fatalError( $e->getMessage() );
			}

			$this->addToDependencies( $dependencies, $extensions, $skins, $name, $reason, $e->getMessage() );
		}

		$this->addToDependencies( $dependencies, $extensions, $skins, $name );
	}

	private function addToDependencies( &$dependencies, $extensions, $skins,
		$why = null, $status = null, $message = null
	) {
		$mainRegistry = ExtensionRegistry::getInstance();
		$iter = [ 'extensions' => $extensions, 'skins' => $skins ];
		foreach ( $iter as $type => $things ) {
			foreach ( $things as $thing ) {
				$preStatus = $dependencies[$type][$thing]['status'] ?? false;
				if ( $preStatus !== 'loaded' ) {
					// OK to overwrite
					if ( $status ) {
						$tStatus = $status;
					} else {
						$tStatus = $mainRegistry->isLoaded( $thing ) ? 'loaded' : 'present';
					}
					$dependencies[$type][$thing]['status'] = $tStatus;
				}
				if ( $why !== null ) {
					$dependencies[$type][$thing]['why'][] = $why;
					// XXX: this is a bit messy
					$dependencies[$type][$thing]['why'] = array_unique(
						$dependencies[$type][$thing]['why'] );
				}

				if ( $message !== null ) {
					$dependencies[$type][$thing]['message'] = trim( $message );
				}

			}
		}
	}

	private function formatForHumans( $dependencies ) {
		$text = '';
		foreach ( $dependencies as $type => $things ) {
			$text .= ucfirst( $type ) . "\n" . str_repeat( '=', strlen( $type ) ) . "\n";
			foreach ( $things as $thing => $info ) {
				$why = $info['why'] ?? [];
				if ( $why ) {
					$whyText = '(because: ' . implode( ',', $why ) . ') ';
				} else {
					$whyText = '';
				}
				$msg = isset( $info['message'] ) ? ", {$info['message']}" : '';

				$text .= "$thing: {$info['status']}{$msg} $whyText\n";
			}
			$text .= "\n";
		}

		return trim( $text );
	}
}

// @codeCoverageIgnoreStart
$maintClass = CheckDependencies::class;
require_once RUN_MAINTENANCE_IF_MAIN;
// @codeCoverageIgnoreEnd