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