PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.14.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.14.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 4 years ago
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