MediaWiki  1.29.1
VersionChecker.php
Go to the documentation of this file.
1 <?php
2 
23 use Composer\Semver\VersionParser;
24 use Composer\Semver\Constraint\Constraint;
25 
36  private $coreVersion = false;
37 
41  private $loaded = [];
42 
46  private $versionParser;
47 
51  public function __construct( $coreVersion ) {
52  $this->versionParser = new VersionParser();
53  $this->setCoreVersion( $coreVersion );
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[] = $mwError;
114  }
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;
122  }
123  }
124  break;
125  default:
126  throw new UnexpectedValueException( 'Dependency type ' . $dependencyType .
127  ' unknown in ' . $extension );
128  }
129  }
130  }
131 
132  return $errors;
133  }
134 
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;
148  }
149 
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;
154  }
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.";
159  }
160 
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.";
173  }
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}.";
185  }
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'] )
192  );
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.";
197  }
198  // Check if the constraint actually matches...
199  if (
200  !$this->versionParser->parseConstraints( $constraint )->matches( $installedVersion )
201  ) {
202  return "{$checkedExt} is not compatible with the current "
203  . "installed version of {$dependencyName} "
204  . "({$this->loaded[$dependencyName]['version']}), "
205  . "it requires: " . $constraint . '.';
206  }
207  }
208 
209  return false;
210  }
211 }
VersionChecker\handleMediaWikiDependency
handleMediaWikiDependency( $constraint, $checkedExt)
Handle a dependency to MediaWiki core.
Definition: VersionChecker.php:144
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
VersionChecker\handleExtensionDependency
handleExtensionDependency( $dependencyName, $constraint, $checkedExt)
Handle a dependency to another extension.
Definition: VersionChecker.php:169
php
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:35
VersionChecker\setCoreVersion
setCoreVersion( $coreVersion)
Set MediaWiki core version.
Definition: VersionChecker.php:73
VersionChecker\__construct
__construct( $coreVersion)
Definition: VersionChecker.php:51
VersionChecker\checkArray
checkArray(array $extDependencies)
Check all given dependencies if they are compatible with the named installed extensions in the $credi...
Definition: VersionChecker.php:105
VersionChecker\$loaded
array $loaded
Loaded extensions.
Definition: VersionChecker.php:41
VersionChecker\$coreVersion
Constraint bool $coreVersion
representing $wgVersion
Definition: VersionChecker.php:36
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:999
ExtensionRegistry\MEDIAWIKI_CORE
const MEDIAWIKI_CORE
"requires" key that applies to MediaWiki core/$wgVersion
Definition: ExtensionRegistry.php:19
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2122
VersionChecker\$versionParser
VersionParser $versionParser
Definition: VersionChecker.php:46
VersionChecker
Provides functions to check a set of extensions with dependencies against a set of loaded extensions ...
Definition: VersionChecker.php:32
VersionChecker\setLoadedExtensionsAndSkins
setLoadedExtensionsAndSkins(array $credits)
Set an array with credits of all loaded extensions and skins.
Definition: VersionChecker.php:62
as
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
Definition: distributors.txt:9
array
the array() calling protocol came about after MediaWiki 1.4rc1.