xref: /plugin/include/helper.php (revision 71ec110157357319e914a434bc535b5b0c2fb3bd)
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
16class helper_plugin_include extends DokuWiki_Plugin { // DokuWiki_Helper_Plugin
17
18    var $pages     = array();   // filechain of included pages
19    var $page      = array();   // associative array with data about the page to include
20    var $ins       = array();   // instructions array
21    var $doc       = '';        // the final output XHTML string
22    var $mode      = 'section'; // inclusion mode: 'page' or 'section'
23    var $clevel    = 0;         // current section level
24    var $firstsec  = 0;         // show first section only
25    var $editbtn   = 1;         // show edit button
26    var $footer    = 1;         // show metaline below page
27    var $noheader  = 0;         // omit header
28    var $permalink = 0;         // make first headline permalink to included page
29    var $header    = array();   // included page / section header
30    var $renderer  = NULL;      // DokuWiki renderer object
31
32    var $INCLUDE_LIMIT = 12;
33
34    // private variables
35    var $_offset   = NULL;
36
37    /**
38     * Constructor loads some config settings
39     */
40    function helper_plugin_include() {
41        $this->firstsec = $this->getConf('firstseconly');
42        $this->editbtn  = $this->getConf('showeditbtn');
43        $this->footer   = $this->getConf('showfooter');
44        $this->noheader = 0;
45        $this->permalink = 0;
46        $this->header   = array();
47    }
48
49    function getInfo() {
50        return array(
51                'author' => 'Gina Häußge, Michael Klier, Esther Brunner',
52                'email'  => 'dokuwiki@chimeric.de',
53                'date'   => '2008-06-28',
54                'name'   => 'Include Plugin (helper class)',
55                'desc'   => 'Functions to include another page in a wiki page',
56                'url'    => 'http://wiki.splitbrain.org/plugin:include',
57                );
58    }
59
60    function getMethods() {
61        $result = array();
62        $result[] = array(
63                'name'   => 'setPage',
64                'desc'   => 'sets the page to include',
65                'params' => array("page attributes, 'id' required, 'section' for filtering" => 'array'),
66                'return' => array('success' => 'boolean'),
67                );
68        $result[] = array(
69                'name'   => 'setMode',
70                'desc'   => 'sets inclusion mode: should indention be merged?',
71                'params' => array("'page' (original) or 'section' (merged indention)" => 'string'),
72                );
73        $result[] = array(
74                'name'   => 'setLevel',
75                'desc'   => 'sets the indention for the current section level',
76                'params' => array('level: 0 to 5' => 'integer'),
77                'return' => array('success' => 'boolean'),
78                );
79        $result[] = array(
80                'name'   => 'setFlags',
81                'desc'   => 'overrides standard values for showfooter and firstseconly settings',
82                'params' => array('flags' => 'array'),
83                );
84        $result[] = array(
85                'name'   => 'renderXHTML',
86                'desc'   => 'renders the XHTML output of the included page',
87                'params' => array('DokuWiki renderer' => 'object'),
88                'return' => array('XHTML' => 'string'),
89                );
90        return $result;
91    }
92
93    /**
94     * Sets the page to include if it is not already included (prevent recursion)
95     * and the current user is allowed to read it
96     */
97    function setPage($page) {
98        global $ID;
99
100        $id     = $page['id'];
101        $fullid = $id.'#'.$page['section'];
102
103        if (!$id) return false;       // no page id given
104        if ($id == $ID) return false; // page can't include itself
105
106        // prevent include recursion
107        if ($this->_in_filechain($id,$page['section']) || (count($this->pages) >= $this->INCLUDE_LIMIT)) return false;
108
109        // we need to make sure 'perm', 'file' and 'exists' are set
110        if (!isset($page['perm'])) $page['perm'] = auth_quickaclcheck($page['id']);
111        if (!isset($page['file'])) $page['file'] = wikiFN($page['id']);
112        if (!isset($page['exists'])) $page['exists'] = @file_exists($page['file']);
113
114        // check permission
115        if ($page['perm'] < AUTH_READ) return false;
116
117        // add the page to the filechain
118        $this->page = $page;
119        return true;
120    }
121
122    function _push_page($id,$section) {
123        global $ID;
124        if (empty($this->pages)) array_push($this->pages, $ID.'#');
125        array_push($this->pages, $id.'#'.$section);
126    }
127
128    function _pop_page() {
129        $page = array_pop($this->pages);
130        if (count($this->pages=1)) $this->pages = array();
131
132        return $page;
133    }
134
135    function _in_filechain($id,$section) {
136        $pattern = $section ? "/^($id#$section|$id#)$/" : "/^$id#/";
137        $match = preg_grep($pattern, $this->pages);
138
139        return (!empty($match));
140    }
141
142    /**
143     * Sets the inclusion mode: 'page' or 'section'
144     */
145    function setMode($mode) {
146        $this->mode = $mode;
147    }
148
149    /**
150     * Sets the right indention for a given section level
151     */
152    function setLevel($level) {
153        if ((is_numeric($level)) && ($level >= 0) && ($level <= 5)) {
154            $this->clevel = $level;
155            return true;
156        }
157        return false;
158    }
159
160    /**
161     * Overrides standard values for showfooter and firstseconly settings
162     */
163    function setFlags($flags) {
164        foreach ($flags as $flag) {
165            switch ($flag) {
166                case 'footer':
167                    $this->footer = 1;
168                    break;
169                case 'nofooter':
170                    $this->footer = 0;
171                    break;
172                case 'firstseconly':
173                case 'firstsectiononly':
174                    $this->firstsec = 1;
175                    break;
176                case 'fullpage':
177                    $this->firstsec = 0;
178                    break;
179                case 'noheader':
180                    $this->noheader = 1;
181                    break;
182                case 'editbtn':
183                case 'editbutton':
184                    $this->editbtn = 1;
185                    break;
186                case 'noeditbtn':
187                case 'noeditbutton':
188                    $this->editbtn = 0;
189                    break;
190                case 'permalink':
191                    $this->permalink = 1;
192                    break;
193                case 'nopermalink':
194                    $this->permalink = 0;
195                    break;
196            }
197        }
198    }
199
200    /**
201     * Builds the XHTML to embed the page to include
202     */
203    function renderXHTML(&$renderer, &$info) {
204        global $ID;
205
206        if (!$this->page['id']) return ''; // page must be set first
207        if (!$this->page['exists'] && ($this->page['perm'] < AUTH_CREATE)) return '';
208
209        $this->_push_page($this->page['id'],$this->page['section']);
210
211        // prepare variables
212        $rdoc  = $renderer->doc;
213        $doc = '';
214        $this->renderer =& $renderer;
215
216        $page = $this->page;
217        $clevel = $this->clevel;
218        $mode = $this->mode;
219
220        // exchange page ID for included one
221        $backupID = $ID;               // store the current ID
222        $ID       = $this->page['id']; // change ID to the included page
223
224        // get instructions and render them on the fly
225        $this->ins = p_cached_instructions($this->page['file']);
226
227        // show only a given section?
228        if ($this->page['section'] && $this->page['exists']) $this->_getSection();
229
230        // convert relative links
231        $this->_convertInstructions();
232
233        $xhtml = p_render('xhtml', $this->ins, $info);
234        $ID = $backupID;               // restore ID
235
236        $this->mode = $mode;
237        $this->clevel = $clevel;
238        $this->page = $page;
239
240        // render the included page
241        $content = '<div class="entry-content">'.DOKU_LF.
242            $this->_cleanXHTML($xhtml).DOKU_LF.
243            '</div><!-- .entry-content -->'.DOKU_LF;
244
245        // restore ID
246        $ID = $backupID;
247
248        // embed the included page
249        $class = ($this->page['draft'] ? 'include draft' : 'include');
250
251        $doc .= DOKU_LF.'<!-- including '.$this->page['id'].' // '.$this->page['file'].' -->'.DOKU_LF;
252        $doc .= '<div class="'.$class.' hentry"'.$this->_showTagLogos().'>'.DOKU_LF;
253        if (!$this->header && $this->clevel && ($this->mode == 'section'))
254            $doc .= '<div class="level'.$this->clevel.'">'.DOKU_LF;
255
256        if ((@file_exists(DOKU_PLUGIN.'editsections/action.php'))
257                && (!plugin_isdisabled('editsections'))) { // for Edit Section Reorganizer Plugin
258            $doc .= $this->_editButton().$content;
259        } else {
260            $doc .= $content.$this->_editButton();
261        }
262
263        // output meta line (if wanted) and remove page from filechain
264        $doc .= $this->_footer($this->page);
265
266        if (!$this->header && $this->clevel && ($this->mode == 'section'))
267            $doc .= '</div>'.DOKU_LF; // class="level?"
268        $doc .= '</div>'.DOKU_LF; // class="include hentry"
269        $doc .= DOKU_LF.'<!-- /including '.$this->page['id'].' -->'.DOKU_LF;
270
271        // reset defaults
272        $this->helper_plugin_include();
273        $this->_pop_page();
274
275        // return XHTML
276        $renderer->doc = $rdoc.$doc;
277        return $doc;
278    }
279
280    /* ---------- Private Methods ---------- */
281
282    /**
283     * Get a section including its subsections
284     */
285    function _getSection() {
286        foreach ($this->ins as $ins) {
287            if ($ins[0] == 'header') {
288
289                // found the right header
290                if (cleanID($ins[1][0]) == $this->page['section']) {
291                    $level = $ins[1][1];
292                    $i[] = $ins;
293
294                    // next header of the same or higher level -> exit
295                } elseif ($ins[1][1] <= $level) {
296                    $this->ins = $i;
297                    return true;
298                } elseif (isset($level)) {
299                    $i[] = $ins;
300                }
301
302                // add instructions from our section
303            } elseif (isset($level)) {
304                $i[] = $ins;
305            }
306        }
307        $this->ins = $i;
308        return true;
309    }
310
311    /**
312     * Corrects relative internal links and media and
313     * converts headers of included pages to subheaders of the current page
314     */
315    function _convertInstructions() {
316        global $ID;
317
318        if (!$this->page['exists']) return false;
319
320        // check if included page is in same namespace
321        $ns      = getNS($this->page['id']);
322        $convert = (getNS($ID) == $ns ? false : true);
323
324        $n = count($this->ins);
325        for ($i = 0; $i < $n; $i++) {
326            $current = $this->ins[$i][0];
327
328            // convert internal links and media from relative to absolute
329            if ($convert && (substr($current, 0, 8) == 'internal')) {
330                $this->ins[$i][1][0] = $this->_convertInternalLink($this->ins[$i][1][0], $ns);
331
332                // set header level to current section level + header level
333            } elseif ($current == 'header') {
334                $this->_convertHeader($i);
335
336                // the same for sections
337            } elseif (($current == 'section_open') && ($this->mode == 'section')) {
338                $this->ins[$i][1][0] = $this->_convertSectionLevel($this->ins[$i][1][0]);
339
340                // show only the first section?
341            } elseif ($this->firstsec && ($current == 'section_close')
342                    && ($this->ins[$i-1][0] != 'section_open')) {
343                $this->_readMore($i);
344                return true;
345            }
346        }
347        $this->_finishConvert();
348        return true;
349    }
350
351    /**
352     * Convert relative internal links and media
353     *
354     * @param    integer $i: counter for current instruction
355     * @param    string  $ns: namespace of included page
356     * @return   string  $link: converted, now absolute link
357     */
358    function _convertInternalLink($link, $ns) {
359
360        // relative subnamespace
361        if ($link{0} == '.') {
362            if ($link{1} == '.') return getNS($ns).':'.substr($link, 2); // parent namespace
363            else return $ns.':'.substr($link, 1);                        // current namespace
364
365            // relative link
366        } elseif (strpos($link, ':') === false) {
367            return $ns.':'.$link;
368
369            // absolute link - don't change
370        } else {
371            return $link;
372        }
373    }
374
375    /**
376     * Convert header level and add header to TOC
377     *
378     * @param    integer $i: counter for current instruction
379     * @return   boolean true
380     */
381    function _convertHeader($i) {
382        global $conf;
383
384        $text = $this->ins[$i][1][0];
385        $hid  = $this->renderer->_headerToLink($text, 'true');
386        if (empty($this->header)) {
387            $this->_offset = $this->clevel - $this->ins[$i][1][1] + 1;
388            $level = $this->_convertSectionLevel(1);
389            $this->header = array('hid' => $hid, 'title' => hsc($text), 'level' => $level);
390            if ($this->noheader) {
391                unset($this->ins[$i]);
392                return true;
393            } else if ($this->permalink){
394                $this->ins[$i] = $this->_permalinkHeader($text, $level, $pos);
395            }
396        } else {
397            $level = $this->_convertSectionLevel($this->ins[$i][1][1]);
398        }
399        if ($this->mode == 'section') {
400            if ($this->permalink) {
401                $this->ins[$i][1][1][1] = $level;
402            } else {
403                $this->ins[$i][1][1] = $level;
404            }
405        }
406
407        // add TOC item
408        if (($level >= $conf['toptoclevel']) && ($level <= $conf['maxtoclevel'])) {
409            $this->renderer->toc[] = array(
410                    'hid'   => $hid,
411                    'title' => $text,
412                    'type'  => 'ul',
413                    'level' => $level - $conf['toptoclevel'] + 1
414                    );
415        }
416        return true;
417    }
418
419    /**
420     * Create instruction item for a permalink header
421     *
422     * @param   string  $text: Headline text
423     * @param   integer $level: Headline level
424     * @param   integer $pos: I wish I knew what this is for...
425     *
426     * @author Gina Haeussge <osd@foosel.net>
427     */
428    function _permalinkHeader($text, $level, $pos) {
429        $newIns = array(
430            'plugin',
431            array(
432                'include_header',
433                array(
434                    $text,
435                    $level,
436                    $pos
437                ),
438            ),
439            $pos
440        );
441
442        return $newIns;
443    }
444
445    /**
446     * Convert the level of headers and sections
447     *
448     * @param    integer $in: current level
449     * @return   integer $out: converted level
450     */
451    function _convertSectionLevel($in) {
452        $out = $in + $this->_offset;
453        if ($out >= 5) return 5;
454        if ($out <= $this->clevel + 1) return $this->clevel + 1;
455        return $out;
456    }
457
458    /**
459     * Adds a read more... link at the bottom of the first section
460     *
461     * @param    integer $i: counter for current instruction
462     * @return   boolean true
463     */
464    function _readMore($i) {
465        $more = ((is_array($this->ins[$i+1])) && ($this->ins[$i+1][0] != 'document_end'));
466
467        if ($this->ins[0][0] == 'document_start') $this->ins = array_slice($this->ins, 1, $i);
468        else $this->ins = array_slice($this->ins, 0, $i);
469
470        if ($more) {
471            array_unshift($this->ins, array('document_start', array(), 0));
472            $last = array_pop($this->ins);
473            $this->ins[] = array('p_open', array(), $last[2]);
474            $this->ins[] = array('internallink',array($this->page['id'], $this->getLang('readmore')),$last[2]);
475            $this->ins[] = array('p_close', array(), $last[2]);
476            $this->ins[] = $last;
477            $this->ins[] = array('document_end', array(), $last[2]);
478        } else {
479            $this->_finishConvert();
480        }
481        return true;
482    }
483
484    /**
485     * Adds 'document_start' and 'document_end' instructions if not already there
486     */
487    function _finishConvert() {
488        if ($this->ins[0][0] != 'document_start')
489            array_unshift($this->ins, array('document_start', array(), 0));
490        $c = count($this->ins) - 1;
491        if ($this->ins[$c][0] != 'document_end')
492            $this->ins[] = array('document_end', array(), 0);
493    }
494
495    /**
496     * Remove TOC, section edit buttons and tags
497     */
498    function _cleanXHTML($xhtml) {
499        $replace = array(
500                '!<div class="toc">.*?(</div>\n</div>)!s'   => '', // remove toc
501                '#<!-- SECTION "(.*?)" \[(\d+-\d*)\] -->#e' => '', // remove section edit buttons
502                '!<div class="tags">.*?(</div>)!s'          => '', // remove category tags
503                );
504        if ($this->clevel)
505            $replace['#<div class="footnotes">#s'] = '<div class="footnotes level'.$this->clevel.'">';
506        $xhtml  = preg_replace(array_keys($replace), array_values($replace), $xhtml);
507        return $xhtml;
508    }
509
510    /**
511     * Optionally display logo for the first tag found in the included page
512     */
513    function _showTagLogos() {
514        if ((!$this->getConf('showtaglogos'))
515                || (plugin_isdisabled('tag'))
516                || (!$taghelper =& plugin_load('helper', 'tag')))
517            return '';
518
519        $subject = p_get_metadata($this->page['id'], 'subject');
520        if (is_array($subject)) $tag = $subject[0];
521        else list($tag, $rest) = explode(' ', $subject, 2);
522        $title = str_replace('_', ' ', noNS($tag));
523        resolve_pageid($taghelper->namespace, $tag, $exists); // resolve shortcuts
524
525        $logosrc = mediaFN($logoID);
526        $types = array('.png', '.jpg', '.gif'); // auto-detect filetype
527        foreach ($types as $type) {
528            if (!@file_exists($logosrc.$type)) continue;
529            $logoID   = $tag.$type;
530            $logosrc .= $type;
531            list($w, $h, $t, $a) = getimagesize($logosrc);
532            return ' style="min-height: '.$h.'px">'.
533                '<img class="mediaright" src="'.ml($logoID).'" alt="'.$title.'"/';
534        }
535        return '';
536    }
537
538    /**
539     * Display an edit button for the included page
540     */
541    function _editButton() {
542        if ($this->page['exists']) {
543            if (($this->page['perm'] >= AUTH_EDIT) && (is_writable($this->page['file'])))
544                $action = 'edit';
545            else return '';
546        } elseif ($this->page['perm'] >= AUTH_CREATE) {
547            $action = 'create';
548        }
549        if ($this->editbtn) {
550            return '<div class="secedit">'.DOKU_LF.DOKU_TAB.
551                html_btn($action, $this->page['id'], '', array('do' => 'edit'), 'post').DOKU_LF.
552                '</div>'.DOKU_LF;
553        } else {
554            return '';
555        }
556    }
557
558    /**
559     * Returns the meta line below the included page
560     */
561    function _footer($page) {
562        global $conf, $ID;
563
564        if (!$this->footer) return ''; // '<div class="inclmeta">&nbsp;</div>'.DOKU_LF;
565
566        $id   = $page['id'];
567        $meta = p_get_metadata($id);
568        $ret  = array();
569
570        // permalink
571        if ($this->getConf('showlink')) {
572            $title = ($page['title'] ? $page['title'] : $meta['title']);
573            if (!$title) $title = str_replace('_', ' ', noNS($id));
574            $class = ($page['exists'] ? 'wikilink1' : 'wikilink2');
575            $link = array(
576                    'url'    => wl($id),
577                    'title'  => $id,
578                    'name'   => hsc($title),
579                    'target' => $conf['target']['wiki'],
580                    'class'  => $class.' permalink',
581                    'more'   => 'rel="bookmark"',
582                    );
583            $ret[] = $this->renderer->_formatLink($link);
584        }
585
586        // date
587        if ($this->getConf('showdate')) {
588            $date = ($page['date'] ? $page['date'] : $meta['date']['created']);
589            if ($date)
590                $ret[] = '<abbr class="published" title="'.strftime('%Y-%m-%dT%H:%M:%SZ', $date).'">'.
591                    strftime($conf['dformat'], $date).
592                    '</abbr>';
593        }
594
595        // author
596        if ($this->getConf('showuser')) {
597            $author   = ($page['user'] ? $page['user'] : $meta['creator']);
598            if ($author) {
599                $userpage = cleanID($this->getConf('usernamespace').':'.$author);
600                resolve_pageid(getNS($ID), $userpage, $exists);
601                $class = ($exists ? 'wikilink1' : 'wikilink2');
602                $link = array(
603                        'url'    => wl($userpage),
604                        'title'  => $userpage,
605                        'name'   => hsc($author),
606                        'target' => $conf['target']['wiki'],
607                        'class'  => $class.' url fn',
608                        'pre'    => '<span class="vcard author">',
609                        'suf'    => '</span>',
610                        );
611                $ret[]    = $this->renderer->_formatLink($link);
612            }
613        }
614
615        // comments - let Discussion Plugin do the work for us
616        if (!$page['section'] && $this->getConf('showcomments')
617                && (!plugin_isdisabled('discussion'))
618                && ($discussion =& plugin_load('helper', 'discussion'))) {
619            $disc = $discussion->td($id);
620            if ($disc) $ret[] = '<span class="comment">'.$disc.'</span>';
621        }
622
623        // linkbacks - let Linkback Plugin do the work for us
624        if (!$page['section'] && $this->getConf('showlinkbacks')
625                && (!plugin_isdisabled('linkback'))
626                && ($linkback =& plugin_load('helper', 'linkback'))) {
627            $link = $linkback->td($id);
628            if ($link) $ret[] = '<span class="linkback">'.$link.'</span>';
629        }
630
631        $ret = implode(DOKU_LF.DOKU_TAB.'&middot; ', $ret);
632
633        // tags - let Tag Plugin do the work for us
634        if (!$page['section'] && $this->getConf('showtags')
635                && (!plugin_isdisabled('tag'))
636                && ($tag =& plugin_load('helper', 'tag'))) {
637            $page['tags'] = '<div class="tags"><span>'.DOKU_LF.
638                DOKU_TAB.$tag->td($id).DOKU_LF.
639                DOKU_TAB.'</span></div>'.DOKU_LF;
640            $ret = $page['tags'].DOKU_TAB.$ret;
641        }
642
643        if (!$ret) $ret = '&nbsp;';
644        $class = 'inclmeta';
645        if ($this->header && $this->clevel && ($this->mode == 'section'))
646            $class .= ' level'.$this->clevel;
647        return '<div class="'.$class.'">'.DOKU_LF.DOKU_TAB.$ret.DOKU_LF.'</div>'.DOKU_LF;
648    }
649
650    /**
651     * Builds the ODT to embed the page to include
652     */
653    function renderODT(&$renderer) {
654        global $ID;
655
656        if (!$this->page['id']) return ''; // page must be set first
657        if (!$this->page['exists'] && ($this->page['perm'] < AUTH_CREATE)) return '';
658
659        // prepare variable
660        $this->renderer =& $renderer;
661
662        // get instructions and render them on the fly
663        $this->ins = p_cached_instructions($this->page['file']);
664
665        // show only a given section?
666        if ($this->page['section'] && $this->page['exists']) $this->_getSection();
667
668        // convert relative links
669        $this->_convertInstructions();
670
671        // render the included page
672        $backupID = $ID;               // store the current ID
673        $ID       = $this->page['id']; // change ID to the included page
674        // remove document_start and document_end to avoid zipping
675        $this->ins = array_slice($this->ins, 1, -1);
676        p_render('odt', $this->ins, $info);
677        $ID = $backupID;               // restore ID
678        // reset defaults
679        $this->helper_plugin_include();
680    }
681}
682//vim:ts=4:sw=4:et:enc=utf-8:
683