phpcs: More require/include is not a function
[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 $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 passthru( "php cleanupSpam.php --wiki='$wikiID' $spec | sed 's/^/$wikiID: /'" );
71 }
72 }
73 if ( $found ) {
74 $this->output( "All done\n" );
75 } else {
76 $this->output( "None found\n" );
77 }
78 } else {
79 // Clean up spam on this wiki
80
81 $dbr = wfGetDB( DB_SLAVE );
82 $res = $dbr->select( 'externallinks', array( 'DISTINCT el_from' ),
83 array( 'el_index' . $dbr->buildLike( $like ) ), __METHOD__ );
84 $count = $dbr->numRows( $res );
85 $this->output( "Found $count articles containing $spec\n" );
86 foreach ( $res as $row ) {
87 $this->cleanupArticle( $row->el_from, $spec );
88 }
89 if ( $count ) {
90 $this->output( "Done\n" );
91 }
92 }
93 }
94
95 private function cleanupArticle( $id, $domain ) {
96 $title = Title::newFromID( $id );
97 if ( !$title ) {
98 $this->error( "Internal error: no page for ID $id" );
99 return;
100 }
101
102 $this->output( $title->getPrefixedDBkey() . " ..." );
103 $rev = Revision::newFromTitle( $title );
104 $currentRevId = $rev->getId();
105
106 while ( $rev && ( $rev->isDeleted( Revision::DELETED_TEXT )
107 || LinkFilter::matchEntry( $rev->getContent( Revision::RAW ), $domain ) ) ) {
108 $rev = $rev->getPrevious();
109 }
110
111 if ( $rev && $rev->getId() == $currentRevId ) {
112 // The regex didn't match the current article text
113 // This happens e.g. when a link comes from a template rather than the page itself
114 $this->output( "False match\n" );
115 } else {
116 $dbw = wfGetDB( DB_MASTER );
117 $dbw->begin( __METHOD__ );
118 $page = WikiPage::factory( $title );
119 if ( $rev ) {
120 // Revert to this revision
121 $content = $rev->getContent( Revision::RAW );
122
123 $this->output( "reverting\n" );
124 $page->doEditContent( $content, wfMessage( 'spam_reverting', $domain )->inContentLanguage()->text(),
125 EDIT_UPDATE, $rev->getId() );
126 } elseif ( $this->hasOption( 'delete' ) ) {
127 // Didn't find a non-spammy revision, blank the page
128 $this->output( "deleting\n" );
129 $page->doDeleteArticle( wfMessage( 'spam_deleting', $domain )->inContentLanguage()->text() );
130 } else {
131 // Didn't find a non-spammy revision, blank the page
132 $handler = ContentHandler::getForTitle( $title );
133 $content = $handler->makeEmptyContent();
134
135 $this->output( "blanking\n" );
136 $page->doEditContent( $content, wfMessage( 'spam_blanking', $domain )->inContentLanguage()->text() );
137 }
138 $dbw->commit( __METHOD__ );
139 }
140 }
141 }
142
143 $maintClass = "CleanupSpam";
144 require_once RUN_MAINTENANCE_IF_MAIN;