r83925 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r83924‎ | r83925 | r83926 >
Date:16:52, 14 March 2011
Author:jeroendedauw
Status:deferred
Tags:
Comment:
MFT r82185
Modified paths:
  • /branches/Maps0.8/includes/Maps_Geocoder.php (modified) (history)
  • /branches/Maps0.8/includes/geocoders/Maps_GeonamesGeocoder.php (modified) (history)
  • /branches/Maps0.8/includes/geocoders/Maps_GeonamesOldGeocoder.php (modified) (history)
  • /branches/Maps0.8/includes/geocoders/Maps_GoogleGeocoder.php (modified) (history)
  • /branches/Maps0.8/includes/geocoders/Maps_YahooGeocoder.php (modified) (history)
  • /branches/Maps0.8/includes/manipulations/Maps_ParamGeoService.php (added) (history)

Diff [purge]

Index: branches/Maps0.8/includes/manipulations/Maps_ParamGeoService.php
@@ -0,0 +1,239 @@
 2+<?php
 3+
 4+/**
 5+ * Parameter manipulation ensuring the value is a geocoding service,
 6+ * and taking care of both alias resolving and overrides handling.
 7+ *
 8+ * @since 0.7.5
 9+ *
 10+ * @file Maps_ParamGeoService.php
 11+ * @ingroup Maps
 12+ * @ingroup ParameterManipulations
 13+ *
 14+ * @author Jeroen De Dauw
 15+ */
 16+class MapsParamGeoService extends ItemParameterManipulation {
 17+
 18+ /**
 19+ * @since 0.7.5
 20+ *
 21+ * @var string or false
 22+ */
 23+ protected $mappingServiceParam;
 24+
 25+ /**
 26+ * Constructor.
 27+ *
 28+ * @since 0.7.5
 29+ */
 30+ public function __construct( $mappingServiceParam = false ) {
 31+ parent::__construct();
 32+
 33+ $this->mappingServiceParam = $mappingServiceParam;
 34+ }
 35+
 36+ /**
 37+ * @see ItemParameterManipulation::doManipulation
 38+ *
 39+ * @since 0.7.5
 40+ */
 41+ public function doManipulation( &$value, Parameter $parameter, array &$parameters ) {
 42+ global $egMapsDefaultGeoService, $egMapsUserGeoOverrides;
 43+ static $validatedDefault = false;
 44+
 45+ if ( !MapsGeocoders::canGeocode() ) {
 46+ throw new Exception( 'There are no geocoders registered, so no geocoding can happen.' );
 47+ }
 48+
 49+ // Get rid of any aliases.
 50+ $value = $this->getMainIndentifier( $value );
 51+
 52+ // Override the defaulting.
 53+ if ( $parameter->wasSetToDefault() && $this->mappingServiceParam !== false ) {
 54+ $value = self::resolveOverrides( $value, $parameters[$this->mappingServiceParam]->getValue() );
 55+ }
 56+
 57+ if ( $value == '' || !array_key_exists( $value, MapsGeocoders::$registeredGeocoders ) ) {
 58+ if ( !$validatedDefault ) {
 59+ if ( !array_key_exists( $egMapsDefaultGeoService, MapsGeocoders::$registeredGeocoders ) ) {
 60+ $egMapsDefaultGeoService = array_shift( array_keys( MapsGeocoders::$registeredGeocoders ) );
 61+ if ( is_null( $egMapsDefaultGeoService ) ) {
 62+ throw new Exception( 'Tried to geocode while there are no geocoders available at ' . __METHOD__ );
 63+ }
 64+ }
 65+ }
 66+
 67+ if ( array_key_exists( $egMapsDefaultGeoService, MapsGeocoders::$registeredGeocoders ) ) {
 68+ $value = $egMapsDefaultGeoService;
 69+ }
 70+ else {
 71+ throw new Exception( 'Attempt to use the default geocoder while it does not exist.' );
 72+ }
 73+ }
 74+ }
 75+
 76+ /**
 77+ * Replaces the geocoder identifier in case there is an override specified by
 78+ * one of the registered geocoders.
 79+ *
 80+ * @since 0.7.5
 81+ *
 82+ * @param string $geocoderIdentifier
 83+ * @param string $mappingService
 84+ *
 85+ * @return string
 86+ */
 87+ protected static function resolveOverrides( $geocoderIdentifier, $mappingService ) {
 88+ static $overrides = false;
 89+
 90+ if ( $overrides === false ) {
 91+ $overrides = array();
 92+
 93+ foreach ( MapsGeocoders::$registeredGeocoders as $key => $class ) {
 94+ $overrides[$key] = call_user_func( array( $class, 'getOverrides' ) );
 95+ }
 96+ }
 97+
 98+ foreach ( $overrides as $geocoder => $services ) {
 99+ if ( in_array( $mappingService, $services ) ) {
 100+ return $geocoder;
 101+ }
 102+ }
 103+
 104+ return $geocoderIdentifier;
 105+ }
 106+
 107+ /**
 108+ * Gets the main geocoder identifier by resolving aliases.
 109+ *
 110+ * @since 0.7.5
 111+ *
 112+ * @param string $geocoderIdentifier
 113+ *
 114+ * @return string
 115+ */
 116+ protected function getMainIndentifier( $geocoderIdentifier ) {
 117+ // TODO: implement actual function
 118+ return $geocoderIdentifier;
 119+ }
 120+
 121+}<?php
 122+
 123+/**
 124+ * Parameter manipulation ensuring the value is a geocoding service,
 125+ * and taking care of both alias resolving and overrides handling.
 126+ *
 127+ * @since 0.7.5
 128+ *
 129+ * @file Maps_ParamGeoService.php
 130+ * @ingroup Maps
 131+ * @ingroup ParameterManipulations
 132+ *
 133+ * @author Jeroen De Dauw
 134+ */
 135+class MapsParamGeoService extends ItemParameterManipulation {
 136+
 137+ /**
 138+ * @since 0.7.5
 139+ *
 140+ * @var string or false
 141+ */
 142+ protected $mappingServiceParam;
 143+
 144+ /**
 145+ * Constructor.
 146+ *
 147+ * @since 0.7.5
 148+ */
 149+ public function __construct( $mappingServiceParam = false ) {
 150+ parent::__construct();
 151+
 152+ $this->mappingServiceParam = $mappingServiceParam;
 153+ }
 154+
 155+ /**
 156+ * @see ItemParameterManipulation::doManipulation
 157+ *
 158+ * @since 0.7.5
 159+ */
 160+ public function doManipulation( &$value, Parameter $parameter, array &$parameters ) {
 161+ global $egMapsDefaultGeoService, $egMapsUserGeoOverrides;
 162+ static $validatedDefault = false;
 163+
 164+ if ( !MapsGeocoders::canGeocode() ) {
 165+ throw new Exception( 'There are no geocoders registered, so no geocoding can happen.' );
 166+ }
 167+
 168+ // Get rid of any aliases.
 169+ $value = $this->getMainIndentifier( $value );
 170+
 171+ // Override the defaulting.
 172+ if ( $parameter->wasSetToDefault() && $this->mappingServiceParam !== false ) {
 173+ $value = self::resolveOverrides( $value, $parameters[$this->mappingServiceParam]->getValue() );
 174+ }
 175+
 176+ if ( $value == '' || !array_key_exists( $value, MapsGeocoders::$registeredGeocoders ) ) {
 177+ if ( !$validatedDefault ) {
 178+ if ( !array_key_exists( $egMapsDefaultGeoService, MapsGeocoders::$registeredGeocoders ) ) {
 179+ $egMapsDefaultGeoService = array_shift( array_keys( MapsGeocoders::$registeredGeocoders ) );
 180+ if ( is_null( $egMapsDefaultGeoService ) ) {
 181+ throw new Exception( 'Tried to geocode while there are no geocoders available at ' . __METHOD__ );
 182+ }
 183+ }
 184+ }
 185+
 186+ if ( array_key_exists( $egMapsDefaultGeoService, MapsGeocoders::$registeredGeocoders ) ) {
 187+ $value = $egMapsDefaultGeoService;
 188+ }
 189+ else {
 190+ throw new Exception( 'Attempt to use the default geocoder while it does not exist.' );
 191+ }
 192+ }
 193+ }
 194+
 195+ /**
 196+ * Replaces the geocoder identifier in case there is an override specified by
 197+ * one of the registered geocoders.
 198+ *
 199+ * @since 0.7.5
 200+ *
 201+ * @param string $geocoderIdentifier
 202+ * @param string $mappingService
 203+ *
 204+ * @return string
 205+ */
 206+ protected static function resolveOverrides( $geocoderIdentifier, $mappingService ) {
 207+ static $overrides = false;
 208+
 209+ if ( $overrides === false ) {
 210+ $overrides = array();
 211+
 212+ foreach ( MapsGeocoders::$registeredGeocoders as $key => $class ) {
 213+ $overrides[$key] = call_user_func( array( $class, 'getOverrides' ) );
 214+ }
 215+ }
 216+
 217+ foreach ( $overrides as $geocoder => $services ) {
 218+ if ( in_array( $mappingService, $services ) ) {
 219+ return $geocoder;
 220+ }
 221+ }
 222+
 223+ return $geocoderIdentifier;
 224+ }
 225+
 226+ /**
 227+ * Gets the main geocoder identifier by resolving aliases.
 228+ *
 229+ * @since 0.7.5
 230+ *
 231+ * @param string $geocoderIdentifier
 232+ *
 233+ * @return string
 234+ */
 235+ protected function getMainIndentifier( $geocoderIdentifier ) {
 236+ // TODO: implement actual function
 237+ return $geocoderIdentifier;
 238+ }
 239+
 240+}
