validateRegistrationFile.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. require_once __DIR__ . '/Maintenance.php';
  3. class ValidateRegistrationFile extends Maintenance {
  4. public function __construct() {
  5. parent::__construct();
  6. $this->addArg( 'path', 'Path to extension.json/skin.json file.', true );
  7. }
  8. public function execute() {
  9. if ( !class_exists( 'JsonSchema\Uri\UriRetriever' ) ) {
  10. $this->error( 'The JsonSchema library cannot be found, please install it through composer.', 1 );
  11. }
  12. $path = $this->getArg( 0 );
  13. $data = json_decode( file_get_contents( $path ) );
  14. if ( !is_object( $data ) ) {
  15. $this->error( "$path is not a valid JSON file.", 1 );
  16. }
  17. if ( !isset( $data->manifest_version ) ) {
  18. $this->output( "Warning: No manifest_version set, assuming 1.\n" );
  19. // For backwards-compatability assume 1
  20. $data->manifest_version = 1;
  21. }
  22. $version = $data->manifest_version;
  23. if ( $version !== ExtensionRegistry::MANIFEST_VERSION ) {
  24. $schemaPath = dirname( __DIR__ ) . "/docs/extension.schema.v$version.json";
  25. } else {
  26. $schemaPath = dirname( __DIR__ ) . '/docs/extension.schema.json';
  27. }
  28. if ( $version < ExtensionRegistry::OLDEST_MANIFEST_VERSION
  29. || $version > ExtensionRegistry::MANIFEST_VERSION
  30. ) {
  31. $this->error( "Error: $path is using a non-supported schema version, it should use "
  32. . ExtensionRegistry::MANIFEST_VERSION, 1 );
  33. } elseif ( $version < ExtensionRegistry::MANIFEST_VERSION ) {
  34. $this->output( "Warning: $path is using a deprecated schema, and should be updated to "
  35. . ExtensionRegistry::MANIFEST_VERSION . "\n" );
  36. }
  37. $retriever = new JsonSchema\Uri\UriRetriever();
  38. $schema = $retriever->retrieve( 'file://' . $schemaPath );
  39. $validator = new JsonSchema\Validator();
  40. $validator->check( $data, $schema );
  41. if ( $validator->isValid() ) {
  42. $this->output( "$path validates against the version $version schema!\n" );
  43. } else {
  44. foreach ( $validator->getErrors() as $error ) {
  45. $this->output( "[{$error['property']}] {$error['message']}\n" );
  46. }
  47. $this->error( "$path does not validate.", 1 );
  48. }
  49. }
  50. }
  51. $maintClass = 'ValidateRegistrationFile';
  52. require_once RUN_MAINTENANCE_IF_MAIN;