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