45 parent::__construct();
46 $this->addDescription(
'Cleanup all spam from a given hostname' );
47 $this->addOption(
'all',
'Check all wikis in $wgLocalDatabases' );
48 $this->addOption(
'delete',
'Delete pages containing only spam instead of blanking them' );
51 'Hostname that was spamming, single * wildcard in the beginning allowed'
58 $username =
wfMessage(
'spambot_username' )->text();
59 $user = User::newSystemUser( $username );
61 $this->fatalError(
"Invalid username specified in 'spambot_username' message: $username" );
64 $this->getServiceContainer()->getUserGroupManager()->addUserToGroup( $user,
'bot' );
65 StubGlobalUser::setUser( $user );
67 $spec = $this->getArg( 0 );
70 foreach ( [
'http://',
'https://' ] as $prot ) {
71 $conds = LinkFilter::getQueryConditions( $spec, [
'protocol' => $prot ] );
73 $this->fatalError(
"Not a valid hostname specification: $spec" );
75 $protConds[$prot] = $conds;
78 if ( $this->hasOption(
'all' ) ) {
84 $dbr = $this->getDB(
DB_REPLICA, [], $wikiId );
86 foreach ( $protConds as $conds ) {
87 $count = $dbr->newSelectQueryBuilder()
88 ->select(
'COUNT(*)' )
89 ->from(
'externallinks' )
91 ->caller( __METHOD__ )
96 "$IP/maintenance/cleanupSpam.php",
97 [
'--wiki', $wikiId, $spec ]
100 passthru(
"$cmd | sed 's/^/$wikiId: /'" );
105 $this->output(
"All done\n" );
107 $this->output(
"None found\n" );
114 $dbr = $this->getReplicaDB();
115 foreach ( $protConds as $prot => $conds ) {
116 $res = $dbr->newSelectQueryBuilder()
117 ->select(
'el_from' )
119 ->from(
'externallinks' )
121 ->caller( __METHOD__ )
123 $count += $res->numRows();
124 $this->output(
"Found $count articles containing $spec so far...\n" );
125 foreach ( $res as $row ) {
126 $this->cleanupArticle(
135 $this->output(
"Done\n" );
146 private function cleanupArticle( $id, $domain, $protocol,
Authority $performer ) {
147 $title = Title::newFromID( $id );
149 $this->error(
"Internal error: no page for ID $id" );
154 $this->output( $title->getPrefixedDBkey() .
" ..." );
156 $services = $this->getServiceContainer();
157 $revLookup = $services->getRevisionLookup();
158 $rev = $revLookup->getRevisionByTitle( $title );
159 $currentRevId = $rev->getId();
161 while ( $rev && ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ||
162 LinkFilter::matchEntry(
164 $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW ),
169 $rev = $revLookup->getPreviousRevision( $rev );
172 if ( $rev && $rev->getId() == $currentRevId ) {
175 $this->output(
"False match\n" );
177 $dbw = $this->getPrimaryDB();
178 $this->beginTransaction( $dbw, __METHOD__ );
179 $page = $services->getWikiPageFactory()->newFromTitle( $title );
182 $content = $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW );
184 $this->output(
"reverting\n" );
185 $page->doUserEditContent(
189 wfMessage(
'spam_reverting', $domain )->inContentLanguage()->text(),
193 } elseif ( $this->hasOption(
'delete' ) ) {
195 $this->output(
"deleting\n" );
196 $deletePage = $services->getDeletePageFactory()->newDeletePage( $page, $performer );
197 $deletePage->deleteUnsafe(
wfMessage(
'spam_deleting', $domain )->inContentLanguage()->text() );
200 $handler = $services->getContentHandlerFactory()
201 ->getContentHandler( $title->getContentModel() );
202 $content = $handler->makeEmptyContent();
204 $this->output(
"blanking\n" );
205 $page->doUserEditContent(
208 wfMessage(
'spam_blanking', $domain )->inContentLanguage()->text(),
212 $this->commitTransaction( $dbw, __METHOD__ );