xref: /plugin/backlinks/syntax.php (revision 4d4aa29256d0c8fc474ff927a8aa444f71267357)
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 */
14// must be run within DokuWiki
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
30    /**
31     * General Info
32     */
33    function getInfo(){
34        return array(
35            'author' => 'Michael Klier',
36            'email'  => 'chi@chimeric.de',
37            'date'   => '2007-08-13',
38            'name'   => 'Backlinks',
39            'desc'   => 'Displays backlinks to a given page.',
40            'url'    => 'http://www.chimeric.de/projects/dokuwiki/plugin/backlinks'
41        );
42    }
43
44    /**
45     * Syntax Type
46     *
47     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
48     */
49    function getType()  { return 'substition'; }
50    function getPType() { return 'block'; }
51    function getSort()  { return 304; }
52
53    /**
54     * Connect pattern to lexer
55     */
56    function connectTo($mode) {
57        $this->Lexer->addSpecialPattern('\{\{backlinks>.+?\}\}',$mode,'plugin_backlinks');
58    }
59
60    /**
61     * Handler to prepare matched data for the rendering process
62     */
63    function handle($match, $state, $pos, &$handler){
64        global $ID;
65
66        $match = substr($match,12,-2); //strip {{backlinks> from start and }} from end
67        $match = ($match == '.') ? $ID : $match;
68
69        if(strstr($match,".:")) {
70            resolve_pageid(getNS($ID),$match,$exists);
71        }
72
73        return (array($match));
74    }
75
76    /**
77     * Handles the actual output creation.
78     */
79    function render($mode, &$renderer, $data) {
80        global $lang;
81
82        if($mode == 'xhtml'){
83            $renderer->info['cache'] = false;
84
85            @require_once(DOKU_INC.'inc/fulltext.php');
86            $backlinks = ft_backlinks($data[0]);
87
88            $renderer->doc .= '<div id="plugin__backlinks">' . DW_LF;
89
90            if(!empty($backlinks)) {
91
92                $renderer->doc .= '<ul class="idx">';
93
94                foreach($backlinks as $backlink){
95                    $name = p_get_metadata($backlink,'title');
96                    if(empty($name)) $name = $backlink;
97                    $renderer->doc .= '<li><div class="li">';
98                    $renderer->doc .= html_wikilink(':'.$backlink,$name,'');
99                    $renderer->doc .= '</div></li>';
100                }
101
102                $renderer->doc .= '</ul>';
103            } else {
104                $renderer->doc .= "<strong>Plugin Backlinks: " . $lang['nothingfound'] . "</strong>";
105            }
106
107            $renderer->doc .= '</div>' . DW_LF;
108
109            return true;
110        }
111        return false;
112    }
113}
114//setup vim:ts=4:sw=4:enc=utf-8:
115