From bf188e7af1bccefba5662e80e635249b695e3e3f Mon Sep 17 00:00:00 2001 From: Jackmcbarn Date: Tue, 7 Jan 2014 13:08:24 -0500 Subject: [PATCH] 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 --- TitleBlacklist.library.php | 33 +++++++++++++++++++++++++++++++++ TitleBlacklist.list.php | 33 +++++++++++++++++++++++---------- TitleBlacklist.php | 7 +++++++ mw.ext.TitleBlacklist.lua | 22 ++++++++++++++++++++++ 4 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 TitleBlacklist.library.php create mode 100644 mw.ext.TitleBlacklist.lua diff --git a/TitleBlacklist.library.php b/TitleBlacklist.library.php new file mode 100644 index 00000000..0c2a599c --- /dev/null +++ b/TitleBlacklist.library.php @@ -0,0 +1,33 @@ + 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 ); + } + +} diff --git a/TitleBlacklist.list.php b/TitleBlacklist.list.php index cec4dd89..3947e8ae 100644 --- a/TitleBlacklist.list.php +++ b/TitleBlacklist.list.php @@ -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; } /** diff --git a/TitleBlacklist.php b/TitleBlacklist.php index 2bb62b0a..acd946d6 100644 --- a/TitleBlacklist.php +++ b/TitleBlacklist.php @@ -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', diff --git a/mw.ext.TitleBlacklist.lua b/mw.ext.TitleBlacklist.lua new file mode 100644 index 00000000..1b48eed1 --- /dev/null +++ b/mw.ext.TitleBlacklist.lua @@ -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