MediaWiki 1.43.1
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
51 public function __construct( callable $missingDepCallback ) {
52 $this->missingDepCallback = $missingDepCallback;
53 }
54
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
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 ) ) {
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
113 ) {
115 "$path is using a non-supported schema version"
116 );
117 }
118
119 $extraErrors = [];
120 // Check if it's a string, if not, schema validation will display an error
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 // All good.
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
167class_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.