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