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