Completed
Push — master ( b977f6...15963e )
by Mark
27s queued 10s
created

syntax.php (8 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
if (!defined('DOKU_INC')) {
17
    die();
18
}
19
20
if (!defined('DOKU_PLUGIN')) {
21
    define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
22
}
23
if (!defined('DW_LF')) {
24
    define('DW_LF', "\n");
25
}
26
27
require_once(DOKU_PLUGIN.'syntax.php');
28
require_once(DOKU_INC.'inc/parserutils.php');
29
30
/**
31
 * All DokuWiki plugins to extend the parser/rendering mechanism
32
 * need to inherit from this class
33
 */
34
class syntax_plugin_backlinks extends DokuWiki_Syntax_Plugin {
35
    /**
36
     * Syntax Type.
37
     *
38
     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
39
     * @see DokuWiki_Syntax_Plugin::getType()
40
     */
41
    function getType() { return 'substition'; }
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
42
43
    /**
44
     * @see DokuWiki_Syntax_Plugin::getPType()
45
     */
46
    function getPType() { return 'block'; }
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
47
48
    /**
49
     * @see Doku_Parser_Mode::getSort()
50
     */
51
    function getSort() { return 304; }
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
52
53
    /**
54
     * Connect pattern to lexer.
55
     * @see Doku_Parser_Mode::connectTo()
56
     */
57
    function connectTo($mode) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
58
        $this->Lexer->addSpecialPattern('\{\{backlinks>.+?\}\}', $mode, 'plugin_backlinks');
59
    }
60
61
    /**
62
     * Handler to prepare matched data for the rendering process.
63
     * @see DokuWiki_Syntax_Plugin::handle()
64
     */
65
    function handle($match, $state, $pos, Doku_Handler $handler) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
66
        // strip {{backlinks> from start and }} from end
67
        $match = substr($match, 12, -2);
68
69
        if (strstr($match, "#")) {
70
            $includeNS = substr(strstr($match, "#", FALSE), 1);
71
            $match = strstr($match, "#", TRUE);
72
        }
73
74
        return (array($match, $includeNS));
0 ignored issues
show
The variable $includeNS does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
75
    }
76
77
    /**
78
     * Handles the actual output creation.
79
     * @see DokuWiki_Syntax_Plugin::render()
80
     */
81
    function render($mode, Doku_Renderer $renderer, $data) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
82
        global $lang;
83
        global $INFO;
84
        global $ID;
85
86
        $id = $ID;
87
            // If it's a sidebar, get the original id.
88
            if ($INFO != null) {
89
                $id = $INFO['id'];
90
            }
91
        $match = $data[0];
92
        $match = ($match == '.') ? $id : $match;
93
            if (strstr($match, ".:")) {
94
                resolve_pageid(getNS($id), $match, $exists);
95
        }
96
97
        if ($mode == 'xhtml') {
98
            $renderer->info['cache'] = false;
99
100
            @require_once(DOKU_INC.'inc/fulltext.php');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

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