r58055 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r58054‎ | r58055 | r58056 >
Date:16:47, 23 October 2009
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Modified paths:
  • /trunk/extensions/Maps/GoogleMaps/Maps_GoogleMapsDispMap.php (modified) (history)
  • /trunk/extensions/Maps/Maps.php (modified) (history)
  • /trunk/extensions/Maps/Maps_Mapper.php (modified) (history)
  • /trunk/extensions/Maps/Maps_Utils.php (modified) (history)
  • /trunk/extensions/Maps/OpenLayers/Maps_OpenLayersDispMap.php (modified) (history)
  • /trunk/extensions/Maps/ParserFunctions/DisplayMap/Maps_BaseMap.php (modified) (history)
  • /trunk/extensions/Maps/ParserFunctions/DisplayPoint/Maps_BasePointMap.php (modified) (history)
  • /trunk/extensions/Maps/ParserFunctions/Maps_ParserGeocoder.php (modified) (history)
  • /trunk/extensions/Maps/YahooMaps/Maps_YahooMapsDispMap.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Maps/OpenLayers/Maps_OpenLayersDispMap.php
@@ -13,7 +13,7 @@
1414 die( 'Not an entry point.' );
1515 }
1616
17 -class MapsOpenLayersDispMap extends MapsBasePointMap {
 17+class MapsOpenLayersDispMap extends MapsBaseMap {
1818
1919 public $serviceName = MapsOpenLayersUtils::SERVICE_NAME;
2020
Index: trunk/extensions/Maps/ParserFunctions/DisplayPoint/Maps_BasePointMap.php
@@ -112,7 +112,6 @@
113113 /**
114114 * Sets the $centre_lat and $centre_lon fields.
115115 * Note: this needs to be done AFTRE the maker coordinates are set.
116 - *
117116 */
118117 private function setCentre() {
119118 if (empty($this->centre)) {
@@ -129,20 +128,34 @@
130129 }
131130 else {
132131 // If centre is not set and there are no markers, use the default latitude and longitutde.
133 - global $egMapsMapLat, $egMapsMapLon;
134 - $this->centre_lat = $egMapsMapLat;
135 - $this->centre_lon = $egMapsMapLon;
 132+ $this->setCentreDefaults();
136133 }
137134 }
138 - else {
139 - // If a centre value is set, use it.
140 - $centre = MapsUtils::getLatLon($this->centre);
141 - $this->centre_lat = $centre['lat'];
142 - $this->centre_lon = $centre['lon'];
143 - }
144 - }
 135+ else { // If a centre value is set, geocode when needed and use it.
 136+ $this->centre = MapsParserGeocoder::attemptToGeocode($this->centre, $this->geoservice, $this->serviceName);
 137+
 138+ // If the centre is not false, it will be a valid coordinate, which can be used to set the latitude and longitutde.
 139+ if ($this->centre) {
 140+ $this->centre = MapsUtils::getLatLon($this->centre);
 141+ $this->centre_lat = $this->centre['lat'];
 142+ $this->centre_lon = $this->centre['lon'];
 143+ }
 144+ else { // If it's false, the coordinate was invalid, or geocoding failed. Either way, the default's should be used.
 145+ $this->setCentreDefaults();
 146+ }
 147+ }
 148+ }
