4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
20 * @author Florian Schmidt
23 use Composer\Semver\VersionParser
;
24 use Composer\Semver\Constraint\Constraint
;
27 * Provides functions to check a set of extensions with dependencies against
28 * a set of loaded extensions and given version information.
32 class VersionChecker
{
34 * @var Constraint|bool representing $wgVersion
36 private $coreVersion = false;
39 * @var Constraint|bool representing PHP version
41 private $phpVersion = false;
44 * @var string[] List of installed PHP extensions
46 private $phpExtensions = [];
49 * @var array Loaded extensions
56 private $versionParser;
59 * @param string $coreVersion Current version of core
60 * @param string $phpVersion Current PHP version
61 * @param string[] $phpExtensions List of installed PHP extensions
63 public function __construct( $coreVersion, $phpVersion, array $phpExtensions ) {
64 $this->versionParser
= new VersionParser();
65 $this->setCoreVersion( $coreVersion );
66 $this->setPhpVersion( $phpVersion );
67 $this->phpExtensions
= $phpExtensions;
71 * Set an array with credits of all loaded extensions and skins.
73 * @param array $credits An array of installed extensions with credits of them
74 * @return VersionChecker $this
76 public function setLoadedExtensionsAndSkins( array $credits ) {
77 $this->loaded
= $credits;
83 * Set MediaWiki core version.
85 * @param string $coreVersion Current version of core
87 private function setCoreVersion( $coreVersion ) {
89 $this->coreVersion
= new Constraint(
91 $this->versionParser
->normalize( $coreVersion )
93 $this->coreVersion
->setPrettyString( $coreVersion );
94 } catch ( UnexpectedValueException
$e ) {
95 // Non-parsable version, don't fatal.
102 * @param string $phpVersion Current PHP version. Must be well-formed.
103 * @throws UnexpectedValueException
105 private function setPhpVersion( $phpVersion ) {
106 // normalize to make this throw an exception if the version is invalid
107 $this->phpVersion
= new Constraint(
109 $this->versionParser
->normalize( $phpVersion )
111 $this->phpVersion
->setPrettyString( $phpVersion );
115 * Check all given dependencies if they are compatible with the named
116 * installed extensions in the $credits array.
118 * Example $extDependencies:
121 * 'MediaWiki' => '>= 1.25.0',
127 * 'FooBaz' => '>= 1.25.0'
130 * 'BazBar' => '>= 1.0.0'
135 * @param array $extDependencies All extensions that depend on other ones
138 public function checkArray( array $extDependencies ) {
140 foreach ( $extDependencies as $extension => $dependencies ) {
141 foreach ( $dependencies as $dependencyType => $values ) {
142 switch ( $dependencyType ) {
143 case ExtensionRegistry
::MEDIAWIKI_CORE
:
144 $mwError = $this->handleDependency(
149 if ( $mwError !== false ) {
152 "{$extension} is not compatible with the current MediaWiki "
153 . "core (version {$this->coreVersion->getPrettyString()}), "
154 . "it requires: $values."
156 'type' => 'incompatible-core',
161 foreach ( $values as $dependency => $constraint ) {
162 if ( $dependency === 'php' ) {
164 $phpError = $this->handleDependency(
169 if ( $phpError !== false ) {
172 "{$extension} is not compatible with the current PHP "
173 . "version {$this->phpVersion->getPrettyString()}), "
174 . "it requires: $constraint."
176 'type' => 'incompatible-php',
179 } elseif ( substr( $dependency, 0, 4 ) === 'ext-' ) {
181 $phpExtension = substr( $dependency, 4 );
182 if ( $constraint !== '*' ) {
183 throw new UnexpectedValueException( 'Version constraints for '
184 . 'PHP extensions are not supported in ' . $extension );
186 if ( !in_array( $phpExtension, $this->phpExtensions
, true ) ) {
189 "{$extension} requires {$phpExtension} PHP extension "
192 'type' => 'missing-phpExtension',
193 'missing' => $phpExtension,
197 // add other platform dependencies here
198 throw new UnexpectedValueException( 'Dependency type ' . $dependency .
199 ' unknown in ' . $extension );
205 foreach ( $values as $dependency => $constraint ) {
206 $extError = $this->handleExtensionDependency(
207 $dependency, $constraint, $extension, $dependencyType
209 if ( $extError !== false ) {
210 $errors[] = $extError;
215 throw new UnexpectedValueException( 'Dependency type ' . $dependencyType .
216 ' unknown in ' . $extension );
225 * Handle a simple dependency to MediaWiki core or PHP. See handleMediaWikiDependency and
226 * handlePhpDependency for details.
228 * @param Constraint|bool $version The version installed
229 * @param string $constraint The required version constraint for this dependency
230 * @param string $checkedExt The Extension, which depends on this dependency
231 * @return bool false if no error, true else
233 private function handleDependency( $version, $constraint, $checkedExt ) {
234 if ( $version === false ) {
235 // Couldn't parse the version, so we can't check anything
239 // if the installed and required version are compatible, return an empty array
240 if ( $this->versionParser
->parseConstraints( $constraint )
241 ->matches( $version ) ) {
249 * Handle a dependency to another extension.
251 * @param string $dependencyName The name of the dependency
252 * @param string $constraint The required version constraint for this dependency
253 * @param string $checkedExt The Extension, which depends on this dependency
254 * @param string $type Either 'extensions' or 'skins'
255 * @return bool|array false for no errors, or an array of info
257 private function handleExtensionDependency( $dependencyName, $constraint, $checkedExt,
260 // Check if the dependency is even installed
261 if ( !isset( $this->loaded
[$dependencyName] ) ) {
263 'msg' => "{$checkedExt} requires {$dependencyName} to be installed.",
264 'type' => "missing-$type",
265 'missing' => $dependencyName,
268 if ( $constraint === '*' ) {
269 // short-circuit since any version is OK.
272 // Check if the dependency has specified a version
273 if ( !isset( $this->loaded
[$dependencyName]['version'] ) ) {
274 $msg = "{$dependencyName} does not expose its version, but {$checkedExt}"
275 . " requires: {$constraint}.";
278 'type' => "incompatible-$type",
279 'incompatible' => $checkedExt,
282 // Try to get a constraint for the dependency version
284 $installedVersion = new Constraint(
286 $this->versionParser
->normalize( $this->loaded
[$dependencyName]['version'] )
288 } catch ( UnexpectedValueException
$e ) {
289 // Non-parsable version, output an error message that the version
292 'msg' => "$dependencyName does not have a valid version string.",
293 'type' => 'invalid-version',
296 // Check if the constraint actually matches...
298 !$this->versionParser
->parseConstraints( $constraint )->matches( $installedVersion )
300 $msg = "{$checkedExt} is not compatible with the current "
301 . "installed version of {$dependencyName} "
302 . "({$this->loaded[$dependencyName]['version']}), "
303 . "it requires: " . $constraint . '.';
306 'type' => "incompatible-$type",
307 'incompatible' => $checkedExt,