Merge "Revert "Log the reason why revision->getContent() returns null""
[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 * @codeCoverageIgnore
44 * @return bool
45 */
46 public function checkDependencies() {
47 if ( !class_exists( Validator::class ) ) {
48 call_user_func( $this->missingDepCallback,
49 'The JsonSchema library cannot be found, please install it through composer.'
50 );
51 return false;
52 } elseif ( !class_exists( SpdxLicenses::class ) ) {
53 call_user_func( $this->missingDepCallback,
54 'The spdx-licenses library cannot be found, please install it through composer.'
55 );
56 return false;
57 }
58
59 return true;
60 }
61
62 /**
63 * @param string $path file to validate
64 * @return bool true if passes validation
65 * @throws ExtensionJsonValidationError on any failure
66 */
67 public function validate( $path ) {
68 $data = json_decode( file_get_contents( $path ) );
69 if ( !is_object( $data ) ) {
70 throw new ExtensionJsonValidationError( "$path is not valid JSON" );
71 }
72
73 if ( !isset( $data->manifest_version ) ) {
74 throw new ExtensionJsonValidationError(
75 "$path does not have manifest_version set." );
76 }
77
78 $version = $data->manifest_version;
79 $schemaPath = __DIR__ . "/../../docs/extension.schema.v$version.json";
80
81 // Not too old
82 if ( $version < ExtensionRegistry::OLDEST_MANIFEST_VERSION ) {
83 throw new ExtensionJsonValidationError(
84 "$path is using a non-supported schema version"
85 );
86 } elseif ( $version > ExtensionRegistry::MANIFEST_VERSION ) {
87 throw new ExtensionJsonValidationError(
88 "$path is using a non-supported schema version"
89 );
90 }
91
92 $licenseError = false;
93 // Check if it's a string, if not, schema validation will display an error
94 if ( isset( $data->{'license-name'} ) && is_string( $data->{'license-name'} ) ) {
95 $licenses = new SpdxLicenses();
96 $valid = $licenses->validate( $data->{'license-name'} );
97 if ( !$valid ) {
98 $licenseError = '[license-name] Invalid SPDX license identifier, '
99 . 'see <https://spdx.org/licenses/>';
100 }
101 }
102
103 $validator = new Validator;
104 $validator->check( $data, (object)[ '$ref' => 'file://' . $schemaPath ] );
105 if ( $validator->isValid() && !$licenseError ) {
106 // All good.
107 return true;
108 } else {
109 $out = "$path did not pass validation.\n";
110 foreach ( $validator->getErrors() as $error ) {
111 $out .= "[{$error['property']}] {$error['message']}\n";
112 }
113 if ( $licenseError ) {
114 $out .= "$licenseError\n";
115 }
116 throw new ExtensionJsonValidationError( $out );
117 }
118 }
119 }