DokuWiki

It's better when it's simple

User Tools

Site Tools


plugin:backlinks

Backlinks Plugin

Compatible with DokuWiki

No compatibility info given!

plugin Show a list of all the backlinks to the current page

Last updated on
2005-10-24
Provides
Syntax
Conflicts with
autolink2, backlinks2, shortcut

This extension has not been updated in over 2 years. It may no longer be maintained or supported and may have compatibility issues.

Similar to backlinks2

Tagged with include, links, listing

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

As of Nov 1, 2009, the link above is reported as Unauthorized1). I'm temporarily uploading a mirror of version 2008-03-21, until I can contact the author. Check here with the Plugin Manager. – — ryan.chappelle 2009/11/01 04:49
That is NOT a mirror version. Jonathan Arkell is the author of the listed backlinks plugin. The above “mirror” is the backlinks2 plugin by Michael Klier which you should get from the official link (and it seems to be slightly outdated). — Saxywolf 2009/11/03
<?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);
 
        $backlinks = array(); // fix for no backlinks
 
        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 warrant 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}}. Hmmm, 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 parameters? For example, partially include page texts, restrict to namespace, etc?


Is this plugin not already part of the native dokuwiki code? If so, could this plugin page be removed? It would be confusing finding a plugin for a function DokuWiki is already capable of. — Matthias Grimm 2009/08/22 13:01

The purpose of this plugin is to embed the backlinks of a page directly into the text of a wiki page. — Michael Klier 2009/08/22 16:22
plugin/backlinks.txt · Last modified: 2015-06-09 04:57 by 115.93.88.196