MediaWiki  master
cleanupSpam.php
Go to the documentation of this file.
1 <?php
29 
30 require_once __DIR__ . '/Maintenance.php';
31 
37 class CleanupSpam extends Maintenance {
38 
39  public function __construct() {
40  parent::__construct();
41  $this->addDescription( 'Cleanup all spam from a given hostname' );
42  $this->addOption( 'all', 'Check all wikis in $wgLocalDatabases' );
43  $this->addOption( 'delete', 'Delete pages containing only spam instead of blanking them' );
44  $this->addArg(
45  'hostname',
46  'Hostname that was spamming, single * wildcard in the beginning allowed'
47  );
48  }
49 
50  public function execute() {
51  global $IP, $wgLocalDatabases;
52 
53  $username = wfMessage( 'spambot_username' )->text();
54  $user = User::newSystemUser( $username );
55  if ( !$user ) {
56  $this->fatalError( "Invalid username specified in 'spambot_username' message: $username" );
57  }
58  // Hack: Grant bot rights so we don't flood RecentChanges
59  MediaWikiServices::getInstance()->getUserGroupManager()->addUserToGroup( $user, 'bot' );
60  StubGlobalUser::setUser( $user );
61 
62  $spec = $this->getArg( 0 );
63 
64  $protConds = [];
65  foreach ( [ 'http://', 'https://' ] as $prot ) {
66  $conds = LinkFilter::getQueryConditions( $spec, [ 'protocol' => $prot ] );
67  if ( !$conds ) {
68  $this->fatalError( "Not a valid hostname specification: $spec" );
69  }
70  $protConds[$prot] = $conds;
71  }
72 
73  if ( $this->hasOption( 'all' ) ) {
74  // Clean up spam on all wikis
75  $this->output( "Finding spam on " . count( $wgLocalDatabases ) . " wikis\n" );
76  $found = false;
77  foreach ( $wgLocalDatabases as $wikiId ) {
79  $dbr = $this->getDB( DB_REPLICA, [], $wikiId );
80 
81  foreach ( $protConds as $conds ) {
82  $count = $dbr->newSelectQueryBuilder()
83  ->select( 'COUNT(*)' )
84  ->from( 'externallinks' )
85  ->where( $conds )
86  ->caller( __METHOD__ )
87  ->fetchField();
88  if ( $count ) {
89  $found = true;
90  $cmd = wfShellWikiCmd(
91  "$IP/maintenance/cleanupSpam.php",
92  [ '--wiki', $wikiId, $spec ]
93  );
94  passthru( "$cmd | sed 's/^/$wikiId: /'" );
95  }
96  }
97  }
98  if ( $found ) {
99  $this->output( "All done\n" );
100  } else {
101  $this->output( "None found\n" );
102  }
103  } else {
104  // Clean up spam on this wiki
105 
106  $count = 0;
108  $dbr = $this->getDB( DB_REPLICA );
109  foreach ( $protConds as $prot => $conds ) {
110  $res = $dbr->newSelectQueryBuilder()
111  ->select( 'el_from' )
112  ->distinct()
113  ->from( 'externallinks' )
114  ->where( $conds )
115  ->caller( __METHOD__ )
116  ->fetchResultSet();
117  $count = $res->numRows();
118  $this->output( "Found $count articles containing $spec\n" );
119  foreach ( $res as $row ) {
120  $this->cleanupArticle(
121  $row->el_from,
122  $spec,
123  $prot,
124  $user
125  );
126  }
127  }
128  if ( $count ) {
129  $this->output( "Done\n" );
130  }
131  }
132  }
133 
141  private function cleanupArticle( $id, $domain, $protocol, Authority $performer ) {
142  $title = Title::newFromID( $id );
143  if ( !$title ) {
144  $this->error( "Internal error: no page for ID $id" );
145 
146  return;
147  }
148 
149  $this->output( $title->getPrefixedDBkey() . " ..." );
150 
151  $services = MediaWikiServices::getInstance();
152  $revLookup = $services->getRevisionLookup();
153  $rev = $revLookup->getRevisionByTitle( $title );
154  $currentRevId = $rev->getId();
155 
156  while ( $rev && ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ||
158  // @phan-suppress-next-line PhanTypeMismatchArgumentNullable RAW never returns null
159  $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW ),
160  $domain,
161  $protocol
162  ) )
163  ) {
164  $rev = $revLookup->getPreviousRevision( $rev );
165  }
166 
167  if ( $rev && $rev->getId() == $currentRevId ) {
168  // The regex didn't match the current article text
169  // This happens e.g. when a link comes from a template rather than the page itself
170  $this->output( "False match\n" );
171  } else {
172  $dbw = $this->getDB( DB_PRIMARY );
173  $this->beginTransaction( $dbw, __METHOD__ );
174  $page = $services->getWikiPageFactory()->newFromTitle( $title );
175  if ( $rev ) {
176  // Revert to this revision
177  $content = $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW );
178 
179  $this->output( "reverting\n" );
180  $page->doUserEditContent(
181  // @phan-suppress-next-line PhanTypeMismatchArgumentNullable RAW never returns null
182  $content,
183  $performer,
184  wfMessage( 'spam_reverting', $domain )->inContentLanguage()->text(),
186  $rev->getId()
187  );
188  } elseif ( $this->hasOption( 'delete' ) ) {
189  // Didn't find a non-spammy revision, blank the page
190  $this->output( "deleting\n" );
191  $page->doDeleteArticleReal(
192  wfMessage( 'spam_deleting', $domain )->inContentLanguage()->text(),
193  $performer->getUser()
194  );
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();
200 
201  $this->output( "blanking\n" );
202  $page->doUserEditContent(
203  $content,
204  $performer,
205  wfMessage( 'spam_blanking', $domain )->inContentLanguage()->text(),
207  );
208  }
209  $this->commitTransaction( $dbw, __METHOD__ );
210  }
211  }
212 }
213 
214 $maintClass = CleanupSpam::class;
215 require_once RUN_MAINTENANCE_IF_MAIN;
const EDIT_FORCE_BOT
Definition: Defines.php:130
const EDIT_UPDATE
Definition: Defines.php:127
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.
if(!defined( 'MEDIAWIKI')) if(ini_get( 'mbstring.func_overload')) if(!defined( 'MW_ENTRY_POINT')) global $IP
Environment checks.
Definition: Setup.php:91
Maintenance script to cleanup all spam from a given hostname.
Definition: cleanupSpam.php:37
__construct()
Default constructor.
Definition: cleanupSpam.php:39
execute()
Do the actual work.
Definition: cleanupSpam.php:50
static getQueryConditions( $filterEntry, array $options=[])
Return query conditions which will match the specified string.
Definition: LinkFilter.php:253
static matchEntry(Content $content, $filterEntry, $protocol='http://')
Check whether $content contains a link to $filterEntry.
Definition: LinkFilter.php:51
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:66
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
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 transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction 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.
Service locator for MediaWiki core services.
Page revision base class.
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40
Stub object for the global user ($wgUser) that makes it possible to change the relevant underlying ob...
static newFromID( $id, $flags=0)
Create a new Title from an article ID.
Definition: Title.php:521
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:795
$maintClass
$wgLocalDatabases
Config variable stub for the LocalDatabases setting, for use by phpdoc and IDEs.
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37
getUser()
Returns the performer of the actions associated with this authority.
const DB_REPLICA
Definition: defines.php:26
const DB_PRIMARY
Definition: defines.php:28
$content
Definition: router.php:76