| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Registration; |
| 22 |
|
| 23 |
use Composer\Semver\Constraint\Constraint; |
| 24 |
use Composer\Semver\VersionParser; |
| 25 |
use UnexpectedValueException; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class VersionChecker { |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
private $coreVersion = false; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
private $phpVersion = false; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
private $phpExtensions; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
private $abilities; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
private $abilityErrors; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
private $loaded = []; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
private $versionParser; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public function __construct( |
| 79 |
$coreVersion, $phpVersion, array $phpExtensions, |
| 80 |
array $abilities = [], array $abilityErrors = [] |
| 81 |
) { |
| 82 |
$this->versionParser = new VersionParser(); |
| 83 |
$this->setCoreVersion( $coreVersion ); |
| 84 |
$this->setPhpVersion( $phpVersion ); |
| 85 |
$this->phpExtensions = $phpExtensions; |
| 86 |
$this->abilities = $abilities; |
| 87 |
$this->abilityErrors = $abilityErrors; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function setLoadedExtensionsAndSkins( array $credits ) { |
| 98 |
$this->loaded = $credits; |
| 99 |
|
| 100 |
return $this; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
private function setCoreVersion( $coreVersion ) { |
| 109 |
try { |
| 110 |
$this->coreVersion = new Constraint( |
| 111 |
'==', |
| 112 |
$this->versionParser->normalize( $coreVersion ) |
| 113 |
); |
| 114 |
$this->coreVersion->setPrettyString( $coreVersion ); |
| 115 |
} catch ( UnexpectedValueException $e ) { |
| 116 |
|
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
private function setPhpVersion( $phpVersion ) { |
| 126 |
|
| 127 |
$this->phpVersion = new Constraint( |
| 128 |
'==', |
| 129 |
$this->versionParser->normalize( $phpVersion ) |
| 130 |
); |
| 131 |
$this->phpVersion->setPrettyString( $phpVersion ); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public function checkArray( array $extDependencies ) { |
| 161 |
$errors = []; |
| 162 |
foreach ( $extDependencies as $extension => $dependencies ) { |
| 163 |
foreach ( $dependencies as $dependencyType => $values ) { |
| 164 |
switch ( $dependencyType ) { |
| 165 |
case ExtensionRegistry::MEDIAWIKI_CORE: |
| 166 |
$mwError = $this->handleDependency( |
| 167 |
$this->coreVersion, |
| 168 |
$values |
| 169 |
); |
| 170 |
if ( $mwError !== false ) { |
| 171 |
$errors[] = [ |
| 172 |
'msg' => |
| 173 |
"{$extension} is not compatible with the current MediaWiki " |
| 174 |
. "core (version {$this->coreVersion->getPrettyString()}), " |
| 175 |
. "it requires: $values.", |
| 176 |
'type' => 'incompatible-core', |
| 177 |
]; |
| 178 |
} |
| 179 |
break; |
| 180 |
case 'platform': |
| 181 |
foreach ( $values as $dependency => $constraint ) { |
| 182 |
if ( $dependency === 'php' ) { |
| 183 |
|
| 184 |
$phpError = $this->handleDependency( |
| 185 |
$this->phpVersion, |
| 186 |
$constraint |
| 187 |
); |
| 188 |
if ( $phpError !== false ) { |
| 189 |
$errors[] = [ |
| 190 |
'msg' => |
| 191 |
"{$extension} is not compatible with the current PHP " |
| 192 |
. "version {$this->phpVersion->getPrettyString()}), " |
| 193 |
. "it requires: $constraint.", |
| 194 |
'type' => 'incompatible-php', |
| 195 |
]; |
| 196 |
} |
| 197 |
} elseif ( substr( $dependency, 0, 4 ) === 'ext-' ) { |
| 198 |
|
| 199 |
$phpExtension = substr( $dependency, 4 ); |
| 200 |
if ( $constraint !== '*' ) { |
| 201 |
throw new UnexpectedValueException( 'Version constraints for ' |
| 202 |
. 'PHP extensions are not supported in ' . $extension ); |
| 203 |
} |
| 204 |
if ( !in_array( $phpExtension, $this->phpExtensions, true ) ) { |
| 205 |
$errors[] = [ |
| 206 |
'msg' => |
| 207 |
"{$extension} requires {$phpExtension} PHP extension " |
| 208 |
. "to be installed.", |
| 209 |
'type' => 'missing-phpExtension', |
| 210 |
'missing' => $phpExtension, |
| 211 |
]; |
| 212 |
} |
| 213 |
} elseif ( substr( $dependency, 0, 8 ) === 'ability-' ) { |
| 214 |
|
| 215 |
$ability = substr( $dependency, 8 ); |
| 216 |
if ( !isset( $this->abilities[$ability] ) ) { |
| 217 |
throw new UnexpectedValueException( 'Dependency type ' |
| 218 |
. $dependency . ' unknown in ' . $extension ); |
| 219 |
} |
| 220 |
if ( !is_bool( $constraint ) ) { |
| 221 |
throw new UnexpectedValueException( 'Only booleans are ' |
| 222 |
. 'allowed to to indicate the presence of abilities ' |
| 223 |
. 'in ' . $extension ); |
| 224 |
} |
| 225 |
|
| 226 |
if ( $constraint && |
| 227 |
$this->abilities[$ability] !== true |
| 228 |
) { |
| 229 |
|
| 230 |
$customMessage = ''; |
| 231 |
if ( isset( $this->abilityErrors[$ability] ) ) { |
| 232 |
$customMessage = ': ' . $this->abilityErrors[$ability]; |
| 233 |
} |
| 234 |
|
| 235 |
$errors[] = [ |
| 236 |
'msg' => |
| 237 |
"{$extension} requires \"{$ability}\" ability" |
| 238 |
. $customMessage, |
| 239 |
'type' => 'missing-ability', |
| 240 |
'missing' => $ability, |
| 241 |
]; |
| 242 |
} |
| 243 |
} else { |
| 244 |
|
| 245 |
throw new UnexpectedValueException( 'Dependency type ' . $dependency . |
| 246 |
' unknown in ' . $extension ); |
| 247 |
} |
| 248 |
} |
| 249 |
break; |
| 250 |
case 'extensions': |
| 251 |
case 'skins': |
| 252 |
foreach ( $values as $dependency => $constraint ) { |
| 253 |
$extError = $this->handleExtensionDependency( |
| 254 |
$dependency, $constraint, $extension, $dependencyType |
| 255 |
); |
| 256 |
if ( $extError !== false ) { |
| 257 |
$errors[] = $extError; |
| 258 |
} |
| 259 |
} |
| 260 |
break; |
| 261 |
default: |
| 262 |
throw new UnexpectedValueException( 'Dependency type ' . $dependencyType . |
| 263 |
' unknown in ' . $extension ); |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
return $errors; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
private function handleDependency( $version, $constraint ) { |
| 281 |
if ( $version === false ) { |
| 282 |
|
| 283 |
return false; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
if ( $this->versionParser->parseConstraints( $constraint ) |
| 288 |
->matches( $version ) ) { |
| 289 |
return false; |
| 290 |
} |
| 291 |
|
| 292 |
return true; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
private function handleExtensionDependency( $dependencyName, $constraint, $checkedExt, |
| 306 |
$type |
| 307 |
) { |
| 308 |
|
| 309 |
if ( !isset( $this->loaded[$dependencyName] ) ) { |
| 310 |
return [ |
| 311 |
'msg' => "{$checkedExt} requires {$dependencyName} to be installed.", |
| 312 |
'type' => "missing-$type", |
| 313 |
'missing' => $dependencyName, |
| 314 |
]; |
| 315 |
} |
| 316 |
if ( $constraint === '*' ) { |
| 317 |
|
| 318 |
return false; |
| 319 |
} |
| 320 |
|
| 321 |
if ( !isset( $this->loaded[$dependencyName]['version'] ) ) { |
| 322 |
$msg = "{$dependencyName} does not expose its version, but {$checkedExt}" |
| 323 |
. " requires: {$constraint}."; |
| 324 |
return [ |
| 325 |
'msg' => $msg, |
| 326 |
'type' => "incompatible-$type", |
| 327 |
'incompatible' => $checkedExt, |
| 328 |
]; |
| 329 |
} else { |
| 330 |
|
| 331 |
try { |
| 332 |
$installedVersion = new Constraint( |
| 333 |
'==', |
| 334 |
$this->versionParser->normalize( $this->loaded[$dependencyName]['version'] ) |
| 335 |
); |
| 336 |
} catch ( UnexpectedValueException $e ) { |
| 337 |
|
| 338 |
|
| 339 |
return [ |
| 340 |
'msg' => "$dependencyName does not have a valid version string.", |
| 341 |
'type' => 'invalid-version', |
| 342 |
]; |
| 343 |
} |
| 344 |
|
| 345 |
if ( |
| 346 |
!$this->versionParser->parseConstraints( $constraint )->matches( $installedVersion ) |
| 347 |
) { |
| 348 |
$msg = "{$checkedExt} is not compatible with the current " |
| 349 |
. "installed version of {$dependencyName} " |
| 350 |
. "({$this->loaded[$dependencyName]['version']}), " |
| 351 |
. "it requires: " . $constraint . '.'; |
| 352 |
return [ |
| 353 |
'msg' => $msg, |
| 354 |
'type' => "incompatible-$type", |
| 355 |
'incompatible' => $checkedExt, |
| 356 |
]; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
return false; |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
class_alias( VersionChecker::class, 'VersionChecker' ); |