Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 110 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
CleanupSpam | |
0.00% |
0 / 110 |
|
0.00% |
0 / 3 |
506 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 59 |
|
0.00% |
0 / 1 |
156 | |||
cleanupArticle | |
0.00% |
0 / 43 |
|
0.00% |
0 / 1 |
90 |
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 | 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; |
31 | use Wikimedia\Rdbms\Database; |
32 | |
33 | // @codeCoverageIgnoreStart |
34 | require_once __DIR__ . '/Maintenance.php'; |
35 | // @codeCoverageIgnoreEnd |
36 | |
37 | /** |
38 | * Maintenance script to cleanup all spam from a given hostname. |
39 | * |
40 | * @ingroup Maintenance |
41 | */ |
42 | class CleanupSpam extends Maintenance { |
43 | |
44 | public function __construct() { |
45 | parent::__construct(); |
46 | $this->addDescription( 'Cleanup all spam from a given hostname' ); |
47 | $this->addOption( 'all', 'Check all wikis in $wgLocalDatabases' ); |
48 | $this->addOption( 'delete', 'Delete pages containing only spam instead of blanking them' ); |
49 | $this->addArg( |
50 | 'hostname', |
51 | 'Hostname that was spamming, single * wildcard in the beginning allowed' |
52 | ); |
53 | } |
54 | |
55 | public function execute() { |
56 | global $IP, $wgLocalDatabases; |
57 | |
58 | $username = wfMessage( 'spambot_username' )->text(); |
59 | $user = User::newSystemUser( $username ); |
60 | if ( !$user ) { |
61 | $this->fatalError( "Invalid username specified in 'spambot_username' message: $username" ); |
62 | } |
63 | // Hack: Grant bot rights so we don't flood RecentChanges |
64 | $this->getServiceContainer()->getUserGroupManager()->addUserToGroup( $user, 'bot' ); |
65 | StubGlobalUser::setUser( $user ); |
66 | |
67 | $spec = $this->getArg( 0 ); |
68 | |
69 | $protConds = []; |
70 | foreach ( [ 'http://', 'https://' ] as $prot ) { |
71 | $conds = LinkFilter::getQueryConditions( $spec, [ 'protocol' => $prot ] ); |
72 | if ( !$conds ) { |
73 | $this->fatalError( "Not a valid hostname specification: $spec" ); |
74 | } |
75 | $protConds[$prot] = $conds; |
76 | } |
77 | |
78 | if ( $this->hasOption( 'all' ) ) { |
79 | // Clean up spam on all wikis |
80 | $this->output( "Finding spam on " . count( $wgLocalDatabases ) . " wikis\n" ); |
81 | $found = false; |
82 | foreach ( $wgLocalDatabases as $wikiId ) { |
83 | /** @var Database $dbr */ |
84 | $dbr = $this->getDB( DB_REPLICA, [], $wikiId ); |
85 | |
86 | foreach ( $protConds as $conds ) { |
87 | $count = $dbr->newSelectQueryBuilder() |
88 | ->select( 'COUNT(*)' ) |
89 | ->from( 'externallinks' ) |
90 | ->where( $conds ) |
91 | ->caller( __METHOD__ ) |
92 | ->fetchField(); |
93 | if ( $count ) { |
94 | $found = true; |
95 | $cmd = wfShellWikiCmd( |
96 | "$IP/maintenance/cleanupSpam.php", |
97 | [ '--wiki', $wikiId, $spec ] |
98 | ); |
99 | // phpcs:ignore MediaWiki.Usage.ForbiddenFunctions.passthru |
100 | passthru( "$cmd | sed 's/^/$wikiId: /'" ); |
101 | } |
102 | } |
103 | } |
104 | if ( $found ) { |
105 | $this->output( "All done\n" ); |
106 | } else { |
107 | $this->output( "None found\n" ); |
108 | } |
109 | } else { |
110 | // Clean up spam on this wiki |
111 | |
112 | $count = 0; |
113 | /** @var Database $dbr */ |
114 | $dbr = $this->getReplicaDB(); |
115 | foreach ( $protConds as $prot => $conds ) { |
116 | $res = $dbr->newSelectQueryBuilder() |
117 | ->select( 'el_from' ) |
118 | ->distinct() |
119 | ->from( 'externallinks' ) |
120 | ->where( $conds ) |
121 | ->caller( __METHOD__ ) |
122 | ->fetchResultSet(); |
123 | $count += $res->numRows(); |
124 | $this->output( "Found $count articles containing $spec so far...\n" ); |
125 | foreach ( $res as $row ) { |
126 | $this->beginTransactionRound( __METHOD__ ); |
127 | $this->cleanupArticle( |
128 | $row->el_from, |
129 | $spec, |
130 | $prot, |
131 | $user |
132 | ); |
133 | $this->commitTransactionRound( __METHOD__ ); |
134 | } |
135 | } |
136 | if ( $count ) { |
137 | $this->output( "Done\n" ); |
138 | } |
139 | } |
140 | } |
141 | |
142 | /** |
143 | * @param int $id |
144 | * @param string $domain |
145 | * @param string $protocol |
146 | * @param Authority $performer |
147 | */ |
148 | private function cleanupArticle( $id, $domain, $protocol, Authority $performer ) { |
149 | $title = Title::newFromID( $id ); |
150 | if ( !$title ) { |
151 | $this->error( "Internal error: no page for ID $id" ); |
152 | |
153 | return; |
154 | } |
155 | |
156 | $this->output( $title->getPrefixedDBkey() . " ..." ); |
157 | |
158 | $services = $this->getServiceContainer(); |
159 | $revLookup = $services->getRevisionLookup(); |
160 | $rev = $revLookup->getRevisionByTitle( $title ); |
161 | $currentRevId = $rev->getId(); |
162 | |
163 | while ( $rev && ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) || |
164 | LinkFilter::matchEntry( |
165 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable RAW never returns null |
166 | $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW ), |
167 | $domain, |
168 | $protocol |
169 | ) ) |
170 | ) { |
171 | $rev = $revLookup->getPreviousRevision( $rev ); |
172 | } |
173 | |
174 | if ( $rev && $rev->getId() == $currentRevId ) { |
175 | // The regex didn't match the current article text |
176 | // This happens e.g. when a link comes from a template rather than the page itself |
177 | $this->output( "False match\n" ); |
178 | } else { |
179 | $page = $services->getWikiPageFactory()->newFromTitle( $title ); |
180 | if ( $rev ) { |
181 | // Revert to this revision |
182 | $content = $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW ); |
183 | |
184 | $this->output( "reverting\n" ); |
185 | $page->doUserEditContent( |
186 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable RAW never returns null |
187 | $content, |
188 | $performer, |
189 | wfMessage( 'spam_reverting', $domain )->inContentLanguage()->text(), |
190 | EDIT_UPDATE | EDIT_FORCE_BOT, |
191 | $rev->getId() |
192 | ); |
193 | } elseif ( $this->hasOption( 'delete' ) ) { |
194 | // Didn't find a non-spammy revision, blank the page |
195 | $this->output( "deleting\n" ); |
196 | $deletePage = $services->getDeletePageFactory()->newDeletePage( $page, $performer ); |
197 | $deletePage->deleteUnsafe( wfMessage( 'spam_deleting', $domain )->inContentLanguage()->text() ); |
198 | } else { |
199 | // Didn't find a non-spammy revision, blank the page |
200 | $handler = $services->getContentHandlerFactory() |
201 | ->getContentHandler( $title->getContentModel() ); |
202 | $content = $handler->makeEmptyContent(); |
203 | |
204 | $this->output( "blanking\n" ); |
205 | $page->doUserEditContent( |
206 | $content, |
207 | $performer, |
208 | wfMessage( 'spam_blanking', $domain )->inContentLanguage()->text(), |
209 | EDIT_UPDATE | EDIT_FORCE_BOT |
210 | ); |
211 | } |
212 | } |
213 | } |
214 | } |
215 | |
216 | // @codeCoverageIgnoreStart |
217 | $maintClass = CleanupSpam::class; |
218 | require_once RUN_MAINTENANCE_IF_MAIN; |
219 | // @codeCoverageIgnoreEnd |