Index: trunk/extensions/Maps/ParserHooks/Maps_Geodistance.php |
— | — | @@ -0,0 +1,148 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Class for the 'geodistance' parser hooks, |
| 6 | + * which can transform the notation of a distance. |
| 7 | + * |
| 8 | + * @since 0.7 |
| 9 | + * |
| 10 | + * @file Maps_Geodistance.php |
| 11 | + * @ingroup Maps |
| 12 | + * |
| 13 | + * @author Jeroen De Dauw |
| 14 | + */ |
| 15 | +class MapsGeodistance extends ParserHook { |
| 16 | + |
| 17 | + /** |
| 18 | + * No LST in pre-5.3 PHP *sigh*. |
| 19 | + * This is to be refactored as soon as php >=5.3 becomes acceptable. |
| 20 | + */ |
| 21 | + public static function staticMagic( array &$magicWords, $langCode ) { |
| 22 | + $className = __CLASS__; |
| 23 | + $instance = new $className(); |
| 24 | + return $instance->magic( $magicWords, $langCode ); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * No LST in pre-5.3 PHP *sigh*. |
| 29 | + * This is to be refactored as soon as php >=5.3 becomes acceptable. |
| 30 | + */ |
| 31 | + public static function staticInit( Parser &$wgParser ) { |
| 32 | + $className = __CLASS__; |
| 33 | + $instance = new $className(); |
| 34 | + return $instance->init( $wgParser ); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Gets the name of the parser hook. |
| 39 | + * @see ParserHook::getName |
| 40 | + * |
| 41 | + * @since 0.7 |
| 42 | + * |
| 43 | + * @return string |
| 44 | + */ |
| 45 | + protected function getName() { |
| 46 | + return 'geodistance'; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Returns an array containing the parameter info. |
| 51 | + * @see ParserHook::getParameterInfo |
| 52 | + * |
| 53 | + * @since 0.7 |
| 54 | + * |
| 55 | + * @return array |
| 56 | + */ |
| 57 | + protected function getParameterInfo() { |
| 58 | + global $egMapsDistanceUnit, $egMapsDistanceDecimals; |
| 59 | + |
| 60 | + return array( |
| 61 | + 'location1' => array( |
| 62 | + 'required' => true, |
| 63 | + 'tolower' => false, |
| 64 | + 'aliases' => array( 'from' ) |
| 65 | + ), |
| 66 | + 'location2' => array( |
| 67 | + 'required' => true, |
| 68 | + 'tolower' => false, |
| 69 | + 'aliases' => array( 'to' ) |
| 70 | + ), |
| 71 | + 'unit' => array( |
| 72 | + 'criteria' => array( |
| 73 | + 'in_array' => MapsDistanceParser::getUnits() |
| 74 | + ), |
| 75 | + 'default' => $egMapsDistanceUnit |
| 76 | + ), |
| 77 | + 'decimals' => array( |
| 78 | + 'type' => 'integer', |
| 79 | + 'default' => $egMapsDistanceDecimals |
| 80 | + ) |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Returns the list of default parameters. |
| 86 | + * @see ParserHook::getDefaultParameters |
| 87 | + * |
| 88 | + * @since 0.7 |
| 89 | + * |
| 90 | + * @return array |
| 91 | + */ |
| 92 | + protected function getDefaultParameters() { |
| 93 | + return array( 'location1', 'location2', 'unit', 'decimals' ); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Renders and returns the output. |
| 98 | + * @see ParserHook::render |
| 99 | + * |
| 100 | + * @since 0.7 |
| 101 | + * |
| 102 | + * @param array $parameters |
| 103 | + * |
| 104 | + * @return string |
| 105 | + */ |
| 106 | + public function render( array $parameters ) { |
| 107 | + $canGeocode = MapsMapper::geocoderIsAvailable(); |
| 108 | + |
| 109 | + if ( $canGeocode ) { |
| 110 | + $start = MapsGeocoder::attemptToGeocode( $parameters['location1'] ); |
| 111 | + $end = MapsGeocoder::attemptToGeocode( $parameters['location2'] ); |
| 112 | + } else { |
| 113 | + $start = MapsCoordinateParser::parseCoordinates( $parameters['location1'] ); |
| 114 | + $end = MapsCoordinateParser::parseCoordinates( $parameters['location2'] ); |
| 115 | + } |
| 116 | + |
| 117 | + if ( $start && $end ) { |
| 118 | + $output = MapsDistanceParser::formatDistance( MapsGeoFunctions::calculateDistance( $start, $end ), $parameters['unit'], $parameters['decimals'] ); |
| 119 | + } else { |
| 120 | + // TODO: use ParserHook class methods to handle errors |
| 121 | + global $egValidatorFatalLevel; |
| 122 | + |
| 123 | + $fails = array(); |
| 124 | + if ( !$start ) $fails[] = $parameters['location1']; |
| 125 | + if ( !$end ) $fails[] = $parameters['location2']; |
| 126 | + |
| 127 | + switch ( $egValidatorFatalLevel ) { |
| 128 | + case Validator_ERRORS_NONE: |
| 129 | + $output = ''; |
| 130 | + break; |
| 131 | + case Validator_ERRORS_WARN: |
| 132 | + $output = '<b>' . htmlspecialchars( wfMsgExt( 'validator_warning_parameters', array( 'parsemag' ), count( $fails ) ) ) . '</b>'; |
| 133 | + break; |
| 134 | + case Validator_ERRORS_SHOW: default: |
| 135 | + global $wgLang; |
| 136 | + |
| 137 | + if ( $canGeocode ) { |
| 138 | + $output = htmlspecialchars( wfMsgExt( 'maps_geocoding_failed', array( 'parsemag' ), $wgLang->listToText( $fails ), count( $fails ) ) ); |
| 139 | + } else { |
| 140 | + $output = htmlspecialchars( wfMsgExt( 'maps_unrecognized_coords', array( 'parsemag' ), $wgLang->listToText( $fails ), count( $fails ) ) ); |
| 141 | + } |
| 142 | + break; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return $output; |
| 147 | + } |
| 148 | + |
| 149 | +} |
\ No newline at end of file |