MediaWiki REL1_32
ExtensionJsonValidator.php
Go to the documentation of this file.
1<?php
2
22use Composer\Spdx\SpdxLicenses;
23use JsonSchema\Validator;
24use Seld\JsonLint\JsonParser;
25use Seld\JsonLint\ParsingException;
26
40
45
49 public function __construct( callable $missingDepCallback ) {
50 $this->missingDepCallback = $missingDepCallback;
51 }
52
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 } elseif ( !class_exists( SpdxLicenses::class ) ) {
64 call_user_func( $this->missingDepCallback,
65 'The spdx-licenses library cannot be found, please install it through composer.'
66 );
67 return false;
68 } elseif ( !class_exists( JsonParser::class ) ) {
69 call_user_func( $this->missingDepCallback,
70 'The JSON lint library cannot be found, please install it through composer.'
71 );
72 }
73
74 return true;
75 }
76
82 public function validate( $path ) {
83 $contents = file_get_contents( $path );
84 $jsonParser = new JsonParser();
85 try {
86 $data = $jsonParser->parse( $contents, JsonParser::DETECT_KEY_CONFLICTS );
87 } catch ( ParsingException $e ) {
88 if ( $e instanceof \Seld\JsonLint\DuplicateKeyException ) {
89 throw new ExtensionJsonValidationError( $e->getMessage() );
90 }
91 throw new ExtensionJsonValidationError( "$path is not valid JSON" );
92 }
93
94 if ( !isset( $data->manifest_version ) ) {
96 "$path does not have manifest_version set." );
97 }
98
99 $version = $data->manifest_version;
100 $schemaPath = __DIR__ . "/../../docs/extension.schema.v$version.json";
101
102 // Not too old
105 "$path is using a non-supported schema version"
106 );
107 } elseif ( $version > ExtensionRegistry::MANIFEST_VERSION ) {
109 "$path is using a non-supported schema version"
110 );
111 }
112
113 $extraErrors = [];
114 // Check if it's a string, if not, schema validation will display an error
115 if ( isset( $data->{'license-name'} ) && is_string( $data->{'license-name'} ) ) {
116 $licenses = new SpdxLicenses();
117 $valid = $licenses->validate( $data->{'license-name'} );
118 if ( !$valid ) {
119 $extraErrors[] = '[license-name] Invalid SPDX license identifier, '
120 . 'see <https://spdx.org/licenses/>';
121 }
122 }
123 if ( isset( $data->url ) && is_string( $data->url ) ) {
124 $parsed = wfParseUrl( $data->url );
125 $mwoUrl = false;
126 if ( $parsed['host'] === 'www.mediawiki.org' ) {
127 $mwoUrl = true;
128 } elseif ( $parsed['host'] === 'mediawiki.org' ) {
129 $mwoUrl = true;
130 $extraErrors[] = '[url] Should use www.mediawiki.org domain';
131 }
132
133 if ( $mwoUrl && $parsed['scheme'] !== 'https' ) {
134 $extraErrors[] = '[url] Should use HTTPS for www.mediawiki.org URLs';
135 }
136 }
137
138 $validator = new Validator;
139 $validator->check( $data, (object)[ '$ref' => 'file://' . $schemaPath ] );
140 if ( $validator->isValid() && !$extraErrors ) {
141 // All good.
142 return true;
143 } else {
144 $out = "$path did not pass validation.\n";
145 foreach ( $validator->getErrors() as $error ) {
146 $out .= "[{$error['property']}] {$error['message']}\n";
147 }
148 if ( $extraErrors ) {
149 $out .= implode( "\n", $extraErrors ) . "\n";
150 }
152 }
153 }
154}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
Validate extension.json files against their JSON schema.
__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:894
returning false will NOT prevent logging $e
Definition hooks.txt:2226
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37