Don't put \n on the end of every error() call, just do it in error() itself. Still...
[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 * @ingroup Maintenance
21 */
22
23 require_once( "Maintenance.php" );
24
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->addArgs( array( 'hostname' ) );
31 }
32
33 public function execute() {
34 global $wgLocalDatabases;
35
36 $username = wfMsg( 'spambot_username' );
37 $wgUser = User::newFromName( $username );
38 // Create the user if necessary
39 if ( !$wgUser->getId() ) {
40 $wgUser->addToDatabase();
41 }
42 $spec = $this->getArg();
43 $like = LinkFilter::makeLike( $spec );
44 if ( !$like ) {
45 $this->error( "Not a valid hostname specification: $spec", true );
46 }
47
48 $dbr = wfGetDB( DB_SLAVE );
49
50 if ( $this->hasOption('all') ) {
51 // Clean up spam on all wikis
52 $dbr = wfGetDB( DB_SLAVE );
53 $this->output( "Finding spam on " . count($wgLocalDatabases) . " wikis\n" );
54 $found = false;
55 foreach ( $wgLocalDatabases as $db ) {
56 $count = $dbr->selectField( "`$db`.externallinks", 'COUNT(*)',
57 array( 'el_index LIKE ' . $dbr->addQuotes( $like ) ), __METHOD__ );
58 if ( $count ) {
59 $found = true;
60 passthru( "php cleanupSpam.php $db $spec | sed s/^/$db: /" );
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 $res = $dbr->select( 'externallinks', array( 'DISTINCT el_from' ),
71 array( 'el_index LIKE ' . $dbr->addQuotes( $like ) ), __METHOD__ );
72 $count = $dbr->numRows( $res );
73 $this->output( "Found $count articles containing $spec\n" );
74 while ( $row = $dbr->fetchObject( $res ) ) {
75 $this->cleanupArticle( $row->el_from, $spec );
76 }
77 if ( $count ) {
78 $this->output( "Done\n" );
79 }
80 }
81 }
82
83 private function cleanupArticle( $id, $domain ) {
84 $title = Title::newFromID( $id );
85 if ( !$title ) {
86 $this->error( "Internal error: no page for ID $id" );
87 return;
88 }
89
90 $this->output( $title->getPrefixedDBkey() . " ..." );
91 $rev = Revision::newFromTitle( $title );
92 $revId = $rev->getId();
93 $currentRevId = $revId;
94
95 while ( $rev && LinkFilter::matchEntry( $rev->getText() , $domain ) ) {
96 # Revision::getPrevious can't be used in this way before MW 1.6 (Revision.php 1.26)
97 #$rev = $rev->getPrevious();
98 $revId = $title->getPreviousRevisionID( $revId );
99 if ( $revId ) {
100 $rev = Revision::newFromTitle( $title, $revId );
101 } else {
102 $rev = false;
103 }
104 }
105 if ( $revId == $currentRevId ) {
106 // The regex didn't match the current article text
107 // This happens e.g. when a link comes from a template rather than the page itself
108 $this->output( "False match\n" );
109 } else {
110 $dbw = wfGetDB( DB_MASTER );
111 $dbw->immediateBegin();
112 if ( !$rev ) {
113 // Didn't find a non-spammy revision, blank the page
114 $this->output( "blanking\n" );
115 $article = new Article( $title );
116 $article->updateArticle( '', wfMsg( 'spam_blanking', $domain ),
117 false, false );
118
119 } else {
120 // Revert to this revision
121 $this->output( "reverting\n" );
122 $article = new Article( $title );
123 $article->updateArticle( $rev->getText(), wfMsg( 'spam_reverting', $domain ), false, false );
124 }
125 $dbw->immediateCommit();
126 wfDoUpdates();
127 }
128 }
129 }
130
131 $maintClass = "CleanupSpam";
132 require_once( DO_MAINTENANCE );