Completed
Pull Request — master (#30)
by Mark
01:26
created

syntax.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * DokuWiki Syntax Plugin Backlinks.
4
 *
5
 * Shows a list of pages that link back to a given page.
6
 *
7
 * Syntax:  {{backlinks>[pagename][#filterNS|!#filterNS]}}
8
 *
9
 *   [pagename] - a valid wiki pagename or a . for the current page
10
 *   [filterNS] - a valid,absolute namespace name, optionally prepended with ! to exclude
11
 *
12
 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
13
 * @author  Michael Klier <[email protected]>
14
 * @author  Mark C. Prins <[email protected]>
15
 */
16
17
/**
18
 * All DokuWiki plugins to extend the parser/rendering mechanism
19
 * need to inherit from this class.
20
 */
21
class syntax_plugin_backlinks extends DokuWiki_Syntax_Plugin {
22
    /**
23
     * Syntax Type.
24
     *
25
     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php.
26
     *
27
     * @see DokuWiki_Syntax_Plugin::getType()
28
     */
29
    public function getType() {
30
        return 'substition';
31
    }
32
33
    /**
34
     * @see DokuWiki_Syntax_Plugin::getPType()
35
     */
36
    public function getPType() {
37
        return 'block';
38
    }
39
40
    /**
41
     * @see Doku_Parser_Mode::getSort()
42
     */
43
    public function getSort() {
44
        return 304;
45
    }
46
47
    /**
48
     * Connect pattern to lexer.
49
     *
50
     * @see Doku_Parser_Mode::connectTo()
51
     */
52
    public function connectTo($mode) {
53
        $this->Lexer->addSpecialPattern('\{\{backlinks>.+?\}\}', $mode, 'plugin_backlinks');
54
    }
55
56
    /**
57
     * Handler to prepare matched data for the rendering process.
58
     *
59
     * @see DokuWiki_Syntax_Plugin::handle()
60
     */
61
    public function handle($match, $state, $pos, Doku_Handler $handler) {
62
        // strip {{backlinks> from start and }} from end
63
        $match = substr($match, 12, -2);
64
65
        $includeNS = '';
66
        if (strstr($match, "#")) {
67
            $includeNS = substr(strstr($match, "#", FALSE), 1);
68
            $match = strstr($match, "#", TRUE);
69
        }
70
71
        return (array($match, $includeNS));
72
    }
73
74
    /**
75
     * Handles the actual output creation.
76
     *
77
     * @see DokuWiki_Syntax_Plugin::render()
78
     */
79
    public function render($mode, Doku_Renderer $renderer, $data) {
80
        global $lang;
81
        global $INFO;
82
        global $ID;
83
84
        $id = $ID;
85
            // If it's a sidebar, get the original id.
86
            if ($INFO != null) {
87
                $id = $INFO['id'];
88
            }
89
        $match = $data[0];
90
        $match = ($match == '.') ? $id : $match;
91
            if (strstr($match, ".:")) {
92
                resolve_pageid(getNS($id), $match, $exists);
0 ignored issues
show
The variable $exists does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
93
        }
94
95
        if ($mode == 'xhtml') {
96
            $renderer->info['cache'] = false;
97
98
            $backlinks = ft_backlinks($match);
99
100
            dbglog($backlinks, "backlinks: all backlinks to: $match");
101
102
            $renderer->doc .= '<div id="plugin__backlinks">'."\n";
103
104
            $filterNS = $data[1];
105
            if (!empty($backlinks) && !empty($filterNS)) {
106
                if (stripos($filterNS, "!", 0) === 0) {
107
                    $filterNS = substr($filterNS, 1);
108
                    dbglog($filterNS, "backlinks: exluding all of namespace: $filterNS");
109
                    $backlinks = array_filter($backlinks, function ($ns) use ($filterNS) {
110
                        return stripos($ns, $filterNS, 0) !== 0;
111
                    });
112
                } else {
113
                    dbglog($filterNS, "backlinks: including namespace: $filterNS only");
114
                    $backlinks = array_filter($backlinks, function ($ns) use ($filterNS) {
115
                        return stripos($ns, $filterNS, 0) === 0;
116
                    });
117
                }
118
            }
119
120
            dbglog($backlinks, "backlinks: all backlinks to be rendered");
121
122
            if (!empty($backlinks)) {
123
124
                $renderer->doc .= '<ul class="idx">';
125
126
                foreach ($backlinks as $backlink) {
127
                    $name = p_get_metadata($backlink, 'title');
128
                    if (empty($name)) {
129
                        $name = $backlink;
130
                    }
131
                    $renderer->doc .= '<li><div class="li">';
132
                    $renderer->doc .= html_wikilink(':'.$backlink, $name);
133
                    $renderer->doc .= '</div></li>'."\n";
134
                }
135
136
                $renderer->doc .= '</ul>'."\n";
137
            } else {
138
                $renderer->doc .= "<strong>Plugin Backlinks: ".$lang['nothingfound']."</strong>"."\n";
139
            }
140
141
            $renderer->doc .= '</div>'."\n";
142
143
            return true;
144
        }
145
        return false;
146
    }
147
}
148