xref: /plugin/include/helper.php (revision 8d13979c0c635dfac29350fc7b2121abb2037082)
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['noheader']  = $this->getConf('noheader');
30        $this->defaults['firstsec']  = $this->getConf('firstseconly');
31        $this->defaults['editbtn']   = $this->getConf('showeditbtn');
32        $this->defaults['taglogos']  = $this->getConf('showtaglogos');
33        $this->defaults['footer']    = $this->getConf('showfooter');
34        $this->defaults['redirect']  = $this->getConf('doredirect');
35        $this->defaults['date']      = $this->getConf('showdate');
36        $this->defaults['mdate']     = $this->getConf('showmdate');
37        $this->defaults['user']      = $this->getConf('showuser');
38        $this->defaults['comments']  = $this->getConf('showcomments');
39        $this->defaults['linkbacks'] = $this->getConf('showlinkbacks');
40        $this->defaults['tags']      = $this->getConf('showtags');
41        $this->defaults['link']      = $this->getConf('showlink');
42        $this->defaults['permalink'] = $this->getConf('showpermalink');
43        $this->defaults['indent']    = $this->getConf('doindent');
44        $this->defaults['linkonly']  = $this->getConf('linkonly');
45        $this->defaults['title']     = $this->getConf('title');
46        $this->defaults['pageexists']  = $this->getConf('pageexists');
47        $this->defaults['parlink']   = $this->getConf('parlink');
48        $this->defaults['inline']    = false;
49        $this->defaults['order']     = $this->getConf('order');
50        $this->defaults['rsort']     = $this->getConf('rsort');
51        $this->defaults['depth']     = $this->getConf('depth');
52    }
53
54    /**
55     * Available methods for other plugins
56     */
57    function getMethods() {
58        $result = array();
59        $result[] = array(
60                'name'   => 'get_flags',
61                'desc'   => 'overrides standard values for showfooter and firstseconly settings',
62                'params' => array('flags' => 'array'),
63                );
64        return $result;
65    }
66
67    /**
68     * Overrides standard values for showfooter and firstseconly settings
69     */
70    function get_flags($setflags) {
71        // load defaults
72        $flags = $this->defaults;
73        foreach ($setflags as $flag) {
74            $value = '';
75            if (strpos($flag, '=') !== -1) {
76                list($flag, $value) = explode('=', $flag, 2);
77            }
78            switch ($flag) {
79                case 'footer':
80                    $flags['footer'] = 1;
81                    break;
82                case 'nofooter':
83                    $flags['footer'] = 0;
84                    break;
85                case 'firstseconly':
86                case 'firstsectiononly':
87                    $flags['firstsec'] = 1;
88                    break;
89                case 'fullpage':
90                    $flags['firstsec'] = 0;
91                    break;
92                case 'showheader':
93                case 'header':
94                    $flags['noheader'] = 0;
95                    break;
96                case 'noheader':
97                    $flags['noheader'] = 1;
98                    break;
99                case 'editbtn':
100                case 'editbutton':
101                    $flags['editbtn'] = 1;
102                    break;
103                case 'noeditbtn':
104                case 'noeditbutton':
105                    $flags['editbtn'] = 0;
106                    break;
107                case 'permalink':
108                    $flags['permalink'] = 1;
109                    break;
110                case 'nopermalink':
111                    $flags['permalink'] = 0;
112                    break;
113                case 'redirect':
114                    $flags['redirect'] = 1;
115                    break;
116                case 'noredirect':
117                    $flags['redirect'] = 0;
118                    break;
119                case 'link':
120                    $flags['link'] = 1;
121                    break;
122                case 'nolink':
123                    $flags['link'] = 0;
124                    break;
125                case 'user':
126                    $flags['user'] = 1;
127                    break;
128                case 'nouser':
129                    $flags['user'] = 0;
130                    break;
131                case 'comments':
132                    $flags['comments'] = 1;
133                    break;
134                case 'nocomments':
135                    $flags['comments'] = 0;
136                    break;
137                case 'linkbacks':
138                    $flags['linkbacks'] = 1;
139                    break;
140                case 'nolinkbacks':
141                    $flags['linkbacks'] = 0;
142                    break;
143                case 'tags':
144                    $flags['tags'] = 1;
145                    break;
146                case 'notags':
147                    $flags['tags'] = 0;
148                    break;
149                case 'date':
150                    $flags['date'] = 1;
151                    break;
152                case 'nodate':
153                    $flags['date'] = 0;
154                    break;
155                case 'mdate':
156                    $flags['mdate'] = 1;
157                    break;
158                case 'nomdate':
159                    $flags['mdate'] = 0;
160                    break;
161                case 'indent':
162                    $flags['indent'] = 1;
163                    break;
164                case 'noindent':
165                    $flags['indent'] = 0;
166                    break;
167                case 'linkonly':
168                    $flags['linkonly'] = 1;
169                    break;
170                case 'nolinkonly':
171                case 'include_content':
172                    $flags['linkonly'] = 0;
173                    break;
174                case 'inline':
175                    $flags['inline'] = 1;
176                    break;
177                case 'title':
178                    $flags['title'] = 1;
179                    break;
180                case 'notitle':
181                    $flags['title'] = 0;
182                    break;
183                case 'pageexists':
184                    $flags['pageexists'] = 1;
185                    break;
186                case 'nopageexists':
187                    $flags['pageexists'] = 0;
188                    break;
189                case 'existlink':
190                    $flags['pageexists'] = 1;
191                    $flags['linkonly'] = 1;
192                    break;
193                case 'parlink':
194                    $flags['parlink'] = 1;
195                    break;
196                case 'noparlink':
197                    $flags['parlink'] = 0;
198                    break;
199                case 'order':
200                    $flags['order'] = $value;
201                    break;
202                case 'sort':
203                    $flags['rsort'] = 0;
204                    break;
205                case 'rsort':
206                    $flags['rsort'] = 1;
207                    break;
208                case 'depth':
209                    $flags['depth'] = max(intval($value), 0);
210                    break;
211                case 'beforeeach':
212                    $flags['beforeeach'] = $value;
213                    break;
214                case 'aftereach':
215                    $flags['aftereach'] = $value;
216                    break;
217            }
218        }
219        // the include_content URL parameter overrides flags
220        if (isset($_REQUEST['include_content']))
221            $flags['linkonly'] = 0;
222        return $flags;
223    }
224
225    /**
226     * Returns the converted instructions of a give page/section
227     *
228     * @author Michael Klier <chi@chimeric.de>
229     * @author Michael Hamann <michael@content-space.de>
230     */
231    function _get_instructions($page, $sect, $mode, $lvl, $flags, $root_id = null) {
232        $key = ($sect) ? $page . '#' . $sect : $page;
233        $this->includes[$key] = true; // legacy code for keeping compatibility with other plugins
234
235        // keep compatibility with other plugins that don't know the $root_id parameter
236        if (is_null($root_id)) {
237            global $ID;
238            $root_id = $ID;
239        }
240
241        if ($flags['linkonly']) {
242            if (page_exists($page) || $flags['pageexists']  == 0) {
243                $title = '';
244                if ($flags['title'])
245                    $title = p_get_first_heading($page);
246                if($flags['parlink']) {
247                    $ins = array(
248                        array('p_open', array()),
249                        array('internallink', array(':'.$key, $title)),
250                        array('p_close', array()),
251                    );
252                } else {
253                    $ins = array(array('internallink', array(':'.$key,$title)));
254                }
255            }else {
256                $ins = array();
257            }
258        } else {
259            if (page_exists($page)) {
260                global $ID;
261                $backupID = $ID;
262                $ID = $page; // Change the global $ID as otherwise plugins like the discussion plugin will save data for the wrong page
263                $ins = p_cached_instructions(wikiFN($page));
264                $ID = $backupID;
265            } else {
266                $ins = array();
267            }
268
269            $this->_convert_instructions($ins, $lvl, $page, $sect, $flags, $root_id);
270        }
271        return $ins;
272    }
273
274    /**
275     * Converts instructions of the included page
276     *
277     * The funcion iterates over the given list of instructions and generates
278     * an index of header and section indicies. It also removes document
279     * start/end instructions, converts links, and removes unwanted
280     * instructions like tags, comments, linkbacks.
281     *
282     * Later all header/section levels are convertet to match the current
283     * inclusion level.
284     *
285     * @author Michael Klier <chi@chimeric.de>
286     */
287    function _convert_instructions(&$ins, $lvl, $page, $sect, $flags, $root_id) {
288        global $conf;
289
290        // filter instructions if needed
291        if(!empty($sect)) {
292            $this->_get_section($ins, $sect);   // section required
293        }
294
295        if($flags['firstsec']) {
296            $this->_get_firstsec($ins, $page);  // only first section
297        }
298
299        $ns  = getNS($page);
300        $num = count($ins);
301
302        $conv_idx = array(); // conversion index
303        $lvl_max  = false;   // max level
304        $first_header = -1;
305        $no_header  = false;
306        $sect_title = false;
307        $endpos     = null; // end position of the raw wiki text
308
309        for($i=0; $i<$num; $i++) {
310            switch($ins[$i][0]) {
311                case 'document_start':
312                case 'document_end':
313                case 'section_edit':
314                    unset($ins[$i]);
315                    break;
316                case 'header':
317                    // get section title of first section
318                    if($sect && !$sect_title) {
319                        $sect_title = $ins[$i][1][0];
320                    }
321                    // check if we need to skip the first header
322                    if((!$no_header) && $flags['noheader']) {
323                        $no_header = true;
324                    }
325
326                    $conv_idx[] = $i;
327                    // get index of first header
328                    if($first_header == -1) $first_header = $i;
329                    // get max level of this instructions set
330                    if(!$lvl_max || ($ins[$i][1][1] < $lvl_max)) {
331                        $lvl_max = $ins[$i][1][1];
332                    }
333                    break;
334                case 'section_open':
335                    if ($flags['inline'])
336                        unset($ins[$i]);
337                    else
338                        $conv_idx[] = $i;
339                    break;
340                case 'section_close':
341                    if ($flags['inline'])
342                        unset($ins[$i]);
343                    break;
344                case 'internallink':
345                case 'internalmedia':
346                    // make sure parameters aren't touched
347                    $link_params = '';
348                    $link_id = $ins[$i][1][0];
349                    $link_parts = explode('?', $link_id, 2);
350                    if (count($link_parts) === 2) {
351                        $link_id = $link_parts[0];
352                        $link_params = $link_parts[1];
353                    }
354                    // resolve the id without cleaning it
355                    $link_id = resolve_id($ns, $link_id, false);
356                    // this id is internal (i.e. absolute) now, add ':' to make resolve_id work again
357                    if ($link_id{0} != ':') $link_id = ':'.$link_id;
358                    // restore parameters
359                    $ins[$i][1][0] = ($link_params != '') ? $link_id.'?'.$link_params : $link_id;
360                    break;
361                case 'plugin':
362                    // FIXME skip other plugins?
363                    switch($ins[$i][1][0]) {
364                        case 'tag_tag':                 // skip tags
365                        case 'discussion_comments':     // skip comments
366                        case 'linkback':                // skip linkbacks
367                        case 'data_entry':              // skip data plugin
368                        case 'meta':                    // skip meta plugin
369                        case 'indexmenu_tag':           // skip indexmenu sort tag
370                        case 'include_sorttag':         // skip include plugin sort tag
371                            unset($ins[$i]);
372                            break;
373                        // adapt indentation level of nested includes
374                        case 'include_include':
375                            if (!$flags['inline'] && $flags['indent'])
376                                $ins[$i][1][1][4] += $lvl;
377                            break;
378                        /*
379                         * if there is already a closelastsecedit instruction (was added by one of the section
380                         * functions), store its position but delete it as it can't be determined yet if it is needed,
381                         * i.e. if there is a header which generates a section edit (depends on the levels, level
382                         * adjustments, $no_header, ...)
383                         */
384                        case 'include_closelastsecedit':
385                            $endpos = $ins[$i][1][1][0];
386                            unset($ins[$i]);
387                            break;
388                    }
389                    break;
390                default:
391                    break;
392            }
393        }
394
395        // calculate difference between header/section level and include level
396        $diff = 0;
397        if (!isset($lvl_max)) $lvl_max = 0; // if no level found in target, set to 0
398        $diff = $lvl - $lvl_max + 1;
399        if ($no_header) $diff -= 1;  // push up one level if "noheader"
400
401        // convert headers and set footer/permalink
402        $hdr_deleted      = false;
403        $has_permalink    = false;
404        $footer_lvl       = false;
405        $contains_secedit = false;
406        $section_close_at = false;
407        foreach($conv_idx as $idx) {
408            if($ins[$idx][0] == 'header') {
409                if ($section_close_at === false) {
410                    // store the index of the first heading (the begin of the first section)
411                    $section_close_at = $idx;
412                }
413
414                if($no_header && !$hdr_deleted) {
415                    unset ($ins[$idx]);
416                    $hdr_deleted = true;
417                    continue;
418                }
419
420                if($flags['indent']) {
421                    $lvl_new = (($ins[$idx][1][1] + $diff) > 5) ? 5 : ($ins[$idx][1][1] + $diff);
422                    $ins[$idx][1][1] = $lvl_new;
423                }
424
425                if($ins[$idx][1][1] <= $conf['maxseclevel'])
426                    $contains_secedit = true;
427
428                // set permalink
429                if($flags['link'] && !$has_permalink && ($idx == $first_header)) {
430                    $this->_permalink($ins[$idx], $page, $sect, $flags);
431                    $has_permalink = true;
432                }
433
434                // set footer level
435                if(!$footer_lvl && ($idx == $first_header) && !$no_header) {
436                    if($flags['indent']) {
437                        $footer_lvl = $lvl_new;
438                    } else {
439                        $footer_lvl = $lvl_max;
440                    }
441                }
442            } else {
443                // it's a section
444                if($flags['indent']) {
445                    $lvl_new = (($ins[$idx][1][0] + $diff) > 5) ? 5 : ($ins[$idx][1][0] + $diff);
446                    $ins[$idx][1][0] = $lvl_new;
447                }
448
449                // check if noheader is used and set the footer level to the first section
450                if($no_header && !$footer_lvl) {
451                    if($flags['indent']) {
452                        $footer_lvl = $lvl_new;
453                    } else {
454                        $footer_lvl = $lvl_max;
455                    }
456                }
457            }
458        }
459
460        // close last open section of the included page if there is any
461        if ($contains_secedit) {
462            array_push($ins, array('plugin', array('include_closelastsecedit', array($endpos))));
463        }
464
465        // add edit button
466        if($flags['editbtn']) {
467            $this->_editbtn($ins, $page, $sect, $sect_title, ($flags['redirect'] ? $root_id : false));
468        }
469
470        // add footer
471        if($flags['footer']) {
472            $ins[] = $this->_footer($page, $sect, $sect_title, $flags, $footer_lvl, $root_id);
473        }
474
475        // wrap content at the beginning of the include that is not in a section in a section
476        if ($lvl > 0 && $section_close_at !== 0 && $flags['indent'] && !$flags['inline']) {
477            if ($section_close_at === false) {
478                $ins[] = array('section_close', array());
479                array_unshift($ins, array('section_open', array($lvl)));
480            } else {
481                $section_close_idx = array_search($section_close_at, array_keys($ins));
482                if ($section_close_idx > 0) {
483                    $before_ins = array_slice($ins, 0, $section_close_idx);
484                    $after_ins = array_slice($ins, $section_close_idx);
485                    $ins = array_merge($before_ins, array(array('section_close', array())), $after_ins);
486                    array_unshift($ins, array('section_open', array($lvl)));
487                }
488            }
489        }
490
491        // add instructions entry wrapper
492        array_unshift($ins, array('plugin', array('include_wrap', array('open', $page, $flags['redirect']))));
493        if (isset($flags['beforeeach']))
494            array_unshift($ins, array('entity', array($flags['beforeeach'])));
495        array_push($ins, array('plugin', array('include_wrap', array('close'))));
496        if (isset($flags['aftereach']))
497            array_push($ins, array('entity', array($flags['aftereach'])));
498
499        // close previous section if any and re-open after inclusion
500        if($lvl != 0 && $this->sec_close && !$flags['inline']) {
501            array_unshift($ins, array('section_close', array()));
502            $ins[] = array('section_open', array($lvl));
503        }
504    }
505
506    /**
507     * Appends instruction item for the include plugin footer
508     *
509     * @author Michael Klier <chi@chimeric.de>
510     */
511    function _footer($page, $sect, $sect_title, $flags, $footer_lvl, $root_id) {
512        $footer = array();
513        $footer[0] = 'plugin';
514        $footer[1] = array('include_footer', array($page, $sect, $sect_title, $flags, $root_id, $footer_lvl));
515        return $footer;
516    }
517
518    /**
519     * Appends instruction item for an edit button
520     *
521     * @author Michael Klier <chi@chimeric.de>
522     */
523    function _editbtn(&$ins, $page, $sect, $sect_title, $root_id) {
524        $title = ($sect) ? $sect_title : $page;
525        $editbtn = array();
526        $editbtn[0] = 'plugin';
527        $editbtn[1] = array('include_editbtn', array($title));
528        $ins[] = $editbtn;
529    }
530
531    /**
532     * Convert instruction item for a permalink header
533     *
534     * @author Michael Klier <chi@chimeric.de>
535     */
536    function _permalink(&$ins, $page, $sect, $flags) {
537        $ins[0] = 'plugin';
538        $ins[1] = array('include_header', array($ins[1][0], $ins[1][1], $ins[1][2], $page, $sect, $flags));
539    }
540
541    /**
542     * Get a section including its subsections
543     *
544     * @author Michael Klier <chi@chimeric.de>
545     */
546    function _get_section(&$ins, $sect) {
547        $num = count($ins);
548        $offset = false;
549        $lvl    = false;
550        $end    = false;
551        $endpos = null; // end position in the input text, needed for section edit buttons
552
553        $check = array(); // used for sectionID() in order to get the same ids as the xhtml renderer
554
555        for($i=0; $i<$num; $i++) {
556            if ($ins[$i][0] == 'header') {
557
558                // found the right header
559                if (sectionID($ins[$i][1][0], $check) == $sect) {
560                    $offset = $i;
561                    $lvl    = $ins[$i][1][1];
562                } elseif ($offset && $lvl && ($ins[$i][1][1] <= $lvl)) {
563                    $end = $i - $offset;
564                    $endpos = $ins[$i][1][2]; // the position directly after the found section, needed for the section edit button
565                    break;
566                }
567            }
568        }
569        $offset = $offset ? $offset : 0;
570        $end = $end ? $end : ($num - 1);
571        if(is_array($ins)) {
572            $ins = array_slice($ins, $offset, $end);
573            // store the end position in the include_closelastsecedit instruction so it can generate a matching button
574            $ins[] = array('plugin', array('include_closelastsecedit', array($endpos)));
575        }
576    }
577
578    /**
579     * Only display the first section of a page and a readmore link
580     *
581     * @author Michael Klier <chi@chimeric.de>
582     */
583    function _get_firstsec(&$ins, $page) {
584        $num = count($ins);
585        $first_sect = false;
586        $endpos = null; // end position in the input text
587        for($i=0; $i<$num; $i++) {
588            if($ins[$i][0] == 'section_close') {
589                $first_sect = $i;
590            }
591            if ($ins[$i][0] == 'header') {
592                /*
593                 * Store the position of the last header that is encountered. As section_close/open-instruction are
594                 * always (unless some plugin modifies this) around a header instruction this means that the last
595                 * position that is stored here is exactly the position of the section_close/open at which the content
596                 * is truncated.
597                 */
598                $endpos = $ins[$i][1][2];
599            }
600            // only truncate the content and add the read more link when there is really
601            // more than that first section
602            if(($first_sect) && ($ins[$i][0] == 'section_open')) {
603                $ins = array_slice($ins, 0, $first_sect);
604                $ins[] = array('plugin', array('include_readmore', array($page)));
605                $ins[] = array('section_close', array());
606                // store the end position in the include_closelastsecedit instruction so it can generate a matching button
607                $ins[] = array('plugin', array('include_closelastsecedit', array($endpos)));
608                return;
609            }
610        }
611    }
612
613    /**
614     * Gives a list of pages for a given include statement
615     *
616     * @author Michael Hamann <michael@content-space.de>
617     */
618    function _get_included_pages($mode, $page, $sect, $parent_id, $flags) {
619        global $conf;
620        $pages = array();
621        switch($mode) {
622        case 'namespace':
623            $page  = cleanID($page);
624            $ns    = utf8_encodeFN(str_replace(':', '/', $page));
625            // depth is absolute depth, not relative depth, but 0 has a special meaning.
626            $depth = $flags['depth'] ? $flags['depth'] + substr_count($page, ':') + ($page ? 1 : 0) : 0;
627            search($pagearrays, $conf['datadir'], 'search_allpages', array('depth' => $depth), $ns);
628            if (is_array($pagearrays)) {
629                foreach ($pagearrays as $pagearray) {
630                    if (!isHiddenPage($pagearray['id'])) // skip hidden pages
631                        $pages[] = $pagearray['id'];
632                }
633            }
634            break;
635        case 'tagtopic':
636            if (!$this->taghelper)
637                $this->taghelper =& plugin_load('helper', 'tag');
638            if(!$this->taghelper) {
639                msg('You have to install the tag plugin to use this functionality!', -1);
640                return array();
641            }
642            $tag   = $page;
643            $sect  = '';
644            $pagearrays = $this->taghelper->getTopic('', null, $tag);
645            foreach ($pagearrays as $pagearray) {
646                $pages[] = $pagearray['id'];
647            }
648            break;
649        default:
650            $page = $this->_apply_macro($page);
651            resolve_pageid(getNS($parent_id), $page, $exists); // resolve shortcuts and clean ID
652            if (auth_quickaclcheck($page) >= AUTH_READ)
653                $pages[] = $page;
654        }
655
656        if (count($pages) > 1) {
657            if ($flags['order'] === 'id') {
658                if ($flags['rsort']) {
659                    usort($pages, array($this, '_r_strnatcasecmp'));
660                } else {
661                    natcasesort($pages);
662                }
663            } else {
664                $ordered_pages = array();
665                foreach ($pages as $page) {
666                    $key = '';
667                    switch ($flags['order']) {
668                        case 'title':
669                            $key = p_get_first_heading($page);
670                            break;
671                        case 'created':
672                            $key = p_get_metadata($page, 'date created', METADATA_DONT_RENDER);
673                            break;
674                        case 'modified':
675                            $key = p_get_metadata($page, 'date modified', METADATA_DONT_RENDER);
676                            break;
677                        case 'indexmenu':
678                            $key = p_get_metadata($page, 'indexmenu_n', METADATA_RENDER_USING_SIMPLE_CACHE);
679                            if ($key === null)
680                                $key = '';
681                            break;
682                        case 'custom':
683                            $key = p_get_metadata($page, 'include_n', METADATA_RENDER_USING_SIMPLE_CACHE);
684                            if ($key === null)
685                                $key = '';
686                            break;
687                    }
688                    $key .= '_'.$page;
689                    $ordered_pages[$key] = $page;
690                }
691                if ($flags['rsort']) {
692                    uksort($ordered_pages, array($this, '_r_strnatcasecmp'));
693                } else {
694                    uksort($ordered_pages, 'strnatcasecmp');
695                }
696                $pages = $ordered_pages;
697            }
698        }
699
700        $result = array();
701        foreach ($pages as $page) {
702            $exists = page_exists($page);
703            $result[] = array('id' => $page, 'exists' => $exists, 'parent_id' => $parent_id);
704        }
705        return $result;
706    }
707
708    /**
709     * String comparisons using a "natural order" algorithm in reverse order
710     *
711     * @link http://php.net/manual/en/function.strnatcmp.php
712     * @param string $a First string
713     * @param string $b Second string
714     * @return int Similar to other string comparison functions, this one returns &lt; 0 if
715     * str1 is greater than str2; &gt;
716     * 0 if str1 is lesser than
717     * str2, and 0 if they are equal.
718     */
719    function _r_strnatcasecmp($a, $b) {
720        return strnatcasecmp($b, $a);
721    }
722
723    /**
724     * This function generates the list of all included pages from a list of metadata
725     * instructions.
726     */
727    function _get_included_pages_from_meta_instructions($instructions) {
728        $pages = array();
729        foreach ($instructions as $instruction) {
730            $mode      = $instruction['mode'];
731            $page      = $instruction['page'];
732            $sect      = $instruction['sect'];
733            $parent_id = $instruction['parent_id'];
734            $flags     = $instruction['flags'];
735            $pages = array_merge($pages, $this->_get_included_pages($mode, $page, $sect, $parent_id, $flags));
736        }
737        return $pages;
738    }
739
740    /**
741     * Makes user or date dependent includes possible
742     */
743    function _apply_macro($id) {
744        global $INFO;
745        global $auth;
746
747        // if we don't have an auth object, do nothing
748        if (!$auth) return $id;
749
750        $user     = $_SERVER['REMOTE_USER'];
751        $group    = $INFO['userinfo']['grps'][0];
752
753        $time_stamp = time();
754        if(preg_match('/@DATE(\w+)@/',$id,$matches)) {
755            switch($matches[1]) {
756            case 'PMONTH':
757                $time_stamp = strtotime("-1 month");
758                break;
759            case 'NMONTH':
760                $time_stamp = strtotime("+1 month");
761                break;
762            case 'NWEEK':
763                $time_stamp = strtotime("+1 week");
764                break;
765            case 'PWEEK':
766                $time_stamp = strtotime("-1 week");
767                break;
768            case 'TOMORROW':
769                $time_stamp = strtotime("+1 day");
770                break;
771            case 'YESTERDAY':
772                $time_stamp = strtotime("-1 day");
773                break;
774            case 'NYEAR':
775                $time_stamp = strtotime("+1 year");
776                break;
777            case 'PYEAR':
778                $time_stamp = strtotime("-1 year");
779                break;
780            }
781            $id = preg_replace('/@DATE(\w+)@/','', $id);
782        }
783
784        $replace = array(
785                '@USER@'  => cleanID($user),
786                '@NAME@'  => cleanID($INFO['userinfo']['name']),
787                '@GROUP@' => cleanID($group),
788                '@YEAR@'  => date('Y',$time_stamp),
789                '@MONTH@' => date('m',$time_stamp),
790                '@WEEK@' => date('W',$time_stamp),
791                '@DAY@'   => date('d',$time_stamp),
792                '@YEARPMONTH@' => date('Ym',strtotime("-1 month")),
793                '@PMONTH@' => date('m',strtotime("-1 month")),
794                '@NMONTH@' => date('m',strtotime("+1 month")),
795                '@YEARNMONTH@' => date('Ym',strtotime("+1 month")),
796                '@YEARPWEEK@' => date('YW',strtotime("-1 week")),
797                '@PWEEK@' => date('W',strtotime("-1 week")),
798                '@NWEEK@' => date('W',strtotime("+1 week")),
799                '@YEARNWEEK@' => date('YW',strtotime("+1 week")),
800                );
801        return str_replace(array_keys($replace), array_values($replace), $id);
802    }
803}
804// vim:ts=4:sw=4:et:
805