4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
22 use Composer\Spdx\SpdxLicenses
;
23 use JsonSchema\Validator
;
24 use Seld\JsonLint\JsonParser
;
25 use Seld\JsonLint\ParsingException
;
28 * Validate extension.json files against their JSON schema.
30 * This is used for static validation from the command-line via
31 * validateRegistrationFile.php, and the PHPUnit structure test suite
32 * (ExtensionJsonValidationTest).
34 * The files are normally read by the ExtensionRegistry
35 * and ExtensionProcessor classes.
39 class ExtensionJsonValidator
{
44 private $missingDepCallback;
47 * @param callable $missingDepCallback
49 public function __construct( callable
$missingDepCallback ) {
50 $this->missingDepCallback
= $missingDepCallback;
57 public function checkDependencies() {
58 if ( !class_exists( Validator
::class ) ) {
59 call_user_func( $this->missingDepCallback
,
60 'The JsonSchema library cannot be found, please install it through composer.'
65 if ( !class_exists( SpdxLicenses
::class ) ) {
66 call_user_func( $this->missingDepCallback
,
67 'The spdx-licenses library cannot be found, please install it through composer.'
72 if ( !class_exists( JsonParser
::class ) ) {
73 call_user_func( $this->missingDepCallback
,
74 'The JSON lint library cannot be found, please install it through composer.'
82 * @param string $path file to validate
83 * @return bool true if passes validation
84 * @throws ExtensionJsonValidationError on any failure
86 public function validate( $path ) {
87 $contents = file_get_contents( $path );
88 $jsonParser = new JsonParser();
90 $data = $jsonParser->parse( $contents, JsonParser
::DETECT_KEY_CONFLICTS
);
91 } catch ( ParsingException
$e ) {
92 if ( $e instanceof \Seld\JsonLint\DuplicateKeyException
) {
93 throw new ExtensionJsonValidationError( $e->getMessage() );
95 throw new ExtensionJsonValidationError( "$path is not valid JSON" );
98 if ( !isset( $data->manifest_version
) ) {
99 throw new ExtensionJsonValidationError(
100 "$path does not have manifest_version set." );
103 $version = $data->manifest_version
;
104 $schemaPath = __DIR__
. "/../../docs/extension.schema.v$version.json";
107 if ( $version < ExtensionRegistry
::OLDEST_MANIFEST_VERSION
) {
108 throw new ExtensionJsonValidationError(
109 "$path is using a non-supported schema version"
113 if ( $version > ExtensionRegistry
::MANIFEST_VERSION
) {
114 throw new ExtensionJsonValidationError(
115 "$path is using a non-supported schema version"
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'} );
125 $extraErrors[] = '[license-name] Invalid SPDX license identifier, '
126 . 'see <https://spdx.org/licenses/>';
129 if ( isset( $data->url
) && is_string( $data->url
) ) {
130 $parsed = wfParseUrl( $data->url
);
132 if ( $parsed['host'] === 'www.mediawiki.org' ) {
134 } elseif ( $parsed['host'] === 'mediawiki.org' ) {
136 $extraErrors[] = '[url] Should use www.mediawiki.org domain';
139 if ( $mwoUrl && $parsed['scheme'] !== 'https' ) {
140 $extraErrors[] = '[url] Should use HTTPS for www.mediawiki.org URLs';
145 if ( isset( $data->ParserTestFiles
) ) {
146 // phpcs:ignore Generic.Files.LineLength.TooLong
147 $extraErrors[] = '[ParserTestFiles] DEPRECATED: see <https://www.mediawiki.org/wiki/Manual:Extension.json/Schema#ParserTestFiles>';
150 $validator = new Validator
;
151 $validator->check( $data, (object)[ '$ref' => 'file://' . $schemaPath ] );
152 if ( $validator->isValid() && !$extraErrors ) {
157 $out = "$path did not pass validation.\n";
158 foreach ( $validator->getErrors() as $error ) {
159 $out .= "[{$error['property']}] {$error['message']}\n";
161 if ( $extraErrors ) {
162 $out .= implode( "\n", $extraErrors ) . "\n";
164 throw new ExtensionJsonValidationError( $out );