Property changes on: branches/Maps0.8/includes/manipulations/Maps_ParamGeoService.php
___________________________________________________________________
Added: svn:eol-style
1241 + native
Index: branches/Maps0.8/includes/Maps_Geocoder.php
@@ -143,7 +143,7 @@
144144 *
145145 * @return array
146146 */
147 - public function getOverrides() {
 147+ public static function getOverrides() {
148148 return array();
149149 }
150150
Index: branches/Maps0.8/includes/geocoders/Maps_GoogleGeocoder.php
@@ -9,7 +9,8 @@
1010 * @ingroup Maps
1111 * @ingroup Geocoders
1212 *
13 - * @author Jeroen De Dauw
 13+ * @licence GNU GPL v3
 14+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
1415 * @author Sergey Chernyshev
1516 */
1617 final class MapsGoogleGeocoder extends MapsGeocoder {
@@ -74,7 +75,7 @@
7576 *
7677 * @return array
7778 */
78 - public function getOverrides() {
 79+ public static function getOverrides() {
7980 return array( 'googlemaps2', 'googlemaps3' );
8081 }
8182
Index: branches/Maps0.8/includes/geocoders/Maps_GeonamesOldGeocoder.php
@@ -10,7 +10,9 @@
1111 * @ingroup Maps
1212 * @ingroup Geocoders
1313 *
14 - * @author Jeroen De Dauw
 14+ * @licence GNU GPL v3
 15+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 16+ *
1517 * Thanks go to Joel Natividad for pointing me to the GeoNames services.
1618 */
1719 final class MapsGeonamesOldGeocoder extends MapsGeocoder {
Index: branches/Maps0.8/includes/geocoders/Maps_GeonamesGeocoder.php
@@ -3,14 +3,13 @@
44 /**
55 * Class for geocoding requests with the GeoNames webservice.
66 *
7 - * GeoNames Web Services Documentation: http://www.geonames.org/export/geonames-search.html
8 - *
 7+ * @since 0.8
98 * @file Maps_GeonamesGeocoder.php
109 * @ingroup Maps
1110 * @ingroup Geocoders
1211 *
13 - * @author Jeroen De Dauw
14 - * Thanks go to Joel Natividad for pointing me to the GeoNames services.
 12+ * @licence GNU GPL v3
 13+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
1514 */
1615 final class MapsGeonamesGeocoder extends MapsGeocoder {
1716
@@ -35,7 +34,7 @@
3635 /**
3736 * @see MapsGeocoder::getRequestUrl
3837 *
39 - * @since 0.7
 38+ * @since 0.8
4039 *
4140 * @param string $address
4241 *
@@ -49,7 +48,7 @@
5049 /**
5150 * @see MapsGeocoder::parseResponse
5251 *
53 - * @since 0.7
 52+ * @since 0.8
5453 *
5554 * @param string $address
5655 *
Index: branches/Maps0.8/includes/geocoders/Maps_YahooGeocoder.php
@@ -9,7 +9,8 @@
1010 * @ingroup Maps
1111 * @ingroup Geocoders
1212 *
13 - * @author Jeroen De Dauw
 13+ * @licence GNU GPL v3
 14+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
1415 */
1516 final class MapsYahooGeocoder extends MapsGeocoder {
1617
@@ -69,7 +70,7 @@
7071 *
7172 * @return array
7273 */
73 - public function getOverrides() {
 74+ public static function getOverrides() {
7475 return array( 'yahoomaps' );
7576 }
7677

Follow-up revisions

RevisionCommit summaryAuthorDate
r83926follow up to r83925jeroendedauw16:58, 14 March 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r82185Some geocoder related fixes and version increment to 0.7.5 rcjeroendedauw19:44, 15 February 2011

Status & tagging log