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