Index: trunk/extensions/Maps/ParserHooks/Maps_Coordinates.php |
— | — | @@ -16,10 +16,10 @@ |
17 | 17 | |
18 | 18 | $wgAutoloadClasses['MapsCoordinates'] = dirname( __FILE__ ) . '/Maps_Coordinates.php'; |
19 | 19 | |
20 | | -$wgHooks['ParserFirstCallInit'][] = 'MapsCoordinates::init'; |
| 20 | +$wgHooks['ParserFirstCallInit'][] = 'MapsCoordinates::staticInit'; |
21 | 21 | |
22 | 22 | if ( version_compare( $wgVersion, '1.16alpha', '<' ) ) { |
23 | | - $wgHooks['LanguageGetMagic'][] = 'MapsCoordinates::magic'; |
| 23 | + $wgHooks['LanguageGetMagic'][] = 'MapsCoordinates::staticMagic'; |
24 | 24 | } |
25 | 25 | |
26 | 26 | /** |
— | — | @@ -30,85 +30,49 @@ |
31 | 31 | * |
32 | 32 | * @author Jeroen De Dauw |
33 | 33 | */ |
34 | | -class MapsCoordinates { |
| 34 | +class MapsCoordinates extends ParserHook { |
35 | 35 | |
36 | 36 | /** |
37 | | - * Function to hook up the coordinate rendering functions to the parser. |
38 | | - * |
39 | | - * @since 0.7 |
40 | | - * |
41 | | - * @param Parser $wgParser |
42 | | - * |
43 | | - * @return true |
| 37 | + * No LST in pre-5.3 PHP *sigh*. |
| 38 | + * This is to be refactored as soon as php >=5.3 becomes acceptable. |
44 | 39 | */ |
45 | | - public static function init( Parser &$wgParser ) { |
46 | | - $wgParser->setHook( 'coordinates', __CLASS__ . '::renderTag' ); |
47 | | - $wgParser->setFunctionHook( 'coordinates', __CLASS__ . '::renderFunction' ); |
48 | | - |
49 | | - return true; |
| 40 | + public static function staticMagic( array &$magicWords, $langCode ) { |
| 41 | + $className = __CLASS__; |
| 42 | + $instance = new $className(); |
| 43 | + return $instance->magic( $magicWords, $langCode ); |
50 | 44 | } |
51 | 45 | |
52 | 46 | /** |
53 | | - * Function to add the magic word in pre MW 1.16. |
54 | | - * |
55 | | - * @since 0.7 |
56 | | - * |
57 | | - * @param array $magicWords |
58 | | - * @param string $langCode |
59 | | - * |
60 | | - * @return true |
61 | | - */ |
62 | | - public static function magic( array &$magicWords, $langCode ) { |
63 | | - $magicWords['coordinates'] = array( 0, 'coordinates' ); |
64 | | - |
65 | | - return true; |
| 47 | + * No LST in pre-5.3 PHP *sigh*. |
| 48 | + * This is to be refactored as soon as php >=5.3 becomes acceptable. |
| 49 | + */ |
| 50 | + public static function staticInit( Parser &$wgParser ) { |
| 51 | + $className = __CLASS__; |
| 52 | + $instance = new $className(); |
| 53 | + return $instance->init( $wgParser ); |
66 | 54 | } |
67 | 55 | |
68 | 56 | /** |
69 | | - * Handler for rendering the tag hook. |
| 57 | + * Gets the name of the parser hook. |
| 58 | + * @see ParserHook::getName |
70 | 59 | * |
71 | 60 | * @since 0.7 |
72 | 61 | * |
73 | | - * @param minxed $input string or null |
74 | | - * @param array $args |
75 | | - * @param Parser $parser |
76 | | - * @param PPFrame $frame |
| 62 | + * @return string |
77 | 63 | */ |
78 | | - public static function renderTag( $input, array $args, Parser $parser, PPFrame $frame ) { |
79 | | - $defaultParam = array_shift( self::getDefaultParameters() ); |
80 | | - |
81 | | - if ( !is_null( $defaultParam ) ) { |
82 | | - $args[$defaultParam] = $input; |
83 | | - } |
84 | | - |
85 | | - return self::render( $args, true ); |
| 64 | + protected function getName() { |
| 65 | + return 'coordinates'; |
86 | 66 | } |
87 | 67 | |
88 | 68 | /** |
89 | | - * Handler for rendering the function hook. |
90 | | - * |
91 | | - * @since 0.7 |
92 | | - * |
93 | | - * @param Parser $parser |
94 | | - * ... further arguments ... |
95 | | - */ |
96 | | - public static function renderFunction() { |
97 | | - $args = func_get_args(); |
98 | | - |
99 | | - // No need for the parser... |
100 | | - array_shift( $args ); |
101 | | - |
102 | | - return array( self::render( $args, false ) ); |
103 | | - } |
104 | | - |
105 | | - /** |
106 | 69 | * Returns an array containing the parameter info. |
| 70 | + * @see ParserHook::getParameterInfo |
107 | 71 | * |
108 | 72 | * @since 0.7 |
109 | 73 | * |
110 | 74 | * @return array |
111 | 75 | */ |
112 | | - protected static function getParameterInfo() { |
| 76 | + protected function getParameterInfo() { |
113 | 77 | global $egMapsAvailableServices, $egMapsAvailableCoordNotations; |
114 | 78 | global $egMapsDefaultServices, $egMapsDefaultGeoService, $egMapsCoordinateNotation; |
115 | 79 | global $egMapsAllowCoordsGeocoding, $egMapsCoordinateDirectional; |
— | — | @@ -136,63 +100,35 @@ |
137 | 101 | |
138 | 102 | /** |
139 | 103 | * Returns the list of default parameters. |
| 104 | + * @see ParserHook::getDefaultParameters |
140 | 105 | * |
141 | 106 | * @since 0.7 |
142 | 107 | * |
143 | 108 | * @return array |
144 | 109 | */ |
145 | | - protected static function getDefaultParameters() { |
| 110 | + protected function getDefaultParameters() { |
146 | 111 | return array( 'location', 'format', 'directional' ); |
147 | 112 | } |
148 | 113 | |
149 | 114 | /** |
150 | 115 | * Renders and returns the output. |
| 116 | + * @see ParserHook::render |
151 | 117 | * |
152 | 118 | * @since 0.7 |
153 | 119 | * |
154 | | - * @param array $arguments |
155 | | - * @param boolean $parsed |
| 120 | + * @param array $parameters |
156 | 121 | * |
157 | 122 | * @return string |
158 | 123 | */ |
159 | | - public static function render( array $arguments, $parsed ) { |
160 | | - $manager = new ValidatorManager(); |
| 124 | + public function render( array $parameters ) { |
| 125 | + $parsedCoords = MapsCoordinateParser::parseCoordinates( $parameters['location'] ); |
161 | 126 | |
162 | | - if ( $parsed ) { |
163 | | - $doFormatting = $manager->manageParsedParameters( |
164 | | - $arguments, |
165 | | - self::getParameterInfo(), |
166 | | - self::getDefaultParameters() |
167 | | - ); |
168 | | - } |
169 | | - else { |
170 | | - $doFormatting = $manager->manageParameters( |
171 | | - $arguments, |
172 | | - self::getParameterInfo(), |
173 | | - self::getDefaultParameters() |
174 | | - ); |
175 | | - } |
176 | | - |
177 | | - if ( $doFormatting ) { |
178 | | - $parameters = $manager->getParameters( false ); |
179 | | - |
180 | | - $parsedCoords = MapsCoordinateParser::parseCoordinates( $parameters['location'] ); |
181 | | - |
182 | | - if ( $parsedCoords ) { |
183 | | - $output = MapsCoordinateParser::formatCoordinates( $parsedCoords, $parameters['format'], $parameters['directional'] ); |
184 | | - } else { |
185 | | - $output = htmlspecialchars( wfMsgExt( 'maps-invalid-coordinates', 'parsemag', $parameters['location'] ) ); |
186 | | - } |
187 | | - |
188 | | - $errorList = $manager->getErrorList(); |
189 | | - |
190 | | - if ( $errorList != '' ) { |
191 | | - $output .= '<br />' . $errorList; |
192 | | - } |
| 127 | + if ( $parsedCoords ) { |
| 128 | + $output = MapsCoordinateParser::formatCoordinates( $parsedCoords, $parameters['format'], $parameters['directional'] ); |
193 | 129 | } else { |
194 | | - $output = $manager->getErrorList(); |
| 130 | + $output = htmlspecialchars( wfMsgExt( 'maps-invalid-coordinates', 'parsemag', $parameters['location'] ) ); |
195 | 131 | } |
196 | | - |
| 132 | + |
197 | 133 | return $output; |
198 | 134 | } |
199 | 135 | |