MediaWiki  master
cleanupSpam.php
Go to the documentation of this file.
1 <?php
27 
28 require_once __DIR__ . '/Maintenance.php';
29 
35 class 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  $user
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;
208 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
$wgLocalDatabases
string[] $wgLocalDatabases
Other wikis on this site, can be administered from a single developer account.
Definition: DefaultSettings.php:2275
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
CleanupSpam
Maintenance script to cleanup all spam from a given hostname.
Definition: cleanupSpam.php:35
LinkFilter\getQueryConditions
static getQueryConditions( $filterEntry, array $options=[])
Return query conditions which will match the specified string.
Definition: LinkFilter.php:256
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:163
EDIT_FORCE_BOT
const EDIT_FORCE_BOT
Definition: Defines.php:145
LinkFilter\matchEntry
static matchEntry(Content $content, $filterEntry, $protocol='http://')
Check whether $content contains a link to $filterEntry.
Definition: LinkFilter.php:51
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:487
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1220
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
$maintClass
$maintClass
Definition: cleanupSpam.php:207
$res
$res
Definition: testCompression.php:57
wfShellWikiCmd
wfShellWikiCmd( $script, array $parameters=[], array $options=[])
Generate a shell-escaped command line string to run a MediaWiki cli script.
Definition: GlobalFunctions.php:2132
CleanupSpam\cleanupArticle
cleanupArticle( $id, $domain, $protocol, User $deleter)
Definition: cleanupSpam.php:138
$dbr
$dbr
Definition: testCompression.php:54
User\newSystemUser
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:738
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
Definition: Maintenance.php:1401
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:159
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:245
$title
$title
Definition: testCompression.php:38
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
$content
$content
Definition: router.php:76
EDIT_UPDATE
const EDIT_UPDATE
Definition: Defines.php:142
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1416
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1370
CleanupSpam\__construct
__construct()
Default constructor.
Definition: cleanupSpam.php:37
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:298
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:463
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:266
CleanupSpam\execute
execute()
Do the actual work.
Definition: cleanupSpam.php:48
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:351
$IP
$IP
Definition: WebStart.php:49
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:56
Title\newFromID
static newFromID( $id, $flags=0)
Create a new Title from an article ID.
Definition: Title.php:472
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40