Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.83% covered (success)
95.83%
46 / 48
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExtensionJsonValidator
97.87% covered (success)
97.87%
46 / 47
50.00% covered (danger)
50.00%
1 / 2
27
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 checkDependencies
n/a
0 / 0
n/a
0 / 0
4
 validate
97.83% covered (success)
97.83%
45 / 46
0.00% covered (danger)
0.00%
0 / 1
22
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Registration;
22
23use Composer\Spdx\SpdxLicenses;
24use JsonSchema\Validator;
25use Seld\JsonLint\DuplicateKeyException;
26use Seld\JsonLint\JsonParser;
27use Seld\JsonLint\ParsingException;
28
29/**
30 * Validate extension.json files against their JSON schema.
31 *
32 * This is used for static validation from the command-line via
33 * validateRegistrationFile.php, and the PHPUnit structure test suite
34 * (ExtensionJsonValidationTest).
35 *
36 * The files are normally read by the ExtensionRegistry and ExtensionProcessor classes.
37 *
38 * @since 1.29
39 * @ingroup ExtensionRegistry
40 */
41class ExtensionJsonValidator {
42
43    /**
44     * @var callable
45     */
46    private $missingDepCallback;
47
48    public function __construct( callable $missingDepCallback ) {
49        $this->missingDepCallback = $missingDepCallback;
50    }
51
52    /**
53     * @codeCoverageIgnore
54     * @return bool
55     */
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
82    /**
83     * @param string $path file to validate
84     *
85     * @return bool true if passes validation
86     * @throws ExtensionJsonValidationError on any failure
87     */
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 ) ) {
101            throw new ExtensionJsonValidationError(
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
108        if ( $version < ExtensionRegistry::OLDEST_MANIFEST_VERSION ||
109            $version > ExtensionRegistry::MANIFEST_VERSION
110        ) {
111            throw new ExtensionJsonValidationError(
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
163/** @deprecated class alias since 1.43 */
164class_alias( ExtensionJsonValidator::class, 'ExtensionJsonValidator' );