Extension:Coordinate

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
Crystal Clear action run.svg
Coordinate
Release status: unmaintained
Implementation Parser function
Description Allows converting geographical coordinates written in decimal degrees format to degrees format using a parser function.
Author(s) Robert Buzink (buzinktalk)
Latest version 1.0 (2007-09-01)
MediaWiki 1.10+
Database changes No
License No license specified
Download See the code section
Hooks used
LanguageGetMagic
Translate the Coordinate extension if it is available at translatewiki.net
Check usage and version matrix.

The Coordinate extension allows converting geographical coordinates written in decimal degrees format to degrees format using a parser function. The function only works for N/E locations, but can be easily extended.

This can be especially useful in combination with the Geographic Coordinate attribute type of the semantic mediawiki extension.

Usage[edit]

({{#coordinate:lat|5.6907811760902405° E, 50.851378111532355° N}}, {{#coordinate:lon|5.6907811760902405° E, 50.851378111532355° N}})

becomes

(5.69078117609, 50.8513781115)

Installation[edit]

  • Add the following code at the bottom of your LocalSettings.php:
    require_once "$IP/extensions/Coordinate/Coordinate.php";
    
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Code[edit]

Coordinate.php
<?php
 
$wgExtensionCredits['other'][] = array(
   'path' => __FILE__,
   'name' => 'Coordinate',
   'author' => array( 'Robert Buzink' ),
   'version' => '1.0',
   'url' => 'https://www.mediawiki.org/wiki/Extension:Coordinate',
   'description' => 'Allows converting geographical coordinates written in decimal degrees format to degrees format using a parser function',
);

# Define a setup function
$wgExtensionFunctions[] = 'wfExampleParserFunction_Setup';
# Add a hook to initialise the magic word
$wgHooks['LanguageGetMagic'][]       = 'wfExampleParserFunction_Magic';
 
function wfExampleParserFunction_Setup() {
        global $wgParser;
        # Set a function hook associating the "example" magic word with our function
        $wgParser->setFunctionHook( 'coordinate', 'wfExampleParserFunction_Render' );
}
 
function wfExampleParserFunction_Magic( &$magicWords, $langCode ) {
        # Add the magic word
        # The first array element is case sensitive, in this case it is not case sensitive
        # All remaining elements are synonyms for our parser function
        $magicWords['coordinate'] = array( 0, 'coordinate' );
        # unless we return true, other parser functions extensions won't get loaded.
        return true;
}
 
function wfExampleParserFunction_Render( &$parser, $param1 = '', $param2 = '' ) {
        # The parser function itself
        # The input parameters are wikitext with templates expanded
        # The output should be wikitext too
		
		$coordinates = preg_split("/,/", $param2);
		
		$lat = floatval($coordinates[0]);
		$lon = floatval($coordinates[1]);
				
		switch ($param1) {
			case 'lat':
				return $lat;
			case 'lon':
				return $lon;
		}		
}