Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / registration / VersionChecker.php
blob5aaaa1b8ab9eca959339fe9202f463c29ee4b052
1 <?php
3 /**
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
19 * @author Legoktm
20 * @author Florian Schmidt
23 use Composer\Semver\VersionParser;
24 use Composer\Semver\Constraint\Constraint;
26 /**
27 * Provides functions to check a set of extensions with dependencies against
28 * a set of loaded extensions and given version information.
30 * @since 1.29
32 class VersionChecker {
33 /**
34 * @var Constraint|bool representing $wgVersion
36 private $coreVersion = false;
38 /**
39 * @var array Loaded extensions
41 private $loaded = [];
43 /**
44 * @var VersionParser
46 private $versionParser;
48 /**
49 * @param string $coreVersion Current version of core
51 public function __construct( $coreVersion ) {
52 $this->versionParser = new VersionParser();
53 $this->setCoreVersion( $coreVersion );
56 /**
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;
65 return $this;
68 /**
69 * Set MediaWiki core version.
71 * @param string $coreVersion Current version of core
73 private function setCoreVersion( $coreVersion ) {
74 try {
75 $this->coreVersion = new Constraint(
76 '==',
77 $this->versionParser->normalize( $coreVersion )
79 $this->coreVersion->setPrettyString( $coreVersion );
80 } catch ( UnexpectedValueException $e ) {
81 // Non-parsable version, don't fatal.
85 /**
86 * Check all given dependencies if they are compatible with the named
87 * installed extensions in the $credits array.
89 * Example $extDependencies:
90 * {
91 * 'FooBar' => {
92 * 'MediaWiki' => '>= 1.25.0',
93 * 'extensions' => {
94 * 'FooBaz' => '>= 1.25.0'
95 * },
96 * 'skins' => {
97 * 'BazBar' => '>= 1.0.0'
98 * }
99 * }
102 * @param array $extDependencies All extensions that depend on other ones
103 * @return array
105 public function checkArray( array $extDependencies ) {
106 $errors = [];
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 ) {
113 $errors[] = $mwError;
115 break;
116 case 'extensions':
117 case 'skin':
118 foreach ( $values as $dependency => $constraint ) {
119 $extError = $this->handleExtensionDependency( $dependency, $constraint, $extension );
120 if ( $extError !== false ) {
121 $errors[] = $extError;
124 break;
125 default:
126 throw new UnexpectedValueException( 'Dependency type ' . $dependencyType .
127 ' unknown in ' . $extension );
132 return $errors;
136 * Handle a dependency to MediaWiki core. It will check, if a MediaWiki version constraint was
137 * set with self::setCoreVersion before this call (if not, it will return an empty array) and
138 * checks the version constraint given against it.
140 * @param string $constraint The required version constraint for this dependency
141 * @param string $checkedExt The Extension, which depends on this dependency
142 * @return bool|string false if no error, or a string with the message
144 private function handleMediaWikiDependency( $constraint, $checkedExt ) {
145 if ( $this->coreVersion === false ) {
146 // Couldn't parse the core version, so we can't check anything
147 return false;
150 // if the installed and required version are compatible, return an empty array
151 if ( $this->versionParser->parseConstraints( $constraint )
152 ->matches( $this->coreVersion ) ) {
153 return false;
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 bool|string false for no errors, or a string message
169 private function handleExtensionDependency( $dependencyName, $constraint, $checkedExt ) {
170 // Check if the dependency is even installed
171 if ( !isset( $this->loaded[$dependencyName] ) ) {
172 return "{$checkedExt} requires {$dependencyName} to be installed.";
174 // Check if the dependency has specified a version
175 if ( !isset( $this->loaded[$dependencyName]['version'] ) ) {
176 // If we depend upon any version, and none is set, that's fine.
177 if ( $constraint === '*' ) {
178 wfDebug( "{$dependencyName} does not expose it's version, but {$checkedExt}
179 mentions it with constraint '*'. Assume it's ok so." );
180 return false;
181 } else {
182 // Otherwise, mark it as incompatible.
183 return "{$dependencyName} does not expose it's version, but {$checkedExt}
184 requires: {$constraint}.";
186 } else {
187 // Try to get a constraint for the dependency version
188 try {
189 $installedVersion = new Constraint(
190 '==',
191 $this->versionParser->normalize( $this->loaded[$dependencyName]['version'] )
193 } catch ( UnexpectedValueException $e ) {
194 // Non-parsable version, output an error message that the version
195 // string is invalid
196 return "$dependencyName does not have a valid version string.";
198 // Check if the constraint actually matches...
199 if (
200 !$this->versionParser->parseConstraints( $constraint )->matches( $installedVersion )
202 return "{$checkedExt} is not compatible with the current "
203 . "installed version of {$dependencyName} "
204 . "({$this->loaded[$dependencyName]['version']}), "
205 . "it requires: " . $constraint . '.';
209 return false;