xref: /plugin/include/helper.php (revision b20a0d9bcd34203f017aa0d99105a639470b859e)
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        $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
133        for($i=0; $i<$num; $i++) {
134            // set current level
135            if($ins[$i][0] == 'section_open') {
136                $lvl = $ins[$i][1][0];
137                if($i > $range) $prev_lvl = $lvl;
138            }
139            if($ins[$i][0] == 'plugin' && $ins[$i][1][0] == 'include_include' ) {
140                // found no previous section set lvl to 0
141                if(!$lvl) $lvl = 0;
142
143                // check if toplevel is set already
144                if(!isset($this->toplevel)) $this->toplevel = $lvl;
145
146                $mode  = $ins[$i][1][1][0];
147                $page  = $ins[$i][1][1][1];
148                $sect  = $ins[$i][1][1][2];
149                $flags = $ins[$i][1][1][3];
150
151                // check if we left the range of possible sub includes and reset lvl to toplevel
152                if($range && ($i > $range)) {
153                    if($prev_lvl) {
154                        $lvl = $prev_lvl;
155                        $prev_lvl = false;
156                    } else {
157                        $lvl = $this->toplevel;
158                    }
159                }
160
161                $page = $this->_apply_macro($page);
162                resolve_pageid(getNS($id), $page, $exists); // resolve shortcuts
163                $flags  = $this->get_flags($flags);
164
165                $ins_inc = $this->_get_instructions($page, $sect, $mode, $lvl, $flags);
166
167                if(!empty($ins_inc)) {
168                    // combine instructions and reset counter
169                    $ins_start = array_slice($ins, 0, $i+1);
170                    $ins_end   = array_slice($ins, $i+1);
171                    $range = $i + count($ins_inc);
172                    $ins = array_merge($ins_start, $ins_inc, $ins_end);
173                    $num = count($ins);
174                }
175            }
176        }
177    }
178
179    /**
180     * Returns the converted instructions of a give page/section
181     *
182     * @author Michael Klier <chi@chimeric.de>
183     */
184    function _get_instructions($page, $sect, $mode, $lvl, $flags) {
185        global $ID;
186
187        if(($ID == $page) || (auth_quickaclcheck($page) < AUTH_READ) || (!page_exists($page)) && (auth_quickaclcheck($page) < AUTH_CREATE)) return array();
188        $key = ($sect) ? $page . '#' . $sect : $page;
189
190        // prevent recursion
191        if(!$this->includes[$key]) {
192            $ins = p_cached_instructions(wikiFN($page));
193            $this->includes[$key] = true;
194            $this->_convert_instructions($ins, $lvl, $page, $sect, $flags);
195            return $ins;
196        }
197    }
198
199    /**
200     * Converts instructions of the included page
201     *
202     * The funcion iterates over the given list of instructions and generates
203     * an index of header and section indicies. It also removes document
204     * start/end instructions, converts links, and removes unwanted
205     * instructions like tags, comments, linkbacks.
206     *
207     * Later all header/section levels are convertet to match the current
208     * inclusion level.
209     *
210     * @author Michael Klier <chi@chimeric.de>
211     */
212    function _convert_instructions(&$ins, $lvl, $page, $sect, $flags) {
213
214        // filter instructions if needed
215        if(!empty($sect)) {
216            $this->_get_section($ins, $sect);   // section required
217        }
218
219        if($flags['firstsec']) {
220            $this->_get_firstsec($ins, $page);  // only first section
221        }
222
223        $ns  = getNS($page);
224        $num = count($ins);
225
226        $conv_idx = array(); // conversion index
227        $lvl_max  = false;   // max level
228        $first_header = -1;
229        $no_header  = false;
230        $sect_title = false;
231
232        for($i=0; $i<$num; $i++) {
233            switch($ins[$i][0]) {
234                case 'document_start':
235                case 'document_end':
236                case 'section_edit':
237                    unset($ins[$i]);
238                    break;
239                case 'header':
240                    // get section title of first section
241                    if($sect && !$sect_title) {
242                        $sect_title = $ins[$i][1][0];
243                    }
244                    // check if we need to skip the first header
245                    if((!$no_header) && $flags['noheader']) {
246                        $no_header = true;
247                    }
248                    $conv_idx[] = $i;
249                    // get index of first header
250                    if($first_header == -1) $first_header = $i;
251                    // get max level if this instructions set
252                    if(!$lvl_max || ($ins[$i][1][1] < $lvl_max)) {
253                        $lvl_max = $ins[$i][1][1];
254                    }
255                    break;
256                case 'section_open':
257                    $conv_idx[] = $i;
258                    break;
259                case 'internallink':
260                case 'internalmedia':
261                    if($ins[$i][1][0]{0} == '.') {
262                        if($ins[$i][1][0]{1} == '.') {
263                            $ins[$i][1][0] = getNS($ns) . ':' . substr($ins[$i][1][0], 2); // parent namespace
264                        } else {
265                            $ins[$i][1][0] = $ns . ':' . substr($ins[$i][1][0], 1); // current namespace
266                        }
267                    } elseif (strpos($ins[$i][1][0], ':') === false) {
268                        $ins[$i][1][0] = $ns . ':' . $ins[$i][1][0]; // relative links
269                    }
270                    break;
271                case 'plugin':
272                    // FIXME skip other plugins?
273                    switch($ins[$i][1][0]) {
274                        case 'tag_tag':                 // skip tags
275                        case 'discussion_comments':     // skip comments
276                        case 'linkback':                // skip linkbacks
277                        case 'data_entry':              // skip data plugin
278                        case 'meta':                    // skip meta plugin
279                            unset($ins[$i]);
280                            break;
281                    }
282                    break;
283                default:
284                    break;
285            }
286        }
287
288        // calculate difference between header/section level and include level
289        $diff = 0;
290   if (!$lvl_max) $lvl_max = 0; // if no level found in taget, set to 0
291   $diff = $lvl - $lvl_max + 1;
292   if ($no_header) $diff -= 1; // push up on level if "noheader"
293
294        // convert headers and set footer/permalink
295        $hdr_deleted       = false;
296        $has_permalink = false;
297        $footer_lvl    = false;
298        foreach($conv_idx as $idx) {
299            if($ins[$idx][0] == 'header') {
300       if($no_header && !$hdr_deleted) {
301           unset ($ins[$idx]);
302           $hdr_deleted = true;
303           continue;
304       }
305                $new_lvl = (($ins[$idx][1][1] + $diff) > 5) ? 5 : ($ins[$idx][1][1] + $diff);
306                $ins[$idx][1][1] = $new_lvl;
307
308                // set permalink
309                if($flags['link'] && !$has_permalink && ($idx == $first_header)) {
310                    $this->_permalink($ins[$idx], $page, $sect, $flags);
311                    $has_permalink = true;
312                }
313            } else {
314                // it's a section
315                $new_lvl = (($ins[$idx][1][0] + $diff) > 5) ? 5 : ($ins[$idx][1][0] + $diff);
316                $ins[$idx][1][0] = $new_lvl;
317                // check if noheader is used and set the footer level to the first section
318                if($no_header && !$footer_lvl) $footer_lvl = $new_lvl;
319            }
320
321            // set footer level
322            if(!$footer_lvl && ($idx == $first_header)) $footer_lvl = $new_lvl;
323        }
324
325        // add edit button
326        if($flags['editbtn'] && (auth_quickaclcheck($page) >= AUTH_EDIT)) $this->_editbtn($ins, $page, $sect, $sect_title);
327
328        // add footer
329        if($flags['footer']) $this->_footer($ins, $page, $sect, $sect_title, $flags, $footer_lvl);
330
331        // add instructions entry divs
332        array_unshift($ins, array('plugin', array('include_div', array('open', $page))));
333        array_push($ins, array('plugin', array('include_div', array('close'))));
334
335        // close previous section if any and re-open after inclusion
336        if($lvl != 0) {
337            array_unshift($ins, array('section_close'));
338            $ins[] = array('section_open', array($lvl));
339        }
340    }
341
342    /**
343     * Appends instruction item for the include plugin footer
344     *
345     * @author Michael Klier <chi@chimeric.de>
346     */
347    function _footer(&$ins, $page, $sect, $sect_title, $flags, $footer_lvl) {
348        $footer = array();
349        $footer[0] = 'plugin';
350        $footer[1] = array('include_footer', array($page, $sect, $sect_title, $flags, $this->toplevel_id, $footer_lvl));
351        $ins[] = $footer;
352    }
353
354    /**
355     * Appends instruction item for an edit button
356     *
357     * @author Michael Klier <chi@chimeric.de>
358     */
359    function _editbtn(&$ins, $page, $sect, $sect_title) {
360        $editbtn = array();
361        $editbtn[0] = 'plugin';
362        $editbtn[1] = array('include_editbtn', array($page, $sect, $sect_title, $this->toplevel_id));
363        $ins[] = $editbtn;
364    }
365
366    /**
367     * Convert instruction item for a permalink header
368     *
369     * @author Michael Klier <chi@chimeric.de>
370     */
371    function _permalink(&$ins, $page, $sect, $flags) {
372        $ins[0] = 'plugin';
373        $ins[1] = array('include_header', array($ins[1][0], $ins[1][1], $page, $sect, $flags));
374    }
375
376    /**
377     * Get a section including its subsections
378     *
379     * @author Michael Klier <chi@chimeric.de>
380     */
381    function _get_section(&$ins, $sect) {
382        $num = count($ins);
383        $offset = false;
384        $lvl    = false;
385        $end    = false;
386
387        for($i=0; $i<$num; $i++) {
388            if ($ins[$i][0] == 'header') {
389
390                // found the right header
391                if (cleanID($ins[$i][1][0]) == $sect) {
392                    $offset = $i;
393                    $lvl    = $ins[$i][1][1];
394                } elseif ($offset && $lvl && ($ins[$i][1][1] <= $lvl)) {
395                    $end = $i - $offset;
396                    break;
397                }
398            }
399        }
400        $offset = $offset ? $offset : 0;
401        $end = $end ? $end : ($num - 1);
402        $ins = array_slice($ins, $offset, $end);
403    }
404
405    /**
406     * Only display the first section of a page and a readmore link
407     *
408     * @author Michael Klier <chi@chimeric.de>
409     */
410    function _get_firstsec(&$ins, $page) {
411        $num = count($ins);
412        $first_sect = false;
413        for($i=0; $i<$num; $i++) {
414            if($ins[$i][0] == 'section_close') {
415                $first_sect = $i;
416            }
417            if(($first_sect) && ($ins[$i][0] == 'section_open')) {
418                $ins = array_slice($ins, 0, $first_sect);
419                $ins[] = array('p_open', array());
420                $ins[] = array('internallink',array($page, $this->getLang('readmore')));
421                $ins[] = array('p_close', array());
422                $ins[] = array('section_close');
423                return;
424            }
425        }
426    }
427
428    /**
429     * Makes user or date dependent includes possible
430     */
431    function _apply_macro($id) {
432        global $INFO;
433        global $auth;
434
435        // if we don't have an auth object, do nothing
436        if (!$auth) return $id;
437
438        $user     = $_SERVER['REMOTE_USER'];
439        $userdata = $auth->getUserData($user);
440        $group    = $userdata['grps'][0];
441
442        $replace = array(
443                '@USER@'  => cleanID($user),
444                '@NAME@'  => cleanID($INFO['userinfo']['name']),
445                '@GROUP@' => cleanID($group),
446                '@YEAR@'  => date('Y'),
447                '@MONTH@' => date('m'),
448                '@DAY@'   => date('d'),
449                );
450        return str_replace(array_keys($replace), array_values($replace), $id);
451    }
452}
453//vim:ts=4:sw=4:et:enc=utf-8:
454