1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DokuWiki Plugin extension (Helper Component) |
4
|
|
|
* |
5
|
|
|
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html |
6
|
|
|
* @author Michael Hamann <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class helper_plugin_extension_repository provides access to the extension repository on dokuwiki.org |
11
|
|
|
*/ |
12
|
|
|
class helper_plugin_extension_repository extends DokuWiki_Plugin |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
const EXTENSION_REPOSITORY_API = 'http://www.dokuwiki.org/lib/plugins/pluginrepo/api.php'; |
16
|
|
|
|
17
|
|
|
private $loaded_extensions = array(); |
18
|
|
|
private $has_access = null; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Initialize the repository (cache), fetches data for all installed plugins |
22
|
|
|
*/ |
23
|
|
|
public function init() |
24
|
|
|
{ |
25
|
|
|
/* @var Doku_Plugin_Controller $plugin_controller */ |
26
|
|
|
global $plugin_controller; |
27
|
|
|
if ($this->hasAccess()) { |
28
|
|
|
$list = $plugin_controller->getList('', true); |
29
|
|
|
$request_data = array('fmt' => 'php'); |
30
|
|
|
$request_needed = false; |
31
|
|
|
foreach ($list as $name) { |
32
|
|
|
$cache = new cache('##extension_manager##'.$name, '.repo'); |
33
|
|
|
|
34
|
|
|
if (!isset($this->loaded_extensions[$name]) && |
35
|
|
|
$this->hasAccess() && |
36
|
|
|
!$cache->useCache(array('age' => 3600 * 24)) |
37
|
|
|
) { |
38
|
|
|
$this->loaded_extensions[$name] = true; |
39
|
|
|
$request_data['ext'][] = $name; |
40
|
|
|
$request_needed = true; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if ($request_needed) { |
45
|
|
|
$httpclient = new DokuHTTPClient(); |
46
|
|
|
$data = $httpclient->post(self::EXTENSION_REPOSITORY_API, $request_data); |
47
|
|
|
if ($data !== false) { |
48
|
|
|
$extensions = unserialize($data); |
49
|
|
|
foreach ($extensions as $extension) { |
50
|
|
|
$cache = new cache('##extension_manager##'.$extension['plugin'], '.repo'); |
51
|
|
|
$cache->storeCache(serialize($extension)); |
52
|
|
|
} |
53
|
|
|
} else { |
54
|
|
|
$this->has_access = false; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* If repository access is available |
62
|
|
|
* |
63
|
|
|
* @return bool If repository access is available |
64
|
|
|
*/ |
65
|
|
|
public function hasAccess() |
66
|
|
|
{ |
67
|
|
|
if ($this->has_access === null) { |
68
|
|
|
$cache = new cache('##extension_manager###hasAccess', '.repo'); |
69
|
|
|
|
70
|
|
|
if (!$cache->useCache(array('age' => 3600 * 24, 'purge'=>1))) { |
71
|
|
|
$httpclient = new DokuHTTPClient(); |
72
|
|
|
$httpclient->timeout = 5; |
73
|
|
|
$data = $httpclient->get(self::EXTENSION_REPOSITORY_API.'?cmd=ping'); |
74
|
|
|
if ($data !== false) { |
75
|
|
|
$this->has_access = true; |
76
|
|
|
$cache->storeCache(1); |
77
|
|
|
} else { |
78
|
|
|
$this->has_access = false; |
79
|
|
|
$cache->storeCache(0); |
80
|
|
|
} |
81
|
|
|
} else { |
82
|
|
|
$this->has_access = ($cache->retrieveCache(false) == 1); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
return $this->has_access; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the remote data of an individual plugin or template |
90
|
|
|
* |
91
|
|
|
* @param string $name The plugin name to get the data for, template names need to be prefix by 'template:' |
92
|
|
|
* @return array The data or null if nothing was found (possibly no repository access) |
93
|
|
|
*/ |
94
|
|
|
public function getData($name) |
95
|
|
|
{ |
96
|
|
|
$cache = new cache('##extension_manager##'.$name, '.repo'); |
97
|
|
|
|
98
|
|
|
if (!isset($this->loaded_extensions[$name]) && |
99
|
|
|
$this->hasAccess() && |
100
|
|
|
!$cache->useCache(array('age' => 3600 * 24)) |
101
|
|
|
) { |
102
|
|
|
$this->loaded_extensions[$name] = true; |
103
|
|
|
$httpclient = new DokuHTTPClient(); |
104
|
|
|
$data = $httpclient->get(self::EXTENSION_REPOSITORY_API.'?fmt=php&ext[]='.urlencode($name)); |
105
|
|
|
if ($data !== false) { |
106
|
|
|
$result = unserialize($data); |
107
|
|
|
$cache->storeCache(serialize($result[0])); |
108
|
|
|
return $result[0]; |
109
|
|
|
} else { |
110
|
|
|
$this->has_access = false; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
if (file_exists($cache->cache)) { |
114
|
|
|
return unserialize($cache->retrieveCache(false)); |
115
|
|
|
} |
116
|
|
|
return array(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Search for plugins or templates using the given query string |
121
|
|
|
* |
122
|
|
|
* @param string $q the query string |
123
|
|
|
* @return array a list of matching extensions |
124
|
|
|
*/ |
125
|
|
|
public function search($q) |
126
|
|
|
{ |
127
|
|
|
$query = $this->parseQuery($q); |
128
|
|
|
$query['fmt'] = 'php'; |
129
|
|
|
|
130
|
|
|
$httpclient = new DokuHTTPClient(); |
131
|
|
|
$data = $httpclient->post(self::EXTENSION_REPOSITORY_API, $query); |
132
|
|
|
if ($data === false) return array(); |
133
|
|
|
$result = unserialize($data); |
134
|
|
|
|
135
|
|
|
$ids = array(); |
136
|
|
|
|
137
|
|
|
// store cache info for each extension |
138
|
|
|
foreach ($result as $ext) { |
139
|
|
|
$name = $ext['plugin']; |
140
|
|
|
$cache = new cache('##extension_manager##'.$name, '.repo'); |
141
|
|
|
$cache->storeCache(serialize($ext)); |
142
|
|
|
$ids[] = $name; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $ids; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Parses special queries from the query string |
150
|
|
|
* |
151
|
|
|
* @param string $q |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
protected function parseQuery($q) |
155
|
|
|
{ |
156
|
|
|
$parameters = array( |
157
|
|
|
'tag' => array(), |
158
|
|
|
'mail' => array(), |
159
|
|
|
'type' => array(), |
160
|
|
|
'ext' => array() |
161
|
|
|
); |
162
|
|
|
|
163
|
|
|
// extract tags |
164
|
|
|
if (preg_match_all('/(^|\s)(tag:([\S]+))/', $q, $matches, PREG_SET_ORDER)) { |
165
|
|
|
foreach ($matches as $m) { |
166
|
|
|
$q = str_replace($m[2], '', $q); |
167
|
|
|
$parameters['tag'][] = $m[3]; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
// extract author ids |
171
|
|
|
if (preg_match_all('/(^|\s)(authorid:([\S]+))/', $q, $matches, PREG_SET_ORDER)) { |
172
|
|
|
foreach ($matches as $m) { |
173
|
|
|
$q = str_replace($m[2], '', $q); |
174
|
|
|
$parameters['mail'][] = $m[3]; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
// extract extensions |
178
|
|
|
if (preg_match_all('/(^|\s)(ext:([\S]+))/', $q, $matches, PREG_SET_ORDER)) { |
179
|
|
|
foreach ($matches as $m) { |
180
|
|
|
$q = str_replace($m[2], '', $q); |
181
|
|
|
$parameters['ext'][] = $m[3]; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
// extract types |
185
|
|
|
if (preg_match_all('/(^|\s)(type:([\S]+))/', $q, $matches, PREG_SET_ORDER)) { |
186
|
|
|
foreach ($matches as $m) { |
187
|
|
|
$q = str_replace($m[2], '', $q); |
188
|
|
|
$parameters['type'][] = $m[3]; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
// FIXME make integer from type value |
193
|
|
|
|
194
|
|
|
$parameters['q'] = trim($q); |
195
|
|
|
return $parameters; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
// vim:ts=4:sw=4:et: |
200
|
|
|
|