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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
<?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\Piwik;
use Piwik\PluginsManager;
use Piwik\Version;
/**
*
* @package CorePluginsAdmin
*/
class MarketplaceApiClient
{
const CACHE_TIMEOUT_IN_SECONDS = 1200;
const HTTP_REQUEST_TIMEOUT = 30;
private $domain = 'http://plugins.piwik.org';
/**
* @var CacheFile
*/
private $cache = null;
public function __construct()
{
$this->cache = new CacheFile('marketplace', self::CACHE_TIMEOUT_IN_SECONDS);
}
public static function clearAllCacheEntries()
{
$cache = new CacheFile('marketplace');
$cache->deleteAll();
}
public function getPluginInfo($name)
{
$action = sprintf('plugins/%s/info', $name);
return $this->fetch($action, array());
}
public function download($pluginOrThemeName, $target)
{
$downloadUrl = $this->getDownloadUrl($pluginOrThemeName);
if (empty($downloadUrl)) {
return false;
}
$success = Http::fetchRemoteFile($downloadUrl, $target, 0, static::HTTP_REQUEST_TIMEOUT);
return $success;
}
/**
* @param \Piwik\Plugin[] $plugins
* @return array|mixed
*/
public function checkUpdates($plugins)
{
$params = array();
foreach ($plugins as $plugin) {
$pluginName = $plugin->getPluginName();
if (!PluginsManager::getInstance()->isPluginBundledWithCore($pluginName)) {
$params[] = array('name' => $plugin->getPluginName(), '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
* @param bool $themesOnly
* @return array
*/
public function getInfoOfPluginsHavingUpdate($plugins, $themesOnly)
{
$hasUpdates = $this->checkUpdates($plugins);
$pluginDetails = array();
foreach ($hasUpdates as $pluginHavingUpdate) {
$plugin = $this->getPluginInfo($pluginHavingUpdate['name']);
if (!empty($plugin['isTheme']) == $themesOnly) {
$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();
}
private function fetch($action, $params)
{
ksort($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);
$response = Http::sendHttpRequest($url, static::HTTP_REQUEST_TIMEOUT);
$result = json_decode($response, true);
if (is_null($result)) {
$message = sprintf('There was an error reading the response from the Marketplace: %s. Please try again later.',
substr($response, 0, 50));
throw new MarketplaceApiException($message);
}
if (!empty($result['error'])) {
throw new MarketplaceApiException($result['error']);
}
$this->cacheResult($action, $query, $result);
}
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));
}
/**
* @param $pluginOrThemeName
* @throws MarketplaceApiException
* @return string
*/
public function getDownloadUrl($pluginOrThemeName)
{
$plugin = $this->getPluginInfo($pluginOrThemeName);
if (empty($plugin['versions'])) {
throw new MarketplaceApiException('Plugin has no versions.');
}
$latestVersion = array_pop($plugin['versions']);
$downloadUrl = $latestVersion['download'];
return $this->domain . $downloadUrl . '?coreVersion=' . Version::VERSION;
}
}
|