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 array Loaded extensions
51 private $versionParser;
54 * @param string $coreVersion Current version of core
56 public function __construct( $coreVersion, $phpVersion ) {
57 $this->versionParser
= new VersionParser();
58 $this->setCoreVersion( $coreVersion );
59 $this->setPhpVersion( $phpVersion );
63 * Set an array with credits of all loaded extensions and skins.
65 * @param array $credits An array of installed extensions with credits of them
66 * @return VersionChecker $this
68 public function setLoadedExtensionsAndSkins( array $credits ) {
69 $this->loaded
= $credits;
75 * Set MediaWiki core version.
77 * @param string $coreVersion Current version of core
79 private function setCoreVersion( $coreVersion ) {
81 $this->coreVersion
= new Constraint(
83 $this->versionParser
->normalize( $coreVersion )
85 $this->coreVersion
->setPrettyString( $coreVersion );
86 } catch ( UnexpectedValueException
$e ) {
87 // Non-parsable version, don't fatal.
94 * @param string $phpVersion Current PHP version. Must be well-formed.
95 * @throws UnexpectedValueException
97 private function setPhpVersion( $phpVersion ) {
98 // normalize to make this throw an exception if the version is invalid
99 $this->phpVersion
= new Constraint(
101 $this->versionParser
->normalize( $phpVersion )
103 $this->phpVersion
->setPrettyString( $phpVersion );
107 * Check all given dependencies if they are compatible with the named
108 * installed extensions in the $credits array.
110 * Example $extDependencies:
113 * 'MediaWiki' => '>= 1.25.0',
118 * 'FooBaz' => '>= 1.25.0'
121 * 'BazBar' => '>= 1.0.0'
126 * @param array $extDependencies All extensions that depend on other ones
129 public function checkArray( array $extDependencies ) {
131 foreach ( $extDependencies as $extension => $dependencies ) {
132 foreach ( $dependencies as $dependencyType => $values ) {
133 switch ( $dependencyType ) {
134 case ExtensionRegistry
::MEDIAWIKI_CORE
:
135 $mwError = $this->handleDependency(
140 if ( $mwError !== false ) {
143 "{$extension} is not compatible with the current MediaWiki "
144 . "core (version {$this->coreVersion->getPrettyString()}), "
145 . "it requires: $values."
147 'type' => 'incompatible-core',
152 foreach ( $values as $dependency => $constraint ) {
153 if ( $dependency === 'php' ) {
154 $phpError = $this->handleDependency(
159 if ( $phpError !== false ) {
162 "{$extension} is not compatible with the current PHP "
163 . "version {$this->phpVersion->getPrettyString()}), "
164 . "it requires: $constraint."
166 'type' => 'incompatible-php',
170 // add other platform dependencies here
171 throw new UnexpectedValueException( 'Dependency type ' . $dependency .
172 ' unknown in ' . $extension );
178 foreach ( $values as $dependency => $constraint ) {
179 $extError = $this->handleExtensionDependency(
180 $dependency, $constraint, $extension, $dependencyType
182 if ( $extError !== false ) {
183 $errors[] = $extError;
188 throw new UnexpectedValueException( 'Dependency type ' . $dependencyType .
189 ' unknown in ' . $extension );
198 * Handle a simple dependency to MediaWiki core or PHP. See handleMediaWikiDependency and
199 * handlePhpDependency for details.
201 * @param Constraint|bool $version The version installed
202 * @param string $constraint The required version constraint for this dependency
203 * @param string $checkedExt The Extension, which depends on this dependency
204 * @return bool false if no error, true else
206 private function handleDependency( $version, $constraint, $checkedExt ) {
207 if ( $version === false ) {
208 // Couldn't parse the version, so we can't check anything
212 // if the installed and required version are compatible, return an empty array
213 if ( $this->versionParser
->parseConstraints( $constraint )
214 ->matches( $version ) ) {
222 * Handle a dependency to another extension.
224 * @param string $dependencyName The name of the dependency
225 * @param string $constraint The required version constraint for this dependency
226 * @param string $checkedExt The Extension, which depends on this dependency
227 * @param string $type Either 'extensions' or 'skins'
228 * @return bool|array false for no errors, or an array of info
230 private function handleExtensionDependency( $dependencyName, $constraint, $checkedExt,
233 // Check if the dependency is even installed
234 if ( !isset( $this->loaded
[$dependencyName] ) ) {
236 'msg' => "{$checkedExt} requires {$dependencyName} to be installed.",
237 'type' => "missing-$type",
238 'missing' => $dependencyName,
241 if ( $constraint === '*' ) {
242 // short-circuit since any version is OK.
245 // Check if the dependency has specified a version
246 if ( !isset( $this->loaded
[$dependencyName]['version'] ) ) {
247 $msg = "{$dependencyName} does not expose its version, but {$checkedExt}"
248 . " requires: {$constraint}.";
251 'type' => "incompatible-$type",
252 'incompatible' => $checkedExt,
255 // Try to get a constraint for the dependency version
257 $installedVersion = new Constraint(
259 $this->versionParser
->normalize( $this->loaded
[$dependencyName]['version'] )
261 } catch ( UnexpectedValueException
$e ) {
262 // Non-parsable version, output an error message that the version
265 'msg' => "$dependencyName does not have a valid version string.",
266 'type' => 'invalid-version',
269 // Check if the constraint actually matches...
271 !$this->versionParser
->parseConstraints( $constraint )->matches( $installedVersion )
273 $msg = "{$checkedExt} is not compatible with the current "
274 . "installed version of {$dependencyName} "
275 . "({$this->loaded[$dependencyName]['version']}), "
276 . "it requires: " . $constraint . '.';
279 'type' => "incompatible-$type",
280 'incompatible' => $checkedExt,