MediaWiki master
ExtensionJsonValidator.php
Go to the documentation of this file.
1<?php
8
9use Composer\Spdx\SpdxLicenses;
10use JsonSchema\Validator;
11use Seld\JsonLint\DuplicateKeyException;
12use Seld\JsonLint\JsonParser;
13use Seld\JsonLint\ParsingException;
14
28
32 private $missingDepCallback;
33
34 public function __construct( callable $missingDepCallback ) {
35 $this->missingDepCallback = $missingDepCallback;
36 }
37
42 public function checkDependencies() {
43 if ( !class_exists( Validator::class ) ) {
44 ( $this->missingDepCallback )(
45 'The JsonSchema library cannot be found, please install it through composer.'
46 );
47
48 return false;
49 }
50
51 if ( !class_exists( SpdxLicenses::class ) ) {
52 ( $this->missingDepCallback )(
53 'The spdx-licenses library cannot be found, please install it through composer.'
54 );
55
56 return false;
57 }
58
59 if ( !class_exists( JsonParser::class ) ) {
60 ( $this->missingDepCallback )(
61 'The JSON lint library cannot be found, please install it through composer.'
62 );
63 }
64
65 return true;
66 }
67
74 public function validate( $path ) {
75 $contents = file_get_contents( $path );
76 $jsonParser = new JsonParser();
77 try {
78 $data = $jsonParser->parse( $contents, JsonParser::DETECT_KEY_CONFLICTS );
79 } catch ( ParsingException $e ) {
80 if ( $e instanceof DuplicateKeyException ) {
81 throw new ExtensionJsonValidationError( $e->getMessage() );
82 }
83 throw new ExtensionJsonValidationError( "$path is not valid JSON" );
84 }
85
86 if ( !isset( $data->manifest_version ) ) {
88 "$path does not have manifest_version set." );
89 }
90
91 $version = $data->manifest_version;
92 $schemaPath = __DIR__ . "/../../docs/extension.schema.v$version.json";
93
96 ) {
98 "$path is using a non-supported schema version"
99 );
100 }
101
102 $extraErrors = [];
103 // Check if it's a string, if not, schema validation will display an error
104 if ( isset( $data->{'license-name'} ) && is_string( $data->{'license-name'} ) ) {
105 $licenses = new SpdxLicenses();
106 $valid = $licenses->validate( $data->{'license-name'} );
107 if ( !$valid ) {
108 $extraErrors[] = '[license-name] Invalid SPDX license identifier, '
109 . 'see <https://spdx.org/licenses/>';
110 }
111 }
112 if ( isset( $data->url ) && is_string( $data->url ) ) {
113 $parsed = parse_url( $data->url );
114 $mwoUrl = false;
115 if ( !$parsed || !isset( $parsed['host'] ) || !isset( $parsed['scheme'] ) ) {
116 $extraErrors[] = '[url] URL cannot be parsed';
117 } else {
118 if ( $parsed['host'] === 'www.mediawiki.org' ) {
119 $mwoUrl = true;
120 } elseif ( $parsed['host'] === 'mediawiki.org' ) {
121 $mwoUrl = true;
122 $extraErrors[] = '[url] Should use www.mediawiki.org domain';
123 }
124
125 if ( $mwoUrl && $parsed['scheme'] !== 'https' ) {
126 $extraErrors[] = '[url] Should use HTTPS for www.mediawiki.org URLs';
127 }
128 }
129 }
130
131 $validator = new Validator;
132 $validator->check( $data, (object)[ '$ref' => 'file://' . $schemaPath ] );
133 if ( $validator->isValid() && !$extraErrors ) {
134 // All good.
135 return true;
136 }
137
138 $out = "$path did not pass validation.\n";
139 foreach ( $validator->getErrors() as $error ) {
140 $out .= "[{$error['property']}] {$error['message']}\n";
141 }
142 if ( $extraErrors ) {
143 $out .= implode( "\n", $extraErrors ) . "\n";
144 }
145 throw new ExtensionJsonValidationError( $out );
146 }
147}
148
150class_alias( ExtensionJsonValidator::class, 'ExtensionJsonValidator' );
Validate extension.json files against their JSON schema.
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.