Daniel Barrett wrote:
Thanks for your analysis, Rolf.
I just looked at the PHP source of ParserFunctions, however, and #if is not evaluating anything:
function ifHook( &$parser, $test = '', $then = '', $else = '' ) { if ( $test !== '' ) { return $then; } else { return $else; } } $wgParser->setFunctionHook( 'if', array( &$wgExtParserFunctions, 'ifHook' ) );
So the values of $then and $else are getting evaluated somewhere else. Does this mean the issue lies with MediaWiki, not with ParserFunctions?
Now you are dragging me out on deep waters (I'm not a PHP coder really), since I actually didn't analyze the code itself, only the end result of parser behavior (when I mimicked the MW parser in Delphi Pascal code, including (most of) the ParserFunctions).
But even if I didn't analyze the PHP code very much (I'm a Delphi programmer) I still have some thoughts on where to expect to find a clue for the current behavior:
TEMPLATE ENGINE RULES
I do suspect that an answer to why *all* expressions are evaluated (unconditionally) can be found in the history of MW, which must have been to make a "template engine". That concept comes with some philosophic implications as to what a template engine should handle (which would also affect HOW it executes its internal logic).
The concept of template engine leads me to expect to find that MW parser first would collect (strip out) all expressions from the text (Wiki syntax, MAGICWORDs, HTML, ParserFunctions etc) and then manage a list of all the expressions found, which it eventually simply evaluates disregarding any logic in them (a "pure" template engine would never regard active logic anyway).
So such straight-forward execution is probably what to *expect* from a template engine, since "active programmatic" constructs SHOULD not be part of a template engine (well, this is a view in one school of thought on template engines).
However, now the reality is, despite the "ideal case" for a template engine, that there IS "active code" (ParserFunctions) involved in MW, and thus it's important how to actually evaluate boolean expressions. Expanding templates is now sometimes *very* costly.
So, with this "ideal template engine" background in mind I think that *possibly* the original design decision to NOT evaluate(parse) ParserFunction statements "conditionally" stems from the fact that the original template engine really should not have any such "active logic". But remark, this is only my guess. =)
In any case, MW Parser is no longer only a "pure" template engine, but good or bad, I don't see any possible way back to "pure" a template engine. At least not without immense amounts of manual work to refactor existing templates.
Regards,
// Rolf Lampa