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
21 * @ingroup Maintenance
24 require_once( dirname( __FILE__
) . '/Maintenance.php' );
26 class CleanupSpam
extends Maintenance
{
27 public function __construct() {
28 parent
::__construct();
29 $this->mDescription
= "Cleanup all spam from a given hostname";
30 $this->addOption( 'all', 'Check all wikis in $wgLocalDatabases' );
31 $this->addArg( 'hostname', 'Hostname that was spamming' );
34 public function execute() {
35 global $wgLocalDatabases, $wgUser;
37 $username = wfMsg( 'spambot_username' );
38 $wgUser = User
::newFromName( $username );
39 // Create the user if necessary
40 if ( !$wgUser->getId() ) {
41 $wgUser->addToDatabase();
43 $spec = $this->getArg();
44 $like = LinkFilter
::makeLikeArray( $spec );
46 $this->error( "Not a valid hostname specification: $spec", true );
49 if ( $this->hasOption( 'all' ) ) {
50 // Clean up spam on all wikis
51 $this->output( "Finding spam on " . count( $wgLocalDatabases ) . " wikis\n" );
53 foreach ( $wgLocalDatabases as $wikiID ) {
54 $dbr = wfGetDB( DB_SLAVE
, array(), $wikiID );
56 $count = $dbr->selectField( 'externallinks', 'COUNT(*)',
57 array( 'el_index' . $dbr->buildLike( $like ) ), __METHOD__
);
60 passthru( "php cleanupSpam.php --wiki='$wikiID' $spec | sed 's/^/$wikiID: /'" );
64 $this->output( "All done\n" );
66 $this->output( "None found\n" );
69 // Clean up spam on this wiki
71 $dbr = wfGetDB( DB_SLAVE
);
72 $res = $dbr->select( 'externallinks', array( 'DISTINCT el_from' ),
73 array( 'el_index' . $dbr->buildLike( $like ) ), __METHOD__
);
74 $count = $dbr->numRows( $res );
75 $this->output( "Found $count articles containing $spec\n" );
76 foreach ( $res as $row ) {
77 $this->cleanupArticle( $row->el_from
, $spec );
80 $this->output( "Done\n" );
85 private function cleanupArticle( $id, $domain ) {
86 $title = Title
::newFromID( $id );
88 $this->error( "Internal error: no page for ID $id" );
92 $this->output( $title->getPrefixedDBkey() . " ..." );
93 $rev = Revision
::newFromTitle( $title );
94 $revId = $rev->getId();
95 $currentRevId = $revId;
97 while ( $rev && LinkFilter
::matchEntry( $rev->getText() , $domain ) ) {
98 # Revision::getPrevious can't be used in this way before MW 1.6 (Revision.php 1.26)
99 # $rev = $rev->getPrevious();
100 $revId = $title->getPreviousRevisionID( $revId );
102 $rev = Revision
::newFromTitle( $title, $revId );
107 if ( $revId == $currentRevId ) {
108 // The regex didn't match the current article text
109 // This happens e.g. when a link comes from a template rather than the page itself
110 $this->output( "False match\n" );
112 $dbw = wfGetDB( DB_MASTER
);
115 // Didn't find a non-spammy revision, blank the page
116 $this->output( "blanking\n" );
117 $article = new Article( $title );
118 $article->doEdit( '', wfMsg( 'spam_blanking', $domain ) );
120 // Revert to this revision
121 $this->output( "reverting\n" );
122 $article = new Article( $title );
123 $article->doEdit( $rev->getText(), wfMsg( 'spam_reverting', $domain ), EDIT_UPDATE
);
130 $maintClass = "CleanupSpam";
131 require_once( RUN_MAINTENANCE_IF_MAIN
);