xref: /plugin/include/helper.php (revision d5b0d6cef14fea0e3ac981fabb4f4a7a4355df13)
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 $defaults  = array();
21    var $sec_close = true;
22    var $taghelper = null;
23    var $includes  = array(); // deprecated - compatibility code for the blog plugin
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    /**
45     * Available methods for other plugins
46     */
47    function getMethods() {
48        $result = array();
49        $result[] = array(
50                'name'   => 'get_flags',
51                'desc'   => 'overrides standard values for showfooter and firstseconly settings',
52                'params' => array('flags' => 'array'),
53                );
54        return $result;
55    }
56
57    /**
58     * Overrides standard values for showfooter and firstseconly settings
59     */
60    function get_flags($setflags) {
61        // load defaults
62        $flags = array();
63        $flags = $this->defaults;
64        foreach ($setflags as $flag) {
65            switch ($flag) {
66                case 'footer':
67                    $flags['footer'] = 1;
68                    break;
69                case 'nofooter':
70                    $flags['footer'] = 0;
71                    break;
72                case 'firstseconly':
73                case 'firstsectiononly':
74                    $flags['firstsec'] = 1;
75                    break;
76                case 'fullpage':
77                    $flags['firstsec'] = 0;
78                    break;
79                case 'noheader':
80                    $flags['noheader'] = 1;
81                    break;
82                case 'editbtn':
83                case 'editbutton':
84                    $flags['editbtn'] = 1;
85                    break;
86                case 'noeditbtn':
87                case 'noeditbutton':
88                    $flags['editbtn'] = 0;
89                    break;
90                case 'permalink':
91                    $flags['permalink'] = 1;
92                    break;
93                case 'nopermalink':
94                    $flags['permalink'] = 0;
95                    break;
96                case 'redirect':
97                    $flags['redirect'] = 1;
98                    break;
99                case 'noredirect':
100                    $flags['redirect'] = 0;
101                    break;
102                case 'link':
103                    $flags['link'] = 1;
104                    break;
105                case 'nolink':
106                    $flags['link'] = 0;
107                    break;
108                case 'user':
109                    $flags['user'] = 1;
110                    break;
111                case 'nouser':
112                    $flags['user'] = 0;
113                    break;
114                case 'comments':
115                    $flags['comments'] = 1;
116                    break;
117                case 'nocomments':
118                    $flags['comments'] = 0;
119                    break;
120                case 'linkbacks':
121                    $flags['linkbacks'] = 1;
122                    break;
123                case 'nolinkbacks':
124                    $flags['linkbacks'] = 0;
125                    break;
126                case 'tags':
127                    $flags['tags'] = 1;
128                    break;
129                case 'notags':
130                    $flags['tags'] = 0;
131                    break;
132                case 'date':
133                    $flags['date'] = 1;
134                    break;
135                case 'nodate':
136                    $flags['date'] = 0;
137                    break;
138                case 'indent':
139                    $flags['indent'] = 1;
140                    break;
141                case 'noindent':
142                    $flags['indent'] = 0;
143                    break;
144            }
145        }
146        return $flags;
147    }
148
149    /**
150     * Returns the converted instructions of a give page/section
151     *
152     * @author Michael Klier <chi@chimeric.de>
153     * @author Michael Hamann <michael@content-space.de>
154     */
155    function _get_instructions($page, $sect, $mode, $lvl, $flags, $root_id = null) {
156        $key = ($sect) ? $page . '#' . $sect : $page;
157        $this->includes[$key] = true; // legacy code for keeping compatibility with other plugins
158
159        // keep compatibility with other plugins that don't know the $root_id parameter
160        if (is_null($root_id)) {
161            global $ID;
162            $root_id = $ID;
163        }
164
165        if (page_exists($page)) {
166            $ins = p_cached_instructions(wikiFN($page));
167        } else {
168            $ins = array();
169        }
170
171        $this->_convert_instructions($ins, $lvl, $page, $sect, $flags, $root_id);
172        return $ins;
173    }
174
175    /**
176     * Converts instructions of the included page
177     *
178     * The funcion iterates over the given list of instructions and generates
179     * an index of header and section indicies. It also removes document
180     * start/end instructions, converts links, and removes unwanted
181     * instructions like tags, comments, linkbacks.
182     *
183     * Later all header/section levels are convertet to match the current
184     * inclusion level.
185     *
186     * @author Michael Klier <chi@chimeric.de>
187     */
188    function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id) {
189        global $conf;
190
191        // filter instructions if needed
192        if(!empty($sect)) {
193            $this->_get_section($ins, $sect);   // section required
194        }
195
196        if($flags['firstsec']) {
197            $this->_get_firstsec($ins, $page);  // only first section
198        }
199
200        $ns  = getNS($page);
201        $num = count($ins);
202
203        $conv_idx = array(); // conversion index
204        $lvl_max  = false;   // max level
205        $first_header = -1;
206        $no_header  = false;
207        $sect_title = false;
208
209        for($i=0; $i<$num; $i++) {
210            switch($ins[$i][0]) {
211                case 'document_start':
212                case 'document_end':
213                case 'section_edit':
214                    unset($ins[$i]);
215                    break;
216                case 'header':
217                    // get section title of first section
218                    if($sect && !$sect_title) {
219                        $sect_title = $ins[$i][1][0];
220                    }
221                    // check if we need to skip the first header
222                    if((!$no_header) && $flags['noheader']) {
223                        $no_header = true;
224                    }
225
226                    $conv_idx[] = $i;
227                    // get index of first header
228                    if($first_header == -1) $first_header = $i;
229                    // get max level of this instructions set
230                    if(!$lvl_max || ($ins[$i][1][1] < $lvl_max)) {
231                        $lvl_max = $ins[$i][1][1];
232                    }
233                    break;
234                case 'section_open':
235                    $conv_idx[] = $i;
236                    break;
237                case 'internallink':
238                case 'internalmedia':
239                    if($ins[$i][1][0]{0} == '.') {
240                        if($ins[$i][1][0]{1} == '.') {
241                            $ins[$i][1][0] = getNS($ns) . ':' . substr($ins[$i][1][0], 2); // parent namespace
242                        } else {
243                            $ins[$i][1][0] = $ns . ':' . substr($ins[$i][1][0], 1); // current namespace
244                        }
245                    } elseif (strpos($ins[$i][1][0], ':') === false) {
246                        $ins[$i][1][0] = $ns . ':' . $ins[$i][1][0]; // relative links
247                    }
248                    break;
249                case 'plugin':
250                    // FIXME skip other plugins?
251                    switch($ins[$i][1][0]) {
252                        case 'tag_tag':                 // skip tags
253                        case 'discussion_comments':     // skip comments
254                        case 'linkback':                // skip linkbacks
255                        case 'data_entry':              // skip data plugin
256                        case 'meta':                    // skip meta plugin
257                            unset($ins[$i]);
258                            break;
259                        // adapt indentation level of nested includes
260                        case 'include_include':
261                            $ins[$i][1][1][4] += $lvl;
262                            break;
263                    }
264                    break;
265                default:
266                    break;
267            }
268        }
269
270        // calculate difference between header/section level and include level
271        $diff = 0;
272        if (!isset($lvl_max)) $lvl_max = 0; // if no level found in target, set to 0
273        $diff = $lvl - $lvl_max + 1;
274        if ($no_header) $diff -= 1;  // push up one level if "noheader"
275
276        // convert headers and set footer/permalink
277        $hdr_deleted      = false;
278        $has_permalink    = false;
279        $footer_lvl       = false;
280        $contains_secedit = false;
281        $section_close_at = false;
282        foreach($conv_idx as $idx) {
283            if($ins[$idx][0] == 'header') {
284                if ($section_close_at === false) {
285                    // store the index of the first heading (the begin of the first section)
286                    $section_close_at = $idx;
287                }
288
289                if($no_header && !$hdr_deleted) {
290                    unset ($ins[$idx]);
291                    $hdr_deleted = true;
292                    continue;
293                }
294
295                if($flags['indent']) {
296                    $lvl_new = (($ins[$idx][1][1] + $diff) > 5) ? 5 : ($ins[$idx][1][1] + $diff);
297                    $ins[$idx][1][1] = $lvl_new;
298                }
299
300                if($ins[$idx][1][1] <= $conf['maxseclevel'])
301                    $contains_secedit = true;
302
303                // set permalink
304                if($flags['link'] && !$has_permalink && ($idx == $first_header)) {
305                    $this->_permalink($ins[$idx], $page, $sect, $flags);
306                    $has_permalink = true;
307                }
308
309                // set footer level
310                if(!$footer_lvl && ($idx == $first_header) && !$no_header) {
311                    if($flags['indent']) {
312                        $footer_lvl = $lvl_new;
313                    } else {
314                        $footer_lvl = $lvl_max;
315                    }
316                }
317            } else {
318                // it's a section
319                if($flags['indent']) {
320                    $lvl_new = (($ins[$idx][1][0] + $diff) > 5) ? 5 : ($ins[$idx][1][0] + $diff);
321                    $ins[$idx][1][0] = $lvl_new;
322                }
323
324                // check if noheader is used and set the footer level to the first section
325                if($no_header && !$footer_lvl) {
326                    if($flags['indent']) {
327                        $footer_lvl = $lvl_new;
328                    } else {
329                        $footer_lvl = $lvl_max;
330                    }
331                }
332            }
333        }
334
335        // close last open section of the included page if there is any
336        if ($contains_secedit) {
337            array_push($ins, array('plugin', array('include_close_last_secedit', array())));
338        }
339
340        // add edit button
341        if($flags['editbtn']) {
342            $perm = auth_quickaclcheck($page);
343            $can_edit = page_exists($page) ? $perm >= AUTH_EDIT : $perm >= AUTH_CREATE;
344            if ($can_edit)
345                $this->_editbtn($ins, $page, $sect, $sect_title, $root_id);
346        }
347
348        // add footer
349        if($flags['footer']) {
350            $ins[] = $this->_footer($page, $sect, $sect_title, $flags, $footer_lvl, $root_id);
351        }
352
353        // wrap content at the beginning of the include that is not in a section in a section
354        if ($lvl > 0 && $section_close_at !== 0) {
355            if ($section_close_at === false) {
356                $ins[] = array('section_close', array());
357                array_unshift($ins, array('section_open', array($lvl)));
358            } else {
359                $section_close_idx = array_search($section_close_at, array_keys($ins));
360                if ($section_close_idx > 0) {
361                    $before_ins = array_slice($ins, 0, $section_close_idx);
362                    $after_ins = array_slice($ins, $section_close_idx);
363                    $ins = array_merge($before_ins, array(array('section_close', array())), $after_ins);
364                    array_unshift($ins, array('section_open', array($lvl)));
365                }
366            }
367        }
368
369        // add instructions entry wrapper
370        array_unshift($ins, array('plugin', array('include_wrap', array('open', $page))));
371        array_push($ins, array('plugin', array('include_wrap', array('close'))));
372
373        // close previous section if any and re-open after inclusion
374        if($lvl != 0 && $this->sec_close) {
375            array_unshift($ins, array('section_close', array()));
376            $ins[] = array('section_open', array($lvl));
377        }
378    }
379
380    /**
381     * Appends instruction item for the include plugin footer
382     *
383     * @author Michael Klier <chi@chimeric.de>
384     */
385    function _footer($page, $sect, $sect_title, $flags, $footer_lvl, $root_id) {
386        $footer = array();
387        $footer[0] = 'plugin';
388        $footer[1] = array('include_footer', array($page, $sect, $sect_title, $flags, $root_id, $footer_lvl));
389        return $footer;
390    }
391
392    /**
393     * Appends instruction item for an edit button
394     *
395     * @author Michael Klier <chi@chimeric.de>
396     */
397    function _editbtn(&$ins, $page, $sect, $sect_title, $root_id) {
398        $editbtn = array();
399        $editbtn[0] = 'plugin';
400        $editbtn[1] = array('include_editbtn', array($page, $sect, $sect_title, $root_id));
401        $ins[] = $editbtn;
402    }
403
404    /**
405     * Convert instruction item for a permalink header
406     *
407     * @author Michael Klier <chi@chimeric.de>
408     */
409    function _permalink(&$ins, $page, $sect, $flags) {
410        $ins[0] = 'plugin';
411        $ins[1] = array('include_header', array($ins[1][0], $ins[1][1], $page, $sect, $flags));
412    }
413
414    /**
415     * Get a section including its subsections
416     *
417     * @author Michael Klier <chi@chimeric.de>
418     */
419    function _get_section(&$ins, $sect) {
420        $num = count($ins);
421        $offset = false;
422        $lvl    = false;
423        $end    = false;
424
425        for($i=0; $i<$num; $i++) {
426            if ($ins[$i][0] == 'header') {
427
428                // found the right header
429                if (cleanID($ins[$i][1][0]) == $sect) {
430                    $offset = $i;
431                    $lvl    = $ins[$i][1][1];
432                } elseif ($offset && $lvl && ($ins[$i][1][1] <= $lvl)) {
433                    $end = $i - $offset;
434                    break;
435                }
436            }
437        }
438        $offset = $offset ? $offset : 0;
439        $end = $end ? $end : ($num - 1);
440        if(is_array($ins)) {
441            $ins = array_slice($ins, $offset, $end);
442        }
443    }
444
445    /**
446     * Only display the first section of a page and a readmore link
447     *
448     * @author Michael Klier <chi@chimeric.de>
449     */
450    function _get_firstsec(&$ins, $page) {
451        $num = count($ins);
452        $first_sect = false;
453        for($i=0; $i<$num; $i++) {
454            if($ins[$i][0] == 'section_close') {
455                $first_sect = $i;
456            }
457            if(($first_sect) && ($ins[$i][0] == 'section_open')) {
458                $ins = array_slice($ins, 0, $first_sect);
459                $ins[] = array('p_open', array());
460                $ins[] = array('internallink', array($page, $this->getLang('readmore')));
461                $ins[] = array('p_close', array());
462                $ins[] = array('section_close', array());
463                return;
464            }
465        }
466    }
467
468    /**
469     * Gives a list of pages for a given include statement
470     *
471     * @author Michael Hamann <michael@content-space.de>
472     */
473    function _get_included_pages($mode, $page, $sect, $parent_id) {
474        global $conf;
475        $pages = array();
476        switch($mode) {
477        case 'namespace':
478            $ns    = str_replace(':', '/', cleanID($page));
479            search($pagearrays, $conf['datadir'], 'search_list', '', $ns);
480            if (is_array($pagearrays)) {
481                foreach ($pagearrays as $pagearray) {
482                    $pages[] = $pagearray['id'];
483                }
484            }
485            break;
486        case 'tagtopic':
487            if (!$this->taghelper)
488                $this->taghelper =& plugin_load('helper', 'tag');
489            if(!$this->taghelper) {
490                msg('You have to install the tag plugin to use this functionality!', -1);
491                return array();
492            }
493            $tag   = $page;
494            $sect  = '';
495            $pagearrays = $this->taghelper->getTopic('', null, $tag);
496            foreach ($pagearrays as $pagearray) {
497                $pages[] = $pagearray['id'];
498            }
499            break;
500        default:
501            $page = $this->_apply_macro($page);
502            resolve_pageid(getNS($parent_id), $page, $exists); // resolve shortcuts and clean ID
503            if (auth_quickaclcheck($page) >= AUTH_READ)
504                $pages[] = $page;
505        }
506
507        sort($pages);
508
509        $result = array();
510        foreach ($pages as $page) {
511            $perm = auth_quickaclcheck($page);
512            $exists = page_exists($page);
513            $result[] = array('id' => $page, 'exists' => $exists, 'can_edit' => ($perm >= AUTH_EDIT), 'parent_id' => $parent_id);
514        }
515        return $result;
516    }
517
518    /**
519     * This function generates the list of all included pages from a list of metadata
520     * instructions.
521     */
522    function _get_included_pages_from_meta_instructions($instructions) {
523        $pages = array();
524        foreach ($instructions as $instruction) {
525            extract($instruction);
526            $pages = array_merge($pages, $this->_get_included_pages($mode, $page, $sect, $parent_id));
527        }
528        return $pages;
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:
556