A parser function demonstrating the bug
SVN r30106 breaks a part of the {{#ask}} function of SMW.
I will first describe what this function does and how it breaks, then I will present a simple test-case.
The #ask function has a mode where it takes a template, and a query for certain properties. For each page that matches the given property, it fills out the template with some parameters relating to the page and the query. It then lets the parser preprocess the constructed string and returns it. This worked fine until r30105, under r30106 it breaks in some cases, namely when the template which is to be expanded contains tags like <nowiki>.
The relevant code is in extensions/SemanticMediaWiki/includes/SMW_QP_Template.php::getResultText() and does basically:
$myparser = clone $wgParser; $parser_options = new ParserOptions(); $result = $myparser->preprocess($text, $wgTitle, $parser_options);
where $text is a string with many templates concatenated (e.g. $text='{{t|1=foo}}{{t|1=bar}}{{t|1=baz}}')
The testcase:
A page Template:Bugtest containing the following line:
<nowiki>Bug</nowiki>
An extension (also attached):
<?php
$wgExtensionFunctions[] = 'efTest_Setup';
$wgHooks['LanguageGetMagic'][] = 'efTest_Magic';
function efTest_Setup() {
global $wgParser; $wgParser->setFunctionHook( 'construct', 'efTest_Construct' );
}
function efTest_Magic( &$magicWords, $langCode ) {
$magicWords['construct'] = array( 0, 'construct' ); return true;
}
function efTest_Construct( &$parser, $template = '' ) {
global $wgTitle, $wgParser; $myparser = clone $wgParser; $parser_options = new ParserOptions(); $text = '{{'.$template.'}}'; $result = $myparser->preprocess($text, $wgTitle, $parser_options); return $result;
}
I found that the function works if the preprocess call is replaced by
$result = $myparser->preprocessToDom($text, $wgTitle, $parser_options); return array( $result, 'isLocalObj' => true );
For SMW, this is not so easy, however, as it currently depends on the preprocessed value being a string (it concatenates it with other strings, for example). Additionally, changing to preprocessToDom would mean that the function would need to check for old vs new parser.
Summary: There should be a way for extensions to properly expand templates, or a flag by which they can tell the parser: "The returned text contains templates, please expand them".
Version: 1.12.x
Severity: normal
Whiteboard: aklapper-moreinfo
Attached: