Anomie has uploaded a new change for review.
https://gerrit.wikimedia.org/r/52061
Change subject: (bug 41769) Add frame:callParserFunction()
......................................................................
(bug 41769) Add frame:callParserFunction()
Requires change I339b882010dedd714e7965e25ad650ed8b8cd48f to
mediawiki/core.
Bug: 41769
Change-Id: I0138836654b0e34c5c23daaedcdf5d4f9d1c7ab2
---
M engines/LuaCommon/LuaCommon.php
M engines/LuaCommon/lualib/mw.lua
M tests/engines/LuaCommon/CommonTest.php
3 files changed, 128 insertions(+), 0 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Scribunto
refs/changes/61/52061/1
diff --git a/engines/LuaCommon/LuaCommon.php b/engines/LuaCommon/LuaCommon.php
index b99b214..bf1531e 100644
--- a/engines/LuaCommon/LuaCommon.php
+++ b/engines/LuaCommon/LuaCommon.php
@@ -76,6 +76,7 @@
'getExpandedArgument',
'getAllExpandedArguments',
'expandTemplate',
+ 'callParserFunction',
'preprocess',
'incrementExpensiveFunctionCount',
);
@@ -446,6 +447,56 @@
}
/**
+ * Handler for callParserFunction()
+ */
+ function callParserFunction( $frameId, $function, $arg1, $args ) {
+ $frame = $this->getFrameById( $frameId );
+ $fargs =
$this->getParser()->getPreprocessor()->newPartNodeArray( $args );
+
+ $result = $this->parser->callParserFunction( $frame, $function,
$arg1, $fargs );
+ if ( !$result['found'] ) {
+ throw new Scribunto_LuaError( "callParserFunction:
function \"$function\" was not found" );
+ }
+
+ # Set defaults for various flags
+ $result += array(
+ 'nowiki' => false,
+ 'isChildObj' => false,
+ 'isLocalObj' => false,
+ 'isHTML' => false,
+ 'title' => false,
+ );
+
+ $text = $result['text'];
+ if ( $result['isChildObj'] ) {
+ $newFrame = $frame->newChild( $args, $result['title'] );
+ if ( $result['nowiki'] ) {
+ $text = $newFrame->expand( $text,
PPFrame::RECOVER_ORIG );
+ } else {
+ $text = $newFrame->expand( $text );
+ }
+ }
+ if ( $result['isLocalObj'] && $result['nowiki'] ) {
+ $text = $frame->expand( $text, PPFrame::RECOVER_ORIG );
+ $result['isLocalObj'] = false;
+ }
+
+ # Replace raw HTML by a placeholder
+ if ( $result['isHTML'] ) {
+ $text = $this->parser->insertStripItem( $text );
+ } elseif ( $result['nowiki'] ) {
+ # Escape nowiki-style return values
+ $text = wfEscapeWikiText( $text );
+ }
+
+ if ( $result['isLocalObj'] ) {
+ $text = $frame->expand( $text );
+ }
+
+ return array( "$text" );
+ }
+
+ /**
* Handler for preprocess()
*/
function preprocess( $frameId, $text ) {
diff --git a/engines/LuaCommon/lualib/mw.lua b/engines/LuaCommon/lualib/mw.lua
index 593d836..f802bda 100644
--- a/engines/LuaCommon/lualib/mw.lua
+++ b/engines/LuaCommon/lualib/mw.lua
@@ -400,6 +400,36 @@
return php.expandTemplate( frameId, title, args )
end
+ function frame:callParserFunction( opt )
+ checkSelf( self, 'callParserFunction' )
+
+ if type( opt ) ~= 'table' then
+ error( "frame:callParserFunction: the first parameter
must be a table" )
+ end
+
+ local name, arg1, args
+
+ if opt.name == nil then
+ error( "frame:callParserFunction: a function name is
required" )
+ else
+ name = tostring( opt.name )
+ end
+ if opt.arg1 == nil then
+ error( "frame:callParserFunction: a first argument is
required" )
+ else
+ arg1 = tostring( opt.arg1 )
+ end
+ if opt.args == nil then
+ args = {}
+ elseif type( opt.args ) ~= 'table' then
+ error( "frame:callParserFunction: args must be a table"
)
+ else
+ args = opt.args
+ end
+
+ return php.callParserFunction( frameId, name, arg1, args )
+ end
+
function frame:preprocess( opt )
checkSelf( self, 'preprocess' )
diff --git a/tests/engines/LuaCommon/CommonTest.php
b/tests/engines/LuaCommon/CommonTest.php
index 2b8369a..5f7300f 100644
--- a/tests/engines/LuaCommon/CommonTest.php
+++ b/tests/engines/LuaCommon/CommonTest.php
@@ -233,4 +233,51 @@
) );
$this->assertSame( "ok\ttable", $ret['return'], 'child frames
have correct parents' );
}
+
+ function testCallParserFunction() {
+ global $wgContLang;
+
+ $engine = $this->getEngine();
+ $parser = $engine->getParser();
+
+ $args = array(
+ 'prevQuestions' => array(),
+ 'content' => 'return {}',
+ 'title' => Title::makeTitle( NS_MODULE, 'dummy' ),
+ );
+
+ $ret = $engine->runConsole( array(
+ 'question' => '=mw.getCurrentFrame():callParserFunction{
+ name = "ns", arg1 = ' . NS_PROJECT .'
+ }',
+ ) + $args );
+ $this->assertSame( $wgContLang->getFormattedNsText( NS_PROJECT
), $ret['return'],
+ 'callParserFunction works for {{ns:' . NS_PROJECT . '}}'
+ );
+
+ $ret = $engine->runConsole( array(
+ 'question' => '=mw.getCurrentFrame():callParserFunction{
+ name = "#tag", arg1 = "nowiki", args = { "ok" },
+ }',
+ ) + $args );
+ $this->assertNotSame( 'ok', $ret['return'], 'callParserFunction
works for #tag:nowiki' );
+ $this->assertSame( 'ok', $parser->mStripState->unstripBoth(
$ret['return'] ),
+ 'callParserFunction works for {{#tag:nowiki|ok}}'
+ );
+
+ try {
+ $ret = $engine->runConsole( array(
+ 'question' =>
'=mw.getCurrentFrame():callParserFunction{
+ name = "thisDoesNot:AndCannot:Exist",
arg1 = ""
+ }',
+ ) + $args );
+ $this->fail( "Expected LuaError not thrown for
nonexistent parser function" );
+ } catch ( Scribunto_LuaError $err ) {
+ $this->assertSame(
+ 'Lua error: callParserFunction: function
"thisDoesNot:AndCannot:Exist" was not found.',
+ $err->getMessage(),
+ 'callParserFunction correctly errors for
nonexistent function'
+ );
+ }
+ }
}
--
To view, visit https://gerrit.wikimedia.org/r/52061
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I0138836654b0e34c5c23daaedcdf5d4f9d1c7ab2
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Scribunto
Gerrit-Branch: master
Gerrit-Owner: Anomie <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits