File size: 4,527 Bytes
f8ce8bb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | <?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Translation\Weblate;
use Exception;
use Piwik\Cache;
use Piwik\Exception\AuthenticationFailedException;
use Piwik\Http;
class API
{
protected $apiUrl = 'https://hosted.weblate.org/api/';
protected $apiToken = '';
protected $projectSlug = '';
public function __construct($apiToken, $project = 'matomo')
{
$this->apiToken = $apiToken;
$this->projectSlug = $project;
}
/**
* Returns all resources available on Weblate project
*
* @return array
*/
public function getAvailableResources()
{
$cache = Cache::getTransientCache();
$cacheId = 'weblate_resources_' . $this->projectSlug;
$result = $cache->fetch($cacheId);
if (empty($result)) {
$apiPath = 'projects/' . $this->projectSlug . '/components/';
$resources = $this->getApiResults($apiPath);
$result = [];
while ($resources->results) {
$result = array_merge($result, $resources->results);
if ($resources->next) {
$apiPath = str_replace($this->apiUrl, '', $resources->next);
$resources = $this->getApiResults($apiPath);
} else {
break;
}
}
$cache->save($cacheId, $result);
}
return $result;
}
/**
* Checks if the given resource exists in Weblate project
*
* @param string $resource
* @return bool
*/
public function resourceExists($resource)
{
$resources = $this->getAvailableResources();
foreach ($resources as $res) {
if ($res->slug == $resource) {
return true;
}
}
return false;
}
/**
* Returns all language codes the Weblate project is available for
*
* @return array
* @throws AuthenticationFailedException
* @throws Exception
*/
public function getAvailableLanguageCodes()
{
$cache = Cache::getTransientCache();
$cacheId = 'weblate_languagescodes_' . $this->projectSlug;
$languageCodes = $cache->fetch($cacheId);
if (empty($languageCodes)) {
$apiData = $this->getApiResults('projects/' . $this->projectSlug . '/languages/');
foreach ($apiData as $languageData) {
$languageCodes[] = $languageData->code;
}
$cache->save($cacheId, $languageCodes);
}
return $languageCodes;
}
/**
* Return the translations for the given resource and language
*
* @param string $resource e.g. piwik-base, piwik-plugin-api,...
* @param string $language e.g. de, pt_BR, hy,...
* @param bool $raw if true plain response will be returned (unparsed json)
* @return mixed
* @throws AuthenticationFailedException
* @throws Exception
*/
public function getTranslations($resource, $language, $raw = false)
{
if ($this->resourceExists($resource)) {
$apiPath = 'translations/' . $this->projectSlug . '/' . $resource . '/' . $language . '/file/';
return $this->getApiResults($apiPath, $raw);
}
return null;
}
/**
* Returns response for API request with given path
*
* @param $apiPath
* @param bool $raw
* @return mixed
* @throws AuthenticationFailedException
* @throws Exception
*/
protected function getApiResults($apiPath, $raw = false)
{
$apiUrl = $this->apiUrl . $apiPath;
$response = Http::sendHttpRequestBy(
Http::getTransportMethod(),
$apiUrl,
60,
null,
null,
null,
5,
false,
false,
false,
true,
'GET',
null,
null,
null,
['Authorization: Token ' . $this->apiToken]
);
$httpStatus = $response['status'];
$response = $response['data'];
if ($httpStatus == 401) {
throw new AuthenticationFailedException();
} elseif ($httpStatus != 200) {
throw new Exception('Error while getting API results', $httpStatus);
}
return $raw ? $response : json_decode($response);
}
}
|