Merge "Remove unused Phan exception"
[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 $schemaPath = __DIR__ . "/../../docs/extension.schema.v$version.json";
79
80 // Not too old
81 if ( $version < ExtensionRegistry::OLDEST_MANIFEST_VERSION ) {
82 throw new ExtensionJsonValidationError(
83 "$path is using a non-supported schema version"
84 );
85 } elseif ( $version > ExtensionRegistry::MANIFEST_VERSION ) {
86 throw new ExtensionJsonValidationError(
87 "$path is using a non-supported schema version"
88 );
89 }
90
91 $licenseError = false;
92 // Check if it's a string, if not, schema validation will display an error
93 if ( isset( $data->{'license-name'} ) && is_string( $data->{'license-name'} ) ) {
94 $licenses = new SpdxLicenses();
95 $valid = $licenses->validate( $data->{'license-name'} );
96 if ( !$valid ) {
97 $licenseError = '[license-name] Invalid SPDX license identifier, '
98 . 'see <https://spdx.org/licenses/>';
99 }
100 }
101
102 $validator = new Validator;
103 $validator->check( $data, (object)[ '$ref' => 'file://' . $schemaPath ] );
104 if ( $validator->isValid() && !$licenseError ) {
105 // All good.
106 return true;
107 } else {
108 $out = "$path did pass validation.\n";
109 foreach ( $validator->getErrors() as $error ) {
110 $out .= "[{$error['property']}] {$error['message']}\n";
111 }
112 if ( $licenseError ) {
113 $out .= "$licenseError\n";
114 }
115 throw new ExtensionJsonValidationError( $out );
116 }
117 }
118 }