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