]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/cleanupSpam.php
MediaWiki 1.17.0-scripts
[autoinstalls/mediawiki.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                 // Create the user if necessary
40                 if ( !$wgUser->getId() ) {
41                         $wgUser->addToDatabase();
42                 }
43                 $spec = $this->getArg();
44                 $like = LinkFilter::makeLikeArray( $spec );
45                 if ( !$like ) {
46                         $this->error( "Not a valid hostname specification: $spec", true );
47                 }
48
49                 if ( $this->hasOption( 'all' ) ) {
50                         // Clean up spam on all wikis
51                         $this->output( "Finding spam on " . count( $wgLocalDatabases ) . " wikis\n" );
52                         $found = false;
53                         foreach ( $wgLocalDatabases as $wikiID ) {
54                                 $dbr = wfGetDB( DB_SLAVE, array(), $wikiID );
55
56                                 $count = $dbr->selectField( 'externallinks', 'COUNT(*)',
57                                         array( 'el_index' . $dbr->buildLike( $like ) ), __METHOD__ );
58                                 if ( $count ) {
59                                         $found = true;
60                                         passthru( "php cleanupSpam.php --wiki='$wikiID' $spec | sed 's/^/$wikiID:  /'" );
61                                 }
62                         }
63                         if ( $found ) {
64                                 $this->output( "All done\n" );
65                         } else {
66                                 $this->output( "None found\n" );
67                         }
68                 } else {
69                         // Clean up spam on this wiki
70
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 );
78                         }
79                         if ( $count ) {
80                                 $this->output( "Done\n" );
81                         }
82                 }
83         }
84
85         private function cleanupArticle( $id, $domain ) {
86                 $title = Title::newFromID( $id );
87                 if ( !$title ) {
88                         $this->error( "Internal error: no page for ID $id" );
89                         return;
90                 }
91
92                 $this->output( $title->getPrefixedDBkey() . " ..." );
93                 $rev = Revision::newFromTitle( $title );
94                 $revId = $rev->getId();
95                 $currentRevId = $revId;
96
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 );
101                         if ( $revId ) {
102                                 $rev = Revision::newFromTitle( $title, $revId );
103                         } else {
104                                 $rev = false;
105                         }
106                 }
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" );
111                 } else {
112                         $dbw = wfGetDB( DB_MASTER );
113                         $dbw->begin();
114                         if ( !$rev ) {
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 ) );
119                         } else {
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 );
124                         }
125                         $dbw->commit();
126                         wfDoUpdates();
127                 }
128         }
129 }
130
131 $maintClass = "CleanupSpam";
132 require_once( RUN_MAINTENANCE_IF_MAIN );