Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.87% covered (success)
97.87%
46 / 47
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
21use Composer\Spdx\SpdxLicenses;
22use JsonSchema\Validator;
23use Seld\JsonLint\DuplicateKeyException;
24use Seld\JsonLint\JsonParser;
25use Seld\JsonLint\ParsingException;
26
27/**
28 * Validate extension.json files against their JSON schema.
29 *
30 * This is used for static validation from the command-line via
31 * validateRegistrationFile.php, and the PHPUnit structure test suite
32 * (ExtensionJsonValidationTest).
33 *
34 * The files are normally read by the ExtensionRegistry and ExtensionProcessor classes.
35 *
36 * @since 1.29
37 * @ingroup ExtensionRegistry
38 */
39class ExtensionJsonValidator {
40
41    /**
42     * @var callable
43     */
44    private $missingDepCallback;
45
46    /**
47     * @param callable $missingDepCallback
48     */
49    public function __construct( callable $missingDepCallback ) {
50        $this->missingDepCallback = $missingDepCallback;
51    }
52
53    /**
54     * @codeCoverageIgnore
55     * @return bool
56     */
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.'
61            );
62            return false;
63        }
64
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.'
68            );
69            return false;
70        }
71
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.'
75            );
76        }
77
78        return true;
79    }
80
81    /**
82     * @param string $path file to validate
83     * @return bool true if passes validation
84     * @throws ExtensionJsonValidationError on any failure
85     */
86    public function validate( $path ) {
87        $contents = file_get_contents( $path );
88        $jsonParser = new JsonParser();
89        try {
90            $data = $jsonParser->parse( $contents, JsonParser::DETECT_KEY_CONFLICTS );
91        } catch ( ParsingException $e ) {
92            if ( $e instanceof DuplicateKeyException ) {
93                throw new ExtensionJsonValidationError( $e->getMessage() );
94            }
95            throw new ExtensionJsonValidationError( "$path is not valid JSON" );
96        }
97
98        if ( !isset( $data->manifest_version ) ) {
99            throw new ExtensionJsonValidationError(
100                "$path does not have manifest_version set." );
101        }
102
103        $version = $data->manifest_version;
104        $schemaPath = __DIR__ . "/../../docs/extension.schema.v$version.json";
105
106        if ( $version < ExtensionRegistry::OLDEST_MANIFEST_VERSION ||
107            $version > ExtensionRegistry::MANIFEST_VERSION
108        ) {
109            throw new ExtensionJsonValidationError(
110                "$path is using a non-supported schema version"
111            );
112        }
113
114        $extraErrors = [];
115        // Check if it's a string, if not, schema validation will display an error
116        if ( isset( $data->{'license-name'} ) && is_string( $data->{'license-name'} ) ) {
117            $licenses = new SpdxLicenses();
118            $valid = $licenses->validate( $data->{'license-name'} );
119            if ( !$valid ) {
120                $extraErrors[] = '[license-name] Invalid SPDX license identifier, '
121                    . 'see <https://spdx.org/licenses/>';
122            }
123        }
124        if ( isset( $data->url ) && is_string( $data->url ) ) {
125            $parsed = parse_url( $data->url );
126            $mwoUrl = false;
127            if ( !$parsed || !isset( $parsed['host'] ) || !isset( $parsed['scheme'] ) ) {
128                $extraErrors[] = '[url] URL cannot be parsed';
129            } else {
130                if ( $parsed['host'] === 'www.mediawiki.org' ) {
131                    $mwoUrl = true;
132                } elseif ( $parsed['host'] === 'mediawiki.org' ) {
133                    $mwoUrl = true;
134                    $extraErrors[] = '[url] Should use www.mediawiki.org domain';
135                }
136
137                if ( $mwoUrl && $parsed['scheme'] !== 'https' ) {
138                    $extraErrors[] = '[url] Should use HTTPS for www.mediawiki.org URLs';
139                }
140            }
141        }
142
143        $validator = new Validator;
144        $validator->check( $data, (object)[ '$ref' => 'file://' . $schemaPath ] );
145        if ( $validator->isValid() && !$extraErrors ) {
146            // All good.
147            return true;
148        }
149
150        $out = "$path did not pass validation.\n";
151        foreach ( $validator->getErrors() as $error ) {
152            $out .= "[{$error['property']}{$error['message']}\n";
153        }
154        if ( $extraErrors ) {
155            $out .= implode( "\n", $extraErrors ) . "\n";
156        }
157        throw new ExtensionJsonValidationError( $out );
158    }
159}