From fbd2740cb1d2af7ac40818842f1ec86aca0bcf04 Mon Sep 17 00:00:00 2001 From: battye Date: Thu, 20 Oct 2022 14:14:52 +0000 Subject: [PATCH] Validate 4.0 composer.json, check for the right version. #71 --- .../Command/ValidateCommand.php | 4 +- .../Validator/FileValidator.php | 58 +++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/Phpbb/TranslationValidator/Command/ValidateCommand.php b/src/Phpbb/TranslationValidator/Command/ValidateCommand.php index e65bf8b..246a780 100644 --- a/src/Phpbb/TranslationValidator/Command/ValidateCommand.php +++ b/src/Phpbb/TranslationValidator/Command/ValidateCommand.php @@ -52,9 +52,9 @@ protected function execute(InputInterface $input, OutputInterface $output) $displayNotices = $input->getOption('display-notices'); $safeMode = $input->getOption('safe-mode'); - if (!in_array($phpbbVersion, array('3.2', '3.3'))) + if (!in_array($phpbbVersion, array('3.2', '3.3', '4.0'))) { - throw new \RuntimeException('Invalid phpbb-version, allowed versions: 3.2 and 3.3'); + throw new \RuntimeException('Invalid phpbb-version, allowed versions: 3.2, 3.3, 4.0'); } $output = new Output($output, $debug); diff --git a/src/Phpbb/TranslationValidator/Validator/FileValidator.php b/src/Phpbb/TranslationValidator/Validator/FileValidator.php index 89d3ba4..8487c68 100644 --- a/src/Phpbb/TranslationValidator/Validator/FileValidator.php +++ b/src/Phpbb/TranslationValidator/Validator/FileValidator.php @@ -14,6 +14,9 @@ class FileValidator { + // Latest version of the English language composer.json + const GITHUB_COMPOSER_JSON_SOURCE = 'https://raw.githubusercontent.com/phpbb/phpbb/master/phpBB/language/en/composer.json'; + /** @var string */ protected $direction; /** @var string */ @@ -237,6 +240,16 @@ public function setSafeMode($safeMode) public function validate($sourceFile, $originFile) { $this->validateLineEndings($originFile); + + // phpBB 4.0 specific checks + if ($this->phpbbVersion == '4.0') + { + if ($originFile == $this->originLanguagePath . 'composer.json') + { + $this->validateComposerJson($originFile); + } + } + if (substr($originFile, -4) === '.php') { $this->validateDefinedInPhpbb($originFile); @@ -381,6 +394,51 @@ public function validateLangFile($sourceFile, $originFile) } } + /** + * For v4.0 only, validate the language composer.json file + * @param $originFile + */ + public function validateComposerJson($originFile) + { + // Get the most up to date version of the JSON schema online + $sourceComposerJson = json_decode(file_get_contents(self::GITHUB_COMPOSER_JSON_SOURCE), true); + + // User composer file + $userComposerJson = json_decode(file_get_contents($this->originPath . '/' . $originFile), true); + + if (array_key_exists('extra', $userComposerJson)) + { + // Intersecting the arrays gives us the common/shared keys. So if they are identical, the number of results should + // match the number of keys in the source file. + if (count(array_intersect_key($userComposerJson['extra'], $sourceComposerJson['extra'])) != count(array_keys($sourceComposerJson['extra']))) + { + // Missing extra sub-values + $this->output->addMessage(Output::ERROR, 'composer.json must contain information under the "extra" key which is currently missing.', $originFile); + } + + else + { + // Keys are present, check the version matches + $phpbbVersionMatches = $sourceComposerJson['extra']['phpbb-version'] == $userComposerJson['extra']['phpbb-version']; + + // Check that 'version' and 'phpbb-version' are matching as well + $languageVersionMatches = isset($userComposerJson['version']) && $userComposerJson['version'] == $sourceComposerJson['extra']['phpbb-version']; + + if (!$phpbbVersionMatches || !$languageVersionMatches) + { + // Version mismatch + $this->output->addMessage(Output::ERROR, sprintf('composer.json contains a version mismatch. (should be %s)', $sourceComposerJson['extra']['phpbb-version']), $originFile); + } + } + } + + else + { + // Extra key required + $this->output->addMessage(Output::ERROR, 'composer.json must contain an "extra" key with the language information.', $originFile); + } + } + /** * Check that the reCaptcha key provided is allowed * @param $originFile