http://www.mediawiki.org/wiki/Special:Code/MediaWiki/71846

Revision: 71846
Author:   jeroendedauw
Date:     2010-08-28 02:33:07 +0000 (Sat, 28 Aug 2010)

Log Message:
-----------
Added tag hook support for 'coordinates'

Modified Paths:
--------------
    trunk/extensions/Maps/ParserHooks/Maps_Coordinates.php

Modified: trunk/extensions/Maps/ParserHooks/Maps_Coordinates.php
===================================================================
--- trunk/extensions/Maps/ParserHooks/Maps_Coordinates.php      2010-08-28 
02:30:54 UTC (rev 71845)
+++ trunk/extensions/Maps/ParserHooks/Maps_Coordinates.php      2010-08-28 
02:33:07 UTC (rev 71846)
@@ -2,8 +2,8 @@
 
 /**
  * This file contains registration for the #coordinates parser function,
- * which can transform the notation of a set of coordinates.
  * 
+ * 
  * @file Maps_Coordinates.php
  * @ingroup Maps
  *
@@ -14,45 +14,106 @@
        die( 'Not an entry point.' );
 }
 
+$wgAutoloadClasses['MapsCoordinates'] = dirname( __FILE__ ) . 
'/Maps_Coordinates.php';
+
+$wgHooks['ParserFirstCallInit'][] = 'MapsCoordinates::init';
+
 if ( version_compare( $wgVersion, '1.16alpha', '<' ) ) {
-       $wgHooks['LanguageGetMagic'][] = 'efMapsCoordinatesMagic';
+       $wgHooks['LanguageGetMagic'][] = 'MapsCoordinates::magic';
 }
-$wgHooks['ParserFirstCallInit'][] = 'efMapsCoordinatesFunction';
 
 /**
- * Adds the magic words for the parser functions.
+ * Class for the 'coordinates' parser hooks, 
+ * which can transform the notation of a set of coordinates.
+ * 
+ * @since 0.7
+ * 
+ * @author Jeroen De Dauw
  */
