Merge "docs: Fix typo"
[mediawiki.git] / includes / db / AbstractSchemaValidator.php
blobea11c2fa14d0b7c2c7dd8b7a560301b1bf0ecb9f
1 <?php
3 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
19 * @file
22 namespace MediaWiki\DB;
24 use JsonSchema\Validator;
25 use Seld\JsonLint\DuplicateKeyException;
26 use Seld\JsonLint\JsonParser;
27 use Seld\JsonLint\ParsingException;
28 use function class_exists;
29 use function file_get_contents;
30 use function is_array;
31 use function is_object;
33 /**
34 * Validate abstract schema json files against their JSON schema.
36 * This is used for static validation from the command-line via
37 * generateSchemaSql.php, generateSchemaChangeSql, and the PHPUnit structure test suite
38 * (AbstractSchemaTest).
40 * The files are normally read by the generateSchemaSql.php and generateSchemaSqlChange.php maintenance scripts.
42 * @since 1.38
44 class AbstractSchemaValidator {
45 public function __construct() {
46 if ( !class_exists( Validator::class ) ) {
47 throw new AbstractSchemaValidationError(
48 'The JsonSchema library cannot be found, please install it through composer.'
52 if ( !class_exists( JsonParser::class ) ) {
53 throw new AbstractSchemaValidationError(
54 'The JSON lint library cannot be found, please install it through composer.'
59 /**
60 * @param string $path file to validate
61 * @return bool true if passes validation
62 * @throws AbstractSchemaValidationError on any failure
64 public function validate( string $path ): bool {
65 $contents = file_get_contents( $path );
66 $jsonParser = new JsonParser();
67 try {
68 $data = $jsonParser->parse( $contents, JsonParser::DETECT_KEY_CONFLICTS );
69 } catch ( DuplicateKeyException $e ) {
70 throw new AbstractSchemaValidationError( $e->getMessage(), $e->getCode(), $e );
71 } catch ( ParsingException $e ) {
72 throw new AbstractSchemaValidationError( "$path is not valid JSON", $e->getCode(), $e );
75 // Regular schema's are arrays, schema changes are objects.
76 if ( is_array( $data ) ) {
77 $schemaPath = __DIR__ . '/../../docs/abstract-schema.schema.json';
78 } elseif ( is_object( $data ) ) {
79 $schemaPath = __DIR__ . '/../../docs/abstract-schema-changes.schema.json';
80 } else {
81 throw new AbstractSchemaValidationError( "$path is not a supported JSON object" );
84 $validator = new Validator;
85 $validator->check( $data, (object)[ '$ref' => 'file://' . $schemaPath ] );
86 if ( $validator->isValid() ) {
87 // All good.
88 return true;
91 $out = "$path did not pass validation.\n";
92 foreach ( $validator->getErrors() as $error ) {
93 $out .= "[{$error['property']}] {$error['message']}\n";
95 throw new AbstractSchemaValidationError( $out );