diff options
author | sgiehl <stefan@piwik.org> | 2015-04-17 01:08:12 +0300 |
---|---|---|
committer | sgiehl <stefan@piwik.org> | 2015-04-21 22:41:20 +0300 |
commit | 1ae88c88edd63fcaa49d17d22afec895d809a456 (patch) | |
tree | bab472f4eacd5cf5b3395fb4c902e3622923f522 /plugins/LanguagesManager | |
parent | 3c8fd3186e17fa18a95fbfb18bb546839bc5c937 (diff) |
refs #7567 - use transifex statistics api and git modification time to determine which resources needs to be updated
Diffstat (limited to 'plugins/LanguagesManager')
-rw-r--r-- | plugins/LanguagesManager/Commands/FetchFromTransifex.php | 23 | ||||
-rw-r--r-- | plugins/LanguagesManager/Commands/Update.php | 26 |
2 files changed, 48 insertions, 1 deletions
diff --git a/plugins/LanguagesManager/Commands/FetchFromTransifex.php b/plugins/LanguagesManager/Commands/FetchFromTransifex.php index 688e2ad8ea..fd6f865dfe 100644 --- a/plugins/LanguagesManager/Commands/FetchFromTransifex.php +++ b/plugins/LanguagesManager/Commands/FetchFromTransifex.php @@ -10,6 +10,7 @@ namespace Piwik\Plugins\LanguagesManager\Commands; use Piwik\Container\StaticContainer; +use Piwik\Plugins\LanguagesManager\API as LanguagesManagerApi; use Piwik\Translation\Transifex\API; use Symfony\Component\Console\Helper\ProgressHelper; use Symfony\Component\Console\Input\InputInterface; @@ -30,6 +31,7 @@ class FetchFromTransifex extends TranslationBase ->setDescription('Fetches translations files from Transifex to ' . $path) ->addOption('username', 'u', InputOption::VALUE_OPTIONAL, 'Transifex username') ->addOption('password', 'p', InputOption::VALUE_OPTIONAL, 'Transifex password') + ->addOption('lastupdate', 'l', InputOption::VALUE_OPTIONAL, 'Last time update ran', time()-30*24*3600) ->addOption('plugin', 'r', InputOption::VALUE_OPTIONAL, 'Plugin to update'); } @@ -38,6 +40,7 @@ class FetchFromTransifex extends TranslationBase $username = $input->getOption('username'); $password = $input->getOption('password'); $plugin = $input->getOption('plugin'); + $lastUpdate = $input->getOption('lastupdate'); $resource = 'piwik-'. ($plugin ? 'plugin-'.strtolower($plugin) : 'base'); @@ -58,17 +61,35 @@ class FetchFromTransifex extends TranslationBase if (!empty($plugin)) { $languages = array_filter($languages, function($language) { - return \Piwik\Plugins\LanguagesManager\API::getInstance()->isLanguageAvailable(str_replace('_', '-', strtolower($language))); + return LanguagesManagerApi::getInstance()->isLanguageAvailable(str_replace('_', '-', strtolower($language))); }); } + $availableLanguages = LanguagesManagerApi::getInstance()->getAvailableLanguageNames(); + + $languageCodes = array(); + foreach ($availableLanguages as $languageInfo) { + $languageCodes[] = $languageInfo['code']; + } + /** @var ProgressHelper $progress */ $progress = $this->getHelperSet()->get('progress'); $progress->start($output, count($languages)); + $statistics = $transifexApi->getStatistics($resource); + foreach ($languages as $language) { try { + // if we have modification date given from statistics api compare it with given last update time to ignore not update resources + if (LanguagesManagerApi::getInstance()->isLanguageAvailable(str_replace('_', '-', strtolower($language))) && isset($statistics->$language)) { + $lastupdated = strtotime($statistics->$language->last_update); + if ($lastUpdate > $lastupdated) { + $progress->advance(); + continue; + } + } + $translations = $transifexApi->getTranslations($resource, $language, true); file_put_contents($this->getDownloadPath() . DIRECTORY_SEPARATOR . str_replace('_', '-', strtolower($language)) . '.json', $translations); } catch (\Exception $e) { diff --git a/plugins/LanguagesManager/Commands/Update.php b/plugins/LanguagesManager/Commands/Update.php index 883a214b14..ea208a2743 100644 --- a/plugins/LanguagesManager/Commands/Update.php +++ b/plugins/LanguagesManager/Commands/Update.php @@ -61,11 +61,18 @@ class Update extends TranslationBase foreach ($pluginList as $plugin) { + $output->writeln(""); + // fetch base or specific plugin $this->fetchTranslations($input, $output, $plugin); $files = _glob(FetchFromTransifex::getDownloadPath() . DIRECTORY_SEPARATOR . '*.json'); + if (count($files) == 0) { + $output->writeln("No translation updates available! Skipped."); + continue; + } + /** @var ProgressHelper $progress */ $progress = $this->getHelperSet()->get('progress'); @@ -149,6 +156,20 @@ class Update extends TranslationBase protected function fetchTranslations($input, $output, $plugin) { + $lastModDate = 0; + try { + // try to find the language file (of given plugin) with the newest modification date in git log + $path = ($plugin ? 'plugins/' . $plugin . '/' : '') . 'lang'; + $files = explode("\n", trim(shell_exec('git ls-tree -r --name-only HEAD ' . $path))); + + foreach ($files as $file) { + $fileModDate = shell_exec('git log -1 --format="%at" -- ' . $file); + if (basename($file) != 'en.json' && ($lastModDate != 0 || $fileModDate > $lastModDate)) { + $lastModDate = $fileModDate; + } + } + } catch (\Exception $e) {} + $command = $this->getApplication()->find('translations:fetch'); $arguments = array( 'command' => 'translations:fetch', @@ -156,6 +177,11 @@ class Update extends TranslationBase '--password' => $input->getOption('password'), '--plugin' => $plugin ); + + if ($lastModDate != 0) { + $arguments['--lastupdate'] = $lastModDate; + } + $inputObject = new ArrayInput($arguments); $inputObject->setInteractive($input->isInteractive()); $command->run($inputObject, $output); |