Extension:Semantic Google Maps/ParserCoordinates.php

From MediaWiki.org
Jump to: navigation, search

This parser function takes coordinates in format 55.76433° N, 5.334556° E and outputs decimal longitude and latitude values (55.76433 and 5.334556).

<?php
 
# Define a setup function
$wgExtensionFunctions[] = 'wfConvertCoordParserFunction_Setup';
# Add a hook to initialise the magic word
$wgHooks['LanguageGetMagic'][]       = 'wfConvertCoordParserFunction_Magic';
 
function wfConvertCoordParserFunction_Setup() {
        global $wgParser;
        # Set a function hook associating the "coordinate" magic word with our function
        $wgParser->setFunctionHook( 'coordinate', 'wfConvertCoordParserFunction_Render' );
}
 
function wfConvertCoordParserFunction_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 wfConvertCoordParserFunction_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);
 
 
 
 
                switch ($param1) {
                        case 'lat':
                                $lat = floatval($coordinates[0]);
                                if (preg_match("/S/",$coordinates[0])) {$lat *= -1;}
                                return $lat;
                        case 'lon':
                                $lon = floatval($coordinates[1]);
                                if (preg_match("/W/",$coordinates[1])) {$lon *= -1;}
                                return $lon;
                }               
}
Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox