r71893 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r71892‎ | r71893 | r71894 >
Date:18:45, 29 August 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Added tag support for finddestination
Modified paths:
  • /trunk/extensions/Maps/Maps.php (modified) (history)
  • /trunk/extensions/Maps/Maps_Settings.php (modified) (history)
  • /trunk/extensions/Maps/ParserHooks/Maps_Finddestination.php (added) (history)
  • /trunk/extensions/Maps/ParserHooks/Maps_GeoFunctions.php (modified) (history)
  • /trunk/extensions/Maps/ParserHooks/Maps_Geodistance.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Maps/ParserHooks/Maps_Finddestination.php
@@ -0,0 +1,166 @@
 2+<?php
 3+
 4+/**
 5+ * Class for the 'finddestination' parser hooks, which can find a
 6+ * destination given a starting point, an initial bearing and a distance.
 7+ *
 8+ * @since 0.7
 9+ *
 10+ * @file Maps_Finddestination.php
 11+ * @ingroup Maps
 12+ *
 13+ * @author Jeroen De Dauw
 14+ */
 15+class MapsFinddestination extends ParserHook {
 16+
 17+ /**
 18+ * No LST in pre-5.3 PHP *sigh*.
 19+ * This is to be refactored as soon as php >=5.3 becomes acceptable.
 20+ */
 21+ public static function staticMagic( array &$magicWords, $langCode ) {
 22+ $className = __CLASS__;
 23+ $instance = new $className();
 24+ return $instance->magic( $magicWords, $langCode );
 25+ }
 26+
 27+ /**
 28+ * No LST in pre-5.3 PHP *sigh*.
 29+ * This is to be refactored as soon as php >=5.3 becomes acceptable.
 30+ */
 31+ public static function staticInit( Parser &$wgParser ) {
 32+ $className = __CLASS__;
 33+ $instance = new $className();
 34+ return $instance->init( $wgParser );
 35+ }
 36+
 37+ /**
 38+ * Gets the name of the parser hook.
 39+ * @see ParserHook::getName
 40+ *
 41+ * @since 0.7
 42+ *
 43+ * @return string
 44+ */
 45+ protected function getName() {
 46+ return 'finddestination';
 47+ }
 48+
 49+ /**
 50+ * Returns an array containing the parameter info.
 51+ * @see ParserHook::getParameterInfo
 52+ *
 53+ * @since 0.7
 54+ *
 55+ * @return array
 56+ */
 57+ protected function getParameterInfo() {
 58+ global $egMapsAvailableServices, $egMapsAvailableGeoServices, $egMapsDefaultGeoService, $egMapsAvailableCoordNotations;
 59+ global $egMapsCoordinateNotation, $egMapsAllowCoordsGeocoding, $egMapsCoordinateDirectional;
 60+
 61+ return array(
 62+ 'location' => array(
 63+ 'required' => true,
 64+ 'tolower' => false
 65+ ),
 66+ 'bearing' => array(
 67+ 'type' => 'float',
 68+ 'required' => true
 69+ ),
 70+ 'distance' => array(
 71+ 'type' => 'float',
 72+ 'required' => true
 73+ ),
 74+ 'mappingservice' => array(
 75+ 'criteria' => array(
 76+ 'in_array' => $egMapsAvailableServices
 77+ ),
 78+ 'default' => false
 79+ ),
 80+ 'service' => array(
 81+ 'criteria' => array(
 82+ 'in_array' => $egMapsAvailableGeoServices
 83+ ),
 84+ 'default' => $egMapsDefaultGeoService
 85+ ),
 86+ 'format' => array(
 87+ 'criteria' => array(
 88+ 'in_array' => $egMapsAvailableCoordNotations
 89+ ),
 90+ 'aliases' => array(
 91+ 'notation'
 92+ ),
 93+ 'default' => $egMapsCoordinateNotation
 94+ ),
 95+ 'allowcoordinates' => array(
 96+ 'type' => 'boolean',
 97+ 'default' => $egMapsAllowCoordsGeocoding
 98+ ),
 99+ 'directional' => array(
 100+ 'type' => 'boolean',
 101+ 'default' => $egMapsCoordinateDirectional
 102+ ),
 103+ );
 104+ }
 105+
 106+ /**
 107+ * Returns the list of default parameters.
 108+ * @see ParserHook::getDefaultParameters
 109+ *
 110+ * @since 0.7
 111+ *
 112+ * @return array
 113+ */
 114+ protected function getDefaultParameters() {
 115+ return array( 'location', 'bearing', 'distance' );
 116+ }
 117+
 118+ /**
 119+ * Renders and returns the output.
 120+ * @see ParserHook::render
 121+ *
 122+ * @since 0.7
 123+ *
 124+ * @param array $parameters
 125+ *
 126+ * @return string
 127+ */
 128+ public function render( array $parameters ) {
 129+ $canGeocode = MapsMapper::geocoderIsAvailable();
 130+
 131+ if ( $canGeocode ) {
 132+ $location = MapsGeocoder::attemptToGeocode( $parameters['location'] );
 133+ } else {
 134+ $location = MapsCoordinateParser::parseCoordinates( $parameters['location'] );
 135+ }
 136+
 137+ if ( $location ) {
 138+ $destination = MapsGeoFunctions::findDestination(
 139+ $location,
 140+ $parameters['bearing'],
 141+ MapsDistanceParser::parseDistance( $parameters['distance'] )
 142+ );
 143+ $output = MapsCoordinateParser::formatCoordinates( $destination, $parameters['format'], $parameters['directional'] );
 144+ } else {
 145+ global $egValidatorFatalLevel;
 146+ switch ( $egValidatorFatalLevel ) {
 147+ case Validator_ERRORS_NONE:
 148+ $output = '';
 149+ break;
 150+ case Validator_ERRORS_WARN:
 151+ $output = '<b>' . htmlspecialchars( wfMsgExt( 'validator_warning_parameters', array( 'parsemag' ), 1 ) ) . '</b>';
 152+ break;
 153+ case Validator_ERRORS_SHOW: default:
 154+ // Show an error that the location could not be geocoded or the coordinates where not recognized.
 155+ if ( $canGeocode ) {
 156+ $output = htmlspecialchars( wfMsgExt( 'maps_geocoding_failed', array( 'parsemag' ), $parameters['location'] ) );
 157+ } else {
 158+ $output = htmlspecialchars( wfMsgExt( 'maps-invalid-coordinates', array( 'parsemag' ), $parameters['location'] ) );
 159+ }
 160+ break;
 161+ }
 162+ }
 163+
 164+ return $output;
 165+ }
 166+
 167+}
