MediaWiki REL1_35
cleanupSpam.php
Go to the documentation of this file.
1<?php
27
28require_once __DIR__ . '/Maintenance.php';
29
35class CleanupSpam extends Maintenance {
36
37 public function __construct() {
38 parent::__construct();
39 $this->addDescription( 'Cleanup all spam from a given hostname' );
40 $this->addOption( 'all', 'Check all wikis in $wgLocalDatabases' );
41 $this->addOption( 'delete', 'Delete pages containing only spam instead of blanking them' );
42 $this->addArg(
43 'hostname',
44 'Hostname that was spamming, single * wildcard in the beginning allowed'
45 );
46 }
47
48 public function execute() {
49 global $IP, $wgLocalDatabases, $wgUser;
50
51 $username = wfMessage( 'spambot_username' )->text();
52 $user = User::newSystemUser( $username );
53 if ( !$user ) {
54 $this->fatalError( "Invalid username specified in 'spambot_username' message: $username" );
55 }
56 // Hack: Grant bot rights so we don't flood RecentChanges
57 $user->addGroup( 'bot' );
58 $wgUser = $user;
59
60 $spec = $this->getArg( 0 );
61
62 $protConds = [];
63 foreach ( [ 'http://', 'https://' ] as $prot ) {
64 $conds = LinkFilter::getQueryConditions( $spec, [ 'protocol' => $prot ] );
65 if ( !$conds ) {
66 $this->fatalError( "Not a valid hostname specification: $spec" );
67 }
68 $protConds[$prot] = $conds;
69 }
70
71 if ( $this->hasOption( 'all' ) ) {
72 // Clean up spam on all wikis
73 $this->output( "Finding spam on " . count( $wgLocalDatabases ) . " wikis\n" );
74 $found = false;
75 foreach ( $wgLocalDatabases as $wikiId ) {
77 $dbr = $this->getDB( DB_REPLICA, [], $wikiId );
78
79 foreach ( $protConds as $conds ) {
80 $count = $dbr->selectField(
81 'externallinks',
82 'COUNT(*)',
83 $conds,
84 __METHOD__
85 );
86 if ( $count ) {
87 $found = true;
88 $cmd = wfShellWikiCmd(
89 "$IP/maintenance/cleanupSpam.php",
90 [ '--wiki', $wikiId, $spec ]
91 );
92 passthru( "$cmd | sed 's/^/$wikiId: /'" );
93 }
94 }
95 }
96 if ( $found ) {
97 $this->output( "All done\n" );
98 } else {
99 $this->output( "None found\n" );
100 }
101 } else {
102 // Clean up spam on this wiki
103
104 $count = 0;
106 $dbr = $this->getDB( DB_REPLICA );
107 foreach ( $protConds as $prot => $conds ) {
108 $res = $dbr->select(
109 'externallinks',
110 [ 'DISTINCT el_from' ],
111 $conds,
112 __METHOD__
113 );
114 $count = $dbr->numRows( $res );
115 $this->output( "Found $count articles containing $spec\n" );
116 foreach ( $res as $row ) {
117 $this->cleanupArticle(
118 $row->el_from,
119 $spec,
120 $prot,
121 $wgUser
122 );
123 }
124 }
125 if ( $count ) {
126 $this->output( "Done\n" );
127 }
128 }
129 }
130
138 private function cleanupArticle( $id, $domain, $protocol, User $deleter ) {
139 $title = Title::newFromID( $id );
140 if ( !$title ) {
141 $this->error( "Internal error: no page for ID $id" );
142
143 return;
144 }
145
146 $this->output( $title->getPrefixedDBkey() . " ..." );
147
148 $revLookup = MediaWikiServices::getInstance()->getRevisionLookup();
149 $rev = $revLookup->getRevisionByTitle( $title );
150 $currentRevId = $rev->getId();
151
152 while ( $rev && ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ||
154 $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW ),
155 $domain,
156 $protocol
157 ) )
158 ) {
159 $rev = $revLookup->getPreviousRevision( $rev );
160 }
161
162 if ( $rev && $rev->getId() == $currentRevId ) {
163 // The regex didn't match the current article text
164 // This happens e.g. when a link comes from a template rather than the page itself
165 $this->output( "False match\n" );
166 } else {
167 $dbw = $this->getDB( DB_MASTER );
168 $this->beginTransaction( $dbw, __METHOD__ );
169 $page = WikiPage::factory( $title );
170 if ( $rev ) {
171 // Revert to this revision
172 $content = $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW );
173
174 $this->output( "reverting\n" );
175 $page->doEditContent(
176 $content,
177 wfMessage( 'spam_reverting', $domain )->inContentLanguage()->text(),
179 $rev->getId()
180 );
181 } elseif ( $this->hasOption( 'delete' ) ) {
182 // Didn't find a non-spammy revision, blank the page
183 $this->output( "deleting\n" );
184 $page->doDeleteArticleReal(
185 wfMessage( 'spam_deleting', $domain )->inContentLanguage()->text(),
186 $deleter
187 );
188 } else {
189 // Didn't find a non-spammy revision, blank the page
190 $handler = MediaWikiServices::getInstance()
191 ->getContentHandlerFactory()
192 ->getContentHandler( $title->getContentModel() );
193 $content = $handler->makeEmptyContent();
194
195 $this->output( "blanking\n" );
196 $page->doEditContent(
197 $content,
198 wfMessage( 'spam_blanking', $domain )->inContentLanguage()->text(),
200 );
201 }
202 $this->commitTransaction( $dbw, __METHOD__ );
203 }
204 }
205}
206
207$maintClass = CleanupSpam::class;
208require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
string[] $wgLocalDatabases
Other wikis on this site, can be administered from a single developer account.
wfShellWikiCmd( $script, array $parameters=[], array $options=[])
Generate a shell-escaped command line string to run a MediaWiki cli script.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
const RUN_MAINTENANCE_IF_MAIN
$IP
Definition WebStart.php:49
Maintenance script to cleanup all spam from a given hostname.
cleanupArticle( $id, $domain, $protocol, User $deleter)
__construct()
Default constructor.
execute()
Do the actual work.
static getQueryConditions( $filterEntry, array $options=[])
Return query conditions which will match the specified string.
static matchEntry(Content $content, $filterEntry, $protocol='http://')
Check whether $content contains a link to $filterEntry.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
addArg( $arg, $description, $required=true)
Add some args that are needed.
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Page revision base class.
Value object representing a content slot associated with a page revision.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:60
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition User.php:758
$maintClass
const EDIT_FORCE_BOT
Definition Defines.php:146
const EDIT_UPDATE
Definition Defines.php:143
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:29
$content
Definition router.php:76