Followup r60051, with the rest of the callers and removing a useless subclassing...
[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( dirname(__FILE__) . '/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->addArg( 'hostname', 'Hostname that was spamming' );
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::makeLikeArray( $spec );
44 if ( !$like ) {
45 $this->error( "Not a valid hostname specification: $spec", true );
46 }
47
48 if ( $this->hasOption('all') ) {
49 // Clean up spam on all wikis
50 $this->output( "Finding spam on " . count( $wgLocalDatabases ) . " wikis\n" );
51 $found = false;
52 foreach ( $wgLocalDatabases as $wikiID ) {
53 $dbr = wfGetDB( DB_SLAVE, array(), $wikiID );
54
55 $count = $dbr->selectField( 'externallinks', 'COUNT(*)',
56 array( 'el_index' . $dbr->buildLike( $like ) ), __METHOD__ );
57 if ( $count ) {
58 $found = true;
59 passthru( "php cleanupSpam.php --wiki='$wikiID' $spec | sed 's/^/$wikiID: /'" );
60 }
61 }
62 if ( $found ) {
63 $this->output( "All done\n" );
64 } else {
65 $this->output( "None found\n" );
66 }
67 } else {
68 // Clean up spam on this wiki
69
70 $dbr = wfGetDB( DB_SLAVE );
71 $res = $dbr->select( 'externallinks', array( 'DISTINCT el_from' ),
72 array( 'el_index' . $dbr->buildLike( $like ) ), __METHOD__ );
73 $count = $dbr->numRows( $res );
74 $this->output( "Found $count articles containing $spec\n" );
75 foreach ( $res as $row ) {
76 $this->cleanupArticle( $row->el_from, $spec );
77 }
78 if ( $count ) {
79 $this->output( "Done\n" );
80 }
81 }
82 }
83
84 private function cleanupArticle( $id, $domain ) {
85 $title = Title::newFromID( $id );
86 if ( !$title ) {
87 $this->error( "Internal error: no page for ID $id" );
88 return;
89 }
90
91 $this->output( $title->getPrefixedDBkey() . " ..." );
92 $rev = Revision::newFromTitle( $title );
93 $revId = $rev->getId();
94 $currentRevId = $revId;
95
96 while ( $rev && LinkFilter::matchEntry( $rev->getText() , $domain ) ) {
97 # Revision::getPrevious can't be used in this way before MW 1.6 (Revision.php 1.26)
98 #$rev = $rev->getPrevious();
99 $revId = $title->getPreviousRevisionID( $revId );
100 if ( $revId ) {
101 $rev = Revision::newFromTitle( $title, $revId );
102 } else {
103 $rev = false;
104 }
105 }
106 if ( $revId == $currentRevId ) {
107 // The regex didn't match the current article text
108 // This happens e.g. when a link comes from a template rather than the page itself
109 $this->output( "False match\n" );
110 } else {
111 $dbw = wfGetDB( DB_MASTER );
112 $dbw->begin();
113 if ( !$rev ) {
114 // Didn't find a non-spammy revision, blank the page
115 $this->output( "blanking\n" );
116 $article = new Article( $title );
117 $article->updateArticle( '', wfMsg( 'spam_blanking', $domain ),
118 false, false );
119
120 } else {
121 // Revert to this revision
122 $this->output( "reverting\n" );
123 $article = new Article( $title );
124 $article->updateArticle( $rev->getText(), wfMsg( 'spam_reverting', $domain ), false, false );
125 }
126 $dbw->commit();
127 wfDoUpdates();
128 }
129 }
130 }
131
132 $maintClass = "CleanupSpam";
133 require_once( DO_MAINTENANCE );