PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.0.3
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.0.3
5.13.0 5.12.1 5.12.0 5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Translation / Weblate / API.php
matomo / app / core / Translation / Weblate Last commit date
API.php 2 years ago
API.php
131 lines
1 <?php
2
3 /**
4 * Matomo - free/libre analytics platform
5 *
6 * @link https://matomo.org
7 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8 *
9 */
10 namespace Piwik\Translation\Weblate;
11
12 use Exception;
13 use Piwik\Cache;
14 use Piwik\Exception\AuthenticationFailedException;
15 use Piwik\Http;
16 class API
17 {
18 protected $apiUrl = 'https://hosted.weblate.org/api/';
19 protected $apiToken = '';
20 protected $projectSlug = '';
21 public function __construct($apiToken, $project = 'matomo')
22 {
23 $this->apiToken = $apiToken;
24 $this->projectSlug = $project;
25 }
26 /**
27 * Returns all resources available on Weblate project
28 *
29 * @return array
30 */
31 public function getAvailableResources()
32 {
33 $cache = Cache::getTransientCache();
34 $cacheId = 'weblate_resources_' . $this->projectSlug;
35 $result = $cache->fetch($cacheId);
36 if (empty($result)) {
37 $apiPath = 'projects/' . $this->projectSlug . '/components/';
38 $resources = $this->getApiResults($apiPath);
39 $result = [];
40 while ($resources->results) {
41 $result = array_merge($result, $resources->results);
42 if ($resources->next) {
43 $apiPath = str_replace($this->apiUrl, '', $resources->next);
44 $resources = $this->getApiResults($apiPath);
45 } else {
46 break;
47 }
48 }
49 $cache->save($cacheId, $result);
50 }
51 return $result;
52 }
53 /**
54 * Checks if the given resource exists in Weblate project
55 *
56 * @param string $resource
57 * @return bool
58 */
59 public function resourceExists($resource)
60 {
61 $resources = $this->getAvailableResources();
62 foreach ($resources as $res) {
63 if ($res->slug == $resource) {
64 return true;
65 }
66 }
67 return false;
68 }
69 /**
70 * Returns all language codes the Weblate project is available for
71 *
72 * @return array
73 * @throws AuthenticationFailedException
74 * @throws Exception
75 */
76 public function getAvailableLanguageCodes()
77 {
78 $cache = Cache::getTransientCache();
79 $cacheId = 'weblate_languagescodes_' . $this->projectSlug;
80 $languageCodes = $cache->fetch($cacheId);
81 if (empty($languageCodes)) {
82 $apiData = $this->getApiResults('projects/' . $this->projectSlug . '/languages/');
83 foreach ($apiData as $languageData) {
84 $languageCodes[] = $languageData->code;
85 }
86 $cache->save($cacheId, $languageCodes);
87 }
88 return $languageCodes;
89 }
90 /**
91 * Return the translations for the given resource and language
92 *
93 * @param string $resource e.g. piwik-base, piwik-plugin-api,...
94 * @param string $language e.g. de, pt_BR, hy,...
95 * @param bool $raw if true plain response will be returned (unparsed json)
96 * @return mixed
97 * @throws AuthenticationFailedException
98 * @throws Exception
99 */
100 public function getTranslations($resource, $language, $raw = false)
101 {
102 if ($this->resourceExists($resource)) {
103 $apiPath = 'translations/' . $this->projectSlug . '/' . $resource . '/' . $language . '/file/';
104 return $this->getApiResults($apiPath, $raw);
105 }
106 return null;
107 }
108 /**
109 * Returns response for API request with given path
110 *
111 * @param $apiPath
112 * @param bool $raw
113 * @return mixed
114 * @throws AuthenticationFailedException
115 * @throws Exception
116 */
117 protected function getApiResults($apiPath, $raw = false)
118 {
119 $apiUrl = $this->apiUrl . $apiPath;
120 $response = Http::sendHttpRequestBy(Http::getTransportMethod(), $apiUrl, 60, null, null, null, 5, false, false, false, true, 'GET', null, null, null, ['Authorization: Token ' . $this->apiToken]);
121 $httpStatus = $response['status'];
122 $response = $response['data'];
123 if ($httpStatus == 401) {
124 throw new AuthenticationFailedException();
125 } elseif ($httpStatus != 200) {
126 throw new Exception('Error while getting API results', $httpStatus);
127 }
128 return $raw ? $response : json_decode($response);
129 }
130 }
131