forked from samuelet/indexmenu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.php
561 lines (509 loc) · 17.4 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
<?php
/**
* Indexmenu Action Plugin: Indexmenu Component.
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Samuele Tognini <[email protected]>
*/
use dokuwiki\Extension\ActionPlugin;
use dokuwiki\Extension\Event;
use dokuwiki\Extension\EventHandler;
use dokuwiki\plugin\indexmenu\Search;
use dokuwiki\Ui\Index;
/**
* Class action_plugin_indexmenu
*/
class action_plugin_indexmenu extends ActionPlugin
{
/**
* plugin should use this method to register its handlers with the dokuwiki's event controller
*
* @param EventHandler $controller DokuWiki's event controller object.
*/
public function register(EventHandler $controller)
{
if ($this->getConf('only_admins')) {
$controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, 'removeSyntaxIfNotAdmin');
}
if ($this->getConf('page_index') != '') {
$controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'loadOwnIndexPage');
}
$controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'extendJSINFO');
$controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, 'purgeCache');
if ($this->getConf('show_sort')) {
$controller->register_hook('TPL_CONTENT_DISPLAY', 'BEFORE', $this, 'showSortNumberAtTopOfPage');
}
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'ajaxCalls');
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'addStylesForSkins');
}
/**
* Check if user has permission to insert indexmenu
*
* @param Event $event
*
* @author Samuele Tognini <[email protected]>
*/
public function removeSyntaxIfNotAdmin(Event $event)
{
global $INFO;
if (!$INFO['ismanager']) {
$event->data[0][1] = preg_replace("/{{indexmenu(|_n)>.+?}}/", "", $event->data[0][1]);
}
}
/**
* Add additional info to $JSINFO
*
* @param Event $event
*
* @author Gerrit Uitslag <[email protected]>
* @author Samuele Tognini <[email protected]>
*/
public function extendJSINFO(Event $event)
{
global $INFO, $JSINFO;
$JSINFO['isadmin'] = (int)$INFO['isadmin'];
$JSINFO['isauth'] = isset($INFO['userinfo']) ? (int) $INFO['userinfo'] : 0;
}
/**
* Check for pages changes and eventually purge cache.
*
* @param Event $event
*
* @author Samuele Tognini <[email protected]>
*/
public function purgeCache(Event $event)
{
global $ID;
global $conf;
global $INPUT;
global $INFO;
/** @var cache_parser $cache */
$cache = &$event->data;
if (!isset($cache->page)) return;
//purge only xhtml cache
if ($cache->mode != "xhtml") return;
//Check if it is an indexmenu page
if (!p_get_metadata($ID, 'indexmenu hasindexmenu')) return;
$aclcache = $this->getConf('aclcache');
if ($conf['useacl']) {
$newkey = false;
if ($aclcache == 'user') {
//Cache per user
if ($INPUT->server->str('REMOTE_USER')) {
$newkey = $INPUT->server->str('REMOTE_USER');
}
} elseif ($aclcache == 'groups') {
//Cache per groups
if (isset($INFO['userinfo']['grps'])) {
$newkey = implode('#', $INFO['userinfo']['grps']);
}
}
if ($newkey) {
$cache->key .= "#" . $newkey;
$cache->cache = getCacheName($cache->key, $cache->ext);
}
}
//Check if a page is more recent than purgefile.
if (@filemtime($cache->cache) < @filemtime($conf['cachedir'] . '/purgefile')) {
$event->preventDefault();
$event->stopPropagation();
$event->result = false;
}
}
/**
* Render a defined page as index.
*
* @param Event $event
*
* @author Samuele Tognini <[email protected]>
*/
public function loadOwnIndexPage(Event $event)
{
if ('index' != $event->data) return;
if (!file_exists(wikiFN($this->getConf('page_index')))) return;
global $lang;
echo '<h1><a id="index">' . $lang['btn_index'] . "</a></h1>\n";
echo p_wiki_xhtml($this->getConf('page_index'));
$event->preventDefault();
$event->stopPropagation();
}
/**
* Display the indexmenu sort number.
*
* @param Event $event
*
* @author Samuele Tognini <[email protected]>
*/
public function showSortNumberAtTopOfPage(Event $event)
{
global $ID, $ACT, $INFO;
if ($INFO['isadmin'] && $ACT == 'show') {
if ($n = p_get_metadata($ID, 'indexmenu_n')) {
echo '<div class="info">';
echo $this->getLang('showsort') . $n;
echo '</div>';
}
}
}
/**
* Handles ajax requests for indexmenu
*
* @param Event $event
*/
public function ajaxCalls(Event $event)
{
if ($event->data !== 'indexmenu') {
return;
}
//no other ajax call handlers needed
$event->stopPropagation();
$event->preventDefault();
global $INPUT;
switch ($INPUT->str('req')) {
case 'local':
//list themes
$this->getlocalThemes();
break;
case 'toc':
//print toc preview
if ($INPUT->has('id')) {
echo $this->printToc($INPUT->str('id'));
}
break;
case 'index':
//for dTree
//retrieval of data of the extra nodes for the indexmenu (if ajax loading set with max#m(#n)
if ($INPUT->has('idx')) {
echo $this->printIndex($INPUT->str('idx'));
}
break;
case 'fancytree':
//data for new index build with Fancytree
$this->getDataFancyTree();
break;
}
}
/**
* Handles ajax requests for FancyTree
*
* @return void
*/
private function getDataFancyTree()
{
global $INPUT;
$ns = $INPUT->str('ns', '');
$ns = rtrim($ns, ':');
//key of directory has extra : on the end
$level = -1; //opened levels. -1=all levels open
$max = 1; //levels to load by lazyloading. Before the default was 0. CHANGED to 1.
$skipFileCombined = [];
$skipNsCombined = [];
if ($INPUT->int('max') > 0) {
$max = $INPUT->int('max'); // max#n#m, if init: #n, otherwise #m
$level = $max;
}
if ($INPUT->int('level', -10) >= -1) {
$level = $INPUT->int('level');
}
$isInit = $INPUT->bool('init');
$currentPage = $INPUT->str('currentpage');
if ($isInit) {
$subnss = $INPUT->arr('subnss');
// if 'navbar' is enabled add current ns to list
if ($INPUT->bool('navbar')) {
$currentNs = getNS($currentPage);
if ($currentNs !== false) {
$subnss[] = [$currentNs, 1];
}
}
// alternative, via javascript.. https://wwwendt.de/tech/fancytree/doc/jsdoc/Fancytree.html#loadKeyPath
} else {
//not set via javascript at the moment.. ajax opens per level, so subnss has no use here
$subnss = $INPUT->str('subnss');
if ($subnss !== '') {
$subnss = [[cleanID($subnss), 1]];
}
}
$skipf = $INPUT->str('skipfile');
$skipFileCombined[] = $this->getConf('skip_file');
if (!empty($skipf)) {
$index = 0;
//prefix is '=' or '+'
if ($skipf[0] == '+') {
$index = 1;
}
$skipFileCombined[$index] = substr($skipf, 1);
}
$skipn = $INPUT->str('skipns');
$skipNsCombined[] = $this->getConf('skip_index');
if (!empty($skipn)) {
$index = 0;
//prefix is '=' or '+'
if ($skipn[0] == '+') {
$index = 1;
}
$skipNsCombined[$index] = substr($skipn, 1);
}
$opts = [
//only set for init, lazy requests equal to max
'level' => $level,
//nons only needed for init as it has no nested nodes
'nons' => $INPUT->bool('nons'),
'nopg' => $INPUT->bool('nopg'),
//init with complex array, empty if lazy loading
'subnss' => $subnss,
'max' => $max,
'skipnscombined' => $skipNsCombined,
'skipfilecombined' => $skipFileCombined,
'headpage' => $this->getConf('headpage'),
'hide_headpage' => $this->getConf('hide_headpage'),
];
$sort = [
'sort' => $INPUT->str('sort'),
'msort' => $INPUT->str('msort'),
'rsort' => $INPUT->bool('rsort'),
'nsort' => $INPUT->bool('nsort'),
'group' => $INPUT->bool('group'),
'hsort' => $INPUT->bool('hsort')
];
$opts['tempNew'] = true; //TODO temporary for recognizing treenew in the search function
$search = new Search($sort);
$data = $search->search($ns, $opts);
$fancytreeData = $search->buildFancytreeData($data, $isInit, $currentPage, $opts['nopg']);
//add eventually debug info
if ($isInit) {
//for lazy loading are other items than children not supported.
// $fancytreeData['opts'] = $opts;
// $fancytreeData['sort'] = $sort;
// $fancytreeData['debug'] = $data;
} else {
//returns only children, therefore, add debug info to first child
// $fancytreeData[0]['opts'] = $opts;
// $fancytreeData[0]['sort'] = $sort;
// $fancytreeData[0]['debug'] = $data;
}
header('Content-Type: application/json');
echo json_encode($fancytreeData);
}
/**
* Print a list of local themes
*
* @author Samuele Tognini <[email protected]>
* @author Gerrit Uitslag <[email protected]>
*/
private function getlocalThemes()
{
header('Content-Type: application/json');
$themebase = 'lib/plugins/indexmenu/images';
$handle = @opendir(DOKU_INC . $themebase);
$themes = [];
while (false !== ($file = readdir($handle))) {
if (
is_dir(DOKU_INC . $themebase . '/' . $file)
&& $file != "."
&& $file != ".."
&& $file != "repository"
&& $file != "tmp"
&& $file != ".svn"
) {
$themes[] = $file;
}
}
closedir($handle);
sort($themes);
echo json_encode([
'themebase' => $themebase,
'themes' => $themes
]);
}
/**
* Print a toc preview
*
* @param string $id
* @return string
*
* @author Samuele Tognini <[email protected]>
* @author Andreas Gohr <[email protected]>
*/
private function printToc($id)
{
$id = cleanID($id);
if (auth_quickaclcheck($id) < AUTH_READ) return '';
$meta = p_get_metadata($id);
$toc = $meta['description']['tableofcontents'] ?? [];
if (count($toc) > 1) {
//display ToC of two or more headings
$out = $this->renderToc($toc);
} else {
//display page abstract
$out = $this->renderAbstract($id, $meta);
}
return $out;
}
/**
* Return the TOC rendered to XHTML
*
* @param $toc
* @return string
*
* @author Andreas Gohr <[email protected]>
* @author Gerrit Uitslag <[email protected]>
*/
private function renderToc($toc)
{
global $lang;
$out = '<div class="tocheader">';
$out .= $lang['toc'];
$out .= '</div>';
$out .= '<div class="indexmenu_toc_inside">';
$out .= html_buildlist($toc, 'toc', [$this, 'formatIndexmenuListTocItem'], null, true);
$out .= '</div>';
return $out;
}
/**
* Return the page abstract rendered to XHTML
*
* @param $id
* @param array $meta by reference
* @return string
*/
private function renderAbstract($id, $meta)
{
$out = '<div class="tocheader">';
$out .= '<a href="' . wl($id) . '">';
$out .= $meta['title'] ? hsc($meta['title']) : hsc(noNS($id));
$out .= '</a>';
$out .= '</div>';
if ($meta['description']['abstract']) {
$out .= '<div class="indexmenu_toc_inside">';
$out .= p_render('xhtml', p_get_instructions($meta['description']['abstract']), $info);
$out .= '</div></div>';
}
return $out;
}
/**
* Callback for html_buildlist
*
* @param $item
* @return string
*/
public function formatIndexmenuListTocItem($item)
{
global $INPUT;
$id = cleanID($INPUT->str('id'));
if (isset($item['hid'])) {
$link = '#' . $item['hid'];
} else {
$link = $item['link'];
}
//prefix anchers with page id
if ($link[0] == '#') {
$link = wl($id, $link, false, '');
}
return '<a href="' . $link . '">' . hsc($item['title']) . '</a>';
}
/**
* Print index nodes
*
* @param $ns
* @return string
*
* @author Rene Hadler <[email protected]>
* @author Samuele Tognini <[email protected]>
* @author Andreas Gohr <[email protected]>
*/
private function printIndex($ns)
{
global $conf, $INPUT;
$idxm = new syntax_plugin_indexmenu_indexmenu();
$ns = $idxm->parseNs(rawurldecode($ns));
$level = -1;
$max = 0;
$data = [];
$skipfilecombined = [];
$skipnscombined = [];
if ($INPUT->int('max') > 0) {
$max = $INPUT->int('max');
$level = $max;
}
$nss = $INPUT->str('nss', '', true);
$sort['sort'] = $INPUT->str('sort', '', true);
$sort['msort'] = $INPUT->str('msort', '', true);
$sort['rsort'] = $INPUT->bool('rsort', false, true);
$sort['nsort'] = $INPUT->bool('nsort', false, true);
$sort['group'] = $INPUT->bool('group', false, true);
$sort['hsort'] = $INPUT->bool('hsort', false, true);
$search = new Search($sort);
$fsdir = "/" . utf8_encodeFN(str_replace(':', '/', $ns));
$skipf = utf8_decodeFN($INPUT->str('skipfile'));
$skipfilecombined[] = $this->getConf('skip_file');
if (!empty($skipf)) {
$index = 0;
if ($skipf[0] == '+') {
$index = 1;
}
$skipfilecombined[$index] = substr($skipf, 1);
}
$skipn = utf8_decodeFN($INPUT->str('skipns'));
$skipnscombined[] = $this->getConf('skip_index');
if (!empty($skipn)) {
$index = 0;
if ($skipn[0] == '+') {
$index = 1;
}
$skipnscombined[$index] = substr($skipn, 1);
}
$opts = [
'level' => $level,
'nons' => $INPUT->bool('nons', false, true),
'nss' => [[$nss, 1]],
'max' => $max,
'js' => false,
'nopg' => $INPUT->bool('nopg', false, true),
'skipnscombined' => $skipnscombined,
'skipfilecombined' => $skipfilecombined,
'headpage' => $idxm->getConf('headpage'),
'hide_headpage' => $idxm->getConf('hide_headpage')
];
if ($sort['sort'] || $sort['msort'] || $sort['rsort'] || $sort['hsort']) {
$search->customSearch($data, $conf['datadir'], [$search, 'searchIndexmenuItems'], $opts, $fsdir);
} else {
search($data, $conf['datadir'], [$search, 'searchIndexmenuItems'], $opts, $fsdir);
}
$out = '';
if ($INPUT->int('nojs') === 1) {
$idx = new Index();
$out_tmp = html_buildlist($data, 'idx', [$idxm, 'formatIndexmenuItem'], [$idx, 'tagListItem']);
$out .= preg_replace('/<ul class="idx">(.*)<\/ul>/s', "$1", $out_tmp);
} else {
$nodes = $idxm->builddTreeNodes($data, '', false);
$out = "ajxnodes = [";
$out .= rtrim($nodes[0], ",");
$out .= "];";
}
return $out;
}
/**
* Add Js & Css after template is displayed
*
* @param Event $event
*/
public function addStylesForSkins(Event $event)
{
// $event->data["link"][] = [
// "type" => "text/css",
// "rel" => "stylesheet",
// "href" => DOKU_BASE . "lib/plugins/indexmenu/scripts/fancytree/... etc etc"
// ];
// $event->data["link"][] = [
// "type" => "text/css",
// "rel" => "stylesheet",
// "href" => "//fonts.googleapis.com/icon?family=Material+Icons"
// ];
// $event->data["link"][] = [
// "type" => "text/css",
// "rel" => "stylesheet",
// "href" => "//code.getmdl.io/1.3.0/material.indigo-pink.min.css"
// ];
}
}