33require_once __DIR__ .
'/Maintenance.php';
43 parent::__construct();
45 $this->
addOption(
'all',
'Check all wikis in $wgLocalDatabases' );
46 $this->
addOption(
'delete',
'Delete pages containing only spam instead of blanking them' );
49 'Hostname that was spamming, single * wildcard in the beginning allowed'
56 $username =
wfMessage(
'spambot_username' )->text();
57 $user = User::newSystemUser( $username );
59 $this->
fatalError(
"Invalid username specified in 'spambot_username' message: $username" );
63 StubGlobalUser::setUser( $user );
65 $spec = $this->
getArg( 0 );
68 foreach ( [
'http://',
'https://' ] as $prot ) {
69 $conds = LinkFilter::getQueryConditions( $spec, [
'protocol' => $prot ] );
71 $this->
fatalError(
"Not a valid hostname specification: $spec" );
73 $protConds[$prot] = $conds;
84 foreach ( $protConds as $conds ) {
85 $count = $dbr->newSelectQueryBuilder()
86 ->select(
'COUNT(*)' )
87 ->from(
'externallinks' )
89 ->caller( __METHOD__ )
94 "$IP/maintenance/cleanupSpam.php",
95 [
'--wiki', $wikiId, $spec ]
98 passthru(
"$cmd | sed 's/^/$wikiId: /'" );
103 $this->
output(
"All done\n" );
105 $this->
output(
"None found\n" );
113 foreach ( $protConds as $prot => $conds ) {
114 $res = $dbr->newSelectQueryBuilder()
115 ->select(
'el_from' )
117 ->from(
'externallinks' )
119 ->caller( __METHOD__ )
121 $count += $res->numRows();
122 $this->
output(
"Found $count articles containing $spec so far...\n" );
123 foreach ( $res as $row ) {
124 $this->cleanupArticle(
133 $this->
output(
"Done\n" );
144 private function cleanupArticle( $id, $domain, $protocol,
Authority $performer ) {
145 $title = Title::newFromID( $id );
147 $this->
error(
"Internal error: no page for ID $id" );
152 $this->
output( $title->getPrefixedDBkey() .
" ..." );
155 $revLookup = $services->getRevisionLookup();
156 $rev = $revLookup->getRevisionByTitle( $title );
157 $currentRevId = $rev->getId();
159 while ( $rev && ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ||
160 LinkFilter::matchEntry(
162 $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW ),
167 $rev = $revLookup->getPreviousRevision( $rev );
170 if ( $rev && $rev->getId() == $currentRevId ) {
173 $this->
output(
"False match\n" );
177 $page = $services->getWikiPageFactory()->newFromTitle( $title );
180 $content = $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW );
182 $this->
output(
"reverting\n" );
183 $page->doUserEditContent(
187 wfMessage(
'spam_reverting', $domain )->inContentLanguage()->text(),
191 } elseif ( $this->
hasOption(
'delete' ) ) {
193 $this->
output(
"deleting\n" );
194 $deletePage = $services->getDeletePageFactory()->newDeletePage( $page, $performer );
195 $deletePage->deleteUnsafe(
wfMessage(
'spam_deleting', $domain )->inContentLanguage()->text() );
198 $handler = $services->getContentHandlerFactory()
199 ->getContentHandler( $title->getContentModel() );
200 $content = $handler->makeEmptyContent();
202 $this->
output(
"blanking\n" );
203 $page->doUserEditContent(
206 wfMessage(
'spam_blanking', $domain )->inContentLanguage()->text(),
216require_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, $multi=false)
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.
getServiceContainer()
Returns the main service container.
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.
$wgLocalDatabases
Config variable stub for the LocalDatabases setting, for use by phpdoc and IDEs.