MediaWiki REL1_32
VersionChecker.php
Go to the documentation of this file.
1<?php
2
23use Composer\Semver\VersionParser;
24use Composer\Semver\Constraint\Constraint;
25
36 private $coreVersion = false;
37
41 private $phpVersion = false;
42
46 private $phpExtensions = [];
47
51 private $loaded = [];
52
57
64 $this->versionParser = new VersionParser();
66 $this->setPhpVersion( $phpVersion );
67 $this->phpExtensions = $phpExtensions;
68 }
69
76 public function setLoadedExtensionsAndSkins( array $credits ) {
77 $this->loaded = $credits;
78
79 return $this;
80 }
81
87 private function setCoreVersion( $coreVersion ) {
88 try {
89 $this->coreVersion = new Constraint(
90 '==',
91 $this->versionParser->normalize( $coreVersion )
92 );
93 $this->coreVersion->setPrettyString( $coreVersion );
94 } catch ( UnexpectedValueException $e ) {
95 // Non-parsable version, don't fatal.
96 }
97 }
98
105 private function setPhpVersion( $phpVersion ) {
106 // normalize to make this throw an exception if the version is invalid
107 $this->phpVersion = new Constraint(
108 '==',
109 $this->versionParser->normalize( $phpVersion )
110 );
111 $this->phpVersion->setPrettyString( $phpVersion );
112 }
113
138 public function checkArray( array $extDependencies ) {
139 $errors = [];
140 foreach ( $extDependencies as $extension => $dependencies ) {
141 foreach ( $dependencies as $dependencyType => $values ) {
142 switch ( $dependencyType ) {
144 $mwError = $this->handleDependency(
145 $this->coreVersion,
146 $values,
147 $extension
148 );
149 if ( $mwError !== false ) {
150 $errors[] = [
151 'msg' =>
152 "{$extension} is not compatible with the current MediaWiki "
153 . "core (version {$this->coreVersion->getPrettyString()}), "
154 . "it requires: $values."
155 ,
156 'type' => 'incompatible-core',
157 ];
158 }
159 break;
160 case 'platform':
161 foreach ( $values as $dependency => $constraint ) {
162 if ( $dependency === 'php' ) {
163 // PHP version
164 $phpError = $this->handleDependency(
165 $this->phpVersion,
166 $constraint,
167 $extension
168 );
169 if ( $phpError !== false ) {
170 $errors[] = [
171 'msg' =>
172 "{$extension} is not compatible with the current PHP "
173 . "version {$this->phpVersion->getPrettyString()}), "
174 . "it requires: $constraint."
175 ,
176 'type' => 'incompatible-php',
177 ];
178 }
179 } elseif ( substr( $dependency, 0, 4 ) === 'ext-' ) {
180 // PHP extensions
181 $phpExtension = substr( $dependency, 4 );
182 if ( $constraint !== '*' ) {
183 throw new UnexpectedValueException( 'Version constraints for '
184 . 'PHP extensions are not supported in ' . $extension );
185 }
186 if ( !in_array( $phpExtension, $this->phpExtensions, true ) ) {
187 $errors[] = [
188 'msg' =>
189 "{$extension} requires {$phpExtension} PHP extension "
190 . "to be installed."
191 ,
192 'type' => 'missing-phpExtension',
193 'missing' => $phpExtension,
194 ];
195 }
196 } else {
197 // add other platform dependencies here
198 throw new UnexpectedValueException( 'Dependency type ' . $dependency .
199 ' unknown in ' . $extension );
200 }
201 }
202 break;
203 case 'extensions':
204 case 'skins':
205 foreach ( $values as $dependency => $constraint ) {
206 $extError = $this->handleExtensionDependency(
207 $dependency, $constraint, $extension, $dependencyType
208 );
209 if ( $extError !== false ) {
210 $errors[] = $extError;
211 }
212 }
213 break;
214 default:
215 throw new UnexpectedValueException( 'Dependency type ' . $dependencyType .
216 ' unknown in ' . $extension );
217 }
218 }
219 }
220
221 return $errors;
222 }
223
233 private function handleDependency( $version, $constraint, $checkedExt ) {
234 if ( $version === false ) {
235 // Couldn't parse the version, so we can't check anything
236 return false;
237 }
238
239 // if the installed and required version are compatible, return an empty array
240 if ( $this->versionParser->parseConstraints( $constraint )
241 ->matches( $version ) ) {
242 return false;
243 }
244
245 return true;
246 }
247
257 private function handleExtensionDependency( $dependencyName, $constraint, $checkedExt,
258 $type
259 ) {
260 // Check if the dependency is even installed
261 if ( !isset( $this->loaded[$dependencyName] ) ) {
262 return [
263 'msg' => "{$checkedExt} requires {$dependencyName} to be installed.",
264 'type' => "missing-$type",
265 'missing' => $dependencyName,
266 ];
267 }
268 if ( $constraint === '*' ) {
269 // short-circuit since any version is OK.
270 return false;
271 }
272 // Check if the dependency has specified a version
273 if ( !isset( $this->loaded[$dependencyName]['version'] ) ) {
274 $msg = "{$dependencyName} does not expose its version, but {$checkedExt}"
275 . " requires: {$constraint}.";
276 return [
277 'msg' => $msg,
278 'type' => "incompatible-$type",
279 'incompatible' => $checkedExt,
280 ];
281 } else {
282 // Try to get a constraint for the dependency version
283 try {
284 $installedVersion = new Constraint(
285 '==',
286 $this->versionParser->normalize( $this->loaded[$dependencyName]['version'] )
287 );
288 } catch ( UnexpectedValueException $e ) {
289 // Non-parsable version, output an error message that the version
290 // string is invalid
291 return [
292 'msg' => "$dependencyName does not have a valid version string.",
293 'type' => 'invalid-version',
294 ];
295 }
296 // Check if the constraint actually matches...
297 if (
298 !$this->versionParser->parseConstraints( $constraint )->matches( $installedVersion )
299 ) {
300 $msg = "{$checkedExt} is not compatible with the current "
301 . "installed version of {$dependencyName} "
302 . "({$this->loaded[$dependencyName]['version']}), "
303 . "it requires: " . $constraint . '.';
304 return [
305 'msg' => $msg,
306 'type' => "incompatible-$type",
307 'incompatible' => $checkedExt,
308 ];
309 }
310 }
311
312 return false;
313 }
314}
const MEDIAWIKI_CORE
"requires" key that applies to MediaWiki core/$wgVersion
Provides functions to check a set of extensions with dependencies against a set of loaded extensions ...
handleExtensionDependency( $dependencyName, $constraint, $checkedExt, $type)
Handle a dependency to another extension.
Constraint bool $coreVersion
representing $wgVersion
setLoadedExtensionsAndSkins(array $credits)
Set an array with credits of all loaded extensions and skins.
handleDependency( $version, $constraint, $checkedExt)
Handle a simple dependency to MediaWiki core or PHP.
checkArray(array $extDependencies)
Check all given dependencies if they are compatible with the named installed extensions in the $credi...
Constraint bool $phpVersion
representing PHP version
array $loaded
Loaded extensions.
VersionParser $versionParser
setCoreVersion( $coreVersion)
Set MediaWiki core version.
setPhpVersion( $phpVersion)
Set PHP version.
__construct( $coreVersion, $phpVersion, array $phpExtensions)
string[] $phpExtensions
List of installed PHP extensions.
returning false will NOT prevent logging $e
Definition hooks.txt:2226
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))