MoreCustomPageVariables

Summary: Additional custom page variables for use in pages, forms, includes and conditional markup.
Version: n/a
Prerequisites:
Status:
Maintainer: Petko
Categories: PageVariables

Questions answered by this recipe

  • How to print the current date?
  • How to include a page or a section depending on the current date? For example
    • (:include Birthdays.{$CurrentMonthName}#{$CurrentDay}:)
    • or (:include ToDo.{$CurrentWeek}:)
    • or [[Blog.{$Today} | Today's articles]]

Description

Please note that the values of the elements of $FmtPV are eval()ed so always sanitize any user input. The following is very insecure:

$FmtPV['$Var'] = $_REQUEST['Var']; # critically insecure, allows PHP code injection
$FmtPV['$Var'] = '"'. addslashes($_REQUEST['Var']).'"'; # critically insecure, allows PHP code injection

See the recipe Cookbook:HttpVariables for a better way to use these variables.

Also, note that for the date variables, PmWiki provides a built-in {(ftime)} markup expression which allow a great flexibility in defining dates and is less prone to errors. All examples of date variables on this page should be considered obsolete.

You can add to a local customisations (in config.php) to define consistent, and easy to use date variables across your wiki website (while these may be 'obsolete' they are also extremely practical).

  • $FmtPV['$CurrentYear'] = 'date("Y")'; // 2006
  • $FmtPV['$CurrentDay'] = 'date("d")'; // 02 (for April 2), 20 (for April 20)
  • $FmtPV['$CurrentWeek'] = 'date("W")'; // ISO-8601 week number of the year
  • $FmtPV['$CurrentMonth'] = 'date("m")'; // 04 (for April), 12 (for December)
  • $FmtPV['$Today'] = 'date("Y-m-d")'; // 2006-03-29
  • $FmtPV['$Tomorrow'] = 'date("Y-m-d", time()+60*60*24 )'; // {$Tomorrow} will output something like '2006-03-29'
  • $FmtPV['$CurrentTime'] = 'date("H:i:s")'; // 14:29:32
  • $FmtPV['$CurrentTimeC'] = 'date("h:i:s A")'; // 02:29:32 PM
  • $FmtPV['$CurrentDayLong'] = 'date("l")'; // Monday through Sunday
  • $FmtPV['$CurrentDayShort'] = 'date("D.")'; // Mon. through Sun.
  • $FmtPV['$CurrentMonthLong'] = 'date("F")'; // April, December, etc.
  • $FmtPV['$TodayLong'] = 'date("F d, Y")'; // June 25, 2010
  • $FmtPV['$TodayLongAlt'] = 'date("d F Y")'; // 25 June 2010
  • $FmtPV['$TodayShort'] = 'date("Ymd")'; // 20060329
  • $FmtPV['$VisitorIP'] = '$_SERVER["REMOTE_ADDR"]'; // may be used in forms or cond. markup;
    on Mac OS X you should replace the latter by $_SERVER["HTTP_PC_REMOTE_ADDR"]. Cf. http://trac.wordpress.org/ticket/1443
  • $FmtPV['$CurrentSkin'] = '$GLOBALS["Skin"]';
  • $FmtPV['$WikiTitle'] = '$GLOBALS["WikiTitle"]';
  • $FmtPV['$RevCount'] = '$page["rev"]';// to get the revision number of a page ; use {$RevCount} in any page you want the rev number to be displayed.

and then use e.g. {$Today} in a page markup.

Please contribute here with some interesting implementations that you have, may be useful for other users.

Date related variables

Here is a set of date related variables I add to all my PmWiki's config.php

## set up custom page variables      
$FmtPV['$ThisYear'] = 'date("Y")'; # 2006
$FmtPV['$ThisDay'] = 'date("d")'; # 02 (for April 2), 20 (for April 20)
$FmtPV['$ThisWeek'] = 'date("W")'; # ISO-8601 week number of the year
$FmtPV['$ThisMonth'] = 'date("m")'; # 04 (for April), 12 (for December)
$FmtPV['$ThisMonthName'] = 'date("F")'; # January to December

$FmtPV['$Today'] = 'date("Y-m-d")'; # 2006-03-29
$FmtPV['$Tomorrow'] = 'date("Y-m-d", time()+60*60*24)'; # {$Tomorrow} will output '2006-03-29'
$FmtPV['$Yesterday'] = 'date("Y-m-d", time()-60*60*24)'; # {$Yesterday} will output '2006-03-29'

$FmtPV['$LastYear'] = 'date("Y")-1'; # 2006
$FmtPV['$LastDay'] = 'date("d", time()-60*60*24)'; # 02 (for April 2), 20 (for April 20)
$FmtPV['$LastWeek'] = 'date("W", time()-60*60*24*7)'; # ISO-8601 week number of the year
$FmtPV['$LastMonth'] = 'date("m", time()-60*60*24*date("d"))'; # 04 (for April), 12 (for December)
$FmtPV['$LastMonthName'] = 'date("F", time()-60*60*24*date("d"))'; # January to December

$FmtPV['$NextYear'] = 'date("Y")+1'; # 2006
$FmtPV['$NextDay'] = 'date("d", time()+60*60*24)'; # 02 (for April 2), 20 (for April 20)
$FmtPV['$NextWeek'] = 'date("W", time()+60*60*24*7)'; # ISO-8601 week number of the year
$FmtPV['$NextMonth'] = 'date("m", time()+60*60*24*(32-date("d")))'; # 04 (for April), 12 (for December)
$FmtPV['$NextMonthName'] = 'date("F", time()+60*60*24*(32-date("d")))'; # January to December

$FmtPV['$NextTuesday'] = "date('Y-m-d',strtotime('next tuesday',strtotime('yesterday')))"; // yyyy-mm-dd, today if Tuesday, otherwise next Tuesday

# If your canonical page name starts as yyyymonth or yyyy-mm-dd then
$FmtPV['$PageMonthNum'] = 'strtotime(substr("$name",0,7)."-01")    ? strftime("%m", strtotime(substr("$name",0,7)."-01"))    : ""'; // extract month number from page name
$FmtPV['$PageMonth']    = 'strtotime(substr("$name",0,7)."-01")    ? strftime("%B", strtotime(substr("$name",0,7)."-01"))    : ""'; // extract month name from page name
$FmtPV['$PageYear']     = 'strtotime(substr("$name",0,4)."-01-01") ? strftime("%Y", strtotime(substr("$name",0,4)."-01-01")) : ""'; // extract year from page name

Simon

Page creation date

To create a variable which shows the page creation time

## add page variable {$PageCreationDate} in format yyyy-mm-dd
$FmtPV['$PageCreationDate'] = 'strftime("%Y-%m-%d", $page["ctime"])';
## to use the same format that you define in config.php with $TimeFmt use
$FmtPV['$Created'] = "strftime(\$GLOBALS['TimeFmt'], \$page['ctime'])";

For pagelist ordering

(from Cookbook.CustomPagelistSortOrderFunctions).

Sorting by title, disregarding leading A/An/The

Assumes pages named or titled as in "A Title" or "The Title" and you wish to disregard the leading A/An/The.

$FmtPV['$TitleNoArticle'] = 
   'preg_replace("/^ *(?:The|An?) /i", "", (@$page["title"] ? $page["title"] : $AsSpacedFunction($name)), 1)';
(:pagelist ... order=$TitleNoArticle ...:)

Sorting with last name first

Assumes pages named or titled as ZachAble, JohnSmith, AaronZimmerman and the desire is to have it sort in that order, by last name.
Note that McBain or other last names with an embedded capital will not work in this setup.

$FmtPV['$TitleLastFirst'] =
   'preg_replace("/^(.*?)([A-Z][a-z0-9_]*)$/", "\\\\2, \\\\1", (@$page["title"] ? $page["title"] : $AsSpacedFunction($name)))';
(:pagelist ... order=$TitleLastFirst ...:)

$ProfileLinksCount and $ProfileTargets

This page variable is used in the Cookbook listings here on PmWiki.org, to count the number of profile links in a recipe or in a talk page. This number could be an indication for the popularity of the recipe.

$FmtPV['$ProfileLinksCount'] = 'ProfileLinksCount($page["targets"])';
function ProfileLinksCount($targets) {
  $cnt = substr_count($targets, "Profiles.")-1; # without the maintainer
  return $cnt>0? "+$cnt" : '';
}

Note that newer recipes usually have fewer comments than older ones, and this number does not generally depend on the Quality, the Stability, and the Ease of use of a recipe.

New version (2009-09-01)

function ProfileTargets($targets) {
  return implode(' ', str_replace("Profiles.","",
      preg_grep('/Profiles\\./',explode(',',$targets))));
}
$FmtPV['$ProfileTargets'] = 'ProfileTargets($page["targets"])';
$MarkupExpr['unique_count'] = 'count(array_unique($args))';

Notes

  • Note that concerning times and dates, now it is better and much more flexible to use ftime from the new Markup expressions: for the blog example, try [[Blog.{(ftime fmt="%Y-%m-%d")} | Today's articles]]. See Markup expressions. (This page was written when Markup Expressions didn't exist yet.)
  • For invariable strings, use the format $FmtPV['$MyVariable'] = "'some text'"; (quotes and inside, nested apostrophes).
  • For PmWiki variables, i.e. defined in config.php, use the format $FmtPV['$MyVariable'] = '$GLOBALS["PmwikiVariable"]';.
  • For some kind of arithmetics or other more complicated output, use
    quote-apostrophe-quote-dot something dot-quote-apostrophe-quote, i.e.
    $FmtPV['$MyVariable'] = "'". complicated_function() ."'"; //i.e.
    $FmtPV['$NextMonth'] = "'". date("n", mktime(0, 0, 0, date("m")+1, date("d"), date("Y")) ) ."'";

Release Notes

If the recipe has multiple releases, then release notes can be placed here. Note that it's often easier for people to work with "release dates" instead of "version numbers".

Comments

You can write all the FmtPV definitions using the php date function a bit simpler like

$FmtPV['$CurrentYear'] = 'date("Y")';

Using the php date function you are restricted to dates in English. To make your dates international use the php strftime function, for instance

# add page variable , formats today's date as yyyy-mm-dd
$FmtPV['$Today'] = 'strftime("%Y-%m-%d", time() )';

You can write a SpecialFunction function and call it like

$FmtPV['$SpecialVar'] = 'SpecialFunction($pagename)';

HansB

Thank you Hans for the simplification tip - I changed it above. About the localized dates, one should probably first call something like

setlocale("fr_FR.utf8");

As I've never had luck with these (really), eventually suggested $[April] and PmWiki's Internationalizations capabilities. --Petko April 02, 2006, at 07:51 AM

See Also

Contributors

  • All users listed on the page

Comments

See discussion at MoreCustomPageVariables-Talk

User notes +2: 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.