mediawiki-extensions-SpamBl.../SpamBlacklist.php
Brion Vibber bde084c272 Some polishing and refactoring on this monstrosity, it's been allowed to grow without some good snipping in a while. :)
* Handle bad regexes more gracefully:
 - The batched regexes are tested for validity, and if one is bad, the lines from that source are broken out line-by-line. This is slower, but the other lines in that source will still be applied correctly.
 - Suppress warnings and be more verbose in the debug log.
 - Check for bad regexes when a local blacklist page is edited, and prompt the user to fix the bad lines.
* Caching issues:
 - Cache the full regexes per-DB instead of per-site; this should be friendlier to shared environments where not every wiki has the same configuration.
 - Hopefully improve the recaching of local pages, which looked like it would preemptively apply the being-edited text to the cache during the filter callback, even though something else might stop the page from being saved. Now just clearing the cache after save is complete, letting it re-load later.
* Split out some of the regex batch functions for clarity.

There are probably still issues with caching of HTTP bits, and in general the local DB loading looks verrrry fragile.
Test this a bit more before syncing. :)
2007-07-20 21:13:26 +00:00

113 lines
3 KiB
PHP

<?php
# Loader for spam blacklist feature
# Include this from LocalSettings.php
if ( defined( 'MEDIAWIKI' ) ) {
global $wgFilterCallback, $wgPreSpamFilterCallback;
global $wgSpamBlacklistFiles;
global $wgSpamBlacklistSettings;
$wgSpamBlacklistFiles = false;
$wgSpamBlacklistSettings = array();
if ( $wgFilterCallback ) {
$wgPreSpamFilterCallback = $wgFilterCallback;
} else {
$wgPreSpamFilterCallback = false;
}
$wgFilterCallback = 'wfSpamBlacklistLoader';
$wgExtensionCredits['other'][] = array(
'name' => 'SpamBlacklist',
'author' => 'Tim Starling',
'url' => 'http://www.mediawiki.org/wiki/Extension:SpamBlacklist',
'description' => 'Regex based anti spam tool',
);
$wgExtensionFunctions[] = 'wfSpamBlacklistMessageLoader';
$wgHooks['EditFilter'][] = 'wfSpamBlacklistValidate';
$wgHooks['ArticleSaveComplete'][] = 'wfSpamBlacklistClearCache';
function wfSpamBlacklistMessageLoader() {
global $wgMessageCache;
require_once( 'SpamBlacklist.i18n.php' );
foreach( efSpamBlacklistMessages() as $lang => $messages ) {
$wgMessageCache->addMessages( $messages, $lang );
}
}
function wfSpamBlacklistLoader( &$title, $text, $section ) {
static $spamObj = false;
if ( $spamObj === false ) {
$spamObj = wfSpamBlacklistObject();
}
return $spamObj->filter( $title, $text, $section );
}
function wfSpamBlacklistObject() {
require_once( "SpamBlacklist_body.php" );
global $wgSpamBlacklistFiles, $wgSpamBlacklistSettings, $wgPreSpamFilterCallback;
$spamObj = new SpamBlacklist( $wgSpamBlacklistSettings );
if( $wgSpamBlacklistFiles ) {
$spamObj->files = $wgSpamBlacklistFiles;
}
$spamObj->previousFilter = $wgPreSpamFilterCallback;
return $spamObj;
}
/**
* Confirm that a local blacklist page being saved is valid,
* and toss back a warning to the user if it isn't.
*/
function wfSpamBlacklistValidate( $editPage, $text, $section, &$hookError ) {
$thisPageName = $editPage->mTitle->getPrefixedDBkey();
$spamObj = wfSpamBlacklistObject();
if( !$spamObj->isLocalSource( $editPage->mTitle ) ) {
wfDebug( "Spam blacklist validator: [[$thisPageName]] not a local blacklist\n" );
return true;
}
$lines = explode( "\n", $text );
$badLines = SpamRegexBatch::getBadLines( $lines );
if( $badLines ) {
wfDebug( "Spam blacklist validator: [[$thisPageName]] given invalid input lines: " .
implode( ', ', $badLines ) . "\n" );
$badList = "*<tt>" .
implode( "</tt>\n*<tt>",
array_map( 'wfEscapeWikiText', $badLines ) ) .
"</tt>\n";
$hookError =
"<div class='errorbox'>" .
wfMsgExt( 'spam-invalid-lines', array( 'parsemag' ), count( $badList ) ) .
$badList .
"</div>\n" .
"<br clear='all' />\n";
return true;
} else {
wfDebug( "Spam blacklist validator: [[$thisPageName]] ok or empty blacklist\n" );
return true;
}
}
/**
* Clear local spam blacklist caches on page save.
*/
function wfSpamBlacklistClearCache( &$article, &$user, $text, $summary, $isminor, $iswatch, $section ) {
$spamObj = wfSpamBlacklistObject();
if( $spamObj->isLocalSource( $article->getTitle() ) ) {
$spamObj->clearCache();
}
return true;
}
} # End invocation guard