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