1<?php
2/**
3 * DokuWiki Plugin talkpage (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <andi@splitbrain.org>
7 */
8
9/** @inheritdoc */
10class syntax_plugin_talkpage extends DokuWiki_Syntax_Plugin
11{
12    /** @inheritdoc */
13    public function getType()
14    {
15        return 'substition';
16    }
17
18    /** @inheritdoc */
19    public function getPType()
20    {
21        return 'normal';
22    }
23
24    /** @inheritdoc */
25    public function getSort()
26    {
27        return 444;
28    }
29
30    /** @inheritdoc */
31    public function connectTo($mode)
32    {
33        $this->Lexer->addSpecialPattern('~~TALKPAGE~~', $mode, 'plugin_talkpage');
34    }
35
36    /** @inheritdoc */
37    public function handle($match, $state, $pos, Doku_Handler $handler)
38    {
39        $data = array();
40
41        return $data;
42    }
43
44    /** @inheritdoc */
45    public function render($mode, Doku_Renderer $renderer, $data)
46    {
47        if ($mode != 'xhtml') return false;
48        $renderer->info['cache'] = false;
49
50        $data = $this->getLink();
51        $renderer->doc .= '<a ' . buildAttributes($data['attr']) . '>' . $data['text'] . '</a>';
52        return true;
53    }
54
55    /**
56     * @return array text and attributes for the link
57     */
58    public function getLink()
59    {
60        global $INFO;
61        $talkns = cleanID($this->getConf('talkns'));
62
63        $attr = array();
64        if (substr($INFO['id'], 0, strlen($talkns) + 1) === "$talkns:") {
65            // we're on the talk page
66            $goto = substr($INFO['id'], strlen($talkns) + 1);
67            $text = 'back';
68        } else {
69            // we want to the talk page
70            $goto = $talkns . ':' . $INFO['id'];
71            if (page_exists($goto)) {
72                $text = 'talk';
73            } else {
74                $text = 'add';
75                $attr['rel'] = 'nofollow';
76            }
77        }
78        $attr['href'] = wl($goto);
79        $attr['class'] = 'talkpage talkpage-' . $text;
80
81        return array(
82            'goto' => $goto,
83            'type' => $text,
84            'text' => $this->getLang($text),
85            'attr' => $attr,
86        );
87    }
88}
89