|
Cookbook /
RouterSummary: Router allows a website's url structure to be different from PmWiki's group/page structure.
Version: 2007-12-19r12beta
Prerequisites: Tested with pmwiki-2.2.6. Should work with pmwiki-2.1.27
Status: beta
Maintainer: MagicBeanDip?
Download: router.phpΔ
Questions answered by this recipe
InstallationPlace router.phpΔ in your cookbook directory. Add the following lines near the top of your config.php file. $aRoute=array();
#Routing instructions go here
#$aRoute['PmWiki']='RoutedPmWiki';
include('cookbook/router.php');
Router is most useful with Usage InstructionsDisambiguation for link direction
$GroupUrlRouter adds the page variable $GroupUrl that returns a routed url to the base page of the current group. Instead of using $aRoute - Routing InstructionsRouter expects to find an array of routing instructions in the global variable $aRoute. Routing instructions are processed first to last for inbound links and last to first for outbound links. Changes made by one instruction are passed on the the next instruction. Routing instructions can be specified in a short and long format Short Format: $aRoute['MatchWiki']='MatchUrl'; Long Format: $aRoute[]=array(
'Mode'=>'BeginsWith'
,'PageName'=>'MatchWiki'
,'Url'=>'MatchUrl'
);
Both instructions do the same thing. For an outbound link, they look for a page name that begins with "MatchWiki" and substitute "MatchUrl" when a match is found.
This instruction is symetrical, so the reverse is true for an inbound link. The short instruction form uses the "BeginsWith" mode for matching. Modes that currently exist are:
Symetrical Examples:Contains$aRoute[]=array( 'Mode'=>'Contains' ,'PageName'=>'MatchWiki' ,'Url'=>'MatchUrl' );
EndsWith$aRoute[]=array( 'Mode'=>'EndsWith' ,'PageName'=>'MatchWiki' ,'Url'=>'MatchUrl' );
ExactMatch$aRoute[]=array( 'Mode'=>'ExactMatch' ,'PageName'=>'MatchWikiGroup/MatchWikiPage' ,'Url'=>'MatchUrlGroup/MatchUrlPage' );
Also $aRoute[]=array( 'Mode'=>'ExactMatch' ,'PageName'=>'Main/HomePage' ,'Url'=>'' );
But more interestingly, Asymetrical ModesAsymetrical modes only work in one direction. The direction to apply the routing instruction to is specified by adding another element. ,'Direction'=>'Out'The 'Direction' element can be used with any instruction mode. RegReplace Example$aRoute[]=array( 'Mode'=>'RegReplace' ,'Search'=>'/^MatchUrl/' ,'Replace'=>'MatchWiki' ,'Direction'=>'In' ); For inbound links,
But this instruction is limited to only inbound links by the So a related outbound instruction is needed. $aRoute[]=array( 'Mode'=>'RegReplace' ,'Search'=>'/^MatchPage/' ,'Replace'=>'MatchUrl' ,'Direction'=>'Out' ); Now The RegReplace instruction simply passes the values of 'Search' and 'Replace' to the php preg_replace() function. If you specify a 'Limit' element in the routing instruction, that value will also be passed to preg_replace(). Special ModesSpecial modes mess with urls in an extra special way. :) CamelCaseToLowerCase ExampleThe 'CamelCaseToLowerCase' mode changes the entire pmwiki url structure from CamelCaseUrls to dashed-lower-case-urls. It functions in both directions unless you specifically add a 'Direction' element to the routing instruction. $aRoute[]=array( 'Mode'=>'CamelCaseToLowerCase' );
and vice versa When using CamelCaseToLowerCase in conjunction with the Apache mod-rewrite module, be sure to change your .htaccess to send all urls that start with lower case characters to index.php. Create your own routing modesAdd a function named Values passed to the function in $aline['Direction'] are either 'In' or 'Out'. The rest of the $aline array keys are determined by your RT_ function. The following function creates a routing mode named "Redirect" $aRoute[]=array(
'Mode'=>'Redirect'
,'In'=>'Main/HomePage'
,'Out'=>'http://example.com/'
);
///
/// Http Redirect to specified location when path matches
///
/// $path: page name at various stages of translation in either Wiki or Url format.
/// $aline: array containing a single routing instruction.
///
function RT_Redirect($path,$aline) {
if ('In'!=$aline['Direction']) {
return $path;
}
if ($path==@$aline['In']) {
header('Location: '.@$aline['Out'],TRUE,301);
}
return $path;
}
Release Notes
CommentsUm... Excuse me for incompetence, but can I do something using this recipe if I want to have page names to be exactly as they are named (like "Main/Wiki sAnDbOx") or (if having spaced filenames arent possible) to substitute "_" for " " and at the same time substitute "__" for "_" (to keep underscores if they are present)? Kriss You can have a url of MagicBeanDip User notes? : If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki. |