r58951 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r58950‎ | r58951 | r58952 >
Date:16:27, 12 November 2009
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Modified paths:
  • /trunk/extensions/Maps/Geocoders/Maps_GeocodeUtils.php (modified) (history)
  • /trunk/extensions/Maps/Maps.i18n.php (modified) (history)
  • /trunk/extensions/Maps/Maps.php (modified) (history)
  • /trunk/extensions/Maps/Maps_Mapper.php (modified) (history)
  • /trunk/extensions/Maps/Maps_Settings.php (modified) (history)
  • /trunk/extensions/Maps/ParserFunctions/DisplayMap/Maps_DisplayMap.php (modified) (history)
  • /trunk/extensions/Maps/ParserFunctions/DisplayPoint/Maps_DisplayPoint.php (modified) (history)
  • /trunk/extensions/Maps/ParserFunctions/Maps_ParserFunctions.php (modified) (history)
  • /trunk/extensions/Maps/ParserFunctions/Maps_ParserGeocoder.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Maps/ParserFunctions/DisplayPoint/Maps_DisplayPoint.php
@@ -63,19 +63,22 @@
6464 return self::getMapHtml($parser, $params, 'display_point', $fails);
6565 }
6666
67 - public static function getMapHtml(&$parser, array $params, $parserFunction, array $coordFails = array()) {
 67+ // TODO: refactor up
 68+ public static function getMapHtml(&$parser, array $params, $parserFunction, array $geoFails = array()) {
6869 global $wgLang;
6970
7071 $map = array();
 72+ $coordFails = array();
7173
7274 // Go through all parameters, split their names and values, and put them in the $map array.
7375 foreach($params as $param) {
74 - $split = split('=', $param);
 76+ $split = explode('=', $param);
7577 if (count($split) > 1) {
7678 $paramName = strtolower(trim($split[0]));
7779 $paramValue = trim($split[1]);
7880 if (strlen($paramName) > 0 && strlen($paramValue) > 0) {
7981 $map[$paramName] = $paramValue;
 82+ if (MapsMapper::inParamAliases($paramName, 'coordinates')) $coordFails = MapsParserFunctions::filterInvalidCoords($map[$paramName]);
8083 }
8184 }
8285 else if (count($split) == 1) { // Default parameter (without name)
@@ -94,13 +97,19 @@
9598
9699 // Call the function according to the map service to get the HTML output
97100 $output = $mapClass->displayMap($parser, $map);
 101+
 102+ if (count($coordFails) > 0) {
 103+ $output .= '<i>' . wfMsgExt( 'maps_unrecognized_coords_for', array( 'parsemag' ), $wgLang->listToText( $coordFails ), count( $coordFails ) ) . '</i>';
 104+ }
98105
99 - if (count($coordFails) > 0) {
100 - $output .= '<i>' . wfMsgExt( 'maps_geocoding_failed_for', array( 'parsemag' ), $wgLang->listToText($coordFails ), count( $coordFails ) ) . '</i>';
 106+ if (count($geoFails) > 0) {
 107+ $output .= '<i>' . wfMsgExt( 'maps_geocoding_failed_for', array( 'parsemag' ), $wgLang->listToText( $geoFails ), count( $geoFails ) ) . '</i>';
101108 }
102109 }
103 - elseif (trim($coords) == "" && count($coordFails) > 0) {
104 - $output = '<i>' . wfMsgExt( 'maps_geocoding_failed', array( 'parsemag' ), $wgLang->listToText( $coordFails ), count( $coordFails ) ) . '</i>';
 110+ elseif (trim($coords) == "" && (count($geoFails) > 0 || count($coordFails) > 0)) {
 111+ if (count($coordFails) > 0) $output = '<i>' . wfMsgExt( 'maps_unrecognized_coords', array( 'parsemag' ), $wgLang->listToText( $coordFails ), count( $coordFails ) ) . '</i>';
 112+ if (count($geoFails) > 0) $output = '<i>' . wfMsgExt( 'maps_geocoding_failed', array( 'parsemag' ), $wgLang->listToText( $geoFails ), count( $geoFails ) ) . '</i>';
 113+ $output .= '<i>' . wfMsgExt('maps_map_cannot_be_displayed') .'</i>';
105114 }
106115 else {
107116 $output = '<i>'.wfMsg( 'maps_coordinates_missing' ).'</i>';
@@ -110,6 +119,7 @@
111120 return array( $output, 'noparse' => true, 'isHTML' => true );
112121 }
113122
 123+ // TODO: refactor up
114124 private static function getParserClassInstance($service, $parserFunction) {
115125 global $egMapsServices;
116126 // TODO: add check to see if the service actually supports this parser function, and return false for error handling if not.
Index: trunk/extensions/Maps/ParserFunctions/DisplayMap/Maps_DisplayMap.php
@@ -62,10 +62,12 @@
6363 return self::getMapHtml($parser, $params, 'display_map', $fails);
6464 }
6565
66 - public static function getMapHtml(&$parser, array $params, $parserFunction, array $coordFails = array()) {
 66+ // TODO: refactor up
 67+ public static function getMapHtml(&$parser, array $params, $parserFunction, array $geoFails = array()) {
6768 global $wgLang;
6869
6970 $map = array();
 71+ $coordFails = array();
7072
7173 // Go through all parameters, split their names and values, and put them in the $map array.
7274 foreach($params as $param) {
@@ -75,6 +77,7 @@
7678 $paramValue = trim($split[1]);
7779 if (strlen($paramName) > 0 && strlen($paramValue) > 0) {
7880 $map[$paramName] = $paramValue;
 81+ if (MapsMapper::inParamAliases($paramName, 'coordinates')) $coordFails = MapsParserFunctions::filterInvalidCoords($map[$paramName]);
7982 }
8083 }
8184 else if (count($split) == 1) { // Default parameter (without name)
@@ -94,12 +97,18 @@
9598 // Call the function according to the map service to get the HTML output
9699 $output = $mapClass->displayMap($parser, $map);
97100
98 - if (count($coordFails) > 0) {
99 - $output .= '<i>' . wfMsgExt( 'maps_geocoding_failed_for', array( 'parsemag' ), $wgLang->listToText($coordFails ), count( $coordFails ) ) . '</i>';
 101+ if (count($coordFails) > 0) {
 102+ $output .= '<i>' . wfMsgExt( 'maps_unrecognized_coords_for', array( 'parsemag' ), $wgLang->listToText( $coordFails ), count( $coordFails ) ) . '</i>';
 103+ }
 104+
 105+ if (count($geoFails) > 0) {
 106+ $output .= '<i>' . wfMsgExt( 'maps_geocoding_failed_for', array( 'parsemag' ), $wgLang->listToText($geoFails ), count( $geoFails ) ) . '</i>';
100107 }
101108 }
102 - elseif (trim($coords) == "" && count($coordFails) > 0) {
103 - $output = '<i>' . wfMsgExt( 'maps_geocoding_failed', array( 'parsemag' ), $wgLang->listToText( $coordFails ), count( $coordFails ) ) . '</i>';
 109+ elseif (trim($coords) == "" && (count($geoFails) > 0 || count($coordFails) > 0)) {
 110+ if (count($coordFails) > 0) $output = '<i>' . wfMsgExt( 'maps_unrecognized_coords', array( 'parsemag' ), $wgLang->listToText( $coordFails ), count( $coordFails ) ) . '</i>';
 111+ if (count($geoFails) > 0) $output = '<i>' . wfMsgExt( 'maps_geocoding_failed', array( 'parsemag' ), $wgLang->listToText( $geoFails ), count( $geoFails ) ) . '</i>';
 112+ $output .= '<i>' . wfMsgExt('maps_map_cannot_be_displayed') .'</i>';
104113 }
105114 else {
106115 $output = '<i>'.wfMsg( 'maps_coordinates_missing' ).'</i>';
Index: trunk/extensions/Maps/ParserFunctions/Maps_ParserGeocoder.php
@@ -34,6 +34,7 @@
3535
3636 $fails = array();
3737
 38+ // Get the service and geoservice from the parameters, since they are needed to geocode addresses.
3839 for ($i = 0; $i < count($params); $i++) {
3940 $split = split('=', $params[$i]);
4041 if (MapsMapper::inParamAliases(strtolower(trim($split[0])), 'service') && count($split) > 1) {
@@ -44,27 +45,31 @@
4546 }
4647 }
4748
 49+ // Make sure the service and geoservice are valid.
4850 $service = isset($service) ? MapsMapper::getValidService($service, 'pf') : $egMapsDefaultService;
49 -
50 - if (!isset($geoservice)) $geoservice = '';
 51+ if (! isset($geoservice)) $geoservice = '';
5152
 53+ // Go over all parameters.
5254 for ($i = 0; $i < count($params); $i++) {
53 -
5455 $split = split('=', $params[$i]);
55 - $isAddress = ((strtolower(trim($split[0])) == 'address' || strtolower(trim($split[0])) == 'addresses') && count($split) > 1) || count($split) == 1;
 56+ $isAddress = (strtolower(trim($split[0])) == 'address' || strtolower(trim($split[0])) == 'addresses') && count($split) > 1;
 57+ $isDefault = count($split) == 1;
5658
57 - if ($isAddress) {
58 - $address_srting = count($split) == 1 ? $split[0] : $split[1];
 59+ // If a parameter is either the default (no name), or an addresses list, extract all locations.
 60+ if ($isAddress || $isDefault) {
 61+
 62+ $address_srting = $split[count($split) == 1 ? 0 : 1];
5963 $addresses = explode(';', $address_srting);
6064
6165 $coordinates = array();
6266
 67+ // Go over every location and attempt to geocode it.
6368 foreach($addresses as $address) {
6469 $args = explode('~', $address);
6570 $args[0] = trim($args[0]);
6671
6772 if (strlen($args[0]) > 0) {
68 - $coords = MapsGeocodeUtils::attemptToGeocode($args[0], $geoservice, $service);
 73+ $coords = MapsGeocodeUtils::attemptToGeocode($args[0], $geoservice, $service, $isDefault);
6974
7075 if ($coords) {
7176 $args[0] = $coords;
@@ -76,7 +81,8 @@
7782 }
7883 }
7984
80 - $params[$i] = 'coordinates=' . implode(';', $coordinates);
 85+ // Add the geocoded result back to the parameter list.
 86+ $params[$i] = implode(';', $coordinates);
8187
8288 }
8389
Index: trunk/extensions/Maps/ParserFunctions/Maps_ParserFunctions.php
@@ -47,6 +47,31 @@
4848 }
4949 }
5050
 51+ /**
 52+ * Filters all non coordinate valus from a coordinate string,
 53+ * and returns an array containing all filtered out values.
 54+ *
 55+ * @param string $coordList
 56+ * @return array
 57+ */
 58+ public static function filterInvalidCoords(&$coordList, $delimeter = ';') {
 59+ $coordFails = array();
 60+ $validCoordinates = array();
 61+ $coordinates = explode($delimeter, $coordList);
 62+
 63+ foreach($coordinates as $coordinate) {
 64+ if (MapsGeocodeUtils::isCoordinate($coordinate)) {
 65+ $validCoordinates[] = $coordinate;
 66+ }
 67+ else {
 68+ $coordFails[] = $coordinate;
 69+ }
 70+ }
 71+
 72+ $coordList = implode($delimeter, $validCoordinates);
 73+ return $coordFails;
 74+ }
 75+
5176
5277
5378 }
\ 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.2 a2');
 27+define('Maps_VERSION', '0.4.2 rc1');
2828
2929 $egMapsScriptPath = $wgScriptPath . '/extensions/Maps';
3030 $egMapsIP = $IP . '/extensions/Maps';
Index: trunk/extensions/Maps/Maps_Mapper.php
@@ -39,9 +39,9 @@
4040 * Gets if a provided name is present in the aliases array of a parameter
4141 * name in the $mainParams array.
4242 *
43 - * @param string $name
44 - * @param string $mainParamName
45 - * @param string $compareMainName
 43+ * @param string $name The name you want to check for.
 44+ * @param string $mainParamName The main parameter name.
 45+ * @param boolean $compareMainName Boolean indicating wether the main name should also be compared.
4646 * @return boolean
4747 */
4848 public static function inParamAliases($name, $mainParamName, $compareMainName = true) {
Index: trunk/extensions/Maps/Maps.i18n.php
@@ -21,12 +21,15 @@
2222 Available mapping services: $1",
2323 'maps_map' => 'Map',
2424
25 - // Geocoding errors
 25+ // Coordinate errors
2626 'maps_coordinates_missing' => 'No coordinates provided for the map.',
27 - 'maps_geocoding_failed' => 'The following {{PLURAL:$2|address|addresses}} could not be geocoded: $1.
28 -The map cannot be displayed.',
 27+ 'maps_geocoding_failed' => 'The following {{PLURAL:$2|address|addresses}} could not be geocoded: $1.',
2928 'maps_geocoding_failed_for' => 'The following {{PLURAL:$2|address|addresses}} could not be geocoded and {{PLURAL:$2|has|have}} been omitted from the map:
3029 $1',
 30+ 'maps_unrecognized_coords' => 'The following coordinates where not recognized: $1.',
 31+ 'maps_unrecognized_coords_for' => 'The following coordinates where not recognized and {{PLURAL:$2|has|have}} been omitted from the map:
 32+$1',
 33+ 'maps_map_cannot_be_displayed' => 'The map cannot be displayed.',
3134
3235 // Parameter errors. Used when strict parameter validation is turned on.
3336 'maps_error_parameters' => 'The following errors have been detected in your syntaxis',
Index: trunk/extensions/Maps/Maps_Settings.php
@@ -167,7 +167,7 @@
168168 # Array. The default overlays for the Google Maps overlays control, and wether they should be shown at pageload.
169169 # This value will only be used when the user does not provide one.
170170 # Available values: photos, videos, wikipedia, webcams
171 -$egMapsGMapOverlays = array('photos' => false, 'videos' => true, 'wikipedia' => true, 'webcams' => false);
 171+$egMapsGMapOverlays = array('photos' => false, 'videos' => false, 'wikipedia' => false, 'webcams' => false);
172172
173173
174174
Index: trunk/extensions/Maps/Geocoders/Maps_GeocodeUtils.php
@@ -25,9 +25,14 @@
2626 *
2727 * @return string or boolean
2828 */
29 - public static function attemptToGeocode($coordsOrAddress, $geoservice, $service) {
30 - if (MapsGeocodeUtils::isCoordinate($coordsOrAddress)) {
31 - $coords = $coordsOrAddress;
 29+ public static function attemptToGeocode($coordsOrAddress, $geoservice, $service, $checkForCoords = true) {
 30+ if ($checkForCoords) {
 31+ if (MapsGeocodeUtils::isCoordinate($coordsOrAddress)) {
 32+ $coords = $coordsOrAddress;
 33+ }
 34+ else {
 35+ $coords = MapsGeocoder::geocodeToString($coordsOrAddress, $geoservice, $service);
 36+ }
3237 }
3338 else {
3439 $coords = MapsGeocoder::geocodeToString($coordsOrAddress, $geoservice, $service);
@@ -43,18 +48,19 @@
4449 *
4550 * @return boolean
4651 */
47 - private static function isCoordinate($coordsOrAddress) {
 52+ public static function isCoordinate($coordsOrAddress) {
4853 $coordRegexes = array(
49 - '/^(-)?\d{1,3}(\.\d{1,7})?,(\s)?(-)?\d{1,3}(\.\d{1,7})?$/', // Floats
 54+ '/^(-)?\d{1,3}(\.\d{1,14})?,(\s)?(-)?\d{1,3}(\.\d{1,14})?$/', // Floats
5055 '/^(\d{1,3}°)(\d{1,2}(\′|\'))?((\d{1,2}(″|"))?|(\d{1,2}\.\d{1,2}(″|"))?)(N|S)(\s)?(\d{1,3}°)(\d{1,2}(\′|\'))?((\d{1,2}(″|"))?|(\d{1,2}\.\d{1,2}(″|"))?)(E|W)$/', // DMS
51 - '/^(-)?\d{1,3}(|\.\d{1,7})°,(\s)?(-)?(\s)?\d{1,3}(|\.\d{1,7})°$/', // DD
52 - '/(-)?\d{1,3}°\d{1,3}(\.\d{1,7}\')?,(\s)?(-)?\d{1,3}°\d{1,3}(\.\d{1,7}\')?$/', // DM
 56+ '/^(-)?\d{1,3}(|\.\d{1,14})°,(\s)?(-)?(\s)?\d{1,3}(|\.\d{1,14})°$/', // DD
 57+ '/^\d{1,3}(|\.\d{1,14})°(\s)?(N|S),(\s)?(\s)?\d{1,3}(|\.\d{1,14})°(\s)(E|W)?$/', // DD (directional)
 58+ '/(-)?\d{1,3}°\d{1,3}(\.\d{1,14}\')?,(\s)?(-)?\d{1,3}°\d{1,3}(\.\d{1,14}\')?$/', // DM
5359 );
5460
5561 $isCoordinate = false;
5662
5763 foreach ($coordRegexes as $coordRegex) {
58 - if (preg_match($coordRegex, $coordsOrAddress)) {
 64+ if (preg_match($coordRegex, trim($coordsOrAddress))) {
5965 $isCoordinate = true;
6066 continue;
6167 }

Status & tagging log