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

Revision: 71893
Author:   jeroendedauw
Date:     2010-08-29 18:45:10 +0000 (Sun, 29 Aug 2010)

Log Message:
-----------
Added tag support for finddestination

Modified Paths:
--------------
    trunk/extensions/Maps/Maps.php
    trunk/extensions/Maps/Maps_Settings.php
    trunk/extensions/Maps/ParserHooks/Maps_GeoFunctions.php
    trunk/extensions/Maps/ParserHooks/Maps_Geodistance.php

Added Paths:
-----------
    trunk/extensions/Maps/ParserHooks/Maps_Finddestination.php

Modified: trunk/extensions/Maps/Maps.php
===================================================================
--- trunk/extensions/Maps/Maps.php      2010-08-29 18:29:59 UTC (rev 71892)
+++ trunk/extensions/Maps/Maps.php      2010-08-29 18:45:10 UTC (rev 71893)
@@ -98,6 +98,7 @@
        $wgAutoloadClasses['MapsCoordinates']                   = $egMapsDir . 
'ParserHooks/Maps_Coordinates.php';
        $wgAutoloadClasses['MapsDistance']                              = 
$egMapsDir . 'ParserHooks/Maps_Distance.php';
        $wgAutoloadClasses['MapsGeodistance']                   = $egMapsDir . 
'ParserHooks/Maps_Geodistance.php';
+       $wgAutoloadClasses['MapsFinddestination']               = $egMapsDir . 
'ParserHooks/Maps_Finddestination.php';
        
        // This function has been deprecated in 1.16, but needed for earlier 
versions.
        // It's present in 1.16 as a stub, but lets check if it exists in case 
it gets removed at some point.

Modified: trunk/extensions/Maps/Maps_Settings.php
===================================================================
--- trunk/extensions/Maps/Maps_Settings.php     2010-08-29 18:29:59 UTC (rev 
71892)
+++ trunk/extensions/Maps/Maps_Settings.php     2010-08-29 18:45:10 UTC (rev 
71893)
@@ -51,7 +51,10 @@
                $wgHooks['LanguageGetMagic'][] = 'MapsDistance::staticMagic';
                # Required for #geodistance.
                $wgHooks['ParserFirstCallInit'][] = 
'MapsGeodistance::staticInit';
-               $wgHooks['LanguageGetMagic'][] = 
'MapsGeodistance::staticMagic';                        
+               $wgHooks['LanguageGetMagic'][] = 'MapsGeodistance::staticMagic';
+               # Required for #finddestination.
+               $wgHooks['ParserFirstCallInit'][] = 
'MapsFinddestination::staticInit';
+               $wgHooks['LanguageGetMagic'][] = 
'MapsFinddestination::staticMagic';            
 
                
                

Added: trunk/extensions/Maps/ParserHooks/Maps_Finddestination.php
===================================================================
--- trunk/extensions/Maps/ParserHooks/Maps_Finddestination.php                  
        (rev 0)
