xref: /plugin/backlinks/syntax.php (revision c5d4ac8e71a0930952fbc252ffaa95643c5e25fd)
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]}}
8 *
9 *   [pagename] - a valid wiki pagename
10 *
11 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
12 * @author  Michael Klier <chi@chimeric.de>
13 * @author  Mark C. Prins <mprins@users.sf.net>
14 */
15if(!defined('DOKU_INC')) die();
16
17if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
18if(!defined('DW_LF')) define('DW_LF',"\n");
19
20require_once(DOKU_PLUGIN.'syntax.php');
21require_once(DOKU_INC.'inc/parserutils.php');
22
23/**
24 * All DokuWiki plugins to extend the parser/rendering mechanism
25 * need to inherit from this class
26 */
27class syntax_plugin_backlinks extends DokuWiki_Syntax_Plugin {
28    /**
29     * Syntax Type.
30     *
31     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
32     * @see DokuWiki_Syntax_Plugin::getType()
33     */
34    function getType()  { return 'substition'; }
35
36    /**
37     * @see DokuWiki_Syntax_Plugin::getPType()
38     */
39    function getPType() { return 'block'; }
40
41    /**
42     * @see Doku_Parser_Mode::getSort()
43     */
44    function getSort()  { return 304; }
45
46    /**
47     * Connect pattern to lexer.
48     * @see Doku_Parser_Mode::connectTo()
49     */
50    function connectTo($mode) {
51        $this->Lexer->addSpecialPattern('\{\{backlinks>.+?\}\}', $mode, 'plugin_backlinks');
52    }
53
54    /**
55     * Handler to prepare matched data for the rendering process.
56     * @see DokuWiki_Syntax_Plugin::handle()
57     */
58    function handle($match, $state, $pos, Doku_Handler $handler){
59
60        // Take the id of the source
61        // It can be a rendering of a sidebar
62        global $INFO;
63        global $ID;
64        $id = $ID;
65        // If it's a sidebar, get the original id.
66        if ($INFO != null) {
67            $id = $INFO['id'];
68        }
69
70
71        $match = substr($match,12,-2); //strip {{backlinks> from start and }} from end
72        $match = ($match == '.') ? $id : $match;
73
74        if(strstr($match,".:")) {
75            resolve_pageid(getNS($id),$match,$exists);
76        }
77
78        return (array($match));
79    }
80
81    /**
82     * Handles the actual output creation.
83     * @see DokuWiki_Syntax_Plugin::render()
84     */
85    function render($mode, Doku_Renderer $renderer, $data) {
86        global $lang;
87
88        if($mode == 'xhtml'){
89            $renderer->info['cache'] = false;
90
91            @require_once(DOKU_INC.'inc/fulltext.php');
92            $backlinks = ft_backlinks($data[0]);
93
94            $renderer->doc .= '<div id="plugin__backlinks">' . DW_LF;
95
96            if(!empty($backlinks)) {
97
98                $renderer->doc .= '<ul class="idx">';
99
100                foreach($backlinks as $backlink){
101                    $name = p_get_metadata($backlink,'title');
102                    if(empty($name)) $name = $backlink;
103                    $renderer->doc .= '<li><div class="li">';
104                    $renderer->doc .= html_wikilink(':'.$backlink,$name,'');
105                    $renderer->doc .= '</div></li>';
106                }
107
108                $renderer->doc .= '</ul>';
109            } else {
110                $renderer->doc .= "<strong>Plugin Backlinks: " . $lang['nothingfound'] . "</strong>";
111            }
112
113            $renderer->doc .= '</div>' . DW_LF;
114
115            return true;
116        }
117        return false;
118    }
119}
120