mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Scribunto
synced 2024-11-24 00:05:00 +00:00
(bug 45512) Add mw.title:getContent()
Bug: 45512 Change-Id: I630033d367a47f7f80809a0918d3e6feede3fe41
This commit is contained in:
parent
61a4dccabd
commit
e004b415d6
|
@ -13,6 +13,7 @@ class Scribunto_LuaTitleLibrary extends Scribunto_LuaLibraryBase {
|
|||
'newTitle' => array( $this, 'newTitle' ),
|
||||
'makeTitle' => array( $this, 'makeTitle' ),
|
||||
'getUrl' => array( $this, 'getUrl' ),
|
||||
'getContent' => array( $this, 'getContent' ),
|
||||
);
|
||||
$this->getEngine()->registerInterface( 'mw.title.lua', $lib, array(
|
||||
'thisTitle' => $this->returnTitleToLua( $this->getTitle() ),
|
||||
|
@ -195,4 +196,20 @@ class Scribunto_LuaTitleLibrary extends Scribunto_LuaLibraryBase {
|
|||
}
|
||||
return array( call_user_func_array( array( $title, $func ), $args ) );
|
||||
}
|
||||
|
||||
function getContent( $text ) {
|
||||
$title = Title::newFromText( $text );
|
||||
if ( !$title ) {
|
||||
return array( null );
|
||||
}
|
||||
$rev = Revision::newFromTitle( $title );
|
||||
if ( !$rev ) {
|
||||
return array( null );
|
||||
}
|
||||
$content = $rev->getContent();
|
||||
if ( !$content ) {
|
||||
return array( null );
|
||||
}
|
||||
return array( $content->serialize() );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -144,6 +144,16 @@ local function makeTitleObject( data )
|
|||
return php.getUrl( self.fullText, 'canonicalUrl', query )
|
||||
end
|
||||
|
||||
function data:getContent()
|
||||
checkSelf( self, 'getContent' )
|
||||
local content = php.getContent( self.fullText )
|
||||
data.getContent = function ( self )
|
||||
checkSelf( self, 'getContent' )
|
||||
return content
|
||||
end
|
||||
return content
|
||||
end
|
||||
|
||||
return setmetatable( obj, {
|
||||
__eq = title.equals,
|
||||
__lt = lt,
|
||||
|
|
|
@ -39,6 +39,13 @@ class Scribunto_LuaTitleLibraryTests extends Scribunto_LuaEngineTestBase {
|
|||
function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Page for getContent test
|
||||
$page = WikiPage::factory( Title::newFromText( 'ScribuntoTestPage' ) );
|
||||
$page->doEditContent(
|
||||
new WikitextContent( '{{int:mainpage}}<includeonly>...</includeonly><noinclude>...</noinclude>' ),
|
||||
'Summary'
|
||||
);
|
||||
|
||||
// Note this depends on every iteration of the data provider running with a clean parser
|
||||
$this->getEngine()->getParser()->getOptions()->setExpensiveParserFunctionLimit( 10 );
|
||||
|
||||
|
|
|
@ -40,6 +40,10 @@ local function test_expensive_cached()
|
|||
return 'did not error'
|
||||
end
|
||||
|
||||
local function test_getContent()
|
||||
return mw.title.new( 'ScribuntoTestPage' ):getContent()
|
||||
end
|
||||
|
||||
-- Tests
|
||||
local tests = {
|
||||
{ name = 'tostring', func = identity, type = 'ToString',
|
||||
|
@ -257,6 +261,10 @@ local tests = {
|
|||
}
|
||||
},
|
||||
|
||||
{ name = '.getContent()', func = test_getContent,
|
||||
expect = { '{{int:mainpage}}<includeonly>...</includeonly><noinclude>...</noinclude>' }
|
||||
},
|
||||
|
||||
{ name = 'expensive functions', func = test_expensive,
|
||||
expect = 'too many expensive function calls'
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue