MediaWiki REL1_31
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 $loaded = [];
42
47
51 public function __construct( $coreVersion ) {
52 $this->versionParser = new VersionParser();
54 }
55
62 public function setLoadedExtensionsAndSkins( array $credits ) {
63 $this->loaded = $credits;
64
65 return $this;
66 }
67
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
105 public function checkArray( array $extDependencies ) {
106 $errors = [];
107 foreach ( $extDependencies as $extension => $dependencies ) {
108 foreach ( $dependencies as $dependencyType => $values ) {
109 switch ( $dependencyType ) {
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
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
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}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
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.
checkArray(array $extDependencies)
Check all given dependencies if they are compatible with the named installed extensions in the $credi...
array $loaded
Loaded extensions.
VersionParser $versionParser
handleMediaWikiDependency( $constraint, $checkedExt)
Handle a dependency to MediaWiki core.
__construct( $coreVersion)
setCoreVersion( $coreVersion)
Set MediaWiki core version.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
the array() calling protocol came about after MediaWiki 1.4rc1.
returning false will NOT prevent logging $e
Definition hooks.txt:2176
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37