xref: /plugin/backlinks/syntax.php (revision c346520311eea5a6f4cfb1a17d990fe55f3ef0aa)
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')) die();
17
18if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
19if(!defined('DW_LF')) define('DW_LF',"\n");
20
21require_once(DOKU_PLUGIN.'syntax.php');
22require_once(DOKU_INC.'inc/parserutils.php');
23
24/**
25 * All DokuWiki plugins to extend the parser/rendering mechanism
26 * need to inherit from this class
27 */
28class syntax_plugin_backlinks extends DokuWiki_Syntax_Plugin {
29    /**
30     * Syntax Type.
31     *
32     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
33     * @see DokuWiki_Syntax_Plugin::getType()
34     */
35    function getType()  { return 'substition'; }
36
37    /**
38     * @see DokuWiki_Syntax_Plugin::getPType()
39     */
40    function getPType() { return 'block'; }
41
42    /**
43     * @see Doku_Parser_Mode::getSort()
44     */
45    function getSort()  { return 304; }
46
47    /**
48     * Connect pattern to lexer.
49     * @see Doku_Parser_Mode::connectTo()
50     */
51    function connectTo($mode) {
52        $this->Lexer->addSpecialPattern('\{\{backlinks>.+?\}\}', $mode, 'plugin_backlinks');
53    }
54
55    /**
56     * Handler to prepare matched data for the rendering process.
57     * @see DokuWiki_Syntax_Plugin::handle()
58     */
59    function handle($match, $state, $pos, Doku_Handler $handler){
60
61        // Take the id of the source
62        // It can be a rendering of a sidebar
63        global $INFO;
64        global $ID;
65        $id = $ID;
66        // If it's a sidebar, get the original id.
67        if ($INFO != null) {
68            $id = $INFO['id'];
69        }
70
71        $match = substr($match,12,-2); //strip {{backlinks> from start and }} from end
72
73        if(strstr($match, "#")){
74            $includeNS = substr(strstr($match, "#", FALSE), 1);
75            $match= strstr($match, "#", TRUE);
76        }
77
78        $match = ($match == '.') ? $id : $match;
79
80        if(strstr($match,".:")) {
81            resolve_pageid(getNS($id),$match,$exists);
82        }
83
84        return (array($match, $includeNS));
85    }
86
87    /**
88     * Handles the actual output creation.
89     * @see DokuWiki_Syntax_Plugin::render()
90     */
91    function render($mode, Doku_Renderer $renderer, $data) {
92        global $lang;
93
94        if($mode == 'xhtml'){
95            $renderer->info['cache'] = false;
96
97            @require_once(DOKU_INC.'inc/fulltext.php');
98            $backlinks = ft_backlinks($data[0]);
99
100            dbglog($backlinks, "backlinks: all backlinks to: $data[0]");
101
102            $renderer->doc .= '<div id="plugin__backlinks">' . DW_LF;
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)) $name = $backlink;
129                    $renderer->doc .= '<li><div class="li">';
130                    //$renderer->doc .= html_wikilink(':'.$backlink, $name);
131                    $renderer->doc .= '<a href="'.wl(':'.$backlink).'">'.$name.'</a>';
132                    $renderer->doc .= '</div></li>' . DW_LF;
133                }
134
135                $renderer->doc .= '</ul>' . DW_LF;
136            } else {
137                $renderer->doc .= "<strong>Plugin Backlinks: " . $lang['nothingfound'] . "</strong>" . DW_LF;
138            }
139
140            $renderer->doc .= '</div>' . DW_LF;
141
142            return true;
143        }
144        return false;
145    }
146}
147