-function efMapsCoordinatesMagic( &$magicWords, $langCode ) {
-       $magicWords['coordinates'] = array( 0, 'coordinates' );
+class MapsCoordinates {
        
-       return true; // Unless we return true, other parser functions won't get 
loaded.
-}
-
-/**
- * Adds the parser function hooks.
- */
-function efMapsCoordinatesFunction( &$wgParser ) {
-       // Hooks to enable the geocoding parser functions.
-       $wgParser->setFunctionHook( 'coordinates', 'efMapsRenderCoordinates' );
+       /**
+        * Function to hook up the coordinate rendering functions to the parser.
+        * 
+        * @since 0.7
+        * 
+        * @param Parser $wgParser
+        * 
+        * @return true
+        */
+       public static function init( Parser &$wgParser ) {
+               $wgParser->setHook( 'coordinates', __CLASS__ . '::renderTag' );
+               $wgParser->setFunctionHook( 'coordinates', __CLASS__ . 
'::renderFunction' );
+               
+               return true;
+       }
        
-       return true;
-}
-
-function efMapsRenderCoordinates() {   
-       global $egMapsAvailableServices, $egMapsAvailableCoordNotations;
-       global $egMapsDefaultServices, $egMapsDefaultGeoService, 
$egMapsCoordinateNotation;
-       global $egMapsAllowCoordsGeocoding, $egMapsCoordinateDirectional;       
+       /**
+        * Function to add the magic word in pre MW 1.16.
+        * 
+        * @since 0.7
+        * 
+        * @param array $magicWords
+        * @param string $langCode
+        * 
+        * @return true
+        */
+       public static function magic( array &$magicWords, $langCode ) {
+               $magicWords['coordinates'] = array( 0, 'coordinates' );
+               
+               return true;
+       }       
        
-       $args = func_get_args();
+       /**
+        * Handler for rendering the tag hook.
+        * 
+        * @since 0.7
+        * 
+        * @param minxed $input string or null
+        * @param array $args
+        * @param Parser $parser
+        * @param PPFrame $frame
+        */
+       public static function renderTag( $input, array $args, Parser $parser, 
PPFrame $frame ) {
+               $defaultParam = array_shift( self::getDefaultParameters() );
+               
+               if ( !is_null( $defaultParam ) ) {
+                       $args[$defaultParam] = $input;
+               }
+               
+               return self::render( $args, true );
+       }
        
-       // We already know the $parser.
-       array_shift( $args );
+       /**
+        * Handler for rendering the function hook.
+        * 
+        * @since 0.7
+        * 
+        * @param Parser $parser
+        * ... further arguments ...
+        */     
+       public static function renderFunction() {
+               $args = func_get_args();
+               
+               // No need for the parser...
+               array_shift( $args );   
        
-       $manager = new ValidatorManager();
+               return array( self::render( $args, false ) );
+       }
        
-       $doFormatting = $manager->manageParameters(
-               $args,
-               array(
+       /**
+        * Returns an array containing the parameter info.
+        * 
+        * @since 0.7
+        * 
+        * @return array
+        */
+       protected static function getParameterInfo() {
+               global $egMapsAvailableServices, $egMapsAvailableCoordNotations;
+               global $egMapsDefaultServices, $egMapsDefaultGeoService, 
$egMapsCoordinateNotation;
+               global $egMapsAllowCoordsGeocoding, 
$egMapsCoordinateDirectional;
+                               
+               return array(
                        'location' => array(
                                'required' => true,
                                'tolower' => false
@@ -69,30 +130,70 @@
                        'directional' => array(
                                'type' => 'boolean',
                                'default' => $egMapsCoordinateDirectional
-                       ),
-               ),
-               array( 'location', 'format', 'directional' )
-       );
+                       )
+               );
+       }
        
-       if ( $doFormatting ) {
-               $parameters = $manager->getParameters( false );
+       /**
+        * Returns the list of default parameters.
+        * 
+        * @since 0.7
+        * 
+        * @return array
+        */
+       protected static function getDefaultParameters() {
+               return array( 'location', 'format', 'directional' );
+       }
+       
+       /**
+        * Renders and returns the output.
+        * 
+        * @since 0.7
+        * 
+        * @param array $arguments
+        * @param boolean $parsed
+        * 
+        * @return string
+        */
+       public static function render( array $arguments, $parsed ) {
+               $manager = new ValidatorManager();
                
-               $parsedCoords = MapsCoordinateParser::parseCoordinates( 
$parameters['location'] );
+               if ( $parsed ) {
+                       $doFormatting = $manager->manageParsedParameters(
+                               $arguments,
+                               self::getParameterInfo(),
+                               self::getDefaultParameters()
+                       );                      
+               }
+               else {
+                       $doFormatting = $manager->manageParameters(
+                               $arguments,
+                               self::getParameterInfo(),
+                               self::getDefaultParameters()
+                       );                      
+               }
                
-               if ( $parsedCoords ) {
-                       $output = MapsCoordinateParser::formatCoordinates( 
$parsedCoords, $parameters['format'], $parameters['directional'] );
+               if ( $doFormatting ) {
+                       $parameters = $manager->getParameters( false );
+                       
+                       $parsedCoords = MapsCoordinateParser::parseCoordinates( 
$parameters['location'] );
+                       
+                       if ( $parsedCoords ) {
+                               $output = 
MapsCoordinateParser::formatCoordinates( $parsedCoords, $parameters['format'], 
$parameters['directional'] );
+                       } else {
+                               $output = htmlspecialchars( wfMsgExt( 
'maps-invalid-coordinates', 'parsemag', $parameters['location'] ) );
+                       }
+                       
+                       $errorList = $manager->getErrorList();
+       
+                       if ( $errorList != '' ) {
+                               $output .= '<br />' . $errorList;
+                       }
                } else {
-                       $output = htmlspecialchars( wfMsgExt( 
'maps-invalid-coordinates', 'parsemag', $parameters['location'] ) );
+                       $output = $manager->getErrorList();
                }
-               
-               $errorList = $manager->getErrorList();
-
-               if ( $errorList != '' ) {
-                       $output .= '<br />' . $errorList;
-               }
-       } else {
-               $output = $manager->getErrorList();
+       
+               return $output;         
        }
-
-       return array( $output );
+       
 }
\ No newline at end of file



_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to