Index: trunk/extensions/Maps/Maps_BaseGeocoder.php |
— | — | @@ -1,118 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | -/** |
5 | | - * MapsBaseGeocoder is an abstract class inherited by the geocoding classes |
6 | | - * |
7 | | - * @file Maps_BaseGeocoder.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 | | -abstract class MapsBaseGeocoder { |
18 | | - |
19 | | - /** |
20 | | - * Returns an array containing the geocoded latitude (lat) and |
21 | | - * longitude (lon) of the provided address, or false in case the |
22 | | - * geocoding fails. |
23 | | - * |
24 | | - * @param string $address |
25 | | - */ |
26 | | - public abstract static function geocode($address); |
27 | | - |
28 | | - /** |
29 | | - * Returns the content of the requested file, or false when the connection fails |
30 | | - * |
31 | | - * @param string $requestURL |
32 | | - * @return string or false |
33 | | - */ |
34 | | - protected static function GetResponse($requestURL) { |
35 | | - // Attempt to get CURL response |
36 | | - $response = self::GetCurlResponse($requestURL); |
37 | | - |
38 | | - // Attempt to get response using fopen when the CURL request failed |
39 | | - if (!$response) $response = self::GetUrlResponse($requestURL); |
40 | | - |
41 | | - return $response; |
42 | | - } |
43 | | - |
44 | | - /** |
45 | | - * Attempts to get the contents of a file via cURL request and |
46 | | - * returns it, or false when the attempt fails. |
47 | | - * |
48 | | - * @param string $requestURL |
49 | | - * @return string or false |
50 | | - */ |
51 | | - protected static function GetCurlResponse($requestURL) { |
52 | | - if (function_exists("curl_init")) { |
53 | | - try { |
54 | | - //Set up a CURL request, telling it not to spit back headers, and to throw out a user agent. |
55 | | - $ch = curl_init(); |
56 | | - |
57 | | - curl_setopt($ch, CURLOPT_URL, $requestURL); |
58 | | - curl_setopt($ch, CURLOPT_HEADER, 0); //Change this to a 1 to return headers |
59 | | - curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); |
60 | | - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); |
61 | | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
62 | | - |
63 | | - $result = curl_exec($ch); |
64 | | - curl_close($ch); |
65 | | - |
66 | | - return $result; |
67 | | - } |
68 | | - catch(Exception $ex) { |
69 | | - return false; |
70 | | - } |
71 | | - } |
72 | | - else { |
73 | | - return false; |
74 | | - } |
75 | | - } |
76 | | - |
77 | | - /** |
78 | | - * Attempts to get the contents of a file via fopen and |
79 | | - * returns it, or false when the attempt fails. |
80 | | - * |
81 | | - * @param string $requestURL |
82 | | - * @return string or false |
83 | | - */ |
84 | | - protected static function GetUrlResponse($requestURL) { |
85 | | - if (function_exists('fopen')) { |
86 | | - try { |
87 | | - if ($handle = fopen($requestURL, 'r')) { |
88 | | - $result = fread($handle, 10000); |
89 | | - fclose($handle); |
90 | | - } |
91 | | - else { // When the request fails, return false |
92 | | - $result = false; |
93 | | - } |
94 | | - } |
95 | | - catch(Exception $ex) { |
96 | | - $result = false; |
97 | | - } |
98 | | - } |
99 | | - else { |
100 | | - $result = false; |
101 | | - } |
102 | | - return $result; |
103 | | - } |
104 | | - |
105 | | - /** |
106 | | - * Gets the contents of the first XML tag with the provided name, |
107 | | - * returns false when no matching element is found. |
108 | | - * |
109 | | - * @param string $xml |
110 | | - * @param string $tagName |
111 | | - * @return string or false |
112 | | - */ |
113 | | - protected static function getXmlElementValue($xml, $tagName) { |
114 | | - $match = array(); |
115 | | - preg_match("/<$tagName>(.*?)<\/$tagName>/", $xml, $match); |
116 | | - return count($match) > 1 ? $match[1] : false; |
117 | | - } |
118 | | - |
119 | | -} |
Index: trunk/extensions/Maps/Maps_ParserFunctions.php |
— | — | @@ -1,177 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | -/** |
5 | | - * A class that holds handlers for the mapping parser functions. |
6 | | - * Spesific functions are located in @see MapsUtils |
7 | | - * |
8 | | - * @file Maps_ParserFunctions.php |
9 | | - * @ingroup Maps |
10 | | - * |
11 | | - * @author Jeroen De Dauw |
12 | | - */ |
13 | | - |
14 | | -if( !defined( 'MEDIAWIKI' ) ) { |
15 | | - die( 'Not an entry point.' ); |
16 | | -} |
17 | | - |
18 | | -final class MapsParserFunctions { |
19 | | - |
20 | | - private static function getMapHtml(&$parser, array $params, array $coordFails = array()) { |
21 | | - global $egMapsServices; |
22 | | - |
23 | | - $map = array(); |
24 | | - |
25 | | - // Go through all parameters, split their names and values, and put them in the $map array. |
26 | | - foreach($params as $param) { |
27 | | - $split = split('=', $param); |
28 | | - if (count($split) == 2) { |
29 | | - $paramName = strtolower(trim($split[0])); |
30 | | - $paramValue = trim($split[1]); |
31 | | - $map[$paramName] = $paramValue; |
32 | | - } |
33 | | - else if (count($split) == 1) { // Default parameter (without name) |
34 | | - $map['coordinates'] = trim($split[0]); |
35 | | - } |
36 | | - } |
37 | | - |
38 | | - $coords = MapsMapper::getParamValue('coordinates', $map); |
39 | | - |
40 | | - if ($coords) { |
41 | | - if (! MapsMapper::paramIsPresent('service', $map)) $map['service'] = ''; |
42 | | - $map['service'] = MapsMapper::getValidService($map['service'], 'pf'); |
43 | | - |
44 | | - $mapClass = new $egMapsServices[$map['service']]['pf']['class'](); |
45 | | - |
46 | | - // Call the function according to the map service to get the HTML output |
47 | | - $output = $mapClass->displayMap($parser, $map); |
48 | | - |
49 | | - if (count($coordFails) > 0) { |
50 | | - $output .= '<i>' . wfMsgExt( 'maps_geocoding_failed_for', array( 'parsemag' ), implode( ',', $coordFails ), count( $coordFails ) ) . '</i>'; |
51 | | - } |
52 | | - } |
53 | | - elseif (trim($coords) == "" && count($coordFails) > 0) { |
54 | | - $output = '<i>' . wfMsgExt( 'maps_geocoding_failed', array( 'parsemag' ), implode(',', $coordFails), count( $coordFails ) ) . '</i>'; |
55 | | - } |
56 | | - else { |
57 | | - $output = '<i>'.wfMsg( 'maps_coordinates_missing' ).'</i>'; |
58 | | - } |
59 | | - |
60 | | - // Return the result |
61 | | - return array( $output, 'noparse' => true, 'isHTML' => true ); |
62 | | - } |
63 | | - |
64 | | - /** |
65 | | - * Sets the default map properties, gets the map HTML depending |
66 | | - * on the provided service, and then returns it. |
67 | | - * |
68 | | - * @param unknown_type $parser |
69 | | - * @return array |
70 | | - */ |
71 | | - public static function displayPointRender(&$parser) { |
72 | | - $params = func_get_args(); |
73 | | - array_shift( $params ); // We already know the $parser ... |
74 | | - |
75 | | - return self::getMapHtml($parser, $params); |
76 | | - } |
77 | | - |
78 | | - /** |
79 | | - * Sets the default map properties, gets the map HTML depending |
80 | | - * on the provided service, and then returns it. |
81 | | - * |
82 | | - * @param unknown_type $parser |
83 | | - */ |
84 | | - public static function displayPointsRender(&$parser) { |
85 | | - $params = func_get_args(); |
86 | | - array_shift( $params ); // We already know the $parser ... |
87 | | - |
88 | | - return self::getMapHtml($parser, $params); |
89 | | - } |
90 | | - |
91 | | - /** |
92 | | - * Turns the address parameter into coordinates, then calls |
93 | | - * getMapHtml() and returns it's result. |
94 | | - * |
95 | | - * @param unknown_type $parser |
96 | | - * @return array |
97 | | - */ |
98 | | - public static function displayAddressRender(&$parser) { |
99 | | - $params = func_get_args(); |
100 | | - array_shift( $params ); // We already know the $parser ... |
101 | | - |
102 | | - $fails = self::changeAddressToCoords($params); |
103 | | - |
104 | | - return self::getMapHtml($parser, $params, $fails); |
105 | | - } |
106 | | - |
107 | | - /** |
108 | | - * Turns the address parameter into coordinates, then calls |
109 | | - * getMapHtml() and returns it's result. |
110 | | - * |
111 | | - * @param unknown_type $parser |
112 | | - */ |
113 | | - public static function displayAddressesRender(&$parser) { |
114 | | - $params = func_get_args(); |
115 | | - array_shift( $params ); // We already know the $parser ... |
116 | | - |
117 | | - $fails = self::changeAddressToCoords($params); |
118 | | - |
119 | | - return self::getMapHtml($parser, $params, $fails); |
120 | | - } |
121 | | - |
122 | | - /** |
123 | | - * Changes the values of the address or addresses parameter into coordinates |
124 | | - * in the provided array. Returns an array containing the addresses that |
125 | | - * could not be geocoded. |
126 | | - * |
127 | | - * @param array $params |
128 | | - */ |
129 | | - private static function changeAddressToCoords(&$params) { |
130 | | - global $egMapsDefaultService; |
131 | | - |
132 | | - $fails = array(); |
133 | | - |
134 | | - for ($i = 0; $i < count($params); $i++) { |
135 | | - $split = split('=', $params[$i]); |
136 | | - if (MapsMapper::inParamAliases(strtolower(trim($split[0])), 'service') && count($split) > 1) { |
137 | | - $service = trim($split[1]); |
138 | | - } |
139 | | - else if (strtolower(trim($split[0])) == 'geoservice' && count($split) > 1) { |
140 | | - $geoservice = trim($split[1]); |
141 | | - } |
142 | | - } |
143 | | - |
144 | | - $service = isset($service) ? MapsMapper::getValidService($service, 'pf') : $egMapsDefaultService; |
145 | | - |
146 | | - $geoservice = isset($geoservice) ? $geoservice : ''; |
147 | | - |
148 | | - for ($i = 0; $i < count($params); $i++) { |
149 | | - $split = split('=', $params[$i]); |
150 | | - if (((strtolower(trim($split[0])) == 'address' || strtolower(trim($split[0])) == 'addresses') && count($split) > 1) || count($split) == 1) { |
151 | | - $address_srting = count($split) == 1 ? $split[0] : $split[1]; |
152 | | - |
153 | | - $addresses = explode(';', $address_srting); |
154 | | - |
155 | | - $coordinates = array(); |
156 | | - |
157 | | - foreach($addresses as $address) { |
158 | | - $args = explode('~', $address); |
159 | | - $coords = MapsGeocoder::geocodeToString(trim($args[0]), $geoservice, $service); |
160 | | - |
161 | | - if ($coords) { |
162 | | - $args[0] = $coords; |
163 | | - $coordinates[] = implode('~', $args); |
164 | | - } |
165 | | - else { |
166 | | - $fails[] = $args[0]; |
167 | | - } |
168 | | - } |
169 | | - |
170 | | - $params[$i] = 'coordinates=' . implode(';', $coordinates); |
171 | | - |
172 | | - } |
173 | | - } |
174 | | - |
175 | | - return $fails; |
176 | | - } |
177 | | - |
178 | | -} |
\ No newline at end of file |
Index: trunk/extensions/Maps/Maps_Geocoder.php |
— | — | @@ -1,190 +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 | | -final class MapsGeocoder { |
23 | | - |
24 | | - /** |
25 | | - * Holds if geocoded data should be cached or not. |
26 | | - * |
27 | | - * @var boolean |
28 | | - */ |
29 | | - private static $mEnableCache = true; |
30 | | - |
31 | | - /** |
32 | | - * The geocoder cache, holding geocoded data when enabled. |
33 | | - * |
34 | | - * @var array |
35 | | - */ |
36 | | - private static $mGeocoderCache = array(); |
37 | | - |
38 | | - /** |
39 | | - * Handler for the geocode parser function. Returns the latitude and longitude |
40 | | - * for the provided address, or an empty string, when the geocoding fails. |
41 | | - * |
42 | | - * @param unknown_type $parser |
43 | | - * @param string $address The address to geocode. |
44 | | - * @param string $service Optional. The geocoding service to use. |
45 | | - * @param string $mappingService Optional. The mapping service that will use the geocoded data. |
46 | | - * @return string |
47 | | - */ |
48 | | - public static function renderGeocoder($parser, $address, $service = '', $mappingService = '') { |
49 | | - $geovalues = MapsGeocoder::geocode($address, $service, $mappingService); |
50 | | - return $geovalues ? $geovalues['lat'].', '.$geovalues['lon'] : ''; |
51 | | - } |
52 | | - |
53 | | - /** |
54 | | - * Handler for the geocode parser function. Returns the latitude |
55 | | - * for the provided address, or an empty string, when the geocoding fails. |
56 | | - * |
57 | | - * @param unknown_type $parser |
58 | | - * @param string $address The address to geocode. |
59 | | - * @param string $service Optional. The geocoding service to use. |
60 | | - * @param string $mappingService Optional. The mapping service that will use the geocoded data. |
61 | | - * @return string |
62 | | - */ |
63 | | - public static function renderGeocoderLat(&$parser, $address, $service = '', $mappingService = '') { |
64 | | - $geovalues = MapsGeocoder::geocode($address, $service, $mappingService); |
65 | | - return $geovalues ? $geovalues['lat'] : ''; |
66 | | - } |
67 | | - |
68 | | - /** |
69 | | - * Handler for the geocode parser function. Returns the longitude |
70 | | - * for the provided address, or an empty string, when the geocoding fails. |
71 | | - * |
72 | | - * @param unknown_type $parser |
73 | | - * @param string $address The address to geocode. |
74 | | - * @param string $service Optional. The geocoding service to use. |
75 | | - * @param string $mappingService Optional. The mapping service that will use the geocoded data. |
76 | | - * @return string |
77 | | - */ |
78 | | - public static function renderGeocoderLng(&$parser, $address, $service = '', $mappingService = '') { |
79 | | - $geovalues = MapsGeocoder::geocode($address, $service, $mappingService); |
80 | | - return $geovalues ? $geovalues['lon'] : ''; |
81 | | - } |
82 | | - |
83 | | - /** |
84 | | - * Geocodes an address with the provided geocoding service and returns the result |
85 | | - * as a string with the optionally provided format, or false when the geocoding failed. |
86 | | - * |
87 | | - * @param string $address |
88 | | - * @param string $service |
89 | | - * @param string $mappingService |
90 | | - * @param string $format |
91 | | - * @return formatted coordinate string or false |
92 | | - */ |
93 | | - public static function geocodeToString($address, $service = '', $mappingService = '', $format = '%1$s, %2$s') { |
94 | | - $geovalues = MapsGeocoder::geocode($address, $service, $mappingService); |
95 | | - return $geovalues ? sprintf($format, $geovalues['lat'], $geovalues['lon']) : false; |
96 | | - } |
97 | | - |
98 | | - /** |
99 | | - * Geocodes an address with the provided geocoding service and returns the result |
100 | | - * as an array, or false when the geocoding failed. |
101 | | - * |
102 | | - * @param string $address |
103 | | - * @param string $service |
104 | | - * @param string $mappingService |
105 | | - * @return array with coordinates or false |
106 | | - */ |
107 | | - private static function geocode($address, $service, $mappingService) { |
108 | | - global $egMapsAvailableGeoServices, $egMapsDefaultGeoService; |
109 | | - |
110 | | - // If the adress is already in the cache and the cache is enabled, return the coordinates |
111 | | - if (self::$mEnableCache && array_key_exists($address, MapsGeocoder::$mGeocoderCache)) { |
112 | | - return self::$mGeocoderCache[$address]; |
113 | | - } |
114 | | - |
115 | | - $coordinates = false; |
116 | | - |
117 | | - $service = self::getValidGeoService($service, $mappingService); |
118 | | - |
119 | | - // If not, use the selected geocoding service to geocode the provided adress |
120 | | - switch(strtolower($service)) { |
121 | | - case 'google': |
122 | | - self::addAutoloadClassIfNeeded('MapsGoogleGeocoder', 'Maps_GoogleGeocoder.php'); |
123 | | - $coordinates = MapsGoogleGeocoder::geocode($address); |
124 | | - break; |
125 | | - case 'yahoo': |
126 | | - self::addAutoloadClassIfNeeded('MapsYahooGeocoder', 'Maps_YahooGeocoder.php'); |
127 | | - $coordinates = MapsYahooGeocoder::geocode($address); |
128 | | - break; |
129 | | - case 'geonames': |
130 | | - self::addAutoloadClassIfNeeded('MapsGeonamesGeocoder', 'Maps_GeonamesGeocoder.php'); |
131 | | - $coordinates = MapsGeonamesGeocoder::geocode($address); |
132 | | - break; |
133 | | - } |
134 | | - |
135 | | - // Add the obtained coordinates to the cache when there is a result and the cache is enabled |
136 | | - if (self::$mEnableCache && $coordinates) { |
137 | | - MapsGeocoder::$mGeocoderCache[$address] = $coordinates; |
138 | | - } |
139 | | - |
140 | | - return $coordinates; |
141 | | - } |
142 | | - |
143 | | - /** |
144 | | - * Add a geocoder class to the $wgAutoloadClasses array when it's not present yet. |
145 | | - * |
146 | | - * @param string $className |
147 | | - * @param string $fileName |
148 | | - */ |
149 | | - private static function addAutoloadClassIfNeeded($className, $fileName) { |
150 | | - global $wgAutoloadClasses, $egMapsIP; |
151 | | - if (!array_key_exists($className, $wgAutoloadClasses)) $wgAutoloadClasses[$className] = $egMapsIP . '/Geocoders/' . $fileName; |
152 | | - } |
153 | | - |
154 | | - /** |
155 | | - * Makes sure that the geo service is one of the available ones. |
156 | | - * Also enforces licencing restrictions when no geocoding service is explicitly provided. |
157 | | - * |
158 | | - * @param string $service |
159 | | - * @param string $mappingService |
160 | | - * @return string |
161 | | - */ |
162 | | - private static function getValidGeoService($service, $mappingService) { |
163 | | - global $egMapsAvailableGeoServices, $egMapsDefaultGeoService; |
164 | | - |
165 | | - if (strlen($service) < 1) { |
166 | | - |
167 | | - // Set the default geocoding services. |
168 | | - // Note that googlemaps and yahoomaps are excetions to the general rule due to licencing. |
169 | | - switch ($mappingService) { |
170 | | - case 'googlemaps' : |
171 | | - $service = 'google'; |
172 | | - break; |
173 | | - case 'yahoomaps' : |
174 | | - $service = 'yahoo'; |
175 | | - break; |
176 | | - default : |
177 | | - $service = $egMapsDefaultGeoService; |
178 | | - break; |
179 | | - } |
180 | | - |
181 | | - } |
182 | | - else { |
183 | | - if(!in_array($service, $egMapsAvailableGeoServices)) $service = $egMapsDefaultGeoService; |
184 | | - } |
185 | | - |
186 | | - return $service; |
187 | | - } |
188 | | -} |
189 | | - |
190 | | - |
191 | | - |
Index: trunk/extensions/Maps/Maps_BaseMap.php |
— | — | @@ -1,155 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | -/** |
5 | | - * MapsBaseMap is an abstract class inherited by the map services classes |
6 | | - * |
7 | | - * @file Maps_BaseMap.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 | | -abstract class MapsBaseMap extends MapsMapFeature { |
18 | | - |
19 | | - // TODO: move this abstract function to a new MapsBaseMapUtils file? |
20 | | - //protected abstract static function getDefaultParams(); |
21 | | - |
22 | | - protected $markerData = array(); |
23 | | - |
24 | | - /** |
25 | | - * Handles the request from the parser hook by doing the work that's common for all |
26 | | - * mapping services, calling the specific methods and finally returning the resulting output. |
27 | | - * |
28 | | - * @param unknown_type $parser |
29 | | - * @param array $map |
30 | | - * @return html |
31 | | - */ |
32 | | - public final function displayMap(&$parser, array $map) { |
33 | | - $this->setMapSettings(); |
34 | | - |
35 | | - $this->doMapServiceLoad(); |
36 | | - |
37 | | - $this->setMapName(); |
38 | | - |
39 | | - $this->manageMapProperties($map, __CLASS__); |
40 | | - |
41 | | - $this->setCoordinates(); |
42 | | - |
43 | | - $this->setZoom(); |
44 | | - |
45 | | - $this->setCentre(); |
46 | | - |
47 | | - $this->doParsing($parser); |
48 | | - |
49 | | - $this->doEscaping(); |
50 | | - |
51 | | - $this->addSpecificMapHTML(); |
52 | | - |
53 | | - return $this->output; |
54 | | - } |
55 | | - |
56 | | - /** |
57 | | - * Sets the zoom level to the provided value. When no zoom is provided, set |
58 | | - * it to the default when there is only one location, or the best fitting soom when |
59 | | - * there are multiple locations. |
60 | | - * |
61 | | - */ |
62 | | - private function setZoom() { |
63 | | - if (strlen($this->zoom) < 1) { |
64 | | - if (count($this->markerData) > 1) { |
65 | | - $this->zoom = 'null'; |
66 | | - } |
67 | | - else { |
68 | | - $this->zoom = $this->defaultZoom; |
69 | | - } |
70 | | - } |
71 | | - } |
72 | | - |
73 | | - /** |
74 | | - * Fills the $markerData array with the locations and their meta data. |
75 | | - * |
76 | | - */ |
77 | | - private function setCoordinates() { |
78 | | - $this->coordinates = explode(';', $this->coordinates); |
79 | | - |
80 | | - foreach($this->coordinates as $coordinates) { |
81 | | - $args = explode('~', $coordinates); |
82 | | - |
83 | | - $args[0] = str_replace('″', '"', $args[0]); |
84 | | - $args[0] = str_replace('′', "'", $args[0]); |
85 | | - |
86 | | - $markerData = MapsUtils::getLatLon($args[0]); |
87 | | - |
88 | | - if (count($args) > 1) { |
89 | | - $markerData['title'] = $args[1]; |
90 | | - |
91 | | - if (count($args) > 2) { |
92 | | - $markerData['label'] = $args[2]; |
93 | | - |
94 | | - if (count($args) > 3) { |
95 | | - $markerData['icon'] = $args[3]; |
96 | | - } |
97 | | - } |
98 | | - } |
99 | | - |
100 | | - $this->markerData[] = $markerData; |
101 | | - } |
102 | | - } |
103 | | - |
104 | | - /** |
105 | | - * Sets the $centre_lat and $centre_lon fields. |
106 | | - * Note: this needs to be done AFTRE the maker coordinates are set. |
107 | | - * |
108 | | - */ |
109 | | - private function setCentre() { |
110 | | - if (empty($this->centre)) { |
111 | | - if (count($this->markerData) == 1) { |
112 | | - // If centre is not set and there is exactelly one marker, use it's coordinates. |
113 | | - $this->centre_lat = $this->markerData[0]['lat']; |
114 | | - $this->centre_lon = $this->markerData[0]['lon']; |
115 | | - } |
116 | | - elseif (count($this->markerData) > 1) { |
117 | | - // If centre is not set and there are multiple markers, set the values to null, |
118 | | - // to be auto determined by the JS of the mapping API. |
119 | | - $this->centre_lat = 'null'; |
120 | | - $this->centre_lon = 'null'; |
121 | | - } |
122 | | - else { |
123 | | - // If centre is not set and there are no markers, use the default latitude and longitutde. |
124 | | - global $egMapsMapLat, $egMapsMapLon; |
125 | | - $this->centre_lat = $egMapsMapLat; |
126 | | - $this->centre_lon = $egMapsMapLon; |
127 | | - } |
128 | | - } |
129 | | - else { |
130 | | - // If a centre value is set, use it. |
131 | | - $centre = MapsUtils::getLatLon($this->centre); |
132 | | - $this->centre_lat = $centre['lat']; |
133 | | - $this->centre_lon = $centre['lon']; |
134 | | - } |
135 | | - } |
136 | | - |
137 | | - /** |
138 | | - * Parse the wiki text in the title and label values. |
139 | | - * |
140 | | - * @param $parser |
141 | | - */ |
142 | | - private function DoParsing(&$parser) { |
143 | | - $this->title = $parser->recursiveTagParse( $this->title ); |
144 | | - $this->label = $parser->recursiveTagParse( $this->label ); |
145 | | - } |
146 | | - |
147 | | - /** |
148 | | - * Escape the title and label text |
149 | | - * |
150 | | - */ |
151 | | - private function doEscaping() { |
152 | | - $this->title = str_replace("'", "\'", $this->title); |
153 | | - $this->label = str_replace("'", "\'", $this->label); |
154 | | - } |
155 | | - |
156 | | -} |
Index: trunk/extensions/Maps/OpenLayers/Maps_OpenLayers.php |
— | — | @@ -1,7 +1,7 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | /** |
5 | | - * Class for handling the Maps parser functions with OpenLayers |
| 5 | + * This file holds the general information for the OpenLayers service |
6 | 6 | * |
7 | 7 | * @file Maps_OpenLayers.php |
8 | 8 | * @ingroup Maps |
— | — | @@ -9,82 +9,14 @@ |
10 | 10 | * @author Jeroen De Dauw |
11 | 11 | */ |
12 | 12 | |
13 | | -if( !defined( 'MEDIAWIKI' ) ) { |
14 | | - die( 'Not an entry point.' ); |
15 | | -} |
16 | | - |
17 | | -class MapsOpenLayers extends MapsBaseMap { |
18 | | - |
19 | | - const SERVICE_NAME = 'openlayers'; |
20 | | - |
21 | | - public $serviceName = self::SERVICE_NAME; |
22 | | - |
23 | | - /** |
24 | | - * @see MapsBaseMap::setMapSettings() |
25 | | - * |
26 | | - */ |
27 | | - protected function setMapSettings() { |
28 | | - global $egMapsOpenLayersZoom, $egMapsOpenLayersPrefix; |
29 | | - |
30 | | - $this->defaultParams = MapsOpenLayersUtils::getDefaultParams(); |
31 | | - |
32 | | - $this->elementNamePrefix = $egMapsOpenLayersPrefix; |
33 | | - $this->defaultZoom = $egMapsOpenLayersZoom; |
34 | | - } |
35 | | - |
36 | | - /** |
37 | | - * @see MapsBaseMap::doMapServiceLoad() |
38 | | - * |
39 | | - */ |
40 | | - protected function doMapServiceLoad() { |
41 | | - global $egOpenLayersOnThisPage; |
42 | | - |
43 | | - MapsOpenLayersUtils::addOLDependencies($this->output); |
44 | | - $egOpenLayersOnThisPage++; |
45 | | - |
46 | | - $this->elementNr = $egOpenLayersOnThisPage; |
47 | | - } |
48 | | - |
49 | | - /** |
50 | | - * @see MapsBaseMap::addSpecificMapHTML() |
51 | | - * |
52 | | - */ |
53 | | - public function addSpecificMapHTML() { |
54 | | - global $wgJsMimeType; |
55 | | - |
56 | | - $controlItems = MapsOpenLayersUtils::createControlsString($this->controls); |
57 | | - |
58 | | - MapsMapper::enforceArrayValues($this->layers); |
59 | | - $layerItems = MapsOpenLayersUtils::createLayersStringAndLoadDependencies($this->output, $this->layers); |
60 | | - |
61 | | - MapsUtils::makePxValue($this->width); |
62 | | - MapsUtils::makePxValue($this->height); |
63 | | - |
64 | | - $markerItems = array(); |
65 | | - |
66 | | - // TODO: Refactor up |
67 | | - foreach ($this->markerData as $markerData) { |
68 | | - $lat = $markerData['lat']; |
69 | | - $lon = $markerData['lon']; |
70 | | - |
71 | | - $title = array_key_exists('title', $markerData) ? $markerData['title'] : $this->title; |
72 | | - $label = array_key_exists('label', $markerData) ? $markerData['label'] : $this->label; |
73 | | - |
74 | | - $title = str_replace("'", "\'", $title); |
75 | | - $label = str_replace("'", "\'", $label); |
76 | | - |
77 | | - $icon = array_key_exists('icon', $markerData) ? $markerData['icon'] : ''; |
78 | | - $markerItems[] = "getOLMarkerData($lon, $lat, '$title', '$label', '$icon')"; |
79 | | - } |
80 | | - |
81 | | - $markersString = implode(',', $markerItems); |
82 | | - |
83 | | - $this->output .= "<div id='$this->mapName' style='width: $this->width; height: $this->height; background-color: #cccccc;'></div> |
84 | | - <script type='$wgJsMimeType'> /*<![CDATA[*/ |
85 | | - addLoadEvent( |
86 | | - initOpenLayer('$this->mapName', $this->centre_lon, $this->centre_lat, $this->zoom, [$layerItems], [$controlItems],[$markersString]) |
87 | | - ); |
88 | | - /*]]>*/ </script>"; |
89 | | - } |
90 | | - |
91 | | -} |
\ No newline at end of file |
| 13 | +$egMapsServices['openlayers'] = array( |
| 14 | + 'pf' => array('class' => 'MapsOpenLayersParserFunctions', 'file' => 'OpenLayers/Maps_OpenLayersParserFunctions.php', 'local' => true), |
| 15 | + 'classes' => array( |
| 16 | + array('class' => 'MapsOpenLayersUtils', 'file' => 'OpenLayers/Maps_OpenLayersUtils.php', 'local' => true) |
| 17 | + ), |
| 18 | + 'aliases' => array('layers', 'openlayer'), |
| 19 | + 'parameters' => array( |
| 20 | + 'layers' => array(), |
| 21 | + 'baselayer' => array() |
| 22 | + ) |
| 23 | + ); |
\ No newline at end of file |
Index: trunk/extensions/Maps/OpenLayers/Maps_OpenLayersUtils.php |
— | — | @@ -15,6 +15,8 @@ |
16 | 16 | |
17 | 17 | final class MapsOpenLayersUtils { |
18 | 18 | |
| 19 | + const SERVICE_NAME = 'openlayers'; |
| 20 | + |
19 | 21 | private static $loadedBing = false; |
20 | 22 | private static $loadedYahoo = false; |
21 | 23 | private static $loadedOL = false; |
Index: trunk/extensions/Maps/OpenLayers/Maps_OpenLayersParserFunctions.php |
— | — | @@ -0,0 +1,88 @@ |
| 2 | +<?php
|
| 3 | +
|
| 4 | +/**
|
| 5 | + * Class for handling the Maps parser functions with OpenLayers
|
| 6 | + *
|
| 7 | + * @file Maps_OpenLayersParserFunctions.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 | +class MapsOpenLayersParserFunctions extends MapsBaseMap {
|
| 18 | +
|
| 19 | + public $serviceName = MapsOpenLayersUtils::SERVICE_NAME;
|
| 20 | +
|
| 21 | + /**
|
| 22 | + * @see MapsBaseMap::setMapSettings()
|
| 23 | + *
|
| 24 | + */
|
| 25 | + protected function setMapSettings() {
|
| 26 | + global $egMapsOpenLayersZoom, $egMapsOpenLayersPrefix;
|
| 27 | +
|
| 28 | + $this->defaultParams = MapsOpenLayersUtils::getDefaultParams();
|
| 29 | +
|
| 30 | + $this->elementNamePrefix = $egMapsOpenLayersPrefix;
|
| 31 | + $this->defaultZoom = $egMapsOpenLayersZoom;
|
| 32 | + }
|
| 33 | +
|
| 34 | + /**
|
| 35 | + * @see MapsBaseMap::doMapServiceLoad()
|
| 36 | + *
|
| 37 | + */
|
| 38 | + protected function doMapServiceLoad() {
|
| 39 | + global $egOpenLayersOnThisPage;
|
| 40 | +
|
| 41 | + MapsOpenLayersUtils::addOLDependencies($this->output);
|
| 42 | + $egOpenLayersOnThisPage++;
|
| 43 | +
|
| 44 | + $this->elementNr = $egOpenLayersOnThisPage;
|
| 45 | + }
|
| 46 | +
|
| 47 | + /**
|
| 48 | + * @see MapsBaseMap::addSpecificMapHTML()
|
| 49 | + *
|
| 50 | + */
|
| 51 | + public function addSpecificMapHTML() {
|
| 52 | + global $wgJsMimeType;
|
| 53 | +
|
| 54 | + $controlItems = MapsOpenLayersUtils::createControlsString($this->controls);
|
| 55 | +
|
| 56 | + MapsMapper::enforceArrayValues($this->layers);
|
| 57 | + $layerItems = MapsOpenLayersUtils::createLayersStringAndLoadDependencies($this->output, $this->layers);
|
| 58 | +
|
| 59 | + MapsUtils::makePxValue($this->width);
|
| 60 | + MapsUtils::makePxValue($this->height);
|
| 61 | +
|
| 62 | + $markerItems = array();
|
| 63 | +
|
| 64 | + // TODO: Refactor up
|
| 65 | + foreach ($this->markerData as $markerData) {
|
| 66 | + $lat = $markerData['lat'];
|
| 67 | + $lon = $markerData['lon'];
|
| 68 | +
|
| 69 | + $title = array_key_exists('title', $markerData) ? $markerData['title'] : $this->title;
|
| 70 | + $label = array_key_exists('label', $markerData) ? $markerData['label'] : $this->label;
|
| 71 | +
|
| 72 | + $title = str_replace("'", "\'", $title);
|
| 73 | + $label = str_replace("'", "\'", $label);
|
| 74 | +
|
| 75 | + $icon = array_key_exists('icon', $markerData) ? $markerData['icon'] : '';
|
| 76 | + $markerItems[] = "getOLMarkerData($lon, $lat, '$title', '$label', '$icon')";
|
| 77 | + }
|
| 78 | +
|
| 79 | + $markersString = implode(',', $markerItems);
|
| 80 | +
|
| 81 | + $this->output .= "<div id='$this->mapName' style='width: $this->width; height: $this->height; background-color: #cccccc;'></div>
|
| 82 | + <script type='$wgJsMimeType'> /*<![CDATA[*/
|
| 83 | + addLoadEvent(
|
| 84 | + initOpenLayer('$this->mapName', $this->centre_lon, $this->centre_lat, $this->zoom, [$layerItems], [$controlItems],[$markersString])
|
| 85 | + );
|
| 86 | + /*]]>*/ </script>";
|
| 87 | + }
|
| 88 | +
|
| 89 | +} |
\ No newline at end of file |
Index: trunk/extensions/Maps/ParserFunctions/Maps_ParserFunctions.php |
— | — | @@ -0,0 +1,244 @@ |
| 2 | +<?php
|
| 3 | +
|
| 4 | +/**
|
| 5 | + * Initialization file for parser function functionality in the Maps extension
|
| 6 | + *
|
| 7 | + * @file Maps_ParserFunctions.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 | +$wgHooks['LanguageGetMagic'][] = 'efMapsFunctionMagic';
|
| 18 | +
|
| 19 | +/**
|
| 20 | + * Adds the magic words for the parser functions
|
| 21 | + */
|
| 22 | +function efMapsFunctionMagic( &$magicWords, $langCode ) {
|
| 23 | + $magicWords['display_point'] = array( 0, 'display_point' );
|
| 24 | + $magicWords['display_points'] = array( 0, 'display_points' );
|
| 25 | + $magicWords['display_address'] = array( 0, 'display_address' );
|
| 26 | + $magicWords['display_addresses'] = array( 0, 'display_addresses' );
|
| 27 | +
|
| 28 | + $magicWords['geocode'] = array( 0, 'geocode' );
|
| 29 | + $magicWords['geocodelat'] = array ( 0, 'geocodelat' );
|
| 30 | + $magicWords['geocodelng'] = array ( 0, 'geocodelng' );
|
| 31 | +
|
| 32 | + return true; // Unless we return true, other parser functions won't get loaded
|
| 33 | +}
|
| 34 | +
|
| 35 | +/**
|
| 36 | + * A class that holds handlers for the mapping parser functions.
|
| 37 | + * Spesific functions are located in @see MapsUtils
|
| 38 | + *
|
| 39 | + * @author Jeroen De Dauw
|
| 40 | + *
|
| 41 | + */
|
| 42 | +final class MapsParserFunctions {
|
| 43 | +
|
| 44 | + /**
|
| 45 | + * Initialize the parser functions feature
|
| 46 | + *
|
| 47 | + */
|
| 48 | + public static function initialize() {
|
| 49 | + global $egMapsIP, $wgAutoloadClasses;
|
| 50 | + $wgAutoloadClasses['MapsBaseMap'] = $egMapsIP . '/ParserFunctions/Maps_BaseMap.php';
|
| 51 | +
|
| 52 | + self::addParserHooks();
|
| 53 | + }
|
| 54 | +
|
| 55 | + /**
|
| 56 | + * Adds the parser function hooks
|
| 57 | + */
|
| 58 | + private static function addParserHooks() {
|
| 59 | + global $wgParser;
|
| 60 | +
|
| 61 | + // A hooks to enable the '#display_point' and '#display_points' parser functions
|
| 62 | + $wgParser->setFunctionHook( 'display_point', array('MapsParserFunctions', 'displayPointRender') );
|
| 63 | + $wgParser->setFunctionHook( 'display_points', array('MapsParserFunctions', 'displayPointsRender') );
|
| 64 | +
|
| 65 | + // A hooks to enable the '#display_adress' and '#display_adresses' parser functions
|
| 66 | + $wgParser->setFunctionHook( 'display_address', array('MapsParserFunctions', 'displayAddressRender') );
|
| 67 | + $wgParser->setFunctionHook( 'display_addresses', array('MapsParserFunctions', 'displayAddressesRender') );
|
| 68 | +
|
| 69 | + // A hook to enable the geocoder parser functions
|
| 70 | + $wgParser->setFunctionHook( 'geocode', array('MapsGeocoder', 'renderGeocoder') );
|
| 71 | + $wgParser->setFunctionHook( 'geocodelat' , array('MapsGeocoder', 'renderGeocoderLat') );
|
| 72 | + $wgParser->setFunctionHook( 'geocodelng' , array('MapsGeocoder', 'renderGeocoderLng') );
|
| 73 | + }
|
| 74 | +
|
| 75 | + public static function getMapHtml(&$parser, array $params, array $coordFails = array()) {
|
| 76 | + global $wgLang;
|
| 77 | +
|
| 78 | + $map = array();
|
| 79 | +
|
| 80 | + // Go through all parameters, split their names and values, and put them in the $map array.
|
| 81 | + foreach($params as $param) {
|
| 82 | + $split = split('=', $param);
|
| 83 | + if (count($split) > 1) {
|
| 84 | + $paramName = strtolower(trim($split[0]));
|
| 85 | + $paramValue = trim($split[1]);
|
| 86 | + if (strlen($paramName) > 0 && strlen($paramValue) > 0) {
|
| 87 | + $map[$paramName] = $paramValue;
|
| 88 | + }
|
| 89 | + }
|
| 90 | + else if (count($split) == 1) { // Default parameter (without name)
|
| 91 | + $split[0] = trim($split[0]);
|
| 92 | + if (strlen($split[0]) > 0) $map['coordinates'] = $split[0];
|
| 93 | + }
|
| 94 | + }
|
| 95 | +
|
| 96 | + $coords = MapsMapper::getParamValue('coordinates', $map);
|
| 97 | +
|
| 98 | + if ($coords) {
|
| 99 | + if (! MapsMapper::paramIsPresent('service', $map)) $map['service'] = '';
|
| 100 | + $map['service'] = MapsMapper::getValidService($map['service'], 'pf');
|
| 101 | +
|
| 102 | + $mapClass = self::getParserClassInstance($map['service']);
|
| 103 | +
|
| 104 | + // Call the function according to the map service to get the HTML output
|
| 105 | + $output = $mapClass->displayMap($parser, $map);
|
| 106 | +
|
| 107 | + if (count($coordFails) > 0) {
|
| 108 | + $output .= '<i>' . wfMsgExt( 'maps_geocoding_failed_for', array( 'parsemag' ), $wgLang->listToText($coordFails ), count( $coordFails ) ) . '</i>';
|
| 109 | + }
|
| 110 | + }
|
| 111 | + elseif (trim($coords) == "" && count($coordFails) > 0) {
|
| 112 | + $output = '<i>' . wfMsgExt( 'maps_geocoding_failed', array( 'parsemag' ), $wgLang->listToText( $coordFails ), count( $coordFails ) ) . '</i>';
|
| 113 | + }
|
| 114 | + else {
|
| 115 | + $output = '<i>'.wfMsg( 'maps_coordinates_missing' ).'</i>';
|
| 116 | + }
|
| 117 | +
|
| 118 | + // Return the result
|
| 119 | + return array( $output, 'noparse' => true, 'isHTML' => true );
|
| 120 | + }
|
| 121 | +
|
| 122 | + private static function getParserClassInstance($service) {
|
| 123 | + global $egMapsServices;
|
| 124 | + return new $egMapsServices[$service]['pf']['class']();
|
| 125 | + }
|
| 126 | +
|
| 127 | + /**
|
| 128 | + * Sets the default map properties, gets the map HTML depending
|
| 129 | + * on the provided service, and then returns it.
|
| 130 | + *
|
| 131 | + * @param unknown_type $parser
|
| 132 | + * @return array
|
| 133 | + */
|
| 134 | + public static function displayPointRender(&$parser) {
|
| 135 | + $params = func_get_args();
|
| 136 | + array_shift( $params ); // We already know the $parser ...
|
| 137 | +
|
| 138 | + return self::getMapHtml($parser, $params);
|
| 139 | + }
|
| 140 | +
|
| 141 | + /**
|
| 142 | + * Sets the default map properties, gets the map HTML depending
|
| 143 | + * on the provided service, and then returns it.
|
| 144 | + *
|
| 145 | + * @param unknown_type $parser
|
| 146 | + */
|
| 147 | + public static function displayPointsRender(&$parser) {
|
| 148 | + $params = func_get_args();
|
| 149 | + array_shift( $params ); // We already know the $parser ...
|
| 150 | +
|
| 151 | + return self::getMapHtml($parser, $params);
|
| 152 | + }
|
| 153 | +
|
| 154 | + /**
|
| 155 | + * Turns the address parameter into coordinates, then calls
|
| 156 | + * getMapHtml() and returns it's result.
|
| 157 | + *
|
| 158 | + * @param unknown_type $parser
|
| 159 | + * @return array
|
| 160 | + */
|
| 161 | + public static function displayAddressRender(&$parser) {
|
| 162 | + $params = func_get_args();
|
| 163 | + array_shift( $params ); // We already know the $parser ...
|
| 164 | +
|
| 165 | + $fails = self::changeAddressToCoords($params);
|
| 166 | +
|
| 167 | + return self::getMapHtml($parser, $params, $fails);
|
| 168 | + }
|
| 169 | +
|
| 170 | + /**
|
| 171 | + * Turns the address parameter into coordinates, then calls
|
| 172 | + * getMapHtml() and returns it's result.
|
| 173 | + *
|
| 174 | + * @param unknown_type $parser
|
| 175 | + */
|
| 176 | + public static function displayAddressesRender(&$parser) {
|
| 177 | + $params = func_get_args();
|
| 178 | + array_shift( $params ); // We already know the $parser ...
|
| 179 | +
|
| 180 | + $fails = self::changeAddressToCoords($params);
|
| 181 | +
|
| 182 | + return self::getMapHtml($parser, $params, $fails);
|
| 183 | + }
|
| 184 | +
|
| 185 | + /**
|
| 186 | + * Changes the values of the address or addresses parameter into coordinates
|
| 187 | + * in the provided array. Returns an array containing the addresses that
|
| 188 | + * could not be geocoded.
|
| 189 | + *
|
| 190 | + * @param array $params
|
| 191 | + */
|
| 192 | + private static function changeAddressToCoords(&$params) {
|
| 193 | + global $egMapsDefaultService;
|
| 194 | +
|
| 195 | + $fails = array();
|
| 196 | +
|
| 197 | + for ($i = 0; $i < count($params); $i++) {
|
| 198 | + $split = split('=', $params[$i]);
|
| 199 | + if (MapsMapper::inParamAliases(strtolower(trim($split[0])), 'service') && count($split) > 1) {
|
| 200 | + $service = trim($split[1]);
|
| 201 | + }
|
| 202 | + else if (strtolower(trim($split[0])) == 'geoservice' && count($split) > 1) {
|
| 203 | + $geoservice = trim($split[1]);
|
| 204 | + }
|
| 205 | + }
|
| 206 | +
|
| 207 | + $service = isset($service) ? MapsMapper::getValidService($service, 'pf') : $egMapsDefaultService;
|
| 208 | +
|
| 209 | + $geoservice = isset($geoservice) ? $geoservice : '';
|
| 210 | +
|
| 211 | + for ($i = 0; $i < count($params); $i++) {
|
| 212 | + $split = split('=', $params[$i]);
|
| 213 | + if (((strtolower(trim($split[0])) == 'address' || strtolower(trim($split[0])) == 'addresses') && count($split) > 0)) {
|
| 214 | + $address_srting = count($split) == 1 ? $split[0] : $split[1];
|
| 215 | + //var_dump($address_srting);
|
| 216 | + $addresses = explode(';', $address_srting);
|
| 217 | +
|
| 218 | + $coordinates = array();
|
| 219 | +
|
| 220 | + foreach($addresses as $address) {
|
| 221 | + $args = explode('~', $address);
|
| 222 | + $args[0] = trim($args[0]);
|
| 223 | +
|
| 224 | + if (strlen($args[0]) > 0) {
|
| 225 | + $coords = MapsGeocoder::geocodeToString($args[0], $geoservice, $service);
|
| 226 | +
|
| 227 | + if ($coords) {
|
| 228 | + $args[0] = $coords;
|
| 229 | + $coordinates[] = implode('~', $args);
|
| 230 | + }
|
| 231 | + else {
|
| 232 | + $fails[] = $args[0];
|
| 233 | + }
|
| 234 | + }
|
| 235 | + }
|
| 236 | +
|
| 237 | + $params[$i] = 'coordinates=' . implode(';', $coordinates);
|
| 238 | +
|
| 239 | + }
|
| 240 | + } //exit;
|
| 241 | +
|
| 242 | + return $fails;
|
| 243 | + }
|
| 244 | +
|
| 245 | +} |
\ No newline at end of file |
Index: trunk/extensions/Maps/ParserFunctions/Maps_BaseMap.php |
— | — | @@ -0,0 +1,161 @@ |
| 2 | +<?php
|
| 3 | +
|
| 4 | +/**
|
| 5 | + * MapsBaseMap is an abstract class inherited by the map services classes
|
| 6 | + *
|
| 7 | + * @file Maps_BaseMap.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 | +abstract class MapsBaseMap extends MapsMapFeature {
|
| 18 | +
|
| 19 | + // TODO: move this abstract function to a new MapsBaseMapUtils file?
|
| 20 | + //protected abstract static function getDefaultParams();
|
| 21 | +
|
| 22 | + protected $markerData = array();
|
| 23 | +
|
| 24 | + /**
|
| 25 | + * Handles the request from the parser hook by doing the work that's common for all
|
| 26 | + * mapping services, calling the specific methods and finally returning the resulting output.
|
| 27 | + *
|
| 28 | + * @param unknown_type $parser
|
| 29 | + * @param array $map
|
| 30 | + * @return html
|
| 31 | + */
|
| 32 | + public final function displayMap(&$parser, array $params) {
|
| 33 | + global $wgLang;
|
| 34 | +
|
| 35 | + $this->setMapSettings();
|
| 36 | +
|
| 37 | + $coords = $this->manageMapProperties($params);
|
| 38 | +
|
| 39 | + $this->doMapServiceLoad();
|
| 40 | +
|
| 41 | + $this->setMapName();
|
| 42 | +
|
| 43 | + $this->setCoordinates();
|
| 44 | +
|
| 45 | + $this->setZoom();
|
| 46 | +
|
| 47 | + $this->setCentre();
|
| 48 | +
|
| 49 | + $this->doParsing($parser);
|
| 50 | +
|
| 51 | + $this->doEscaping();
|
| 52 | +
|
| 53 | + $this->addSpecificMapHTML();
|
| 54 | +
|
| 55 | + return $this->output;
|
| 56 | + }
|
| 57 | +
|
| 58 | + protected function manageMapProperties($params) {
|
| 59 | + parent::manageMapProperties($params, __CLASS__);
|
| 60 | + }
|
| 61 | +
|
| 62 | + /**
|
| 63 | + * Sets the zoom level to the provided value. When no zoom is provided, set
|
| 64 | + * it to the default when there is only one location, or the best fitting soom when
|
| 65 | + * there are multiple locations.
|
| 66 | + *
|
| 67 | + */
|
| 68 | + private function setZoom() {
|
| 69 | + if (strlen($this->zoom) < 1) {
|
| 70 | + if (count($this->markerData) > 1) {
|
| 71 | + $this->zoom = 'null';
|
| 72 | + }
|
| 73 | + else {
|
| 74 | + $this->zoom = $this->defaultZoom;
|
| 75 | + }
|
| 76 | + }
|
| 77 | + }
|
| 78 | +
|
| 79 | + /**
|
| 80 | + * Fills the $markerData array with the locations and their meta data.
|
| 81 | + *
|
| 82 | + */
|
| 83 | + private function setCoordinates() {
|
| 84 | + $this->coordinates = explode(';', $this->coordinates);
|
| 85 | +
|
| 86 | + foreach($this->coordinates as $coordinates) {
|
| 87 | + $args = explode('~', $coordinates);
|
| 88 | +
|
| 89 | + $args[0] = str_replace('″', '"', $args[0]);
|
| 90 | + $args[0] = str_replace('′', "'", $args[0]);
|
| 91 | +
|
| 92 | + $markerData = MapsUtils::getLatLon($args[0]);
|
| 93 | +
|
| 94 | + if (count($args) > 1) {
|
| 95 | + $markerData['title'] = $args[1];
|
| 96 | +
|
| 97 | + if (count($args) > 2) {
|
| 98 | + $markerData['label'] = $args[2];
|
| 99 | +
|
| 100 | + if (count($args) > 3) {
|
| 101 | + $markerData['icon'] = $args[3];
|
| 102 | + }
|
| 103 | + }
|
| 104 | + }
|
| 105 | +
|
| 106 | + $this->markerData[] = $markerData;
|
| 107 | + }
|
| 108 | + }
|
| 109 | +
|
| 110 | + /**
|
| 111 | + * Sets the $centre_lat and $centre_lon fields.
|
| 112 | + * Note: this needs to be done AFTRE the maker coordinates are set.
|
| 113 | + *
|
| 114 | + */
|
| 115 | + private function setCentre() {
|
| 116 | + if (empty($this->centre)) {
|
| 117 | + if (count($this->markerData) == 1) {
|
| 118 | + // If centre is not set and there is exactelly one marker, use it's coordinates.
|
| 119 | + $this->centre_lat = $this->markerData[0]['lat'];
|
| 120 | + $this->centre_lon = $this->markerData[0]['lon'];
|
| 121 | + }
|
| 122 | + elseif (count($this->markerData) > 1) {
|
| 123 | + // If centre is not set and there are multiple markers, set the values to null,
|
| 124 | + // to be auto determined by the JS of the mapping API.
|
| 125 | + $this->centre_lat = 'null';
|
| 126 | + $this->centre_lon = 'null';
|
| 127 | + }
|
| 128 | + else {
|
| 129 | + // If centre is not set and there are no markers, use the default latitude and longitutde.
|
| 130 | + global $egMapsMapLat, $egMapsMapLon;
|
| 131 | + $this->centre_lat = $egMapsMapLat;
|
| 132 | + $this->centre_lon = $egMapsMapLon;
|
| 133 | + }
|
| 134 | + }
|
| 135 | + else {
|
| 136 | + // If a centre value is set, use it.
|
| 137 | + $centre = MapsUtils::getLatLon($this->centre);
|
| 138 | + $this->centre_lat = $centre['lat'];
|
| 139 | + $this->centre_lon = $centre['lon'];
|
| 140 | + }
|
| 141 | + }
|
| 142 | +
|
| 143 | + /**
|
| 144 | + * Parse the wiki text in the title and label values.
|
| 145 | + *
|
| 146 | + * @param $parser
|
| 147 | + */
|
| 148 | + private function DoParsing(&$parser) {
|
| 149 | + $this->title = $parser->recursiveTagParse( $this->title );
|
| 150 | + $this->label = $parser->recursiveTagParse( $this->label );
|
| 151 | + }
|
| 152 | +
|
| 153 | + /**
|
| 154 | + * Escape the title and label text
|
| 155 | + *
|
| 156 | + */
|
| 157 | + private function doEscaping() {
|
| 158 | + $this->title = str_replace("'", "\'", $this->title);
|
| 159 | + $this->label = str_replace("'", "\'", $this->label);
|
| 160 | + }
|
| 161 | +
|
| 162 | +}
|
Index: trunk/extensions/Maps/Maps.php |
— | — | @@ -41,74 +41,41 @@ |
42 | 42 | |
43 | 43 | $wgExtensionMessagesFiles['Maps'] = $egMapsIP . '/Maps.i18n.php'; |
44 | 44 | |
45 | | -$wgHooks['LanguageGetMagic'][] = 'efMapsFunctionMagic'; |
46 | 45 | $wgHooks['AdminLinks'][] = 'efMapsAddToAdminLinks'; |
47 | 46 | |
48 | 47 | // Autoload the general classes |
49 | 48 | $wgAutoloadClasses['MapsMapFeature'] = $egMapsIP . '/Maps_MapFeature.php'; |
50 | | -$wgAutoloadClasses['MapsBaseMap'] = $egMapsIP . '/Maps_BaseMap.php'; |
51 | 49 | $wgAutoloadClasses['MapsMapper'] = $egMapsIP . '/Maps_Mapper.php'; |
52 | | -$wgAutoloadClasses['MapsParserFunctions'] = $egMapsIP . '/Maps_ParserFunctions.php'; |
53 | 50 | $wgAutoloadClasses['MapsUtils'] = $egMapsIP . '/Maps_Utils.php'; |
54 | | -$wgAutoloadClasses['MapsGeocoder'] = $egMapsIP . '/Maps_Geocoder.php'; |
55 | | -$wgAutoloadClasses['MapsBaseGeocoder'] = $egMapsIP . '/Maps_BaseGeocoder.php'; |
56 | 51 | |
| 52 | +// TODO: change geocoder setup to feature hook, and load the relevant classes from it's init function |
| 53 | +$wgAutoloadClasses['MapsGeocoder'] = $egMapsIP . '/Geocoders/Maps_Geocoder.php'; |
| 54 | +$wgAutoloadClasses['MapsBaseGeocoder'] = $egMapsIP . '/Geocoders/Maps_BaseGeocoder.php'; |
| 55 | + |
57 | 56 | if (empty($egMapsServices)) $egMapsServices = array(); |
58 | 57 | |
59 | | -$egMapsServices['googlemaps'] = array( |
60 | | - 'pf' => array('class' => 'MapsGoogleMaps', 'file' => 'GoogleMaps/Maps_GoogleMaps.php', 'local' => true), |
61 | | - 'classes' => array( |
62 | | - array('class' => 'MapsGoogleMapsUtils', 'file' => 'GoogleMaps/Maps_GoogleMapsUtils.php', 'local' => true) |
63 | | - ), |
64 | | - 'aliases' => array('google', 'googlemap', 'gmap', 'gmaps'), |
65 | | - 'parameters' => array( |
66 | | - 'type' => array('map-type', 'map type'), |
67 | | - 'types' => array('map-types', 'map types'), |
68 | | - 'earth' => array(), |
69 | | - 'autozoom' => array('auto zoom', 'mouse zoom', 'mousezoom'), |
70 | | - 'class' => array(), |
71 | | - 'style' => array() |
72 | | - ) |
73 | | - ); |
74 | | - |
75 | | -$egMapsServices['openlayers'] = array( |
76 | | - 'pf' => array('class' => 'MapsOpenLayers', 'file' => 'OpenLayers/Maps_OpenLayers.php', 'local' => true), |
77 | | - 'classes' => array( |
78 | | - array('class' => 'MapsOpenLayersUtils', 'file' => 'OpenLayers/Maps_OpenLayersUtils.php', 'local' => true) |
79 | | - ), |
80 | | - 'aliases' => array('layers', 'openlayer'), |
81 | | - 'parameters' => array( |
82 | | - 'layers' => array(), |
83 | | - 'baselayer' => array() |
84 | | - ) |
85 | | - ); |
86 | | - |
87 | | -$egMapsServices['yahoomaps'] = array( |
88 | | - 'pf' => array('class' => 'MapsYahooMaps', 'file' => 'YahooMaps/Maps_YahooMaps.php', 'local' => true), |
89 | | - 'classes' => array( |
90 | | - array('class' => 'MapsYahooMapsUtils', 'file' => 'YahooMaps/Maps_YahooMapsUtils.php', 'local' => true) |
91 | | - ), |
92 | | - 'aliases' => array('yahoo', 'yahoomap', 'ymap', 'ymaps'), |
93 | | - 'parameters' => array( |
94 | | - 'type' => array('map-type'), |
95 | | - 'types' => array('map-types', 'map types'), |
96 | | - 'autozoom' => array('auto zoom', 'mouse zoom', 'mousezoom') |
97 | | - ) |
98 | | - ); |
| 58 | +// TODO: move inclusions, or add init file to settings to allow auto load? |
| 59 | +include_once $egMapsIP . '/GoogleMaps/Maps_GoogleMaps.php'; |
| 60 | +include_once $egMapsIP . '/OpenLayers/Maps_OpenLayers.php'; |
| 61 | +include_once $egMapsIP . '/YahooMaps/Maps_YahooMaps.php'; |
99 | 62 | |
| 63 | +// TODO: split Maps_ParserFunctions.php functionallity so this line is not required |
| 64 | +include_once $egMapsIP . '/ParserFunctions/Maps_ParserFunctions.php'; |
| 65 | + |
100 | 66 | /** |
101 | 67 | * Initialization function for the Maps extension |
102 | 68 | */ |
103 | | -function efMapsSetup() { |
| 69 | +function efMapsSetup() { |
104 | 70 | global $wgExtensionCredits, $wgOut, $wgLang, $wgAutoloadClasses, $IP; |
105 | | - global $egMapsDefaultService, $egMapsAvailableServices, $egMapsServices, $egMapsScriptPath, $egMapsDefaultGeoService, $egMapsAvailableGeoServices, $egMapsIP; |
| 71 | + global $egMapsDefaultService, $egMapsAvailableServices, $egMapsServices, $egMapsScriptPath, $egMapsDefaultGeoService, $egMapsAvailableGeoServices, $egMapsIP, $egMapsAvailableFeatures; |
106 | 72 | |
107 | 73 | efMapsValidateGoogleMapsKey(); |
108 | 74 | |
109 | | - // Make sure the default service is one of the enabled ones |
| 75 | + // Enure that the default service is one of the enabled ones |
110 | 76 | $egMapsDefaultService = in_array($egMapsDefaultService, $egMapsAvailableServices) ? $egMapsDefaultService : $egMapsAvailableServices[0]; |
111 | 77 | $egMapsDefaultGeoService = in_array($egMapsDefaultGeoService, $egMapsAvailableGeoServices) ? $egMapsDefaultGeoService : $egMapsAvailableGeoServices[0]; |
112 | 78 | |
| 79 | + // TODO: split for feature hook system? |
113 | 80 | wfLoadExtensionMessages( 'Maps' ); |
114 | 81 | |
115 | 82 | // Creation of a list of internationalized service names |
— | — | @@ -126,72 +93,52 @@ |
127 | 94 | 'descriptionmsg' => wfMsgExt( 'maps_desc', 'parsemag', $services_list ), |
128 | 95 | ); |
129 | 96 | |
130 | | - efMapsAddParserHooks(); |
131 | | - |
132 | 97 | $wgOut->addScriptFile($egMapsScriptPath . '/MapUtilityFunctions.js'); |
133 | 98 | |
134 | | - foreach ($egMapsServices as $serviceData) { |
135 | | - if (array_key_exists('pf', $serviceData)) { |
136 | | - $file = $serviceData['pf']['local'] ? $egMapsIP . '/' . $serviceData['pf']['file'] : $IP . '/extensions/' . $serviceData['pf']['file']; |
137 | | - $wgAutoloadClasses[$serviceData['pf']['class']] = $file; |
| 99 | + foreach($egMapsAvailableFeatures as $key => $values) { |
| 100 | + // Load and optionally initizlize feature |
| 101 | + if (array_key_exists('class', $values) && array_key_exists('file', $values) && array_key_exists('local', $values)) { |
| 102 | + $wgAutoloadClasses[$values['class']] = $values['local'] ? $egMapsIP . '/' . $values['file'] : $IP . '/extensions/' . $values['file']; |
| 103 | + if (method_exists($values['class'], 'initialize')) { |
| 104 | + call_user_func(array($values['class'], 'initialize')); |
| 105 | + } |
138 | 106 | } |
139 | | - |
140 | | - if (array_key_exists('classes', $serviceData)) { |
141 | | - foreach($serviceData['classes'] as $class) { |
142 | | - $file = $class['local'] ? $egMapsIP . '/' . $class['file'] : $IP . '/extensions/' . $class['file']; |
143 | | - $wgAutoloadClasses[$class['class']] = $file; |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + // TODO: move to above loop |
| 111 | + foreach ($egMapsServices as $serviceData) { |
| 112 | + foreach($egMapsAvailableFeatures as $key => $name) { |
| 113 | + if (array_key_exists($key, $serviceData)) { |
| 114 | + $file = $serviceData[$key]['local'] ? $egMapsIP . '/' . $serviceData[$key]['file'] : $IP . '/extensions/' . $serviceData[$key]['file']; |
| 115 | + $wgAutoloadClasses[$serviceData[$key]['class']] = $file; |
| 116 | + } |
| 117 | + |
| 118 | + if (array_key_exists('classes', $serviceData)) { |
| 119 | + foreach($serviceData['classes'] as $class) { |
| 120 | + $file = $class['local'] ? $egMapsIP . '/' . $class['file'] : $IP . '/extensions/' . $class['file']; |
| 121 | + $wgAutoloadClasses[$class['class']] = $file; |
| 122 | + } |
144 | 123 | } |
145 | 124 | } |
146 | | - |
| 125 | + |
147 | 126 | } |
148 | 127 | |
149 | 128 | return true; |
150 | 129 | } |
151 | 130 | |
152 | | -/** |
153 | | - * Adds the parser function hooks |
154 | | - */ |
155 | | -function efMapsAddParserHooks() { |
156 | | - global $wgParser; |
157 | | - |
158 | | - // A hooks to enable the '#display_point' and '#display_points' parser functions |
159 | | - $wgParser->setFunctionHook( 'display_point', array('MapsParserFunctions', 'displayPointRender') ); |
160 | | - $wgParser->setFunctionHook( 'display_points', array('MapsParserFunctions', 'displayPointsRender') ); |
161 | 131 | |
162 | | - // A hooks to enable the '#display_adress' and '#display_adresses' parser functions |
163 | | - $wgParser->setFunctionHook( 'display_address', array('MapsParserFunctions', 'displayAddressRender') ); |
164 | | - $wgParser->setFunctionHook( 'display_addresses', array('MapsParserFunctions', 'displayAddressesRender') ); |
165 | 132 | |
166 | | - // A hook to enable the geocoder parser functions |
167 | | - $wgParser->setFunctionHook( 'geocode', array('MapsGeocoder', 'renderGeocoder') ); |
168 | | - $wgParser->setFunctionHook( 'geocodelat' , array('MapsGeocoder', 'renderGeocoderLat') ); |
169 | | - $wgParser->setFunctionHook( 'geocodelng' , array('MapsGeocoder', 'renderGeocoderLng') ); |
170 | | -} |
171 | | - |
172 | 133 | /** |
173 | | - * Adds the magic words for the parser functions |
174 | | - */ |
175 | | -function efMapsFunctionMagic( &$magicWords, $langCode ) { |
176 | | - $magicWords['display_point'] = array( 0, 'display_point' ); |
177 | | - $magicWords['display_points'] = array( 0, 'display_points' ); |
178 | | - $magicWords['display_address'] = array( 0, 'display_address' ); |
179 | | - $magicWords['display_addresses'] = array( 0, 'display_addresses' ); |
180 | | - |
181 | | - $magicWords['geocode'] = array( 0, 'geocode' ); |
182 | | - $magicWords['geocodelat'] = array ( 0, 'geocodelat' ); |
183 | | - $magicWords['geocodelng'] = array ( 0, 'geocodelng' ); |
184 | | - |
185 | | - return true; // Unless we return true, other parser functions won't get loaded |
186 | | -} |
187 | | - |
188 | | -/** |
189 | 134 | * This function ensures backward compatibility with Semantic Google Maps and other extensions |
190 | 135 | * using $wgGoogleMapsKey instead of $egGoogleMapsKey. |
191 | 136 | */ |
192 | 137 | function efMapsValidateGoogleMapsKey() { |
193 | 138 | global $egGoogleMapsKey, $wgGoogleMapsKey; |
194 | 139 | |
195 | | - if (strlen($egGoogleMapsKey) < 1 && isset($wgGoogleMapsKey)) $egGoogleMapsKey = $wgGoogleMapsKey; |
| 140 | + if (isset($wgGoogleMapsKey)){ |
| 141 | + if (strlen(trim($egGoogleMapsKey)) < 1) $egGoogleMapsKey = $wgGoogleMapsKey; |
| 142 | + } |
196 | 143 | } |
197 | 144 | |
198 | 145 | /** |
Index: trunk/extensions/Maps/GoogleMaps/Maps_GoogleMaps.php |
— | — | @@ -1,7 +1,7 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | /** |
5 | | - * Class for handling the Maps parser functions with Google Maps |
| 5 | + * This file holds the general information for the Google Maps service |
6 | 6 | * |
7 | 7 | * @file Maps_GoogleMaps.php |
8 | 8 | * @ingroup Maps |
— | — | @@ -9,92 +9,18 @@ |
10 | 10 | * @author Jeroen De Dauw |
11 | 11 | */ |
12 | 12 | |
13 | | -if( !defined( 'MEDIAWIKI' ) ) { |
14 | | - die( 'Not an entry point.' ); |
15 | | -} |
16 | | - |
17 | | -final class MapsGoogleMaps extends MapsBaseMap { |
18 | | - |
19 | | - const SERVICE_NAME = 'googlemaps'; |
20 | | - |
21 | | - public $serviceName = self::SERVICE_NAME; |
22 | | - |
23 | | - /** |
24 | | - * @see MapsBaseMap::setMapSettings() |
25 | | - * |
26 | | - */ |
27 | | - protected function setMapSettings() { |
28 | | - global $egMapsGoogleMapsZoom, $egMapsGoogleMapsPrefix; |
29 | | - |
30 | | - $this->defaultParams = MapsGoogleMapsUtils::getDefaultParams(); |
31 | | - |
32 | | - $this->elementNamePrefix = $egMapsGoogleMapsPrefix; |
33 | | - $this->defaultZoom = $egMapsGoogleMapsZoom; |
34 | | - } |
35 | | - |
36 | | - /** |
37 | | - * @see MapsBaseMap::doMapServiceLoad() |
38 | | - * |
39 | | - */ |
40 | | - protected function doMapServiceLoad() { |
41 | | - global $egGoogleMapsOnThisPage; |
42 | | - |
43 | | - MapsGoogleMapsUtils::addGMapDependencies($this->output); |
44 | | - $egGoogleMapsOnThisPage++; |
45 | | - |
46 | | - $this->elementNr = $egGoogleMapsOnThisPage; |
47 | | - } |
48 | | - |
49 | | - /** |
50 | | - * @see MapsBaseMap::addSpecificMapHTML() |
51 | | - * |
52 | | - */ |
53 | | - public function addSpecificMapHTML() { |
54 | | - global $wgJsMimeType; |
55 | | - |
56 | | - $enableEarth = MapsGoogleMapsUtils::getEarthValue($this->earth); |
57 | | - |
58 | | - $this->type = MapsGoogleMapsUtils::getGMapType($this->type, true); |
59 | | - |
60 | | - $this->controls = MapsGoogleMapsUtils::createControlsString($this->controls); |
61 | | - |
62 | | - $this->autozoom = MapsGoogleMapsUtils::getAutozoomJSValue($this->autozoom); |
63 | | - |
64 | | - $markerItems = array(); |
65 | | - |
66 | | - // TODO: Refactor up |
67 | | - foreach ($this->markerData as $markerData) { |
68 | | - $lat = $markerData['lat']; |
69 | | - $lon = $markerData['lon']; |
70 | | - |
71 | | - $title = array_key_exists('title', $markerData) ? $markerData['title'] : $this->title; |
72 | | - $label = array_key_exists('label', $markerData) ? $markerData['label'] : $this->label; |
73 | | - |
74 | | - $title = str_replace("'", "\'", $title); |
75 | | - $label = str_replace("'", "\'", $label); |
76 | | - |
77 | | - $icon = array_key_exists('icon', $markerData) ? $markerData['icon'] : ''; |
78 | | - $markerItems[] = "getGMarkerData($lat, $lon, '$title', '$label', '$icon')"; |
79 | | - } |
80 | | - |
81 | | - $markersString = implode(',', $markerItems); |
82 | | - |
83 | | - $this->types = explode(",", $this->types); |
84 | | - |
85 | | - $typesString = MapsGoogleMapsUtils::createTypesString($this->types, $enableEarth); |
86 | | - |
87 | | - $this->output .=<<<END |
88 | | - |
89 | | -<div id="$this->mapName" class="$this->class" style="$this->style" ></div> |
90 | | -<script type="$wgJsMimeType"> /*<![CDATA[*/ |
91 | | -addLoadEvent( |
92 | | - initializeGoogleMap('$this->mapName', $this->width, $this->height, $this->centre_lat, $this->centre_lon, $this->zoom, $this->type, [$typesString], [$this->controls], $this->autozoom, [$markersString]) |
93 | | -); |
94 | | -/*]]>*/ </script> |
95 | | - |
96 | | -END; |
97 | | - |
98 | | - } |
99 | | - |
100 | | -} |
101 | | - |
| 13 | +$egMapsServices['googlemaps'] = array( |
| 14 | + 'pf' => array('class' => 'MapsGoogleMapsParserFunctions', 'file' => 'GoogleMaps/Maps_GoogleMapsParserFunctions.php', 'local' => true), |
| 15 | + 'classes' => array( |
| 16 | + array('class' => 'MapsGoogleMapsUtils', 'file' => 'GoogleMaps/Maps_GoogleMapsUtils.php', 'local' => true) |
| 17 | + ), |
| 18 | + 'aliases' => array('google', 'googlemap', 'gmap', 'gmaps'), |
| 19 | + 'parameters' => array( |
| 20 | + 'type' => array('map-type', 'map type'), |
| 21 | + 'types' => array('map-types', 'map types'), |
| 22 | + 'earth' => array(), |
| 23 | + 'autozoom' => array('auto zoom', 'mouse zoom', 'mousezoom'), |
| 24 | + 'class' => array(), |
| 25 | + 'style' => array() |
| 26 | + ) |
| 27 | + ); |
\ No newline at end of file |
Index: trunk/extensions/Maps/GoogleMaps/Maps_GoogleMapsUtils.php |
— | — | @@ -15,6 +15,8 @@ |
16 | 16 | |
17 | 17 | final class MapsGoogleMapsUtils { |
18 | 18 | |
| 19 | + const SERVICE_NAME = 'googlemaps'; |
| 20 | + |
19 | 21 | // http://code.google.com/apis/maps/documentation/reference.html#GMapType.G_NORMAL_MAP |
20 | 22 | // TODO: Add a true alliasing system? Might be overkill. |
21 | 23 | private static $mapTypes = array( |
Index: trunk/extensions/Maps/GoogleMaps/Maps_GoogleMapsParserFunctions.php |
— | — | @@ -0,0 +1,98 @@ |
| 2 | +<?php
|
| 3 | +
|
| 4 | +/**
|
| 5 | + * Class for handling the Maps parser functions with Google Maps
|
| 6 | + *
|
| 7 | + * @file Maps_GoogleMapsParserFunctions.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 | +final class MapsGoogleMapsParserFunctions extends MapsBaseMap {
|
| 18 | +
|
| 19 | + public $serviceName = MapsGoogleMapsUtils::SERVICE_NAME;
|
| 20 | +
|
| 21 | + /**
|
| 22 | + * @see MapsBaseMap::setMapSettings()
|
| 23 | + *
|
| 24 | + */
|
| 25 | + protected function setMapSettings() {
|
| 26 | + global $egMapsGoogleMapsZoom, $egMapsGoogleMapsPrefix;
|
| 27 | +
|
| 28 | + $this->defaultParams = MapsGoogleMapsUtils::getDefaultParams();
|
| 29 | +
|
| 30 | + $this->elementNamePrefix = $egMapsGoogleMapsPrefix;
|
| 31 | + $this->defaultZoom = $egMapsGoogleMapsZoom;
|
| 32 | + }
|
| 33 | +
|
| 34 | + /**
|
| 35 | + * @see MapsBaseMap::doMapServiceLoad()
|
| 36 | + *
|
| 37 | + */
|
| 38 | + protected function doMapServiceLoad() {
|
| 39 | + global $egGoogleMapsOnThisPage;
|
| 40 | +
|
| 41 | + MapsGoogleMapsUtils::addGMapDependencies($this->output);
|
| 42 | + $egGoogleMapsOnThisPage++;
|
| 43 | +
|
| 44 | + $this->elementNr = $egGoogleMapsOnThisPage;
|
| 45 | + }
|
| 46 | +
|
| 47 | + /**
|
| 48 | + * @see MapsBaseMap::addSpecificMapHTML()
|
| 49 | + *
|
| 50 | + */
|
| 51 | + public function addSpecificMapHTML() {
|
| 52 | + global $wgJsMimeType;
|
| 53 | +
|
| 54 | + $enableEarth = MapsGoogleMapsUtils::getEarthValue($this->earth);
|
| 55 | +
|
| 56 | + $this->type = MapsGoogleMapsUtils::getGMapType($this->type, true);
|
| 57 | +
|
| 58 | + $this->controls = MapsGoogleMapsUtils::createControlsString($this->controls);
|
| 59 | +
|
| 60 | + $this->autozoom = MapsGoogleMapsUtils::getAutozoomJSValue($this->autozoom);
|
| 61 | +
|
| 62 | + $markerItems = array();
|
| 63 | +
|
| 64 | + // TODO: Refactor up
|
| 65 | + foreach ($this->markerData as $markerData) {
|
| 66 | + $lat = $markerData['lat'];
|
| 67 | + $lon = $markerData['lon'];
|
| 68 | +
|
| 69 | + $title = array_key_exists('title', $markerData) ? $markerData['title'] : $this->title;
|
| 70 | + $label = array_key_exists('label', $markerData) ? $markerData['label'] : $this->label;
|
| 71 | +
|
| 72 | + $title = str_replace("'", "\'", $title);
|
| 73 | + $label = str_replace("'", "\'", $label);
|
| 74 | +
|
| 75 | + $icon = array_key_exists('icon', $markerData) ? $markerData['icon'] : '';
|
| 76 | + $markerItems[] = "getGMarkerData($lat, $lon, '$title', '$label', '$icon')";
|
| 77 | + }
|
| 78 | +
|
| 79 | + $markersString = implode(',', $markerItems);
|
| 80 | +
|
| 81 | + $this->types = explode(",", $this->types);
|
| 82 | +
|
| 83 | + $typesString = MapsGoogleMapsUtils::createTypesString($this->types, $enableEarth);
|
| 84 | +
|
| 85 | + $this->output .=<<<END
|
| 86 | +
|
| 87 | +<div id="$this->mapName" class="$this->class" style="$this->style" ></div>
|
| 88 | +<script type="$wgJsMimeType"> /*<![CDATA[*/
|
| 89 | +addLoadEvent(
|
| 90 | + initializeGoogleMap('$this->mapName', $this->width, $this->height, $this->centre_lat, $this->centre_lon, $this->zoom, $this->type, [$typesString], [$this->controls], $this->autozoom, [$markersString])
|
| 91 | +);
|
| 92 | +/*]]>*/ </script>
|
| 93 | +
|
| 94 | +END;
|
| 95 | +
|
| 96 | + }
|
| 97 | +
|
| 98 | +}
|
| 99 | +
|
Index: trunk/extensions/Maps/Maps_MapFeature.php |
— | — | @@ -52,7 +52,6 @@ |
53 | 53 | /** |
54 | 54 | * Sets the map properties as class fields. |
55 | 55 | * |
56 | | - * @param unknown_type $map |
57 | 56 | */ |
58 | 57 | protected function manageMapProperties($mapProperties, $className) { |
59 | 58 | global $egMapsServices; |
Index: trunk/extensions/Maps/YahooMaps/Maps_YahooMaps.php |
— | — | @@ -1,7 +1,7 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | /** |
5 | | - * Class for handling the Maps parser functions with Yahoo! Maps |
| 5 | + * This file holds the general information for the Yahoo! Maps service |
6 | 6 | * |
7 | 7 | * @file Maps_YahooMaps.php |
8 | 8 | * @ingroup Maps |
— | — | @@ -9,90 +9,15 @@ |
10 | 10 | * @author Jeroen De Dauw |
11 | 11 | */ |
12 | 12 | |
13 | | -if( !defined( 'MEDIAWIKI' ) ) { |
14 | | - die( 'Not an entry point.' ); |
15 | | -} |
16 | | - |
17 | | -class MapsYahooMaps extends MapsBaseMap { |
18 | | - |
19 | | - const SERVICE_NAME = 'yahoomaps'; |
20 | | - |
21 | | - public $serviceName = self::SERVICE_NAME; |
22 | | - |
23 | | - /** |
24 | | - * @see MapsBaseMap::setFormInputSettings() |
25 | | - * |
26 | | - */ |
27 | | - protected function setMapSettings() { |
28 | | - global $egMapsYahooMapsZoom, $egMapsYahooMapsPrefix; |
29 | | - |
30 | | - $this->defaultParams = MapsYahooMapsUtils::getDefaultParams(); |
31 | | - |
32 | | - $this->elementNamePrefix = $egMapsYahooMapsPrefix; |
33 | | - $this->defaultZoom = $egMapsYahooMapsZoom; |
34 | | - } |
35 | | - |
36 | | - /** |
37 | | - * @see MapsBaseMap::doMapServiceLoad() |
38 | | - * |
39 | | - */ |
40 | | - protected function doMapServiceLoad() { |
41 | | - global $egYahooMapsOnThisPage; |
42 | | - |
43 | | - MapsYahooMapsUtils::addYMapDependencies($this->output); |
44 | | - $egYahooMapsOnThisPage++; |
45 | | - |
46 | | - $this->elementNr = $egYahooMapsOnThisPage; |
47 | | - } |
48 | | - |
49 | | - /** |
50 | | - * @see MapsBaseMap::addSpecificMapHTML() |
51 | | - * |
52 | | - */ |
53 | | - public function addSpecificMapHTML() { |
54 | | - global $wgJsMimeType; |
55 | | - |
56 | | - $this->type = MapsYahooMapsUtils::getYMapType($this->type, true); |
57 | | - |
58 | | - $this->controls = MapsYahooMapsUtils::createControlsString($this->controls); |
59 | | - |
60 | | - MapsUtils::makePxValue($this->width); |
61 | | - MapsUtils::makePxValue($this->height); |
62 | | - |
63 | | - $this->autozoom = MapsYahooMapsUtils::getAutozoomJSValue($this->autozoom); |
64 | | - |
65 | | - $markerItems = array(); |
66 | | - |
67 | | - // TODO: Refactor up |
68 | | - foreach ($this->markerData as $markerData) { |
69 | | - $lat = $markerData['lat']; |
70 | | - $lon = $markerData['lon']; |
71 | | - |
72 | | - $title = array_key_exists('title', $markerData) ? $markerData['title'] : $this->title; |
73 | | - $label = array_key_exists('label', $markerData) ? $markerData['label'] : $this->label; |
74 | | - |
75 | | - $title = str_replace("'", "\'", $title); |
76 | | - $label = str_replace("'", "\'", $label); |
77 | | - |
78 | | - $icon = array_key_exists('icon', $markerData) ? $markerData['icon'] : ''; |
79 | | - $markerItems[] = "getYMarkerData($lat, $lon, '$title', '$label', '$icon')"; |
80 | | - } |
81 | | - |
82 | | - $markersString = implode(',', $markerItems); |
83 | | - |
84 | | - $this->types = explode(",", $this->types); |
85 | | - |
86 | | - $typesString = MapsYahooMapsUtils::createTypesString($this->types); |
87 | | - |
88 | | - $this->output .= <<<END |
89 | | - <div id="$this->mapName" style="width: $this->width; height: $this->height;"></div> |
90 | | - |
91 | | - <script type="$wgJsMimeType">/*<![CDATA[*/ |
92 | | - addLoadEvent( |
93 | | - initializeYahooMap('$this->mapName', $this->centre_lat, $this->centre_lon, $this->zoom, $this->type, [$typesString], [$this->controls], $this->autozoom, [$markersString]) |
94 | | - ); |
95 | | - /*]]>*/</script> |
96 | | -END; |
97 | | - } |
98 | | - |
99 | | -} |
| 13 | +$egMapsServices['yahoomaps'] = array( |
| 14 | + 'pf' => array('class' => 'MapsYahooMapsParserFunctions', 'file' => 'YahooMaps/Maps_YahooMapsParserFunctions.php', 'local' => true), |
| 15 | + 'classes' => array( |
| 16 | + array('class' => 'MapsYahooMapsUtils', 'file' => 'YahooMaps/Maps_YahooMapsUtils.php', 'local' => true) |
| 17 | + ), |
| 18 | + 'aliases' => array('yahoo', 'yahoomap', 'ymap', 'ymaps'), |
| 19 | + 'parameters' => array( |
| 20 | + 'type' => array('map-type'), |
| 21 | + 'types' => array('map-types', 'map types'), |
| 22 | + 'autozoom' => array('auto zoom', 'mouse zoom', 'mousezoom') |
| 23 | + ) |
| 24 | + ); |
\ No newline at end of file |
Index: trunk/extensions/Maps/YahooMaps/Maps_YahooMapsUtils.php |
— | — | @@ -15,6 +15,8 @@ |
16 | 16 | |
17 | 17 | final class MapsYahooMapsUtils { |
18 | 18 | |
| 19 | + const SERVICE_NAME = 'yahoomaps'; |
| 20 | + |
19 | 21 | // http://developer.yahoo.com/maps/ajax |
20 | 22 | private static $mapTypes = array( |
21 | 23 | 'normal' => 'YAHOO_MAP_REG', |
Index: trunk/extensions/Maps/YahooMaps/Maps_YahooMapsParserFunctions.php |
— | — | @@ -0,0 +1,96 @@ |
| 2 | +<?php
|
| 3 | +
|
| 4 | +/**
|
| 5 | + * Class for handling the Maps parser functions with Yahoo! Maps
|
| 6 | + *
|
| 7 | + * @file Maps_YahooMapsParserFunctions.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 | +class MapsYahooMapsParserFunctions extends MapsBaseMap {
|
| 18 | +
|
| 19 | + public $serviceName = MapsYahooMapsUtils::SERVICE_NAME;
|
| 20 | +
|
| 21 | + /**
|
| 22 | + * @see MapsBaseMap::setFormInputSettings()
|
| 23 | + *
|
| 24 | + */
|
| 25 | + protected function setMapSettings() {
|
| 26 | + global $egMapsYahooMapsZoom, $egMapsYahooMapsPrefix;
|
| 27 | +
|
| 28 | + $this->defaultParams = MapsYahooMapsUtils::getDefaultParams();
|
| 29 | +
|
| 30 | + $this->elementNamePrefix = $egMapsYahooMapsPrefix;
|
| 31 | + $this->defaultZoom = $egMapsYahooMapsZoom;
|
| 32 | + }
|
| 33 | +
|
| 34 | + /**
|
| 35 | + * @see MapsBaseMap::doMapServiceLoad()
|
| 36 | + *
|
| 37 | + */
|
| 38 | + protected function doMapServiceLoad() {
|
| 39 | + global $egYahooMapsOnThisPage;
|
| 40 | +
|
| 41 | + MapsYahooMapsUtils::addYMapDependencies($this->output);
|
| 42 | + $egYahooMapsOnThisPage++;
|
| 43 | +
|
| 44 | + $this->elementNr = $egYahooMapsOnThisPage;
|
| 45 | + }
|
| 46 | +
|
| 47 | + /**
|
| 48 | + * @see MapsBaseMap::addSpecificMapHTML()
|
| 49 | + *
|
| 50 | + */
|
| 51 | + public function addSpecificMapHTML() {
|
| 52 | + global $wgJsMimeType;
|
| 53 | +
|
| 54 | + $this->type = MapsYahooMapsUtils::getYMapType($this->type, true);
|
| 55 | +
|
| 56 | + $this->controls = MapsYahooMapsUtils::createControlsString($this->controls);
|
| 57 | +
|
| 58 | + MapsUtils::makePxValue($this->width);
|
| 59 | + MapsUtils::makePxValue($this->height);
|
| 60 | +
|
| 61 | + $this->autozoom = MapsYahooMapsUtils::getAutozoomJSValue($this->autozoom);
|
| 62 | +
|
| 63 | + $markerItems = array();
|
| 64 | +
|
| 65 | + // TODO: Refactor up
|
| 66 | + foreach ($this->markerData as $markerData) {
|
| 67 | + $lat = $markerData['lat'];
|
| 68 | + $lon = $markerData['lon'];
|
| 69 | +
|
| 70 | + $title = array_key_exists('title', $markerData) ? $markerData['title'] : $this->title;
|
| 71 | + $label = array_key_exists('label', $markerData) ? $markerData['label'] : $this->label;
|
| 72 | +
|
| 73 | + $title = str_replace("'", "\'", $title);
|
| 74 | + $label = str_replace("'", "\'", $label);
|
| 75 | +
|
| 76 | + $icon = array_key_exists('icon', $markerData) ? $markerData['icon'] : '';
|
| 77 | + $markerItems[] = "getYMarkerData($lat, $lon, '$title', '$label', '$icon')";
|
| 78 | + }
|
| 79 | +
|
| 80 | + $markersString = implode(',', $markerItems);
|
| 81 | +
|
| 82 | + $this->types = explode(",", $this->types);
|
| 83 | +
|
| 84 | + $typesString = MapsYahooMapsUtils::createTypesString($this->types);
|
| 85 | +
|
| 86 | + $this->output .= <<<END
|
| 87 | + <div id="$this->mapName" style="width: $this->width; height: $this->height;"></div>
|
| 88 | +
|
| 89 | + <script type="$wgJsMimeType">/*<![CDATA[*/
|
| 90 | + addLoadEvent(
|
| 91 | + initializeYahooMap('$this->mapName', $this->centre_lat, $this->centre_lon, $this->zoom, $this->type, [$typesString], [$this->controls], $this->autozoom, [$markersString])
|
| 92 | + );
|
| 93 | + /*]]>*/</script>
|
| 94 | +END;
|
| 95 | + }
|
| 96 | +
|
| 97 | +}
|
Index: trunk/extensions/Maps/Maps_Settings.php |
— | — | @@ -37,12 +37,18 @@ |
38 | 38 | # The array element name contains an abbriviation, used for code references, |
39 | 39 | # and in the service data arrays, the value is the human readible version for displaying purpouses. |
40 | 40 | if (empty($egMapsAvailableFeatures)) $egMapsAvailableFeatures = array(); |
41 | | -$egMapsAvailableFeatures['pf'] = 'Parser Function'; |
42 | 41 | |
| 42 | +$egMapsAvailableFeatures['pf'] = array( |
| 43 | + 'name' => 'Parser Function', |
| 44 | + 'class' => 'MapsParserFunctions', |
| 45 | + 'file' => 'ParserFunctions/Maps_ParserFunctions.php', |
| 46 | + 'local' => true |
| 47 | + ); |
43 | 48 | |
44 | 49 | |
45 | 50 | |
46 | 51 | |
| 52 | + |
47 | 53 | # Map services configuration |
48 | 54 | # Note: You can not use aliases in the setting. Use the main service names. |
49 | 55 | |
Index: trunk/extensions/Maps/Geocoders/Maps_BaseGeocoder.php |
— | — | @@ -0,0 +1,118 @@ |
| 2 | +<?php
|
| 3 | +
|
| 4 | +/**
|
| 5 | + * MapsBaseGeocoder is an abstract class inherited by the geocoding classes
|
| 6 | + *
|
| 7 | + * @file Maps_BaseGeocoder.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 | +abstract class MapsBaseGeocoder {
|
| 18 | +
|
| 19 | + /**
|
| 20 | + * Returns an array containing the geocoded latitude (lat) and
|
| 21 | + * longitude (lon) of the provided address, or false in case the
|
| 22 | + * geocoding fails.
|
| 23 | + *
|
| 24 | + * @param string $address
|
| 25 | + */
|
| 26 | + public abstract static function geocode($address);
|
| 27 | +
|
| 28 | + /**
|
| 29 | + * Returns the content of the requested file, or false when the connection fails
|
| 30 | + *
|
| 31 | + * @param string $requestURL
|
| 32 | + * @return string or false
|
| 33 | + */
|
| 34 | + protected static function GetResponse($requestURL) {
|
| 35 | + // Attempt to get CURL response
|
| 36 | + $response = self::GetCurlResponse($requestURL);
|
| 37 | +
|
| 38 | + // Attempt to get response using fopen when the CURL request failed
|
| 39 | + if (!$response) $response = self::GetUrlResponse($requestURL);
|
| 40 | +
|
| 41 | + return $response;
|
| 42 | + }
|
| 43 | +
|
| 44 | + /**
|
| 45 | + * Attempts to get the contents of a file via cURL request and
|
| 46 | + * returns it, or false when the attempt fails.
|
| 47 | + *
|
| 48 | + * @param string $requestURL
|
| 49 | + * @return string or false
|
| 50 | + */
|
| 51 | + protected static function GetCurlResponse($requestURL) {
|
| 52 | + if (function_exists("curl_init")) {
|
| 53 | + try {
|
| 54 | + //Set up a CURL request, telling it not to spit back headers, and to throw out a user agent.
|
| 55 | + $ch = curl_init();
|
| 56 | +
|
| 57 | + curl_setopt($ch, CURLOPT_URL, $requestURL);
|
| 58 | + curl_setopt($ch, CURLOPT_HEADER, 0); //Change this to a 1 to return headers
|
| 59 | + curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
|
| 60 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
| 61 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
| 62 | +
|
| 63 | + $result = curl_exec($ch);
|
| 64 | + curl_close($ch);
|
| 65 | +
|
| 66 | + return $result;
|
| 67 | + }
|
| 68 | + catch(Exception $ex) {
|
| 69 | + return false;
|
| 70 | + }
|
| 71 | + }
|
| 72 | + else {
|
| 73 | + return false;
|
| 74 | + }
|
| 75 | + }
|
| 76 | +
|
| 77 | + /**
|
| 78 | + * Attempts to get the contents of a file via fopen and
|
| 79 | + * returns it, or false when the attempt fails.
|
| 80 | + *
|
| 81 | + * @param string $requestURL
|
| 82 | + * @return string or false
|
| 83 | + */
|
| 84 | + protected static function GetUrlResponse($requestURL) {
|
| 85 | + if (function_exists('fopen')) {
|
| 86 | + try {
|
| 87 | + if ($handle = fopen($requestURL, 'r')) {
|
| 88 | + $result = fread($handle, 10000);
|
| 89 | + fclose($handle);
|
| 90 | + }
|
| 91 | + else { // When the request fails, return false
|
| 92 | + $result = false;
|
| 93 | + }
|
| 94 | + }
|
| 95 | + catch(Exception $ex) {
|
| 96 | + $result = false;
|
| 97 | + }
|
| 98 | + }
|
| 99 | + else {
|
| 100 | + $result = false;
|
| 101 | + }
|
| 102 | + return $result;
|
| 103 | + }
|
| 104 | +
|
| 105 | + /**
|
| 106 | + * Gets the contents of the first XML tag with the provided name,
|
| 107 | + * returns false when no matching element is found.
|
| 108 | + *
|
| 109 | + * @param string $xml
|
| 110 | + * @param string $tagName
|
| 111 | + * @return string or false
|
| 112 | + */
|
| 113 | + protected static function getXmlElementValue($xml, $tagName) {
|
| 114 | + $match = array();
|
| 115 | + preg_match("/<$tagName>(.*?)<\/$tagName>/", $xml, $match);
|
| 116 | + return count($match) > 1 ? $match[1] : false;
|
| 117 | + }
|
| 118 | +
|
| 119 | +}
|
Index: trunk/extensions/Maps/Geocoders/Maps_Geocoder.php |
— | — | @@ -0,0 +1,190 @@ |
| 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 | +final class MapsGeocoder {
|
| 23 | +
|
| 24 | + /**
|
| 25 | + * Holds if geocoded data should be cached or not.
|
| 26 | + *
|
| 27 | + * @var boolean
|
| 28 | + */
|
| 29 | + private static $mEnableCache = true;
|
| 30 | +
|
| 31 | + /**
|
| 32 | + * The geocoder cache, holding geocoded data when enabled.
|
| 33 | + *
|
| 34 | + * @var array
|
| 35 | + */
|
| 36 | + private static $mGeocoderCache = array();
|
| 37 | +
|
| 38 | + /**
|
| 39 | + * Handler for the geocode parser function. Returns the latitude and longitude
|
| 40 | + * for the provided address, or an empty string, when the geocoding fails.
|
| 41 | + *
|
| 42 | + * @param unknown_type $parser
|
| 43 | + * @param string $address The address to geocode.
|
| 44 | + * @param string $service Optional. The geocoding service to use.
|
| 45 | + * @param string $mappingService Optional. The mapping service that will use the geocoded data.
|
| 46 | + * @return string
|
| 47 | + */
|
| 48 | + public static function renderGeocoder($parser, $address, $service = '', $mappingService = '') {
|
| 49 | + $geovalues = MapsGeocoder::geocode($address, $service, $mappingService);
|
| 50 | + return $geovalues ? $geovalues['lat'].', '.$geovalues['lon'] : '';
|
| 51 | + }
|
| 52 | +
|
| 53 | + /**
|
| 54 | + * Handler for the geocode parser function. Returns the latitude
|
| 55 | + * for the provided address, or an empty string, when the geocoding fails.
|
| 56 | + *
|
| 57 | + * @param unknown_type $parser
|
| 58 | + * @param string $address The address to geocode.
|
| 59 | + * @param string $service Optional. The geocoding service to use.
|
| 60 | + * @param string $mappingService Optional. The mapping service that will use the geocoded data.
|
| 61 | + * @return string
|
| 62 | + */
|
| 63 | + public static function renderGeocoderLat(&$parser, $address, $service = '', $mappingService = '') {
|
| 64 | + $geovalues = MapsGeocoder::geocode($address, $service, $mappingService);
|
| 65 | + return $geovalues ? $geovalues['lat'] : '';
|
| 66 | + }
|
| 67 | +
|
| 68 | + /**
|
| 69 | + * Handler for the geocode parser function. Returns the longitude
|
| 70 | + * for the provided address, or an empty string, when the geocoding fails.
|
| 71 | + *
|
| 72 | + * @param unknown_type $parser
|
| 73 | + * @param string $address The address to geocode.
|
| 74 | + * @param string $service Optional. The geocoding service to use.
|
| 75 | + * @param string $mappingService Optional. The mapping service that will use the geocoded data.
|
| 76 | + * @return string
|
| 77 | + */
|
| 78 | + public static function renderGeocoderLng(&$parser, $address, $service = '', $mappingService = '') {
|
| 79 | + $geovalues = MapsGeocoder::geocode($address, $service, $mappingService);
|
| 80 | + return $geovalues ? $geovalues['lon'] : '';
|
| 81 | + }
|
| 82 | +
|
| 83 | + /**
|
| 84 | + * Geocodes an address with the provided geocoding service and returns the result
|
| 85 | + * as a string with the optionally provided format, or false when the geocoding failed.
|
| 86 | + *
|
| 87 | + * @param string $address
|
| 88 | + * @param string $service
|
| 89 | + * @param string $mappingService
|
| 90 | + * @param string $format
|
| 91 | + * @return formatted coordinate string or false
|
| 92 | + */
|
| 93 | + public static function geocodeToString($address, $service = '', $mappingService = '', $format = '%1$s, %2$s') {
|
| 94 | + $geovalues = MapsGeocoder::geocode($address, $service, $mappingService);
|
| 95 | + return $geovalues ? sprintf($format, $geovalues['lat'], $geovalues['lon']) : false;
|
| 96 | + }
|
| 97 | +
|
| 98 | + /**
|
| 99 | + * Geocodes an address with the provided geocoding service and returns the result
|
| 100 | + * as an array, or false when the geocoding failed.
|
| 101 | + *
|
| 102 | + * @param string $address
|
| 103 | + * @param string $service
|
| 104 | + * @param string $mappingService
|
| 105 | + * @return array with coordinates or false
|
| 106 | + */
|
| 107 | + private static function geocode($address, $service, $mappingService) {
|
| 108 | + global $egMapsAvailableGeoServices, $egMapsDefaultGeoService;
|
| 109 | +
|
| 110 | + // If the adress is already in the cache and the cache is enabled, return the coordinates
|
| 111 | + if (self::$mEnableCache && array_key_exists($address, MapsGeocoder::$mGeocoderCache)) {
|
| 112 | + return self::$mGeocoderCache[$address];
|
| 113 | + }
|
| 114 | +
|
| 115 | + $coordinates = false;
|
| 116 | +
|
| 117 | + $service = self::getValidGeoService($service, $mappingService);
|
| 118 | +
|
| 119 | + // If not, use the selected geocoding service to geocode the provided adress
|
| 120 | + switch(strtolower($service)) {
|
| 121 | + case 'google':
|
| 122 | + self::addAutoloadClassIfNeeded('MapsGoogleGeocoder', 'Maps_GoogleGeocoder.php');
|
| 123 | + $coordinates = MapsGoogleGeocoder::geocode($address);
|
| 124 | + break;
|
| 125 | + case 'yahoo':
|
| 126 | + self::addAutoloadClassIfNeeded('MapsYahooGeocoder', 'Maps_YahooGeocoder.php');
|
| 127 | + $coordinates = MapsYahooGeocoder::geocode($address);
|
| 128 | + break;
|
| 129 | + case 'geonames':
|
| 130 | + self::addAutoloadClassIfNeeded('MapsGeonamesGeocoder', 'Maps_GeonamesGeocoder.php');
|
| 131 | + $coordinates = MapsGeonamesGeocoder::geocode($address);
|
| 132 | + break;
|
| 133 | + }
|
| 134 | +
|
| 135 | + // Add the obtained coordinates to the cache when there is a result and the cache is enabled
|
| 136 | + if (self::$mEnableCache && $coordinates) {
|
| 137 | + MapsGeocoder::$mGeocoderCache[$address] = $coordinates;
|
| 138 | + }
|
| 139 | +
|
| 140 | + return $coordinates;
|
| 141 | + }
|
| 142 | +
|
| 143 | + /**
|
| 144 | + * Add a geocoder class to the $wgAutoloadClasses array when it's not present yet.
|
| 145 | + *
|
| 146 | + * @param string $className
|
| 147 | + * @param string $fileName
|
| 148 | + */
|
| 149 | + private static function addAutoloadClassIfNeeded($className, $fileName) {
|
| 150 | + global $wgAutoloadClasses, $egMapsIP;
|
| 151 | + if (!array_key_exists($className, $wgAutoloadClasses)) $wgAutoloadClasses[$className] = $egMapsIP . '/Geocoders/' . $fileName;
|
| 152 | + }
|
| 153 | +
|
| 154 | + /**
|
| 155 | + * Makes sure that the geo service is one of the available ones.
|
| 156 | + * Also enforces licencing restrictions when no geocoding service is explicitly provided.
|
| 157 | + *
|
| 158 | + * @param string $service
|
| 159 | + * @param string $mappingService
|
| 160 | + * @return string
|
| 161 | + */
|
| 162 | + private static function getValidGeoService($service, $mappingService) {
|
| 163 | + global $egMapsAvailableGeoServices, $egMapsDefaultGeoService;
|
| 164 | +
|
| 165 | + if (strlen($service) < 1) {
|
| 166 | +
|
| 167 | + // Set the default geocoding services.
|
| 168 | + // Note that googlemaps and yahoomaps are excetions to the general rule due to licencing.
|
| 169 | + switch ($mappingService) {
|
| 170 | + case 'googlemaps' :
|
| 171 | + $service = 'google';
|
| 172 | + break;
|
| 173 | + case 'yahoomaps' :
|
| 174 | + $service = 'yahoo';
|
| 175 | + break;
|
| 176 | + default :
|
| 177 | + $service = $egMapsDefaultGeoService;
|
| 178 | + break;
|
| 179 | + }
|
| 180 | +
|
| 181 | + }
|
| 182 | + else {
|
| 183 | + if(!in_array($service, $egMapsAvailableGeoServices)) $service = $egMapsDefaultGeoService;
|
| 184 | + }
|
| 185 | +
|
| 186 | + return $service;
|
| 187 | + }
|
| 188 | +}
|
| 189 | +
|
| 190 | +
|
| 191 | +
|