2012-01-17 06:13:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for different kinds of blacklists
|
|
|
|
*/
|
|
|
|
abstract class BaseBlacklist {
|
|
|
|
/**
|
|
|
|
* Array of blacklist sources
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-06-06 16:29:27 +00:00
|
|
|
public $files = [];
|
2012-01-17 06:13:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Array containing regexes to test against
|
|
|
|
*
|
|
|
|
* @var bool|array
|
|
|
|
*/
|
|
|
|
protected $regexes = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Chance of receiving a warning when the filter is hit
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $warningChance = 100;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $warningTime = 600;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $expiryTime = 900;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array containing blacklists that extend BaseBlacklist
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-06-06 16:29:27 +00:00
|
|
|
private static $blacklistTypes = [
|
2012-01-17 06:13:46 +00:00
|
|
|
'spam' => 'SpamBlacklist',
|
2012-01-18 23:29:37 +00:00
|
|
|
'email' => 'EmailBlacklist',
|
2017-06-06 16:29:27 +00:00
|
|
|
];
|
2012-01-17 06:13:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of blacklist instances
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-06-06 16:29:27 +00:00
|
|
|
private static $instances = [];
|
2012-01-17 06:13:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param array $settings
|
|
|
|
*/
|
2017-06-06 16:29:27 +00:00
|
|
|
function __construct( $settings = [] ) {
|
2012-01-17 06:13:46 +00:00
|
|
|
foreach ( $settings as $name => $value ) {
|
|
|
|
$this->$name = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-22 20:00:28 +00:00
|
|
|
/**
|
|
|
|
* @param array $links
|
|
|
|
* @param Title $title
|
|
|
|
* @param bool $preventLog
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
abstract public function filter( array $links, Title $title, $preventLog = false );
|
|
|
|
|
2012-01-17 06:13:46 +00:00
|
|
|
/**
|
|
|
|
* Adds a blacklist class to the registry
|
|
|
|
*
|
|
|
|
* @param $type string
|
|
|
|
* @param $class string
|
|
|
|
*/
|
|
|
|
public static function addBlacklistType( $type, $class ) {
|
|
|
|
self::$blacklistTypes[$type] = $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the array of blacklist types currently defined
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getBlacklistTypes() {
|
|
|
|
return self::$blacklistTypes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an instance of the given blacklist
|
|
|
|
*
|
|
|
|
* @param $type string Code for the blacklist
|
|
|
|
* @return BaseBlacklist
|
2015-01-10 03:04:35 +00:00
|
|
|
* @throws Exception
|
2012-01-17 06:13:46 +00:00
|
|
|
*/
|
|
|
|
public static function getInstance( $type ) {
|
|
|
|
if ( !isset( self::$blacklistTypes[$type] ) ) {
|
2015-01-10 03:04:35 +00:00
|
|
|
throw new Exception( "Invalid blacklist type '$type' passed to " . __METHOD__ );
|
2012-01-17 06:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( !isset( self::$instances[$type] ) ) {
|
|
|
|
global $wgBlacklistSettings;
|
|
|
|
|
|
|
|
// Prevent notices
|
|
|
|
if ( !isset( $wgBlacklistSettings[$type] ) ) {
|
2017-06-06 16:29:27 +00:00
|
|
|
$wgBlacklistSettings[$type] = [];
|
2012-01-17 06:13:46 +00:00
|
|
|
}
|
|
|
|
|
2015-12-03 18:41:12 +00:00
|
|
|
$class = self::$blacklistTypes[$type];
|
|
|
|
self::$instances[$type] = new $class( $wgBlacklistSettings[$type] );
|
2012-01-17 06:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self::$instances[$type];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the code for the blacklist implementation
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
abstract protected function getBlacklistType();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the given local page title is a spam regex source.
|
|
|
|
*
|
|
|
|
* @param Title $title
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-01-18 23:29:37 +00:00
|
|
|
public static function isLocalSource( Title $title ) {
|
2012-01-17 06:13:46 +00:00
|
|
|
global $wgDBname, $wgBlacklistSettings;
|
|
|
|
|
2017-06-06 16:29:27 +00:00
|
|
|
if ( $title->getNamespace() == NS_MEDIAWIKI ) {
|
|
|
|
$sources = [];
|
2012-01-17 06:13:46 +00:00
|
|
|
foreach ( self::$blacklistTypes as $type => $class ) {
|
|
|
|
$type = ucfirst( $type );
|
2017-06-06 16:29:27 +00:00
|
|
|
$sources += [
|
2012-01-17 06:13:46 +00:00
|
|
|
"$type-blacklist",
|
|
|
|
"$type-whitelist"
|
2017-06-06 16:29:27 +00:00
|
|
|
];
|
2012-01-17 06:13:46 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 16:29:27 +00:00
|
|
|
if ( in_array( $title->getDBkey(), $sources ) ) {
|
2012-01-17 06:13:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$thisHttp = wfExpandUrl( $title->getFullUrl( 'action=raw' ), PROTO_HTTP );
|
|
|
|
$thisHttpRegex = '/^' . preg_quote( $thisHttp, '/' ) . '(?:&.*)?$/';
|
|
|
|
|
2017-06-06 16:29:27 +00:00
|
|
|
$files = [];
|
2012-01-17 06:13:46 +00:00
|
|
|
foreach ( self::$blacklistTypes as $type => $class ) {
|
|
|
|
if ( isset( $wgBlacklistSettings[$type]['files'] ) ) {
|
|
|
|
$files += $wgBlacklistSettings[$type]['files'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-06 16:29:27 +00:00
|
|
|
foreach ( $files as $fileName ) {
|
|
|
|
$matches = [];
|
2012-01-17 06:13:46 +00:00
|
|
|
if ( preg_match( '/^DB: (\w*) (.*)$/', $fileName, $matches ) ) {
|
|
|
|
if ( $wgDBname == $matches[1] ) {
|
2017-06-06 16:29:27 +00:00
|
|
|
if ( $matches[2] == $title->getPrefixedDbKey() ) {
|
2012-01-17 06:13:46 +00:00
|
|
|
// Local DB fetch of this page...
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2017-06-06 16:29:27 +00:00
|
|
|
} elseif ( preg_match( $thisHttpRegex, $fileName ) ) {
|
2012-01-17 06:13:46 +00:00
|
|
|
// Raw view of this page
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-18 23:29:37 +00:00
|
|
|
/**
|
|
|
|
* Returns the type of blacklist from the given title
|
|
|
|
*
|
2017-07-06 01:13:28 +00:00
|
|
|
* @todo building a regex for this is pretty overkill
|
2012-01-18 23:29:37 +00:00
|
|
|
* @param Title $title
|
|
|
|
* @return bool|string
|
|
|
|
*/
|
|
|
|
public static function getTypeFromTitle( Title $title ) {
|
2017-07-06 01:13:28 +00:00
|
|
|
global $wgContLang;
|
|
|
|
|
|
|
|
$types = array_map( [ $wgContLang, 'ucfirst' ], array_keys( self::$blacklistTypes ) );
|
|
|
|
$regex = '/(' . implode( '|', $types ). ')-(?:blacklist|whitelist)/';
|
2012-01-18 23:29:37 +00:00
|
|
|
|
|
|
|
if ( preg_match( $regex, $title->getDBkey(), $m ) ) {
|
|
|
|
return strtolower( $m[1] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-17 06:13:46 +00:00
|
|
|
/**
|
|
|
|
* Fetch local and (possibly cached) remote blacklists.
|
|
|
|
* Will be cached locally across multiple invocations.
|
|
|
|
* @return array set of regular expressions, potentially empty.
|
|
|
|
*/
|
|
|
|
function getBlacklists() {
|
2017-06-06 16:29:27 +00:00
|
|
|
if ( $this->regexes === false ) {
|
2012-01-17 06:13:46 +00:00
|
|
|
$this->regexes = array_merge(
|
|
|
|
$this->getLocalBlacklists(),
|
|
|
|
$this->getSharedBlacklists() );
|
|
|
|
}
|
|
|
|
return $this->regexes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the local blacklist
|
|
|
|
*
|
|
|
|
* @return array Regular expressions
|
|
|
|
*/
|
|
|
|
public function getLocalBlacklists() {
|
2015-07-28 22:01:39 +00:00
|
|
|
$that = $this;
|
|
|
|
$type = $this->getBlacklistType();
|
|
|
|
|
|
|
|
return ObjectCache::getMainWANInstance()->getWithSetCallback(
|
|
|
|
wfMemcKey( 'spamblacklist', $type, 'blacklist-regex' ),
|
2015-10-08 02:18:50 +00:00
|
|
|
$this->expiryTime,
|
2015-07-28 22:01:39 +00:00
|
|
|
function () use ( $that, $type ) {
|
|
|
|
return SpamRegexBatch::regexesFromMessage( "{$type}-blacklist", $that );
|
2015-10-08 02:18:50 +00:00
|
|
|
}
|
2015-07-28 22:01:39 +00:00
|
|
|
);
|
2012-01-17 06:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the (local) whitelist
|
|
|
|
*
|
|
|
|
* @return array Regular expressions
|
|
|
|
*/
|
|
|
|
public function getWhitelists() {
|
2015-07-28 22:01:39 +00:00
|
|
|
$that = $this;
|
|
|
|
$type = $this->getBlacklistType();
|
|
|
|
|
|
|
|
return ObjectCache::getMainWANInstance()->getWithSetCallback(
|
|
|
|
wfMemcKey( 'spamblacklist', $type, 'whitelist-regex' ),
|
2015-10-08 02:18:50 +00:00
|
|
|
$this->expiryTime,
|
2015-07-28 22:01:39 +00:00
|
|
|
function () use ( $that, $type ) {
|
|
|
|
return SpamRegexBatch::regexesFromMessage( "{$type}-whitelist", $that );
|
2015-10-08 02:18:50 +00:00
|
|
|
}
|
2015-07-28 22:01:39 +00:00
|
|
|
);
|
2012-01-17 06:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch (possibly cached) remote blacklists.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function getSharedBlacklists() {
|
|
|
|
$listType = $this->getBlacklistType();
|
|
|
|
|
|
|
|
wfDebugLog( 'SpamBlacklist', "Loading $listType regex..." );
|
|
|
|
|
|
|
|
if ( count( $this->files ) == 0 ){
|
|
|
|
# No lists
|
|
|
|
wfDebugLog( 'SpamBlacklist', "no files specified\n" );
|
2017-06-06 16:29:27 +00:00
|
|
|
return [];
|
2012-01-17 06:13:46 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 22:01:39 +00:00
|
|
|
$miss = false;
|
|
|
|
|
|
|
|
$that = $this;
|
|
|
|
$regexes = ObjectCache::getMainWANInstance()->getWithSetCallback(
|
|
|
|
// This used to be cached per-site, but that could be bad on a shared
|
|
|
|
// server where not all wikis have the same configuration.
|
|
|
|
wfMemcKey( 'spamblacklist', $listType, 'shared-blacklist-regex' ),
|
2015-10-08 02:18:50 +00:00
|
|
|
$this->expiryTime,
|
2015-07-28 22:01:39 +00:00
|
|
|
function () use ( $that, &$miss ) {
|
|
|
|
$miss = true;
|
|
|
|
return $that->buildSharedBlacklists();
|
2015-10-08 02:18:50 +00:00
|
|
|
}
|
2015-07-28 22:01:39 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if ( !$miss ) {
|
2012-01-17 06:13:46 +00:00
|
|
|
wfDebugLog( 'SpamBlacklist', "Got shared spam regexes from cache\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $regexes;
|
|
|
|
}
|
|
|
|
|
2015-07-28 22:01:39 +00:00
|
|
|
/**
|
|
|
|
* Clear all primary blacklist cache keys
|
|
|
|
*
|
|
|
|
* @note: this method is unused atm
|
|
|
|
*/
|
2012-01-17 06:13:46 +00:00
|
|
|
function clearCache() {
|
|
|
|
$listType = $this->getBlacklistType();
|
|
|
|
|
2015-04-29 21:30:22 +00:00
|
|
|
$cache = ObjectCache::getMainWANInstance();
|
2015-07-28 22:01:39 +00:00
|
|
|
$cache->delete( wfMemcKey( 'spamblacklist', $listType, 'shared-blacklist-regex' ) );
|
|
|
|
$cache->delete( wfMemcKey( 'spamblacklist', $listType, 'blacklist-regex' ) );
|
|
|
|
$cache->delete( wfMemcKey( 'spamblacklist', $listType, 'whitelist-regex' ) );
|
|
|
|
|
2012-01-17 06:13:46 +00:00
|
|
|
wfDebugLog( 'SpamBlacklist', "$listType blacklist local cache cleared.\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildSharedBlacklists() {
|
2017-06-06 16:29:27 +00:00
|
|
|
$regexes = [];
|
2012-01-17 06:13:46 +00:00
|
|
|
$listType = $this->getBlacklistType();
|
|
|
|
# Load lists
|
|
|
|
wfDebugLog( 'SpamBlacklist', "Constructing $listType blacklist\n" );
|
|
|
|
foreach ( $this->files as $fileName ) {
|
2017-06-06 16:29:27 +00:00
|
|
|
$matches = [];
|
2012-01-17 06:13:46 +00:00
|
|
|
if ( preg_match( '/^DB: ([\w-]*) (.*)$/', $fileName, $matches ) ) {
|
|
|
|
$text = $this->getArticleText( $matches[1], $matches[2] );
|
2015-06-15 23:33:58 +00:00
|
|
|
} elseif ( preg_match( '/^(https?:)?\/\//', $fileName ) ) {
|
2012-01-17 06:13:46 +00:00
|
|
|
$text = $this->getHttpText( $fileName );
|
|
|
|
} else {
|
|
|
|
$text = file_get_contents( $fileName );
|
|
|
|
wfDebugLog( 'SpamBlacklist', "got from file $fileName\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build a separate batch of regexes from each source.
|
|
|
|
// While in theory we could squeeze a little efficiency
|
|
|
|
// out of combining multiple sources in one regex, if
|
|
|
|
// there's a bad line in one of them we'll gain more
|
|
|
|
// from only having to break that set into smaller pieces.
|
|
|
|
$regexes = array_merge( $regexes,
|
2012-01-18 23:29:37 +00:00
|
|
|
SpamRegexBatch::regexesFromText( $text, $this, $fileName ) );
|
2012-01-17 06:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $regexes;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getHttpText( $fileName ) {
|
|
|
|
global $wgDBname, $messageMemc;
|
|
|
|
$listType = $this->getBlacklistType();
|
|
|
|
|
|
|
|
# HTTP request
|
|
|
|
# To keep requests to a minimum, we save results into $messageMemc, which is
|
|
|
|
# similar to $wgMemc except almost certain to exist. By default, it is stored
|
|
|
|
# in the database
|
|
|
|
# There are two keys, when the warning key expires, a random thread will refresh
|
|
|
|
# the real key. This reduces the chance of multiple requests under high traffic
|
|
|
|
# conditions.
|
|
|
|
$key = "{$listType}_blacklist_file:$fileName";
|
|
|
|
$warningKey = "$wgDBname:{$listType}filewarning:$fileName";
|
|
|
|
$httpText = $messageMemc->get( $key );
|
|
|
|
$warning = $messageMemc->get( $warningKey );
|
|
|
|
|
|
|
|
if ( !is_string( $httpText ) || ( !$warning && !mt_rand( 0, $this->warningChance ) ) ) {
|
|
|
|
wfDebugLog( 'SpamBlacklist', "Loading $listType blacklist from $fileName\n" );
|
|
|
|
$httpText = Http::get( $fileName );
|
2017-06-06 16:29:27 +00:00
|
|
|
if ( $httpText === false ) {
|
2012-01-17 06:13:46 +00:00
|
|
|
wfDebugLog( 'SpamBlacklist', "Error loading $listType blacklist from $fileName\n" );
|
|
|
|
}
|
|
|
|
$messageMemc->set( $warningKey, 1, $this->warningTime );
|
|
|
|
$messageMemc->set( $key, $httpText, $this->expiryTime );
|
|
|
|
} else {
|
|
|
|
wfDebugLog( 'SpamBlacklist', "Got $listType blacklist from HTTP cache for $fileName\n" );
|
|
|
|
}
|
|
|
|
return $httpText;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch an article from this or another local MediaWiki database.
|
|
|
|
* This is probably *very* fragile, and shouldn't be used perhaps.
|
|
|
|
*
|
2015-07-28 21:20:19 +00:00
|
|
|
* @param string $wiki
|
2012-01-17 06:13:46 +00:00
|
|
|
* @param string $article
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-07-28 21:20:19 +00:00
|
|
|
function getArticleText( $wiki, $article ) {
|
|
|
|
wfDebugLog( 'SpamBlacklist',
|
|
|
|
"Fetching {$this->getBlacklistType()} blacklist from '$article' on '$wiki'...\n" );
|
|
|
|
|
|
|
|
$title = Title::newFromText( $article );
|
|
|
|
// Load all the relevant tables from the correct DB.
|
|
|
|
// This assumes that old_text is the actual text or
|
|
|
|
// that the external store system is at least unified.
|
2017-06-06 16:29:27 +00:00
|
|
|
$row = wfGetDB( DB_SLAVE, [], $wiki )->selectRow(
|
|
|
|
[ 'page', 'revision', 'text' ],
|
2015-07-28 21:20:19 +00:00
|
|
|
array_merge(
|
|
|
|
Revision::selectFields(),
|
|
|
|
Revision::selectPageFields(),
|
|
|
|
Revision::selectTextFields()
|
|
|
|
),
|
2017-06-06 16:29:27 +00:00
|
|
|
[
|
2015-07-28 21:20:19 +00:00
|
|
|
'page_namespace' => $title->getNamespace(), // assume NS IDs match
|
|
|
|
'page_title' => $title->getDBkey(), // assume same case rules
|
|
|
|
'rev_id=page_latest',
|
|
|
|
'old_id=rev_text_id'
|
2017-06-06 16:29:27 +00:00
|
|
|
],
|
2015-07-28 21:20:19 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
|
2016-10-04 23:33:30 +00:00
|
|
|
return $row
|
|
|
|
? ContentHandler::getContentText( Revision::newFromRow( $row )->getContent() )
|
|
|
|
: false;
|
2012-01-17 06:13:46 +00:00
|
|
|
}
|
|
|
|
|
2012-01-18 23:29:37 +00:00
|
|
|
/**
|
|
|
|
* Returns the start of the regex for matches
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getRegexStart() {
|
|
|
|
return '/[a-z0-9_\-.]*';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the end of the regex for matches
|
|
|
|
*
|
|
|
|
* @param $batchSize
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getRegexEnd( $batchSize ) {
|
2017-06-06 16:29:27 +00:00
|
|
|
return ( $batchSize > 0 ) ? '/Sim' : '/im';
|
2012-01-18 23:29:37 +00:00
|
|
|
}
|
2016-02-16 21:39:06 +00:00
|
|
|
|
|
|
|
/**
|
2016-06-30 21:26:36 +00:00
|
|
|
* @param Title $title
|
|
|
|
* @param string[] $entries
|
2016-02-16 21:39:06 +00:00
|
|
|
*/
|
2016-06-30 21:26:36 +00:00
|
|
|
public function warmCachesForFilter( Title $title, array $entries ) {
|
2016-02-16 21:39:06 +00:00
|
|
|
// subclass this
|
|
|
|
}
|
2012-01-17 06:13:46 +00:00
|
|
|
}
|