<?php
/**
- * Cleanup all spam from a given hostname
+ * Cleanup all spam from a given hostname.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
require_once( dirname( __FILE__ ) . '/Maintenance.php' );
+/**
+ * Maintenance script to cleanup all spam from a given hostname.
+ *
+ * @ingroup Maintenance
+ */
class CleanupSpam extends Maintenance {
+
public function __construct() {
parent::__construct();
$this->mDescription = "Cleanup all spam from a given hostname";
$this->addOption( 'all', 'Check all wikis in $wgLocalDatabases' );
- $this->addArg( 'hostname', 'Hostname that was spamming' );
+ $this->addOption( 'delete', 'Delete pages containing only spam instead of blanking them' );
+ $this->addArg( 'hostname', 'Hostname that was spamming, single * wildcard in the beginning allowed' );
}
public function execute() {
global $wgLocalDatabases, $wgUser;
- $username = wfMsg( 'spambot_username' );
+ $username = wfMessage( 'spambot_username' )->text();
$wgUser = User::newFromName( $username );
if ( !$wgUser ) {
$this->error( "Invalid username", true );
$rev = Revision::newFromTitle( $title );
$currentRevId = $rev->getId();
- while ( $rev && ( $rev->isDeleted( Revision::DELETED_TEXT ) || LinkFilter::matchEntry( $rev->getText() , $domain ) ) ) {
+ while ( $rev && ( $rev->isDeleted( Revision::DELETED_TEXT )
+ || LinkFilter::matchEntry( $rev->getContent( Revision::RAW ), $domain ) ) ) {
$rev = $rev->getPrevious();
}
$dbw = wfGetDB( DB_MASTER );
$dbw->begin( __METHOD__ );
$page = WikiPage::factory( $title );
- if ( !$rev ) {
- // Didn't find a non-spammy revision, blank the page
- $this->output( "blanking\n" );
- $page->doEdit( '', wfMsgForContent( 'spam_blanking', $domain ) );
- } else {
+ if ( $rev ) {
// Revert to this revision
+ $content = $rev->getContent( Revision::RAW );
+
$this->output( "reverting\n" );
- $page->doEdit( $rev->getText(), wfMsgForContent( 'spam_reverting', $domain ),
+ $page->doEditContent( $content, wfMessage( 'spam_reverting', $domain )->inContentLanguage()->text(),
EDIT_UPDATE, $rev->getId() );
+ } elseif ( $this->hasOption( 'delete' ) ) {
+ // Didn't find a non-spammy revision, blank the page
+ $this->output( "deleting\n" );
+ $page->doDeleteArticle( wfMessage( 'spam_deleting', $domain )->inContentLanguage()->text() );
+ } else {
+ // Didn't find a non-spammy revision, blank the page
+ $handler = ContentHandler::getForTitle( $title );
+ $content = $handler->makeEmptyContent();
+
+ $this->output( "blanking\n" );
+ $page->doEditContent( $content, wfMessage( 'spam_blanking', $domain )->inContentLanguage()->text() );
}
$dbw->commit( __METHOD__ );
}