145149
146150 /**
 151+ * Sets the centre latitude and longitutde to the defaults.
 152+ */
 153+ private function setCentreDefaults() {
 154+ global $egMapsMapLat, $egMapsMapLon;
 155+ $this->centre_lat = $egMapsMapLat;
 156+ $this->centre_lon = $egMapsMapLon;
 157+ }
 158+
 159+ /**
147160 * Parse the wiki text in the title and label values.
148161 *
149162 * @param unknown_type $parser
Index: trunk/extensions/Maps/ParserFunctions/DisplayMap/Maps_BaseMap.php
@@ -27,8 +27,6 @@
2828 * @return html
2929 */
3030 public final function displayMap(&$parser, array $params) {
31 -die('disp map');
32 -
3331 $this->setMapSettings();
3432
3533 $coords = $this->manageMapProperties($params);
@@ -61,11 +59,31 @@
6260 *
6361 */
6462 private function setZoom() {
65 - if (strlen($this->zoom) < 1) $this->zoom = $this->defaultZoom;
 63+ if (empty($this->zoom)) $this->zoom = $this->defaultZoom;
6664 }
6765
 66+ /**
 67+ * Sets the $centre_lat and $centre_lon fields.
 68+ */
6869 private function setCentre() {
69 - if (strlen($this->centre) < 1) $this->centre = $this->defaultZoom;
 70+ if (empty($this->coordinates)) { // If centre is not set, use the default latitude and longitutde.
 71+ global $egMapsMapLat, $egMapsMapLon;
 72+ $this->centre_lat = $egMapsMapLat;
 73+ $this->centre_lon = $egMapsMapLon;
 74+ }
 75+ else { // If a centre value is set, geocode when needed and use it.
 76+ $this->coordinates = MapsParserGeocoder::attemptToGeocode($this->coordinates, $this->geoservice, $this->serviceName);
 77+
 78+ // If the centre is not false, it will be a valid coordinate, which can be used to set the latitude and longitutde.
 79+ if ($this->coordinates) {
 80+ $this->coordinates = MapsUtils::getLatLon($this->coordinates);
 81+ $this->centre_lat = $this->coordinates['lat'];
 82+ $this->centre_lon = $this->coordinates['lon'];
 83+ }
 84+ else { // If it's false, the coordinate was invalid, or geocoding failed. Either way, the default's should be used.
 85+ $this->setCentreDefaults();
 86+ }
 87+ }
7088 }
7189
7290 }
Index: trunk/extensions/Maps/ParserFunctions/Maps_ParserGeocoder.php
@@ -79,31 +79,57 @@
8080 $params[$i] = 'coordinates=' . implode(';', $coordinates);
8181
8282 }
 83+
8384 }
8485
8586 return $fails;
8687 }
8788
8889 /**
 90+ * This function first determines wether the provided string is a pair or coordinates
 91+ * or an address. If it's the later, an attempt to geocode will be made. The function will
 92+ * return the coordinates or false, in case a geocoding attempt was made but failed.
8993 *
90 - * @return unknown_type
 94+ * @param $coordsOrAddress
 95+ * @param $geoservice
 96+ * @param $service
 97+ *
 98+ * @return string or boolean
9199 */
92 - private static function attemptToGeocode($coordsOrAddress, $geoservice, $service) {
93 - // TODO: add check for DM and DD notations
94 - $floatRegex = "/^\d{1,3}(|\.\d{1,7}),(|\s)\d{1,3}(|\.\d{1,7})$/";
95 - $dmsRegex = "/^(\d{1,2}�)(\d{2}')?((\d{2}\")?|(\d{2}\.\d{2}\")?)(N|S)(| )(\d{1,2}�)(\d{2}')?((\d{2}\")?|(\d{2}\.\d{2}\")?)(E|W)$/";
96 -
97 - $needsGeocoding = !preg_match($floatRegex, $coordsOrAddress);
98 - if ($needsGeocoding) $needsGeocoding = !preg_match($dmsRegex, $coordsOrAddress);
99 -
100 - if ($needsGeocoding) {
101 - $coords = MapsGeocoder::geocodeToString($coordsOrAddress, $geoservice, $service);
 100+ public static function attemptToGeocode($coordsOrAddress, $geoservice, $service) {
 101+ if (MapsParserGeocoder::isCoordinate($coordsOrAddress)) {
 102+ $coords = $coordsOrAddress;
102103 }
103104 else {
104 - $coords = $coordsOrAddress;
 105+ $coords = MapsGeocoder::geocodeToString($coordsOrAddress, $geoservice, $service);
105106 }
106107
107108 return $coords;
108109 }
109110
 111+ /**
 112+ *
 113+ * @param $coordsOrAddress
 114+ * @return unknown_type
 115+ */
 116+ private static function isCoordinate($coordsOrAddress) {
 117+ $coordRegexes = array( // TODO: change . to �, this won't work for some reason
 118+ '/^\d{1,3}(\.\d{1,7})?,(\s)?\d{1,3}(\.\d{1,7})?$/', // Floats
 119+ '/^(\d{1,2}.)(\d{2}\')?((\d{2}")?|(\d{2}\.\d{2}")?)(N|S)(\s)?(\d{1,2}.)(\d{2}\')?((\d{2}")?|(\d{2}\.\d{2}")?)(E|W)$/', // DMS // TODO: compress logic
 120+ '/^(-)?\d{1,3}(|\.\d{1,7}).,(\s)?(-)?(\s)?\d{1,3}(|\.\d{1,7}).$/', // DD
 121+ '/(-)?\d{1,3}.\d{1,3}(\.\d{1,7}\')?,(\s)?(-)?\d{1,3}.\d{1,3}(\.\d{1,7}\')?$/', // DM
 122+ );
 123+
 124+ $isCoordinate = false;
 125+
 126+ foreach ($coordRegexes as $coordRegex) {
 127+ if (preg_match($coordRegex, $coordsOrAddress)) {
 128+ $isCoordinate = true;
 129+ continue;
 130+ }
 131+ }
 132+
 133+ return $isCoordinate;
 134+ }
 135+
110136 }
\ No newline at end of file
Index: trunk/extensions/Maps/Maps.php
@@ -23,7 +23,7 @@
2424 die( 'Not an entry point.' );
2525 }
2626
27 -define('Maps_VERSION', '0.4 a7');
 27+define('Maps_VERSION', '0.4 a8');
