xref: /plugin/backlinks/syntax.php (revision df10d6bd721bae611eaad720dfd43c8e3458b5e4)
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 <chi@chimeric.de>
14 * @author  Mark C. Prins <mprins@users.sf.net>
15 */
16if (!defined('DOKU_INC')) {
17    die();
18}
19
20if (!defined('DOKU_PLUGIN')) {
21    define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
22}
23if (!defined('DW_LF')) {
24    define('DW_LF', "\n");
25}
26
27require_once(DOKU_PLUGIN.'syntax.php');
28require_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 */
34class 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'; }
42
43    /**
44     * @see DokuWiki_Syntax_Plugin::getPType()
45     */
46    function getPType() { return 'block'; }
47
48    /**
49     * @see Doku_Parser_Mode::getSort()
50     */
51    function getSort() { return 304; }
52
53    /**
54     * Connect pattern to lexer.
55     * @see Doku_Parser_Mode::connectTo()
56     */
57    function connectTo($mode) {
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) {
66        // Take the id of the source
67        // It can be a rendering of a sidebar
68        global $INFO;
69        global $ID;
70        $id = $ID;
71        // If it's a sidebar, get the original id.
72        if ($INFO != null) {
73            $id = $INFO['id'];
74        }
75
76        // strip {{backlinks> from start and }} from end
77        $match = substr($match, 12, -2);
78
79        if (strstr($match, "#")) {
80            $includeNS = substr(strstr($match, "#", FALSE), 1);
81            $match = strstr($match, "#", TRUE);
82        }
83
84        $match = ($match == '.') ? $id : $match;
85
86        if (strstr($match, ".:")) {
87            resolve_pageid(getNS($id), $match, $exists);
88        }
89
90        return (array($match, $includeNS));
91    }
92
93    /**
94     * Handles the actual output creation.
95     * @see DokuWiki_Syntax_Plugin::render()
96     */
97    function render($mode, Doku_Renderer $renderer, $data) {
98        global $lang;
99
100        if ($mode == 'xhtml') {
101            $renderer->info['cache'] = false;
102
103            @require_once(DOKU_INC.'inc/fulltext.php');
104            $backlinks = ft_backlinks($data[0]);
105
106            dbglog($backlinks, "backlinks: all backlinks to: $data[0]");
107
108            $renderer->doc .= '<div id="plugin__backlinks">'.DW_LF;
109
110            $filterNS = $data[1];
111            if (!empty($backlinks) && !empty($filterNS)) {
112                if (stripos($filterNS, "!", 0) === 0) {
113                    $filterNS = substr($filterNS, 1);
114                    dbglog($filterNS, "backlinks: exluding all of namespace: $filterNS");
115                    $backlinks = array_filter($backlinks, function($ns) use($filterNS) {
116                        return stripos($ns, $filterNS, 0) !== 0;
117                    });
118                } else {
119                    dbglog($filterNS, "backlinks: including namespace: $filterNS only");
120                    $backlinks = array_filter($backlinks, function($ns) use($filterNS) {
121                        return stripos($ns, $filterNS, 0) === 0;
122                    });
123                }
124            }
125
126            dbglog($backlinks, "backlinks: all backlinks to be rendered");
127
128            if (!empty($backlinks)) {
129
130                $renderer->doc .= '<ul class="idx">';
131
132                foreach ($backlinks as $backlink) {
133                    $name = p_get_metadata($backlink, 'title');
134                    if (empty($name)) {
135                        $name = $backlink;
136                    }
137                    $renderer->doc .= '<li><div class="li">';
138                    $renderer->doc .= html_wikilink(':'.$backlink, $name);
139                    $renderer->doc .= '</div></li>'.DW_LF;
140                }
141
142                $renderer->doc .= '</ul>'.DW_LF;
143            } else {
144                $renderer->doc .= "<strong>Plugin Backlinks: ".$lang['nothingfound']."</strong>".DW_LF;
145            }
146
147            $renderer->doc .= '</div>'.DW_LF;
148
149            return true;
150        }
151        return false;
152    }
153}
154