Jackmcbarn has uploaded a new change for review.
https://gerrit.wikimedia.org/r/159238
Change subject: Expose variables (aka magic words) to Lua
......................................................................
Expose variables (aka magic words) to Lua
Currently, there's no way to get a magic word like {{NUMBEROFADMINS}} from
Lua without using frame:preprocess. Since a lot of magic words are also
parser functions ({{PROTECTIONLEVEL}} and {{PROTECTIONLEVEL:Main Page}}),
and it's impossible to call a parser function without any arguments, and
magic words never take arguments, just get the magic word instead of
calling the parser function when no arguments are provided.
Change-Id: Ia32b0625588c99796deb1175eac6b926ccd9cf6f
---
M engines/LuaCommon/LuaCommon.php
M tests/engines/LuaCommon/CommonTest.php
2 files changed, 38 insertions(+), 6 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Scribunto
refs/changes/38/159238/1
diff --git a/engines/LuaCommon/LuaCommon.php b/engines/LuaCommon/LuaCommon.php
index cd55585..0218f60 100644
--- a/engines/LuaCommon/LuaCommon.php
+++ b/engines/LuaCommon/LuaCommon.php
@@ -616,12 +616,18 @@
$function = substr( $function, 0, $colonPos );
}
if ( !isset( $args[0] ) ) {
- # It's impossible to call a parser function from
wikitext without
- # supplying an arg 0. Insist that one be provided via
Lua, too.
- throw new Scribunto_LuaError( 'callParserFunction: At
least one unnamed parameter ' .
- '(the parameter that comes after the colon in
wikitext) ' .
- 'must be provided'
- );
+ # All parser functions take at least one argument when
called, so if we
+ # didn't get any, the user is actually requesting a
variable
+ $id = $this->parser->mVariables->matchStartToEnd(
$function );
+ if ( $id === false ) {
+ throw new Scribunto_LuaError(
"callParserFunction: variable \"$function\" was not found" );
+ }
+
+ $text = $this->parser->getVariableValue( $id, $frame );
+ if ( MagicWord::getCacheTTL( $id ) > -1 ) {
+ $this->parser->getOutput()->updateCacheExpiry(
MagicWord::getCacheTTL( $id ) );
+ }
+ return array( "$text" );
}
$result = $this->parser->callParserFunction( $frame, $function,
$args );
diff --git a/tests/engines/LuaCommon/CommonTest.php
b/tests/engines/LuaCommon/CommonTest.php
index a367e12..135a07a 100644
--- a/tests/engines/LuaCommon/CommonTest.php
+++ b/tests/engines/LuaCommon/CommonTest.php
@@ -344,6 +344,16 @@
'extensionTag works for
{{#tag:pre|foo|style=margin-left: 1.6em}}'
);
+ // Test getting a variable
+ $ret = $engine->runConsole( array(
+ 'question' => '=mw.getCurrentFrame():callParserFunction(
+ "FULLPAGENAME"
+ )',
+ ) + $args );
+ $this->assertSame( 'Main Page', $ret['return'],
+ 'callParserFunction works for {{FULLPAGENAME}} (a
variable)'
+ );
+
// Test calling a non-existent function
try {
$ret = $engine->runConsole( array(
@@ -359,6 +369,22 @@
'callParserFunction correctly errors for
nonexistent function'
);
}
+
+ // Test getting a non-existent variable
+ try {
+ $ret = $engine->runConsole( array(
+ 'question' =>
'=mw.getCurrentFrame():callParserFunction(
+ "thisDoesNotExist"
+ )',
+ ) + $args );
+ $this->fail( "Expected LuaError not thrown for
nonexistent variable" );
+ } catch ( Scribunto_LuaError $err ) {
+ $this->assertSame(
+ 'Lua error: callParserFunction: variable
"thisDoesNotExist" was not found.',
+ $err->getMessage(),
+ 'callParserFunction correctly errors for
nonexistent variable'
+ );
+ }
}
function testBug62291() {
--
To view, visit https://gerrit.wikimedia.org/r/159238
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia32b0625588c99796deb1175eac6b926ccd9cf6f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Scribunto
Gerrit-Branch: master
Gerrit-Owner: Jackmcbarn <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits