PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / trunk
Matomo Analytics – Powerful, Privacy-First Insights for WordPress vtrunk
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 1 year ago
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