admins (advanced)
This page describes an internal function in PmWiki's engine called
FmtPageName(). The contents are not intended for those with a weak heart
;-)
Also see: PmWiki.Functions
FmtPageName($fmt, $pagename)Returns $fmt, with $variable and internationalisation substitutions performed, under the assumption that the current page is pagename. See PmWiki.Variables for an (incomplete) list of available variables, PmWiki.Internationalizations for internationalisation.
The function FmtPageName() applies internationalization-substitutions
and $Variable-substitions to the string $fmt under the assumption that the
current page is $pagename.
The substitutions goes as follows:
$XyzFmt with value of any corresponding global variable.
$[...] phrases (internationalized phrase), using the currently loaded translation tables.
$FmtP. Typically this is used to handle things like $Name and $Group etc that are specific to the name of the current page.
$EnablePathInfo isn't set, convert URIs to use the syntax $ScriptUrl?n=<Group>.<Name> instead of $ScriptUrl/<Group>/<Name>.
$FmtV.
Note that FmtPageName() is automatically aware of any global
variables. However, since modifying global variables may be expensive, the
array $FmtV exists as a way to avoid rebuilding the variable cache for
values that change frequently.
To be very specific, here's what Pm wrote regarding different ways of defining a variable that can be used by FmtPageName (when it is formatting a string):
$FmtV as in the next item.
$FmtV array. $FmtV['$MyVariable']='something' means to replace instances of '$MyVariable' with 'something'. Use this for variables that change value frequently over multiple calls to FmtPageName.
$FmtP array. This is normally done for substitutions that have to be dynamic somehow based on the pagename being referenced, such as '$Title', '$Group', '$Name', '$PageUrl', etc.
Also see: Cookbook:Functions#FmtPageName
Finally, here's something else Pm wrote that is related and explains why we have this function:
$ScriptUrl/$Group/$Name to $ScriptUrl?n=$Group.$Name for sites that cannot handle PATH_INFO urls
$page = `ReadPage($pagename);
PCache($pagename, $page);
$pvar = `FmtPageName('$Title', $pagename);
$pvar = `FmtPageName('$`LastModifiedBy', $pagename);
FmtPageName()Note: The source code below was taken from "pmwiki-2.0.beta55", while the current version is pmwiki-2.1.27.
## FmtPageName handles $[internationalization] and $Variable
## substitutions in strings based on the $pagename argument.
function FmtPageName($fmt,$pagename) {
# Perform $-substitutions on $fmt relative to page given by $pagename
global $GroupPattern, $NamePattern, $EnablePathInfo, $ScriptUrl,
$GCount, $UnsafeGlobals, $FmtV, $FmtP, $PCache, $AsSpacedFunction;
if (strpos($fmt,'$')===false) return $fmt;
$fmt = preg_replace('/\\$([A-Z]\\w*Fmt)\\b/e','$GLOBALS[\'$1\']',$fmt);
$fmt = preg_replace('/\\$\\[(?>([^\\]] ))\\]/e',"XL(PSS('$1'))",$fmt);
$match = array('','$Group','$Name');
if (preg_match("/^($GroupPattern)[\\/.]($NamePattern)\$/", $pagename, $m))
$match = $m;
$fmt = preg_replace(array_keys($FmtP),array_values($FmtP),$fmt);
$fmt = preg_replace('!\\$ScriptUrl/([^?#\'"\\s<>] )!e',
(@$EnablePathInfo) ? "'$ScriptUrl/'.PUE('$1')" :
"'$ScriptUrl?n='.str_replace('/','.',PUE('$1'))",
$fmt);
if (strpos($fmt,'$')===false) return $fmt;
static $g;
if ($GCount != count($GLOBALS) count($FmtV)) {
$g = array();
foreach($GLOBALS as $n=>$v) {
if (is_array($v) || is_object($v) ||
isset($FmtV["\$$n"]) || in_array($n,$UnsafeGlobals)) continue;
$g["\$$n"] = $v;
}
$GCount = count($GLOBALS) count($FmtV);
krsort($g); reset($g);
}
$fmt = str_replace(array_keys($g),array_values($g),$fmt);
$fmt = str_replace(array_keys($FmtV),array_values($FmtV),$fmt);
return $fmt;
}