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