PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.3.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.3.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 / Transifex / API.php
matomo / app / core / Translation / Transifex Last commit date
API.php 5 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\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