merged master
[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
28 public function __construct() {
29 parent::__construct();
30 $this->mDescription = "Cleanup all spam from a given hostname";
31 $this->addOption( 'all', 'Check all wikis in $wgLocalDatabases' );
32 $this->addOption( 'delete', 'Delete pages containing only spam instead of blanking them' );
33 $this->addArg( 'hostname', 'Hostname that was spamming, single * wildcard in the beginning allowed' );
34 }
35
36 public function execute() {
37 global $wgLocalDatabases, $wgUser;
38
39 $username = wfMsg( 'spambot_username' );
40 $wgUser = User::newFromName( $username );
41 if ( !$wgUser ) {
42 $this->error( "Invalid username", true );
43 }
44 // Create the user if necessary
45 if ( !$wgUser->getId() ) {
46 $wgUser->addToDatabase();
47 }
48 $spec = $this->getArg();
49 $like = LinkFilter::makeLikeArray( $spec );
50 if ( !$like ) {
51 $this->error( "Not a valid hostname specification: $spec", true );
52 }
53
54 if ( $this->hasOption( 'all' ) ) {
55 // Clean up spam on all wikis
56 $this->output( "Finding spam on " . count( $wgLocalDatabases ) . " wikis\n" );
57 $found = false;
58 foreach ( $wgLocalDatabases as $wikiID ) {
59 $dbr = wfGetDB( DB_SLAVE, array(), $wikiID );
60
61 $count = $dbr->selectField( 'externallinks', 'COUNT(*)',
62 array( 'el_index' . $dbr->buildLike( $like ) ), __METHOD__ );
63 if ( $count ) {
64 $found = true;
65 passthru( "php cleanupSpam.php --wiki='$wikiID' $spec | sed 's/^/$wikiID: /'" );
66 }
67 }
68 if ( $found ) {
69 $this->output( "All done\n" );
70 } else {
71 $this->output( "None found\n" );
72 }
73 } else {
74 // Clean up spam on this wiki
75
76 $dbr = wfGetDB( DB_SLAVE );
77 $res = $dbr->select( 'externallinks', array( 'DISTINCT el_from' ),
78 array( 'el_index' . $dbr->buildLike( $like ) ), __METHOD__ );
79 $count = $dbr->numRows( $res );
80 $this->output( "Found $count articles containing $spec\n" );
81 foreach ( $res as $row ) {
82 $this->cleanupArticle( $row->el_from, $spec );
83 }
84 if ( $count ) {
85 $this->output( "Done\n" );
86 }
87 }
88 }
89
90 private function cleanupArticle( $id, $domain ) {
91 $title = Title::newFromID( $id );
92 if ( !$title ) {
93 $this->error( "Internal error: no page for ID $id" );
94 return;
95 }
96
97 $this->output( $title->getPrefixedDBkey() . " ..." );
98 $rev = Revision::newFromTitle( $title );
99 $currentRevId = $rev->getId();
100
101 //FIXME: LinkFilter needs to handle Content objects! Or rather, ContentHandler needs to provide the appropriate LinkFilter.
102 while ( $rev && ( $rev->isDeleted( Revision::DELETED_TEXT ) || LinkFilter::matchEntry( $rev->getText() , $domain ) ) ) {
103 $rev = $rev->getPrevious();
104 }
105
106 if ( $rev && $rev->getId() == $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( __METHOD__ );
113 $page = WikiPage::factory( $title );
114 if ( $rev ) {
115 // Revert to this revision
116 $this->output( "reverting\n" );
117 $page->doEdit( $rev->getText(), wfMsgForContent( 'spam_reverting', $domain ),
118 EDIT_UPDATE, $rev->getId() );
119 } elseif ( $this->hasOption( 'delete' ) ) {
120 // Didn't find a non-spammy revision, blank the page
121 $this->output( "deleting\n" );
122 $page->doDeleteArticle( wfMsgForContent( 'spam_deleting', $domain ) );
123 } else {
124 // Didn't find a non-spammy revision, blank the page
125 $this->output( "blanking\n" );
126 $page->doEdit( '', wfMsgForContent( 'spam_blanking', $domain ) );
127 }
128 $dbw->commit( __METHOD__ );
129 }
130 }
131 }
132
133 $maintClass = "CleanupSpam";
134 require_once( RUN_MAINTENANCE_IF_MAIN );