Skip to content

Commit cff143b

Browse files
authored
Merge pull request #70 from battye/issue/39
Prompt to download Viglink language files plus new command to put them into place
2 parents 078c97b + 1d02210 commit cff143b

File tree

4 files changed

+144
-1
lines changed

4 files changed

+144
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ There are more arguments that can be supplied. For example, suppose you wanted t
3535

3636
The `--safe-mode` flag indicates that you want to parse files instead of directly including them. This is useful if you want to run validations on a web server.
3737

38+
If you are missing the English language files for the official Viglink extension, they can be easily donwloaded using this command:
39+
40+
php translation.php download --files=phpbb-extensions/viglink --phpbb-version=3.3
41+
3842
## Tests
3943

4044
[![Build Status](https://travis-ci.org/phpbb/phpbb-translation-validator.png?branch=master)](https://travis-ci.org/phpbb/phpbb-translation-validator)

src/Phpbb/TranslationValidator/Cli.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99
namespace Phpbb\TranslationValidator;
1010

11+
use Phpbb\TranslationValidator\Command\DownloadCommand;
1112
use Phpbb\TranslationValidator\Command\ValidateCommand;
1213
use Symfony\Component\Console\Application;
1314

@@ -16,7 +17,10 @@ class Cli extends Application
1617
protected function getDefaultCommands()
1718
{
1819
$commands = parent::getDefaultCommands();
20+
1921
$commands[] = new ValidateCommand();
22+
$commands[] = new DownloadCommand();
23+
2024
return $commands;
2125
}
2226
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
}

src/Phpbb/TranslationValidator/Validator/FileListValidator.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Symfony\Component\Console\Input\InputInterface;
1212
use Symfony\Component\Finder\Finder;
13+
use Phpbb\TranslationValidator\Command\DownloadCommand;
1314
use Phpbb\TranslationValidator\Output\Output;
1415
use Phpbb\TranslationValidator\Output\OutputInterface;
1516

@@ -210,7 +211,16 @@ public function validate()
210211
$level = Output::ERROR;
211212
}
212213

213-
$this->output->addMessage($level, 'Found additional file', $origin_file);
214+
// Treat some official extensions differently
215+
if (strpos($origin_file, DownloadCommand::VIGLINK_PATH) !== false)
216+
{
217+
$this->output->addMessage($level, 'No source file for the official extension found - to download, run: php translation.php download --files=phpbb-extensions/viglink', $origin_file);
218+
}
219+
220+
else
221+
{
222+
$this->output->addMessage($level, 'Found additional file', $origin_file);
223+
}
214224
}
215225

216226
if (substr($origin_file, -14) === '/site_logo.gif')

0 commit comments

Comments
 (0)