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