MRedirect Plugin

Compatible with DokuWiki

  • 2012-01-25 "Angua" yes
  • 2011-05-25 "Rincewind" unknown
  • 2010-11-07 "Anteater" unknown

plugin Redirection plugin - any page containing nothing but a standard link will auto-redirect instantaneously

Last updated on
2012-02-10
Provides
Action

Similar to goto, redirect

Tagged with redirect

Description

Redirection from one page to another. The redirecting page must contain nothing but a standard DokuWiki link to the target page (this may include a section anchor), OR a standard URL.

At wiki-internal redirection a small message pops up showing where you came from (using the standard msg() function). Click the page name in this message to go straight to the redirecting article in edit mode.

Updates:

  • 2012-02-10: Added support for outside standard URLs. (Hat tip: JP Viljoen)

Plugin Code

action.php
<?php
 
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'action.php');
 
class action_plugin_mredirect extends DokuWiki_Action_Plugin {
 
    function register(&$controller){
        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'handle_start', array());
    }
 
    function handle_start(&$event, $param){
        global $ID;
        global $ACT;
        global $INFO;
 
      if ($ACT != 'show') return;
      if (!($INFO['exists'])) return;      # don't try to read an article that doesn't exist

      if (preg_match('/^\[\[(([^]#]*)\#?([^]]*))\]\]$/',rawWiki($ID),$p)) {
          msg (sprintf('From: <a href="'.wl($ID,'do=edit').'">'.hsc($ID).'</a>'));
          if (strpos ($p[1], '://')) {
              $url = $p[1];                 # link is URL already
          } else {
              $url = $p[2];
              if ($INFO['namespace']) {     # namespace support
                  $url = $INFO['namespace'] . ':' . $url;
              }    
              $url = wl($url);
              if ($p[3]) {                  # section support
                  $check = false;           # dummy variable for sectionID()
                  $url .= '#' . sectionID($p[3], $check);
              }
          }
          idx_addPage($ID);  # ensure fulltext search indexing of referrer article - to put it on the backlink page of target article
          send_redirect($url);
      }
    }
}
?>

Discussion

Use Heading Extension

I find the plugin extremely useful and elegant, so thanks a lot for that. As I also like the useheading option of the wiki, I made the plugin compatible with it, or at least support it more.

This is the file from above with two changes:

  • The regular expression now also allows for a single headline, with an optional newline, before the link, so the page gets a nice name in the rest of the wiki.
  • The redirection notice will now use this title, if present, to show from where the user was redirected.

Maybe this will be useful for someone else :-) I apologize if adding this section here broke some guideline of these plugin pages.

action.php
<?php
 
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'action.php');
 
class action_plugin_mredirect extends DokuWiki_Action_Plugin {
 
    function register(&$controller){
        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'handle_start', array());
    }
 
    function handle_start(&$event, $param){
        global $ID;
        global $ACT;
        global $INFO;
 
      if ($ACT != 'show') return;
      if (!($INFO['exists'])) return;      # don't try to read an article that doesn't exist

      $text = rawWiki($ID);
      if(preg_match('/^([=]+[^=]+[=]+[\n]?)?\[\[([^]#]*)\#?([^]]*)\]\]$/',$text,$p)) {
        msg(sprintf('From: <a href="'.wl($ID,'do=edit').'">'.tpl_pagetitle($ID, true).'</a>'));
        $check = false;    # dummy variable for sectionID
        $url = ($p[3] == '') ? wl($p[2]) : wl($p[2]) . '#' . sectionID($p[3], $check);
        idx_addPage($ID);  # seemingly required for fulltext search indexing of the page, without which we won't show up on the$
        send_redirect($url);
      }
    }
}
?>
plugin/mredirect.txt · Last modified: 2012/02/10 20:19 by 87.73.94.180
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Imprint Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki
WikiForumIRCBugsGitXRefTranslate