Completed
Pull Request — master (#19)
by Mark
01:37
created

syntax.php (2 issues)

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
// must be run within Dokuwiki
18
if (!defined('DOKU_INC')) die();
19
20
/**
21
 * All DokuWiki plugins to extend the parser/rendering mechanism
22
 * need to inherit from this class
23
 */
24
class syntax_plugin_backlinks extends DokuWiki_Syntax_Plugin {
25
    /**
26
     * Syntax Type.
27
     *
28
     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
29
     * @see DokuWiki_Syntax_Plugin::getType()
30
     */
31
    function getType() { return 'substition'; }
32
33
    /**
34
     * @see DokuWiki_Syntax_Plugin::getPType()
35
     */
36
    function getPType() { return 'block'; }
37
38
    /**
39
     * @see Doku_Parser_Mode::getSort()
40
     */
41
    function getSort() { return 304; }
42
43
    /**
44
     * Connect pattern to lexer.
45
     * @see Doku_Parser_Mode::connectTo()
46
     */
47
    function connectTo($mode) {
48
        $this->Lexer->addSpecialPattern('\{\{backlinks>.+?\}\}', $mode, 'plugin_backlinks');
49
    }
50
51
    /**
52
     * Handler to prepare matched data for the rendering process.
53
     * @see DokuWiki_Syntax_Plugin::handle()
54
     */
55
    function handle($match, $state, $pos, Doku_Handler $handler) {
56
        global $INFO;
57
        global $ID;
58
59
        // strip {{backlinks> from start and }} from end
60
        $match = substr($match, 12, -2);
61
62
        // Take the id of the source
63
        // It can be a rendering of a sidebar
64
	if ($ID == $INFO['id']) {
65
	    if (strstr($match, "#")) {
66
		$includeNS = substr(strstr($match, "#", FALSE), 1);
67
		$match = strstr($match, "#", TRUE);
68
	    }
69
70
	    $match = ($match == '.') ? $id : $match;
0 ignored issues
show
The variable $id 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...
71
72
	    if (strstr($match, ".:")) {
73
		resolve_pageid(getNS($id), $match, $exists);
74
	    }
75
	}
76
77
        return (array($match, $includeNS));
78
    }
79
80
    /**
81
     * Handles the actual output creation.
82
     * @see DokuWiki_Syntax_Plugin::render()
83
     */
84
    function render($mode, Doku_Renderer $renderer, $data) {
85
        global $lang;
86
	global $ID;
87
	global $INFO;
88
89
	if ($mode == 'xhtml') {
90
            $renderer->info['cache'] = false;
91
92
            @require_once(DOKU_INC.'inc/fulltext.php');
93
	    $page_id = $data[0];
94
	    if($page_id == '.') {
95
		$page_id = $INFO['id'];
96
	    }
97
            $backlinks = ft_backlinks($page_id);
98
99
            dbglog($backlinks, "backlinks: all backlinks to: $page_id");
100
101
            $renderer->doc .= '<div id="plugin__backlinks">'.PHP_EOL;
102
103
            $filterNS = $data[1];
104
            if (!empty($backlinks) && !empty($filterNS)) {
105
                if (stripos($filterNS, "!", 0) === 0) {
106
                    $filterNS = substr($filterNS, 1);
107
                    dbglog($filterNS, "backlinks: exluding all of namespace: $filterNS");
108
                    $backlinks = array_filter($backlinks, function($ns) use($filterNS) {
109
                        return stripos($ns, $filterNS, 0) !== 0;
110
                    });
111
                } else {
112
                    dbglog($filterNS, "backlinks: including namespace: $filterNS only");
113
                    $backlinks = array_filter($backlinks, function($ns) use($filterNS) {
114
                        return stripos($ns, $filterNS, 0) === 0;
115
                    });
116
                }
117
            }
118
119
            dbglog($backlinks, "backlinks: all backlinks to be rendered");
120
121
            if (!empty($backlinks)) {
122
123
                $renderer->doc .= '<ul class="idx">';
124
125
                foreach ($backlinks as $backlink) {
126
                    $name = p_get_metadata($backlink, 'title');
127
                    if (empty($name)) {
128
                        $name = $backlink;
129
                    }
130
                    $renderer->doc .= '<li><div class="li">';
131
                    $renderer->doc .= html_wikilink(':'.$backlink, $name);
132
                    $renderer->doc .= '</div></li>'.PHP_EOL;
133
                }
134
135
                $renderer->doc .= '</ul>'.PHP_EOL;
136
            } else {
137
                $renderer->doc .= "<strong>Plugin Backlinks: ".$lang['nothingfound']."</strong>".PHP_EOL;
138
            }
139
140
            $renderer->doc .= '</div>'.PHP_EOL;
141
142
            return true;
143
        }
144
        return false;
145
    }
146
}
147
// vim:ts=4:sw=4:et:
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
148