forked from cosmocode/tagging
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.php
More file actions
303 lines (249 loc) · 8.53 KB
/
action.php
File metadata and controls
303 lines (249 loc) · 8.53 KB
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
299
300
301
302
303
<?php
if (!defined('DOKU_INC')) {
die();
}
class action_plugin_tagging extends DokuWiki_Action_Plugin {
/**
* Register handlers
*/
function register(Doku_Event_Handler $controller) {
$controller->register_hook(
'TPL_CONTENT_DISPLAY', 'BEFORE', $this,
'echo_searchresults'
);
$controller->register_hook(
'AJAX_CALL_UNKNOWN', 'BEFORE', $this,
'handle_ajax_call_unknown'
);
$controller->register_hook(
'ACTION_ACT_PREPROCESS', 'BEFORE', $this,
'handle_jump'
);
$controller->register_hook(
'DOKUWIKI_STARTED', 'AFTER', $this,
'js_add_security_token'
);
}
/**
* Add sectok to JavaScript to secure ajax requests
*
* @param Doku_Event $event
* @param $param
*/
function js_add_security_token(Doku_Event $event, $param) {
global $JSINFO;
$JSINFO['sectok'] = getSecurityToken();
}
/**
* Handle our AJAX requests
*
* @param Doku_Event $event
* @param $param
*/
function handle_ajax_call_unknown(Doku_Event &$event, $param) {
$handled = true;
if ($event->data == 'plugin_tagging_save') {
$this->save();
} elseif ($event->data == 'plugin_tagging_autocomplete') {
$this->autocomplete();
} elseif ($event->data === 'plugin_tagging_admin_change') {
$this->admin_change();
} else {
$handled = false;
}
if (!$handled) {
return;
}
$event->preventDefault();
$event->stopPropagation();
}
/**
* Jump to a tag
*
* @param Doku_Event $event
* @param $param
*/
function handle_jump(Doku_Event &$event, $param) {
if (act_clean($event->data) != 'tagjmp') {
return;
}
$event->preventDefault();
$event->stopPropagation();
$event->data = 'show';
global $INPUT;
$tags = $INPUT->arr('tag', (array)$INPUT->str('tag'));
$lang = $INPUT->str('lang');
/** @var helper_plugin_tagging $hlp */
$hlp = plugin_load('helper', 'tagging');
foreach ($tags as $tag) {
$filter = array('tag' => $tag);
if ($lang) {
$filter['lang'] = $lang;
}
$pages = $hlp->findItems($filter, 'pid', 1);
if (!count($pages)) {
continue;
}
$id = array_pop(array_keys($pages));
send_redirect(wl($id, '', true, '&'));
}
$tags = array_map('hsc', $tags);
msg(sprintf($this->getLang('tagjmp_error'), join(', ', $tags)), -1);
}
/**
* Save new/changed tags
*/
function save() {
global $INPUT;
global $INFO;
/** @var helper_plugin_tagging $hlp */
$hlp = plugin_load('helper', 'tagging');
$data = $INPUT->arr('tagging');
$id = $data['id'];
$INFO['writable'] = auth_quickaclcheck($id) >= AUTH_EDIT; // we also need this in findItems
if ($INFO['writable'] && $hlp->getUser()) {
$hlp->replaceTags(
$id, $hlp->getUser(),
preg_split(
'/(\s*,\s*)|(\s*,?\s*\n\s*)/', $data['tags'], -1,
PREG_SPLIT_NO_EMPTY
)
);
}
$tags = $hlp->findItems(array('pid' => $id), 'tag');
$hlp->html_cloud($tags, 'tag', array($hlp, 'linkToSearch'), false);
}
/**
* Return autocompletion data
*/
function autocomplete() {
global $INPUT;
/** @var helper_plugin_tagging $hlp */
$hlp = plugin_load('helper', 'tagging');
$search = $INPUT->str('term');
$tags = $hlp->findItems(array('tag' => '%' . $hlp->getDB()->escape_string($search) . '%'), 'tag');
arsort($tags);
$tags = array_keys($tags);
header('Content-Type: application/json');
$json = new JSON();
echo $json->encode(array_combine($tags, $tags));
}
/**
* Allow admins to change all tags (not only their own)
* We change the tag for every user
*/
function admin_change() {
global $INPUT;
/** @var helper_plugin_tagging $hlp */
$hlp = plugin_load('helper', 'tagging');
header('Content-Type: application/json');
$json = new JSON();
if (!auth_isadmin()) {
echo $json->encode(array('status' => 'error', 'msg' => $this->getLang('no_admin')));
return;
}
if (!checkSecurityToken()) {
echo $json->encode(array('status' => 'error', 'msg' => 'Security Token did not match. Possible CSRF attack.'));
return;
}
if (!$INPUT->has('id')) {
echo $json->encode(array('status' => 'error', 'msg' => 'No page id given.'));
return;
}
$pid = $INPUT->str('id');
if (!$INPUT->has('oldValue') || !$INPUT->has('newValue')) {
echo $json->encode(array('status' => 'error', 'msg' => 'No proper input. Give "oldValue" and "newValue"'));
return;
}
list($err, $msg) = $hlp->modifyPageTag($pid, $INPUT->str('oldValue'), $INPUT->str('newValue'));
if ($err) {
echo $json->encode(array('status' => 'error', 'msg' => $msg));
return;
}
$tags = $hlp->findItems(array('pid' => $pid), 'tag');
$userTags = $hlp->findItems(array('pid' => $pid, 'tagger' => $hlp->getUser()), 'tag');
echo $json->encode(array(
'status' => 'ok',
'tags_edit_value' => implode(', ', array_keys($userTags)),
'html_cloud' => $hlp->html_cloud($tags, 'tag', array($hlp, 'linkToSearch'), false, true),
));
}
/**
* Show tagged pages on searches
*
* @param $event
* @param $param
*/
function echo_searchresults(Doku_Event &$event, $param) {
global $ACT;
global $QUERY;
if ($ACT !== 'search') {
return;
}
// parse the search query and use the first found word as term
$terms = ft_queryParser(idx_get_indexer(), $QUERY);
$tag = '';
if (isset($terms['phrases'][0])) {
$tag = $terms['phrases'][0];
} else {
if (isset($terms['and'][0])) {
$tag = $terms['and'][0];
}
}
if (!$tag) {
return;
}
// create filter from term and namespace
$filter = array('tag' => $tag);
if (isset($terms['ns'][0])) {
$filter['pid'] = $terms['ns'][0];
if (substr($filter['pid'], -1) !== ':') {
$filter['pid'] .= ':';
}
$filter['pid'] .= '%';
}
if (isset($terms['notns'][0])) {
$i = 0;
foreach ($terms['notns'] as $notns) {
if (substr($notns, -1) !== ':') {
$notns .= ':';
}
$notns .= '%';
$filter['notpid' . $i] = $notns;
++$i;
}
}
/** @var helper_plugin_tagging $hlp */
$hlp = plugin_load('helper', 'tagging');
$pages = $hlp->findItems($filter, 'pid');
if (!count($pages)) {
return;
}
// create output HTML
$results = '<div class="search_quickresult">';
$results .= '<h3>' . $this->getLang('search_section_title') . ' "' . hsc($tag) . '"' . '</h3>';
$results .= '<ul class="search_quickhits">';
global $ID;
$oldID = $ID;
foreach ($pages as $page => $cnt) {
$ID = $page;
$results .= '<li><div class="li">';
$results .= html_wikilink($page);
$results .= '</div></li>';
}
$ID = $oldID;
$results .= '</ul>';
$results .= '<div class="clearer"></div>';
$results .= '</div>';
if (preg_match('/<div class="nothing">.*?<\/div>/', $event->data)) {
// there are no other hits, replace the nothing found
$event->data = preg_replace('/<div class="nothing">.*?<\/div>/', $results, $event->data, 1);
} elseif (preg_match('/(<\/h2>)/', $event->data)) {
// insert it right after second level headline
$event->data = preg_replace('/(<\/h2>)/', "\\1\n" . $results, $event->data, 1);
} else {
// unclear what happened, let's just append
$event->data .= $results;
}
}
}