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