Merge "Add title to list item of language link"
[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 __DIR__ . '/Maintenance.php';
25
26 /**
27 * Maintenance script to cleanup all spam from a given hostname.
28 *
29 * @ingroup Maintenance
30 */
31 class CleanupSpam extends Maintenance {
32
33 public function __construct() {
34 parent::__construct();
35 $this->mDescription = "Cleanup all spam from a given hostname";
36 $this->addOption( 'all', 'Check all wikis in $wgLocalDatabases' );
37 $this->addOption( 'delete', 'Delete pages containing only spam instead of blanking them' );
38 $this->addArg( 'hostname', 'Hostname that was spamming, single * wildcard in the beginning allowed' );
39 }
40
41 public function execute() {
42 global $IP, $wgLocalDatabases, $wgUser;
43
44 $username = wfMessage( 'spambot_username' )->text();
45 $wgUser = User::newFromName( $username );
46 if ( !$wgUser ) {
47 $this->error( "Invalid username specified in 'spambot_username' message: $username", true );
48 }
49 // Create the user if necessary
50 if ( !$wgUser->getId() ) {
51 $wgUser->addToDatabase();
52 }
53 $spec = $this->getArg();
54 $like = LinkFilter::makeLikeArray( $spec );
55 if ( !$like ) {
56 $this->error( "Not a valid hostname specification: $spec", true );
57 }
58
59 if ( $this->hasOption( 'all' ) ) {
60 // Clean up spam on all wikis
61 $this->output( "Finding spam on " . count( $wgLocalDatabases ) . " wikis\n" );
62 $found = false;
63 foreach ( $wgLocalDatabases as $wikiID ) {
64 $dbr = wfGetDB( DB_SLAVE, array(), $wikiID );
65
66 $count = $dbr->selectField( 'externallinks', 'COUNT(*)',
67 array( 'el_index' . $dbr->buildLike( $like ) ), __METHOD__ );
68 if ( $count ) {
69 $found = true;
70 $cmd = wfShellWikiCmd( "$IP/maintenance/cleanupSpam.php",
71 array( '--wiki', $wikiID, $spec ) );
72 passthru( "$cmd | sed 's/^/$wikiID: /'" );
73 }
74 }
75 if ( $found ) {
76 $this->output( "All done\n" );
77 } else {
78 $this->output( "None found\n" );
79 }
80 } else {
81 // Clean up spam on this wiki
82
83 $dbr = wfGetDB( DB_SLAVE );
84 $res = $dbr->select( 'externallinks', array( 'DISTINCT el_from' ),
85 array( 'el_index' . $dbr->buildLike( $like ) ), __METHOD__ );
86 $count = $dbr->numRows( $res );
87 $this->output( "Found $count articles containing $spec\n" );
88 foreach ( $res as $row ) {
89 $this->cleanupArticle( $row->el_from, $spec );
90 }
91 if ( $count ) {
92 $this->output( "Done\n" );
93 }
94 }
95 }
96
97 private function cleanupArticle( $id, $domain ) {
98 $title = Title::newFromID( $id );
99 if ( !$title ) {
100 $this->error( "Internal error: no page for ID $id" );
101 return;
102 }
103
104 $this->output( $title->getPrefixedDBkey() . " ..." );
105 $rev = Revision::newFromTitle( $title );
106 $currentRevId = $rev->getId();
107
108 while ( $rev && ( $rev->isDeleted( Revision::DELETED_TEXT )
109 || LinkFilter::matchEntry( $rev->getContent( Revision::RAW ), $domain ) ) ) {
110 $rev = $rev->getPrevious();
111 }
112
113 if ( $rev && $rev->getId() == $currentRevId ) {
114 // The regex didn't match the current article text
115 // This happens e.g. when a link comes from a template rather than the page itself
116 $this->output( "False match\n" );
117 } else {
118 $dbw = wfGetDB( DB_MASTER );
119 $dbw->begin( __METHOD__ );
120 $page = WikiPage::factory( $title );
121 if ( $rev ) {
122 // Revert to this revision
123 $content = $rev->getContent( Revision::RAW );
124
125 $this->output( "reverting\n" );
126 $page->doEditContent( $content, wfMessage( 'spam_reverting', $domain )->inContentLanguage()->text(),
127 EDIT_UPDATE, $rev->getId() );
128 } elseif ( $this->hasOption( 'delete' ) ) {
129 // Didn't find a non-spammy revision, blank the page
130 $this->output( "deleting\n" );
131 $page->doDeleteArticle( wfMessage( 'spam_deleting', $domain )->inContentLanguage()->text() );
132 } else {
133 // Didn't find a non-spammy revision, blank the page
134 $handler = ContentHandler::getForTitle( $title );
135 $content = $handler->makeEmptyContent();
136
137 $this->output( "blanking\n" );
138 $page->doEditContent( $content, wfMessage( 'spam_blanking', $domain )->inContentLanguage()->text() );
139 }
140 $dbw->commit( __METHOD__ );
141 }
142 }
143 }
144
145 $maintClass = "CleanupSpam";
146 require_once RUN_MAINTENANCE_IF_MAIN;