|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * @package phpBB Translation Validator |
| 5 | + * @copyright (c) 2021 phpBB Ltd. |
| 6 | + * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 |
| 7 | + * |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Phpbb\TranslationValidator\Command; |
| 11 | + |
| 12 | +use Phpbb\TranslationValidator\Output\Output; |
| 13 | +use Phpbb\TranslationValidator\Output\OutputFormatter; |
| 14 | +use Symfony\Component\Console\Command\Command; |
| 15 | +use Symfony\Component\Console\Input\InputInterface; |
| 16 | +use Symfony\Component\Console\Input\InputOption; |
| 17 | +use Symfony\Component\Console\Output\OutputInterface; |
| 18 | + |
| 19 | +class DownloadCommand extends Command |
| 20 | +{ |
| 21 | + const GITHUB_API_URL = 'https://api.github.com/repos/%s/git/trees/master?recursive=1'; |
| 22 | + const GITHUB_LANGUAGE_EXTRACT = 'language/en/'; |
| 23 | + |
| 24 | + // Supported extensions |
| 25 | + const VIGLINK_EXTENSION = 'phpbb-extensions/viglink'; |
| 26 | + const VIGLINK_PATH = 'ext/phpbb/viglink/'; |
| 27 | + |
| 28 | + protected function configure() |
| 29 | + { |
| 30 | + $this |
| 31 | + ->setName('download') |
| 32 | + ->setDescription('If you are missing important files, this tool can automatically download them for you.') |
| 33 | + ->addOption('files', null, InputOption::VALUE_REQUIRED, 'Which files do you want to download?', 'phpbb-extensions/viglink') |
| 34 | + ->addOption('phpbb-version', null, InputOption::VALUE_OPTIONAL, 'The phpBB version you use to validate against', '3.3'); |
| 35 | + } |
| 36 | + |
| 37 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 38 | + { |
| 39 | + $files = $input->getOption('files'); |
| 40 | + $phpbbVersion = $input->getOption('phpbb-version'); |
| 41 | + |
| 42 | + if (!in_array($files, [self::VIGLINK_EXTENSION])) |
| 43 | + { |
| 44 | + throw new \RuntimeException($files . ' is not supported for automatic download.'); |
| 45 | + } |
| 46 | + |
| 47 | + $output = new Output($output, false); |
| 48 | + $output->setFormatter(new OutputFormatter($output->isDecorated())); |
| 49 | + |
| 50 | + $output->writeln('Downloading ' . $files); |
| 51 | + |
| 52 | + if ($files === self::VIGLINK_EXTENSION) |
| 53 | + { |
| 54 | + // Download Viglink files if they are missing |
| 55 | + $this->downloadViglinkExtensionLanguagesFiles($output, $phpbbVersion); |
| 56 | + } |
| 57 | + |
| 58 | + $output->writeln('Script complete.'); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Download missing Viglink files and store them in phpbb-translation-validator/3.x/en/ext/phpbb/viglink/language/en |
| 63 | + * @param $output |
| 64 | + * @param $phpbbVersion |
| 65 | + */ |
| 66 | + private function downloadViglinkExtensionLanguagesFiles($output, $phpbbVersion) |
| 67 | + { |
| 68 | + $files = $this->readGitHubApiUrl(sprintf(self::GITHUB_API_URL, self::VIGLINK_EXTENSION)); |
| 69 | + |
| 70 | + // Create Viglink folder structure if it doesn't exist |
| 71 | + $directory = __DIR__ . '/../../../../' . $phpbbVersion . '/en/' . self::VIGLINK_PATH; |
| 72 | + |
| 73 | + if (!file_exists($directory)) |
| 74 | + { |
| 75 | + $output->writeln('Viglink directory does not exist, creating now at ' . $directory . self::GITHUB_LANGUAGE_EXTRACT); |
| 76 | + mkdir($directory . self::GITHUB_LANGUAGE_EXTRACT, 0777, true); |
| 77 | + } |
| 78 | + |
| 79 | + foreach ($files['tree'] as $file) |
| 80 | + { |
| 81 | + if (strpos($file['path'], self::GITHUB_LANGUAGE_EXTRACT) !== false) |
| 82 | + { |
| 83 | + $fileToCreate = $directory . '/' . $file['path']; |
| 84 | + |
| 85 | + if (!file_exists($fileToCreate)) |
| 86 | + { |
| 87 | + // This is a file we want |
| 88 | + $languageFile = $this->readGitHubApiUrl($file['url']); |
| 89 | + $languageFileContents = base64_decode($languageFile['content']); |
| 90 | + |
| 91 | + // Save the file |
| 92 | + $output->writeln('Creating missing file now at ' . $fileToCreate); |
| 93 | + file_put_contents($fileToCreate, $languageFileContents); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Return JSON from GitHub API |
| 101 | + * Must supply a user agent otherwise GitHub will reject the request |
| 102 | + * @param $file |
| 103 | + * @return mixed |
| 104 | + */ |
| 105 | + private function readGitHubApiUrl($file) |
| 106 | + { |
| 107 | + $context = stream_context_create([ |
| 108 | + 'http' => [ |
| 109 | + 'method' => 'GET', |
| 110 | + 'header' => [ |
| 111 | + 'User-Agent: phpbb-translation-validator' |
| 112 | + ] |
| 113 | + ] |
| 114 | + ]); |
| 115 | + |
| 116 | + // An unauthenticated request is fine as long is this isn't run over 60 times within an hour |
| 117 | + // More information at: https://docs.github.com/en/rest/guides/getting-started-with-the-rest-api |
| 118 | + $content = json_decode( |
| 119 | + file_get_contents($file, false, $context), |
| 120 | + true |
| 121 | + ); |
| 122 | + |
| 123 | + return $content; |
| 124 | + } |
| 125 | +} |
0 commit comments