Translations of this page?:

Backlinks Plugin

backlinks plugin by Jonathan Arkell
Show A list of all the backlinks to the current page

Last updated on 2005-10-24. Provides Syntax.
No compatibility info given!

Conflicts with backlinks2!
Similar to backlinks2.

Tagged with backlinks.

Description

This is a very simple plugin to display the backlinks of the given page. You invoke it like this:

{{backlinks}}

This will be replaced with an alphabetized list of pages that link to the current page.

Plugin

To install, put the following PHP file in /lib/plugins/backlinks/syntax.php. Or download from (or point your plugin manager) here: http://wiki.jonnay.net/_media/bunny/backlinks.tar?cache=nocache

<?php
/**
 * Backlinks: Show a list of backlinks for the given pagename
 * Usage:
 * {{backlinks>page}} to display the backlinks page in the current namespace
 * {{backlinks>:page}} for "page" in top namespace
 * {{backlinks>namespace:page}} for "page" in namespace "namespace"
 * {{backlinks>.namespace:page}} for "page" in subnamespace "namespace"
 *
 */
 
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');
require_once(DOKU_INC.'inc/fulltext.php');
 
/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_backlinks extends DokuWiki_Syntax_Plugin {
    /**
     * return some info
     */
    function getInfo(){
        return array(
            'author' => 'Jonathan Arkell',
            'email'  => 'jonnay@jonnay.net',
            'date'   => '2005-10-24',
            'name'   => 'Backlink Plugin',
            'desc'   => 'Displays the backlinks for the given wikipage',
            'url'    => '',
        );
    }
 
    /**
     * What kind of syntax are we?
     */
    function getType(){
        return 'substition';
    }
 
    /**
     * Where to sort in?
     */
    function getSort(){
        return 30;
    }
 
    /**
     * Paragraph Type
     */
    function getPType(){
        return 'block';
    }
 
    /**
     * Connect pattern to lexer
     */
    function connectTo($mode) {
      $this->Lexer->addSpecialPattern("{{backlinks}}",$mode,'plugin_backlinks');
    }
 
    /**
     * Handle the match
     */
    function handle($match, $state, $pos, &$handler){
        global $ID;
 
        $syntax = substr($match,12,-2);                 		// strip markup
        $page = $ID;
 
        resolve_pageid(getNS($ID),$page,$exists); 		// resolve shortcuts
 
        // check for permission
        if (auth_quickaclcheck($page) < 1) return false;
 
 		$links = ft_backlinks($page);
        foreach ($links as $link)
        {
            $backlinks[substr($link, strrpos($link, ':')+1)] = $link;
        }
        ksort($backlinks);
 
        return array($page,$backlinks);
    }    
 
    /**
     * Create output
     */
    function render($mode, &$renderer, $data) {
        if($mode == 'xhtml'){
            $file = wikiFN($data[0]);
 
            $renderer->doc .= '<div class="backlinks">';
 
            if (!empty($data[1]))
            {
                $lastLetter = '';
                $pageList = array();
                array_push($data[1]," Dummy Entry"); // ugh.  Hacky Kludge.
                foreach($data[1] as $name => $page) {                  
                    if ($name{0} != $lastLetter) {
                        if (!empty($pageList)) {
                            $renderer->doc .= '<h3>'.strtoupper($lastLetter).'</h3>';
                            $this->renderPageList($renderer, $pageList);
                            $pageList = array();
                        }
                    }
                    $pageList[] = $page;
                    $lastLetter = $name{0};
                }
            } else {
            	$renderer->doc .= "{$data[0]} has no backlinks";
            } 
 
            $renderer->doc .= '</div>';
            return true;
        }
        return false;
    }
 
    function renderpageList(&$renderer, $pageList)
    {
        if (empty ($pageList)) {
            return;
        }
 
        $renderer->doc .= '<ul>';
        foreach($pageList as $page) {
          $renderer->doc .= '<li>';
          $renderer->internalLink($page, $page);
          $renderer->doc .= '</li>';
        }
        $renderer->doc .= '</ul>';
    }
 
    /**
     * Corrects relative internal links and media
     */
    function _correctRelNS($instr,$incl){
        global $ID;
 
        // check if included page is in same namespace
        $iNS = getNS($incl);
        if (getNS($ID) == $iNS) return $instr;
 
        // convert internal links and media from relative to absolute
        $n = count($instr);
        for($i = 0; $i < $n; $i++){
            if (substr($instr[$i][0], 0, 8) == 'internal'){
 
                // relative subnamespace
                if ($instr[$i][1][0]{0} == '.'){
                    $instr[$i][1][0] = $iNS.':'.substr($instr[$i][1][0], 1);
 
                // relative link
                } elseif (strpos($instr[$i][1][0],':') === false) {
                    $instr[$i][1][0] = $iNS.':'.$instr[$i][1][0];
                }
            }
        }
        return $instr;
    }
}
 
//Setup VIM: ex: et ts=4 enc=utf-8 :
 
?>

Style

The backlinks are in a <div> tag of class backlinks.

/* Styles for the backlinks */
.backlinks
{
	margin-bottom: 2em;
}
 
.backlinks h3
{
	border: 0px solid #000;
	border-bottom-width: 1px;
	margin-bottom: 0em;
}
 
.backlinks ul
{
	margin-left: 5.5em;
}

Changes

  • 2005-11-11 v0.1
    • First Version

To Do

  • Add config items of special page prefixes that warrent different lists, (i.e. pages with the prefix “category” get put under a different alphabetized list)

Discussion

Doesn't seem to work for me. The backlinks marker is just interpreted as a media-link…
I get it. Just using {{backlinks}} doesn't work (you'll get a ksort error in preview mode); you need to reference the page: {{backlinks>page}}. Hmm, a default would be handy though….


The link provided http://wiki.jonnay.net/_media/bunny/backlinks.tar?cache=nocache deliver a tar.gz file not a tar file so it can't be downloaded using plugin manager. Just need to be renamed.


This also doesn't work for me. I get an error in line 83 about $backlinks not being an array. If I put an “if is_array($backlinks)…” before the ksort(…) instruction (so far this is all I know about PHP) the error goes away provided the page has no backlinks. If it has backlinks, however, I get more errors and the page contents isn't rendered at all.

Also, I don't know whether this is related or not, but when I click the page link in the top (which should show the backlinks), only the text saying “these are the backlinks…” appear, but not the backlinks themselves. Would someone have any hint on how to make this plugin and/or the standard backlink function work? — Alexander Gieg 2005-12-12 18:51

  • I had that when there were no backlinks to show. I fixed this by changing line 83 in syntax.php from ksort($backlinks); to if ($backlinks!=null) ksort($backlinks);.

The plugin seems flaky on the latest nightly builds. Sometimes I get the list of backlinks, sometimes not. Also {{backlinks}} sometimes works for me. I get the backlinks for the current page. Two improvements would be great: being able to ignore pages globally (i.e., always hide the start page from the backlinks list) and being able to display the page's title instead of its wikiname.

Maybe, add some params? For ex. partially include page texts, restrict to namespace, ets?

 
plugin/backlinks.txt · Last modified: 2007/10/07 11:26 by andi
 
Imprint Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki
WikiForumIRCBugsTranslate