Index: trunk/extensions/SemanticGoogleMaps/SemanticGoogleMaps.php |
— | — | @@ -26,7 +26,7 @@ |
27 | 27 | // credits |
28 | 28 | $wgExtensionCredits['parserhook'][] = array( |
29 | 29 | 'name' => 'Semantic Google Maps', |
30 | | - 'version' => '0.4', |
| 30 | + 'version' => '0.4.1', |
31 | 31 | 'author' => array( 'Robert Buzink', 'Yaron Koren' ), |
32 | 32 | 'url' => 'http://www.mediawiki.org/wiki/Extension:Semantic_Google_Maps', |
33 | 33 | 'description' => 'Allows users to add Google Maps to wiki pages based on structured data', |
— | — | @@ -37,9 +37,9 @@ |
38 | 38 | $wgParser->setFunctionHook( 'semantic_google_map', 'sgmFunctionRender' ); |
39 | 39 | global $sfgFormPrinter; |
40 | 40 | if ($sfgFormPrinter) { |
41 | | - $sfgFormPrinter->setInputTypeHook('googlemap', 'sgmInputHTML', array()); |
| 41 | + $sfgFormPrinter->setInputTypeHook('googlemap', array('SGMUtils', 'formInputHTML'), array()); |
42 | 42 | // for backwards compatibility |
43 | | - $sfgFormPrinter->setInputTypeHook('coordinatesmap', 'sgmInputHTML', array()); |
| 43 | + $sfgFormPrinter->setInputTypeHook('coordinatesmap', array('SGMUtils', 'formInputHTML'), array()); |
44 | 44 | } |
45 | 45 | |
46 | 46 | include_once('SGM_QueryPrinter.php'); |
— | — | @@ -72,7 +72,7 @@ |
73 | 73 | if (!$wgGoogleMapsOnThisPage) {$wgGoogleMapsOnThisPage = 0;} |
74 | 74 | $wgGoogleMapsOnThisPage++; |
75 | 75 | |
76 | | - list($lat, $lon) = sgmGetLatLon($coordinates); |
| 76 | + list($lat, $lon) = SGMUtils::getLatLon($coordinates); |
77 | 77 | |
78 | 78 | $output =<<<END |
79 | 79 | <script src="http://maps.google.com/maps?file=api&v=2&key=$wgGoogleMapsKey" type="$wgJsMimeType"></script> |
— | — | @@ -86,150 +86,153 @@ |
87 | 87 | |
88 | 88 | } |
89 | 89 | |
90 | | -function sgmGetLatLon($param2) { |
91 | | - $coordinates = preg_split("/,/", $param2); |
92 | | - if (count($coordinates) == 2) { |
93 | | - $lat = sm_convert_coord($coordinates[0]); |
94 | | - $lon = sm_convert_coord($coordinates[1]); |
95 | | - return array($lat, $lon); |
| 90 | +class SGMUtils { |
| 91 | + |
| 92 | + function getLatLon($param2) { |
| 93 | + $coordinates = preg_split("/,/", $param2); |
| 94 | + if (count($coordinates) == 2) { |
| 95 | + $lat = SGMUtils::convertCoord($coordinates[0]); |
| 96 | + $lon = SGMUtils::convertCoord($coordinates[1]); |
| 97 | + return array($lat, $lon); |
| 98 | + } |
| 99 | + return array(null, null); |
96 | 100 | } |
97 | | - return array(); |
98 | | -} |
99 | 101 | |
100 | | -function sm_degree2decimal($deg_coord="") { |
101 | | - $dpos=strpos($deg_coord,'°'); |
102 | | - $mpos=strpos($deg_coord,'.'); |
103 | | - $spos=strpos($deg_coord,'"'); |
104 | | - $mlen=(($mpos-$dpos)-1); |
105 | | - $slen=(($spos-$mpos)-1); |
106 | | - $direction=substr(strrev($deg_coord),0,1); |
107 | | - $degrees=substr($deg_coord,0,$dpos); |
108 | | - $minutes=substr($deg_coord,$dpos+1,$mlen); |
109 | | - $seconds=substr($deg_coord,$mpos+1,$slen); |
110 | | - $seconds=($seconds/60); |
111 | | - $minutes=($minutes+$seconds); |
112 | | - $minutes=($minutes/60); |
113 | | - $decimal=($degrees+$minutes); |
114 | | - //South latitudes and West longitudes need to return a negative result |
115 | | - if (($direction=="S") or ($direction=="W")) { |
116 | | - $decimal=$decimal*(-1); |
| 102 | + function degree2Decimal($deg_coord="") { |
| 103 | + $dpos=strpos($deg_coord,'°'); |
| 104 | + $mpos=strpos($deg_coord,'.'); |
| 105 | + $spos=strpos($deg_coord,'"'); |
| 106 | + $mlen=(($mpos-$dpos)-1); |
| 107 | + $slen=(($spos-$mpos)-1); |
| 108 | + $direction=substr(strrev($deg_coord),0,1); |
| 109 | + $degrees=substr($deg_coord,0,$dpos); |
| 110 | + $minutes=substr($deg_coord,$dpos+1,$mlen); |
| 111 | + $seconds=substr($deg_coord,$mpos+1,$slen); |
| 112 | + $seconds=($seconds/60); |
| 113 | + $minutes=($minutes+$seconds); |
| 114 | + $minutes=($minutes/60); |
| 115 | + $decimal=($degrees+$minutes); |
| 116 | + //South latitudes and West longitudes need to return a negative result |
| 117 | + if (($direction=="S") or ($direction=="W")) { |
| 118 | + $decimal *= -1; |
| 119 | + } |
| 120 | + return $decimal; |
117 | 121 | } |
118 | | - return $decimal; |
119 | | -} |
120 | 122 | |
121 | | -function sm_decdegree2decimal($deg_coord="") { |
122 | | - $direction=substr(strrev($deg_coord),0,1); |
123 | | - $decimal=floatval($deg_coord); |
124 | | - if (($direction=="S") or ($direction=="W")) { |
125 | | - $decimal=$decimal*(-1); |
| 123 | + function decDegree2Decimal($deg_coord = "") { |
| 124 | + $direction = substr(strrev($deg_coord), 0, 1); |
| 125 | + $decimal = floatval($deg_coord); |
| 126 | + if (($direction == "S") or ($direction == "W")) { |
| 127 | + $decimal *= -1; |
| 128 | + } |
| 129 | + return $decimal; |
126 | 130 | } |
127 | | - return $decimal; |
128 | | -} |
129 | 131 | |
130 | | -function sm_convert_coord ($deg_coord="") { |
131 | | - if (preg_match('/°/', $deg_coord)) { |
132 | | - if (preg_match('/"/', $deg_coord)) { |
133 | | - $deg_coord = sm_degree2decimal($deg_coord); |
134 | | - } else { |
135 | | - $deg_coord = sm_decdegree2decimal($deg_coord); |
| 132 | + function convertCoord($deg_coord = "") { |
| 133 | + if (preg_match('/°/', $deg_coord)) { |
| 134 | + if (preg_match('/"/', $deg_coord)) { |
| 135 | + return SGMUtils::degree2Decimal($deg_coord); |
| 136 | + } else { |
| 137 | + return SGMUtils::decDegree2Decimal($deg_coord); |
| 138 | + } |
136 | 139 | } |
| 140 | + return $deg_coord; |
137 | 141 | } |
138 | | - return $deg_coord; |
139 | | -} |
140 | 142 | |
141 | | -function sgmLatDecimal2Degree($decimal) { |
142 | | - if ($decimal < 0) { |
143 | | - return abs($decimal) . "° S"; |
| 143 | + function latDecimal2Degree($decimal) { |
| 144 | + if ($decimal < 0) { |
| 145 | + return abs($decimal) . "° S"; |
144 | 146 | } else { |
145 | | - return $decimal . "° N"; |
| 147 | + return $decimal . "° N"; |
146 | 148 | } |
147 | 149 | } |
148 | 150 | |
149 | | -function sgmLonDecimal2Degree($decimal) { |
150 | | - if ($decimal < 0) { |
151 | | - return abs($decimal) . "° W"; |
| 151 | + function lonDecimal2Degree($decimal) { |
| 152 | + if ($decimal < 0) { |
| 153 | + return abs($decimal) . "° W"; |
152 | 154 | } else { |
153 | | - return $decimal . "° E"; |
| 155 | + return $decimal . "° E"; |
154 | 156 | } |
155 | 157 | } |
156 | 158 | |
157 | | -// the function that outputs the custom form html |
158 | | -function sgmInputHTML($coordinates, $input_name, $is_mandatory, $is_disabled, $field_args) { |
159 | | - global $gTabIndex, $gDisabledText, $wgJsMimeType, $wgGoogleMapsKey, $wgGoogleMapsOnThisPage; |
| 159 | + // the function that outputs the custom form html |
| 160 | + function formInputHTML($coordinates, $input_name, $is_mandatory, $is_disabled, $field_args) { |
| 161 | + global $gTabIndex, $gDisabledText, $wgJsMimeType; |
| 162 | + global $wgGoogleMapsKey, $wgGoogleMapsOnThisPage; |
160 | 163 | |
161 | | - // default values |
162 | | - $flat = 0; |
163 | | - $flon = 0; |
| 164 | + // default values |
| 165 | + $flat = 0; |
| 166 | + $flon = 0; |
164 | 167 | |
165 | | - if ($coordinates) { |
166 | | - // can show up here either as an array or a string, depending on |
167 | | - // whether it came from user input or a wiki page |
168 | | - if (is_array($coordinates)) { |
169 | | - // todo if relevant |
| 168 | + if ($coordinates) { |
| 169 | + // can show up here either as an array or a string, |
| 170 | + // depending on whether it came from user input or a wiki page |
| 171 | + if (is_array($coordinates)) { |
| 172 | + // todo if relevant |
| 173 | + } else { |
| 174 | + list($flat, $flon) = SGMUtils::getLatLon($coordinates); |
| 175 | + } |
| 176 | + $zoom = '14'; |
170 | 177 | } else { |
171 | | - list($flat, $flon) = sgmGetLatLon($coordinates); |
| 178 | + // if there's no starting value, get the value for the |
| 179 | + // map center and zoom from the form input, if they exist |
| 180 | + if (array_key_exists('center', $field_args)) { |
| 181 | + list($flat, $flon) = SGMUtils::getLatLon($field_args['center']); |
| 182 | + $zoom = '14'; |
| 183 | + } else { |
| 184 | + $zoom = '0'; |
| 185 | + } |
| 186 | + if (array_key_exists('zoom', $field_args)) { |
| 187 | + $zoom = $field_args['zoom']; |
| 188 | + } |
172 | 189 | } |
173 | | - $zoom = '14'; |
174 | | - } else { |
175 | | - // if there's no starting value, get the value for the map |
176 | | - // center and zoom from the form input, if they exist |
177 | | - if (array_key_exists('center', $field_args)) { |
178 | | - list($flat, $flon) = sgmGetLatLon($field_args['center']); |
179 | | - $zoom = '14'; |
| 190 | + if (!$wgGoogleMapsOnThisPage) {$wgGoogleMapsOnThisPage = 0;} |
| 191 | + $wgGoogleMapsOnThisPage++; |
| 192 | + if (array_key_exists('width', $field_args)) { |
| 193 | + $width = $field_args['width']; |
180 | 194 | } else { |
181 | | - $zoom = '0'; |
| 195 | + $width = '200'; |
182 | 196 | } |
183 | | - if (array_key_exists('zoom', $field_args)) { |
184 | | - $zoom = $field_args['zoom']; |
| 197 | + if (array_key_exists('height', $field_args)) { |
| 198 | + $height = $field_args['height']; |
| 199 | + } else { |
| 200 | + $height = '200'; |
185 | 201 | } |
186 | | - } |
187 | | - if (!$wgGoogleMapsOnThisPage) {$wgGoogleMapsOnThisPage = 0;} |
188 | | - $wgGoogleMapsOnThisPage++; |
189 | | - if (array_key_exists('width', $field_args)) { |
190 | | - $width = $field_args['width']; |
191 | | - } else { |
192 | | - $width = '200'; |
193 | | - } |
194 | | - if (array_key_exists('height', $field_args)) { |
195 | | - $height = $field_args['height']; |
196 | | - } else { |
197 | | - $height = '200'; |
198 | | - } |
199 | | - $class = 'sm_map'; |
200 | | - if (array_key_exists('map type', $field_args)) { |
201 | | - $type = $field_args['map type']; |
202 | | - } else { |
203 | | - $type = 'G_NORMAL_MAP'; |
204 | | - } |
205 | | - if (array_key_exists('map control', $field_args)) { |
206 | | - $control_class = $field_args['map control']; |
207 | | - } else { |
208 | | - $control_class = 'GSmallMapControl'; |
209 | | - } |
210 | | - if ($flat == 0) { $lat = '50';} else {$lat = $flat;} |
211 | | - if ($flon == 0) { $lon = '5';} else {$lon = $flon;} |
| 202 | + $class = 'sm_map'; |
| 203 | + if (array_key_exists('map type', $field_args)) { |
| 204 | + $type = $field_args['map type']; |
| 205 | + } else { |
| 206 | + $type = 'G_NORMAL_MAP'; |
| 207 | + } |
| 208 | + if (array_key_exists('map control', $field_args)) { |
| 209 | + $control_class = $field_args['map control']; |
| 210 | + } else { |
| 211 | + $control_class = 'GSmallMapControl'; |
| 212 | + } |
| 213 | + if ($flat == 0) { $lat = '50';} else {$lat = $flat;} |
| 214 | + if ($flon == 0) { $lon = '5';} else {$lon = $flon;} |
212 | 215 | |
213 | | - // input field |
214 | | - $starting_coords = ""; |
215 | | - if ($coordinates != null && $flat != 0 && $flon != 0) { |
216 | | - $deg_lat = sgmLatDecimal2Degree($flat); |
217 | | - $deg_lon = sgmLonDecimal2Degree($flon); |
218 | | - $starting_coords = "$deg_lat, $deg_lon"; |
219 | | - } |
220 | | - $info_id = "info_$gTabIndex"; |
221 | | - $text =<<<END |
| 216 | + // input field |
| 217 | + $starting_coords = ""; |
| 218 | + if ($coordinates != null && $flat != 0 && $flon != 0) { |
| 219 | + $deg_lat = SGMUtils::latDecimal2Degree($flat); |
| 220 | + $deg_lon = SGMUtils::lonDecimal2Degree($flon); |
| 221 | + $starting_coords = "$deg_lat, $deg_lon"; |
| 222 | + } |
| 223 | + $info_id = "info_$gTabIndex"; |
| 224 | + $text =<<<END |
222 | 225 | <input tabindex="$gTabIndex" id="input_$gTabIndex" name="$input_name" type="text" value="$starting_coords" size="40" $gDisabledText> |
223 | 226 | <span id="$info_id" class="error_message"></span> |
224 | 227 | |
225 | 228 | END; |
226 | 229 | |
227 | | - // map div |
228 | | - $text .= '<div id="sm_map'.$wgGoogleMapsOnThisPage.'" class="'.$class.'"></div>'; |
| 230 | + // map div |
| 231 | + $text .= '<div id="sm_map'.$wgGoogleMapsOnThisPage.'" class="'.$class.'"></div>'; |
229 | 232 | |
230 | | - //geocoder html |
231 | | - wfLoadExtensionMessages( 'SemanticGoogleMaps' ); |
232 | | - $lookup_coordinates_text = wfMsg('semanticgooglemaps_lookupcoordinates'); |
233 | | - $text .= <<<END |
| 233 | + //geocoder HTML |
| 234 | + wfLoadExtensionMessages( 'SemanticGoogleMaps' ); |
| 235 | + $lookup_coordinates_text = wfMsg('semanticgooglemaps_lookupcoordinates'); |
| 236 | + $text .= <<<END |
234 | 237 | <p> |
235 | 238 | <input size="60" id= "geocode" name="geocode" value="" type="text"> |
236 | 239 | <a href="#" onClick="showAddress(document.forms['createbox'].geocode.value); return false">$lookup_coordinates_text</a> |
— | — | @@ -238,9 +241,8 @@ |
239 | 242 | |
240 | 243 | END; |
241 | 244 | |
242 | | - // map javascript |
243 | | - |
244 | | - $text .= <<<END |
| 245 | + // map Javascript |
| 246 | + $text .= <<<END |
245 | 247 | <script src="http://maps.google.com/maps?file=api&v=2&key=$wgGoogleMapsKey" type="$wgJsMimeType"></script> |
246 | 248 | <script type="text/javascript"> |
247 | 249 | function showAddress(address) { |
— | — | @@ -294,7 +296,7 @@ |
295 | 297 | </script> |
296 | 298 | |
297 | 299 | END; |
298 | | - $javascript_text = <<<END |
| 300 | + $javascript_text = <<<END |
299 | 301 | <script type="text/javascript"> |
300 | 302 | function makeMap{$wgGoogleMapsOnThisPage}() { |
301 | 303 | if (GBrowserIsCompatible()) { |
— | — | @@ -335,11 +337,12 @@ |
336 | 338 | </script> |
337 | 339 | |
338 | 340 | END; |
339 | | - // remove all newlines, to avoid wiki parsing inserting unwanted |
340 | | - // <p> tags within the Javascript |
341 | | - $javascript_text = preg_replace('/\s+/m', ' ', $javascript_text); |
342 | | - $text .= $javascript_text; |
| 341 | + // remove all newlines, to avoid wiki parsing inserting unwanted |
| 342 | + // <p> tags within the Javascript |
| 343 | + $javascript_text = preg_replace('/\s+/m', ' ', $javascript_text); |
| 344 | + $text .= $javascript_text; |
343 | 345 | |
344 | | - $output = array($text,''); |
345 | | - return $output; |
| 346 | + $output = array($text, ''); |
| 347 | + return $output; |
| 348 | + } |
346 | 349 | } |