r101518 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r101517‎ | r101518 | r101519 >
Date:19:38, 1 November 2011
Author:danwe
Status:deferred (Comments)
Tags:
Comment:
Started 0.4.13 alpha.
ParserHook::$frame now always is set in case SFH_OBJECT_ARGS and therewith object style parser function args are enabled. Generally better support to handle different kinds of parser hook function/tag calls. Anotehr effect is that ParserHook::$parser now always is a reference to the original Parser Object.
Modified paths:
  • /trunk/extensions/Validator/RELEASE-NOTES (modified) (history)
  • /trunk/extensions/Validator/Validator.i18n.php (modified) (history)
  • /trunk/extensions/Validator/Validator.php (modified) (history)
  • /trunk/extensions/Validator/includes/ParserHook.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Validator/RELEASE-NOTES
@@ -4,6 +4,18 @@
55 This change log contains a list of completed to-do's (new features, bug fixes, refactoring) for every version of Validator.
66
77
 8+=== Validator 0.4.13 alpha ===
 9+( trunk )
 10+
 11+* ParserHook::$parser now is a reference to the original parser object, as one would suspect.
 12+ Before this has only been the case for tag extension but not for parser function calls.
 13+
 14+* if SFH_OBJECT_ARGS and therefore object parser function arguments are available in the MW
 15+ version used with Validator, ParserHook::$frame will not be null anymore. Therefore a new
 16+ function ParserHook::renderFunctionObj() is introduced, handling these SFH_OBJECT_ARGS hooks.
 17+
 18+* ParserHook Validation messages will now output text in global content language instead of users interface language.
 19+
820 === Validator 0.4.12 ===
921 (2011-10-15)
1022
Index: trunk/extensions/Validator/includes/ParserHook.php
@@ -11,6 +11,10 @@
1212 *
1313 * @licence GNU GPL v3 or later
1414 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 15+ * @author Daniel Werner
 16+ *
 17+ * @ToDo: Add possiblity to use 'functionTagHooks'. See Parser::setFunctionTagHook() for
 18+ * details. There is not much information on this kind of parser funktion hook though.
