Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
57.14% covered (warning)
57.14%
16 / 28
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractSchemaValidator
57.14% covered (warning)
57.14%
16 / 28
0.00% covered (danger)
0.00%
0 / 2
17.87
0.00% covered (danger)
0.00%
0 / 1
 __construct
25.00% covered (danger)
25.00%
2 / 8
0.00% covered (danger)
0.00%
0 / 1
6.80
 validate
70.00% covered (warning)
70.00%
14 / 20
0.00% covered (danger)
0.00%
0 / 1
8.32
1<?php
2
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.
8 *
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.
13 *
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
18 *
19 * @file
20 */
21
22namespace MediaWiki\DB;
23
24use JsonSchema\Validator;
25use Seld\JsonLint\DuplicateKeyException;
26use Seld\JsonLint\JsonParser;
27use Seld\JsonLint\ParsingException;
28use function class_exists;
29use function file_get_contents;
30use function is_array;
31use function is_object;
32
33/**
34 * Validate abstract schema json files against their JSON schema.
35 *
36 * This is used for static validation from the command-line via
37 * generateSchemaSql.php, generateSchemaChangeSql, and the PHPUnit structure test suite
38 * (AbstractSchemaTest).
39 *
40 * The files are normally read by the generateSchemaSql.php and generateSchemaSqlChange.php maintenance scripts.
41 *
42 * @since 1.38
43 */
44class 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.'
49            );
50        }
51
52        if ( !class_exists( JsonParser::class ) ) {
53            throw new AbstractSchemaValidationError(
54                'The JSON lint library cannot be found, please install it through composer.'
55            );
56        }
57    }
58
59    /**
60     * @param string $path file to validate
61     * @return bool true if passes validation
62     * @throws AbstractSchemaValidationError on any failure
63     */
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 );
73        }
74
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" );
82        }
83
84        $validator = new Validator;
85        $validator->check( $data, (object)[ '$ref' => 'file://' . $schemaPath ] );
86        if ( $validator->isValid() ) {
87            // All good.
88            return true;
89        }
90
91        $out = "$path did not pass validation.\n";
92        foreach ( $validator->getErrors() as $error ) {
93            $out .= "[{$error['property']}{$error['message']}\n";
94        }
95        throw new AbstractSchemaValidationError( $out );
96    }
97}