"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "includes/registration/ExtensionJsonValidator.php" between
mediawiki-1.31.1.tar.gz and mediawiki-1.32.0.tar.gz

About: MediaWiki is a wiki engine (the collaborative editing software that runs for e.g. Wikipedia, the free encyclopedia).

ExtensionJsonValidator.php  (mediawiki-1.31.1):ExtensionJsonValidator.php  (mediawiki-1.32.0)
skipping to change at line 24 skipping to change at line 24
* You should have received a copy of the GNU General Public License along * You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., * with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html * http://www.gnu.org/copyleft/gpl.html
* *
* @file * @file
*/ */
use Composer\Spdx\SpdxLicenses; use Composer\Spdx\SpdxLicenses;
use JsonSchema\Validator; use JsonSchema\Validator;
use Seld\JsonLint\JsonParser;
use Seld\JsonLint\ParsingException;
/** /**
* Validate extension.json files against their JSON schema.
*
* This is used for static validation from the command-line via
* validateRegistrationFile.php, and the PHPUnit structure test suite
* (ExtensionJsonValidationTest).
*
* The files are normally read by the ExtensionRegistry
* and ExtensionProcessor classes.
*
* @since 1.29 * @since 1.29
*/ */
class ExtensionJsonValidator { class ExtensionJsonValidator {
/** /**
* @var callable * @var callable
*/ */
private $missingDepCallback; private $missingDepCallback;
/** /**
skipping to change at line 57 skipping to change at line 68
if ( !class_exists( Validator::class ) ) { if ( !class_exists( Validator::class ) ) {
call_user_func( $this->missingDepCallback, call_user_func( $this->missingDepCallback,
'The JsonSchema library cannot be found, please i nstall it through composer.' 'The JsonSchema library cannot be found, please i nstall it through composer.'
); );
return false; return false;
} elseif ( !class_exists( SpdxLicenses::class ) ) { } elseif ( !class_exists( SpdxLicenses::class ) ) {
call_user_func( $this->missingDepCallback, call_user_func( $this->missingDepCallback,
'The spdx-licenses library cannot be found, pleas e install it through composer.' 'The spdx-licenses library cannot be found, pleas e install it through composer.'
); );
return false; return false;
} elseif ( !class_exists( JsonParser::class ) ) {
call_user_func( $this->missingDepCallback,
'The JSON lint library cannot be found, please in
stall it through composer.'
);
} }
return true; return true;
} }
/** /**
* @param string $path file to validate * @param string $path file to validate
* @return bool true if passes validation * @return bool true if passes validation
* @throws ExtensionJsonValidationError on any failure * @throws ExtensionJsonValidationError on any failure
*/ */
public function validate( $path ) { public function validate( $path ) {
$data = json_decode( file_get_contents( $path ) ); $contents = file_get_contents( $path );
if ( !is_object( $data ) ) { $jsonParser = new JsonParser();
try {
$data = $jsonParser->parse( $contents, JsonParser::DETECT
_KEY_CONFLICTS );
} catch ( ParsingException $e ) {
if ( $e instanceof \Seld\JsonLint\DuplicateKeyException )
{
throw new ExtensionJsonValidationError( $e->getMe
ssage() );
}
throw new ExtensionJsonValidationError( "$path is not val id JSON" ); throw new ExtensionJsonValidationError( "$path is not val id JSON" );
} }
if ( !isset( $data->manifest_version ) ) { if ( !isset( $data->manifest_version ) ) {
throw new ExtensionJsonValidationError( throw new ExtensionJsonValidationError(
"$path does not have manifest_version set." ); "$path does not have manifest_version set." );
} }
$version = $data->manifest_version; $version = $data->manifest_version;
$schemaPath = __DIR__ . "/../../docs/extension.schema.v$version.j son"; $schemaPath = __DIR__ . "/../../docs/extension.schema.v$version.j son";
skipping to change at line 92 skipping to change at line 113
if ( $version < ExtensionRegistry::OLDEST_MANIFEST_VERSION ) { if ( $version < ExtensionRegistry::OLDEST_MANIFEST_VERSION ) {
throw new ExtensionJsonValidationError( throw new ExtensionJsonValidationError(
"$path is using a non-supported schema version" "$path is using a non-supported schema version"
); );
} elseif ( $version > ExtensionRegistry::MANIFEST_VERSION ) { } elseif ( $version > ExtensionRegistry::MANIFEST_VERSION ) {
throw new ExtensionJsonValidationError( throw new ExtensionJsonValidationError(
"$path is using a non-supported schema version" "$path is using a non-supported schema version"
); );
} }
$licenseError = false; $extraErrors = [];
// Check if it's a string, if not, schema validation will display an error // Check if it's a string, if not, schema validation will display an error
if ( isset( $data->{'license-name'} ) && is_string( $data->{'lice nse-name'} ) ) { if ( isset( $data->{'license-name'} ) && is_string( $data->{'lice nse-name'} ) ) {
$licenses = new SpdxLicenses(); $licenses = new SpdxLicenses();
$valid = $licenses->validate( $data->{'license-name'} ); $valid = $licenses->validate( $data->{'license-name'} );
if ( !$valid ) { if ( !$valid ) {
$licenseError = '[license-name] Invalid SPDX lice nse identifier, ' $extraErrors[] = '[license-name] Invalid SPDX lic ense identifier, '
. 'see <https://spdx.org/licenses/>'; . 'see <https://spdx.org/licenses/>';
} }
} }
if ( isset( $data->url ) && is_string( $data->url ) ) {
$parsed = wfParseUrl( $data->url );
$mwoUrl = false;
if ( $parsed['host'] === 'www.mediawiki.org' ) {
$mwoUrl = true;
} elseif ( $parsed['host'] === 'mediawiki.org' ) {
$mwoUrl = true;
$extraErrors[] = '[url] Should use www.mediawiki.
org domain';
}
if ( $mwoUrl && $parsed['scheme'] !== 'https' ) {
$extraErrors[] = '[url] Should use HTTPS for www.
mediawiki.org URLs';
}
}
$validator = new Validator; $validator = new Validator;
$validator->check( $data, (object)[ '$ref' => 'file://' . $schema Path ] ); $validator->check( $data, (object)[ '$ref' => 'file://' . $schema Path ] );
if ( $validator->isValid() && !$licenseError ) { if ( $validator->isValid() && !$extraErrors ) {
// All good. // All good.
return true; return true;
} else { } else {
$out = "$path did not pass validation.\n"; $out = "$path did not pass validation.\n";
foreach ( $validator->getErrors() as $error ) { foreach ( $validator->getErrors() as $error ) {
$out .= "[{$error['property']}] {$error['message' ]}\n"; $out .= "[{$error['property']}] {$error['message' ]}\n";
} }
if ( $licenseError ) { if ( $extraErrors ) {
$out .= "$licenseError\n"; $out .= implode( "\n", $extraErrors ) . "\n";
} }
throw new ExtensionJsonValidationError( $out ); throw new ExtensionJsonValidationError( $out );
} }
} }
} }
 End of changes. 9 change blocks. 
7 lines changed or deleted 48 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)