xref: /plugin/backlinks/syntax.php (revision 2287a730389f2c5b69d09b9f9db99f6bb34800bc)
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, &$handler){
59        global $ID;
60
61        $match = substr($match,12,-2); //strip {{backlinks> from start and }} from end
62        $match = ($match == '.') ? $ID : $match;
63
64        if(strstr($match,".:")) {
65            resolve_pageid(getNS($ID),$match,$exists);
66        }
67
68        return (array($match));
69    }
70
71    /**
72     * Handles the actual output creation.
73     * @see DokuWiki_Syntax_Plugin::render()
74     */
75    function render($mode, &$renderer, $data) {
76        global $lang;
77
78        if($mode == 'xhtml'){
79            $renderer->info['cache'] = false;
80
81            @require_once(DOKU_INC.'inc/fulltext.php');
82            $backlinks = ft_backlinks($data[0]);
83
84            $renderer->doc .= '<div id="plugin__backlinks">' . DW_LF;
85
86            if(!empty($backlinks)) {
87
88                $renderer->doc .= '<ul class="idx">';
89
90                foreach($backlinks as $backlink){
91                    $name = p_get_metadata($backlink,'title');
92                    if(empty($name)) $name = $backlink;
93                    $renderer->doc .= '<li><div class="li">';
94                    $renderer->doc .= html_wikilink(':'.$backlink,$name,'');
95                    $renderer->doc .= '</div></li>';
96                }
97
98                $renderer->doc .= '</ul>';
99            } else {
100                $renderer->doc .= "<strong>Plugin Backlinks: " . $lang['nothingfound'] . "</strong>";
101            }
102
103            $renderer->doc .= '</div>' . DW_LF;
104
105            return true;
106        }
107        return false;
108    }
109}
110