+++ trunk/extensions/Maps/ParserHooks/Maps_Finddestination.php  2010-08-29 
18:45:10 UTC (rev 71893)
@@ -0,0 +1,166 @@
+<?php
+
+/**
+ * Class for the 'finddestination' parser hooks, which can find a
+ * destination given a starting point, an initial bearing and a distance.
+ * 
+ * @since 0.7
+ * 
+ * @file Maps_Finddestination.php
+ * @ingroup Maps
+ * 
+ * @author Jeroen De Dauw
+ */
+class MapsFinddestination extends ParserHook {
+       
+       /**
+        * No LST in pre-5.3 PHP *sigh*.
+        * This is to be refactored as soon as php >=5.3 becomes acceptable.
+        */
+       public static function staticMagic( array &$magicWords, $langCode ) {
+               $className = __CLASS__;
+               $instance = new $className();
+               return $instance->magic( $magicWords, $langCode );
+       }
+       
+       /**
+        * No LST in pre-5.3 PHP *sigh*.
+        * This is to be refactored as soon as php >=5.3 becomes acceptable.
+        */     
+       public static function staticInit( Parser &$wgParser ) {
+               $className = __CLASS__;
+               $instance = new $className();
+               return $instance->init( $wgParser );
+       }       
+       
+       /**
+        * Gets the name of the parser hook.
+        * @see ParserHook::getName
+        * 
+        * @since 0.7
+        * 
+        * @return string
+        */
+       protected function getName() {
+               return 'finddestination';
+       }
+       
+       /**
+        * Returns an array containing the parameter info.
+        * @see ParserHook::getParameterInfo
+        * 
+        * @since 0.7
+        * 
+        * @return array
+        */
+       protected function getParameterInfo() {
+               global $egMapsAvailableServices, $egMapsAvailableGeoServices, 
$egMapsDefaultGeoService, $egMapsAvailableCoordNotations;
+               global $egMapsCoordinateNotation, $egMapsAllowCoordsGeocoding, 
$egMapsCoordinateDirectional;     
+               
+               return array(
+                       'location' => array(
+                               'required' => true,
+                               'tolower' => false
+                       ),
+                       'bearing' => array(
+                               'type' => 'float',
+                               'required' => true
+                       ),
+                       'distance' => array(
+                               'type' => 'float',
+                               'required' => true
+                       ),
+                       'mappingservice' => array(
+                               'criteria' => array(
+                                       'in_array' => $egMapsAvailableServices
+                               ),
+                               'default' => false
+                       ),
+                       'service' => array(
+                               'criteria' => array(
+                                       'in_array' => 
$egMapsAvailableGeoServices
+                               ),
+                               'default' => $egMapsDefaultGeoService
+                       ),
+                       'format' => array(
+                               'criteria' => array(
+                                       'in_array' => 
$egMapsAvailableCoordNotations
+                               ),
+                               'aliases' => array(
+                                       'notation'
+                               ),
+                               'default' => $egMapsCoordinateNotation
+                       ),
+                       'allowcoordinates' => array(
+                               'type' => 'boolean',
+                               'default' => $egMapsAllowCoordsGeocoding
+                       ),
+                       'directional' => array(
+                               'type' => 'boolean',
+                               'default' => $egMapsCoordinateDirectional
+                       ),
+               );
+       }
+       
+       /**
+        * Returns the list of default parameters.
+        * @see ParserHook::getDefaultParameters
+        * 
+        * @since 0.7
+        * 
+        * @return array
+        */
+       protected function getDefaultParameters() {
+               return array( 'location', 'bearing', 'distance' );
+       }
+       
+       /**
+        * Renders and returns the output.
+        * @see ParserHook::render
+        * 
+        * @since 0.7
+        * 
+        * @param array $parameters
+        * 
+        * @return string
+        */
+       public function render( array $parameters ) {
+               $canGeocode = MapsMapper::geocoderIsAvailable();
+                       
+               if ( $canGeocode ) {
+                       $location = MapsGeocoder::attemptToGeocode( 
$parameters['location'] );
+               } else {
+                       $location = MapsCoordinateParser::parseCoordinates( 
$parameters['location'] );
+               }
+               
+               if ( $location ) {
+                       $destination = MapsGeoFunctions::findDestination(
+                               $location,
+                               $parameters['bearing'],
+                               MapsDistanceParser::parseDistance( 
$parameters['distance'] )
+                       );
+                       $output = MapsCoordinateParser::formatCoordinates( 
$destination, $parameters['format'], $parameters['directional'] );
+               } else {
+                       global $egValidatorFatalLevel;
+                       switch ( $egValidatorFatalLevel ) {
+                               case Validator_ERRORS_NONE:
+                                       $output = '';
+                                       break;
+                               case Validator_ERRORS_WARN:
+                                       $output = '<b>' . htmlspecialchars( 
wfMsgExt( 'validator_warning_parameters', array( 'parsemag' ), 1 ) ) . '</b>';
+                                       break;
+                               case Validator_ERRORS_SHOW: default:
+                                       // Show an error that the location 
could not be geocoded or the coordinates where not recognized.
+                                       if ( $canGeocode ) {
+                                               $output = htmlspecialchars( 
wfMsgExt( 'maps_geocoding_failed', array( 'parsemag' ), $parameters['location'] 
) );
+                                       } else {
+                                               $output = htmlspecialchars( 
wfMsgExt( 'maps-invalid-coordinates', array( 'parsemag' ), 
$parameters['location'] ) );
+                                       }
+                                       break;
+                       }
+               }
+                       
+               return $output;
+       }
+       
+}
\ No newline at end of file


