<?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;
    }
}