Revision Link Plugin

revlink plugin by Braden Pellett
Extend internal link syntax to allow linking to a previous revision a of page.

Last updated on 2009-01-25. Provides Syntax.
Compatible with DokuWiki 2009-01-25+.

Tagged with links, revisions.

    Syntax

    Just append '@<revision #>' to the page name of an internal link, e.g.

    [[page@1234567890|Page (old version)]]

    Installation

    Just copy the code below into the file lib/plugins/revlink/syntax.php

    For more help, refer to Plugins on how to install plugins manually.

    Wish list

    • Append to tooltip the revision a link is linking to.
    • Be more permissive with the revision number, perhaps linking to the last revision at or before the given revision number.
    • Allow other formats for referring to a revision, such as date and time

    Issues

    • Possibly would not work with other XHTML renderer plugins, as revlink assumes the existence of a particular set of arguments on the renderer's internallink method.

    Code

    lib/plugins/revlink/syntax.php :

    <?php
    /**
     * Revision Link plugin
     *
     * Syntax: internal links with '@<revision #>' appended to the
     *         page name will make links to that revision of the page.
     *         e.g. [[page@1234567890|Page (old version)]]
     * 
     * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
     * @author     Braden Pellett <dokuwiki@dabrado.net>
     */
     
    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');
     
    /**
     * revlink plugin
     */
    class syntax_plugin_revlink extends DokuWiki_Syntax_Plugin {
     
       /**
        * Get an associative array with plugin info.
        */
        function getInfo(){
            return array(
                'author' => 'Braden Pellett',
                'email'  => 'dokuwiki@dabrado.net',
                'date'   => '2009-01-25',
                'name'   => 'Revision Link',
                'desc'   => 'Extend internal link syntax to allow linking to a previous revision a of page.',
                'url'    => 'http://www.dokuwiki.org/plugin:revlink',
            );
        }
     
       /**
        * We are a substitution type.
        */
        function getType(){
            return 'substition';
        }
     
       /**
        * Hijack internallink.
        */
        function connectTo($mode) {
          $this->Lexer->mapHandler('internallink','plugin_revlink');
        }
     
       /**
        * Sort position only matters if another plugin uses mapHandler to remap
        * 'internallink'.  Two such plugins cannot coexist, as far as I know.
        */
        function getSort(){
            return 999;
        }
     
       /**
        * Do $hander's internallink work, and if we find that it was actually
        * an internal link and one that contained a revision reference, then
        * pull that out and pass it on to our renderer.
        */
        function handle($match, $state, $pos, &$handler){
            $handler->internallink($match, $state, $pos);
            $calls=&$handler->CallWriter->calls;
            if(!$calls){
                $calls=&$handler->calls;
            }
            $call=end($calls);
            $data=false;
            if($call[0] == 'internallink'){
                preg_match('/^(.*?)(?:@(\d+))?(#.*?)?$/', $call[1][0], $matches);
                if($matches[2]){
                    array_pop($calls);
                    $data=array($matches[1].$matches[3], $matches[2], $call[1][1]);
                }
            }
            return $data;
        }
     
       /**
        * Render internal link as XHTML using normal procedure, and then in the
        * href part of the resulting XHTML we add in the needed ?rev=xxxx.
        */
        function render($mode, &$renderer, $data) {
            if($mode == 'xhtml'){
                $xhtml=$renderer->internallink($data[0], $data[2], NULL, true);
                $renderer->doc .= preg_replace('/(href=".*?)((#.*?)?")/', '$1&rev='.$data[1].'$2', $xhtml);
                return true;
            }
            return false;
        }
    }
    ?>

    Comments

     
    plugin/revlink.txt · Last modified: 2009/10/06 10:00 by laynee
     
    Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
    Imprint Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki
    WikiForumIRCBugsGitXRefTranslate