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 );
40 $this->error( "Invalid username", true );
42 // Create the user if necessary
43 if ( !$wgUser->getId() ) {
44 $wgUser->addToDatabase();
46 $spec = $this->getArg();
47 $like = LinkFilter
::makeLikeArray( $spec );
49 $this->error( "Not a valid hostname specification: $spec", true );
52 if ( $this->hasOption( 'all' ) ) {
53 // Clean up spam on all wikis
54 $this->output( "Finding spam on " . count( $wgLocalDatabases ) . " wikis\n" );
56 foreach ( $wgLocalDatabases as $wikiID ) {
57 $dbr = wfGetDB( DB_SLAVE
, array(), $wikiID );
59 $count = $dbr->selectField( 'externallinks', 'COUNT(*)',
60 array( 'el_index' . $dbr->buildLike( $like ) ), __METHOD__
);
63 passthru( "php cleanupSpam.php --wiki='$wikiID' $spec | sed 's/^/$wikiID: /'" );
67 $this->output( "All done\n" );
69 $this->output( "None found\n" );
72 // Clean up spam on this wiki
74 $dbr = wfGetDB( DB_SLAVE
);
75 $res = $dbr->select( 'externallinks', array( 'DISTINCT el_from' ),
76 array( 'el_index' . $dbr->buildLike( $like ) ), __METHOD__
);
77 $count = $dbr->numRows( $res );
78 $this->output( "Found $count articles containing $spec\n" );
79 foreach ( $res as $row ) {
80 $this->cleanupArticle( $row->el_from
, $spec );
83 $this->output( "Done\n" );
88 private function cleanupArticle( $id, $domain ) {
89 $title = Title
::newFromID( $id );
91 $this->error( "Internal error: no page for ID $id" );
95 $this->output( $title->getPrefixedDBkey() . " ..." );
96 $rev = Revision
::newFromTitle( $title );
97 $currentRevId = $rev->getId();
99 while ( $rev && ( $rev->isDeleted( Revision
::DELETED_TEXT
) || LinkFilter
::matchEntry( $rev->getText() , $domain ) ) ) {
100 $rev = $rev->getPrevious();
103 if ( $rev && $rev->getId() == $currentRevId ) {
104 // The regex didn't match the current article text
105 // This happens e.g. when a link comes from a template rather than the page itself
106 $this->output( "False match\n" );
108 $dbw = wfGetDB( DB_MASTER
);
109 $dbw->begin( __METHOD__
);
110 $page = WikiPage
::factory( $title );
112 // Didn't find a non-spammy revision, blank the page
113 $this->output( "blanking\n" );
114 $page->doEdit( '', wfMsgForContent( 'spam_blanking', $domain ) );
116 // Revert to this revision
117 $this->output( "reverting\n" );
118 $page->doEdit( $rev->getText(), wfMsgForContent( 'spam_reverting', $domain ),
119 EDIT_UPDATE
, $rev->getId() );
121 $dbw->commit( __METHOD__
);
126 $maintClass = "CleanupSpam";
127 require_once( RUN_MAINTENANCE_IF_MAIN
);