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