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.
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.
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
21 namespace MediaWiki\Registration
;
23 use Composer\Spdx\SpdxLicenses
;
24 use JsonSchema\Validator
;
25 use Seld\JsonLint\DuplicateKeyException
;
26 use Seld\JsonLint\JsonParser
;
27 use Seld\JsonLint\ParsingException
;
30 * Validate extension.json files against their JSON schema.
32 * This is used for static validation from the command-line via
33 * validateRegistrationFile.php, and the PHPUnit structure test suite
34 * (ExtensionJsonValidationTest).
36 * The files are normally read by the ExtensionRegistry and ExtensionProcessor classes.
39 * @ingroup ExtensionRegistry
41 class ExtensionJsonValidator
{
46 private $missingDepCallback;
49 * @param callable $missingDepCallback
51 public function __construct( callable
$missingDepCallback ) {
52 $this->missingDepCallback
= $missingDepCallback;
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.'
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.'
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.'
86 * @param string $path file to validate
88 * @return bool true if passes validation
89 * @throws ExtensionJsonValidationError on any failure
91 public function validate( $path ) {
92 $contents = file_get_contents( $path );
93 $jsonParser = new JsonParser();
95 $data = $jsonParser->parse( $contents, JsonParser
::DETECT_KEY_CONFLICTS
);
96 } catch ( ParsingException
$e ) {
97 if ( $e instanceof DuplicateKeyException
) {
98 throw new ExtensionJsonValidationError( $e->getMessage() );
100 throw new ExtensionJsonValidationError( "$path is not valid JSON" );
103 if ( !isset( $data->manifest_version
) ) {
104 throw new ExtensionJsonValidationError(
105 "$path does not have manifest_version set." );
108 $version = $data->manifest_version
;
109 $schemaPath = __DIR__
. "/../../docs/extension.schema.v$version.json";
111 if ( $version < ExtensionRegistry
::OLDEST_MANIFEST_VERSION ||
112 $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 = parse_url( $data->url
);
132 if ( !$parsed ||
!isset( $parsed['host'] ) ||
!isset( $parsed['scheme'] ) ) {
133 $extraErrors[] = '[url] URL cannot be parsed';
135 if ( $parsed['host'] === 'www.mediawiki.org' ) {
137 } elseif ( $parsed['host'] === 'mediawiki.org' ) {
139 $extraErrors[] = '[url] Should use www.mediawiki.org domain';
142 if ( $mwoUrl && $parsed['scheme'] !== 'https' ) {
143 $extraErrors[] = '[url] Should use HTTPS for www.mediawiki.org URLs';
148 $validator = new Validator
;
149 $validator->check( $data, (object)[ '$ref' => 'file://' . $schemaPath ] );
150 if ( $validator->isValid() && !$extraErrors ) {
155 $out = "$path did not pass validation.\n";
156 foreach ( $validator->getErrors() as $error ) {
157 $out .= "[{$error['property']}] {$error['message']}\n";
159 if ( $extraErrors ) {
160 $out .= implode( "\n", $extraErrors ) . "\n";
162 throw new ExtensionJsonValidationError( $out );
166 /** @deprecated class alias since 1.43 */
167 class_alias( ExtensionJsonValidator
::class, 'ExtensionJsonValidator' );