-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
MarketplaceApiClient.php
182 lines (141 loc) · 4.41 KB
/
MarketplaceApiClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
* @category Piwik_Plugins
* @package CorePluginsAdmin
*/
namespace Piwik\Plugins\CorePluginsAdmin;
use Piwik\CacheFile;
use Piwik\Http;
use Piwik\Tracker\Cache;
/**
*
* @package CorePluginsAdmin
*/
class MarketplaceApiClient
{
private $domain = 'http://plugins.piwik.org/';
/**
* @var CacheFile
*/
private $cache = null;
public function __construct()
{
$this->cache = new CacheFile('marketplace', 7200);
}
private function fetch($action, $params)
{
$query = http_build_query($params);
$result = $this->getCachedResult($action, $query);
if (false === $result) {
$endpoint = $this->domain . '/api/1.0/';
$url = sprintf('%s%s?%s', $endpoint, $action, $query);
$result = Http::sendHttpRequest($url, 5);
$this->cacheResult($action, $query, $result);
}
$result = json_decode($result);
if (!empty($result->error)) {
// TODO create own exception
throw new \Exception($result->error);
}
return $result;
}
private function getCachedResult($action, $query)
{
$cacheKey = $this->getCacheKey($action, $query);
return $this->cache->get($cacheKey);
}
private function cacheResult($action, $query, $result)
{
$cacheKey = $this->getCacheKey($action, $query);
$this->cache->set($cacheKey, $result);
}
private function getCacheKey($action, $query)
{
return sprintf('api.1.0.%s.%s', str_replace('/', '.', $action), md5($query));
}
public function getPluginInfo($name)
{
$action = sprintf('plugins/%s/info', $name);
return $this->fetch($action, array());
}
public function download($pluginOrThemeName, $target)
{
$plugin = $this->getPluginInfo($pluginOrThemeName);
if (empty($plugin)) {
// TODO throw exception notExistingPlugin
return;
}
$latestVersion = array_pop($plugin->versions);
$downloadUrl = $latestVersion->download;
$success = Http::fetchRemoteFile($this->domain . $downloadUrl, $target);
return $success;
}
/**
* @param \Piwik\Plugin[] $plugins
*/
public function checkUpdates($plugins)
{
$params = array();
foreach ($plugins as $plugin) {
$pluginName = $plugin->getPluginName();
$params[] = array('name' => $pluginName, 'version' => $plugin->getVersion());
}
$params = array('plugins' => $params);
$hasUpdates = $this->fetch('plugins/checkUpdates', array('plugins' => json_encode($params)));
if (empty($hasUpdates)) {
return array();
}
return $hasUpdates;
}
/**
* @param \Piwik\Plugin[] $plugins
*/
public function getInfoOfPluginsHavingUpdate($plugins)
{
$hasUpdates = $this->checkUpdates($plugins);
$pluginDetails = array();
foreach ($hasUpdates as $pluginHavingUpdate) {
$plugin = $this->getPluginInfo($pluginHavingUpdate->name);
if (!$plugin->isTheme) {
$pluginDetails[] = $plugin;
}
}
return $pluginDetails;
}
/**
* @param \Piwik\Plugin[] $plugins
*/
public function getInfoOfThemesHavingUpdate($plugins)
{
$hasUpdates = $this->checkUpdates($plugins);
$pluginDetails = array();
foreach ($hasUpdates as $pluginHavingUpdate) {
$plugin = $this->getPluginInfo($pluginHavingUpdate->name);
if ($plugin->isTheme) {
$pluginDetails[] = $plugin;
}
}
return $pluginDetails;
}
public function searchForPlugins($keywords, $query, $sort)
{
$response = $this->fetch('plugins', array('keywords' => $keywords, 'query' => $query, 'sort' => $sort));
if (!empty($response->plugins)) {
return $response->plugins;
}
return array();
}
public function searchForThemes($keywords, $query, $sort)
{
$response = $this->fetch('themes', array('keywords' => $keywords, 'query' => $query, 'sort' => $sort));
if (!empty($response->plugins)) {
return $response->plugins;
}
return array();
}
}