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 use MediaWiki\Storage\RevisionRecord
;
26 require_once __DIR__
. '/Maintenance.php';
29 * Maintenance script to cleanup all spam from a given hostname.
31 * @ingroup Maintenance
33 class CleanupSpam
extends Maintenance
{
35 public function __construct() {
36 parent
::__construct();
37 $this->addDescription( 'Cleanup all spam from a given hostname' );
38 $this->addOption( 'all', 'Check all wikis in $wgLocalDatabases' );
39 $this->addOption( 'delete', 'Delete pages containing only spam instead of blanking them' );
42 'Hostname that was spamming, single * wildcard in the beginning allowed'
46 public function execute() {
47 global $IP, $wgLocalDatabases, $wgUser;
49 $username = wfMessage( 'spambot_username' )->text();
50 $wgUser = User
::newSystemUser( $username );
52 $this->fatalError( "Invalid username specified in 'spambot_username' message: $username" );
54 // Hack: Grant bot rights so we don't flood RecentChanges
55 $wgUser->addGroup( 'bot' );
57 $spec = $this->getArg( 0 );
60 foreach ( [ 'http://', 'https://' ] as $prot ) {
61 $conds = LinkFilter
::getQueryConditions( $spec, [ 'protocol' => $prot ] );
63 $this->fatalError( "Not a valid hostname specification: $spec" );
65 $protConds[$prot] = $conds;
68 if ( $this->hasOption( 'all' ) ) {
69 // Clean up spam on all wikis
70 $this->output( "Finding spam on " . count( $wgLocalDatabases ) . " wikis\n" );
72 foreach ( $wgLocalDatabases as $wikiID ) {
73 /** @var Database $dbr */
74 $dbr = $this->getDB( DB_REPLICA
, [], $wikiID );
76 foreach ( $protConds as $conds ) {
77 $count = $dbr->selectField(
85 $cmd = wfShellWikiCmd(
86 "$IP/maintenance/cleanupSpam.php",
87 [ '--wiki', $wikiID, $spec ]
89 passthru( "$cmd | sed 's/^/$wikiID: /'" );
94 $this->output( "All done\n" );
96 $this->output( "None found\n" );
99 // Clean up spam on this wiki
102 /** @var Database $dbr */
103 $dbr = $this->getDB( DB_REPLICA
);
104 foreach ( $protConds as $prot => $conds ) {
107 [ 'DISTINCT el_from' ],
111 $count = $dbr->numRows( $res );
112 $this->output( "Found $count articles containing $spec\n" );
113 foreach ( $res as $row ) {
114 $this->cleanupArticle( $row->el_from
, $spec, $prot );
118 $this->output( "Done\n" );
125 * @param string $domain
126 * @param string $protocol
127 * @throws MWException
129 private function cleanupArticle( $id, $domain, $protocol ) {
130 $title = Title
::newFromID( $id );
132 $this->error( "Internal error: no page for ID $id" );
137 $this->output( $title->getPrefixedDBkey() . " ..." );
138 $rev = Revision
::newFromTitle( $title );
139 $currentRevId = $rev->getId();
141 while ( $rev && ( $rev->isDeleted( RevisionRecord
::DELETED_TEXT
)
142 || LinkFilter
::matchEntry( $rev->getContent( RevisionRecord
::RAW
), $domain, $protocol ) )
144 $rev = $rev->getPrevious();
147 if ( $rev && $rev->getId() == $currentRevId ) {
148 // The regex didn't match the current article text
149 // This happens e.g. when a link comes from a template rather than the page itself
150 $this->output( "False match\n" );
152 $dbw = $this->getDB( DB_MASTER
);
153 $this->beginTransaction( $dbw, __METHOD__
);
154 $page = WikiPage
::factory( $title );
156 // Revert to this revision
157 $content = $rev->getContent( RevisionRecord
::RAW
);
159 $this->output( "reverting\n" );
160 $page->doEditContent(
162 wfMessage( 'spam_reverting', $domain )->inContentLanguage()->text(),
163 EDIT_UPDATE | EDIT_FORCE_BOT
,
166 } elseif ( $this->hasOption( 'delete' ) ) {
167 // Didn't find a non-spammy revision, blank the page
168 $this->output( "deleting\n" );
169 $page->doDeleteArticle(
170 wfMessage( 'spam_deleting', $domain )->inContentLanguage()->text()
173 // Didn't find a non-spammy revision, blank the page
174 $handler = ContentHandler
::getForTitle( $title );
175 $content = $handler->makeEmptyContent();
177 $this->output( "blanking\n" );
178 $page->doEditContent(
180 wfMessage( 'spam_blanking', $domain )->inContentLanguage()->text(),
181 EDIT_UPDATE | EDIT_FORCE_BOT
184 $this->commitTransaction( $dbw, __METHOD__
);
189 $maintClass = CleanupSpam
::class;
190 require_once RUN_MAINTENANCE_IF_MAIN
;