mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/TitleBlacklist
synced 2024-12-01 00:56:33 +00:00
TitleBlacklist:
* Refactor code * Filter images with incorrect titles
This commit is contained in:
parent
3299f7bc9a
commit
ef2a6402e5
49
TitleBlacklist.hooks.php
Normal file
49
TitleBlacklist.hooks.php
Normal file
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Hooks for Title Blacklist
|
||||
*
|
||||
* @package MediaWiki
|
||||
* @subpackage Extensions
|
||||
* @author VasilievVV
|
||||
* @copyright © 2007 VasilievVV
|
||||
* @licence GNU General Public Licence 2.0 or later
|
||||
*/
|
||||
|
||||
class TitleBlacklistHooks {
|
||||
public static function userCan( $title, $user, $action, &$result )
|
||||
{
|
||||
global $wgTitleBlacklist;
|
||||
if ( ( $action != 'create' && $action != 'upload' ) || $user->isAllowed('tboverride') ) {
|
||||
$result = true;
|
||||
return $result;
|
||||
}
|
||||
$blacklisted = $wgTitleBlacklist->isBlacklisted( $title );
|
||||
if( is_string( $blacklisted ) ) {
|
||||
return wfMsgWikiHtml( "titleblacklist-forbidden", $blacklisted, $title->getFullText() );
|
||||
}
|
||||
|
||||
$result = true;
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function abortMove( $old, $nt, $user, &$err ) {
|
||||
global $wgTitleBlacklist;
|
||||
$blacklisted = $wgTitleBlacklist->isBlacklisted( $nt );
|
||||
if( !$user->isAllowed( 'tboverride' ) && is_string( $blacklisted ) ) {
|
||||
$err = wfMsgWikiHtml( "titleblacklist-forbidden-move", $blacklisted, $nt->getFullText(), $old->getFullText() );
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function verifyUpload( $fname, $fpath, &$err ) {
|
||||
global $wgTitleBlacklist, $wgUser;
|
||||
$blacklisted = $wgTitleBlacklist->isBlacklisted( $fname );
|
||||
if( !$wgUser->isAllowed( 'tboverride' ) && is_string( $blacklisted ) ) {
|
||||
$err = wfMsgWikiHtml( "titleblacklist-forbidden-upload", $blacklisted, $fname );
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -18,6 +18,9 @@ It matches the following blacklist regex: '''''\$1'''''
|
|||
'''A page titled \"\$2\" cannot be moved to \"\$3\"''' <br/>
|
||||
It matches the following blacklist regex: '''''\$1'''''
|
||||
</span>",
|
||||
'titleblacklist-forbidden-upload' => "
|
||||
'''A file named \"\$2\" cannot be uploaded''' <br/>
|
||||
It matches the following blacklist regex: '''''\$1'''''",
|
||||
),
|
||||
|
||||
'ar' => array(
|
||||
|
|
55
TitleBlacklist.list.php
Normal file
55
TitleBlacklist.list.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Title Blacklist class
|
||||
*
|
||||
* @package MediaWiki
|
||||
* @subpackage Extensions
|
||||
* @author VasilievVV
|
||||
* @copyright © 2007 VasilievVV
|
||||
* @licence GNU General Public Licence 2.0 or later
|
||||
*/
|
||||
class TitleBlacklist {
|
||||
private $mBlacklist = null;
|
||||
|
||||
public function load() {
|
||||
$this->mBlacklist = $this->parseBlacklist( $this->getBlacklistText() );
|
||||
}
|
||||
|
||||
public function getBlacklistText() {
|
||||
return wfMsgForContent( 'titleblacklist' );
|
||||
}
|
||||
|
||||
public function parseBlacklist( $list ) {
|
||||
$lines = preg_split( "/\r?\n/", $list );
|
||||
$result = array();
|
||||
foreach ( $lines as $line )
|
||||
{
|
||||
$line = preg_replace( "/^\\s*([^#]*)\\s*((.*)?)$/", "\\1", $line );
|
||||
$line = trim( $line );
|
||||
if ($line) $result[] = $line;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function isBlacklisted( $title ) {
|
||||
if( $title instanceof Title ) {
|
||||
$title = $title->getFullText();
|
||||
}
|
||||
$blacklist = $this->getBlacklist();
|
||||
foreach ( $blacklist as $item ) {
|
||||
if( preg_match( "/^$item$/is", $title ) ) {
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getBlacklist() {
|
||||
if( is_null( $this->mBlacklist ) ) {
|
||||
$this->load();
|
||||
}
|
||||
return $this->mBlacklist;
|
||||
}
|
||||
}
|
|
@ -9,13 +9,22 @@ $wgExtensionCredits['other'][] = array(
|
|||
'description' => 'Allows to forbide creation of pages with specified titles'
|
||||
);
|
||||
|
||||
$wgExtensionFunctions[] = 'efSetupTitleBlacklistMessages';
|
||||
$wgHooks['userCan'][] = 'efUserCanCreateTitle';
|
||||
$wgHooks['AbortMove'][] = 'efTitleBlacklistCanMove';
|
||||
$wgAutoloadClasses['TitleBlacklist'] = dirname( __FILE__ ) . '/TitleBlacklist.list.php';
|
||||
$wgAutoloadClasses['TitleBlacklistHooks'] = dirname( __FILE__ ) . '/TitleBlacklist.hooks.php';
|
||||
|
||||
$wgExtensionFunctions[] = 'efInitTitleBlacklist';
|
||||
|
||||
|
||||
$wgAvailableRights[] = 'tboverride';
|
||||
$wgGroupPermissions['sysop']['tboverride'] = true;
|
||||
|
||||
function efInitTitleBlacklist() {
|
||||
global $wgTitleBlacklist;
|
||||
$wgTitleBlacklist = new TitleBlacklist();
|
||||
efSetupTitleBlacklistMessages();
|
||||
efSetupTitleBlacklistHooks();
|
||||
}
|
||||
|
||||
function efSetupTitleBlacklistMessages() {
|
||||
global $wgMessageCache;
|
||||
require_once( 'TitleBlacklist.i18n.php' );
|
||||
|
@ -24,56 +33,10 @@ function efSetupTitleBlacklistMessages() {
|
|||
}
|
||||
}
|
||||
|
||||
function efGetTitleBlacklist() {
|
||||
return wfMsgForContent( 'titleblacklist' );
|
||||
}
|
||||
|
||||
function efParseTitleBlacklist( $list ) {
|
||||
$lines = preg_split( "/\r?\n/", $list );
|
||||
$result = array();
|
||||
foreach ( $lines as $line )
|
||||
{
|
||||
$line = preg_replace( "/^\\s*([^#]*)\\s*((.*)?)$/", "\\1", $line );
|
||||
$line = trim( $line );
|
||||
if ($line) $result[] = $line;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
function efIsBlacklistedTitle( $title ) {
|
||||
if( $title instanceof Title ) {
|
||||
$title = $title->getFullText();
|
||||
}
|
||||
$blacklist = efParseTitleBlacklist( efGetTitleBlacklist() );
|
||||
foreach ( $blacklist as $item ) {
|
||||
if( preg_match( "/^$item$/is", $title ) ) {
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function efUserCanCreateTitle( $title, $user, $action, &$result )
|
||||
{
|
||||
if ( $action != 'create' || $user->isAllowed('tboverride') ) {
|
||||
$result = true;
|
||||
return $result;
|
||||
}
|
||||
$blacklisted = efIsBlacklistedTitle( $title );
|
||||
if( is_string( $blacklisted ) ) {
|
||||
return wfMsgWikiHtml( "titleblacklist-forbidden", $blacklisted, $title->getFullText() );
|
||||
}
|
||||
|
||||
$result = true;
|
||||
return $result;
|
||||
}
|
||||
|
||||
function efTitleBlacklistCanMove( $old, $nt, $user, &$err ) {
|
||||
$blacklisted = efIsBlacklistedTitle( $nt );
|
||||
if( !$user->isAllowed( 'tboverride' ) && is_string( $blacklisted ) ) {
|
||||
$err = wfMsgWikiHtml( "titleblacklist-forbidden-move", $blacklisted, $nt->getFullText(), $old->getFullText() );
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
function efSetupTitleBlacklistHooks() {
|
||||
global $wgHooks;
|
||||
$titleBlacklistHooks = new TitleBlacklistHooks();
|
||||
$wgHooks['userCan'][] = array( $titleBlacklistHooks, 'userCan' );
|
||||
$wgHooks['AbortMove'][] = array( $titleBlacklistHooks, 'abortMove' );
|
||||
$wgHooks['UploadVerification'][] = array( $titleBlacklistHooks, 'verifyUpload' );
|
||||
}
|
Loading…
Reference in a new issue