registration: Use the correct key for skin dependencies
[lhc/web/wiklou.git] / includes / registration / VersionChecker.php
1 <?php
2
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.
8 *
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.
13 *
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
18 *
19 * @author Legoktm
20 * @author Florian Schmidt
21 */
22
23 use Composer\Semver\VersionParser;
24 use Composer\Semver\Constraint\Constraint;
25
26 /**
27 * Provides functions to check a set of extensions with dependencies against
28 * a set of loaded extensions and given version information.
29 *
30 * @since 1.29
31 */
32 class VersionChecker {
33 /**
34 * @var Constraint|bool representing $wgVersion
35 */
36 private $coreVersion = false;
37
38 /**
39 * @var array Loaded extensions
40 */
41 private $loaded = [];
42
43 /**
44 * @var VersionParser
45 */
46 private $versionParser;
47
48 /**
49 * @param string $coreVersion Current version of core
50 */
51 public function __construct( $coreVersion ) {
52 $this->versionParser = new VersionParser();
53 $this->setCoreVersion( $coreVersion );
54 }
55
56 /**
57 * Set an array with credits of all loaded extensions and skins.
58 *
59 * @param array $credits An array of installed extensions with credits of them
60 * @return VersionChecker $this
61 */
62 public function setLoadedExtensionsAndSkins( array $credits ) {
63 $this->loaded = $credits;
64
65 return $this;
66 }
67
68 /**
69 * Set MediaWiki core version.
70 *
71 * @param string $coreVersion Current version of core
72 */
73 private function setCoreVersion( $coreVersion ) {
74 try {
75 $this->coreVersion = new Constraint(
76 '==',
77 $this->versionParser->normalize( $coreVersion )
78 );
79 $this->coreVersion->setPrettyString( $coreVersion );
80 } catch ( UnexpectedValueException $e ) {
81 // Non-parsable version, don't fatal.
82 }
83 }
84
85 /**
86 * Check all given dependencies if they are compatible with the named
87 * installed extensions in the $credits array.
88 *
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 * }
100 * }
101 *
102 * @param array $extDependencies All extensions that depend on other ones
103 * @return array
104 */
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[] = [
114 'msg' => $mwError,
115 'type' => 'incompatible-core',
116 ];
117 }
118 break;
119 case 'extensions':
120 case 'skins':
121 foreach ( $values as $dependency => $constraint ) {
122 $extError = $this->handleExtensionDependency(
123 $dependency, $constraint, $extension, $dependencyType
124 );
125 if ( $extError !== false ) {
126 $errors[] = $extError;
127 }
128 }
129 break;
130 default:
131 throw new UnexpectedValueException( 'Dependency type ' . $dependencyType .
132 ' unknown in ' . $extension );
133 }
134 }
135 }
136
137 return $errors;
138 }
139
140 /**
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.
144 *
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
148 */
149 private function handleMediaWikiDependency( $constraint, $checkedExt ) {
150 if ( $this->coreVersion === false ) {
151 // Couldn't parse the core version, so we can't check anything
152 return false;
153 }
154
155 // if the installed and required version are compatible, return an empty array
156 if ( $this->versionParser->parseConstraints( $constraint )
157 ->matches( $this->coreVersion ) ) {
158 return false;
159 }
160 // otherwise mark this as incompatible.
161 return "{$checkedExt} is not compatible with the current "
162 . "MediaWiki core (version {$this->coreVersion->getPrettyString()}), it requires: "
163 . "$constraint.";
164 }
165
166 /**
167 * Handle a dependency to another extension.
168 *
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
174 */
175 private function handleExtensionDependency( $dependencyName, $constraint, $checkedExt,
176 $type
177 ) {
178 // Check if the dependency is even installed
179 if ( !isset( $this->loaded[$dependencyName] ) ) {
180 return [
181 'msg' => "{$checkedExt} requires {$dependencyName} to be installed.",
182 'type' => "missing-$type",
183 'missing' => $dependencyName,
184 ];
185 }
186 // Check if the dependency has specified a version
187 if ( !isset( $this->loaded[$dependencyName]['version'] ) ) {
188 // If we depend upon any version, and none is set, that's fine.
189 if ( $constraint === '*' ) {
190 wfDebug( "{$dependencyName} does not expose its version, but {$checkedExt}"
191 . " mentions it with constraint '*'. Assume it's ok so." );
192 return false;
193 } else {
194 // Otherwise, mark it as incompatible.
195 $msg = "{$dependencyName} does not expose its version, but {$checkedExt}"
196 . " requires: {$constraint}.";
197 return [
198 'msg' => $msg,
199 'type' => "incompatible-$type",
200 'incompatible' => $checkedExt,
201 ];
202 }
203 } else {
204 // Try to get a constraint for the dependency version
205 try {
206 $installedVersion = new Constraint(
207 '==',
208 $this->versionParser->normalize( $this->loaded[$dependencyName]['version'] )
209 );
210 } catch ( UnexpectedValueException $e ) {
211 // Non-parsable version, output an error message that the version
212 // string is invalid
213 return [
214 'msg' => "$dependencyName does not have a valid version string.",
215 'type' => 'invalid-version',
216 ];
217 }
218 // Check if the constraint actually matches...
219 if (
220 !$this->versionParser->parseConstraints( $constraint )->matches( $installedVersion )
221 ) {
222 $msg = "{$checkedExt} is not compatible with the current "
223 . "installed version of {$dependencyName} "
224 . "({$this->loaded[$dependencyName]['version']}), "
225 . "it requires: " . $constraint . '.';
226 return [
227 'msg' => $msg,
228 'type' => "incompatible-$type",
229 'incompatible' => $checkedExt,
230 ];
231 }
232 }
233
234 return false;
235 }
236 }