Merge "Chinese Conversion Table Update 2016-7"
[lhc/web/wiklou.git] / includes / registration / ExtensionJsonValidator.php
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
22 use Composer\Spdx\SpdxLicenses;
23 use JsonSchema\Validator;
24
25 /**
26 * @since 1.29
27 */
28 class ExtensionJsonValidator {
29
30 /**
31 * @var callable
32 */
33 private $missingDepCallback;
34
35 /**
36 * @param callable $missingDepCallback
37 */
38 public function __construct( callable $missingDepCallback ) {
39 $this->missingDepCallback = $missingDepCallback;
40 }
41
42 /**
43 * @return bool
44 */
45 public function checkDependencies() {
46 if ( !class_exists( Validator::class ) ) {
47 call_user_func( $this->missingDepCallback,
48 'The JsonSchema library cannot be found, please install it through composer.'
49 );
50 return false;
51 } elseif ( !class_exists( SpdxLicenses::class ) ) {
52 call_user_func( $this->missingDepCallback,
53 'The spdx-licenses library cannot be found, please install it through composer.'
54 );
55 return false;
56 }
57
58 return true;
59 }
60
61 /**
62 * @param string $path file to validate
63 * @return bool true if passes validation
64 * @throws ExtensionJsonValidationError on any failure
65 */
66 public function validate( $path ) {
67 $data = json_decode( file_get_contents( $path ) );
68 if ( !is_object( $data ) ) {
69 throw new ExtensionJsonValidationError( "$path is not valid JSON" );
70 }
71
72 if ( !isset( $data->manifest_version ) ) {
73 throw new ExtensionJsonValidationError(
74 "$path does not have manifest_version set." );
75 }
76
77 $version = $data->manifest_version;
78 if ( $version !== ExtensionRegistry::MANIFEST_VERSION ) {
79 $schemaPath = __DIR__ . "/../../docs/extension.schema.v$version.json";
80 } else {
81 $schemaPath = __DIR__ . '/../../docs/extension.schema.json';
82 }
83
84 // Not too old
85 if ( $version < ExtensionRegistry::OLDEST_MANIFEST_VERSION ) {
86 throw new ExtensionJsonValidationError(
87 "$path is using a non-supported schema version"
88 );
89 } elseif ( $version > ExtensionRegistry::MANIFEST_VERSION ) {
90 throw new ExtensionJsonValidationError(
91 "$path is using a non-supported schema version"
92 );
93 }
94
95 $licenseError = false;
96 // Check if it's a string, if not, schema validation will display an error
97 if ( isset( $data->{'license-name'} ) && is_string( $data->{'license-name'} ) ) {
98 $licenses = new SpdxLicenses();
99 $valid = $licenses->validate( $data->{'license-name'} );
100 if ( !$valid ) {
101 $licenseError = '[license-name] Invalid SPDX license identifier, '
102 . 'see <https://spdx.org/licenses/>';
103 }
104 }
105
106 $validator = new Validator;
107 $validator->check( $data, (object)[ '$ref' => 'file://' . $schemaPath ] );
108 if ( $validator->isValid() && !$licenseError ) {
109 // All good.
110 return true;
111 } else {
112 $out = "$path did pass validation.\n";
113 foreach ( $validator->getErrors() as $error ) {
114 $out .= "[{$error['property']}] {$error['message']}\n";
115 }
116 if ( $licenseError ) {
117 $out .= "$licenseError\n";
118 }
119 throw new ExtensionJsonValidationError( $out );
120 }
121 }
122 }