r41694 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r41693‎ | r41694 | r41695 >
Date:14:14, 5 October 2008
Author:yaron
Status:old
Tags:
Comment:
Fixed PHP bug, and moved most functions into a new class, 'SGMUtils'
Modified paths:
  • /trunk/extensions/SemanticGoogleMaps/SemanticGoogleMaps.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticGoogleMaps/SemanticGoogleMaps.php
@@ -26,7 +26,7 @@
2727 // credits
2828 $wgExtensionCredits['parserhook'][] = array(
2929 'name' => 'Semantic Google Maps',
30 - 'version' => '0.4',
 30+ 'version' => '0.4.1',
3131 'author' => array( 'Robert Buzink', 'Yaron Koren' ),
3232 'url' => 'http://www.mediawiki.org/wiki/Extension:Semantic_Google_Maps',
3333 'description' => 'Allows users to add Google Maps to wiki pages based on structured data',
@@ -37,9 +37,9 @@
3838 $wgParser->setFunctionHook( 'semantic_google_map', 'sgmFunctionRender' );
3939 global $sfgFormPrinter;
4040 if ($sfgFormPrinter) {
41 - $sfgFormPrinter->setInputTypeHook('googlemap', 'sgmInputHTML', array());
 41+ $sfgFormPrinter->setInputTypeHook('googlemap', array('SGMUtils', 'formInputHTML'), array());
4242 // for backwards compatibility
43 - $sfgFormPrinter->setInputTypeHook('coordinatesmap', 'sgmInputHTML', array());
 43+ $sfgFormPrinter->setInputTypeHook('coordinatesmap', array('SGMUtils', 'formInputHTML'), array());
4444 }
4545
4646 include_once('SGM_QueryPrinter.php');
@@ -72,7 +72,7 @@
7373 if (!$wgGoogleMapsOnThisPage) {$wgGoogleMapsOnThisPage = 0;}
7474 $wgGoogleMapsOnThisPage++;
7575
76 - list($lat, $lon) = sgmGetLatLon($coordinates);
 76+ list($lat, $lon) = SGMUtils::getLatLon($coordinates);
7777
7878 $output =<<<END
7979 <script src="http://maps.google.com/maps?file=api&v=2&key=$wgGoogleMapsKey" type="$wgJsMimeType"></script>
@@ -86,150 +86,153 @@
8787
8888 }
8989
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);
96100 }
97 - return array();
98 -}
99101
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;
117121 }
118 - return $decimal;
119 -}
120122
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;
126130 }
127 - return $decimal;
128 -}
129131
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+ }
136139 }
 140+ return $deg_coord;
137141 }
138 - return $deg_coord;
139 -}
140142
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";
144146 } else {
145 - return $decimal . "° N";
 147+ return $decimal . "° N";
146148 }
147149 }
148150
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";
152154 } else {
153 - return $decimal . "° E";
 155+ return $decimal . "° E";
154156 }
155157 }
156158
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;
160163
161 - // default values
162 - $flat = 0;
163 - $flon = 0;
 164+ // default values
 165+ $flat = 0;
 166+ $flon = 0;
164167
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';
170177 } 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+ }
172189 }
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'];
180194 } else {
181 - $zoom = '0';
 195+ $width = '200';
182196 }
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';
185201 }
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;}
212215
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
222225 <input tabindex="$gTabIndex" id="input_$gTabIndex" name="$input_name" type="text" value="$starting_coords" size="40" $gDisabledText>
223226 <span id="$info_id" class="error_message"></span>
224227
225228 END;
226229
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>';
229232
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
234237 <p>
235238 <input size="60" id= "geocode" name="geocode" value="" type="text">
236239 <a href="#" onClick="showAddress(document.forms['createbox'].geocode.value); return false">$lookup_coordinates_text</a>
@@ -238,9 +241,8 @@
239242
240243 END;
241244
242 - // map javascript
243 -
244 - $text .= <<<END
 245+ // map Javascript
 246+ $text .= <<<END
245247 <script src="http://maps.google.com/maps?file=api&v=2&key=$wgGoogleMapsKey" type="$wgJsMimeType"></script>
246248 <script type="text/javascript">
247249 function showAddress(address) {
@@ -294,7 +296,7 @@
295297 </script>
296298
297299 END;
298 - $javascript_text = <<<END
 300+ $javascript_text = <<<END
299301 <script type="text/javascript">
300302 function makeMap{$wgGoogleMapsOnThisPage}() {
301303 if (GBrowserIsCompatible()) {
@@ -335,11 +337,12 @@
336338 </script>
337339
338340 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;
343345
344 - $output = array($text,'');
345 - return $output;
 346+ $output = array($text, '');
 347+ return $output;
 348+ }
346349 }

Status & tagging log