mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/TitleBlacklist
synced 2024-11-23 21:53:40 +00:00
Add mw.ext.TitleBlacklist.test
Add lua method mw.ext.TitleBlacklist.test(action, title), which tests whether a titleblacklist entry is active for a given title and action, and if so, returns details of the entry. Change-Id: I4360d1ad4e602d58dabd12b683d338a0e09a2950
This commit is contained in:
parent
3e8165cf33
commit
bf188e7af1
33
TitleBlacklist.library.php
Normal file
33
TitleBlacklist.library.php
Normal file
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
class Scribunto_LuaTitleBlacklistLibrary extends Scribunto_LuaLibraryBase {
|
||||
public function register() {
|
||||
$lib = array(
|
||||
'test' => array( $this, 'test' ),
|
||||
);
|
||||
|
||||
$this->getEngine()->registerInterface( __DIR__ . '/mw.ext.TitleBlacklist.lua', $lib, array() );
|
||||
}
|
||||
|
||||
public function test( $action = null, $title = null ) {
|
||||
$this->checkType( 'mw.ext.TitleBlacklist.test', 1, $action, 'string' );
|
||||
$this->checkTypeOptional( 'mw.ext.TitleBlacklist.test', 2, $title, 'string', '' );
|
||||
$this->incrementExpensiveFunctionCount();
|
||||
if ( $title == '' ) {
|
||||
$title = $this->getParser()->mTitle->getPrefixedText();
|
||||
}
|
||||
$entry = TitleBlacklist::singleton()->isBlacklisted( $title, $action );
|
||||
if ( $entry ) {
|
||||
return array( array(
|
||||
'params' => $entry->getParams(),
|
||||
'regex' => $entry->getRegex(),
|
||||
'raw' => $entry->getRaw(),
|
||||
'version' => $entry->getFormatVersion(),
|
||||
'message' => $entry->getErrorMessage( $action ),
|
||||
'custommessage' => $entry->getCustomMessage()
|
||||
) );
|
||||
}
|
||||
return array( null );
|
||||
}
|
||||
|
||||
}
|
|
@ -156,7 +156,12 @@ class TitleBlacklist {
|
|||
if ( $override && self::userCanOverride( $user, $action ) ) {
|
||||
return false;
|
||||
} else {
|
||||
return $this->isBlacklisted( $title, $action );
|
||||
$entry = $this->isBlacklisted( $title, $action );
|
||||
if ( !$entry ) {
|
||||
return false;
|
||||
}
|
||||
$params = $entry->getParams();
|
||||
return isset( $params['autoconfirmed'] ) && $user->isAllowed( 'autoconfirmed' ) ? false : $entry;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -172,17 +177,29 @@ class TitleBlacklist {
|
|||
public function isBlacklisted( $title, $action = 'edit' ) {
|
||||
if ( !( $title instanceof Title ) ) {
|
||||
$title = Title::newFromText( $title );
|
||||
if ( !( $title instanceof Title ) ) {
|
||||
// The fact that the page name is invalid will stop whatever
|
||||
// action is going through. No sense in doing more work here.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
$blacklist = $this->getBlacklist();
|
||||
$autoconfirmedItem = false;
|
||||
foreach ( $blacklist as $item ) {
|
||||
if ( $item->matches( $title->getFullText(), $action ) ) {
|
||||
if ( $this->isWhitelisted( $title, $action ) ) {
|
||||
return false;
|
||||
}
|
||||
return $item; // "returning true"
|
||||
$params = $item->getParams();
|
||||
if ( !isset( $params['autoconfirmed'] ) ) {
|
||||
return $item;
|
||||
}
|
||||
if ( !$autoconfirmedItem ) {
|
||||
$autoconfirmedItem = $item;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return $autoconfirmedItem;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -370,11 +387,7 @@ class TitleBlacklistEntry {
|
|||
$match = preg_match( "/^(?:{$this->mRegex})$/us" . ( isset( $this->mParams['casesensitive'] ) ? '' : 'i' ), $title );
|
||||
wfRestoreWarnings();
|
||||
|
||||
global $wgUser;
|
||||
if ( $match ) {
|
||||
if ( isset( $this->mParams['autoconfirmed'] ) && $wgUser->isAllowed( 'autoconfirmed' ) ) {
|
||||
return false;
|
||||
}
|
||||
if ( isset( $this->mParams['moveonly'] ) && $action != 'move' ) {
|
||||
return false;
|
||||
}
|
||||
|
@ -481,10 +494,10 @@ class TitleBlacklistEntry {
|
|||
}
|
||||
|
||||
/**
|
||||
* @return array This entry's options
|
||||
* @return array This entry's parameters
|
||||
*/
|
||||
public function getOptions() {
|
||||
return $this->mOptions;
|
||||
public function getParams() {
|
||||
return $this->mParams;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -22,6 +22,7 @@ $dir = __DIR__;
|
|||
$wgExtensionMessagesFiles['TitleBlacklist'] = $dir . '/TitleBlacklist.i18n.php';
|
||||
$wgAutoloadClasses['TitleBlacklist'] = $dir . '/TitleBlacklist.list.php';
|
||||
$wgAutoloadClasses['TitleBlacklistHooks'] = $dir . '/TitleBlacklist.hooks.php';
|
||||
$wgAutoloadClasses['Scribunto_LuaTitleBlacklistLibrary'] = $dir . '/TitleBlacklist.library.php';
|
||||
|
||||
/** @defgroup Title blacklist source types
|
||||
* @{
|
||||
|
@ -80,6 +81,12 @@ $wgHooks['UnitTestsList'][] = function( &$files ) {
|
|||
$files += glob( __DIR__ . '/tests/*Test.php' );
|
||||
return true;
|
||||
};
|
||||
$wgHooks['ScribuntoExternalLibraries'][] = function( $engine, array &$extraLibraries ) {
|
||||
if( $engine == 'lua' ) {
|
||||
$extraLibraries['mw.ext.TitleBlacklist'] = 'Scribunto_LuaTitleBlacklistLibrary';
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
$wgResourceModules['mediawiki.api.titleblacklist'] = array(
|
||||
'scripts' => 'mediawiki.api.titleblacklist.js',
|
||||
|
|
22
mw.ext.TitleBlacklist.lua
Normal file
22
mw.ext.TitleBlacklist.lua
Normal file
|
@ -0,0 +1,22 @@
|
|||
local TitleBlacklist = {}
|
||||
local php
|
||||
|
||||
function TitleBlacklist.test( action, title )
|
||||
return php.test( action, title )
|
||||
end
|
||||
|
||||
function TitleBlacklist.setupInterface( options )
|
||||
-- Boilerplate
|
||||
TitleBlacklist.setupInterface = nil
|
||||
php = mw_interface
|
||||
mw_interface = nil
|
||||
|
||||
-- Register this library in the "mw" global
|
||||
mw = mw or {}
|
||||
mw.ext = mw.ext or {}
|
||||
mw.ext.TitleBlacklist = TitleBlacklist
|
||||
|
||||
package.loaded['mw.ext.TitleBlacklist'] = TitleBlacklist
|
||||
end
|
||||
|
||||
return TitleBlacklist
|
Loading…
Reference in a new issue