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;
48 public function __construct() {
49 $this->versionParser
= new VersionParser();
53 * Set an array with credits of all loaded extensions and skins.
55 * @param array $credits An array of installed extensions with credits of them
56 * @return VersionChecker $this
58 public function setLoadedExtensionsAndSkins( array $credits ) {
59 $this->loaded
= $credits;
65 * Set MediaWiki core version.
67 * @param string $coreVersion Current version of core
68 * @return VersionChecker $this
70 public function setCoreVersion( $coreVersion ) {
72 $this->coreVersion
= new Constraint(
74 $this->versionParser
->normalize( $coreVersion )
76 $this->coreVersion
->setPrettyString( $coreVersion );
77 } catch ( UnexpectedValueException
$e ) {
78 // Non-parsable version, don't fatal.
85 * Check all given dependencies if they are compatible with the named
86 * installed extensions in the $credits array.
88 * Example $extDependencies:
90 * 'GoogleAPIClient' => {
91 * 'MediaWiki' => '>= 1.25.0',
93 * 'FakeExtension' => '>= 1.25.0'
96 * 'FakeSkin' => '>= 1.0.0'
101 * @param array $extDependencies All extensions that depend on other ones
104 public function checkArray( array $extDependencies ) {
106 foreach ( $extDependencies as $extension => $dependencies ) {
107 foreach ( $dependencies as $dependencyType => $values ) {
108 switch ( $dependencyType ) {
109 case ExtensionRegistry
::MEDIAWIKI_CORE
:
110 $errors = array_merge(
112 $this->handleMediaWikiDependency( $values, $extension )
117 foreach ( $values as $dependency => $constraint ) {
118 $errors = array_merge(
120 $this->handleExtensionDependency( $dependency, $constraint, $extension )
125 throw new UnexpectedValueException( 'Dependency type ' . $dependencyType .
126 ' unknown in ' . $extension );
135 * Handle a dependency to MediaWiki core. It will check, if a MediaWiki version constraint was
136 * set with self::setCoreVersion before this call (if not, it will return an empty array) and
137 * checks the version constraint given against it.
139 * @param string $constraint The required version constraint for this dependency
140 * @param string $checkedExt The Extension, which depends on this dependency
141 * @return array An empty array, if MediaWiki version is compatible with $constraint, an array
142 * with an error message, otherwise.
144 private function handleMediaWikiDependency( $constraint, $checkedExt ) {
145 if ( $this->coreVersion
=== false ) {
146 // Couldn't parse the core version, so we can't check anything
150 // if the installed and required version are compatible, return an empty array
151 if ( $this->versionParser
->parseConstraints( $constraint )
152 ->matches( $this->coreVersion
) ) {
155 // otherwise mark this as incompatible.
156 return [ "{$checkedExt} is not compatible with the current "
157 . "MediaWiki core (version {$this->coreVersion->getPrettyString()}), it requires: "
158 . $constraint . '.' ];
162 * Handle a dependency to another extension.
164 * @param string $dependencyName The name of the dependency
165 * @param string $constraint The required version constraint for this dependency
166 * @param string $checkedExt The Extension, which depends on this dependency
167 * @return array An empty array, if installed version is compatible with $constraint, an array
168 * with an error message, otherwise.
170 private function handleExtensionDependency( $dependencyName, $constraint, $checkedExt ) {
172 // Check if the dependency is even installed
173 if ( !isset( $this->loaded
[$dependencyName] ) ) {
174 $incompatible[] = "{$checkedExt} requires {$dependencyName} to be installed.";
175 return $incompatible;
177 // Check if the dependency has specified a version
178 if ( !isset( $this->loaded
[$dependencyName]['version'] ) ) {
179 // If we depend upon any version, and none is set, that's fine.
180 if ( $constraint === '*' ) {
181 wfDebug( "{$dependencyName} does not expose it's version, but {$checkedExt}
182 mentions it with constraint '*'. Assume it's ok so." );
184 // Otherwise, mark it as incompatible.
185 $incompatible[] = "{$dependencyName} does not expose it's version, but {$checkedExt}
186 requires: {$constraint}.";
189 // Try to get a constraint for the dependency version
191 $installedVersion = new Constraint(
193 $this->versionParser
->normalize( $this->loaded
[$dependencyName]['version'] )
195 } catch ( UnexpectedValueException
$e ) {
196 // Non-parsable version, don't fatal, output an error message that the version
198 return [ "Dependency $dependencyName provides an invalid version string." ];
200 // Check if the constraint actually matches...
202 isset( $installedVersion ) &&
203 !$this->versionParser
->parseConstraints( $constraint )->matches( $installedVersion )
205 $incompatible[] = "{$checkedExt} is not compatible with the current "
206 . "installed version of {$dependencyName} "
207 . "({$this->loaded[$dependencyName]['version']}), "
208 . "it requires: " . $constraint . '.';
212 return $incompatible;