Skip to content
Extraits de code Groupes Projets
Valider 78abf42c rédigé par sgiehl's avatar sgiehl
Parcourir les fichiers

refs #7567 - improved usage of transifex api

parent 6f1c8004
Branches
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -13,7 +13,7 @@ use Piwik\Exception\AuthenticationFailedException; ...@@ -13,7 +13,7 @@ use Piwik\Exception\AuthenticationFailedException;
class API class API
{ {
protected $apiUrl = 'https://www.transifex.com/api/2/project/piwik/'; protected $apiUrl = 'https://www.transifex.com/api/2/';
protected $username = ''; protected $username = '';
protected $password = ''; protected $password = '';
protected $projectSlug = ''; protected $projectSlug = '';
...@@ -25,21 +25,48 @@ class API ...@@ -25,21 +25,48 @@ class API
$this->projectSlug = $project; $this->projectSlug = $project;
} }
public function getAvailableLanguageCodes() public function getAvailableResources()
{
static $resources;
if (empty($resources)) {
$apiPath = 'project/' . $this->projectSlug . '/resources';
$resources = $this->getApiResults($apiPath);
}
return $resources;
}
public function resourceExists($resource)
{ {
$languageCodes = array(); $resources = $this->getAvailableResources();
$apiData = $this->getApiResults('languages'); foreach ($resources as $res) {
foreach ($apiData as $languageData) { if ($res->slug == $resource) {
$languageCodes[] = $languageData->language_code; return true;
}
} }
return false;
}
public function getAvailableLanguageCodes()
{
static $languageCodes = array();
if (empty($languageCodes)) {
$apiData = $this->getApiResults('project/' . $this->projectSlug . '/languages');
foreach ($apiData as $languageData) {
$languageCodes[] = $languageData->language_code;
}
}
return $languageCodes; return $languageCodes;
} }
public function getTranslations($resource, $language, $raw=false) public function getTranslations($resource, $language, $raw=false)
{ {
$apiPath = 'resource/'.$resource.'/translation/'.$language.'/?mode=onlytranslated&file'; if ($this->resourceExists($resource)) {
return $this->getApiResults($apiPath, $raw); $apiPath = 'project/' . $this->projectSlug . '/resource/' . $resource . '/translation/' . $language . '/?mode=onlytranslated&file';
return $this->getApiResults($apiPath, $raw);
}
return null;
} }
protected function getApiResults($apiPath, $raw=false) protected function getApiResults($apiPath, $raw=false)
...@@ -49,6 +76,7 @@ class API ...@@ -49,6 +76,7 @@ class API
$curl = curl_init($apiUrl); $curl = curl_init($apiUrl);
curl_setopt($curl, CURLOPT_USERPWD, sprintf("%s:%s", $this->username, $this->password)); curl_setopt($curl, CURLOPT_USERPWD, sprintf("%s:%s", $this->username, $this->password));
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_ENCODING, '');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl); $response = curl_exec($curl);
$httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE); $httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
...@@ -57,7 +85,7 @@ class API ...@@ -57,7 +85,7 @@ class API
if ($httpStatus == 401) { if ($httpStatus == 401) {
throw new AuthenticationFailedException(); throw new AuthenticationFailedException();
} else if ($httpStatus != 200) { } else if ($httpStatus != 200) {
throw new Exception(); throw new Exception('Error while getting API results', $httpStatus);
} }
return $raw ? $response : json_decode($response); return $raw ? $response : json_decode($response);
......
...@@ -41,12 +41,27 @@ class FetchFromTransifex extends TranslationBase ...@@ -41,12 +41,27 @@ class FetchFromTransifex extends TranslationBase
$resource = 'piwik-'. ($plugin ? 'plugin-'.strtolower($plugin) : 'base'); $resource = 'piwik-'. ($plugin ? 'plugin-'.strtolower($plugin) : 'base');
$output->writeln("Fetching translations from Transifex for resource $resource");
$transifexApi = new API($username, $password); $transifexApi = new API($username, $password);
// remove all existing translation files in download path
$files = glob($this->getDownloadPath() . DIRECTORY_SEPARATOR . '*.json');
array_map('unlink', $files);
if (!$transifexApi->resourceExists($resource)) {
$output->writeln("Skipping resource $resource as it doesn't exist on Transifex");
return;
}
$output->writeln("Fetching translations from Transifex for resource $resource");
$languages = $transifexApi->getAvailableLanguageCodes(); $languages = $transifexApi->getAvailableLanguageCodes();
if (!empty($plugin)) {
$languages = array_filter($languages, function($language) {
return \Piwik\Plugins\LanguagesManager\API::getInstance()->isLanguageAvailable(str_replace('_', '-', strtolower($language)));
});
}
/** @var ProgressHelper $progress */ /** @var ProgressHelper $progress */
$progress = $this->getHelperSet()->get('progress'); $progress = $this->getHelperSet()->get('progress');
......
...@@ -33,6 +33,8 @@ class Update extends TranslationBase ...@@ -33,6 +33,8 @@ class Update extends TranslationBase
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$start = microtime(true);
/** @var DialogHelper $dialog */ /** @var DialogHelper $dialog */
$dialog = $this->getHelperSet()->get('dialog'); $dialog = $this->getHelperSet()->get('dialog');
...@@ -109,7 +111,7 @@ class Update extends TranslationBase ...@@ -109,7 +111,7 @@ class Update extends TranslationBase
$progress->finish(); $progress->finish();
} }
$output->writeln("Finished."); $output->writeln("Finished in " . round(microtime(true)-$start, 3) . "s");
} }
/** /**
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter