X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fregistration%2FVersionChecker.php;h=586729d057f02ae53457769dca7a0ad2ea4beccf;hb=bcc4c79042a9f2399412f1d1aad27b8f389200c2;hp=9c673bc5db1aea6f03be38c541c09a596e69b2c9;hpb=43c128e4e30c7386c4d534ff2ef1f6ec84f5605f;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/registration/VersionChecker.php b/includes/registration/VersionChecker.php index 9c673bc5db..586729d057 100644 --- a/includes/registration/VersionChecker.php +++ b/includes/registration/VersionChecker.php @@ -35,6 +35,16 @@ class VersionChecker { */ private $coreVersion = false; + /** + * @var Constraint|bool representing PHP version + */ + private $phpVersion = false; + + /** + * @var string[] List of installed PHP extensions + */ + private $phpExtensions = []; + /** * @var array Loaded extensions */ @@ -47,10 +57,14 @@ class VersionChecker { /** * @param string $coreVersion Current version of core + * @param string $phpVersion Current PHP version + * @param string[] $phpExtensions List of installed PHP extensions */ - public function __construct( $coreVersion ) { + public function __construct( $coreVersion, $phpVersion, array $phpExtensions ) { $this->versionParser = new VersionParser(); $this->setCoreVersion( $coreVersion ); + $this->setPhpVersion( $phpVersion ); + $this->phpExtensions = $phpExtensions; } /** @@ -82,6 +96,21 @@ class VersionChecker { } } + /** + * Set PHP version. + * + * @param string $phpVersion Current PHP version. Must be well-formed. + * @throws UnexpectedValueException + */ + private function setPhpVersion( $phpVersion ) { + // normalize to make this throw an exception if the version is invalid + $this->phpVersion = new Constraint( + '==', + $this->versionParser->normalize( $phpVersion ) + ); + $this->phpVersion->setPrettyString( $phpVersion ); + } + /** * Check all given dependencies if they are compatible with the named * installed extensions in the $credits array. @@ -90,6 +119,10 @@ class VersionChecker { * { * 'FooBar' => { * 'MediaWiki' => '>= 1.25.0', + * 'platform': { + * 'php': '>= 7.0.0', + * 'ext-foo': '*' + * }, * 'extensions' => { * 'FooBaz' => '>= 1.25.0' * }, @@ -108,16 +141,67 @@ class VersionChecker { foreach ( $dependencies as $dependencyType => $values ) { switch ( $dependencyType ) { case ExtensionRegistry::MEDIAWIKI_CORE: - $mwError = $this->handleMediaWikiDependency( $values, $extension ); + $mwError = $this->handleDependency( + $this->coreVersion, + $values, + $extension + ); if ( $mwError !== false ) { $errors[] = [ - 'msg' => $mwError, + 'msg' => + "{$extension} is not compatible with the current MediaWiki " + . "core (version {$this->coreVersion->getPrettyString()}), " + . "it requires: $values." + , 'type' => 'incompatible-core', ]; } break; + case 'platform': + foreach ( $values as $dependency => $constraint ) { + if ( $dependency === 'php' ) { + // PHP version + $phpError = $this->handleDependency( + $this->phpVersion, + $constraint, + $extension + ); + if ( $phpError !== false ) { + $errors[] = [ + 'msg' => + "{$extension} is not compatible with the current PHP " + . "version {$this->phpVersion->getPrettyString()}), " + . "it requires: $constraint." + , + 'type' => 'incompatible-php', + ]; + } + } elseif ( substr( $dependency, 0, 4 ) === 'ext-' ) { + // PHP extensions + $phpExtension = substr( $dependency, 4 ); + if ( $constraint !== '*' ) { + throw new UnexpectedValueException( 'Version constraints for ' + . 'PHP extensions are not supported in ' . $extension ); + } + if ( !in_array( $phpExtension, $this->phpExtensions, true ) ) { + $errors[] = [ + 'msg' => + "{$extension} requires {$phpExtension} PHP extension " + . "to be installed." + , + 'type' => 'missing-phpExtension', + 'missing' => $phpExtension, + ]; + } + } else { + // add other platform dependencies here + throw new UnexpectedValueException( 'Dependency type ' . $dependency . + ' unknown in ' . $extension ); + } + } + break; case 'extensions': - case 'skin': + case 'skins': foreach ( $values as $dependency => $constraint ) { $extError = $this->handleExtensionDependency( $dependency, $constraint, $extension, $dependencyType @@ -138,29 +222,27 @@ class VersionChecker { } /** - * Handle a dependency to MediaWiki core. It will check, if a MediaWiki version constraint was - * set with self::setCoreVersion before this call (if not, it will return an empty array) and - * checks the version constraint given against it. + * Handle a simple dependency to MediaWiki core or PHP. See handleMediaWikiDependency and + * handlePhpDependency for details. * + * @param Constraint|bool $version The version installed * @param string $constraint The required version constraint for this dependency * @param string $checkedExt The Extension, which depends on this dependency - * @return bool|string false if no error, or a string with the message + * @return bool false if no error, true else */ - private function handleMediaWikiDependency( $constraint, $checkedExt ) { - if ( $this->coreVersion === false ) { - // Couldn't parse the core version, so we can't check anything + private function handleDependency( $version, $constraint, $checkedExt ) { + if ( $version === false ) { + // Couldn't parse the version, so we can't check anything return false; } // if the installed and required version are compatible, return an empty array if ( $this->versionParser->parseConstraints( $constraint ) - ->matches( $this->coreVersion ) ) { + ->matches( $version ) ) { return false; } - // otherwise mark this as incompatible. - return "{$checkedExt} is not compatible with the current " - . "MediaWiki core (version {$this->coreVersion->getPrettyString()}), it requires: " - . "$constraint."; + + return true; } /** @@ -169,7 +251,7 @@ class VersionChecker { * @param string $dependencyName The name of the dependency * @param string $constraint The required version constraint for this dependency * @param string $checkedExt The Extension, which depends on this dependency - * @param string $type Either 'extension' or 'skin' + * @param string $type Either 'extensions' or 'skins' * @return bool|array false for no errors, or an array of info */ private function handleExtensionDependency( $dependencyName, $constraint, $checkedExt, @@ -183,23 +265,19 @@ class VersionChecker { 'missing' => $dependencyName, ]; } + if ( $constraint === '*' ) { + // short-circuit since any version is OK. + return false; + } // Check if the dependency has specified a version if ( !isset( $this->loaded[$dependencyName]['version'] ) ) { - // If we depend upon any version, and none is set, that's fine. - if ( $constraint === '*' ) { - wfDebug( "{$dependencyName} does not expose its version, but {$checkedExt}" - . " mentions it with constraint '*'. Assume it's ok so." ); - return false; - } else { - // Otherwise, mark it as incompatible. - $msg = "{$dependencyName} does not expose its version, but {$checkedExt}" - . " requires: {$constraint}."; - return [ - 'msg' => $msg, - 'type' => "incompatible-$type", - 'incompatible' => $checkedExt, - ]; - } + $msg = "{$dependencyName} does not expose its version, but {$checkedExt}" + . " requires: {$constraint}."; + return [ + 'msg' => $msg, + 'type' => "incompatible-$type", + 'incompatible' => $checkedExt, + ]; } else { // Try to get a constraint for the dependency version try {