MediaWiki  REL1_31
ExtensionJsonValidator.php
Go to the documentation of this file.
1 <?php
2 
22 use Composer\Spdx\SpdxLicenses;
23 use 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  }
116  throw new ExtensionJsonValidationError( $out );
117  }
118  }
119 }
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
ExtensionRegistry\MANIFEST_VERSION
const MANIFEST_VERSION
Version of the highest supported manifest version Note: Update MANIFEST_VERSION_MW_VERSION when chang...
Definition: ExtensionRegistry.php:25
$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 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
ExtensionJsonValidator\$missingDepCallback
callable $missingDepCallback
Definition: ExtensionJsonValidator.php:33
ExtensionJsonValidator\checkDependencies
checkDependencies()
Definition: ExtensionJsonValidator.php:46
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:37
ExtensionRegistry\OLDEST_MANIFEST_VERSION
const OLDEST_MANIFEST_VERSION
Version of the oldest supported manifest version.
Definition: ExtensionRegistry.php:36
ExtensionJsonValidator\__construct
__construct(callable $missingDepCallback)
Definition: ExtensionJsonValidator.php:38
ExtensionJsonValidator
Definition: ExtensionJsonValidator.php:28
ExtensionJsonValidator\validate
validate( $path)
Definition: ExtensionJsonValidator.php:67
$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:22
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:56