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\ExternalLinks\LinkFilter
;
25 use MediaWiki\Permissions\Authority
;
26 use MediaWiki\Revision\RevisionRecord
;
27 use MediaWiki\Revision\SlotRecord
;
28 use MediaWiki\StubObject\StubGlobalUser
;
29 use MediaWiki\Title\Title
;
30 use MediaWiki\User\User
;
32 require_once __DIR__
. '/Maintenance.php';
35 * Maintenance script to cleanup all spam from a given hostname.
37 * @ingroup Maintenance
39 class CleanupSpam
extends Maintenance
{
41 public function __construct() {
42 parent
::__construct();
43 $this->addDescription( 'Cleanup all spam from a given hostname' );
44 $this->addOption( 'all', 'Check all wikis in $wgLocalDatabases' );
45 $this->addOption( 'delete', 'Delete pages containing only spam instead of blanking them' );
48 'Hostname that was spamming, single * wildcard in the beginning allowed'
52 public function execute() {
53 global $IP, $wgLocalDatabases;
55 $username = wfMessage( 'spambot_username' )->text();
56 $user = User
::newSystemUser( $username );
58 $this->fatalError( "Invalid username specified in 'spambot_username' message: $username" );
60 // Hack: Grant bot rights so we don't flood RecentChanges
61 $this->getServiceContainer()->getUserGroupManager()->addUserToGroup( $user, 'bot' );
62 StubGlobalUser
::setUser( $user );
64 $spec = $this->getArg( 0 );
67 foreach ( [ 'http://', 'https://' ] as $prot ) {
68 $conds = LinkFilter
::getQueryConditions( $spec, [ 'protocol' => $prot ] );
70 $this->fatalError( "Not a valid hostname specification: $spec" );
72 $protConds[$prot] = $conds;
75 if ( $this->hasOption( 'all' ) ) {
76 // Clean up spam on all wikis
77 $this->output( "Finding spam on " . count( $wgLocalDatabases ) . " wikis\n" );
79 foreach ( $wgLocalDatabases as $wikiId ) {
80 /** @var Database $dbr */
81 $dbr = $this->getDB( DB_REPLICA
, [], $wikiId );
83 foreach ( $protConds as $conds ) {
84 $count = $dbr->newSelectQueryBuilder()
85 ->select( 'COUNT(*)' )
86 ->from( 'externallinks' )
88 ->caller( __METHOD__
)
92 $cmd = wfShellWikiCmd(
93 "$IP/maintenance/cleanupSpam.php",
94 [ '--wiki', $wikiId, $spec ]
96 // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.passthru
97 passthru( "$cmd | sed 's/^/$wikiId: /'" );
102 $this->output( "All done\n" );
104 $this->output( "None found\n" );
107 // Clean up spam on this wiki
110 /** @var Database $dbr */
111 $dbr = $this->getReplicaDB();
112 foreach ( $protConds as $prot => $conds ) {
113 $res = $dbr->newSelectQueryBuilder()
114 ->select( 'el_from' )
116 ->from( 'externallinks' )
118 ->caller( __METHOD__
)
120 $count +
= $res->numRows();
121 $this->output( "Found $count articles containing $spec so far...\n" );
122 foreach ( $res as $row ) {
123 $this->cleanupArticle(
132 $this->output( "Done\n" );
139 * @param string $domain
140 * @param string $protocol
141 * @param Authority $performer
143 private function cleanupArticle( $id, $domain, $protocol, Authority
$performer ) {
144 $title = Title
::newFromID( $id );
146 $this->error( "Internal error: no page for ID $id" );
151 $this->output( $title->getPrefixedDBkey() . " ..." );
153 $services = $this->getServiceContainer();
154 $revLookup = $services->getRevisionLookup();
155 $rev = $revLookup->getRevisionByTitle( $title );
156 $currentRevId = $rev->getId();
158 while ( $rev && ( $rev->isDeleted( RevisionRecord
::DELETED_TEXT
) ||
159 LinkFilter
::matchEntry(
160 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable RAW never returns null
161 $rev->getContent( SlotRecord
::MAIN
, RevisionRecord
::RAW
),
166 $rev = $revLookup->getPreviousRevision( $rev );
169 if ( $rev && $rev->getId() == $currentRevId ) {
170 // The regex didn't match the current article text
171 // This happens e.g. when a link comes from a template rather than the page itself
172 $this->output( "False match\n" );
174 $dbw = $this->getPrimaryDB();
175 $this->beginTransaction( $dbw, __METHOD__
);
176 $page = $services->getWikiPageFactory()->newFromTitle( $title );
178 // Revert to this revision
179 $content = $rev->getContent( SlotRecord
::MAIN
, RevisionRecord
::RAW
);
181 $this->output( "reverting\n" );
182 $page->doUserEditContent(
183 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable RAW never returns null
186 wfMessage( 'spam_reverting', $domain )->inContentLanguage()->text(),
187 EDIT_UPDATE | EDIT_FORCE_BOT
,
190 } elseif ( $this->hasOption( 'delete' ) ) {
191 // Didn't find a non-spammy revision, blank the page
192 $this->output( "deleting\n" );
193 $deletePage = $services->getDeletePageFactory()->newDeletePage( $page, $performer );
194 $deletePage->deleteUnsafe( wfMessage( 'spam_deleting', $domain )->inContentLanguage()->text() );
196 // Didn't find a non-spammy revision, blank the page
197 $handler = $services->getContentHandlerFactory()
198 ->getContentHandler( $title->getContentModel() );
199 $content = $handler->makeEmptyContent();
201 $this->output( "blanking\n" );
202 $page->doUserEditContent(
205 wfMessage( 'spam_blanking', $domain )->inContentLanguage()->text(),
206 EDIT_UPDATE | EDIT_FORCE_BOT
209 $this->commitTransaction( $dbw, __METHOD__
);
214 $maintClass = CleanupSpam
::class;
215 require_once RUN_MAINTENANCE_IF_MAIN
;