Index: trunk/extensions/SemanticGoogleMaps/SemanticGoogleMaps.php |
— | — | @@ -3,8 +3,8 @@ |
4 | 4 | if (!defined('MEDIAWIKI')) die(); |
5 | 5 | |
6 | 6 | /** |
7 | | - * An extension to that allows users to add Google Maps to wiki pages based |
8 | | - * on structured data |
| 7 | + * An extension that allows users to add, edit and display coordinate |
| 8 | + * information stored by the Semantic MediaWiki extension using Google Maps |
9 | 9 | * |
10 | 10 | * @addtogroup Extensions |
11 | 11 | * |
— | — | @@ -12,37 +12,37 @@ |
13 | 13 | * @author Yaron Koren |
14 | 14 | */ |
15 | 15 | |
16 | | -# Define a setup function |
| 16 | +// credits |
| 17 | +$wgExtensionCredits['parserhook'][] = array( |
| 18 | + 'name' => 'Semantic Google Maps', |
| 19 | + 'version' => '0.5', |
| 20 | + 'author' => array( 'Robert Buzink', 'Yaron Koren' ), |
| 21 | + 'url' => 'http://www.mediawiki.org/wiki/Extension:Semantic_Google_Maps', |
| 22 | + 'description' => 'Allows users to edit and display semantic coordinate data using Google Maps', |
| 23 | + 'descriptionmsg' => 'semanticgooglemaps-desc', |
| 24 | +); |
| 25 | + |
17 | 26 | $wgExtensionFunctions[] = 'sgmSetup'; |
18 | 27 | |
19 | | -# Add a hook to initialise the magic word |
20 | 28 | $wgHooks['LanguageGetMagic'][] = 'sgmFunctionMagic'; |
21 | 29 | |
22 | 30 | $wgExtensionMessagesFiles['SemanticGoogleMaps'] = dirname(__FILE__) . '/SemanticGoogleMaps.i18n.php'; |
23 | 31 | |
| 32 | +$sgmgIP = $IP . '/extensions/SemanticGoogleMaps'; |
| 33 | +$wgAutoloadClasses['SGMUtils'] = $sgmgIP . '/SGM_Utils.inc'; |
| 34 | +$wgAutoloadClasses['SGMResultPrinter'] = $sgmgIP . '/SGM_QueryPrinter.php'; |
| 35 | + |
24 | 36 | function sgmSetup() { |
25 | 37 | global $wgParser, $wgExtensionCredits; |
26 | 38 | |
27 | | - // credits |
28 | | - $wgExtensionCredits['parserhook'][] = array( |
29 | | - 'name' => 'Semantic Google Maps', |
30 | | - 'version' => '0.4.2', |
31 | | - 'author' => array( 'Robert Buzink', 'Yaron Koren' ), |
32 | | - 'url' => 'http://www.mediawiki.org/wiki/Extension:Semantic_Google_Maps', |
33 | | - 'description' => 'Allows users to add Google Maps to wiki pages based on structured data', |
34 | | - 'descriptionmsg' => 'semanticgooglemaps-desc', |
35 | | - ); |
36 | | - |
37 | | - // A hook to enable the '#semantic_google_map' parser function |
38 | | - $wgParser->setFunctionHook( 'semantic_google_map', 'sgmFunctionRender' ); |
| 39 | + // a hook to enable the '#semantic_google_map' parser function |
| 40 | + $wgParser->setFunctionHook( 'semantic_google_map', array('SGMUtils', 'functionRender' )); |
| 41 | + // add the 'googlemap' form input type, if Semantic Forms is installed |
39 | 42 | global $sfgFormPrinter; |
40 | 43 | if ($sfgFormPrinter) { |
41 | 44 | $sfgFormPrinter->setInputTypeHook('googlemap', array('SGMUtils', 'formInputHTML'), array()); |
42 | | - // for backwards compatibility |
43 | | - $sfgFormPrinter->setInputTypeHook('coordinatesmap', array('SGMUtils', 'formInputHTML'), array()); |
44 | 45 | } |
45 | 46 | |
46 | | - include_once('SGM_QueryPrinter.php'); |
47 | 47 | // global variable introduced in SMW 1.2.2 |
48 | 48 | global $smwgResultFormats; |
49 | 49 | if (isset($smwgResultFormats)) |
— | — | @@ -53,296 +53,7 @@ |
54 | 54 | } |
55 | 55 | |
56 | 56 | function sgmFunctionMagic( &$magicWords, $langCode ) { |
57 | | - # Add the magic word |
58 | | - # The first array element is case sensitive, in this case it is not case sensitive |
59 | | - # All remaining elements are synonyms for our parser function |
60 | 57 | $magicWords['semantic_google_map'] = array( 0, 'semantic_google_map' ); |
61 | | - // for backwards compatibility |
62 | | - $magicWords['insert_map'] = array( 0, 'semantic_google_map' ); |
63 | | - # unless we return true, other parser functions extensions won't get loaded. |
| 58 | + // unless we return true, other parser functions won't get loaded |
64 | 59 | return true; |
65 | 60 | } |
66 | | - |
67 | | -function sgmFunctionRender( &$parser, $coordinates = '1,1', $zoom = '14', $type = 'G_NORMAL_MAP', $control_class = 'GSmallMapControl', $class = 'pmap', $width = '200', $height = '200', $style = '' ) { |
68 | | - # The parser function itself |
69 | | - # The input parameters are wikitext with templates expanded |
70 | | - # The output is not parsed as wikitext |
71 | | - global $wgJsMimeType, $wgGoogleMapsKey, $wgGoogleMapsOnThisPage; |
72 | | - |
73 | | - if (!$wgGoogleMapsOnThisPage) {$wgGoogleMapsOnThisPage = 0;} |
74 | | - $wgGoogleMapsOnThisPage++; |
75 | | - |
76 | | - list($lat, $lon) = SGMUtils::getLatLon($coordinates); |
77 | | - |
78 | | - $output =<<<END |
79 | | -<script src="http://maps.google.com/maps?file=api&v=2&key=$wgGoogleMapsKey" type="$wgJsMimeType"></script> |
80 | | -<script type="text/javascript"> function createMarker(point, label) { var marker = new GMarker(point); GEvent.addListener(marker, 'click', function() { marker.openInfoWindowHtml(label, GInfoWindoOptions.maxWidth=100); }); return marker; } function addLoadEvent(func) { var oldonload = window.onload; if (typeof oldonload == 'function') { window.onload = function() { oldonload(); func(); }; } else { window.onload = func; } } window.unload = GUnload;</script> |
81 | | -<div id="map$wgGoogleMapsOnThisPage" class="$class" style="$style" ></div> |
82 | | -<script type="text/javascript"> function makeMap{$wgGoogleMapsOnThisPage}() { if (GBrowserIsCompatible()) {var map = new GMap2(document.getElementById("map{$wgGoogleMapsOnThisPage}"), {size: new GSize('$width', '$height')}); map.addControl(new {$control_class}()); map.addControl(new GMapTypeControl()); map.setCenter(new GLatLng({$lat}, {$lon}), {$zoom}, {$type}); var point = new GLatLng({$lat}, {$lon}); var marker = new GMarker(point); map.addOverlay(marker); } else { document.write('should show map'); } } addLoadEvent(makeMap{$wgGoogleMapsOnThisPage});</script> |
83 | | - |
84 | | -END; |
85 | | - |
86 | | - return array( $output, 'noparse' => true, 'isHTML' => true ); |
87 | | - |
88 | | -} |
89 | | - |
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); |
100 | | - } |
101 | | - |
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; |
121 | | - } |
122 | | - |
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; |
130 | | - } |
131 | | - |
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 | | - } |
139 | | - } |
140 | | - return $deg_coord; |
141 | | - } |
142 | | - |
143 | | - function latDecimal2Degree($decimal) { |
144 | | - if ($decimal < 0) { |
145 | | - return abs($decimal) . "° S"; |
146 | | - } else { |
147 | | - return $decimal . "° N"; |
148 | | - } |
149 | | - } |
150 | | - |
151 | | - function lonDecimal2Degree($decimal) { |
152 | | - if ($decimal < 0) { |
153 | | - return abs($decimal) . "° W"; |
154 | | - } else { |
155 | | - return $decimal . "° E"; |
156 | | - } |
157 | | - } |
158 | | - |
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; |
163 | | - |
164 | | - // default values |
165 | | - $flat = 0; |
166 | | - $flon = 0; |
167 | | - |
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'; |
177 | | - } else { |
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 | | - } |
189 | | - } |
190 | | - if (!$wgGoogleMapsOnThisPage) {$wgGoogleMapsOnThisPage = 0;} |
191 | | - $wgGoogleMapsOnThisPage++; |
192 | | - if (array_key_exists('width', $field_args)) { |
193 | | - $width = $field_args['width']; |
194 | | - } else { |
195 | | - $width = '200'; |
196 | | - } |
197 | | - if (array_key_exists('height', $field_args)) { |
198 | | - $height = $field_args['height']; |
199 | | - } else { |
200 | | - $height = '200'; |
201 | | - } |
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;} |
215 | | - |
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 |
225 | | - <input tabindex="$gTabIndex" id="input_$gTabIndex" name="$input_name" type="text" value="$starting_coords" size="40" $gDisabledText> |
226 | | - <span id="$info_id" class="error_message"></span> |
227 | | - |
228 | | -END; |
229 | | - |
230 | | - // map div |
231 | | - $text .= '<div id="sm_map'.$wgGoogleMapsOnThisPage.'" class="'.$class.'"></div>'; |
232 | | - |
233 | | - //geocoder HTML |
234 | | - wfLoadExtensionMessages( 'SemanticGoogleMaps' ); |
235 | | - $lookup_coordinates_text = wfMsg('semanticgooglemaps_lookupcoordinates'); |
236 | | - $text .= <<<END |
237 | | - <p> |
238 | | - <input size="60" id= "geocode" name="geocode" value="" type="text"> |
239 | | - <a href="#" onClick="showAddress(document.forms['createbox'].geocode.value); return false">$lookup_coordinates_text</a> |
240 | | - </p> |
241 | | - <br /> |
242 | | - |
243 | | -END; |
244 | | - |
245 | | - // map Javascript |
246 | | - $text .= <<<END |
247 | | -<script src="http://maps.google.com/maps?file=api&v=2&key=$wgGoogleMapsKey" type="$wgJsMimeType"></script> |
248 | | -<script type="text/javascript"> |
249 | | -function showAddress(address) { |
250 | | - makeMap{$wgGoogleMapsOnThisPage}(); |
251 | | - if (geocoder) { |
252 | | - geocoder.getLatLng(address, |
253 | | - function(point) { |
254 | | - if (!point) { |
255 | | - alert(address + " not found"); |
256 | | - } else { |
257 | | - map.clearOverlays() |
258 | | - map.setCenter(point, 14); |
259 | | - var marker = new GMarker(point); |
260 | | - map.addOverlay(marker); |
261 | | - document.getElementById("input_$gTabIndex").value = convertLatToDMS(point.y)+', '+convertLngToDMS(point.x); |
262 | | - } |
263 | | - } |
264 | | - ); |
265 | | - } |
266 | | -} |
267 | | - |
268 | | -function convertLatToDMS (val) { |
269 | | - if (val < 0) { |
270 | | - return Math.abs(val) + "° " + "S"; |
271 | | - } else { |
272 | | - return Math.abs(val) + "° " + "N"; |
273 | | - } |
274 | | -} |
275 | | - |
276 | | -function convertLngToDMS (val) { |
277 | | - if (val < 0) { |
278 | | - return Math.abs(val) + "° " + "W"; |
279 | | - } else { |
280 | | - return Math.abs(val) + "° " + "E"; |
281 | | - } |
282 | | -} |
283 | | - |
284 | | -function addLoadEvent(func) { |
285 | | - var oldonload = window.onload; |
286 | | - if (typeof oldonload == 'function') { |
287 | | - window.onload = function() { |
288 | | - oldonload(); |
289 | | - func(); |
290 | | - }; |
291 | | - } else { |
292 | | - window.onload = func; |
293 | | - } |
294 | | -} |
295 | | - |
296 | | -window.unload = GUnload; |
297 | | -</script> |
298 | | - |
299 | | -END; |
300 | | - $javascript_text = <<<END |
301 | | -<script type="text/javascript"> |
302 | | -function makeMap{$wgGoogleMapsOnThisPage}() { |
303 | | - if (GBrowserIsCompatible()) { |
304 | | - window.map = new GMap2(document.getElementById("sm_map{$wgGoogleMapsOnThisPage}"), {size: new GSize('$width', '$height')}); |
305 | | - geocoder = new GClientGeocoder(); |
306 | | - map.addControl(new {$control_class}()); |
307 | | - map.addControl(new GMapTypeControl()); |
308 | | - map.setCenter(new GLatLng({$lat}, {$lon}), {$zoom}, {$type}); |
309 | | - |
310 | | -END; |
311 | | - // show a starting marker only if a value already exists |
312 | | - if ($coordinates != null) { |
313 | | - $javascript_text .= <<<END |
314 | | - var point = new GLatLng({$lat}, {$lon}); |
315 | | - var marker = new GMarker(point); |
316 | | - map.addOverlay(marker); |
317 | | - |
318 | | -END; |
319 | | - } |
320 | | - $javascript_text .= <<<END |
321 | | - GEvent.addListener(map, "click", |
322 | | - function(overlay, point) { |
323 | | - place = null; |
324 | | - if (overlay) { |
325 | | - map.removeOverlay (overlay); |
326 | | - } else { |
327 | | - var marker = new GMarker (point); |
328 | | - map.clearOverlays(); |
329 | | - document.getElementById("input_$gTabIndex").value = convertLatToDMS(point.y)+', '+convertLngToDMS(point.x); |
330 | | - map.addOverlay(marker); |
331 | | - map.panTo(point); |
332 | | - } |
333 | | - } |
334 | | - ); |
335 | | - } |
336 | | -} |
337 | | -addLoadEvent(makeMap{$wgGoogleMapsOnThisPage}); |
338 | | -</script> |
339 | | - |
340 | | -END; |
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; |
345 | | - |
346 | | - $output = array($text, ''); |
347 | | - return $output; |
348 | | - } |
349 | | -} |