21 use Composer\Spdx\SpdxLicenses;
22 use JsonSchema\Validator;
23 use Seld\JsonLint\JsonParser;
24 use Seld\JsonLint\ParsingException;
43 private $missingDepCallback;
49 $this->missingDepCallback = $missingDepCallback;
57 if ( !class_exists( Validator::class ) ) {
58 call_user_func( $this->missingDepCallback,
59 'The JsonSchema library cannot be found, please install it through composer.'
64 if ( !class_exists( SpdxLicenses::class ) ) {
65 call_user_func( $this->missingDepCallback,
66 'The spdx-licenses library cannot be found, please install it through composer.'
71 if ( !class_exists( JsonParser::class ) ) {
72 call_user_func( $this->missingDepCallback,
73 'The JSON lint library cannot be found, please install it through composer.'
86 $contents = file_get_contents(
$path );
87 $jsonParser =
new JsonParser();
89 $data = $jsonParser->parse( $contents, JsonParser::DETECT_KEY_CONFLICTS );
90 }
catch ( ParsingException $e ) {
91 if ( $e instanceof \Seld\JsonLint\DuplicateKeyException ) {
97 if ( !isset( $data->manifest_version ) ) {
99 "$path does not have manifest_version set." );
102 $version = $data->manifest_version;
103 $schemaPath = __DIR__ .
"/../../docs/extension.schema.v$version.json";
109 "$path is using a non-supported schema version"
115 if ( isset( $data->{
'license-name'} ) && is_string( $data->{
'license-name'} ) ) {
116 $licenses =
new SpdxLicenses();
117 $valid = $licenses->validate( $data->{
'license-name'} );
119 $extraErrors[] =
'[license-name] Invalid SPDX license identifier, '
120 .
'see <https://spdx.org/licenses/>';
123 if ( isset( $data->url ) && is_string( $data->url ) ) {
124 $parsed = parse_url( $data->url );
126 if ( !$parsed || !isset( $parsed[
'host'] ) || !isset( $parsed[
'scheme'] ) ) {
127 $extraErrors[] =
'[url] URL cannot be parsed';
129 if ( $parsed[
'host'] ===
'www.mediawiki.org' ) {
131 } elseif ( $parsed[
'host'] ===
'mediawiki.org' ) {
133 $extraErrors[] =
'[url] Should use www.mediawiki.org domain';
136 if ( $mwoUrl && $parsed[
'scheme'] !==
'https' ) {
137 $extraErrors[] =
'[url] Should use HTTPS for www.mediawiki.org URLs';
142 $validator =
new Validator;
143 $validator->check( $data, (
object)[
'$ref' =>
'file://' . $schemaPath ] );
144 if ( $validator->isValid() && !$extraErrors ) {
149 $out =
"$path did not pass validation.\n";
150 foreach ( $validator->getErrors() as $error ) {
151 $out .=
"[{$error['property']}] {$error['message']}\n";
153 if ( $extraErrors ) {
154 $out .= implode(
"\n", $extraErrors ) .
"\n";
Validate extension.json files against their JSON schema.
__construct(callable $missingDepCallback)
const MANIFEST_VERSION
Version of the highest supported manifest version Note: Update MANIFEST_VERSION_MW_VERSION when chang...
const OLDEST_MANIFEST_VERSION
Version of the oldest supported manifest version.