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 array Loaded extensions
46 private $versionParser;
49 * @param string $coreVersion Current version of core
51 public function __construct( $coreVersion ) {
52 $this->versionParser
= new VersionParser();
53 $this->setCoreVersion( $coreVersion );
57 * Set an array with credits of all loaded extensions and skins.
59 * @param array $credits An array of installed extensions with credits of them
60 * @return VersionChecker $this
62 public function setLoadedExtensionsAndSkins( array $credits ) {
63 $this->loaded
= $credits;
69 * Set MediaWiki core version.
71 * @param string $coreVersion Current version of core
73 private function setCoreVersion( $coreVersion ) {
75 $this->coreVersion
= new Constraint(
77 $this->versionParser
->normalize( $coreVersion )
79 $this->coreVersion
->setPrettyString( $coreVersion );
80 } catch ( UnexpectedValueException
$e ) {
81 // Non-parsable version, don't fatal.
86 * Check all given dependencies if they are compatible with the named
87 * installed extensions in the $credits array.
89 * Example $extDependencies:
92 * 'MediaWiki' => '>= 1.25.0',
94 * 'FooBaz' => '>= 1.25.0'
97 * 'BazBar' => '>= 1.0.0'
102 * @param array $extDependencies All extensions that depend on other ones
105 public function checkArray( array $extDependencies ) {
107 foreach ( $extDependencies as $extension => $dependencies ) {
108 foreach ( $dependencies as $dependencyType => $values ) {
109 switch ( $dependencyType ) {
110 case ExtensionRegistry
::MEDIAWIKI_CORE
:
111 $mwError = $this->handleMediaWikiDependency( $values, $extension );
112 if ( $mwError !== false ) {
115 'type' => 'incompatible-core',
121 foreach ( $values as $dependency => $constraint ) {
122 $extError = $this->handleExtensionDependency(
123 $dependency, $constraint, $extension, $dependencyType
125 if ( $extError !== false ) {
126 $errors[] = $extError;
131 throw new UnexpectedValueException( 'Dependency type ' . $dependencyType .
132 ' unknown in ' . $extension );
141 * Handle a dependency to MediaWiki core. It will check, if a MediaWiki version constraint was
142 * set with self::setCoreVersion before this call (if not, it will return an empty array) and
143 * checks the version constraint given against it.
145 * @param string $constraint The required version constraint for this dependency
146 * @param string $checkedExt The Extension, which depends on this dependency
147 * @return bool|string false if no error, or a string with the message
149 private function handleMediaWikiDependency( $constraint, $checkedExt ) {
150 if ( $this->coreVersion
=== false ) {
151 // Couldn't parse the core version, so we can't check anything
155 // if the installed and required version are compatible, return an empty array
156 if ( $this->versionParser
->parseConstraints( $constraint )
157 ->matches( $this->coreVersion
) ) {
160 // otherwise mark this as incompatible.
161 return "{$checkedExt} is not compatible with the current "
162 . "MediaWiki core (version {$this->coreVersion->getPrettyString()}), it requires: "
167 * Handle a dependency to another extension.
169 * @param string $dependencyName The name of the dependency
170 * @param string $constraint The required version constraint for this dependency
171 * @param string $checkedExt The Extension, which depends on this dependency
172 * @param string $type Either 'extensions' or 'skins'
173 * @return bool|array false for no errors, or an array of info
175 private function handleExtensionDependency( $dependencyName, $constraint, $checkedExt,
178 // Check if the dependency is even installed
179 if ( !isset( $this->loaded
[$dependencyName] ) ) {
181 'msg' => "{$checkedExt} requires {$dependencyName} to be installed.",
182 'type' => "missing-$type",
183 'missing' => $dependencyName,
186 if ( $constraint === '*' ) {
187 // short-circuit since any version is OK.
190 // Check if the dependency has specified a version
191 if ( !isset( $this->loaded
[$dependencyName]['version'] ) ) {
192 $msg = "{$dependencyName} does not expose its version, but {$checkedExt}"
193 . " requires: {$constraint}.";
196 'type' => "incompatible-$type",
197 'incompatible' => $checkedExt,
200 // Try to get a constraint for the dependency version
202 $installedVersion = new Constraint(
204 $this->versionParser
->normalize( $this->loaded
[$dependencyName]['version'] )
206 } catch ( UnexpectedValueException
$e ) {
207 // Non-parsable version, output an error message that the version
210 'msg' => "$dependencyName does not have a valid version string.",
211 'type' => 'invalid-version',
214 // Check if the constraint actually matches...
216 !$this->versionParser
->parseConstraints( $constraint )->matches( $installedVersion )
218 $msg = "{$checkedExt} is not compatible with the current "
219 . "installed version of {$dependencyName} "
220 . "({$this->loaded[$dependencyName]['version']}), "
221 . "it requires: " . $constraint . '.';
224 'type' => "incompatible-$type",
225 'incompatible' => $checkedExt,