xref: /plugin/include/helper.php (revision 3a39ce39e8d4f84e01581e2c3f0063ef5790a00a)
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
226        for($i=0; $i<$num; $i++) {
227            switch($ins[$i][0]) {
228                case 'document_start':
229                case 'document_end':
230                case 'section_edit':
231                    unset($ins[$i]);
232                    break;
233                case 'header':
234                    $conv_idx[] = $i;
235                    // get index of first header
236                    if($first_header == -1) $first_header = $i;
237                    // get max level if this instructions set
238                    if(!$lvl_max || ($ins[$i][1][1] < $lvl_max)) {
239                        $lvl_max = $ins[$i][1][1];
240                    }
241                    break;
242                case 'section_open':
243                    $conv_idx[] = $i;
244                    break;
245                case 'internallink':
246                case 'internalmedia':
247                    if($ins[$i][1][0]{0} == '.') {
248                        if($ins[$i][1][0]{1} == '.') {
249                            $ins[$i][1][0] = getNS($ns) . ':' . substr($ins[$i][1][0], 2); // parent namespace
250                        } else {
251                            $ins[$i][1][0] = $ns . ':' . substr($ins[$i][1][0], 1); // current namespace
252                        }
253                    } elseif (strpos($ins[$i][1][0], ':') === false) {
254                        $ins[$i][1][0] = $ns . ':' . $ins[$i][1][0]; // relative links
255                    }
256                    break;
257                case 'plugin':
258                    // FIXME skip other plugins?
259                    if($ins[$i][1][0] == 'tag_tag') unset($ins[$i]);                // skip tags
260                    if($ins[$i][1][0] == 'discussion_comments') unset($ins[$i]);    // skip comments
261                    if($ins[$i][1][0] == 'linkback') unset($ins[$i]);               // skip linkbacks
262                    break;
263                default:
264                    break;
265            }
266        }
267
268        // calculate difference between header/section level and include level
269        $diff = 0;
270        if($lvl_max) {
271            // max level equals inclusion level diff is 1
272            if($lvl_max == $lvl) {
273                $diff = 1;
274            }
275            // max level is les than inclusion level, we have to convert downwards
276            if($lvl_max < $lvl) {
277                $diff = (($lvl - $lvl_max) + 1);
278            }
279            // max level is greate inclusion level, we have to convert upwards
280            if($lvl_max > $lvl) {
281                if($lvl == 0) {
282                    // we had no previous section pretend it was 1
283                    $diff = (1 - $lvl_max);
284                } elseif(($lvl - $lvl_max) == -1) {
285                    // we don't need to convert anything up diff is 0
286                    $diff = 0;
287                } else {
288                    // convert everything up
289                    $diff = ($lvl - $lvl_max);
290                }
291            }
292        }
293
294        // convert headers and set footer/permalink
295        $has_permalink = false;
296        $footer_lvl    = false;
297        foreach($conv_idx as $idx) {
298            if($ins[$idx][0] == 'header') {
299                $new_lvl = (($ins[$idx][1][1] + $diff) > 5) ? 5 : ($ins[$idx][1][1] + $diff);
300                $ins[$idx][1][1] = $new_lvl;
301
302                // get section title if we only include one section
303                if(($sect && !$sect_title) && ($idx == $first_header)) {
304                    $sect_title = $ins[$idx][1][0];
305                }
306
307                // set permalink
308                if($flags['link'] && !$has_permalink && ($idx == $first_header)) {
309                    $this->_permalink($ins[$idx], $page, $sect, $flags);
310                    $has_permalink = true;
311                }
312            } else {
313                // it's a section
314                $new_lvl = (($ins[$idx][1][0] + $diff) > 5) ? 5 : ($ins[$idx][1][0] + $diff);
315                $ins[$idx][1][0] = $new_lvl;
316            }
317
318            // set footer level
319            if(!$footer_lvl && ($idx == $first_header)) $footer_lvl = $new_lvl;
320        }
321
322        // add edit button
323        if($flags['editbtn']) $this->_editbtn($ins, $page, $sect, $sect_title);
324
325        // add footer
326        if($flags['footer']) $this->_footer($ins, $page, $sect, $sect_title, $flags, $footer_lvl);
327
328        // add instructions entry divs
329        array_unshift($ins, array('plugin', array('include_div', array('open'))));
330        array_push($ins, array('plugin', array('include_div', array('close'))));
331
332        // close previous section if any and re-open after inclusion
333        if($lvl != 0) {
334            array_unshift($ins, array('section_close'));
335            $ins[] = array('section_open', array($lvl));
336        }
337    }
338
339    /**
340     * Appends instruction item for the include plugin footer
341     *
342     * @author Michael Klier <chi@chimeric.de>
343     */
344    function _footer(&$ins, $page, $sect, $sect_title, $flags, $footer_lvl) {
345        $footer = array();
346        $footer[0] = 'plugin';
347        $footer[1] = array('include_footer', array($page, $sect, $sect_title, $flags, $this->toplevel_id, $footer_lvl));
348        $ins[] = $footer;
349    }
350
351    /**
352     * Appends instruction item for an edit button
353     *
354     * @author Michael Klier <chi@chimeric.de>
355     */
356    function _editbtn(&$ins, $page, $sect, $sect_title) {
357        $editbtn = array();
358        $editbtn[0] = 'plugin';
359        $editbtn[1] = array('include_editbtn', array($page, $sect, $sect_title, $this->toplevel_id));
360        $ins[] = $editbtn;
361    }
362
363    /**
364     * Convert instruction item for a permalink header
365     *
366     * @author Michael Klier <chi@chimeric.de>
367     */
368    function _permalink(&$ins, $page, $sect, $flags) {
369        $ins[0] = 'plugin';
370        $ins[1] = array('include_header', array($ins[1][0], $ins[1][1], $page, $sect, $flags));
371    }
372
373    /**
374     * Get a section including its subsections
375     *
376     * @author Michael Klier <chi@chimeric.de>
377     */
378    function _get_section(&$ins, $sect) {
379        $num = count($ins);
380        $offset = false;
381        $lvl    = false;
382
383        for($i=0; $i<$num; $i++) {
384            if ($ins[$i][0] == 'header') {
385
386                // found the right header
387                if (cleanID($ins[$i][1][0]) == $sect) {
388                    $offset = $i;
389                    $lvl    = $ins[$i][1][1];
390                } elseif ($offset && $lvl && ($ins[$i][1][1] <= $lvl)) {
391                    $ins = array_slice($ins, $offset, ($i - $offset));
392                }
393            }
394        }
395    }
396
397    /**
398     * Only display the first section of a page and a readmore link
399     *
400     * @author Michael Klier <chi@chimeric.de>
401     */
402    function _get_firstsec(&$ins, $page) {
403        $num = count($ins);
404        for($i=0; $i<$num; $i++) {
405            if($ins[$i][0] == 'section_close') {
406                $ins = array_slice($ins, 0, $i);
407                $ins[] = array('p_open', array());
408                $ins[] = array('internallink',array($page, $this->getLang('readmore')));
409                $ins[] = array('p_close', array());
410                $ins[] = array('section_close');
411                return;
412            }
413        }
414    }
415
416    /**
417     * Makes user or date dependent includes possible
418     */
419    function _apply_macro($id) {
420        global $INFO;
421        global $auth;
422
423        // if we don't have an auth object, do nothing
424        if (!$auth) return $id;
425
426        $user     = $_SERVER['REMOTE_USER'];
427        $userdata = $auth->getUserData($user);
428        $group    = $userdata['grps'][0];
429
430        $replace = array(
431                '@USER@'  => cleanID($user),
432                '@NAME@'  => cleanID($INFO['userinfo']['name']),
433                '@GROUP@' => cleanID($group),
434                '@YEAR@'  => date('Y'),
435                '@MONTH@' => date('m'),
436                '@DAY@'   => date('d'),
437                );
438        return str_replace(array_keys($replace), array_values($replace), $id);
439    }
440}
441//vim:ts=4:sw=4:et:enc=utf-8:
442