3 * Cleanup all spam from a given hostname.
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.
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.
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
21 * @ingroup Maintenance
24 require_once __DIR__
. '/Maintenance.php';
27 * Maintenance script to cleanup all spam from a given hostname.
29 * @ingroup Maintenance
31 class CleanupSpam
extends Maintenance
{
33 public function __construct() {
34 parent
::__construct();
35 $this->addDescription( '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' );
40 'Hostname that was spamming, single * wildcard in the beginning allowed'
44 public function execute() {
45 global $IP, $wgLocalDatabases, $wgUser;
47 $username = wfMessage( 'spambot_username' )->text();
48 $wgUser = User
::newSystemUser( $username );
50 $this->error( "Invalid username specified in 'spambot_username' message: $username", true );
52 // Hack: Grant bot rights so we don't flood RecentChanges
53 $wgUser->addGroup( 'bot' );
55 $spec = $this->getArg();
56 $like = LinkFilter
::makeLikeArray( $spec );
58 $this->error( "Not a valid hostname specification: $spec", true );
61 if ( $this->hasOption( 'all' ) ) {
62 // Clean up spam on all wikis
63 $this->output( "Finding spam on " . count( $wgLocalDatabases ) . " wikis\n" );
65 foreach ( $wgLocalDatabases as $wikiID ) {
66 $dbr = $this->getDB( DB_REPLICA
, [], $wikiID );
68 $count = $dbr->selectField( 'externallinks', 'COUNT(*)',
69 [ 'el_index' . $dbr->buildLike( $like ) ], __METHOD__
);
72 $cmd = wfShellWikiCmd( "$IP/maintenance/cleanupSpam.php",
73 [ '--wiki', $wikiID, $spec ] );
74 passthru( "$cmd | sed 's/^/$wikiID: /'" );
78 $this->output( "All done\n" );
80 $this->output( "None found\n" );
83 // Clean up spam on this wiki
85 $dbr = $this->getDB( DB_REPLICA
);
86 $res = $dbr->select( 'externallinks', [ 'DISTINCT el_from' ],
87 [ 'el_index' . $dbr->buildLike( $like ) ], __METHOD__
);
88 $count = $dbr->numRows( $res );
89 $this->output( "Found $count articles containing $spec\n" );
90 foreach ( $res as $row ) {
91 $this->cleanupArticle( $row->el_from
, $spec );
94 $this->output( "Done\n" );
99 private function cleanupArticle( $id, $domain ) {
100 $title = Title
::newFromID( $id );
102 $this->error( "Internal error: no page for ID $id" );
107 $this->output( $title->getPrefixedDBkey() . " ..." );
108 $rev = Revision
::newFromTitle( $title );
109 $currentRevId = $rev->getId();
111 while ( $rev && ( $rev->isDeleted( Revision
::DELETED_TEXT
)
112 || LinkFilter
::matchEntry( $rev->getContent( Revision
::RAW
), $domain ) )
114 $rev = $rev->getPrevious();
117 if ( $rev && $rev->getId() == $currentRevId ) {
118 // The regex didn't match the current article text
119 // This happens e.g. when a link comes from a template rather than the page itself
120 $this->output( "False match\n" );
122 $dbw = $this->getDB( DB_MASTER
);
123 $this->beginTransaction( $dbw, __METHOD__
);
124 $page = WikiPage
::factory( $title );
126 // Revert to this revision
127 $content = $rev->getContent( Revision
::RAW
);
129 $this->output( "reverting\n" );
130 $page->doEditContent(
132 wfMessage( 'spam_reverting', $domain )->inContentLanguage()->text(),
133 EDIT_UPDATE | EDIT_FORCE_BOT
,
136 } elseif ( $this->hasOption( 'delete' ) ) {
137 // Didn't find a non-spammy revision, blank the page
138 $this->output( "deleting\n" );
139 $page->doDeleteArticle(
140 wfMessage( 'spam_deleting', $domain )->inContentLanguage()->text()
143 // Didn't find a non-spammy revision, blank the page
144 $handler = ContentHandler
::getForTitle( $title );
145 $content = $handler->makeEmptyContent();
147 $this->output( "blanking\n" );
148 $page->doEditContent(
150 wfMessage( 'spam_blanking', $domain )->inContentLanguage()->text(),
151 EDIT_UPDATE | EDIT_FORCE_BOT
154 $this->commitTransaction( $dbw, __METHOD__
);
159 $maintClass = "CleanupSpam";
160 require_once RUN_MAINTENANCE_IF_MAIN
;