21require_once __DIR__ .
'/Maintenance.php';
32 parent::__construct();
34 $this->
addOption(
'all',
'Check all wikis in $wgLocalDatabases' );
35 $this->
addOption(
'delete',
'Delete pages containing only spam instead of blanking them' );
38 'Hostname that was spamming, single * wildcard in the beginning allowed'
45 $username =
wfMessage(
'spambot_username' )->text();
46 $user = User::newSystemUser( $username );
48 $this->
fatalError(
"Invalid username specified in 'spambot_username' message: $username" );
53 $spec = $this->
getArg( 0 );
56 foreach ( [
'http://',
'https://' ] as $prot ) {
57 $conds = LinkFilter::getQueryConditions( $spec, [
'protocol' => $prot ] );
59 $this->
fatalError(
"Not a valid hostname specification: $spec" );
61 $protConds[$prot] = $conds;
71 foreach ( $protConds as $conds ) {
72 $count = $dbr->newSelectQueryBuilder()
73 ->select(
'COUNT(*)' )
74 ->from(
'externallinks' )
76 ->caller( __METHOD__ )
80 $cmd = Shell::makeScriptCommand(
81 MW_INSTALL_PATH .
'/maintenance/cleanupSpam.php',
82 [
'--wiki', $wikiId, $spec ]
85 passthru(
"$cmd | sed 's/^/$wikiId: /'" );
90 $this->
output(
"All done\n" );
92 $this->
output(
"None found\n" );
99 ExternalLinksTable::VIRTUAL_DOMAIN
101 foreach ( $protConds as $prot => $conds ) {
102 $res = $dbr->newSelectQueryBuilder()
103 ->select(
'el_from' )
105 ->from(
'externallinks' )
107 ->caller( __METHOD__ )
109 $count += $res->numRows();
110 $this->
output(
"Found $count articles containing $spec so far...\n" );
111 foreach ( $res as $row ) {
113 $this->cleanupArticle(
123 $this->
output(
"Done\n" );
134 private function cleanupArticle( $id, $domain, $protocol,
Authority $performer ) {
135 $title = Title::newFromID( $id );
137 $this->
error(
"Internal error: no page for ID $id" );
142 $this->
output( $title->getPrefixedDBkey() .
" ..." );
145 $revLookup = $services->getRevisionLookup();
146 $rev = $revLookup->getRevisionByTitle( $title );
147 $currentRevId = $rev->getId();
149 while ( $rev && ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ||
150 LinkFilter::matchEntry(
152 $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW ),
157 $rev = $revLookup->getPreviousRevision( $rev );
160 if ( $rev && $rev->getId() == $currentRevId ) {
163 $this->
output(
"False match\n" );
165 $page = $services->getWikiPageFactory()->newFromTitle( $title );
168 $content = $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW );
170 $this->
output(
"reverting\n" );
171 $page->doUserEditContent(
175 wfMessage(
'spam_reverting', $domain )->inContentLanguage()->text(),
179 } elseif ( $this->
hasOption(
'delete' ) ) {
181 $this->
output(
"deleting\n" );
182 $deletePage = $services->getDeletePageFactory()->newDeletePage( $page, $performer );
183 $deletePage->deleteUnsafe(
wfMessage(
'spam_deleting', $domain )->inContentLanguage()->text() );
186 $handler = $services->getContentHandlerFactory()
187 ->getContentHandler( $title->getContentModel() );
188 $content = $handler->makeEmptyContent();
190 $this->
output(
"blanking\n" );
191 $page->doUserEditContent(
194 wfMessage(
'spam_blanking', $domain )->inContentLanguage()->text(),
204require_once RUN_MAINTENANCE_IF_MAIN;
const EDIT_FORCE_BOT
Mark the edit a "bot" edit regardless of user rights.
const EDIT_UPDATE
Article is assumed to be pre-existing, fail if it doesn't exist.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
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...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArg( $argId=0, $default=null)
Get an argument.
output( $out, $channel=null)
Throw some output to the user.
error( $err)
Throw an error to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
commitTransactionRound( $fname)
Commit a transactional batch of DB operations and wait for replica DB servers to catch up.
getReplicaDB(string|false $virtualDomain=false)
beginTransactionRound( $fname)
Start a transactional batch of DB operations.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
$wgLocalDatabases
Config variable stub for the LocalDatabases setting, for use by phpdoc and IDEs.