Skip to content

Commit

Permalink
Validate 4.0 composer.json, check for the right version. phpbb#71
Browse files Browse the repository at this point in the history
  • Loading branch information
battye committed Oct 20, 2022
1 parent 753b51f commit fbd2740
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Phpbb/TranslationValidator/Command/ValidateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
58 changes: 58 additions & 0 deletions src/Phpbb/TranslationValidator/Validator/FileValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fbd2740

Please sign in to comment.