Difference between revisions of "MediaWiki Implementation"

From Geohashing
imported>Tjtrumpet2323
m
m (link extensions page)
 
(61 intermediate revisions by 31 users not shown)
Line 1: Line 1:
{{30w compliant|no=1}}
+
'''Note: Version 1.01 of ''geohashes.php'' is [[Special:Version#mw-version-ext|currently]] installed on this wiki, as well as ''Variables.php''.'''
'''Note: Not yet installed on this wiki.'''
 
  
 
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.
Line 23: Line 22:
 
     'description' => "Adds {{#md5}} and {{#dow}} handlers",
 
     'description' => "Adds {{#md5}} and {{#dow}} handlers",
 
     'descriptionmsg' => "",
 
     'descriptionmsg' => "",
     'version' => "1.0",
+
     'version' => "1.01",
 
     'author' => "Edgemaster (grand.edgemaster@gmail.com)",
 
     'author' => "Edgemaster (grand.edgemaster@gmail.com)",
 
     'url' => "http://wiki.xkcd.com/geohashing/MediaWiki_Implementation"
 
     'url' => "http://wiki.xkcd.com/geohashing/MediaWiki_Implementation"
Line 47: Line 46:
 
     if(!$half) return $md5;
 
     if(!$half) return $md5;
 
      
 
      
     $md5 = substr($md5, ($half-1)*16, 16);
+
     $md5 = substr($md5, ($half-1)*16, 15);
 
     if(!$decimal) return $md5;
 
     if(!$decimal) return $md5;
 
      
 
      
     $dec = ('0x'.$md5) / pow(16,16);
+
     $dec = hexdec($md5) / pow(16,15);
 
     return $dec;
 
     return $dec;
 
}
 
}
Line 60: Line 59:
 
         if(!intval($$var)) $$var = date($var);
 
         if(!intval($$var)) $$var = date($var);
 
     }
 
     }
     return trim(@file_get_contents("http://irc.peeron.com/xkcd/map/data/$Y/$m/$d"));
+
     return trim(@file_get_contents("http://carabiner.peeron.com/xkcd/map/data/$Y/$m/$d"));
 
     // file_get_contents returns nothing on http error, we hide the warning from php
 
     // file_get_contents returns nothing on http error, we hide the warning from php
 
}
 
}
 
?></pre>
 
?></pre>
 +
 +
=== Changelog ===
 +
 +
*'''[{{fullurl:MediaWiki Implementation|diff=30594&oldid=6067}} v1.01 - 1 Jul 2008]''' Passing all 16 hex-characters of a half-hash causes PHP to misinterpret sign when decimalising, so instead we only pass the first 15... besides, that still gives microscopic accuracy (~100 femtometres).  Hence the fifteens shown in lines 30 and 33 above.  --[[User:Tjtrumpet2323|Tim P]] 19:13, 1 July 2008 (UTC)
  
 
== In-wiki Usage ==
 
== In-wiki Usage ==
Line 74: Line 77:
 
Parameters:
 
Parameters:
 
* '''string''': the string to md5
 
