Expose cascading protection directly to Lua

Add a way to fetch cascading protection information from Lua without
needing to call the CASCADINGSOURCES parser function.

Change-Id: I1b3ac18af11d3066f78d27b31da8d6709a6a2631
This commit is contained in:
Jackmcbarn 2014-05-10 15:05:45 -04:00
parent ee1e9b5207
commit fd9ecb9cbe
4 changed files with 56 additions and 21 deletions

View file

@ -16,6 +16,7 @@ class Scribunto_LuaTitleLibrary extends Scribunto_LuaLibraryBase {
'getContent' => array( $this, 'getContent' ),
'fileExists' => array( $this, 'fileExists' ),
'protectionLevels' => array( $this, 'protectionLevels' ),
'cascadingProtection' => array( $this, 'cascadingProtection' ),
);
$this->getEngine()->registerInterface( 'mw.title.lua', $lib, array(
'thisTitle' => $this->returnTitleToLua( $this->getTitle() ),
@ -256,16 +257,11 @@ class Scribunto_LuaTitleLibrary extends Scribunto_LuaLibraryBase {
return array( (bool)$file->exists() );
}
private function makeRestrictionsArraysOneBased( $restrictions ) {
$ret = array();
foreach ( $restrictions as $action => $requirements ) {
if ( empty( $requirements ) ) {
$ret[$action] = $requirements;
} else {
$ret[$action] = array_combine( range( 1, count( $requirements ) ), array_values( $requirements ) );
private static function makeArrayOneBased( $arr ) {
if ( empty( $arr ) ) {
return $arr;
}
}
return $ret;
return array_combine( range( 1, count( $arr ) ), array_values( $arr ) );
}
public function protectionLevels( $text ) {
@ -275,19 +271,30 @@ class Scribunto_LuaTitleLibrary extends Scribunto_LuaLibraryBase {
return array( null );
}
// @todo Once support for MediaWiki prior to 1.23 is dropped, remove this if block
// (and maybe inline makeRestrictionsArraysOneBased)
if ( !is_callable( array( $title, 'areRestrictionsLoaded' ) ) ) {
if ( !$title->mRestrictionsLoaded ) {
$this->incrementExpensiveFunctionCount();
$title->loadRestrictions();
}
return array( $this->makeRestrictionsArraysOneBased( $title->mRestrictions ) );
}
if ( !$title->areRestrictionsLoaded() ) {
$this->incrementExpensiveFunctionCount();
}
return array( $this->makeRestrictionsArraysOneBased( $title->getAllRestrictions() ) );
return array( array_map( 'Scribunto_LuaTitleLibrary::makeArrayOneBased', $title->getAllRestrictions() ) );
}
public function cascadingProtection( $text ) {
$this->checkType( 'cascadingProtection', 1, $text, 'string' );
$title = Title::newFromText( $text );
if ( !$title ) {
return array( null );
}
if ( !$title->areCascadeProtectionSourcesLoaded() ) {
$this->incrementExpensiveFunctionCount();
}
list( $sources, $restrictions ) = $title->getCascadeProtectionSources();
return array( array(
'sources' => Scribunto_LuaTitleLibrary::makeArrayOneBased( array_map(
function ( $t ) {
return $t->getPrefixedText();
},
$sources ) ),
'restrictions' => array_map( 'Scribunto_LuaTitleLibrary::makeArrayOneBased', $restrictions )
) );
}
}

View file

@ -165,6 +165,7 @@ local function makeTitleObject( data )
subjectPageTitle = true,
fileExists = true,
protectionLevels = true,
cascadingProtection = true,
}
for k in pairs( data ) do
readOnlyFields[k] = true
@ -236,6 +237,12 @@ local function makeTitleObject( data )
end
return data.protectionLevels
end
if k == 'cascadingProtection' then
if data.cascadingProtection == nil then
data.cascadingProtection = php.cascadingProtection( data.prefixedText )
end
return data.cascadingProtection
end
return data[k]
end,

View file

@ -49,26 +49,39 @@ class Scribunto_LuaTitleLibraryTests extends Scribunto_LuaEngineTestBase {
'Summary'
);
// Set restrictions for protectionLevels test
// Set restrictions for protectionLevels and cascadingProtection tests
// Since mRestrictionsLoaded is true, they don't count as expensive
$title = Title::newFromText( 'Main Page' );
$title->mRestrictionsLoaded = true;
$title->mRestrictions = array( 'edit' => array(), 'move' => array() );
$title->mCascadeSources = array( Title::makeTitle( NS_MAIN, "Lockbox" ), Title::makeTitle( NS_MAIN, "Lockbox2" ) );
$title->mCascadingRestrictions = array( 'edit' => array( 'sysop' ) );
$title = Title::newFromText( 'Module:TestFramework' );
$title->mRestrictionsLoaded = true;
$title->mRestrictions = array( 'edit' => array( 'sysop', 'bogus' ), 'move' => array( 'sysop', 'bogus' ) );
$title->mCascadeSources = array();
$title->mCascadingRestrictions = array();
$title = Title::newFromText( 'scribuntotitletest:Module:TestFramework' );
$title->mRestrictionsLoaded = true;
$title->mRestrictions = array();
$title->mCascadeSources = array();
$title->mCascadingRestrictions = array();
$title = Title::newFromText( 'Talk:Has/A/Subpage' );
$title->mRestrictionsLoaded = true;
$title->mRestrictions = array( 'create' => array( 'sysop' ) );
$title->mCascadeSources = array();
$title->mCascadingRestrictions = array();
$title = Title::newFromText( 'Not/A/Subpage' );
$title->mRestrictionsLoaded = true;
$title->mRestrictions = array( 'edit' => array( 'autoconfirmed' ), 'move' => array( 'sysop' ) );
$title->mCascadeSources = array();
$title->mCascadingRestrictions = array();
$title = Title::newFromText( 'Module talk:Test Framework' );
$title->mRestrictionsLoaded = true;
$title->mRestrictions = array( 'edit' => array(), 'move' => array( 'sysop' ) );
$title->mCascadeSources = array();
$title->mCascadingRestrictions = array();
// Note this depends on every iteration of the data provider running with a clean parser
$this->getEngine()->getParser()->getOptions()->setExpensiveParserFunctionLimit( 10 );

View file

@ -252,6 +252,14 @@ local tests = {
{ edit = {}, move = { 'sysop' } }, { edit = {}, move = { 'sysop' } }
}
},
{ name = '.cascadingProtection', func = prop_foreach,
args = { 'cascadingProtection' },
expect = {
{ restrictions = { edit = { 'sysop' } }, sources = { 'Lockbox', 'Lockbox2' } }, { restrictions = {}, sources = {} },
{ restrictions = {}, sources = {} }, { restrictions = {}, sources = {} }, { restrictions = {}, sources = {} },
{ restrictions = {}, sources = {} }, { restrictions = {}, sources = {} }
}
},
{ name = '.inNamespace()', func = func_foreach,
args = { 'inNamespace', 'Module' },
expect = { false, true, false, false, false, false, false }