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