-
Notifications
You must be signed in to change notification settings - Fork 3
/
action.php
executable file
·313 lines (271 loc) · 10.8 KB
/
action.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
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
304
305
306
307
308
309
310
311
312
313
<?php
use dokuwiki\Extension\Event;
/**
* DokuWiki Plugin linksuggest (Action Component)
*
* ajax autosuggest for links
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author lisps
*/
class action_plugin_linksuggest extends DokuWiki_Action_Plugin {
/**
* Register the eventhandlers
*
* @param Doku_Event_Handler $controller
*/
public function register(Doku_Event_Handler $controller) {
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'page_link');
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'media_link');
$controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, '_add_config');
}
public function _add_config(&$event, $param) {
global $JSINFO;
$JSINFO['append_header'] = $this->getConf('append_header');
}
/**
* ajax Request Handler
* page_link
*
* @param $event
*/
public function page_link($event) {
if ($event->data !== 'plugin_linksuggest') {
return;
}
//no other ajax call handlers needed
$event->stopPropagation();
$event->preventDefault();
global $INPUT;
//current page/ns
$current_pageid = trim($INPUT->post->str('id')); //current id
$current_ns = getNS($current_pageid);
$q = trim($INPUT->post->str('q')); //entered string
//keep hashlink if exists
list($q, $hash) = array_pad(explode('#', $q, 2), 2, null);
$has_hash = !($hash === null);
$entered_ns = getNS($q); //namespace of entered string
$trailing = ':'; //needs to be remembered, such that actual user input can be returned
if($entered_ns === false) {
//no namespace given (i.e. none : in $q)
// .xxx, ..xxx, ~xxx, if in front of ns, cleaned in $entered_page
if (substr($q, 0, 2) === '..') {
$entered_ns = '..';
} elseif (substr($q, 0, 1) === '.') {
$entered_ns = '.';
} elseif (substr($q, 0, 1) === '~') {
$entered_ns = '~';
}
$trailing = '';
}
$entered_page = cleanID(noNS($q)); //page part of entered string
if ($entered_ns === '') { // [[:xxx -> absolute link
$matchedPages = $this->search_pages('', $entered_page, $has_hash);
} else if (strpos($q, '.') !== false //relative link (., .:, .., ..:, .ns: etc, and :..:, :.: )
|| substr($entered_ns, 0, 1) == '~') { // ~, ~:,
//resolve the ns based on current id
$ns = $entered_ns;
if($entered_ns === '~') {
//add a random page name, otherwise it ~ or ~: are interpret as ~:start
$ns .= 'uniqueadditionforlinksuggestplugin';
}
if (class_exists('dokuwiki\File\PageResolver')) {
// Igor and later
$resolver = new dokuwiki\File\PageResolver($current_pageid);
$resolved_ns = $resolver->resolveId($ns);
} else {
// Compatibility with older releases
$resolved_ns = $ns;
resolve_pageid(getNS($current_pageid), $resolved_ns, $exists);
}
if($entered_ns === '~') {
$resolved_ns = substr($resolved_ns, 0,-35); //remove : and unique string
}
$matchedPages = $this->search_pages($resolved_ns, $entered_page, $has_hash);
} else if ($entered_ns === false && $current_ns) { // [[xxx while current page not in root-namespace
$matchedPages = array_merge(
$this->search_pages($current_ns, $entered_page, true),//search in current for pages
$this->search_pages('', $entered_page, $has_hash) //search in root both pgs and ns
);
} else {
$matchedPages = $this->search_pages($entered_ns, $entered_page, $has_hash);
}
$data_suggestions = [];
$link = '';
if ($hash !== null && $matchedPages[0]['type'] === 'f') {
//if hash is given and a page was found
$page = $matchedPages[0]['id'];
$meta = p_get_metadata($page, false, METADATA_RENDER_USING_CACHE);
if (isset($meta['internal']['toc'])) {
$toc = $meta['description']['tableofcontents'];
Event::createAndTrigger('TPL_TOC_RENDER', $toc, null, false);
if (is_array($toc) && count($toc) !== 0) {
foreach ($toc as $t) { //loop through toc and compare
if ($hash === '' || strpos($t['hid'], $hash) === 0) {
$data_suggestions[] = $t;
}
}
$link = $q;
}
}
} else {
foreach ($matchedPages as $entry) {
//a page in rootns
if($current_ns !== '' && !$entry['ns'] && $entry['type'] === 'f') {
$trailing = ':';
}
$data_suggestions[] = [
'id' => noNS($entry['id']),
//return literally ns what user has typed in before page name/namespace name that is suggested
'ns' => $entered_ns . $trailing,
'type' => $entry['type'], // d/f
'title' => $entry['title'] ?? '', //namespace have no title, for pages sometimes no title
'rootns' => $entry['ns'] ? 0 : 1,
];
}
}
echo json_encode([
'data' => $data_suggestions,
'link' => $link
]);
}
/**
* ajax Request Handler
* media_link
*
* @param Event $event
*/
public function media_link($event) {
if ($event->data !== 'plugin_imglinksuggest') {
return;
}
//no other ajax call handlers needed
$event->stopPropagation();
$event->preventDefault();
global $INPUT;
//current media/ns
$current_pageid = trim($INPUT->post->str('id')); //current id
$current_ns = getNS($current_pageid);
$q = trim($INPUT->post->str('q')); //entered string
$entered_ns = getNS($q); //namespace of entered string
$trailing = ':'; //needs to be remembered, such that actual user input can be returned
if($entered_ns === false) {
//no namespace given (i.e. none : in $q)
// .xxx, ..xxx, ~xxx, if in front of ns, cleaned in $entered_page
if (substr($q, 0, 2) === '..') {
$entered_ns = '..';
} elseif (substr($q, 0, 1) === '.') {
$entered_ns = '.';
} elseif (substr($q, 0, 1) === '~') {
$entered_ns = '~';
}
$trailing = '';
}
$entered_media = cleanID(noNS($q)); //page part of entered string
if ($entered_ns === '') { // [[:xxx -> absolute link
$matchedMedias = $this->search_medias('', $entered_media);
} else if (strpos($q, '.') !== false //relative link (., .:, .., ..:, .ns: etc, and :..:, :.: )
|| substr($entered_ns, 0, 1) == '~') { // ~, ~:,
//resolve the ns based on current id
$ns = $entered_ns;
if($entered_ns === '~') {
//add a random page name, otherwise it ~ or ~: are interpret as ~:start
$ns .= 'uniqueadditionforlinksuggestplugin';
}
if (class_exists('dokuwiki\File\PageResolver')) {
// Igor and later
$resolver = new dokuwiki\File\MediaResolver($current_pageid);
$resolved_ns = $resolver->resolveId($ns);
} else {
// Compatibility with older releases
$resolved_ns = $ns;
resolve_mediaid(getNS($current_pageid), $resolved_ns, $exists);
}
if($entered_ns === '~') {
$resolved_ns = substr($resolved_ns, 0,-35); //remove : and unique string
}
$matchedMedias = $this->search_medias($resolved_ns, $entered_media);
} else if ($entered_ns === false && $current_ns) { // [[xxx while current page not in root-namespace
$matchedMedias = array_merge(
$this->search_medias($current_ns, $entered_media), //search in current for pages
$this->search_medias('', $entered_media) //search in root both pgs and ns
);
} else {
$matchedMedias = $this->search_medias($entered_ns, $entered_media);
}
$data_suggestions = [];
foreach ($matchedMedias as $entry) {
//a page in rootns
if($current_ns !== '' && !$entry['ns'] && $entry['type'] === 'f') {
$trailing = ':';
}
$data_suggestions[] = [
'id' => noNS($entry['id']),
//return literally ns what user has typed in before page name/namespace name that is suggested
'ns' => $entered_ns . $trailing,
'type' => $entry['type'], // d/f
'rootns' => $entry['ns'] ? 0 : 1,
];
}
echo json_encode([
'data' => $data_suggestions,
'link' => ''
]);
}
/**
* List available pages, and eventually namespaces
*
* @param string $ns namespace to search in
* @param string $id
* @param bool $pagesonly true: pages only, false: pages and namespaces
* @return array
*/
protected function search_pages($ns, $id, $pagesonly = false) {
global $conf;
$data = [];
$nsd = utf8_encodeFN(str_replace(':', '/', $ns)); //dir
$opts = [
'depth' => 1,
'listfiles' => true,
'listdirs' => !$pagesonly,
'pagesonly' => true,
'firsthead' => true,
'sneakyacl' => $conf['sneaky_index'],
];
if ($id) {
$opts['filematch'] = '^.*\/' . $id;
}
if ($id && !$pagesonly) {
$opts['dirmatch'] = '^.*\/' . $id;
}
search($data, $conf['datadir'], 'search_universal', $opts, $nsd);
return $data;
}
/**
* List available media
*
* @param string $ns
* @param string $id
* @return array
*/
protected function search_medias($ns, $id) {
global $conf;
$data = [];
$nsd = utf8_encodeFN(str_replace(':', '/', $ns)); //dir
$opts = [
'depth' => 1,
'listfiles' => true,
'listdirs' => true,
'firsthead' => true,
'sneakyacl' => $conf['sneaky_index'],
];
if ($id) {
$opts['filematch'] = '^.*\/' . $id;
}
if ($id) {
$opts['dirmatch'] = '^.*\/' . $id;
}
search($data, $conf['mediadir'], 'search_universal', $opts, $nsd);
return $data;
}
}