r71860 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r71859‎ | r71860 | r71861 >
Date:18:23, 28 August 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Follow up to r71859
Modified paths:
  • /trunk/extensions/Maps/ParserHooks/Maps_Coordinates.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Maps/ParserHooks/Maps_Coordinates.php
@@ -16,10 +16,10 @@
1717
1818 $wgAutoloadClasses['MapsCoordinates'] = dirname( __FILE__ ) . '/Maps_Coordinates.php';
1919
20 -$wgHooks['ParserFirstCallInit'][] = 'MapsCoordinates::init';
 20+$wgHooks['ParserFirstCallInit'][] = 'MapsCoordinates::staticInit';
2121
2222 if ( version_compare( $wgVersion, '1.16alpha', '<' ) ) {
23 - $wgHooks['LanguageGetMagic'][] = 'MapsCoordinates::magic';
 23+ $wgHooks['LanguageGetMagic'][] = 'MapsCoordinates::staticMagic';
2424 }
2525
2626 /**
@@ -30,85 +30,49 @@
3131 *
3232 * @author Jeroen De Dauw
3333 */
34 -class MapsCoordinates {
 34+class MapsCoordinates extends ParserHook {
3535
3636 /**
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.
4439 */
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 );
5044 }
5145
5246 /**
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 );
6654 }
6755
6856 /**
69 - * Handler for rendering the tag hook.
 57+ * Gets the name of the parser hook.
 58+ * @see ParserHook::getName
7059 *
7160 * @since 0.7
7261 *
73 - * @param minxed $input string or null
74 - * @param array $args
75 - * @param Parser $parser
76 - * @param PPFrame $frame
 62+ * @return string
7763 */
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';
8666 }
8767
8868 /**
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 - /**
10669 * Returns an array containing the parameter info.
 70+ * @see ParserHook::getParameterInfo
10771 *
10872 * @since 0.7
10973 *
11074 * @return array
11175 */
112 - protected static function getParameterInfo() {
 76+ protected function getParameterInfo() {
11377 global $egMapsAvailableServices, $egMapsAvailableCoordNotations;
11478 global $egMapsDefaultServices, $egMapsDefaultGeoService, $egMapsCoordinateNotation;
11579 global $egMapsAllowCoordsGeocoding, $egMapsCoordinateDirectional;
@@ -136,63 +100,35 @@
137101
138102 /**
139103 * Returns the list of default parameters.
 104+ * @see ParserHook::getDefaultParameters
140105 *
141106 * @since 0.7
142107 *
143108 * @return array
144109 */
145 - protected static function getDefaultParameters() {
 110+ protected function getDefaultParameters() {
146111 return array( 'location', 'format', 'directional' );
147112 }
148113
149114 /**
150115 * Renders and returns the output.
 116+ * @see ParserHook::render
151117 *
152118 * @since 0.7
153119 *
154 - * @param array $arguments
155 - * @param boolean $parsed
 120+ * @param array $parameters
156121 *
157122 * @return string
158123 */
159 - public static function render( array $arguments, $parsed ) {
160 - $manager = new ValidatorManager();
 124+ public function render( array $parameters ) {
 125+ $parsedCoords = MapsCoordinateParser::parseCoordinates( $parameters['location'] );
161126
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'] );
193129 } else {
194 - $output = $manager->getErrorList();
 130+ $output = htmlspecialchars( wfMsgExt( 'maps-invalid-coordinates', 'parsemag', $parameters['location'] ) );
195131 }
196 -
 132+
197133 return $output;
198134 }
199135

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r71859Added new class for parser hooks - will refactor further later on to better i...jeroendedauw18:23, 28 August 2010

Status & tagging log