1<?php
2/**
3 * Redirect2 - DokuWiki Redirect Manager
4 *
5 * based on DokuWiki Plugin pageredirect (Syntax Component)
6 *
7 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
8 * @author  Elan Ruusamäe <glen@delfi.ee>
9 * @author  David Lorentsen <zyberdog@quakenet.org>
10 */
11
12// must be run within Dokuwiki
13if (!defined('DOKU_INC')) die();
14
15class syntax_plugin_redirect2 extends DokuWiki_Syntax_Plugin {
16
17    public function getType() { return 'substition'; }
18    public function getPType() { return 'block'; }
19    public function getSort() { return 1; }
20
21    /**
22     * Connect lookup pattern to lexer.
23     *
24     * @param string $mode Parser mode
25     */
26    public function connectTo($mode) {
27        if (plugin_isdisabled('pageredirect')) {
28            $this->Lexer->addSpecialPattern('~~REDIRECT>.+?~~',
29                $mode, substr(get_class($this), 7) );
30            $this->Lexer->addSpecialPattern('\n#(?i:redirect)\b.*(?=\n)',
31                $mode, substr(get_class($this), 7) );
32        }
33    }
34
35    /**
36     * Handler to prepare matched data for the rendering process
37     *
38     * @param   string       $match   The text matched by the patterns
39     * @param   int          $state   The lexer state for the match
40     * @param   int          $pos     The character position of the matched text
41     * @param   Doku_Handler $handler Reference to the Doku_Handler object
42     * @return  array Return an array with all data you want to use in render
43     */
44    public function handle($match, $state, $pos, Doku_Handler $handler){
45        // extract target page from match pattern
46        if ($match[0] == '#') {     // #REDIRECT PAGE
47            $page = substr(ltrim($match), 10);
48        } else {                    // ~~REDIRECT>PAGE~~
49            $page = substr($match, 11, -2);
50        }
51        $page = trim($page);
52
53        if (!preg_match('#^(https?)://#i', $page)) {
54            $page = cleanID($page);
55            $title = p_get_metadata($page, 'title');
56            $link = html_wikilink($page, $title);
57        } else {
58            $link = '<a href="'.hsc($page).'" class="urlextern">'.hsc($page).'</a>';
59        }
60
61        // prepare message here instead of in render
62        $message = '<div class="notify">'.sprintf($this->getLang('redirect_to'), $link).'</div>';
63
64        return array($page, $message);
65    }
66
67    /**
68     * Handles the actual output creation.
69     *
70     * @param   $format   string        output format being rendered
71     * @param   $renderer Doku_Renderer reference to the current renderer object
72     * @param   $data     array         data created by handler()
73     * @return  boolean                 rendered correctly?
74     */
75    public function render($format, Doku_Renderer $renderer, $data) {
76        if ($format == 'xhtml') {
77            // add prepared note about redirection
78            $renderer->doc .= $data[1];
79            return true;
80        }
81        if ($format == 'metadata') {
82            // add redirection to metadata
83            $renderer->meta['relation']['isreplacedby'] = $data[0];
84            return true;
85        }
86        return false;
87    }
88}
89