3 * Cleanup all spam from a given hostname
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @ingroup Maintenance
23 require_once( dirname(__FILE__
) . '/Maintenance.php' );
25 class CleanupSpam
extends Maintenance
{
26 public function __construct() {
27 parent
::__construct();
28 $this->mDescription
= "Cleanup all spam from a given hostname";
29 $this->addOption( 'all', 'Check all wikis in $wgLocalDatabases' );
30 $this->addArg( 'hostname', 'Hostname that was spamming' );
33 public function execute() {
34 global $wgLocalDatabases;
36 $username = wfMsg( 'spambot_username' );
37 $wgUser = User
::newFromName( $username );
38 // Create the user if necessary
39 if ( !$wgUser->getId() ) {
40 $wgUser->addToDatabase();
42 $spec = $this->getArg();
43 $like = LinkFilter
::makeLikeArray( $spec );
45 $this->error( "Not a valid hostname specification: $spec", true );
48 if ( $this->hasOption('all') ) {
49 // Clean up spam on all wikis
50 $this->output( "Finding spam on " . count( $wgLocalDatabases ) . " wikis\n" );
52 foreach ( $wgLocalDatabases as $wikiID ) {
53 $dbr = wfGetDB( DB_SLAVE
, array(), $wikiID );
55 $count = $dbr->selectField( 'externallinks', 'COUNT(*)',
56 array( 'el_index' . $dbr->buildLike( $like ) ), __METHOD__
);
59 passthru( "php cleanupSpam.php --wiki='$wikiID' $spec | sed 's/^/$wikiID: /'" );
63 $this->output( "All done\n" );
65 $this->output( "None found\n" );
68 // Clean up spam on this wiki
70 $dbr = wfGetDB( DB_SLAVE
);
71 $res = $dbr->select( 'externallinks', array( 'DISTINCT el_from' ),
72 array( 'el_index' . $dbr->buildLike( $like ) ), __METHOD__
);
73 $count = $dbr->numRows( $res );
74 $this->output( "Found $count articles containing $spec\n" );
75 foreach ( $res as $row ) {
76 $this->cleanupArticle( $row->el_from
, $spec );
79 $this->output( "Done\n" );
84 private function cleanupArticle( $id, $domain ) {
85 $title = Title
::newFromID( $id );
87 $this->error( "Internal error: no page for ID $id" );
91 $this->output( $title->getPrefixedDBkey() . " ..." );
92 $rev = Revision
::newFromTitle( $title );
93 $revId = $rev->getId();
94 $currentRevId = $revId;
96 while ( $rev && LinkFilter
::matchEntry( $rev->getText() , $domain ) ) {
97 # Revision::getPrevious can't be used in this way before MW 1.6 (Revision.php 1.26)
98 #$rev = $rev->getPrevious();
99 $revId = $title->getPreviousRevisionID( $revId );
101 $rev = Revision
::newFromTitle( $title, $revId );
106 if ( $revId == $currentRevId ) {
107 // The regex didn't match the current article text
108 // This happens e.g. when a link comes from a template rather than the page itself
109 $this->output( "False match\n" );
111 $dbw = wfGetDB( DB_MASTER
);
112 $dbw->immediateBegin();
114 // Didn't find a non-spammy revision, blank the page
115 $this->output( "blanking\n" );
116 $article = new Article( $title );
117 $article->updateArticle( '', wfMsg( 'spam_blanking', $domain ),
121 // Revert to this revision
122 $this->output( "reverting\n" );
123 $article = new Article( $title );
124 $article->updateArticle( $rev->getText(), wfMsg( 'spam_reverting', $domain ), false, false );
126 $dbw->immediateCommit();
132 $maintClass = "CleanupSpam";
133 require_once( DO_MAINTENANCE
);