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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
<?php
namespace dokuwiki\plugin\extension;
use dokuwiki\Cache\Cache;
use dokuwiki\plugin\upgrade\HTTP\DokuHTTPClient;
use JsonException;
class Repository
{
public const EXTENSION_REPOSITORY_API = 'https://www.dokuwiki.org/lib/plugins/pluginrepo/api.php';
protected const CACHE_PREFIX = '##extension_manager##';
protected const CACHE_SUFFIX = '.repo';
protected const CACHE_TIME = 3600 * 24;
protected static $instance;
protected $hasAccess;
/**
*
*/
protected function __construct()
{
}
/**
* @return Repository
*/
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Check if access to the repository is possible
*
* On the first call this will throw an exception if access is not possible. On subsequent calls
* it will return the cached result. Thus it is recommended to call this method once when instantiating
* the repository for the first time and handle the exception there. Subsequent calls can then be used
* to access cached data.
*
* @return bool
* @throws Exception
*/
public function checkAccess()
{
if ($this->hasAccess !== null) {
return $this->hasAccess; // we already checked
}
// check for SSL support
if (!in_array('ssl', stream_get_transports())) {
throw new Exception('nossl');
}
// ping the API
$httpclient = new DokuHTTPClient();
$httpclient->timeout = 5;
$data = $httpclient->get(self::EXTENSION_REPOSITORY_API . '?cmd=ping');
if ($data === false) {
$this->hasAccess = false;
throw new Exception('repo_error');
} elseif ($data !== '1') {
$this->hasAccess = false;
throw new Exception('repo_badresponse');
} else {
$this->hasAccess = true;
}
return $this->hasAccess;
}
/**
* Fetch the data for multiple extensions from the repository
*
* @param string[] $ids A list of extension ids
* @throws Exception
*/
protected function fetchExtensions($ids)
{
if (!$this->checkAccess()) return;
$httpclient = new DokuHTTPClient();
$data = [
'fmt' => 'json',
'ext' => $ids
];
$response = $httpclient->post(self::EXTENSION_REPOSITORY_API, $data);
if ($response === false) {
$this->hasAccess = false;
throw new Exception('repo_error');
}
try {
$extensions = json_decode($response, true, 512, JSON_THROW_ON_ERROR);
foreach ($extensions as $extension) {
$this->storeCache($extension['plugin'], $extension);
}
} catch (JsonExceptionAlias $e) {
$this->hasAccess = false;
throw new Exception('repo_badresponse', 0, $e);
}
}
/**
* This creates a list of Extension objects from the given list of ids
*
* The extensions are initialized by fetching their data from the cache or the repository.
* This is the recommended way to initialize a whole bunch of extensions at once as it will only do
* a single API request for all extensions that are not in the cache.
*
* Extensions that are not found in the cache or the repository will be initialized as null.
*
* @param string[] $ids
* @return (Extension|null)[] [id => Extension|null, ...]
* @throws Exception
*/
public function initExtensions($ids)
{
$result = [];
$toload = [];
// first get all that are cached
foreach ($ids as $id) {
$data = $this->retrieveCache($id);
if ($data === null) {
$toload[] = $id;
} else {
$result[$id] = Extension::createFromRemoteData($data);
}
}
// then fetch the rest at once
if ($toload) {
$this->fetchExtensions($toload);
foreach ($toload as $id) {
$data = $this->retrieveCache($id);
if ($data === null) {
$result[$id] = null;
} else {
$result[$id] = Extension::createFromRemoteData($data);
}
}
}
return $result;
}
/**
* Initialize a new Extension object from remote data for the given id
*
* @param string $id
* @return Extension|null
* @throws Exception
*/
public function initExtension($id)
{
$result = $this->initExtensions([$id]);
return $result[$id];
}
/**
* Get the pure API data for a single extension
*
* Used when lazy loading remote data in Extension
*
* @param string $id
* @return array|null
* @throws Exception
*/
public function getExtensionData($id)
{
$data = $this->retrieveCache($id);
if ($data === null) {
$this->fetchExtensions([$id]);
$data = $this->retrieveCache($id);
}
return $data;
}
/**
* Search for extensions using the given query string
*
* @param string $q the query string
* @return Extension[] a list of matching extensions
* @throws Exception
*/
public function searchExtensions($q)
{
if (!$this->checkAccess()) return [];
$query = $this->parseQuery($q);
$query['fmt'] = 'json';
$httpclient = new DokuHTTPClient();
$response = $httpclient->post(self::EXTENSION_REPOSITORY_API, $query);
if ($response === false) {
$this->hasAccess = false;
throw new Exception('repo_error');
}
try {
$items = json_decode($response, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
$this->hasAccess = false;
throw new Exception('repo_badresponse', 0, $e);
}
$results = [];
foreach ($items as $item) {
$this->storeCache($item['plugin'], $item);
$results[] = Extension::createFromRemoteData($item);
}
return $results;
}
/**
* Parses special queries from the query string
*
* @param string $q
* @return array
*/
protected function parseQuery($q)
{
$parameters = [
'tag' => [],
'mail' => [],
'type' => [],
'ext' => []
];
// extract tags
if (preg_match_all('/(^|\s)(tag:([\S]+))/', $q, $matches, PREG_SET_ORDER)) {
foreach ($matches as $m) {
$q = str_replace($m[2], '', $q);
$parameters['tag'][] = $m[3];
}
}
// extract author ids
if (preg_match_all('/(^|\s)(authorid:([\S]+))/', $q, $matches, PREG_SET_ORDER)) {
foreach ($matches as $m) {
$q = str_replace($m[2], '', $q);
$parameters['mail'][] = $m[3];
}
}
// extract extensions
if (preg_match_all('/(^|\s)(ext:([\S]+))/', $q, $matches, PREG_SET_ORDER)) {
foreach ($matches as $m) {
$q = str_replace($m[2], '', $q);
$parameters['ext'][] = $m[3];
}
}
// extract types
if (preg_match_all('/(^|\s)(type:([\S]+))/', $q, $matches, PREG_SET_ORDER)) {
foreach ($matches as $m) {
$q = str_replace($m[2], '', $q);
$parameters['type'][] = $m[3];
}
}
// FIXME make integer from type value
$parameters['q'] = trim($q);
return $parameters;
}
/**
* Store the data for a single extension in the cache
*
* @param string $id
* @param array $data
*/
protected function storeCache($id, $data)
{
$cache = new Cache(self::CACHE_PREFIX . $id, self::CACHE_SUFFIX);
$cache->storeCache(serialize($data));
}
/**
* Retrieve the data for a single extension from the cache
*
* @param string $id
* @return array|null the data or null if not in cache
*/
protected function retrieveCache($id)
{
$cache = new Cache(self::CACHE_PREFIX . $id, self::CACHE_SUFFIX);
if ($cache->useCache(['age' => self::CACHE_TIME])) {
return unserialize($cache->retrieveCache(false));
}
return null;
}
}
|