add icons for Character groups
This commit is contained in:
commit
2d9a41a5fe
3461 changed files with 594457 additions and 0 deletions
220
www/analytics/plugins/LanguagesManager/Commands/CreatePull.php
Normal file
220
www/analytics/plugins/LanguagesManager/Commands/CreatePull.php
Normal file
|
|
@ -0,0 +1,220 @@
|
|||
<?php
|
||||
/**
|
||||
* Piwik - Open source web analytics
|
||||
*
|
||||
* @link http://piwik.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Piwik\Plugins\LanguagesManager\Commands;
|
||||
|
||||
use Piwik\Plugin\ConsoleCommand;
|
||||
use Piwik\Plugins\LanguagesManager\API;
|
||||
use Piwik\Plugins\LanguagesManager\Commands\Update;
|
||||
use Symfony\Component\Console\Input\ArrayInput;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
*/
|
||||
class CreatePull extends ConsoleCommand
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('translations:createpull')
|
||||
->setDescription('Updates translation files')
|
||||
->addOption('username', 'u', InputOption::VALUE_OPTIONAL, 'oTrance username')
|
||||
->addOption('password', 'p', InputOption::VALUE_OPTIONAL, 'oTrance password')
|
||||
->addOption('plugin', 'P', InputOption::VALUE_OPTIONAL, 'optional name of plugin to update translations for');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$changes = shell_exec('git status --porcelain -uno');
|
||||
|
||||
if (!empty($changes)) {
|
||||
|
||||
$output->writeln("You have uncommited changes. Creating pull request is only available with a clean working directory");
|
||||
return;
|
||||
}
|
||||
|
||||
$unpushedCommits = shell_exec('git log origin/master..HEAD');
|
||||
|
||||
if (!empty($unpushedCommits)) {
|
||||
|
||||
$output->writeln("You have unpushed commits. Creating pull request is only available with a clean working directory");
|
||||
return;
|
||||
}
|
||||
|
||||
chdir(PIWIK_DOCUMENT_ROOT);
|
||||
|
||||
shell_exec('
|
||||
git checkout master > /dev/null 2>&1
|
||||
git pull > /dev/null 2>&1
|
||||
git submodule init > /dev/null 2>&1
|
||||
git submodule update > /dev/null 2>&1
|
||||
');
|
||||
|
||||
$plugin = $input->getOption('plugin');
|
||||
if (!empty($plugin)) {
|
||||
|
||||
chdir(PIWIK_DOCUMENT_ROOT.DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.$plugin);
|
||||
shell_exec('
|
||||
git checkout master > /dev/null 2>&1
|
||||
git pull > /dev/null 2>&1
|
||||
');
|
||||
}
|
||||
|
||||
// check if branch exists localy and track it if not
|
||||
$branch = shell_exec('git branch | grep translationupdates');
|
||||
|
||||
if (empty($branch)) {
|
||||
|
||||
shell_exec('git checkout -b translationupdates origin/translationupdates');
|
||||
}
|
||||
|
||||
// switch to branch and update it to latest master
|
||||
shell_exec('
|
||||
git checkout translationupdates > /dev/null 2>&1
|
||||
git merge master > /dev/null 2>&1
|
||||
git push origin translationupdates > /dev/null 2>&1
|
||||
');
|
||||
|
||||
// update translation files
|
||||
$command = $this->getApplication()->find('translations:update');
|
||||
$arguments = array(
|
||||
'command' => 'translations:update',
|
||||
'--username' => $input->getOption('username'),
|
||||
'--password' => $input->getOption('password'),
|
||||
'--plugin' => $plugin
|
||||
);
|
||||
$inputObject = new ArrayInput($arguments);
|
||||
$inputObject->setInteractive($input->isInteractive());
|
||||
$command->run($inputObject, $output);
|
||||
|
||||
shell_exec('git add lang/. > /dev/null 2>&1');
|
||||
|
||||
if (empty($plugin)) {
|
||||
foreach (Update::getPluginsInCore() AS $pluginName) {
|
||||
shell_exec(sprintf('git add plugins/%s/lang/. > /dev/null 2>&1', $pluginName));
|
||||
}
|
||||
}
|
||||
|
||||
$changes = shell_exec('git status --porcelain -uno');
|
||||
|
||||
if (empty($changes)) {
|
||||
|
||||
$output->writeln("Nothing changed. Everything is already up to date.");
|
||||
shell_exec('git checkout master > /dev/null 2>&1');
|
||||
return;
|
||||
}
|
||||
|
||||
API::unsetInstance(); // reset languagemanager api (to force refresh of data)
|
||||
|
||||
$stats = shell_exec('git diff --numstat HEAD');
|
||||
|
||||
preg_match_all('/([0-9]+)\t([0-9]+)\t[a-zA-Z\/]*lang\/([a-z]{2,3})\.json/', $stats, $lineChanges);
|
||||
|
||||
$addedLinesSum = 0;
|
||||
if (!empty($lineChanges[1])) {
|
||||
$addedLinesSum = array_sum($lineChanges[1]);
|
||||
}
|
||||
|
||||
$linesSumByLang = array();
|
||||
for($i=0; $i<count($lineChanges[0]); $i++) {
|
||||
@$linesSumByLang[$lineChanges[3][$i]] += $lineChanges[1][$i];
|
||||
}
|
||||
|
||||
preg_match_all('/M [a-zA-Z\/]*lang\/([a-z]{2,3})\.json/', $changes, $modifiedFiles);
|
||||
preg_match_all('/A [a-zA-Z\/]*lang\/([a-z]{2,3})\.json/', $changes, $addedFiles);
|
||||
|
||||
$messages = array();
|
||||
|
||||
$languageCodesTouched = array();
|
||||
if (!empty($addedFiles[1])) {
|
||||
foreach ($addedFiles[1] AS $addedFile) {
|
||||
$languageInfo = $this->getLanguageInfoByIsoCode($addedFile);
|
||||
$messages[$addedFile] = sprintf('- Added %s (%s changes / %s translated)\n', $languageInfo['english_name'], $linesSumByLang[$addedFile], $languageInfo['percentage_complete']);
|
||||
}
|
||||
$languageCodesTouched = array_merge($languageCodesTouched, $addedFiles[1]);
|
||||
}
|
||||
|
||||
if (!empty($modifiedFiles[1])) {
|
||||
foreach ($modifiedFiles[1] AS $modifiedFile) {
|
||||
$languageInfo = $this->getLanguageInfoByIsoCode($modifiedFile);
|
||||
$messages[$modifiedFile] = sprintf('- Updated %s (%s changes / %s translated)\n', $languageInfo['english_name'], $linesSumByLang[$modifiedFile], $languageInfo['percentage_complete']);
|
||||
}
|
||||
$languageCodesTouched = $modifiedFiles[1];
|
||||
}
|
||||
|
||||
$message = implode('', $messages);
|
||||
|
||||
$languageCodesTouched = array_unique($languageCodesTouched, SORT_REGULAR);
|
||||
|
||||
$title = sprintf(
|
||||
'Updated %s strings in %u languages (%s)',
|
||||
$addedLinesSum,
|
||||
count($languageCodesTouched),
|
||||
implode(', ', $languageCodesTouched)
|
||||
);
|
||||
|
||||
shell_exec('git commit -m "language update ${pluginName} refs #3430"');
|
||||
shell_exec('git push');
|
||||
shell_exec('git checkout master > /dev/null 2>&1');
|
||||
|
||||
$this->createPullRequest($output, $title, $message);
|
||||
}
|
||||
|
||||
private function getLanguageInfoByIsoCode($isoCode)
|
||||
{
|
||||
$languages = API::getInstance()->getAvailableLanguagesInfo();
|
||||
foreach ($languages AS $languageInfo) {
|
||||
if ($languageInfo['code'] == $isoCode) {
|
||||
return $languageInfo;
|
||||
}
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
private function createPullRequest(OutputInterface $output, $title, $message)
|
||||
{
|
||||
$dialog = $this->getHelperSet()->get('dialog');
|
||||
|
||||
while (true) {
|
||||
|
||||
$username = $dialog->ask($output, 'Please provide your github username (to create a pull request using Github API): ');
|
||||
|
||||
$returnCode = shell_exec('curl \
|
||||
-X POST \
|
||||
-k \
|
||||
--silent \
|
||||
--write-out %{http_code} \
|
||||
--stderr /dev/null \
|
||||
-o /dev/null \
|
||||
-u '.$username.' \
|
||||
--data "{\"title\":\"[automatic translation update] '.$title.'\",\"body\":\"'.$message.'\",\"head\":\"translationupdates\",\"base\":\"master\"}" \
|
||||
-H "Accept: application/json" \
|
||||
https://api.github.com/repos/piwik/piwik/pulls');
|
||||
|
||||
switch ($returnCode) {
|
||||
case 401:
|
||||
$output->writeln("Pull request failed. Bad credentials... Please try again");
|
||||
continue;
|
||||
|
||||
case 422:
|
||||
$output->writeln("Pull request failed. Unprocessable Entity. Maybe a pull request was already created before.");
|
||||
return;
|
||||
|
||||
case 201:
|
||||
case 200:
|
||||
$output->writeln("Pull request successfully created.");
|
||||
return;
|
||||
|
||||
default:
|
||||
$output->writeln("Pull request failed... Please try again");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,172 @@
|
|||
<?php
|
||||
/**
|
||||
* Piwik - Open source web analytics
|
||||
*
|
||||
* @link http://piwik.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Piwik\Plugins\LanguagesManager\Commands;
|
||||
|
||||
use Piwik\Plugin\ConsoleCommand;
|
||||
use Piwik\Unzip;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
*/
|
||||
class FetchFromOTrance extends ConsoleCommand
|
||||
{
|
||||
const DOWNLOADPATH = 'tmp/oTrance';
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('translations:fetch')
|
||||
->setDescription('Fetches translations files from oTrance to '.self::DOWNLOADPATH)
|
||||
->addOption('username', 'u', InputOption::VALUE_OPTIONAL, 'oTrance username')
|
||||
->addOption('password', 'p', InputOption::VALUE_OPTIONAL, 'oTrance password');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$output->writeln("Starting to fetch latest language pack");
|
||||
|
||||
$dialog = $this->getHelperSet()->get('dialog');
|
||||
|
||||
$cookieFile = self::getDownloadPath() . DIRECTORY_SEPARATOR . 'cookie.txt';
|
||||
@unlink($cookieFile);
|
||||
|
||||
$username = $input->getOption('username');
|
||||
$password = $input->getOption('password');
|
||||
|
||||
while (!file_exists($cookieFile)) {
|
||||
if (empty($username)) {
|
||||
$username = $dialog->ask($output, 'What is your oTrance username? ');
|
||||
}
|
||||
|
||||
if (empty($password)) {
|
||||
$password = $dialog->askHiddenResponse($output, 'What is your oTrance password? ');
|
||||
}
|
||||
|
||||
// send login request to oTrance and save the login cookie
|
||||
$curl = curl_init('http://translations.piwik.org/public/index/login');
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, sprintf("user=%s&pass=%s&autologin=1", $username, $password));
|
||||
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookieFile);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_exec($curl);
|
||||
curl_close($curl);
|
||||
|
||||
if (strpos(file_get_contents($cookieFile), 'oTranCe_autologin') !== false) {
|
||||
break;
|
||||
}
|
||||
|
||||
$username = null;
|
||||
$password = null;
|
||||
@unlink($cookieFile);
|
||||
$output->writeln("Invalid oTrance credentials. Please try again...");
|
||||
}
|
||||
|
||||
// send request to create a new download package using the cookie file
|
||||
$createNewPackage = true;
|
||||
if ($input->isInteractive()) {
|
||||
$createNewPackage = $dialog->askConfirmation($output, 'Shall we create a new language pack? ');
|
||||
}
|
||||
|
||||
if ($createNewPackage) {
|
||||
|
||||
$curl = curl_init('http://translations.piwik.org/public/export/update.all');
|
||||
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookieFile);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_exec($curl);
|
||||
curl_close($curl);
|
||||
}
|
||||
|
||||
// request download page to search for available packages
|
||||
$curl = curl_init('http://translations.piwik.org/public/downloads/');
|
||||
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookieFile);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||
$response = curl_exec($curl);
|
||||
curl_close($curl);
|
||||
|
||||
preg_match_all('/language\_pack\-[0-9]{8}\-[0-9]{6}\.tar\.gz/i', $response, $matches);
|
||||
|
||||
if (empty($matches[0])) {
|
||||
|
||||
$output->writeln("No packages found for download. Please try again.");
|
||||
return;
|
||||
}
|
||||
|
||||
$downloadPackage = array_shift($matches[0]);
|
||||
|
||||
$continueWithPackage = true;
|
||||
if ($input->isInteractive()) {
|
||||
$continueWithPackage = $dialog->askConfirmation($output, "Found language pack $downloadPackage. Proceed? ");
|
||||
}
|
||||
|
||||
if (!$continueWithPackage) {
|
||||
|
||||
$output->writeln('Aborted.');
|
||||
return;
|
||||
}
|
||||
|
||||
// download language pack
|
||||
$packageHandle = fopen(self::getDownloadPath() . DIRECTORY_SEPARATOR . 'language_pack.tar.gz', 'w');
|
||||
$curl = curl_init('http://translations.piwik.org/public/downloads/download/file/'.$downloadPackage);
|
||||
curl_setopt($curl, CURLOPT_COOKIEFILE, self::getDownloadPath() . DIRECTORY_SEPARATOR . 'cookie.txt');
|
||||
curl_setopt($curl, CURLOPT_FILE, $packageHandle);
|
||||
curl_exec($curl);
|
||||
curl_close($curl);
|
||||
|
||||
@unlink($cookieFile);
|
||||
|
||||
$output->writeln("Extracting package...");
|
||||
|
||||
$unzipper = Unzip::factory('tar.gz', self::getDownloadPath() . DIRECTORY_SEPARATOR . 'language_pack.tar.gz');
|
||||
$unzipper->extract(self::getDownloadPath());
|
||||
|
||||
@unlink(self::getDownloadPath() . DIRECTORY_SEPARATOR . 'en.php');
|
||||
@unlink(self::getDownloadPath() . DIRECTORY_SEPARATOR . 'language_pack.tar.gz');
|
||||
|
||||
$filesToConvert = _glob(self::getDownloadPath() . DIRECTORY_SEPARATOR . '*.php');
|
||||
|
||||
$output->writeln("Converting downloaded php files to json");
|
||||
|
||||
$progress = $this->getHelperSet()->get('progress');
|
||||
|
||||
$progress->start($output, count($filesToConvert));
|
||||
foreach ($filesToConvert AS $filename) {
|
||||
|
||||
require_once $filename;
|
||||
$basename = explode(".", basename($filename));
|
||||
$nested = array();
|
||||
foreach ($translations as $key => $value) {
|
||||
list($plugin, $nkey) = explode("_", $key, 2);
|
||||
$nested[$plugin][$nkey] = $value;
|
||||
}
|
||||
$translations = $nested;
|
||||
$data = json_encode($translations, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
|
||||
$newFile = sprintf("%s/%s.json", self::getDownloadPath(), $basename[0]);
|
||||
file_put_contents($newFile, $data);
|
||||
@unlink($filename);
|
||||
|
||||
$progress->advance();
|
||||
}
|
||||
|
||||
$progress->finish();
|
||||
|
||||
$output->writeln("Finished fetching new language files from oTrance");
|
||||
}
|
||||
|
||||
public static function getDownloadPath() {
|
||||
|
||||
$path = PIWIK_DOCUMENT_ROOT . DIRECTORY_SEPARATOR . self::DOWNLOADPATH;
|
||||
|
||||
if (!is_dir($path)) {
|
||||
mkdir($path);
|
||||
}
|
||||
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
/**
|
||||
* Piwik - Open source web analytics
|
||||
*
|
||||
* @link http://piwik.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Piwik\Plugins\LanguagesManager\Commands;
|
||||
|
||||
use Piwik\Plugin\ConsoleCommand;
|
||||
use Piwik\Plugins\LanguagesManager\API;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
*/
|
||||
class LanguageCodes extends ConsoleCommand
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('translations:languagecodes')
|
||||
->setDescription('Shows available language codes');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$languages = API::getInstance()->getAvailableLanguageNames();
|
||||
|
||||
$languageCodes = array();
|
||||
foreach ($languages AS $languageInfo) {
|
||||
$languageCodes[] = $languageInfo['code'];
|
||||
}
|
||||
|
||||
sort($languageCodes);
|
||||
|
||||
$output->writeln("Currently available languages:");
|
||||
$output->writeln(implode("\n", $languageCodes));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
/**
|
||||
* Piwik - Open source web analytics
|
||||
*
|
||||
* @link http://piwik.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Piwik\Plugins\LanguagesManager\Commands;
|
||||
|
||||
use Piwik\Plugin\ConsoleCommand;
|
||||
use Piwik\Plugins\LanguagesManager\API;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
*/
|
||||
class LanguageNames extends ConsoleCommand
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('translations:languagenames')
|
||||
->setDescription('Shows available language names');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$languages = API::getInstance()->getAvailableLanguageNames();
|
||||
|
||||
$languageNames = array();
|
||||
foreach ($languages AS $languageInfo) {
|
||||
$languageNames[] = $languageInfo['english_name'];
|
||||
}
|
||||
|
||||
sort($languageNames);
|
||||
|
||||
$output->writeln("Currently available languages:");
|
||||
$output->writeln(implode("\n", $languageNames));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
/**
|
||||
* Piwik - Open source web analytics
|
||||
*
|
||||
* @link http://piwik.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Piwik\Plugins\LanguagesManager\Commands;
|
||||
|
||||
use Piwik\Plugin\ConsoleCommand;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
*/
|
||||
class PluginsWithTranslations extends ConsoleCommand
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('translations:plugins')
|
||||
->setDescription('Shows all plugins that have own translation files');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$output->writeln("Following plugins contain their own translation files:");
|
||||
|
||||
$pluginFiles = glob(sprintf('%s/plugins/*/lang/en.json', PIWIK_INCLUDE_PATH));
|
||||
$pluginFiles = array_map(function($elem){
|
||||
return str_replace(array(sprintf('%s/plugins/', PIWIK_INCLUDE_PATH), '/lang/en.json'), '', $elem);
|
||||
}, $pluginFiles);
|
||||
|
||||
$output->writeln(join("\n", $pluginFiles));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,105 @@
|
|||
<?php
|
||||
/**
|
||||
* Piwik - Open source web analytics
|
||||
*
|
||||
* @link http://piwik.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Piwik\Plugins\LanguagesManager\Commands;
|
||||
|
||||
use Piwik\Plugin\ConsoleCommand;
|
||||
use Piwik\Plugins\LanguagesManager\API;
|
||||
use Piwik\Translate\Filter\ByBaseTranslations;
|
||||
use Piwik\Translate\Filter\ByParameterCount;
|
||||
use Piwik\Translate\Filter\EmptyTranslations;
|
||||
use Piwik\Translate\Filter\EncodedEntities;
|
||||
use Piwik\Translate\Filter\UnnecassaryWhitespaces;
|
||||
use Piwik\Translate\Validate\CoreTranslations;
|
||||
use Piwik\Translate\Validate\NoScripts;
|
||||
use Piwik\Translate\Writer;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
*/
|
||||
class SetTranslations extends ConsoleCommand
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('translations:set')
|
||||
->setDescription('Sets new translations for a given language')
|
||||
->addOption('code', 'c', InputOption::VALUE_REQUIRED, 'code of the language to set translations for')
|
||||
->addOption('file', 'f', InputOption::VALUE_REQUIRED, 'json file to load new translations from')
|
||||
->addOption('plugin', 'pl', InputOption::VALUE_OPTIONAL, 'optional name of plugin to set translations for');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$dialog = $this->getHelperSet()->get('dialog');
|
||||
|
||||
$languageCode = $input->getOption('code');
|
||||
$filename = $input->getOption('file');
|
||||
|
||||
$languageCodes = API::getInstance()->getAvailableLanguages();
|
||||
|
||||
if (empty($languageCode) || !in_array($languageCode, $languageCodes)) {
|
||||
$languageCode = $dialog->askAndValidate($output, 'Please provide a valid language code: ', function ($code) use ($languageCodes) {
|
||||
if (!in_array($code, array_values($languageCodes))) {
|
||||
throw new \InvalidArgumentException(sprintf('Language code "%s" is invalid.', $code));
|
||||
}
|
||||
|
||||
return $code;
|
||||
});
|
||||
}
|
||||
|
||||
if (empty($filename) || !file_exists($filename)) {
|
||||
$filename = $dialog->askAndValidate($output, 'Please provide a file to load translations from: ', function ($file) {
|
||||
if (!file_exists($file)) {
|
||||
throw new \InvalidArgumentException(sprintf('File "%s" does not exist.', $file));
|
||||
}
|
||||
|
||||
return $file;
|
||||
});
|
||||
}
|
||||
|
||||
$output->writeln("Starting to import data from '$filename' to language '$languageCode'");
|
||||
|
||||
$plugin = $input->getOption('plugin');
|
||||
$translationWriter = new Writer($languageCode, $plugin);
|
||||
|
||||
$baseTranslations = $translationWriter->getTranslations("en");
|
||||
|
||||
$translationWriter->addValidator(new NoScripts());
|
||||
if (empty($plugin)) {
|
||||
$translationWriter->addValidator(new CoreTranslations($baseTranslations));
|
||||
}
|
||||
|
||||
$translationWriter->addFilter(new ByBaseTranslations($baseTranslations));
|
||||
$translationWriter->addFilter(new EmptyTranslations());
|
||||
$translationWriter->addFilter(new ByParameterCount($baseTranslations));
|
||||
$translationWriter->addFilter(new UnnecassaryWhitespaces($baseTranslations));
|
||||
$translationWriter->addFilter(new EncodedEntities());
|
||||
|
||||
$translationData = file_get_contents($filename);
|
||||
$translations = json_decode($translationData, true);
|
||||
|
||||
$translationWriter->setTranslations($translations);
|
||||
|
||||
if (!$translationWriter->isValid()) {
|
||||
$output->writeln("Failed setting translations:" . $translationWriter->getValidationMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$translationWriter->hasTranslations()) {
|
||||
$output->writeln("No translations available");
|
||||
return;
|
||||
}
|
||||
|
||||
$translationWriter->save();
|
||||
|
||||
$output->writeln("Finished.");
|
||||
}
|
||||
}
|
||||
162
www/analytics/plugins/LanguagesManager/Commands/Update.php
Normal file
162
www/analytics/plugins/LanguagesManager/Commands/Update.php
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
<?php
|
||||
/**
|
||||
* Piwik - Open source web analytics
|
||||
*
|
||||
* @link http://piwik.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Piwik\Plugins\LanguagesManager\Commands;
|
||||
|
||||
use Piwik\Plugin\ConsoleCommand;
|
||||
use Piwik\Plugins\LanguagesManager\API;
|
||||
use Symfony\Component\Console\Input\ArrayInput;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\NullOutput;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
*/
|
||||
class Update extends ConsoleCommand
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('translations:update')
|
||||
->setDescription('Updates translation files')
|
||||
->addOption('username', 'u', InputOption::VALUE_OPTIONAL, 'oTrance username')
|
||||
->addOption('password', 'p', InputOption::VALUE_OPTIONAL, 'oTrance password')
|
||||
->addOption('plugin', 'P', InputOption::VALUE_OPTIONAL, 'optional name of plugin to update translations for');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$dialog = $this->getHelperSet()->get('dialog');
|
||||
|
||||
$command = $this->getApplication()->find('translations:fetch');
|
||||
$arguments = array(
|
||||
'command' => 'translations:fetch',
|
||||
'--username' => $input->getOption('username'),
|
||||
'--password' => $input->getOption('password')
|
||||
);
|
||||
$inputObject = new ArrayInput($arguments);
|
||||
$inputObject->setInteractive($input->isInteractive());
|
||||
$command->run($inputObject, $output);
|
||||
|
||||
$languages = API::getInstance()->getAvailableLanguageNames();
|
||||
|
||||
$languageCodes = array();
|
||||
foreach ($languages AS $languageInfo) {
|
||||
$languageCodes[] = $languageInfo['code'];
|
||||
}
|
||||
|
||||
$plugin = $input->getOption('plugin');
|
||||
|
||||
$files = _glob(FetchFromOTrance::getDownloadPath() . DIRECTORY_SEPARATOR . '*.json');
|
||||
|
||||
$output->writeln("Starting to import new language files");
|
||||
|
||||
if (!$input->isInteractive()) {
|
||||
$output->writeln("(!) Non interactive mode: New languages will be skipped");
|
||||
}
|
||||
|
||||
$progress = $this->getHelperSet()->get('progress');
|
||||
|
||||
$progress->start($output, count($files));
|
||||
|
||||
foreach ($files AS $filename) {
|
||||
|
||||
$progress->advance();
|
||||
|
||||
$code = basename($filename, '.json');
|
||||
|
||||
if (!in_array($code, $languageCodes)) {
|
||||
|
||||
if (!empty($plugin)) {
|
||||
|
||||
continue; # never create a new language for plugin only
|
||||
}
|
||||
|
||||
$createNewFile = false;
|
||||
if ($input->isInteractive()) {
|
||||
$createNewFile = $dialog->askConfirmation($output, "\nLanguage $code does not exist. Should it be added? ", false);
|
||||
}
|
||||
|
||||
if (!$createNewFile) {
|
||||
|
||||
continue; # do not create a new file for the language
|
||||
}
|
||||
|
||||
@touch(PIWIK_DOCUMENT_ROOT . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR . $code . '.json');
|
||||
API::unsetInstance(); // unset language manager instance, so valid names are refetched
|
||||
}
|
||||
|
||||
$command = $this->getApplication()->find('translations:set');
|
||||
$arguments = array(
|
||||
'command' => 'translations:set',
|
||||
'--code' => $code,
|
||||
'--file' => $filename,
|
||||
'--plugin' => $plugin
|
||||
);
|
||||
$inputObject = new ArrayInput($arguments);
|
||||
$inputObject->setInteractive($input->isInteractive());
|
||||
$command->run($inputObject, new NullOutput());
|
||||
|
||||
// update core modules that aren't in their own repo
|
||||
if (empty($plugin)) {
|
||||
|
||||
foreach (self::getPluginsInCore() AS $pluginName) {
|
||||
|
||||
// update translation files
|
||||
$command = $this->getApplication()->find('translations:set');
|
||||
$arguments = array(
|
||||
'command' => 'translations:set',
|
||||
'--code' => $code,
|
||||
'--file' => $filename,
|
||||
'--plugin' => $pluginName
|
||||
);
|
||||
$inputObject = new ArrayInput($arguments);
|
||||
$inputObject->setInteractive($input->isInteractive());
|
||||
$command->run($inputObject, new NullOutput());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$progress->finish();
|
||||
$output->writeln("Finished.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all plugins having their own translations that are bundled in core
|
||||
* @return array
|
||||
*/
|
||||
public static function getPluginsInCore()
|
||||
{
|
||||
static $pluginsInCore;
|
||||
|
||||
if (!empty($pluginsInCore)) {
|
||||
return $pluginsInCore;
|
||||
}
|
||||
|
||||
$submodules = shell_exec('git submodule status');
|
||||
preg_match_all('/plugins\/([a-zA-z]+) /', $submodules, $matches);
|
||||
$submodulePlugins = $matches[1];
|
||||
|
||||
// ignore complete new plugins aswell
|
||||
$changes = shell_exec('git status');
|
||||
preg_match_all('/plugins\/([a-zA-z]+)\/\n/', $changes, $matches);
|
||||
$newPlugins = $matches[1];
|
||||
|
||||
$pluginsNotInCore = array_merge($submodulePlugins, $newPlugins);
|
||||
|
||||
$pluginsWithTranslations = glob(sprintf('%s/plugins/*/lang/en.json', PIWIK_INCLUDE_PATH));
|
||||
$pluginsWithTranslations = array_map(function($elem){
|
||||
return str_replace(array(sprintf('%s/plugins/', PIWIK_INCLUDE_PATH), '/lang/en.json'), '', $elem);
|
||||
}, $pluginsWithTranslations);
|
||||
|
||||
$pluginsInCore = array_diff($pluginsWithTranslations, $pluginsNotInCore);
|
||||
|
||||
return $pluginsInCore;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue