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\Transifex; |
| 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://www.transifex.com/api/2/'; |
| 19 | protected $username = ''; |
| 20 | protected $password = ''; |
| 21 | protected $projectSlug = ''; |
| 22 | |
| 23 | public function __construct($username, $password, $project = 'matomo') |
| 24 | { |
| 25 | $this->username = $username; |
| 26 | $this->password = $password; |
| 27 | $this->projectSlug = $project; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Returns all resources available on Transifex project |
| 32 | * |
| 33 | * @return array |
| 34 | */ |
| 35 | public function getAvailableResources() |
| 36 | { |
| 37 | $cache = Cache::getTransientCache(); |
| 38 | $cacheId = 'transifex_resources_' . $this->projectSlug; |
| 39 | $resources = $cache->fetch($cacheId); |
| 40 | |
| 41 | if (empty($resources)) { |
| 42 | $apiPath = 'project/' . $this->projectSlug . '/resources'; |
| 43 | $resources = $this->getApiResults($apiPath); |
| 44 | $cache->save($cacheId, $resources); |
| 45 | } |
| 46 | |
| 47 | return $resources; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Checks if the given resource exists in Transifex project |
| 52 | * |
| 53 | * @param string $resource |
| 54 | * @return bool |
| 55 | */ |
| 56 | public function resourceExists($resource) |
| 57 | { |
| 58 | $resources = $this->getAvailableResources(); |
| 59 | foreach ($resources as $res) { |
| 60 | if ($res->slug == $resource) { |
| 61 | return true; |
| 62 | } |
| 63 | } |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns all language codes the transifex project is available for |
| 69 | * |
| 70 | * @return array |
| 71 | * @throws AuthenticationFailedException |
| 72 | * @throws Exception |
| 73 | */ |
| 74 | public function getAvailableLanguageCodes() |
| 75 | { |
| 76 | $cache = Cache::getTransientCache(); |
| 77 | $cacheId = 'transifex_languagescodes_' . $this->projectSlug; |
| 78 | $languageCodes = $cache->fetch($cacheId); |
| 79 | |
| 80 | if (empty($languageCodes)) { |
| 81 | $apiData = $this->getApiResults('project/' . $this->projectSlug . '/languages'); |
| 82 | foreach ($apiData as $languageData) { |
| 83 | $languageCodes[] = $languageData->language_code; |
| 84 | } |
| 85 | $cache->save($cacheId, $languageCodes); |
| 86 | } |
| 87 | return $languageCodes; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns statistic data for the given resource |
| 92 | * |
| 93 | * @param string $resource e.g. piwik-base, piwik-plugin-api,... |
| 94 | * @return array |
| 95 | * @throws AuthenticationFailedException |
| 96 | * @throws Exception |
| 97 | */ |
| 98 | public function getStatistics($resource) |
| 99 | { |
| 100 | return $this->getApiResults('project/' . $this->projectSlug . '/resource/' . $resource . '/stats/'); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Return the translations for the given resource and language |
| 105 | * |
| 106 | * @param string $resource e.g. piwik-base, piwik-plugin-api,... |
| 107 | * @param string $language e.g. de, pt_BR, hy,... |
| 108 | * @param bool $raw if true plain response will be returned (unparsed json) |
| 109 | * @return mixed |
| 110 | * @throws AuthenticationFailedException |
| 111 | * @throws Exception |
| 112 | */ |
| 113 | public function getTranslations($resource, $language, $raw = false) |
| 114 | { |
| 115 | if ($this->resourceExists($resource)) { |
| 116 | $apiPath = 'project/' . $this->projectSlug . '/resource/' . $resource . '/translation/' . $language . '/?mode=onlytranslated&file'; |
| 117 | return $this->getApiResults($apiPath, $raw); |
| 118 | } |
| 119 | return null; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Returns response for API request with given path |
| 124 | * |
| 125 | * @param $apiPath |
| 126 | * @param bool $raw |
| 127 | * @return mixed |
| 128 | * @throws AuthenticationFailedException |
| 129 | * @throws Exception |
| 130 | */ |
| 131 | protected function getApiResults($apiPath, $raw = false) |
| 132 | { |
| 133 | $apiUrl = $this->apiUrl . $apiPath; |
| 134 | |
| 135 | $response = Http::sendHttpRequest($apiUrl, 1000, null, null, 5, false, false, true, 'GET', $this->username, $this->password); |
| 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 |