Index: trunk/extensions/Maps/ParserFunctions/Geocode/Maps_Geocoder.php |
— | — | @@ -1,216 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | -/** |
5 | | - * File containing the MapsGeocoder class which handles the non specific geocoding tasks |
6 | | - * |
7 | | - * {{#geocode:<Address>|<param1>=<value1>|<param2>=<value2>}} |
8 | | - * {{#geocodelat:<Address>|<param1>=<value1>|<param2>=<value2>}} |
9 | | - * {{#geocodelng:<Address>|<param1>=<value1>|<param2>=<value2>}} |
10 | | - * |
11 | | - * @file Maps_Geocoder.php |
12 | | - * @ingroup Maps |
13 | | - * |
14 | | - * @author Jeroen De Dauw |
15 | | - * @author Sergey Chernyshev |
16 | | - */ |
17 | | - |
18 | | -if( !defined( 'MEDIAWIKI' ) ) { |
19 | | - die( 'Not an entry point.' ); |
20 | | -} |
21 | | - |
22 | | -$wgHooks['LanguageGetMagic'][] = 'efMapsGeoFunctionMagic'; |
23 | | -$wgHooks['ParserFirstCallInit'][] = 'efMapsRegisterGeoFunctions'; |
24 | | - |
25 | | -/** |
26 | | - * Adds the magic words for the parser functions |
27 | | - */ |
28 | | -function efMapsGeoFunctionMagic( &$magicWords, $langCode ) { |
29 | | - $magicWords['geocode'] = array( 0, 'geocode' ); |
30 | | - $magicWords['geocodelat'] = array ( 0, 'geocodelat' ); |
31 | | - $magicWords['geocodelng'] = array ( 0, 'geocodelng' ); |
32 | | - |
33 | | - return true; // Unless we return true, other parser functions won't get loaded |
34 | | -} |
35 | | - |
36 | | -/** |
37 | | - * Adds the parser function hooks |
38 | | - */ |
39 | | -function efMapsRegisterGeoFunctions(&$wgParser) { |
40 | | - // Hooks to enable the geocoding parser functions |
41 | | - $wgParser->setFunctionHook( 'geocode', array('MapsGeocoder', 'renderGeocoder') ); |
42 | | - $wgParser->setFunctionHook( 'geocodelat', array('MapsGeocoder', 'renderGeocoderLat') ); |
43 | | - $wgParser->setFunctionHook( 'geocodelng', array('MapsGeocoder', 'renderGeocoderLng') ); |
44 | | - |
45 | | - return true; |
46 | | -} |
47 | | - |
48 | | -final class MapsGeocoder { |
49 | | - |
50 | | - /** |
51 | | - * Holds if geocoded data should be cached or not. |
52 | | - * |
53 | | - * @var boolean |
54 | | - */ |
55 | | - private static $mEnableCache = true; |
56 | | - |
57 | | - /** |
58 | | - * The geocoder cache, holding geocoded data when enabled. |
59 | | - * |
60 | | - * @var array |
61 | | - */ |
62 | | - private static $mGeocoderCache = array(); |
63 | | - |
64 | | - /** |
65 | | - * Handler for the geocode parser function. Returns the latitude and longitude |
66 | | - * for the provided address, or an empty string, when the geocoding fails. |
67 | | - * |
68 | | - * @param unknown_type $parser |
69 | | - * @param string $address The address to geocode. |
70 | | - * @param string $service Optional. The geocoding service to use. |
71 | | - * @param string $mappingService Optional. The mapping service that will use the geocoded data. |
72 | | - * @return string |
73 | | - */ |
74 | | - public static function renderGeocoder($parser, $address, $service = '', $mappingService = '') { |
75 | | - $geovalues = MapsGeocoder::geocode($address, $service, $mappingService); |
76 | | - return $geovalues ? $geovalues['lat'].', '.$geovalues['lon'] : ''; |
77 | | - } |
78 | | - |
79 | | - /** |
80 | | - * Handler for the geocode parser function. Returns the latitude |
81 | | - * for the provided address, or an empty string, when the geocoding fails. |
82 | | - * |
83 | | - * @param unknown_type $parser |
84 | | - * @param string $address The address to geocode. |
85 | | - * @param string $service Optional. The geocoding service to use. |
86 | | - * @param string $mappingService Optional. The mapping service that will use the geocoded data. |
87 | | - * @return string |
88 | | - */ |
89 | | - public static function renderGeocoderLat(&$parser, $address, $service = '', $mappingService = '') { |
90 | | - $geovalues = MapsGeocoder::geocode($address, $service, $mappingService); |
91 | | - return $geovalues ? $geovalues['lat'] : ''; |
92 | | - } |
93 | | - |
94 | | - /** |
95 | | - * Handler for the geocode parser function. Returns the longitude |
96 | | - * for the provided address, or an empty string, when the geocoding fails. |
97 | | - * |
98 | | - * @param unknown_type $parser |
99 | | - * @param string $address The address to geocode. |
100 | | - * @param string $service Optional. The geocoding service to use. |
101 | | - * @param string $mappingService Optional. The mapping service that will use the geocoded data. |
102 | | - * @return string |
103 | | - */ |
104 | | - public static function renderGeocoderLng(&$parser, $address, $service = '', $mappingService = '') { |
105 | | - $geovalues = MapsGeocoder::geocode($address, $service, $mappingService); |
106 | | - return $geovalues ? $geovalues['lon'] : ''; |
107 | | - } |
108 | | - |
109 | | - /** |
110 | | - * Geocodes an address with the provided geocoding service and returns the result |
111 | | - * as a string with the optionally provided format, or false when the geocoding failed. |
112 | | - * |
113 | | - * @param string $address |
114 | | - * @param string $service |
115 | | - * @param string $mappingService |
116 | | - * @param string $format |
117 | | - * @return formatted coordinate string or false |
118 | | - */ |
119 | | - public static function geocodeToString($address, $service = '', $mappingService = '', $format = '%1$s, %2$s') { |
120 | | - $geovalues = MapsGeocoder::geocode($address, $service, $mappingService); |
121 | | - return $geovalues ? sprintf($format, $geovalues['lat'], $geovalues['lon']) : false; |
122 | | - } |
123 | | - |
124 | | - /** |
125 | | - * Geocodes an address with the provided geocoding service and returns the result |
126 | | - * as an array, or false when the geocoding failed. |
127 | | - * |
128 | | - * @param string $address |
129 | | - * @param string $service |
130 | | - * @param string $mappingService |
131 | | - * @return array with coordinates or false |
132 | | - */ |
133 | | - private static function geocode($address, $service, $mappingService) { |
134 | | - global $egMapsAvailableGeoServices, $egMapsDefaultGeoService; |
135 | | - |
136 | | - // If the adress is already in the cache and the cache is enabled, return the coordinates |
137 | | - if (self::$mEnableCache && array_key_exists($address, MapsGeocoder::$mGeocoderCache)) { |
138 | | - return self::$mGeocoderCache[$address]; |
139 | | - } |
140 | | - |
141 | | - $coordinates = false; |
142 | | - |
143 | | - $service = self::getValidGeoService($service, $mappingService); |
144 | | - |
145 | | - // If not, use the selected geocoding service to geocode the provided adress |
146 | | - switch(strtolower($service)) { |
147 | | - case 'google': |
148 | | - self::addAutoloadClassIfNeeded('MapsGoogleGeocoder', 'Maps_GoogleGeocoder.php'); |
149 | | - $coordinates = MapsGoogleGeocoder::geocode($address); |
150 | | - break; |
151 | | - case 'yahoo': |
152 | | - self::addAutoloadClassIfNeeded('MapsYahooGeocoder', 'Maps_YahooGeocoder.php'); |
153 | | - $coordinates = MapsYahooGeocoder::geocode($address); |
154 | | - break; |
155 | | - case 'geonames': |
156 | | - self::addAutoloadClassIfNeeded('MapsGeonamesGeocoder', 'Maps_GeonamesGeocoder.php'); |
157 | | - $coordinates = MapsGeonamesGeocoder::geocode($address); |
158 | | - break; |
159 | | - } |
160 | | - |
161 | | - // Add the obtained coordinates to the cache when there is a result and the cache is enabled |
162 | | - if (self::$mEnableCache && $coordinates) { |
163 | | - MapsGeocoder::$mGeocoderCache[$address] = $coordinates; |
164 | | - } |
165 | | - |
166 | | - return $coordinates; |
167 | | - } |
168 | | - |
169 | | - /** |
170 | | - * Add a geocoder class to the $wgAutoloadClasses array when it's not present yet. |
171 | | - * |
172 | | - * @param string $className |
173 | | - * @param string $fileName |
174 | | - */ |
175 | | - private static function addAutoloadClassIfNeeded($className, $fileName) { |
176 | | - global $wgAutoloadClasses, $egMapsIP; |
177 | | - if (!array_key_exists($className, $wgAutoloadClasses)) $wgAutoloadClasses[$className] = $egMapsIP . '/Geocoders/' . $fileName; |
178 | | - } |
179 | | - |
180 | | - /** |
181 | | - * Makes sure that the geo service is one of the available ones. |
182 | | - * Also enforces licencing restrictions when no geocoding service is explicitly provided. |
183 | | - * |
184 | | - * @param string $service |
185 | | - * @param string $mappingService |
186 | | - * @return string |
187 | | - */ |
188 | | - private static function getValidGeoService($service, $mappingService) { |
189 | | - global $egMapsAvailableGeoServices, $egMapsDefaultGeoService; |
190 | | - |
191 | | - if (strlen($service) < 1) { |
192 | | - |
193 | | - // Set the default geocoding services. |
194 | | - // Note that googlemaps and yahoomaps are excetions to the general rule due to licencing. |
195 | | - switch ($mappingService) { |
196 | | - case 'googlemaps' : |
197 | | - $service = 'google'; |
198 | | - break; |
199 | | - case 'yahoomaps' : |
200 | | - $service = 'yahoo'; |
201 | | - break; |
202 | | - default : |
203 | | - $service = $egMapsDefaultGeoService; |
204 | | - break; |
205 | | - } |
206 | | - |
207 | | - } |
208 | | - else { |
209 | | - if(!in_array($service, $egMapsAvailableGeoServices)) $service = $egMapsDefaultGeoService; |
210 | | - } |
211 | | - |
212 | | - return $service; |
213 | | - } |
214 | | -} |
215 | | - |
216 | | - |
217 | | - |
Index: trunk/extensions/Maps/ParserFunctions/Geocode/Maps_GeocodeFunctions.php |
— | — | @@ -0,0 +1,102 @@ |
| 2 | +<?php
|
| 3 | +
|
| 4 | +/**
|
| 5 | + * This file contains registration
|
| 6 | + *
|
| 7 | + * {{#geocode:<Address>|<param1>=<value1>|<param2>=<value2>}}
|
| 8 | + * {{#geocodelat:<Address>|<param1>=<value1>|<param2>=<value2>}}
|
| 9 | + * {{#geocodelng:<Address>|<param1>=<value1>|<param2>=<value2>}}
|
| 10 | + *
|
| 11 | + * @file Maps_GeocodeFunctions.php
|
| 12 | + * @ingroup Maps
|
| 13 | + *
|
| 14 | + * @author Jeroen De Dauw
|
| 15 | + * @author Sergey Chernyshev
|
| 16 | + */
|
| 17 | +
|
| 18 | +if( !defined( 'MEDIAWIKI' ) ) {
|
| 19 | + die( 'Not an entry point.' );
|
| 20 | +}
|
| 21 | +
|
| 22 | +$wgHooks['LanguageGetMagic'][] = 'efMapsGeoFunctionMagic';
|
| 23 | +$wgHooks['ParserFirstCallInit'][] = 'efMapsRegisterGeoFunctions';
|
| 24 | +
|
| 25 | +/**
|
| 26 | + * Adds the magic words for the parser functions
|
| 27 | + */
|
| 28 | +function efMapsGeoFunctionMagic( &$magicWords, $langCode ) {
|
| 29 | + $magicWords['geocode'] = array( 0, 'geocode' );
|
| 30 | + $magicWords['geocodelat'] = array ( 0, 'geocodelat' );
|
| 31 | + $magicWords['geocodelng'] = array ( 0, 'geocodelng' );
|
| 32 | +
|
| 33 | + return true; // Unless we return true, other parser functions won't get loaded
|
| 34 | +}
|
| 35 | +
|
| 36 | +/**
|
| 37 | + * Adds the parser function hooks
|
| 38 | + */
|
| 39 | +function efMapsRegisterGeoFunctions(&$wgParser) {
|
| 40 | + // Hooks to enable the geocoding parser functions
|
| 41 | + $wgParser->setFunctionHook( 'geocode', array('MapsGeocodeFunctions', 'renderGeocoder') );
|
| 42 | + $wgParser->setFunctionHook( 'geocodelat', array('MapsGeocodeFunctions', 'renderGeocoderLat') );
|
| 43 | + $wgParser->setFunctionHook( 'geocodelng', array('MapsGeocodeFunctions', 'renderGeocoderLng') );
|
| 44 | +
|
| 45 | + return true;
|
| 46 | +}
|
| 47 | +
|
| 48 | +final class MapsGeocodeFunctions {
|
| 49 | +
|
| 50 | + private static function geocoderIsAvailable() {
|
| 51 | + global $wgAutoloadClasses;
|
| 52 | + return array_key_exists('MapsGeocoder', $wgAutoloadClasses);
|
| 53 | + }
|
| 54 | +
|
| 55 | + /**
|
| 56 | + * Handler for the geocode parser function. Returns the latitude and longitude
|
| 57 | + * for the provided address, or an empty string, when the geocoding fails.
|
| 58 | + *
|
| 59 | + * @param unknown_type $parser
|
| 60 | + * @param string $address The address to geocode.
|
| 61 | + * @param string $service Optional. The geocoding service to use.
|
| 62 | + * @param string $mappingService Optional. The mapping service that will use the geocoded data.
|
| 63 | + * @return string
|
| 64 | + */
|
| 65 | + public static function renderGeocoder($parser, $address, $service = '', $mappingService = '') {
|
| 66 | + if (self::geocoderIsAvailable()) $geovalues = MapsGeocoder::geocode($address, $service, $mappingService);
|
| 67 | + return $geovalues ? $geovalues['lat'].', '.$geovalues['lon'] : '';
|
| 68 | + }
|
| 69 | +
|
| 70 | + /**
|
| 71 | + * Handler for the geocode parser function. Returns the latitude
|
| 72 | + * for the provided address, or an empty string, when the geocoding fails.
|
| 73 | + *
|
| 74 | + * @param unknown_type $parser
|
| 75 | + * @param string $address The address to geocode.
|
| 76 | + * @param string $service Optional. The geocoding service to use.
|
| 77 | + * @param string $mappingService Optional. The mapping service that will use the geocoded data.
|
| 78 | + * @return string
|
| 79 | + */
|
| 80 | + public static function renderGeocoderLat(&$parser, $address, $service = '', $mappingService = '') {
|
| 81 | + if (self::geocoderIsAvailable()) $geovalues = MapsGeocoder::geocode($address, $service, $mappingService);
|
| 82 | + return $geovalues ? $geovalues['lat'] : '';
|
| 83 | + }
|
| 84 | +
|
| 85 | + /**
|
| 86 | + * Handler for the geocode parser function. Returns the longitude
|
| 87 | + * for the provided address, or an empty string, when the geocoding fails.
|
| 88 | + *
|
| 89 | + * @param unknown_type $parser
|
| 90 | + * @param string $address The address to geocode.
|
| 91 | + * @param string $service Optional. The geocoding service to use.
|
| 92 | + * @param string $mappingService Optional. The mapping service that will use the geocoded data.
|
| 93 | + * @return string
|
| 94 | + */
|
| 95 | + public static function renderGeocoderLng(&$parser, $address, $service = '', $mappingService = '') {
|
| 96 | + if (self::geocoderIsAvailable()) $geovalues = MapsGeocoder::geocode($address, $service, $mappingService);
|
| 97 | + return $geovalues ? $geovalues['lon'] : '';
|
| 98 | + }
|
| 99 | +
|
| 100 | +}
|
| 101 | +
|
| 102 | +
|
| 103 | +
|
Index: trunk/extensions/Maps/ParserFunctions/Maps_ParserGeocoder.php |
— | — | @@ -84,4 +84,23 @@ |
85 | 85 | return $fails; |
86 | 86 | } |
87 | 87 | |
| 88 | + /** |
| 89 | + * |
| 90 | + * @return unknown_type |
| 91 | + */ |
| 92 | + public static function attemptToGeocode($coordsOrAddress) { |
| 93 | + // TODO: add check for DM and DD notations |
| 94 | + $floatRegex = "/^\d{1,3}(|\.\d{1,7}),(|\s)\d{1,3}(|\.\d{1,7})$/"; |
| 95 | + $dmsRegex = "/^(\d{1,2}�)(\d{2}')?((\d{2}\")?|(\d{2}\.\d{2}\")?)(N|S)(| )(\d{1,2}�)(\d{2}')?((\d{2}\")?|(\d{2}\.\d{2}\")?)(E|W)$/"; |
| 96 | + |
| 97 | + $needsGeocoding = !preg_match($floatRegex, $coordsOrAddress); |
| 98 | + if ($needsGeocoding) $needsGeocoding = !preg_match($dmsRegex, $coordsOrAddress); |
| 99 | + |
| 100 | + if ($needsGeocoding) { |
| 101 | + // TODO: geocode |
| 102 | + } |
| 103 | + |
| 104 | + return array('coords' => $coords, 'geocoded' => $needsGeocoding); |
| 105 | + } |
| 106 | + |
88 | 107 | } |
\ No newline at end of file |
Index: trunk/extensions/Maps/ParserFunctions/Maps_ParserFunctions.php |
— | — | @@ -47,19 +47,6 @@ |
48 | 48 | } |
49 | 49 | } |
50 | 50 | |
51 | | - /** |
52 | | - * |
53 | | - * @return unknown_type |
54 | | - */ |
55 | | - public static function attemptToGeocode($coordsOrAddress) { |
56 | | - |
57 | | - $needsGeocoding = true; |
58 | | - |
59 | | - if ($needsGeocoding) { |
60 | | - // TODO: geocode |
61 | | - } |
62 | | - |
63 | | - return array('coords' => $coords, 'geocoded' => $needsGeocoding); |
64 | | - } |
65 | 51 | |
| 52 | + |
66 | 53 | } |
\ No newline at end of file |
Index: trunk/extensions/Maps/Maps.php |
— | — | @@ -1,6 +1,6 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | | -/** |
| 4 | +/** |
5 | 5 | * Initialization file for the Maps extension. |
6 | 6 | * Extension documentation: http://www.mediawiki.org/wiki/Extension:Maps |
7 | 7 | * |
— | — | @@ -56,7 +56,7 @@ |
57 | 57 | $egMapsDefaultGeoService = in_array($egMapsDefaultGeoService, $egMapsAvailableGeoServices) ? $egMapsDefaultGeoService : end(array_reverse(array_keys($egMapsAvailableGeoServices))); |
58 | 58 | |
59 | 59 | // TODO: split for feature hook system? |
60 | | - wfLoadExtensionMessages( 'Maps' ); |
| 60 | + wfLoadExtensionMessages( 'Maps' ); |
61 | 61 | |
62 | 62 | // Creation of a list of internationalized service names. |
63 | 63 | $services = array(); |
Index: trunk/extensions/Maps/GoogleMaps/Maps_GoogleMapsUtils.php |
— | — | @@ -12,7 +12,7 @@ |
13 | 13 | if( !defined( 'MEDIAWIKI' ) ) { |
14 | 14 | die( 'Not an entry point.' ); |
15 | 15 | } |
16 | | - |
| 16 | + |
17 | 17 | final class MapsGoogleMapsUtils { |
18 | 18 | |
19 | 19 | const SERVICE_NAME = 'googlemaps'; |
— | — | @@ -119,10 +119,13 @@ |
120 | 120 | global $egGoogleMapsKey, $egMapsScriptPath, $egGoogleMapsOnThisPage; |
121 | 121 | |
122 | 122 | if (empty($egGoogleMapsOnThisPage)) { |
123 | | - // TODO: strbuilder for performance? |
124 | 123 | $egGoogleMapsOnThisPage = 0; |
125 | 124 | |
| 125 | + MapsGoogleMapsUtils::validateGoogleMapsKey(); |
| 126 | + |
126 | 127 | $wgOut->addScriptFile($egMapsScriptPath . '/GoogleMaps/GoogleMapFunctions.js'); |
| 128 | + |
| 129 | + // TODO: use strbuilder for performance gain? |
127 | 130 | $output .= "<script src='http://maps.google.com/maps?file=api&v=2&key=$egGoogleMapsKey&hl={$wgLang->getCode()}' type='$wgJsMimeType'></script> |
128 | 131 | <script type='$wgJsMimeType' src='$egMapsScriptPath/GoogleMaps/GoogleMapFunctions.js'></script>"; |
129 | 132 | } |
— | — | @@ -167,7 +170,7 @@ |
168 | 171 | * @param boolean $enableEarth |
169 | 172 | * @return string |
170 | 173 | */ |
171 | | - public function createTypesString(array &$types, $enableEarth = false) { |
| 174 | + public static function createTypesString(array &$types, $enableEarth = false) { |
172 | 175 | global $egMapsGoogleMapsTypes, $egMapsGoogleMapTypesValid; |
173 | 176 | |
174 | 177 | $types = MapsMapper::getValidTypes($types, $egMapsGoogleMapsTypes, $egMapsGoogleMapTypesValid, array(__CLASS__, 'getGMapType')); |
— | — | @@ -178,4 +181,16 @@ |
179 | 182 | return MapsMapper::createJSItemsString($types, null, false, false); |
180 | 183 | } |
181 | 184 | |
| 185 | + /** |
| 186 | + * This function ensures backward compatibility with Semantic Google Maps and other extensions |
| 187 | + * using $wgGoogleMapsKey instead of $egGoogleMapsKey. |
| 188 | + */ |
| 189 | + public static function validateGoogleMapsKey() { |
| 190 | + global $egGoogleMapsKey, $wgGoogleMapsKey; |
| 191 | + |
| 192 | + if (isset($wgGoogleMapsKey)){ |
| 193 | + if (strlen(trim($egGoogleMapsKey)) < 1) $egGoogleMapsKey = $wgGoogleMapsKey; |
| 194 | + } |
| 195 | + } |
| 196 | + |
182 | 197 | } |
\ No newline at end of file |
Index: trunk/extensions/Maps/Maps.i18n.php |
— | — | @@ -25,9 +25,11 @@ |
26 | 26 | 'maps_geocoding_failed_for' => 'The following {{PLURAL:$2|address|addresses}} could not be geocoded and {{PLURAL:$2|has|have}} been omitted from the map: |
27 | 27 | $1', |
28 | 28 | |
| 29 | + 'maps_map' => 'Map', |
| 30 | + |
29 | 31 | 'maps_googlemaps' => 'Google Maps', |
30 | 32 | 'maps_yahoomaps' => 'Yahoo! Maps', |
31 | | - 'maps_openlayers' => 'OpenLayers', |
| 33 | + 'maps_openlayers' => 'OpenLayers', |
32 | 34 | ); |
33 | 35 | |
34 | 36 | /** Message documentation (Message documentation) |
— | — | @@ -36,7 +38,7 @@ |
37 | 39 | */ |
38 | 40 | $messages['qqq'] = array( |
39 | 41 | 'maps_name' => '{{Optional}}', |
40 | | - 'maps_desc' => '{{desc}} |
| 42 | + 'maps_desc' => '{{desc}} |
41 | 43 | |
42 | 44 | * $1: a list of available map services', |
43 | 45 | 'maps_geocoding_failed_for' => '* $1 is a list |
Index: trunk/extensions/Maps/Maps_Settings.php |
— | — | @@ -16,7 +16,7 @@ |
17 | 17 | |
18 | 18 | if( !defined( 'MEDIAWIKI' ) ) { |
19 | 19 | die( 'Not an entry point.' ); |
20 | | -} |
| 20 | +} |
21 | 21 | |
22 | 22 | |
23 | 23 | |
— | — | @@ -28,24 +28,34 @@ |
29 | 29 | # and in the service data arrays, the value is the human readible version for displaying purpouses. |
30 | 30 | if (empty($egMapsAvailableFeatures)) $egMapsAvailableFeatures = array(); |
31 | 31 | |
| 32 | +$egMapsAvailableFeatures['geocode'] = array( |
| 33 | + 'name' => 'Geocoding', |
| 34 | + 'class' => 'Geocoders', |
| 35 | + 'file' => 'Geocoders/Maps_Geocoders.php', |
| 36 | + 'local' => true, |
| 37 | + ); |
| 38 | + |
32 | 39 | $egMapsAvailableFeatures['pf'] = array( |
33 | | - 'name' => 'Parser Function', |
| 40 | + 'name' => 'Parser Functions', |
34 | 41 | 'class' => 'MapsParserFunctions', |
35 | 42 | 'file' => 'ParserFunctions/Maps_ParserFunctions.php', |
36 | 43 | 'local' => true, |
37 | 44 | ); |
38 | | - |
39 | | -$egMapsAvailableFeatures['geocode'] = array( |
40 | | - 'name' => 'Parser Function', |
41 | | - 'class' => 'MapsParserFunctions', |
42 | | - 'file' => 'ParserFunctions/Maps_ParserFunctions.php', |
43 | | - 'local' => true, |
44 | | - ); |
45 | 45 | |
46 | 46 | |
47 | 47 | |
48 | 48 | |
49 | 49 | |
| 50 | +# Include the parser functions that should be loaded into Maps. |
| 51 | +# Commenting or removing a parser functions will cause Maps to completely ignore it, and so improve performance. |
| 52 | +include_once $egMapsIP . '/ParserFunctions/DisplayMap/Maps_DisplayMap.php'; // display_map |
| 53 | +include_once $egMapsIP . '/ParserFunctions/DisplayPoint/Maps_DisplayPoint.php'; // display_point(s) |
| 54 | +include_once $egMapsIP . '/ParserFunctions/Geocode/Maps_GeocodeFunctions.php'; // geocode, geocodelon, geocodelat |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
50 | 60 | # Map services configuration |
51 | 61 | # Note: You can not use aliases in the settings. Use the main service names. |
52 | 62 | |
— | — | @@ -77,7 +87,25 @@ |
78 | 88 | |
79 | 89 | # Array of String. Array containing all the geocoding services that will be made available to the user. |
80 | 90 | # Currently Maps provides the following services: googlemaps, yahoomaps, openlayers |
81 | | -$egMapsAvailableGeoServices = array('google', 'yahoo', 'geonames'); |
| 91 | +$egMapsAvailableGeoServices = array( |
| 92 | + 'google' => array( |
| 93 | + 'class' => 'MapsGoogleGeocoder', |
| 94 | + 'file' => 'Geocoders/Maps_GoogleGeocoder.php', |
| 95 | + 'local' => true, |
| 96 | + 'overrides' => array('googlemaps'), |
| 97 | + ), |
| 98 | + 'yahoo' => array( |
| 99 | + 'class' => 'MapsYahooGeocoder', |
| 100 | + 'file' => 'Geocoders/Maps_YahooGeocoder.php', |
| 101 | + 'local' => true, |
| 102 | + 'overrides' => array('yahoomaps'), |
| 103 | + ), |
| 104 | + 'geonames' => array( |
| 105 | + 'class' => 'MapsGeonamesGeocoder', |
| 106 | + 'file' => 'Geocoders/Maps_GeonamesGeocoder.php', |
| 107 | + 'local' => true, |
| 108 | + ), |
| 109 | + ); |
82 | 110 | |
83 | 111 | # String. The default geocoding service, which will be used when no service is provided by the user. |
84 | 112 | # This service needs to be enabled, if not, the first one from the available services will be taken. |
Index: trunk/extensions/Maps/Maps_Utils.php |
— | — | @@ -1,6 +1,6 @@ |
2 | | -<?php |
| 2 | +<?php |
3 | 3 | |
4 | | -/** |
| 4 | +/** |
5 | 5 | * A class that holds static helper functions for common functionality that is map-spesific. |
6 | 6 | * Non spesific functions are located in @see MapsParserFunctions |
7 | 7 | * |
— | — | @@ -102,4 +102,26 @@ |
103 | 103 | $value .= 'px'; |
104 | 104 | } |
105 | 105 | |
| 106 | + /** |
| 107 | + * Returns if the current php version is equal of bigger then the provided one. |
| 108 | + * |
| 109 | + * @param string $requiredVersion |
| 110 | + * @return boolean |
| 111 | + */ |
| 112 | + public static function phpVersionIsEqualOrBigger($requiredVersion) { |
| 113 | + // TODO: Ensure this works, and does not cause errors for some versions. |
| 114 | + $currentVersion = phpversion(); |
| 115 | + |
| 116 | + for($i = 0; $i < 3; $i++) { |
| 117 | + if ($currentVersion[$i] < $requiredVersion[$i]) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + else if($currentVersion[$i] > $requiredVersion[$i]) { |
| 121 | + return true; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return true; |
| 126 | + } |
| 127 | + |
106 | 128 | } |
Index: trunk/extensions/Maps/Geocoders/Maps_Geocoders.php |
— | — | @@ -0,0 +1,36 @@ |
| 2 | +<?php
|
| 3 | +
|
| 4 | +/**
|
| 5 | + * Initialization file for geocoder functionality in the Maps extension
|
| 6 | + *
|
| 7 | + * @file Maps_Geocoders.php
|
| 8 | + * @ingroup Maps
|
| 9 | + *
|
| 10 | + * @author Jeroen De Dauw
|
| 11 | + */
|
| 12 | +
|
| 13 | +if( !defined( 'MEDIAWIKI' ) ) {
|
| 14 | + die( 'Not an entry point.' );
|
| 15 | +}
|
| 16 | +
|
| 17 | +/**
|
| 18 | + * Initialization class for geocoder functionality in the Maps extension.
|
| 19 | + *
|
| 20 | + * @author Jeroen De Dauw
|
| 21 | + *
|
| 22 | + */
|
| 23 | +final class Geocoders {
|
| 24 | +
|
| 25 | + /**
|
| 26 | + * Initialization function for Maps geocoder functionality.
|
| 27 | + */
|
| 28 | + public static function initialize() {
|
| 29 | + global $wgAutoloadClasses, $egMapsIP, $egMapsGeoServices;
|
| 30 | +
|
| 31 | + $egMapsGeoServices = array();
|
| 32 | +
|
| 33 | + $wgAutoloadClasses['MapsBaseGeocoder'] = $egMapsIP . '/Geocoders/Maps_BaseGeocoder.php';
|
| 34 | + $wgAutoloadClasses['MapsGeocoder'] = $egMapsIP . '/Geocoders/Maps_Geocoder.php';
|
| 35 | + }
|
| 36 | +
|
| 37 | +} |
\ No newline at end of file |
Index: trunk/extensions/Maps/Geocoders/Maps_Geocoder.php |
— | — | @@ -0,0 +1,119 @@ |
| 2 | +<?php
|
| 3 | +
|
| 4 | +/**
|
| 5 | + * Geocoding class. Provides methods to geocode a string to a pair of coordinates
|
| 6 | + * using one of the available geocoding services.
|
| 7 | + *
|
| 8 | + * @file Maps_Geocoder.php
|
| 9 | + * @ingroup Maps
|
| 10 | + *
|
| 11 | + * @author Jeroen De Dauw
|
| 12 | + * @author Sergey Chernyshev
|
| 13 | + */
|
| 14 | +
|
| 15 | +if( !defined( 'MEDIAWIKI' ) ) {
|
| 16 | + die( 'Not an entry point.' );
|
| 17 | +}
|
| 18 | +
|
| 19 | +final class MapsGeocoder {
|
| 20 | +
|
| 21 | + /**
|
| 22 | + * Holds if geocoded data should be cached or not.
|
| 23 | + *
|
| 24 | + * @var boolean
|
| 25 | + */
|
| 26 | + private static $mEnableCache = true;
|
| 27 | +
|
| 28 | + /**
|
| 29 | + * The geocoder cache, holding geocoded data when enabled.
|
| 30 | + *
|
| 31 | + * @var array
|
| 32 | + */
|
| 33 | + private static $mGeocoderCache = array();
|
| 34 | +
|
| 35 | + /**
|
| 36 | + * Geocodes an address with the provided geocoding service and returns the result
|
| 37 | + * as a string with the optionally provided format, or false when the geocoding failed.
|
| 38 | + *
|
| 39 | + * @param string $address
|
| 40 | + * @param string $service
|
| 41 | + * @param string $mappingService
|
| 42 | + * @param string $format
|
| 43 | + * @return formatted coordinate string or false
|
| 44 | + */
|
| 45 | + public static function geocodeToString($address, $service = '', $mappingService = '', $format = '%1$s, %2$s') {
|
| 46 | + $geovalues = MapsGeocoder::geocode($address, $service, $mappingService);
|
| 47 | + return $geovalues ? sprintf($format, $geovalues['lat'], $geovalues['lon']) : false;
|
| 48 | + }
|
| 49 | +
|
| 50 | + /**
|
| 51 | + * Geocodes an address with the provided geocoding service and returns the result
|
| 52 | + * as an array, or false when the geocoding failed.
|
| 53 | + *
|
| 54 | + * @param string $address
|
| 55 | + * @param string $service
|
| 56 | + * @param string $mappingService
|
| 57 | + * @return array with coordinates or false
|
| 58 | + */
|
| 59 | + public static function geocode($address, $service, $mappingService) {
|
| 60 | + global $egMapsAvailableGeoServices, $wgAutoloadClasses, $egMapsIP, $IP;
|
| 61 | +
|
| 62 | + // If the adress is already in the cache and the cache is enabled, return the coordinates.
|
| 63 | + if (self::$mEnableCache && array_key_exists($address, MapsGeocoder::$mGeocoderCache)) {
|
| 64 | + return self::$mGeocoderCache[$address];
|
| 65 | + }
|
| 66 | +
|
| 67 | + $coordinates = false;
|
| 68 | +
|
| 69 | + $service = self::getValidGeoService($service, $mappingService);
|
| 70 | +
|
| 71 | + // Make sure the needed class is loaded.
|
| 72 | + $file = $egMapsAvailableGeoServices[$service]['local'] ? $egMapsIP . '/' . $egMapsAvailableGeoServices[$service]['file'] : $IP . '/extensions/' . $egMapsAvailableGeoServices[$service]['file'];
|
| 73 | + $wgAutoloadClasses[$egMapsAvailableGeoServices[$service]['class']] = $file;
|
| 74 | +
|
| 75 | + // Call the geocode function in the spesific geocoder class.
|
| 76 | + $phpAtLeast523 = MapsUtils::phpVersionIsEqualOrBigger('5.2.3');
|
| 77 | + $coordinates = call_user_func(array($egMapsAvailableGeoServices[$service]['class'], 'geocode'), $phpAtLeast523 ? $address : array($address));
|
| 78 | +
|
| 79 | + // Add the obtained coordinates to the cache when there is a result and the cache is enabled.
|
| 80 | + if (self::$mEnableCache && $coordinates) {
|
| 81 | + MapsGeocoder::$mGeocoderCache[$address] = $coordinates;
|
| 82 | + }
|
| 83 | +
|
| 84 | + return $coordinates;
|
| 85 | + }
|
| 86 | +
|
| 87 | + /**
|
| 88 | + * Makes sure that the geo service is one of the available ones.
|
| 89 | + * Also enforces licencing restrictions when no geocoding service is explicitly provided.
|
| 90 | + *
|
| 91 | + * @param string $service
|
| 92 | + * @param string $mappingService
|
| 93 | + * @return string
|
| 94 | + */
|
| 95 | + private static function getValidGeoService($service, $mappingService) {
|
| 96 | + global $egMapsAvailableGeoServices, $egMapsDefaultGeoService;
|
| 97 | +
|
| 98 | + if (strlen($service) < 1) {
|
| 99 | + // If no service has been provided, check if there are overrides for the default.
|
| 100 | + foreach ($egMapsAvailableGeoServices as $geoService => $serviceData) {
|
| 101 | + if (in_array($mappingService, $serviceData)) {
|
| 102 | + $service = $geoService; // Use the override
|
| 103 | + continue;
|
| 104 | + }
|
| 105 | + }
|
| 106 | +
|
| 107 | + // If no overrides where applied, use the default mapping service.
|
| 108 | + if (strlen($service) < 1) $service = $egMapsDefaultGeoService;
|
| 109 | + }
|
| 110 | + else {
|
| 111 | + // If a service is provided, but is not supported, use the default.
|
| 112 | + if(!array_key_exists($service, $egMapsAvailableGeoServices)) $service = $egMapsDefaultGeoService;
|
| 113 | + }
|
| 114 | +
|
| 115 | + return $service;
|
| 116 | + }
|
| 117 | +}
|
| 118 | +
|
| 119 | +
|
| 120 | +
|