| 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\Spdx\SpdxLicenses; |
| 24 |
use JsonSchema\Validator; |
| 25 |
use Seld\JsonLint\DuplicateKeyException; |
| 26 |
use Seld\JsonLint\JsonParser; |
| 27 |
use Seld\JsonLint\ParsingException; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class ExtensionJsonValidator { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private $missingDepCallback; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function __construct( callable $missingDepCallback ) { |
| 52 |
$this->missingDepCallback = $missingDepCallback; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public function checkDependencies() { |
| 60 |
if ( !class_exists( Validator::class ) ) { |
| 61 |
call_user_func( $this->missingDepCallback, |
| 62 |
'The JsonSchema library cannot be found, please install it through composer.' |
| 63 |
); |
| 64 |
|
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
if ( !class_exists( SpdxLicenses::class ) ) { |
| 69 |
call_user_func( $this->missingDepCallback, |
| 70 |
'The spdx-licenses library cannot be found, please install it through composer.' |
| 71 |
); |
| 72 |
|
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
if ( !class_exists( JsonParser::class ) ) { |
| 77 |
call_user_func( $this->missingDepCallback, |
| 78 |
'The JSON lint library cannot be found, please install it through composer.' |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
return true; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public function validate( $path ) { |
| 92 |
$contents = file_get_contents( $path ); |
| 93 |
$jsonParser = new JsonParser(); |
| 94 |
try { |
| 95 |
$data = $jsonParser->parse( $contents, JsonParser::DETECT_KEY_CONFLICTS ); |
| 96 |
} catch ( ParsingException $e ) { |
| 97 |
if ( $e instanceof DuplicateKeyException ) { |
| 98 |
throw new ExtensionJsonValidationError( $e->getMessage() ); |
| 99 |
} |
| 100 |
throw new ExtensionJsonValidationError( "$path is not valid JSON" ); |
| 101 |
} |
| 102 |
|
| 103 |
if ( !isset( $data->manifest_version ) ) { |
| 104 |
throw new ExtensionJsonValidationError( |
| 105 |
"$path does not have manifest_version set." ); |
| 106 |
} |
| 107 |
|
| 108 |
$version = $data->manifest_version; |
| 109 |
$schemaPath = __DIR__ . "/../../docs/extension.schema.v$version.json"; |
| 110 |
|
| 111 |
if ( $version < ExtensionRegistry::OLDEST_MANIFEST_VERSION || |
| 112 |
$version > ExtensionRegistry::MANIFEST_VERSION |
| 113 |
) { |
| 114 |
throw new ExtensionJsonValidationError( |
| 115 |
"$path is using a non-supported schema version" |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
$extraErrors = []; |
| 120 |
|
| 121 |
if ( isset( $data->{'license-name'} ) && is_string( $data->{'license-name'} ) ) { |
| 122 |
$licenses = new SpdxLicenses(); |
| 123 |
$valid = $licenses->validate( $data->{'license-name'} ); |
| 124 |
if ( !$valid ) { |
| 125 |
$extraErrors[] = '[license-name] Invalid SPDX license identifier, ' |
| 126 |
. 'see <https://spdx.org/licenses/>'; |
| 127 |
} |
| 128 |
} |
| 129 |
if ( isset( $data->url ) && is_string( $data->url ) ) { |
| 130 |
$parsed = parse_url( $data->url ); |
| 131 |
$mwoUrl = false; |
| 132 |
if ( !$parsed || !isset( $parsed['host'] ) || !isset( $parsed['scheme'] ) ) { |
| 133 |
$extraErrors[] = '[url] URL cannot be parsed'; |
| 134 |
} else { |
| 135 |
if ( $parsed['host'] === 'www.mediawiki.org' ) { |
| 136 |
$mwoUrl = true; |
| 137 |
} elseif ( $parsed['host'] === 'mediawiki.org' ) { |
| 138 |
$mwoUrl = true; |
| 139 |
$extraErrors[] = '[url] Should use www.mediawiki.org domain'; |
| 140 |
} |
| 141 |
|
| 142 |
if ( $mwoUrl && $parsed['scheme'] !== 'https' ) { |
| 143 |
$extraErrors[] = '[url] Should use HTTPS for www.mediawiki.org URLs'; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
$validator = new Validator; |
| 149 |
$validator->check( $data, (object)[ '$ref' => 'file://' . $schemaPath ] ); |
| 150 |
if ( $validator->isValid() && !$extraErrors ) { |
| 151 |
|
| 152 |
return true; |
| 153 |
} |
| 154 |
|
| 155 |
$out = "$path did not pass validation.\n"; |
| 156 |
foreach ( $validator->getErrors() as $error ) { |
| 157 |
$out .= "[{$error['property']}] {$error['message']}\n"; |
| 158 |
} |
| 159 |
if ( $extraErrors ) { |
| 160 |
$out .= implode( "\n", $extraErrors ) . "\n"; |
| 161 |
} |
| 162 |
throw new ExtensionJsonValidationError( $out ); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
class_alias( ExtensionJsonValidator::class, 'ExtensionJsonValidator' ); |