\ No newline at end of file
Property changes on: trunk/extensions/Maps/ParserHooks/Maps_Finddestination.php
___________________________________________________________________
Added: svn:eol-style
1168 + native
Index: trunk/extensions/Maps/ParserHooks/Maps_GeoFunctions.php
@@ -1,8 +1,12 @@
22 <?php
33
 4+// The approximate radius of the earth in meters, according to http://en.wikipedia.org/wiki/Earth_radius.
 5+define( 'Maps_EARTH_RADIUS', 6371000 );
 6+
47 /**
5 - * This file contains registration for the geographical coordinate functions
6 - * such as #geodistance for the Maps extension.
 8+ * Static class containing geographical functions.
 9+ *
 10+ * @since 0.6
711 *
812 * @file Maps_GeoFunctions.php
913 * @ingroup Maps
@@ -11,154 +15,9 @@
1216 * @author Pnelnik
1317 * @author Matěj Grabovský
1418 */
15 -
16 -if ( !defined( 'MEDIAWIKI' ) ) {
17 - die( 'Not an entry point.' );
18 -}
19 -
20 -// The approximate radius of the earth in meters, according to http://en.wikipedia.org/wiki/Earth_radius.
21 -define( 'Maps_EARTH_RADIUS', 6371000 );
22 -
23 -if ( version_compare( $wgVersion, '1.16alpha', '<' ) ) {
24 - $wgHooks['LanguageGetMagic'][] = 'efMapsGeoFunctionsMagic';
25 -}
26 -$wgHooks['ParserFirstCallInit'][] = 'efMapsGeoFunctions';
27 -
28 -/**
29 - * Adds the magic words for the parser functions.
30 - */
31 -function efMapsGeoFunctionsMagic( &$magicWords, $langCode ) {
32 - $magicWords['geodistance'] = array( 0, 'geodistance' );
33 - $magicWords['finddestination'] = array( 0, 'finddestination' );
 19+final class MapsGeoFunctions {
3420
35 - return true; // Unless we return true, other parser functions won't get loaded.
36 -}
37 -
38 -/**
39 - * Adds the parser function hooks.
40 - */
41 -function efMapsGeoFunctions( &$wgParser ) {
42 - // Hooks to enable the geocoding parser functions.
43 - $wgParser->setFunctionHook( 'finddestination', array( 'MapsGeoFunctions', 'renderFindDestination' ) );
44 -
45 - return true;
46 -}
47 -
48 -final class MapsGeoFunctions {
49 -
5021 /**
51 - * Handler for the #finddestination parser function.
52 - * See http://mapping.referata.com/wiki/Finddestination
53 - *
54 - * @since 0.6
55 - *
56 - * @param Parser $parser
57 - */
58 - public static function renderFindDestination( Parser &$parser ) {
59 - global $egMapsAvailableServices, $egMapsAvailableGeoServices, $egMapsDefaultGeoService, $egMapsAvailableCoordNotations;
60 - global $egMapsCoordinateNotation, $egMapsAllowCoordsGeocoding, $egMapsCoordinateDirectional;
61 -
62 - $args = func_get_args();
63 -
64 - // We already know the $parser.
65 - array_shift( $args );
66 -
67 - $manager = new ValidatorManager();
68 -
69 - $doCalculation = $manager->manageParameters(
70 - $args,
71 - array(
72 - 'location' => array(
73 - 'required' => true,
74 - 'tolower' => false
75 - ),
76 - 'bearing' => array(
77 - 'type' => 'float',
78 - 'required' => true
79 - ),
80 - 'distance' => array(
81 - 'type' => 'float',
82 - 'required' => true
83 - ),
84 - 'mappingservice' => array(
85 - 'criteria' => array(
86 - 'in_array' => $egMapsAvailableServices
87 - ),
88 - 'default' => false
89 - ),
90 - 'service' => array(
91 - 'criteria' => array(
92 - 'in_array' => $egMapsAvailableGeoServices
93 - ),
94 - 'default' => $egMapsDefaultGeoService
95 - ),
96 - 'format' => array(
97 - 'criteria' => array(
98 - 'in_array' => $egMapsAvailableCoordNotations
99 - ),
100 - 'aliases' => array(
101 - 'notation'
102 - ),
103 - 'default' => $egMapsCoordinateNotation
104 - ),
105 - 'allowcoordinates' => array(
106 - 'type' => 'boolean',
107 - 'default' => $egMapsAllowCoordsGeocoding
108 - ),
109 - 'directional' => array(
110 - 'type' => 'boolean',
111 - 'default' => $egMapsCoordinateDirectional
112 - ),
113 - ),
114 - array( 'location', 'bearing', 'distance' )
115 - );
116 -
117 - if ( $doCalculation ) {
118 - $parameters = $manager->getParameters( false );
119 -
120 - $canGeocode = MapsMapper::geocoderIsAvailable();
121 -
122 - if ( $canGeocode ) {
123 - $location = MapsGeocoder::attemptToGeocode( $parameters['location'] );
124 - } else {
125 - $location = MapsCoordinateParser::parseCoordinates( $parameters['location'] );
126 - }
127 -
128 - if ( $location ) {
129 - $destination = self::findDestination(
130 - $location,
131 - $parameters['bearing'],
132 - MapsDistanceParser::parseDistance( $parameters['distance'] )
133 - );
134 - $output = MapsCoordinateParser::formatCoordinates( $destination, $parameters['format'], $parameters['directional'] );
135 - } else {
136 - global $egValidatorFatalLevel;
137 - switch ( $egValidatorFatalLevel ) {
138 - case Validator_ERRORS_NONE:
139 - $output = '';
140 - break;
141 - case Validator_ERRORS_WARN:
142 - $output = '<b>' . htmlspecialchars( wfMsgExt( 'validator_warning_parameters', array( 'parsemag' ), 1 ) ) . '</b>';
143 - break;
144 - case Validator_ERRORS_SHOW: default:
145 - // Show an error that the location could not be geocoded or the coordinates where not recognized.
146 - if ( $canGeocode ) {
147 - $output = htmlspecialchars( wfMsgExt( 'maps_geocoding_failed', array( 'parsemag' ), $parameters['location'] ) );
148 - } else {
149 - $output = htmlspecialchars( wfMsgExt( 'maps-invalid-coordinates', array( 'parsemag' ), $parameters['location'] ) );
150 - }
151 - break;
152 - }
153 - }
154 - } else {
155 - // Either required parameters are missing, or there are errors while having a strict error level.
156 - $output = $manager->getErrorList();
157 - }
158 -
159 - return $output;
160 - }
161 -
162 - /**
16322 * Returns the geographical distance between two coordinates.
16423 * See http://en.wikipedia.org/wiki/Geographical_distance
16524 *
Index: trunk/extensions/Maps/ParserHooks/Maps_Geodistance.php
@@ -1,8 +1,8 @@
22 <?php
33
44 /**
5 - * Class for the 'geodistance' parser hooks,
6 - * which can transform the notation of a distance.
 5+ * Class for the 'geodistance' parser hooks, which can
 6+ * calculate the geographical distance between two points.
77 *
88 * @since 0.7
99 *
Index: trunk/extensions/Maps/Maps.php
@@ -98,6 +98,7 @@
9999 $wgAutoloadClasses['MapsCoordinates'] = $egMapsDir . 'ParserHooks/Maps_Coordinates.php';
100100 $wgAutoloadClasses['MapsDistance'] = $egMapsDir . 'ParserHooks/Maps_Distance.php';
101101 $wgAutoloadClasses['MapsGeodistance'] = $egMapsDir . 'ParserHooks/Maps_Geodistance.php';
 102+ $wgAutoloadClasses['MapsFinddestination'] = $egMapsDir . 'ParserHooks/Maps_Finddestination.php';
102103
103104 // This function has been deprecated in 1.16, but needed for earlier versions.
104105 // It's present in 1.16 as a stub, but lets check if it exists in case it gets removed at some point.
Index: trunk/extensions/Maps/Maps_Settings.php
@@ -51,7 +51,10 @@
5252 $wgHooks['LanguageGetMagic'][] = 'MapsDistance::staticMagic';
5353 # Required for #geodistance.
5454 $wgHooks['ParserFirstCallInit'][] = 'MapsGeodistance::staticInit';
55 - $wgHooks['LanguageGetMagic'][] = 'MapsGeodistance::staticMagic';
 55+ $wgHooks['LanguageGetMagic'][] = 'MapsGeodistance::staticMagic';
 56+ # Required for #finddestination.
 57+ $wgHooks['ParserFirstCallInit'][] = 'MapsFinddestination::staticInit';
 58+ $wgHooks['LanguageGetMagic'][] = 'MapsFinddestination::staticMagic';
5659
5760
5861

Status & tagging log