User:Socoljam/ardaDates

ArdaDates.php is a "convenience" extension that converts a date into a string of links. It is designed to work with dates formatted day month year age from Tolkien's Middle-earth, but I might port it to use real, human dates, or accept various formats.

History

edit

On Wikipedia, simply making a date a link will cause it to display in the user's prefered format. But I ran into two problems: this wasn't what should happen, and I didn't know how to do it anyway. So I wrote this extension. It does require dates to be in a specific format (Day Month Year Age) but in the context of the wiki that isn't a problem. All a user does is add <date>...</date> around a date, and it returns the date as a series of wiki links.

Usage

edit

Using ArdaDates is incredibly simple. Just type the date in <date>...</date> tags, and it gives you a string of links, like:

<date>23 July 2006 IV</date>
returns
[[23_July|23]] [[July]] [[2006_IV|2006]] [[IV]]

You can also leave off the day, or the day and the month, giving something like:

<date>July 2006 IV</date>
returns
[[July]] [[2006_IV|2006]] [[IV]]

or

<date>2006 IV</date>
returns
[[2006_IV|2006]] [[IV]]

The point is that the date automatically links to a page of what happened, in any year, on that day, a page about the month, a page about that (specific) year, and a page for the age of Middle-earth. Since there are so many ages, most years appear several times (ie: 10 I, 10 II, 10 III, 10 IV) so it's important to differentiate.

Future

edit

This could easily be ported to use real dates, either by dropping the "age" parameter or changing it to BCE/CE. (In fact, if BCE/CE was used, you wouldn't need to change anything but the name.) I expect to port this to "real" dates, and possibly even use the user's prefered date format, sometime soon.

Installation

edit

Installation is as simple as any other extensions:

  • Copy the code below into your favorite editor
  • Save it as ArdaDates.php in your extensions directory
  • Add require_once( "extensions/ArdaDates.php" ); to your LocalSettings.php file
    • Good practice is to add the line somewhere below require_once( "DefaultSettings.php" );

Code

edit

Here's the code:

<?php
# ArdaDates
# Middle-earth Dates Extension
# This extension automatically generates bloody-complicated links
# for the date format used on ArdaWiki. Dates must be of the format
#    <date>[[[#day] #month] #year] #age</date>
# for example:
#    <date>1 March 2931 III</date>
# or
#    <date>May 120 IV</date>
# To activate the extension, include it from your LocalSettings.php
# with: include("extensions/ardaDates.php");
# @author James Socol <me@jamessocol.com>

$wgExtensionFunctions[] = "wfArdaDates";
$wgExtensionCredits['parserhook'][] = array(
    'name' => 'ArdaDates ',
    'version' => '1.0',
    'author' => 'James Socol',
    'description' => 'converts dates into strings of internal links',
    'url' => 'http://meta.wikemedia.org/wiki/User:Socoljam/ardaDates' );

function wfArdaDates() {
    global $wgParser;
    $wgParser->setHook( "date", "renderDate" );
}

function renderDate( $input, $argv, &$parser ) {
	if (!$parser) $parser =& $GLOBALS['wgParser'];
	
	//extract the date from the input, do it
	// backwards, to accept less specific dates,
	// like those without days or months
    list($age, $year, $month, $day) = array_reverse(explode(' ', $input, 4), false);
	
	//blank string
	$str = '';
	
	//if day and month are included,
	// make the links
	if (($day != '')&&($month != '')) {
		$str .= "[[$day $month|$day]] [[$month]] ";
	} elseif ($month != '') {
		$str .= "[[$month]] ";
	}
	
	//if the year and age are included,
	// make the links; if tag is empty,
	// return a warning string
	if (($year != '')&&($age != '')) {
		$str .= "[[$year $age|$year]] [[$age]]";
	} elseif ($age != '') {
		$str .= "[[$age]]";
	} else {
		$str .= "'''Invalid Date Format'''";
	}

	//parse wikimarkup and return
	$output = $parser->parse($str, $parser->mTitle, $parser->mOptions, false, false);
	return $output->getText();
}
?>

Examples

edit

If you want to see it in action or try to mess with something, please feel free. I'm open to any suggestions. (The link is to a "sandbox".)