MediaWiki  1.33.0
ExtensionJsonValidator.php
Go to the documentation of this file.
1 <?php
2 
22 use Composer\Spdx\SpdxLicenses;
23 use JsonSchema\Validator;
24 use Seld\JsonLint\JsonParser;
25 use 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  }
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 
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 \Seld\JsonLint\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 ) ) {
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  // Not too old
109  "$path is using a non-supported schema version"
110  );
111  }
112 
113  if ( $version > ExtensionRegistry::MANIFEST_VERSION ) {
115  "$path is using a non-supported schema version"
116  );
117  }
118 
119  $extraErrors = [];
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'} );
124  if ( !$valid ) {
125  $extraErrors[] = '[license-name] Invalid SPDX license identifier, '
126  . 'see <https://spdx.org/licenses/>';
127  }
128  }
129  if ( isset( $data->url ) && is_string( $data->url ) ) {
130  $parsed = wfParseUrl( $data->url );
131  $mwoUrl = false;
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  $validator = new Validator;
145  $validator->check( $data, (object)[ '$ref' => 'file://' . $schemaPath ] );
146  if ( $validator->isValid() && !$extraErrors ) {
147  // All good.
148  return true;
149  }
150 
151  $out = "$path did not pass validation.\n";
152  foreach ( $validator->getErrors() as $error ) {
153  $out .= "[{$error['property']}] {$error['message']}\n";
154  }
155  if ( $extraErrors ) {
156  $out .= implode( "\n", $extraErrors ) . "\n";
157  }
158  throw new ExtensionJsonValidationError( $out );
159  }
160 }
ExtensionRegistry\MANIFEST_VERSION
const MANIFEST_VERSION
Version of the highest supported manifest version Note: Update MANIFEST_VERSION_MW_VERSION when chang...
Definition: ExtensionRegistry.php:28
$out
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password 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:780
ExtensionJsonValidator\$missingDepCallback
callable $missingDepCallback
Definition: ExtensionJsonValidator.php:44
ExtensionJsonValidator\checkDependencies
checkDependencies()
Definition: ExtensionJsonValidator.php:57
php
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:35
$data
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
Definition: generatePhpCharToUpperMappings.php:13
wfParseUrl
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
Definition: GlobalFunctions.php:817
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
ExtensionRegistry\OLDEST_MANIFEST_VERSION
const OLDEST_MANIFEST_VERSION
Version of the oldest supported manifest version.
Definition: ExtensionRegistry.php:39
ExtensionJsonValidator\__construct
__construct(callable $missingDepCallback)
Definition: ExtensionJsonValidator.php:49
ExtensionJsonValidator
Validate extension.json files against their JSON schema.
Definition: ExtensionJsonValidator.php:39
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2162
ExtensionJsonValidator\validate
validate( $path)
Definition: ExtensionJsonValidator.php:86
$path
$path
Definition: NoLocalSettings.php:25
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ExtensionJsonValidationError
Definition: ExtensionJsonValidationError.php:21
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52