1<?php
2
3use dokuwiki\Extension\SyntaxPlugin;
4
5/**
6 * Redirect2 - DokuWiki Redirect Manager
7 *
8 * based on DokuWiki Plugin pageredirect (Syntax Component)
9 *
10 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11 * @author  Elan Ruusamäe <glen@delfi.ee>
12 * @author  David Lorentsen <zyberdog@quakenet.org>
13 */
14class syntax_plugin_redirect2 extends SyntaxPlugin
15{
16    public function getType() { return 'substition'; }
17    public function getPType() { return 'block'; }
18    public function getSort() { return 1; }
19
20    /**
21     * Connect lookup pattern to lexer.
22     *
23     * @param string $mode Parser mode
24     */
25    public function connectTo($mode)
26    {
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    {
46        // extract target page from match pattern
47        if ($match[0] == '#') {     // #REDIRECT PAGE
48            $page = substr(ltrim($match), 10);
49        } else {                    // ~~REDIRECT>PAGE~~
50            $page = substr($match, 11, -2);
51        }
52        $page = trim($page);
53
54        if (!preg_match('#^(https?)://#i', $page)) {
55            $page = cleanID($page);
56            $title = p_get_metadata($page, 'title');
57            $link = html_wikilink($page, $title);
58        } else {
59            $link = '<a href="'.hsc($page).'" class="urlextern">'.hsc($page).'</a>';
60        }
61
62        // prepare message here instead of in render
63        $message = '<div class="notify">'.sprintf($this->getLang('redirect_to'), $link).'</div>';
64
65        return array($page, $message);
66    }
67
68    /**
69     * Handles the actual output creation.
70     *
71     * @param   $format   string        output format being rendered
72     * @param   $renderer Doku_Renderer reference to the current renderer object
73     * @param   $data     array         data created by handler()
74     * @return  boolean                 rendered correctly?
75     */
76    public function render($format, Doku_Renderer $renderer, $data)
77    {
78        if ($format == 'xhtml') {
79            // add prepared note about redirection
80            $renderer->doc .= $data[1];
81            return true;
82        }
83        if ($format == 'metadata') {
84            // add redirection to metadata
85            $renderer->meta['relation']['isreplacedby'] = $data[0];
86            return true;
87        }
88        return false;
89    }
90}
91