32 require_once __DIR__ .
'/Maintenance.php';
42 parent::__construct();
44 $this->
addOption(
'all',
'Check all wikis in $wgLocalDatabases' );
45 $this->
addOption(
'delete',
'Delete pages containing only spam instead of blanking them' );
48 'Hostname that was spamming, single * wildcard in the beginning allowed'
55 $username =
wfMessage(
'spambot_username' )->text();
56 $user = User::newSystemUser( $username );
58 $this->
fatalError(
"Invalid username specified in 'spambot_username' message: $username" );
62 StubGlobalUser::setUser( $user );
64 $spec = $this->
getArg( 0 );
67 foreach ( [
'http://',
'https://' ] as $prot ) {
68 $conds = LinkFilter::getQueryConditions( $spec, [
'protocol' => $prot ] );
70 $this->
fatalError(
"Not a valid hostname specification: $spec" );
72 $protConds[$prot] = $conds;
83 foreach ( $protConds as $conds ) {
84 $count = $dbr->newSelectQueryBuilder()
85 ->select(
'COUNT(*)' )
86 ->from(
'externallinks' )
88 ->caller( __METHOD__ )
93 "$IP/maintenance/cleanupSpam.php",
94 [
'--wiki', $wikiId, $spec ]
97 passthru(
"$cmd | sed 's/^/$wikiId: /'" );
102 $this->
output(
"All done\n" );
104 $this->
output(
"None found\n" );
112 foreach ( $protConds as $prot => $conds ) {
113 $res = $dbr->newSelectQueryBuilder()
114 ->select(
'el_from' )
116 ->from(
'externallinks' )
118 ->caller( __METHOD__ )
120 $count += $res->numRows();
121 $this->
output(
"Found $count articles containing $spec so far...\n" );
122 foreach ( $res as $row ) {
123 $this->cleanupArticle(
132 $this->
output(
"Done\n" );
143 private function cleanupArticle( $id, $domain, $protocol,
Authority $performer ) {
144 $title = Title::newFromID( $id );
146 $this->
error(
"Internal error: no page for ID $id" );
151 $this->
output( $title->getPrefixedDBkey() .
" ..." );
154 $revLookup = $services->getRevisionLookup();
155 $rev = $revLookup->getRevisionByTitle( $title );
156 $currentRevId = $rev->getId();
158 while ( $rev && ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ||
159 LinkFilter::matchEntry(
161 $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW ),
166 $rev = $revLookup->getPreviousRevision( $rev );
169 if ( $rev && $rev->getId() == $currentRevId ) {
172 $this->
output(
"False match\n" );
176 $page = $services->getWikiPageFactory()->newFromTitle( $title );
179 $content = $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW );
181 $this->
output(
"reverting\n" );
182 $page->doUserEditContent(
186 wfMessage(
'spam_reverting', $domain )->inContentLanguage()->text(),
190 } elseif ( $this->
hasOption(
'delete' ) ) {
192 $this->
output(
"deleting\n" );
193 $deletePage = $services->getDeletePageFactory()->newDeletePage( $page, $performer );
194 $deletePage->deleteUnsafe(
wfMessage(
'spam_deleting', $domain )->inContentLanguage()->text() );
197 $handler = $services->getContentHandlerFactory()
198 ->getContentHandler( $title->getContentModel() );
199 $content = $handler->makeEmptyContent();
201 $this->
output(
"blanking\n" );
202 $page->doUserEditContent(
205 wfMessage(
'spam_blanking', $domain )->inContentLanguage()->text(),
215 require_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...
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, $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.