2828
2929 $egMapsScriptPath = $wgScriptPath . '/extensions/Maps';
3030 $egMapsIP = $IP . '/extensions/Maps';
Index: trunk/extensions/Maps/Maps_Mapper.php
@@ -24,6 +24,7 @@
2525 private static $mainParams = array
2626 (
2727 'service' => array(),
 28+ 'geoservice' => array(),
2829 'coordinates' => array('coords', 'location', 'locations'),
2930 'zoom' => array(),
3031 'centre' => array('center'),
@@ -115,6 +116,7 @@
116117
117118 $mapDefaults = array(
118119 'service' => $egMapsDefaultService,
 120+ 'geoservice' => '',
119121 'coordinates' => "$egMapsMapLat, $egMapsMapLon",
120122 'zoom' => '',
121123 'centre' => '',
Index: trunk/extensions/Maps/GoogleMaps/Maps_GoogleMapsDispMap.php
@@ -13,7 +13,7 @@
1414 die( 'Not an entry point.' );
1515 }
1616
17 -final class MapsGoogleMapsDispMap extends MapsBasePointMap {
 17+final class MapsGoogleMapsDispMap extends MapsBaseMap {
1818
1919 public $serviceName = MapsGoogleMapsUtils::SERVICE_NAME;
2020
Index: trunk/extensions/Maps/YahooMaps/Maps_YahooMapsDispMap.php
@@ -13,7 +13,7 @@
1414 die( 'Not an entry point.' );
1515 }
1616
17 -class MapsYahooMapsDispMap extends MapsBasePointMap {
 17+class MapsYahooMapsDispMap extends MapsBaseMap {
1818
1919 public $serviceName = MapsYahooMapsUtils::SERVICE_NAME;
2020
Index: trunk/extensions/Maps/Maps_Utils.php
@@ -35,6 +35,17 @@
3636 }
3737 }
3838
 39+ private static function convertCoord($deg_coord = "") {
 40+ if (preg_match ( '/°/', $deg_coord )) {
 41+ if (preg_match ( '/"/', $deg_coord )) {
 42+ return MapsUtils::degree2Decimal ( $deg_coord );
 43+ } else {
 44+ return MapsUtils::decDegree2Decimal ( $deg_coord );
 45+ }
 46+ }
 47+ return $deg_coord;
 48+ }
 49+
3950 private static function degree2Decimal($deg_coord = "") {
4051 $dpos = strpos ( $deg_coord, '°' );
4152 $mpos = strpos ( $deg_coord, '.' );
@@ -65,17 +76,6 @@
6677 return $decimal;
6778 }
6879
69 - private static function convertCoord($deg_coord = "") {
70 - if (preg_match ( '/°/', $deg_coord )) {
71 - if (preg_match ( '/"/', $deg_coord )) {
72 - return MapsUtils::degree2Decimal ( $deg_coord );
73 - } else {
74 - return MapsUtils::decDegree2Decimal ( $deg_coord );
75 - }
76 - }
77 - return $deg_coord;
78 - }
79 -
8080 public static function latDecimal2Degree($decimal) {
8181 if ($decimal < 0) {
8282 return abs ( $decimal ) . "° S";

Status & tagging log