29require_once __DIR__ .
'/Maintenance.php';
39 parent::__construct();
41 $this->
addOption(
'all',
'Check all wikis in $wgLocalDatabases' );
42 $this->
addOption(
'delete',
'Delete pages containing only spam instead of blanking them' );
45 'Hostname that was spamming, single * wildcard in the beginning allowed'
52 $username =
wfMessage(
'spambot_username' )->text();
55 $this->
fatalError(
"Invalid username specified in 'spambot_username' message: $username" );
58 MediaWikiServices::getInstance()->getUserGroupManager()->addUserToGroup( $user,
'bot' );
61 $spec = $this->
getArg( 0 );
64 foreach ( [
'http://',
'https://' ] as $prot ) {
65 $conds = LinkFilter::getQueryConditions( $spec, [
'protocol' => $prot ] );
67 $this->
fatalError(
"Not a valid hostname specification: $spec" );
69 $protConds[$prot] = $conds;
80 foreach ( $protConds as $conds ) {
81 $count =
$dbr->newSelectQueryBuilder()
82 ->select(
'COUNT(*)' )
83 ->from(
'externallinks' )
85 ->caller( __METHOD__ )
90 "$IP/maintenance/cleanupSpam.php",
91 [
'--wiki', $wikiId, $spec ]
93 passthru(
"$cmd | sed 's/^/$wikiId: /'" );
98 $this->
output(
"All done\n" );
100 $this->
output(
"None found\n" );
108 foreach ( $protConds as $prot => $conds ) {
109 $res =
$dbr->newSelectQueryBuilder()
110 ->select(
'el_from' )
112 ->from(
'externallinks' )
114 ->caller( __METHOD__ )
116 $count =
$res->numRows();
117 $this->
output(
"Found $count articles containing $spec\n" );
118 foreach (
$res as $row ) {
119 $this->cleanupArticle(
128 $this->
output(
"Done\n" );
140 private function cleanupArticle( $id, $domain, $protocol,
Authority $performer ) {
141 $title = Title::newFromID( $id );
143 $this->
error(
"Internal error: no page for ID $id" );
150 $services = MediaWikiServices::getInstance();
151 $revLookup = $services->getRevisionLookup();
152 $rev = $revLookup->getRevisionByTitle(
$title );
153 $currentRevId = $rev->getId();
155 while ( $rev && ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ||
156 LinkFilter::matchEntry(
158 $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW ),
163 $rev = $revLookup->getPreviousRevision( $rev );
166 if ( $rev && $rev->getId() == $currentRevId ) {
169 $this->
output(
"False match\n" );
173 $page = $services->getWikiPageFactory()->newFromTitle(
$title );
176 $content = $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW );
178 $this->
output(
"reverting\n" );
179 $page->doUserEditContent(
183 wfMessage(
'spam_reverting', $domain )->inContentLanguage()->text(),
187 } elseif ( $this->
hasOption(
'delete' ) ) {
189 $this->
output(
"deleting\n" );
190 $page->doDeleteArticleReal(
191 wfMessage(
'spam_deleting', $domain )->inContentLanguage()->text(),
196 $handler = $services->getContentHandlerFactory()
197 ->getContentHandler(
$title->getContentModel() );
198 $content = $handler->makeEmptyContent();
200 $this->
output(
"blanking\n" );
201 $page->doUserEditContent(
204 wfMessage(
'spam_blanking', $domain )->inContentLanguage()->text(),
214require_once RUN_MAINTENANCE_IF_MAIN;
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.
Maintenance script to cleanup all spam from a given hostname.
__construct()
Default constructor.
execute()
Do the actual work.
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 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.
static setUser( $user)
Reset the stub global user to a different "real" user object, while ensuring that any method calls on...
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
$wgLocalDatabases
Config variable stub for the LocalDatabases setting, for use by phpdoc and IDEs.