MediaWiki REL1_31
ExtensionJsonValidator.php
Go to the documentation of this file.
1<?php
2
22use Composer\Spdx\SpdxLicenses;
23use JsonSchema\Validator;
24
29
34
38 public function __construct( callable $missingDepCallback ) {
39 $this->missingDepCallback = $missingDepCallback;
40 }
41
46 public function checkDependencies() {
47 if ( !class_exists( Validator::class ) ) {
48 call_user_func( $this->missingDepCallback,
49 'The JsonSchema library cannot be found, please install it through composer.'
50 );
51 return false;
52 } elseif ( !class_exists( SpdxLicenses::class ) ) {
53 call_user_func( $this->missingDepCallback,
54 'The spdx-licenses library cannot be found, please install it through composer.'
55 );
56 return false;
57 }
58
59 return true;
60 }
61
67 public function validate( $path ) {
68 $data = json_decode( file_get_contents( $path ) );
69 if ( !is_object( $data ) ) {
70 throw new ExtensionJsonValidationError( "$path is not valid JSON" );
71 }
72
73 if ( !isset( $data->manifest_version ) ) {
75 "$path does not have manifest_version set." );
76 }
77
78 $version = $data->manifest_version;
79 $schemaPath = __DIR__ . "/../../docs/extension.schema.v$version.json";
80
81 // Not too old
84 "$path is using a non-supported schema version"
85 );
86 } elseif ( $version > ExtensionRegistry::MANIFEST_VERSION ) {
88 "$path is using a non-supported schema version"
89 );
90 }
91
92 $licenseError = false;
93 // Check if it's a string, if not, schema validation will display an error
94 if ( isset( $data->{'license-name'} ) && is_string( $data->{'license-name'} ) ) {
95 $licenses = new SpdxLicenses();
96 $valid = $licenses->validate( $data->{'license-name'} );
97 if ( !$valid ) {
98 $licenseError = '[license-name] Invalid SPDX license identifier, '
99 . 'see <https://spdx.org/licenses/>';
100 }
101 }
102
103 $validator = new Validator;
104 $validator->check( $data, (object)[ '$ref' => 'file://' . $schemaPath ] );
105 if ( $validator->isValid() && !$licenseError ) {
106 // All good.
107 return true;
108 } else {
109 $out = "$path did not pass validation.\n";
110 foreach ( $validator->getErrors() as $error ) {
111 $out .= "[{$error['property']}] {$error['message']}\n";
112 }
113 if ( $licenseError ) {
114 $out .= "$licenseError\n";
115 }
117 }
118 }
119}
__construct(callable $missingDepCallback)
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.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:864