* '''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.
+
* '''halve''': ''(optional)'' splits the hash into two, as required by [[The Algorithm]]. 0 = do not halve, 1 = first half, 2 = 2nd half. Defaults to 0.  If '''halve'''!=0, only the first 15 hex digits of the corresponding half are returned.
 
* '''decimal''': ''(optional)'' converts the half to decimal if set. Defaults to 0, ''requires halve to be set to 1 or 2''!
 
* '''decimal''': ''(optional)'' converts the half to decimal if set. Defaults to 0, ''requires halve to be set to 1 or 2''!
  
 
Examples:
 
Examples:
*<nowiki>{{#md5:testing testing!}}:</nowiki> {{#md5:testing testing!}}
+
{| class="wikitable"
*<nowiki>{{#md5:2005-05-26-10458.68}}:</nowiki> {{#md5:2005-05-26-10458.68}}
+
|-
*<nowiki>{{#md5:2005-05-26-10458.68|0}}:</nowiki> {{#md5:2005-05-26-10458.68|0}}
+
! Template code !! Expected output !! Template output
*<nowiki>{{#md5:2005-05-26-10458.68|1}}:</nowiki> {{#md5:2005-05-26-10458.68|1}}
+
|-
*<nowiki>{{#md5:2005-05-26-10458.68|2}}:</nowiki> {{#md5:2005-05-26-10458.68|2}}
+
| <code><nowiki>{{#md5:testing testing!}}</nowiki></code> || f9da4c546dff04d46e1fab3ca0456b42 || {{#md5:testing testing!}}
*<nowiki>{{#md5:2005-05-26-10458.68|1|1}}:</nowiki> {{#md5:2005-05-26-10458.68|1|1}}
+
|-
*<nowiki>{{#md5:2005-05-26-10458.68|2|1}}:</nowiki> {{#md5:2005-05-26-10458.68|2|1}}
+
| <code><nowiki>{{#md5:2005-05-26-10458.68}}</nowiki></code> || db9318c2259923d08b672cb305440f97 || {{#md5:2005-05-26-10458.68}}
 +
|-
 +
| <code><nowiki>{{#md5:2005-05-26-10458.68|0}}</nowiki></code> || db9318c2259923d08b672cb305440f97 || {{#md5:2005-05-26-10458.68|0}}
 +
|-
 +
| <code><nowiki>{{#md5:2005-05-26-10458.68|1}}</nowiki></code> || db9318c2259923d || {{#md5:2005-05-26-10458.68|1}}
 +
|-
 +
| <code><nowiki>{{#md5:2005-05-26-10458.68|2}}</nowiki></code> || 8b672cb305440f9 || {{#md5:2005-05-26-10458.68|2}}
 +
|-
 +
| <code><nowiki>{{#md5:2005-05-26-10458.68|1|1}}</nowiki></code> || 0.857713267707 || {{#md5:2005-05-26-10458.68|1|1}}
 +
|-
 +
| <code><nowiki>{{#md5:2005-05-26-10458.68|2|1}}</nowiki></code> || 0.54454306955928 || {{#md5:2005-05-26-10458.68|2|1}}
 +
|}
  
 
=== #dow ===
 
=== #dow ===
Fetches the opening [[Dow_Jones_Industrial_Average|Dow Index]] for a date from the irc.peeron.com service.
+
Fetches the opening [[Dow_Jones_Industrial_Average|Dow Index]] for a date from the carabiner.peeron.com service.
 
  <nowiki>{{</nowiki>#dow:'''year'''|'''month'''|'''day'''<nowiki>}}</nowiki>
 
  <nowiki>{{</nowiki>#dow:'''year'''|'''month'''|'''day'''<nowiki>}}</nowiki>
  
Line 97: Line 111:
  
 
Examples:
 
Examples:
*<nowiki>{{#dow:}}:</nowiki> {{#dow:}}
+
{| class="wikitable"
*<nowiki>{{#dow:2005|05|26}}:</nowiki> {{#dow:2005|05|26}}
+
|-
*These will fail:
+
! Template code !! Expected output !! Template output
**<nowiki>(lack of data) {{#dow:1854|05|26}}:</nowiki> {{#dow:1854|05|26}}
+
|-
**<nowiki>(bad syntax) {{#dow}}:</nowiki> {{#dow}}
+
| <code><nowiki>{{#dow:}}</nowiki></code> || ''[[Dow]] opening for today, if available (past 09:30 [[ET]])'' || {{#dow:}}
 +
|-
 +
| <code><nowiki>{{#dow:2005|05|26}}</nowiki></code> || 10458.68 || {{#dow:2005|05|26}}
 +
|-
 +
| <code><nowiki>{{#dow:1854|05|26}}</nowiki></code> || ''fails (no data)'' || {{#dow:1854|05|26}}
 +
|-
 +
| <code><nowiki>{{#dow}}</nowiki></code> || ''fails (bad syntax)'' || {{#dow}}
 +
|}
 +
 
 +
==Variables Extension too plz==
 +
 
 +
=== Installing ===
 +
 
 +
Copy this into a file called $mediwikipath/extensions/Variables/Variables.php:
 +
 
 +
<pre>
 +
<?php
 +
 
 +
if ( !defined( 'MEDIAWIKI' ) ) {
 +
    die( 'This file is a MediaWiki extension, it is not a valid entry point' );
 +
}
 +
 
 +
$wgExtensionFunctions[] = 'wfSetupVariables';
 +
 
 +
$wgExtensionCredits['parserhook'][] = array(
 +
'name' => 'Variables',
 +
'url' => 'http://www.mediawiki.org/wiki/Extension:VariablesExtension',
 +
'author' => 'Rob Adams',
 +
'description' => 'Define page-scoped variables'
 +
);
 +
 
 +
$wgHooks['LanguageGetMagic'][]      = 'wfVariablesLanguageGetMagic';
 +
 
 +
class ExtVariables {
 +
    var $mVariables;
 +
 
 +
    function vardefine( &$parser, $expr = '', $value = '' ) {
 +
$this->mVariables[$expr] = $value;
 +
return '';
 +
    }
 +
 
 +
    function varf( &$parser, $expr = '' ) {
 +
return $this->mVariables[$expr];
 +
    }
 +
}
 +
 
 +
function wfSetupVariables() {
 +
    global $wgParser, $wgMessageCache, $wgExtVariables, $wgMessageCache, $wgHooks;
 +
 
 +
    $wgExtVariables = new ExtVariables;
 +
 
 +
    $wgParser->setFunctionHook( 'vardefine', array( &$wgExtVariables, 'vardefine' ) );
 +
    $wgParser->setFunctionHook( 'var', array( &$wgExtVariables, 'varf' ) );
 +
}
 +
 
 +
function wfVariablesLanguageGetMagic( &$magicWords, $langCode = 0 ) {
 +
require_once( dirname( __FILE__ ) . '/Variables.i18n.php' );
 +
foreach( efVariablesWords( $langCode ) as $word => $trans )
 +
$magicWords[$word] = $trans;
 +
return true;
 +
}
 +
</pre>
 +
 
 +
Copy this into a file called $mediwikipath/extensions/Variables/Variables.i18n.php:
 +
<pre>
 +
 
 +
<?php
 +
 
 +
/**
 +
* Get translated magic words, if available
 +
*
 +
* @param string $lang Language code
 +
* @return array
 +
*/
 +
function efVariablesWords( $lang ) {
 +
$words = array();
 +
 
 +
/**
 +
* English
 +
*/
 +
$words['en'] = array(
 +
'var' => array( 0, 'var' ),
 +
'vardefine' => array( 0, 'vardefine' ),
 +
);
 +
 
 +
# English is used as a fallback, and the English synonyms are
 +
# used if a translation has not been provided for a given word
 +
return ( $lang == 'en' || !isset( $words[$lang] ) )
 +
? $words['en']
 +
: array_merge( $words['en'], $words[$lang] );
 +
}
 +
</pre>
 +
 
 +
Then add
 +
<pre>require_once( "$IP/extensions/Variables/Variables.php" );</pre>
 +
to the end of LocalSettings.php.
 +
 
 +
[[Category:Algorithm]]
 +
[[Category:Implementations]]

Latest revision as of 01:28, 29 January 2021

Note: Version 1.01 of geohashes.php is currently installed on this wiki, as well as Variables.php.

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.01",
    '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, 15);
    if(!$decimal) return $md5;
    
    $dec = hexdec($md5) / pow(16,15);
    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://carabiner.peeron.com/xkcd/map/data/$Y/$m/$d"));
    // file_get_contents returns nothing on http error, we hide the warning from php
}
?>

Changelog

  • v1.01 - 1 Jul 2008 Passing all 16 hex-characters of a half-hash causes PHP to misinterpret sign when decimalising, so instead we only pass the first 15... besides, that still gives microscopic accuracy (~100 femtometres). Hence the fifteens shown in lines 30 and 33 above. --Tim P 19:13, 1 July 2008 (UTC)

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 halve, 1 = first half, 2 = 2nd half. Defaults to 0. If halve!=0, only the first 15 hex digits of the corresponding half are returned.
  • decimal: (optional) converts the half to decimal if set. Defaults to 0, requires halve to be set to 1 or 2!

Examples:

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

#dow

Fetches the opening Dow Index for a date from the carabiner.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:

Template code Expected output Template output
{{#dow:}} Dow opening for today, if available (past 09:30 ET)
{{#dow:2005|05|26}} 10458.68 10458.68
{{#dow:1854|05|26}} fails (no data)
{{#dow}} fails (bad syntax) {{#dow}}

Variables Extension too plz

Installing

Copy this into a file called $mediwikipath/extensions/Variables/Variables.php:

<?php

if ( !defined( 'MEDIAWIKI' ) ) {
    die( 'This file is a MediaWiki extension, it is not a valid entry point' );
}

$wgExtensionFunctions[] = 'wfSetupVariables';

$wgExtensionCredits['parserhook'][] = array(
	'name' => 'Variables',
	'url' => 'http://www.mediawiki.org/wiki/Extension:VariablesExtension',
	'author' => 'Rob Adams',
	'description' => 'Define page-scoped variables'
);

$wgHooks['LanguageGetMagic'][]       = 'wfVariablesLanguageGetMagic';

class ExtVariables {
    var $mVariables;

    function vardefine( &$parser, $expr = '', $value = '' ) {
	$this->mVariables[$expr] = $value;
	return '';
    }

    function varf( &$parser, $expr = '' ) {
	return $this->mVariables[$expr];
    }
}

function wfSetupVariables() {
    global $wgParser, $wgMessageCache, $wgExtVariables, $wgMessageCache, $wgHooks;

    $wgExtVariables = new ExtVariables;

    $wgParser->setFunctionHook( 'vardefine', array( &$wgExtVariables, 'vardefine' ) );
    $wgParser->setFunctionHook( 'var', array( &$wgExtVariables, 'varf' ) );
}

function wfVariablesLanguageGetMagic( &$magicWords, $langCode = 0 ) {
	require_once( dirname( __FILE__ ) . '/Variables.i18n.php' );
	foreach( efVariablesWords( $langCode ) as $word => $trans )
		$magicWords[$word] = $trans;
	return true;
}

Copy this into a file called $mediwikipath/extensions/Variables/Variables.i18n.php:


<?php

/**
 * Get translated magic words, if available
 *
 * @param string $lang Language code
 * @return array
 */
function efVariablesWords( $lang ) {
	$words = array();

	/**
	 * English
	 */
	$words['en'] = array(
		'var' 		=> array( 0, 'var' ),
		'vardefine' 	=> array( 0, 'vardefine' ),
	);

	# English is used as a fallback, and the English synonyms are
	# used if a translation has not been provided for a given word
	return ( $lang == 'en' || !isset( $words[$lang] ) )
		? $words['en']
		: array_merge( $words['en'], $words[$lang] );
}

Then add

require_once( "$IP/extensions/Variables/Variables.php" );

to the end of LocalSettings.php.