Property changes on: trunk/extensions/Maps/ParserHooks/Maps_Finddestination.php
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: trunk/extensions/Maps/ParserHooks/Maps_GeoFunctions.php
===================================================================
--- trunk/extensions/Maps/ParserHooks/Maps_GeoFunctions.php     2010-08-29 
18:29:59 UTC (rev 71892)
+++ trunk/extensions/Maps/ParserHooks/Maps_GeoFunctions.php     2010-08-29 
18:45:10 UTC (rev 71893)
@@ -1,8 +1,12 @@
 <?php
 
+// The approximate radius of the earth in meters, according to 
http://en.wikipedia.org/wiki/Earth_radius.
+define( 'Maps_EARTH_RADIUS', 6371000 );
+
 /**
- * This file contains registration for the geographical coordinate functions
- * such as #geodistance for the Maps extension. 
+ * Static class containing geographical functions.
+ *
+ * @since 0.6
  * 
  * @file Maps_GeoFunctions.php
  * @ingroup Maps
@@ -11,154 +15,9 @@
  * @author Pnelnik
  * @author Matěj Grabovský
  */
-
-if ( !defined( 'MEDIAWIKI' ) ) {
-       die( 'Not an entry point.' );
-}
-
-// The approximate radius of the earth in meters, according to 
http://en.wikipedia.org/wiki/Earth_radius.
-define( 'Maps_EARTH_RADIUS', 6371000 );
-
-if ( version_compare( $wgVersion, '1.16alpha', '<' ) ) {
-       $wgHooks['LanguageGetMagic'][] = 'efMapsGeoFunctionsMagic';
-}
-$wgHooks['ParserFirstCallInit'][] = 'efMapsGeoFunctions';
-
-/**
- * Adds the magic words for the parser functions.
- */
-function efMapsGeoFunctionsMagic( &$magicWords, $langCode ) {
-       $magicWords['geodistance'] = array( 0, 'geodistance' );
-       $magicWords['finddestination'] = array( 0, 'finddestination' );
+final class MapsGeoFunctions { 
        
-       return true; // Unless we return true, other parser functions won't get 
loaded.
-}
-
-/**
- * Adds the parser function hooks.
- */
-function efMapsGeoFunctions( &$wgParser ) {
-       // Hooks to enable the geocoding parser functions.
-       $wgParser->setFunctionHook( 'finddestination', array( 
'MapsGeoFunctions', 'renderFindDestination' ) );
-       
-       return true;
-}
-
-final class MapsGeoFunctions {
-       
        /**
-        * Handler for the #finddestination parser function. 
-        * See http://mapping.referata.com/wiki/Finddestination
-        * 
-        * @since 0.6
-        * 
-        * @param Parser $parser
-        */
-       public static function renderFindDestination( Parser &$parser ) {
-               global $egMapsAvailableServices, $egMapsAvailableGeoServices, 
$egMapsDefaultGeoService, $egMapsAvailableCoordNotations;
-               global $egMapsCoordinateNotation, $egMapsAllowCoordsGeocoding, 
$egMapsCoordinateDirectional;
-               
-               $args = func_get_args();
-               
-               // We already know the $parser.
-               array_shift( $args );
-               
-               $manager = new ValidatorManager();
-               
-               $doCalculation = $manager->manageParameters(
-                       $args,
-                       array(
-                               'location' => array(
-                                       'required' => true,
-                                       'tolower' => false
-                               ),
-                               'bearing' => array(
-                                       'type' => 'float',
-                                       'required' => true
-                               ),
-                               'distance' => array(
-                                       'type' => 'float',
-                                       'required' => true
-                               ),
-                               'mappingservice' => array(
-                                       'criteria' => array(
-                                               'in_array' => 
$egMapsAvailableServices
-                                       ),
-                                       'default' => false
-                               ),
-                               'service' => array(
-                                       'criteria' => array(
-                                               'in_array' => 
$egMapsAvailableGeoServices
-                                       ),
-                                       'default' => $egMapsDefaultGeoService
-                               ),
-                               'format' => array(
-                                       'criteria' => array(
-                                               'in_array' => 
$egMapsAvailableCoordNotations
-                                       ),
-                                       'aliases' => array(
-                                               'notation'
-                                       ),
-                                       'default' => $egMapsCoordinateNotation
-                               ),
-                               'allowcoordinates' => array(
-                                       'type' => 'boolean',
-                                       'default' => $egMapsAllowCoordsGeocoding
-                               ),
-                               'directional' => array(
-                                       'type' => 'boolean',
-                                       'default' => 
$egMapsCoordinateDirectional
-                               ),
-                       ),
-                       array( 'location', 'bearing', 'distance' )
-               );
-
-               if ( $doCalculation ) {
-                       $parameters = $manager->getParameters( false );
-                       
-                       $canGeocode = MapsMapper::geocoderIsAvailable();
-                       
-                       if ( $canGeocode ) {
-                               $location = MapsGeocoder::attemptToGeocode( 
$parameters['location'] );
-                       } else {
-                               $location = 
MapsCoordinateParser::parseCoordinates( $parameters['location'] );
-                       }
-                       
-                       if ( $location ) {
-                               $destination = self::findDestination(
-                                       $location,
-                                       $parameters['bearing'],
-                                       MapsDistanceParser::parseDistance( 
$parameters['distance'] )
-                               );
-                               $output = 
MapsCoordinateParser::formatCoordinates( $destination, $parameters['format'], 
$parameters['directional'] );
-                       } else {
-                               global $egValidatorFatalLevel;
-                               switch ( $egValidatorFatalLevel ) {
-                                       case Validator_ERRORS_NONE:
-                                               $output = '';
-                                               break;
-                                       case Validator_ERRORS_WARN:
-                                               $output = '<b>' . 
htmlspecialchars( wfMsgExt( 'validator_warning_parameters', array( 'parsemag' 
), 1 ) ) . '</b>';
-                                               break;
-                                       case Validator_ERRORS_SHOW: default:
-                                               // Show an error that the 
location could not be geocoded or the coordinates where not recognized.
-                                               if ( $canGeocode ) {
-                                                       $output = 
htmlspecialchars( wfMsgExt( 'maps_geocoding_failed', array( 'parsemag' ), 
$parameters['location'] ) );
-                                               } else {
-                                                       $output = 
htmlspecialchars( wfMsgExt( 'maps-invalid-coordinates', array( 'parsemag' ), 
$parameters['location'] ) );
-                                               }
-                                               break;
-                               }
-                       }
-               } else {
-                       // Either required parameters are missing, or there are 
errors while having a strict error level.
-                       $output = $manager->getErrorList();
-               }
-               
-               return $output;
-       }
-       
-       /**
         * Returns the geographical distance between two coordinates.
         * See http://en.wikipedia.org/wiki/Geographical_distance
         * 

Modified: trunk/extensions/Maps/ParserHooks/Maps_Geodistance.php
===================================================================
--- trunk/extensions/Maps/ParserHooks/Maps_Geodistance.php      2010-08-29 
18:29:59 UTC (rev 71892)
+++ trunk/extensions/Maps/ParserHooks/Maps_Geodistance.php      2010-08-29 
18:45:10 UTC (rev 71893)
@@ -1,8 +1,8 @@
 <?php
 
 /**
- * Class for the 'geodistance' parser hooks, 
- * which can transform the notation of a distance.
+ * Class for the 'geodistance' parser hooks, which can
+ * calculate the geographical distance between two points.
  * 
  * @since 0.7
  * 



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

Reply via email to