ExtensionJsonValidator.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. use Composer\Spdx\SpdxLicenses;
  21. use JsonSchema\Validator;
  22. use Seld\JsonLint\JsonParser;
  23. use Seld\JsonLint\ParsingException;
  24. /**
  25. * Validate extension.json files against their JSON schema.
  26. *
  27. * This is used for static validation from the command-line via
  28. * validateRegistrationFile.php, and the PHPUnit structure test suite
  29. * (ExtensionJsonValidationTest).
  30. *
  31. * The files are normally read by the ExtensionRegistry
  32. * and ExtensionProcessor classes.
  33. *
  34. * @since 1.29
  35. */
  36. class ExtensionJsonValidator {
  37. /**
  38. * @var callable
  39. */
  40. private $missingDepCallback;
  41. /**
  42. * @param callable $missingDepCallback
  43. */
  44. public function __construct( callable $missingDepCallback ) {
  45. $this->missingDepCallback = $missingDepCallback;
  46. }
  47. /**
  48. * @codeCoverageIgnore
  49. * @return bool
  50. */
  51. public function checkDependencies() {
  52. if ( !class_exists( Validator::class ) ) {
  53. call_user_func( $this->missingDepCallback,
  54. 'The JsonSchema library cannot be found, please install it through composer.'
  55. );
  56. return false;
  57. }
  58. if ( !class_exists( SpdxLicenses::class ) ) {
  59. call_user_func( $this->missingDepCallback,
  60. 'The spdx-licenses library cannot be found, please install it through composer.'
  61. );
  62. return false;
  63. }
  64. if ( !class_exists( JsonParser::class ) ) {
  65. call_user_func( $this->missingDepCallback,
  66. 'The JSON lint library cannot be found, please install it through composer.'
  67. );
  68. }
  69. return true;
  70. }
  71. /**
  72. * @param string $path file to validate
  73. * @return bool true if passes validation
  74. * @throws ExtensionJsonValidationError on any failure
  75. */
  76. public function validate( $path ) {
  77. $contents = file_get_contents( $path );
  78. $jsonParser = new JsonParser();
  79. try {
  80. $data = $jsonParser->parse( $contents, JsonParser::DETECT_KEY_CONFLICTS );
  81. } catch ( ParsingException $e ) {
  82. if ( $e instanceof \Seld\JsonLint\DuplicateKeyException ) {
  83. throw new ExtensionJsonValidationError( $e->getMessage() );
  84. }
  85. throw new ExtensionJsonValidationError( "$path is not valid JSON" );
  86. }
  87. if ( !isset( $data->manifest_version ) ) {
  88. throw new ExtensionJsonValidationError(
  89. "$path does not have manifest_version set." );
  90. }
  91. $version = $data->manifest_version;
  92. $schemaPath = __DIR__ . "/../../docs/extension.schema.v$version.json";
  93. // Not too old
  94. if ( $version < ExtensionRegistry::OLDEST_MANIFEST_VERSION ) {
  95. throw new ExtensionJsonValidationError(
  96. "$path is using a non-supported schema version"
  97. );
  98. }
  99. if ( $version > ExtensionRegistry::MANIFEST_VERSION ) {
  100. throw new ExtensionJsonValidationError(
  101. "$path is using a non-supported schema version"
  102. );
  103. }
  104. $extraErrors = [];
  105. // Check if it's a string, if not, schema validation will display an error
  106. if ( isset( $data->{'license-name'} ) && is_string( $data->{'license-name'} ) ) {
  107. $licenses = new SpdxLicenses();
  108. $valid = $licenses->validate( $data->{'license-name'} );
  109. if ( !$valid ) {
  110. $extraErrors[] = '[license-name] Invalid SPDX license identifier, '
  111. . 'see <https://spdx.org/licenses/>';
  112. }
  113. }
  114. if ( isset( $data->url ) && is_string( $data->url ) ) {
  115. $parsed = wfParseUrl( $data->url );
  116. $mwoUrl = false;
  117. if ( $parsed['host'] === 'www.mediawiki.org' ) {
  118. $mwoUrl = true;
  119. } elseif ( $parsed['host'] === 'mediawiki.org' ) {
  120. $mwoUrl = true;
  121. $extraErrors[] = '[url] Should use www.mediawiki.org domain';
  122. }
  123. if ( $mwoUrl && $parsed['scheme'] !== 'https' ) {
  124. $extraErrors[] = '[url] Should use HTTPS for www.mediawiki.org URLs';
  125. }
  126. }
  127. // Deprecated stuff
  128. if ( isset( $data->ParserTestFiles ) ) {
  129. // phpcs:ignore Generic.Files.LineLength.TooLong
  130. $extraErrors[] = '[ParserTestFiles] DEPRECATED: see <https://www.mediawiki.org/wiki/Manual:Extension.json/Schema#ParserTestFiles>';
  131. }
  132. $validator = new Validator;
  133. $validator->check( $data, (object)[ '$ref' => 'file://' . $schemaPath ] );
  134. if ( $validator->isValid() && !$extraErrors ) {
  135. // All good.
  136. return true;
  137. }
  138. $out = "$path did not pass validation.\n";
  139. foreach ( $validator->getErrors() as $error ) {
  140. $out .= "[{$error['property']}] {$error['message']}\n";
  141. }
  142. if ( $extraErrors ) {
  143. $out .= implode( "\n", $extraErrors ) . "\n";
  144. }
  145. throw new ExtensionJsonValidationError( $out );
  146. }
  147. }