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;
41 private $versionParser;
43 public function __construct() {
44 $this->versionParser
= new VersionParser();
48 * Set MediaWiki core version.
50 * @param string $coreVersion Current version of core
51 * @return VersionChecker $this
53 public function setCoreVersion( $coreVersion ) {
55 $this->coreVersion
= new Constraint(
57 $this->versionParser
->normalize( $coreVersion )
59 $this->coreVersion
->setPrettyString( $coreVersion );
60 } catch ( UnexpectedValueException
$e ) {
61 // Non-parsable version, don't fatal.
68 * Check all given dependencies if they are compatible with the named
69 * installed extensions in the $credits array.
71 * Example $extDependencies:
73 * 'GoogleAPIClient' => {
74 * 'MediaWiki' => '>= 1.25.0'
78 * @param array $extDependencies All extensions that depend on other ones
81 public function checkArray( array $extDependencies ) {
83 foreach ( $extDependencies as $extension => $dependencies ) {
84 foreach ( $dependencies as $dependencyType => $values ) {
85 switch ( $dependencyType ) {
86 case ExtensionRegistry
::MEDIAWIKI_CORE
:
87 $errors = array_merge(
89 $this->handleMediaWikiDependency( $values, $extension )
93 throw new UnexpectedValueException( 'Dependency type ' . $dependencyType .
94 ' unknown in ' . $extension );
103 * Handle a dependency to MediaWiki core. It will check, if a MediaWiki version constraint was
104 * set with self::setCoreVersion before this call (if not, it will return an empty array) and
105 * checks the version constraint given against it.
107 * @param string $constraint The required version constraint for this dependency
108 * @param string $checkedExt The Extension, which depends on this dependency
109 * @return array An empty array, if MediaWiki version is compatible with $constraint, an array
110 * with an error message, otherwise.
112 private function handleMediaWikiDependency( $constraint, $checkedExt ) {
113 if ( $this->coreVersion
=== false ) {
114 // Couldn't parse the core version, so we can't check anything
118 // if the installed and required version are compatible, return an empty array
119 if ( $this->versionParser
->parseConstraints( $constraint )
120 ->matches( $this->coreVersion
) ) {
123 // otherwise mark this as incompatible.
124 return [ "{$checkedExt} is not compatible with the current "
125 . "MediaWiki core (version {$this->coreVersion->getPrettyString()}), it requires: "
126 . $constraint . '.' ];