xref: /plugin/include/helper.php (revision cd4be22655f6a88efc96f70197e6f7955d7276bc)
1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Esther Brunner <wikidesign@gmail.com>
5 * @author     Christopher Smith <chris@jalakai.co.uk>
6 * @author     Gina Häußge, Michael Klier <dokuwiki@chimeric.de>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
15
16require_once(DOKU_INC.'inc/search.php');
17
18class helper_plugin_include extends DokuWiki_Plugin { // DokuWiki_Helper_Plugin
19
20    var $includes     = array();
21    var $hasparts     = array();
22    var $toplevel_id  = NULL;
23    var $toplevel     = 0;
24    var $defaults     = array();
25    var $include_key  = '';
26
27    /**
28     * Constructor loads default config settings once
29     */
30    function helper_plugin_include() {
31        $this->defaults['firstsec']  = $this->getConf('firstseconly');
32        $this->defaults['editbtn']   = $this->getConf('showeditbtn');
33        $this->defaults['taglogos']  = $this->getConf('showtaglogos');
34        $this->defaults['footer']    = $this->getConf('showfooter');
35        $this->defaults['redirect']  = $this->getConf('doredirect');
36        $this->defaults['date']      = $this->getConf('showdate');
37        $this->defaults['user']      = $this->getConf('showuser');
38        $this->defaults['comments']  = $this->getConf('showcomments');
39        $this->defaults['linkbacks'] = $this->getConf('showlinkbacks');
40        $this->defaults['tags']      = $this->getConf('showtags');
41        $this->defaults['link']      = $this->getConf('showlink');
42        $this->defaults['permalink'] = $this->getConf('showpermalink');
43        $this->defaults['indent']    = $this->getConf('doindent');
44    }
45
46    /**
47     * Available methods for other plugins
48     */
49    function getMethods() {
50        $result = array();
51        $result[] = array(
52                'name'   => 'get_flags',
53                'desc'   => 'overrides standard values for showfooter and firstseconly settings',
54                'params' => array('flags' => 'array'),
55                );
56        return $result;
57    }
58
59    /**
60     * Overrides standard values for showfooter and firstseconly settings
61     */
62    function get_flags($setflags) {
63        // load defaults
64        $flags = array();
65        $flags = $this->defaults;
66        foreach ($setflags as $flag) {
67            switch ($flag) {
68                case 'footer':
69                    $flags['footer'] = 1;
70                    break;
71                case 'nofooter':
72                    $flags['footer'] = 0;
73                    break;
74                case 'firstseconly':
75                case 'firstsectiononly':
76                    $flags['firstsec'] = 1;
77                    break;
78                case 'fullpage':
79                    $flags['firstsec'] = 0;
80                    break;
81                case 'noheader':
82                    $flags['noheader'] = 1;
83                    break;
84                case 'editbtn':
85                case 'editbutton':
86                    $flags['editbtn'] = 1;
87                    break;
88                case 'noeditbtn':
89                case 'noeditbutton':
90                    $flags['editbtn'] = 0;
91                    break;
92                case 'permalink':
93                    $flags['permalink'] = 1;
94                    break;
95                case 'nopermalink':
96                    $flags['permalink'] = 0;
97                    break;
98                case 'redirect':
99                    $flags['redirect'] = 1;
100                    break;
101                case 'noredirect':
102                    $flags['redirect'] = 0;
103                    break;
104                case 'link':
105                    $flags['link'] = 1;
106                    break;
107                case 'nolink':
108                    $flags['link'] = 0;
109                    break;
110                case 'user':
111                    $flags['user'] = 1;
112                    break;
113                case 'nouser':
114                    $flags['user'] = 0;
115                    break;
116                case 'comments':
117                    $flags['comments'] = 1;
118                    break;
119                case 'nocomments':
120                    $flags['comments'] = 0;
121                    break;
122                case 'linkbacks':
123                    $flags['linkbacks'] = 1;
124                    break;
125                case 'nolinkbacks':
126                    $flags['linkbacks'] = 0;
127                    break;
128                case 'tags':
129                    $flags['tags'] = 1;
130                    break;
131                case 'notags':
132                    $flags['tags'] = 0;
133                    break;
134                case 'date':
135                    $flags['date'] = 1;
136                    break;
137                case 'nodate':
138                    $flags['date'] = 0;
139                    break;
140                case 'indent':
141                    $flags['indent'] = 1;
142                    break;
143                case 'noindent':
144                    $flags['indent'] = 0;
145                    break;
146            }
147        }
148        return $flags;
149    }
150
151    /**
152     * Parses the instructions list of the page which contains the includes
153     *
154     * @author Michael Klier <chi@chimeric.de>
155     */
156    function parse_instructions($id, &$ins) {
157        global $conf;
158        global $INFO;
159
160        $num = count($ins);
161
162        $lvl      = false;
163        $prev_lvl = 0;
164        $mode     = '';
165        $page     = '';
166        $flags    = array();
167        $range    = false;
168        $scope    = $id;
169
170        for($i=0; $i<$num; $i++) {
171            // set current level
172            if($ins[$i][0] == 'section_open') {
173                $lvl = $ins[$i][1][0];
174                if($i > $range) $prev_lvl = $lvl;
175            }
176
177            if($ins[$i][0] == 'plugin' && $ins[$i][1][0] == 'include_include' ) {
178                // found no previous section set lvl to 0
179                if(!$lvl) $lvl = 0;
180
181                $mode  = $ins[$i][1][1][0];
182
183                if($mode == 'namespace') {
184                    $ns    = str_replace(':', '/', cleanID($ins[$i][1][1][1]));
185                    $sect  = '';
186                    $flags = $ins[$i][1][1][3];
187
188                    $pages = array();
189                    search($pages, $conf['datadir'], 'search_list', '', $ns);
190                    sort($pages);
191
192                    if(!empty($pages)) {
193                        $ins_inc = array();
194                        foreach($pages as $page) {
195                            $perm = auth_quickaclcheck($page['id']);
196                            array_push($this->hasparts, $page['id']);
197                            if($perm < AUTH_READ) continue;
198                            $ins_tmp[0]       = 'plugin';
199                            $ins_tmp[1][0]    = 'include_include';
200                            $ins_tmp[1][1][0] = 'page';
201                            $ins_tmp[1][1][1] = $page['id'];
202                            $ins_tmp[1][1][2] = '';
203                            $ins_tmp[1][1][3] = $flags;
204                            $ins_inc = array_merge($ins_inc, array($ins_tmp));
205                        }
206                        $ins_start = array_slice($ins, 0, $i+1);
207                        $ins_end   = array_slice($ins, $i+1);
208                        $ins       = array_merge($ins_start, $ins_inc, $ins_end);
209                    }
210                    unset($ins[$i]);
211                    $i--;
212                }
213
214                if($mode == 'page' || $mode == 'section') {
215                    $page  = $ins[$i][1][1][1];
216                    $perm = auth_quickaclcheck($page);
217
218                    array_push($this->hasparts, $page);
219                    if($perm < AUTH_READ) continue;
220
221                    $sect  = $ins[$i][1][1][2];
222                    $flags = $ins[$i][1][1][3];
223
224                    $page = $this->_apply_macro($page);
225                    resolve_pageid(getNS($scope), $page, $exists); // resolve shortcuts
226                    $ins[$i][1][1][4] = $scope;
227                    $scope = $page;
228                    $flags = $this->get_flags($flags);
229
230                    if(!page_exists($page)) {
231                        if($flags['footer']) {
232                            $ins[$i] = $this->_footer($page, $sect, '', $flags, 0);
233                        } else {
234                            unset($ins[$i]);
235                        }
236                    } else {
237                        $ins_inc = $this->_get_instructions($page, $sect, $mode, $lvl, $flags);
238                        if(!empty($ins_inc)) {
239                            // combine instructions and reset counter
240                            $ins_start = array_slice($ins, 0, $i+1);
241                            $ins_end   = array_slice($ins, $i+1);
242                            $range = $i + count($ins_inc);
243                            $ins = array_merge($ins_start, $ins_inc, $ins_end);
244                            $num = count($ins);
245                        }
246                    }
247                }
248            }
249
250            // check if we left the range of possible sub includes and reset lvl and scope to toplevel_id
251            if($range && ($i >= $range)) {
252                $lvl = ($prev_lvl == 0) ? 0 : $prev_lvl;
253                $range    = false;
254                // reset scope to toplevel_id
255                $scope = $this->toplevel_id;
256            }
257        }
258
259        if(!empty($INFO['userinfo'])) {
260            $include_key = $INFO['userinfo']['name'] . '|' . implode('|', $INFO['userinfo']['grps']);
261        } else {
262            $include_key = '@ALL';
263        }
264
265        // handle meta data
266        $meta = array();
267        $meta = p_get_metadata($id, 'plugin_include');
268        $meta['pages'] = array_unique($this->hasparts);
269        $meta['keys'][$include_key] = true;
270        $ins_meta    = array();
271        $ins_meta[0] = 'plugin';
272        $ins_meta[1] = array('include_meta', array($meta));
273        array_push($ins, $ins_meta);
274    }
275
276    /**
277     * Returns the converted instructions of a give page/section
278     *
279     * @author Michael Klier <chi@chimeric.de>
280     */
281    function _get_instructions($page, $sect, $mode, $lvl, $flags) {
282        $key = ($sect) ? $page . '#' . $sect : $page;
283
284        // prevent recursion
285        if(!$this->includes[$key]) {
286            $ins = p_cached_instructions(wikiFN($page));
287            $this->includes[$key] = true;
288            $this->_convert_instructions($ins, $lvl, $page, $sect, $flags);
289            return $ins;
290        }
291    }
292
293    /**
294     * Converts instructions of the included page
295     *
296     * The funcion iterates over the given list of instructions and generates
297     * an index of header and section indicies. It also removes document
298     * start/end instructions, converts links, and removes unwanted
299     * instructions like tags, comments, linkbacks.
300     *
301     * Later all header/section levels are convertet to match the current
302     * inclusion level.
303     *
304     * @author Michael Klier <chi@chimeric.de>
305     */
306    function _convert_instructions(&$ins, $lvl, $page, $sect, $flags) {
307
308        // filter instructions if needed
309        if(!empty($sect)) {
310            $this->_get_section($ins, $sect);   // section required
311        }
312
313        if($flags['firstsec']) {
314            $this->_get_firstsec($ins, $page);  // only first section
315        }
316
317        $ns  = getNS($page);
318        $num = count($ins);
319
320        $conv_idx = array(); // conversion index
321        $lvl_max  = false;   // max level
322        $first_header = -1;
323        $no_header  = false;
324        $sect_title = false;
325
326        for($i=0; $i<$num; $i++) {
327            switch($ins[$i][0]) {
328                case 'document_start':
329                case 'document_end':
330                case 'section_edit':
331                    unset($ins[$i]);
332                    break;
333                case 'header':
334                    // get section title of first section
335                    if($sect && !$sect_title) {
336                        $sect_title = $ins[$i][1][0];
337                    }
338                    // check if we need to skip the first header
339                    if((!$no_header) && $flags['noheader']) {
340                        $no_header = true;
341                    }
342
343                    $conv_idx[] = $i;
344                    // get index of first header
345                    if($first_header == -1) $first_header = $i;
346                    // get max level of this instructions set
347                    if(!$lvl_max || ($ins[$i][1][1] < $lvl_max)) {
348                        $lvl_max = $ins[$i][1][1];
349                    }
350                    break;
351                case 'section_open':
352                    $conv_idx[] = $i;
353                    break;
354                case 'internallink':
355                case 'internalmedia':
356                    if($ins[$i][1][0]{0} == '.') {
357                        if($ins[$i][1][0]{1} == '.') {
358                            $ins[$i][1][0] = getNS($ns) . ':' . substr($ins[$i][1][0], 2); // parent namespace
359                        } else {
360                            $ins[$i][1][0] = $ns . ':' . substr($ins[$i][1][0], 1); // current namespace
361                        }
362                    } elseif (strpos($ins[$i][1][0], ':') === false) {
363                        $ins[$i][1][0] = $ns . ':' . $ins[$i][1][0]; // relative links
364                    }
365                    break;
366                case 'plugin':
367                    // FIXME skip other plugins?
368                    switch($ins[$i][1][0]) {
369                        case 'tag_tag':                 // skip tags
370                        case 'discussion_comments':     // skip comments
371                        case 'linkback':                // skip linkbacks
372                        case 'data_entry':              // skip data plugin
373                        case 'meta':                    // skip meta plugin
374                            unset($ins[$i]);
375                            break;
376                    }
377                    break;
378                default:
379                    break;
380            }
381        }
382
383        // calculate difference between header/section level and include level
384        $diff = 0;
385        if (!isset($lvl_max)) $lvl_max = 0; // if no level found in target, set to 0
386        $diff = $lvl - $lvl_max + 1;
387        if ($no_header) $diff -= 1;  // push up one level if "noheader"
388
389        // convert headers and set footer/permalink
390        $hdr_deleted   = false;
391        $has_permalink = false;
392        $footer_lvl    = false;
393        foreach($conv_idx as $idx) {
394            if($ins[$idx][0] == 'header') {
395                if($no_header && !$hdr_deleted) {
396                    unset ($ins[$idx]);
397                    $hdr_deleted = true;
398                    continue;
399                }
400
401                if($flags['indent']) {
402                    $lvl_new = (($ins[$idx][1][1] + $diff) > 5) ? 5 : ($ins[$idx][1][1] + $diff);
403                    $ins[$idx][1][1] = $lvl_new;
404                }
405
406                // set permalink
407                if($flags['link'] && !$has_permalink && ($idx == $first_header)) {
408                    $this->_permalink($ins[$idx], $page, $sect, $flags);
409                    $has_permalink = true;
410                }
411
412                // set footer level
413                if(!$footer_lvl && ($idx == $first_header) && !$no_header) {
414                    if($flags['indent']) {
415                        $footer_lvl = $lvl_new;
416                    } else {
417                        $footer_lvl = $lvl_max;
418                    }
419                }
420            } else {
421                // it's a section
422                if($flags['indent']) {
423                    $lvl_new = (($ins[$idx][1][0] + $diff) > 5) ? 5 : ($ins[$idx][1][0] + $diff);
424                    $ins[$idx][1][0] = $lvl_new;
425                }
426
427                // check if noheader is used and set the footer level to the first section
428                if($no_header && !$footer_lvl) {
429                    if($flags['indent']) {
430                        $footer_lvl = $lvl_new;
431                    } else {
432                        $footer_lvl = $lvl_max;
433                    }
434                }
435            }
436        }
437
438        // add edit button
439        if($flags['editbtn'] && (auth_quickaclcheck($page) >= AUTH_EDIT)) {
440            $this->_editbtn($ins, $page, $sect, $sect_title);
441        }
442
443        // add footer
444        if($flags['footer']) {
445            $ins[] = $this->_footer($page, $sect, $sect_title, $flags, $footer_lvl);
446        }
447
448        // add instructions entry divs
449        array_unshift($ins, array('plugin', array('include_div', array('open', $page))));
450        array_push($ins, array('plugin', array('include_div', array('close'))));
451
452        // close previous section if any and re-open after inclusion
453        if($lvl != 0) {
454            array_unshift($ins, array('section_close'));
455            $ins[] = array('section_open', array($lvl));
456        }
457    }
458
459    /**
460     * Appends instruction item for the include plugin footer
461     *
462     * @author Michael Klier <chi@chimeric.de>
463     */
464    function _footer($page, $sect, $sect_title, $flags, $footer_lvl) {
465        $footer = array();
466        $footer[0] = 'plugin';
467        $footer[1] = array('include_footer', array($page, $sect, $sect_title, $flags, $this->toplevel_id, $footer_lvl));
468        return $footer;
469    }
470
471    /**
472     * Appends instruction item for an edit button
473     *
474     * @author Michael Klier <chi@chimeric.de>
475     */
476    function _editbtn(&$ins, $page, $sect, $sect_title) {
477        $editbtn = array();
478        $editbtn[0] = 'plugin';
479        $editbtn[1] = array('include_editbtn', array($page, $sect, $sect_title, $this->toplevel_id));
480        $ins[] = $editbtn;
481    }
482
483    /**
484     * Convert instruction item for a permalink header
485     *
486     * @author Michael Klier <chi@chimeric.de>
487     */
488    function _permalink(&$ins, $page, $sect, $flags) {
489        $ins[0] = 'plugin';
490        $ins[1] = array('include_header', array($ins[1][0], $ins[1][1], $page, $sect, $flags));
491    }
492
493    /**
494     * Get a section including its subsections
495     *
496     * @author Michael Klier <chi@chimeric.de>
497     */
498    function _get_section(&$ins, $sect) {
499        $num = count($ins);
500        $offset = false;
501        $lvl    = false;
502        $end    = false;
503
504        for($i=0; $i<$num; $i++) {
505            if ($ins[$i][0] == 'header') {
506
507                // found the right header
508                if (cleanID($ins[$i][1][0]) == $sect) {
509                    $offset = $i;
510                    $lvl    = $ins[$i][1][1];
511                } elseif ($offset && $lvl && ($ins[$i][1][1] <= $lvl)) {
512                    $end = $i - $offset;
513                    break;
514                }
515            }
516        }
517        $offset = $offset ? $offset : 0;
518        $end = $end ? $end : ($num - 1);
519        if(is_array($ins)) {
520            $ins = array_slice($ins, $offset, $end);
521        }
522    }
523
524    /**
525     * Only display the first section of a page and a readmore link
526     *
527     * @author Michael Klier <chi@chimeric.de>
528     */
529    function _get_firstsec(&$ins, $page) {
530        $num = count($ins);
531        $first_sect = false;
532        for($i=0; $i<$num; $i++) {
533            if($ins[$i][0] == 'section_close') {
534                $first_sect = $i;
535            }
536            if(($first_sect) && ($ins[$i][0] == 'section_open')) {
537                $ins = array_slice($ins, 0, $first_sect);
538                $ins[] = array('p_open', array());
539                $ins[] = array('internallink',array($page, $this->getLang('readmore')));
540                $ins[] = array('p_close', array());
541                $ins[] = array('section_close');
542                return;
543            }
544        }
545    }
546
547    /**
548     * Makes user or date dependent includes possible
549     */
550    function _apply_macro($id) {
551        global $INFO;
552        global $auth;
553
554        // if we don't have an auth object, do nothing
555        if (!$auth) return $id;
556
557        $user     = $_SERVER['REMOTE_USER'];
558        $group    = $INFO['userinfo']['grps'][0];
559
560        $replace = array(
561                '@USER@'  => cleanID($user),
562                '@NAME@'  => cleanID($INFO['userinfo']['name']),
563                '@GROUP@' => cleanID($group),
564                '@YEAR@'  => date('Y'),
565                '@MONTH@' => date('m'),
566                '@DAY@'   => date('d'),
567                );
568        return str_replace(array_keys($replace), array_values($replace), $id);
569    }
570}
571//vim:ts=4:sw=4:et:enc=utf-8:
572