And while I'm a it:
[lhc/web/wiklou.git] / maintenance / cleanupSpam.php
1 <?php
2 /**
3 * Cleanup all spam from a given hostname
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
25
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' );
32 }
33
34 public function execute() {
35 global $wgLocalDatabases, $wgUser;
36
37 $username = wfMsg( 'spambot_username' );
38 $wgUser = User::newFromName( $username );
39 if ( !$wgUser ) {
40 $this->error( "Invalid username", true );
41 }
42 // Create the user if necessary
43 if ( !$wgUser->getId() ) {
44 $wgUser->addToDatabase();
45 }
46 $spec = $this->getArg();
47 $like = LinkFilter::makeLikeArray( $spec );
48 if ( !$like ) {
49 $this->error( "Not a valid hostname specification: $spec", true );
50 }
51
52 if ( $this->hasOption( 'all' ) ) {
53 // Clean up spam on all wikis
54 $this->output( "Finding spam on " . count( $wgLocalDatabases ) . " wikis\n" );
55 $found = false;
56 foreach ( $wgLocalDatabases as $wikiID ) {
57 $dbr = wfGetDB( DB_SLAVE, array(), $wikiID );
58
59 $count = $dbr->selectField( 'externallinks', 'COUNT(*)',
60 array( 'el_index' . $dbr->buildLike( $like ) ), __METHOD__ );
61 if ( $count ) {
62 $found = true;
63 passthru( "php cleanupSpam.php --wiki='$wikiID' $spec | sed 's/^/$wikiID: /'" );
64 }
65 }
66 if ( $found ) {
67 $this->output( "All done\n" );
68 } else {
69 $this->output( "None found\n" );
70 }
71 } else {
72 // Clean up spam on this wiki
73
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 );
81 }
82 if ( $count ) {
83 $this->output( "Done\n" );
84 }
85 }
86 }
87
88 private function cleanupArticle( $id, $domain ) {
89 $title = Title::newFromID( $id );
90 if ( !$title ) {
91 $this->error( "Internal error: no page for ID $id" );
92 return;
93 }
94
95 $this->output( $title->getPrefixedDBkey() . " ..." );
96 $rev = Revision::newFromTitle( $title );
97 $currentRevId = $rev->getId();
98
99 while ( $rev && ( $rev->isDeleted( Revision::DELETED_TEXT ) || LinkFilter::matchEntry( $rev->getText() , $domain ) ) ) {
100 $rev = $rev->getPrevious();
101 }
102
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" );
107 } else {
108 $dbw = wfGetDB( DB_MASTER );
109 $dbw->begin();
110 $page = WikiPage::factory( $title );
111 if ( !$rev ) {
112 // Didn't find a non-spammy revision, blank the page
113 $this->output( "blanking\n" );
114 $page->doEdit( '', wfMsgForContent( 'spam_blanking', $domain ) );
115 } else {
116 // Revert to this revision
117 $this->output( "reverting\n" );
118 $page->doEdit( $rev->getText(), wfMsgForContent( 'spam_reverting', $domain ),
119 EDIT_UPDATE, $rev->getId() );
120 }
121 $dbw->commit();
122 }
123 }
124 }
125
126 $maintClass = "CleanupSpam";
127 require_once( RUN_MAINTENANCE_IF_MAIN );