xref: /plugin/backlinks/syntax.php (revision ae91fe05368d9718346e2830b98496676adf4cd6)
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'   => '2008-03-21',
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        $this->Lexer->addSpecialPattern('~~BACKLINKS~~', $mode, 'plugin_backlinks');
59    }
60
61    /**
62     * Handler to prepare matched data for the rendering process
63     */
64    function handle($match, $state, $pos, &$handler){
65        global $ID;
66
67        if($match == '~~BACKLINKS~~') { //check for deprecated syntax
68            $match = $ID;
69        } else {
70            $match = substr($match,12,-2); //strip {{backlinks> from start and }} from end
71            $match = ($match == '.') ? $ID : $match;
72
73            if(strstr($match,".:")) {
74                resolve_pageid(getNS($ID),$match,$exists);
75            }
76        }
77
78        return (array($match));
79    }
80
81    /**
82     * Handles the actual output creation.
83     */
84    function render($mode, &$renderer, $data) {
85        global $lang;
86
87        if($mode == 'xhtml'){
88            $renderer->info['cache'] = false;
89
90            @require_once(DOKU_INC.'inc/fulltext.php');
91            $backlinks = ft_backlinks($data[0]);
92
93            $renderer->doc .= '<div id="plugin__backlinks">' . DW_LF;
94
95            if(!empty($backlinks)) {
96
97                $renderer->doc .= '<ul class="idx">';
98
99                foreach($backlinks as $backlink){
100                    $name = p_get_metadata($backlink,'title');
101                    if(empty($name)) $name = $backlink;
102                    $renderer->doc .= '<li><div class="li">';
103                    $renderer->doc .= html_wikilink(':'.$backlink,$name,'');
104                    $renderer->doc .= '</div></li>';
105                }
106
107                $renderer->doc .= '</ul>';
108            } else {
109                $renderer->doc .= "<strong>Plugin Backlinks: " . $lang['nothingfound'] . "</strong>";
110            }
111
112            $renderer->doc .= '</div>' . DW_LF;
113
114            return true;
115        }
116        return false;
117    }
118}
119//setup vim:ts=4:sw=4:enc=utf-8:
120