[JsonCodec] Hide TYPE_ANNOTATION from the unserialization methods
[mediawiki.git] / maintenance / cleanupSpam.php
blobb3f859aea3b98c2f758d5d2ce33525690f374df8
1 <?php
2 /**
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
20 * @file
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';
34 /**
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' );
46 $this->addArg(
47 'hostname',
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 );
57 if ( !$user ) {
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 );
66 $protConds = [];
67 foreach ( [ 'http://', 'https://' ] as $prot ) {
68 $conds = LinkFilter::getQueryConditions( $spec, [ 'protocol' => $prot ] );
69 if ( !$conds ) {
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" );
78 $found = false;
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' )
87 ->where( $conds )
88 ->caller( __METHOD__ )
89 ->fetchField();
90 if ( $count ) {
91 $found = true;
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: /'" );
101 if ( $found ) {
102 $this->output( "All done\n" );
103 } else {
104 $this->output( "None found\n" );
106 } else {
107 // Clean up spam on this wiki
109 $count = 0;
110 /** @var Database $dbr */
111 $dbr = $this->getReplicaDB();
112 foreach ( $protConds as $prot => $conds ) {
113 $res = $dbr->newSelectQueryBuilder()
114 ->select( 'el_from' )
115 ->distinct()
116 ->from( 'externallinks' )
117 ->where( $conds )
118 ->caller( __METHOD__ )
119 ->fetchResultSet();
120 $count += $res->numRows();
121 $this->output( "Found $count articles containing $spec so far...\n" );
122 foreach ( $res as $row ) {
123 $this->cleanupArticle(
124 $row->el_from,
125 $spec,
126 $prot,
127 $user
131 if ( $count ) {
132 $this->output( "Done\n" );
138 * @param int $id
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 );
145 if ( !$title ) {
146 $this->error( "Internal error: no page for ID $id" );
148 return;
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 ),
162 $domain,
163 $protocol
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" );
173 } else {
174 $dbw = $this->getPrimaryDB();
175 $this->beginTransaction( $dbw, __METHOD__ );
176 $page = $services->getWikiPageFactory()->newFromTitle( $title );
177 if ( $rev ) {
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
184 $content,
185 $performer,
186 wfMessage( 'spam_reverting', $domain )->inContentLanguage()->text(),
187 EDIT_UPDATE | EDIT_FORCE_BOT,
188 $rev->getId()
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() );
195 } else {
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(
203 $content,
204 $performer,
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;