1519 */
1620 abstract class ParserHook {
1721
@@ -130,11 +134,11 @@
131135 *
132136 * @since 0.4
133137 *
134 - * @param Parser $wgParser
 138+ * @param Parser $parser
135139 *
136140 * @return true
137141 */
138 - public function init( Parser &$wgParser ) {
 142+ public function init( Parser &$parser ) {
139143 $className = get_class( $this );
140144 $first = true;
141145
@@ -143,18 +147,30 @@
144148 self::$registeredHooks[$name] = $className;
145149 $first = false;
146150 }
147 -
 151+
148152 if ( $this->forTagExtensions ) {
149 - $wgParser->setHook(
 153+ $parser->setHook(
150154 $name,
151 - array( new ParserHookCaller( $className, 'renderTag' ), 'runHook' )
 155+ array( new ParserHookCaller( $className, 'renderTag' ), 'runTagHook' )
152156 );
153157 }
154158
155159 if ( $this->forParserFunctions ) {
156 - $wgParser->setFunctionHook(
 160+ $flags = 0;
 161+ $function = 'renderFunction';
 162+ $callerFunction = 'runFunctionHook';
 163+
 164+ // use object arguments if available:
 165+ if ( defined( 'SFH_OBJECT_ARGS' ) ) {
 166+ $flags = $flags | SFH_OBJECT_ARGS;
 167+ $function .= 'Obj';
 168+ $callerFunction .= 'Obj';
 169+ }
 170+
 171+ $parser->setFunctionHook(
157172 $name,
158 - array( new ParserHookCaller( $className, 'renderFunction' ), 'runHook' )
 173+ array( new ParserHookCaller( $className, $function ), $callerFunction ),
 174+ $flags
159175 );
160176 }
161177 }
@@ -198,11 +214,11 @@
199215 }
200216
201217 /**
202 - * Handler for rendering the tag hook.
 218+ * Handler for rendering the tag hook registered by Parser::setHook()
203219 *
204220 * @since 0.4
205221 *
206 - * @param minxed $input string or null
 222+ * @param mixed $input string or null
207223 * @param array $args
208224 * @param Parser $parser
209225 * @param PPFrame $frame Available from 1.16
@@ -216,7 +232,7 @@
217233 $defaultParameters = $this->getDefaultParameters( self::TYPE_TAG );
218234 $defaultParam = array_shift( $defaultParameters );
219235
220 - // If there is a first default parameter, set the tag contents as it's value.
 236+ // If there is a first default parameter, set the tag contents as its value.
221237 if ( !is_null( $defaultParam ) && !is_null( $input ) ) {
222238 $args[$defaultParam] = $input;
223239 }
@@ -225,23 +241,28 @@
226242 }
227243
228244 /**
229 - * Handler for rendering the function hook.
 245+ * Handler for rendering the function hook registered by Parser::setFunctionHook()
230246 *
231247 * @since 0.4
232248 *
233 - * @param Parser $parser
 249+ * @param Parser &$parser
234250 * ... further arguments ...
235251 *
236252 * @return array
237253 */
238 - public function renderFunction() {
 254+ public function renderFunction( Parser &$parser /*, n args */ ) {
239255 $args = func_get_args();
240 -
 256+
241257 $this->parser = array_shift( $args );
 258+
242259 $output = $this->validateAndRender( $args, self::TYPE_FUNCTION );
243260 $options = $this->getFunctionOptions();
244261
245262 if ( array_key_exists( 'isHTML', $options ) && $options['isHTML'] ) {
 263+ /** @ToDo: FIXME: Is this really necessary? The same thing is propably going to
 264+ * happen in Parser::braceSubstitution() if 'isHTML' is set!
 265+ * @ToDo: other options besides 'isHTML' like 'noparse' are ignored here!
 266+ */
246267 return $this->parser->insertStripItem( $output, $this->parser->mStripState );
247268 }
248269
@@ -250,6 +271,34 @@
251272 $options
252273 );
253274 }
 275+
 276+ /**
 277+ * Handler for rendering the function hook registered by Parser::setFunctionHook() together
 278+ * with object style arguments (SFH_OBJECT_ARGS flag).
 279+ *
 280+ * @since 0.4.13
 281+ *
 282+ * @param Parser &$parser
 283+ * @param PPFrame $frame
 284+ * @param type $args
 285+ * @return array
 286+ */
 287+ public function renderFunctionObj( Parser &$parser, PPFrame $frame, $args ) {
 288+ $this->frame = $frame;
 289+
 290+ // create non-object args for old style 'renderFunction()'
 291+ $oldStyleArgs = array( &$parser );
 292+
 293+ foreach( $args as $arg ) {
 294+ $oldStyleArgs[] = trim( $frame->expand( $arg ) );
 295+ }
 296+
 297+ /*
 298+ * since we can't validate un-expandet arguments, we just go on with old-style function
 299+ * handling from here. Only advantage is that we have $this->frame set properly.
 300+ */
 301+ return call_user_func_array( array( $this, 'renderFunction' ), $oldStyleArgs );
 302+ }
254303
255304 /**
256305 * Returns the parser function otpions.
@@ -331,9 +380,9 @@
332381 *
333382 * @return string
334383 */
335 - protected function renderFatalError( ValidationError $error ) {
 384+ protected function renderFatalError( ValidationError $error ) {
336385 return '<div><span class="errorbox">' .
337 - htmlspecialchars( wfMsgExt( 'validator-fatal-error', 'parsemag', $error->getMessage() ) ) .
 386+ htmlspecialchars( wfMsgExt( 'validator-fatal-error', array( 'parsemag', 'content' ), $error->getMessage() ) ) .
338387 '</span></div><br /><br />';
339388 }
340389
@@ -348,21 +397,21 @@
349398 $displayStuff = $this->getErrorsToDisplay();
350399
351400 if ( count( $displayStuff['errors'] ) > 0 ) {
352 - $output .= wfMsgExt( 'validator_error_parameters', 'parsemag', count( $displayStuff['errors'] ) );
 401+ $output .= wfMsgExt( 'validator_error_parameters', array( 'parsemag', 'content' ), count( $displayStuff['errors'] ) );
353402
354403 foreach( $displayStuff['errors'] as $error ) {
355404 $output .= '<br />* ' . $error->getMessage();
356405 }
357406
358407 if ( count( $displayStuff['warnings'] ) > 0 ) {
359 - $output .= '<br />* ' . wfMsgExt( 'validator-warning-adittional-errors', 'parsemag', count( $displayStuff['warnings'] ) );
 408+ $output .= '<br />* ' . wfMsgExt( 'validator-warning-adittional-errors', array( 'parsemag', 'content' ), count( $displayStuff['warnings'] ) );
360409 }
361410 }
362411 elseif ( count( $displayStuff['warnings'] ) > 0 ) {
363412 $output .= wfMsgExt(
364413 'validator-warning',
365 - 'parsemag',
366 - wfMsgExt( 'validator_warning_parameters', 'parsemag', count( $displayStuff['warnings'] ) )
 414+ array( 'parsemag', 'content' ),
 415+ wfMsgExt( 'validator_warning_parameters', array( 'parsemag', 'content' ), count( $displayStuff['warnings'] ) )
367416 );
368417 }
369418
@@ -505,20 +554,40 @@
506555 * @since 0.4
507556 *
508557 * @author Jeroen De Dauw
 558+ * @author Daniel Werner
509559 */
510560 class ParserHookCaller {
511 -
 561+
512562 protected $class;
513563 protected $method;
514 -
 564+
515565 function __construct( $class, $method ) {
516566 $this->class = $class;
517567 $this->method = $method;
518568 }
519 -
520 - public function runHook() {
 569+
 570+ /*
 571+ * See Parser::braceSubstitution() and Parser::extensionSubstitution() for details about
 572+ * how the Parser object and other parameters are being passed. Therefore for function
 573+ * hooks &$parser fullfills the same purpos as $parser for the tag hook.
 574+ * functionTagHook (!) calls (if implemented at a later time) are more like function hooks,
 575+ * meaning, they would require &$parser as well.
 576+ */
 577+
 578+ public function runTagHook( $input, array $args, Parser $parser, PPFrame $frame = null ) {
 579+ $obj = new $this->class();
 580+ return $obj->{$this->method}( $input, $args, $parser, $frame );
 581+ }
 582+
 583+ public function runFunctionHook( Parser &$parser /*, n args */ ) {
521584 $args = func_get_args();
 585+ $args[0] = &$parser; // with '&' becaus call_user_func_array is being used
522586 return call_user_func_array( array( new $this->class(), $this->method ), $args );
523587 }
 588+
 589+ public function runFunctionHookObj( Parser &$parser, PPFrame $frame, array $args ) {
 590+ $obj = new $this->class();
 591+ return $obj->{$this->method}( $parser, $frame, $args );
 592+ }
524593
525594 }
\ No newline at end of file
Index: trunk/extensions/Validator/Validator.i18n.php
@@ -8,7 +8,7 @@
99 *
1010 * @licence GNU GPL v3 or later
1111 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
12 -*/
 12+ */
1313
1414 $messages = array();
1515
Index: trunk/extensions/Validator/Validator.php
@@ -25,7 +25,7 @@
2626 die( 'Not an entry point.' );
2727 }
2828
29 -define( 'Validator_VERSION', '0.4.12' );
 29+define( 'Validator_VERSION', '0.4.13 alpha' );
3030
3131 // Register the internationalization file.
3232 $wgExtensionMessagesFiles['Validator'] = dirname( __FILE__ ) . '/Validator.i18n.php';

Comments

#Comment by Nikerabbit (talk | contribs)   08:24, 2 November 2011

So what is the difference now if I write a new tag using Validator?

#Comment by Danwe (talk | contribs)   15:21, 2 November 2011

Nothing changes for tags, just for parser functions: 1. Before ParserHook::$parser only was a reference to the original parser if you had a tag extension but not in case of a parser function. 2. ParserHook::$frame now is available for parser functions, not only for tag extensions (if your MW version supports SFH_OBJECT_ARGS) 3. You could overwrite ParserHook::renderFunctionObj() to expand arguments manually which would allow something like a then function as in ParserFunctions.