Notes on a redirection plugin

lib/plugins/pageredirect/syntax.php

<?php
/**
 * Plugin pageredirect:   redirect browser to another wiki page
 * 
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Christopher Smith <chris@jalakai.co.uk>
 */
 
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');
 
/**
 * All DokuWiki plugins to extend the parser/rendering mechanism
 * need to inherit from this class
 */
class syntax_plugin_pageredirect extends DokuWiki_Syntax_Plugin {
 
    /**
     * return some info
     */
    function getInfo(){
        return array(
          'author' => 'David Lorentsen',
          'email'  => 'zyberdog@quakenet.org',
          'date'   => '2006-11-09',
          'name'   => 'Page Redirector',
          'desc'   => 'Redirects page requests based on content',
          'url'    => 'http://wiki.splitbrain.org/plugin:page_redirector',
        );
    }
 
    function getType(){ return 'substition'; }
    function getPType(){ return 'block'; } 
    function getSort(){ return 1; }
    function connectTo($mode) { $this->Lexer->addSpecialPattern('~~REDIRECT>[a-zA-Z0-9_\-:]+~~',$mode,'plugin_pageredirect'); }
 
    /**
     * Handle the match
     */
    function handle($match, $state, $pos, &$handler){
        $page = substr($match,11,-2);
        return array($page);
    }
 
    /**
     * Create output
     */
    function render($format, &$renderer, $data) {
 
        switch ($format) {
          case 'metadata' :
            $renderer->meta['relation']['isreplacedby'] = $data[0];
            return true;
          case 'xhtml' :
            // output a page moved message
            $renderer->doc.= '<p class="redirect">'.sprintf($this->getLang('page_moved'),wl($data[0])).'</p>';
 
            // hook into the post render event to allow the page to be redirected
            global $EVENT_HANDLER;
            $action =& plugin_load('action','pageredirect');
            $EVENT_HANDLER->register_hook('TPL_ACT_RENDER','AFTER', $action, 'handle_page_redirect');
            return true;
 
          default :
            return false;
        }
 
        return false;
    }
 
}
 
//Setup VIM: ex: et ts=4 enc=utf-8 :

lib/plugins/pageredirect/action.php

<?php
/**
 * Action Plugin:   Redirects page requests based on content
 * 
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     David Lorentsen <zyberdog@quakenet.org>  
 */
 
if(!defined('DOKU_INC')) die();
 
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'action.php');
 
/**
 * All DokuWiki action plugins need to inherit from this class
 */
class action_plugin_pageredirect extends DokuWiki_Action_Plugin {
 
    /**
     * return some info
     */
    function getInfo(){
      return array(
        'author' => 'David Lorentsen',
        'email'  => 'zyberdog@quakenet.org',
        'date'   => '2006-11-09',
        'name'   => 'Page Redirector',
        'desc'   => 'Redirects page requests based on content',
        'url'    => 'http://wiki.splitbrain.org/plugin:page_redirector',
      );
    }
 
    function register(&$controller) {
      $controller->register_hook('DOKUWIKI_STARTED','BEFORE', $this, 'handle_page_redirect');
#      $controller->register_hook('PARSER_METADATA_RENDER','BEFORE', $this, 'handle_redirect_metadata');
    }
 
    function handle_page_redirect(&$event, $param) {
      global $ID, $ACT, $REV;
 
      // only redirect on page show for the current page revision
      if (($ACT == 'show' || $ACT == '') && empty($REV)) {
        $page = p_get_metadata($ID,'relation isreplacedby');
        if (empty($page)) { return; }
        if (isset($_GET['redirect']) && $_GET['redirect'] == 'no') { return; }
 
        // verify metadata currency
        if (@filemtime(metaFN($ID,'.meta')) < @filemtime(wikiFN($ID))) { return; }
 
        header("Location: ".wl($page));
        exit();
      }
    }
 
    function handle_redirect_metadata(&$event, $param) {
      unset($event->data->meta['relation']['isreplacedby']);
    }
}
 
//Setup VIM: ex: et ts=4 enc=utf-8 :

lib/plugins/pageredirect/lang/en/lang.php

<?php
/**
 * English language file
 *
 * @author Chris Smith <chris@jalakai.co.uk>
 */
$lang['page_moved'] = 'This page has been moved to a <a href="%s">new location</a>';

 
tutorials/redirect.txt · Last modified: 2006/12/16 05:25 by chris
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki