Difference between revisions of "MediaWiki Implementation"

From Geohashing
imported>Edgemaster
(New page: A MediaWiki extension to allow us to handle Geohashes a little nicer and more automatically. Any questions to the talk page here, although I would appreciate a poke on my talk page also. ...)
 
imported>Edgemaster
m
Line 1: Line 1:
 
A MediaWiki extension to allow us to handle Geohashes a little nicer and more automatically.
 
A MediaWiki extension to allow us to handle Geohashes a little nicer and more automatically.
 
Any questions to the talk page here, although I would appreciate a poke on my talk page also.
 
Any questions to the talk page here, although I would appreciate a poke on my talk page also.
 +
 +
Note that the whole algorithm is not fully implemented, because those on IRC said that templates could do the rest.
  
 
--[[User:Edgemaster|Edgemaster]] 21:39, 23 May 2008 (UTC)
 
--[[User:Edgemaster|Edgemaster]] 21:39, 23 May 2008 (UTC)

Revision as of 21:42, 23 May 2008

A MediaWiki extension to allow us to handle Geohashes a little nicer and more automatically. Any questions to the talk page here, although I would appreciate a poke on my talk page also.

Note that the whole algorithm is not fully implemented, because those on IRC said that templates could do the rest.

--Edgemaster 21:39, 23 May 2008 (UTC)

Installation

  • Drop the code below into mediawiki/extensions/geohashes.php
  • Add the following to mediawiki/LocalSettings.php:
require_once "$IP/extensions/geohashes.php";

The Code

Feel free to tidy it up, poke an admin if you feel your changes are worthy to be deployed on this wiki.

<?php
$wgExtensionCredits[$type][] = array(
    'name' => "Geohashing MediaWiki Parser Functions",
    'description' => "Adds {{#md5}} and {{#dow}} handlers",
    'descriptionmsg' => "",
    'version' => "1.0",
    'author' => "Edgemaster (grand.edgemaster@gmail.com)",
    'url' => "http://wiki.xkcd.com/geohashing/MediaWiki_Implementation"
);

$wgExtensionFunctions[] = 'geohashingFunction_Setup';
$wgHooks['LanguageGetMagic'][]       = 'geohashingFunction_Magic';
 
function geohashingFunction_Setup() {
    global $wgParser;
    $wgParser->setFunctionHook( 'md5', 'geohashingMd5Function_Render' );
    $wgParser->setFunctionHook( 'dow', 'geohashingDowFunction_Render' );
}
 
function geohashingFunction_Magic( &$magicWords, $langCode ) {
    $magicWords['md5'] = array( 0, 'md5' );
    $magicWords['dow'] = array( 0, 'dow' );
    return true;
}
 
function geohashingMd5Function_Render( &$parser, $src = '', $half = 0, $decimal = 0) {
    $md5 = md5(trim($src));
    if(!$half) return $md5;
    
    $md5 = substr($md5, ($half-1)*16, 16);
    if(!$decimal) return $md5;
    
    $dec = ('0x'.$md5) / pow(16,16);
    return $dec;
}

function geohashingDowFunction_Render( &$parser, $Y = 0, $m = 0, $d = 0) {
    foreach(array('Y', 'm', 'd') as $var) {
        $$var = intval($$var);
        if($$var < 10) $$var = '0' . $$var;
        if(!intval($$var)) $$var = date($var);
    }
    return trim(@file_get_contents("http://irc.peeron.com/xkcd/map/data/$Y/$m/$d"));
    // file_get_contents returns nothing on http error, we hide the warning from php
}
?>

In-wiki Usage

This extension adds two new parser functions: {{#md5}} and {{#dow}}.

#md5

md5s a string, optionally splitting into halves and decimalising.

{{#md5:string|halve|decimal}}

Parameters:

  • string: the string to md5
  • halve: (optional) splits the hash into two, as required by The Algorithm. 0 = do not half, 1 = first half, 2 = 2nd half. Defaults to 0.
  • decimal: (optional) converts the half to decimal if set. Defaults to 0, requires halve to be set to 1 or 2!

Examples:

  • {{#md5:testing testing!}}: f9da4c546dff04d46e1fab3ca0456b42
  • {{#md5:2005-05-26-10458.68}}: db9318c2259923d08b672cb305440f97
  • {{#md5:2005-05-26-10458.68|0}}: db9318c2259923d08b672cb305440f97
  • {{#md5:2005-05-26-10458.68|1}}: db9318c2259923d
  • {{#md5:2005-05-26-10458.68|2}}: 8b672cb305440f9
  • {{#md5:2005-05-26-10458.68|1|1}}: 0.857713267707
  • {{#md5:2005-05-26-10458.68|2|1}}: 0.54454306955928

#dow

Fetches the opening Dow_Jones_Industrial_Average for a date from the irc.peeron.com service.

{{#dow:year|month|day}}

Parameters:

  • year: 4 digit year
  • month: month (1 or 2 digits are acceptable)
  • day: day (1 or 2 digits are acceptable)

The only validation done is to check the values are non-null. If any of the 3 are null (or all of the 3), then today's date will be used instead. (Always requires the colon to produce a result)

Examples:

  • {{#dow:}}:
  • {{#dow:2005|05|26}}: 10458.68
  • These will fail:
    • (lack of data) {{#dow:1854|05|26}}:
    • (bad syntax) {{#dow}}: {{#dow}}