Avoid pointless use of isset() in LBFactoryMulti()
[mediawiki.git] / maintenance / cleanupSpam.php
blob4e47cfba57a88043e6edd40bcf88506e3609bd8f
1 <?php
2 /**
3 * Cleanup all spam from a given hostname.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Maintenance
24 require_once __DIR__ . '/Maintenance.php';
26 /**
27 * Maintenance script to cleanup all spam from a given hostname.
29 * @ingroup Maintenance
31 class CleanupSpam extends Maintenance {
33 public function __construct() {
34 parent::__construct();
35 $this->addDescription( 'Cleanup all spam from a given hostname' );
36 $this->addOption( 'all', 'Check all wikis in $wgLocalDatabases' );
37 $this->addOption( 'delete', 'Delete pages containing only spam instead of blanking them' );
38 $this->addArg(
39 'hostname',
40 'Hostname that was spamming, single * wildcard in the beginning allowed'
44 public function execute() {
45 global $IP, $wgLocalDatabases, $wgUser;
47 $username = wfMessage( 'spambot_username' )->text();
48 $wgUser = User::newSystemUser( $username );
49 if ( !$wgUser ) {
50 $this->error( "Invalid username specified in 'spambot_username' message: $username", true );
52 // Create the user if necessary
53 if ( !$wgUser->getId() ) {
54 $wgUser->addToDatabase();
56 $spec = $this->getArg();
57 $like = LinkFilter::makeLikeArray( $spec );
58 if ( !$like ) {
59 $this->error( "Not a valid hostname specification: $spec", true );
62 if ( $this->hasOption( 'all' ) ) {
63 // Clean up spam on all wikis
64 $this->output( "Finding spam on " . count( $wgLocalDatabases ) . " wikis\n" );
65 $found = false;
66 foreach ( $wgLocalDatabases as $wikiID ) {
67 $dbr = $this->getDB( DB_REPLICA, [], $wikiID );
69 $count = $dbr->selectField( 'externallinks', 'COUNT(*)',
70 [ 'el_index' . $dbr->buildLike( $like ) ], __METHOD__ );
71 if ( $count ) {
72 $found = true;
73 $cmd = wfShellWikiCmd( "$IP/maintenance/cleanupSpam.php",
74 [ '--wiki', $wikiID, $spec ] );
75 passthru( "$cmd | sed 's/^/$wikiID: /'" );
78 if ( $found ) {
79 $this->output( "All done\n" );
80 } else {
81 $this->output( "None found\n" );
83 } else {
84 // Clean up spam on this wiki
86 $dbr = $this->getDB( DB_REPLICA );
87 $res = $dbr->select( 'externallinks', [ 'DISTINCT el_from' ],
88 [ 'el_index' . $dbr->buildLike( $like ) ], __METHOD__ );
89 $count = $dbr->numRows( $res );
90 $this->output( "Found $count articles containing $spec\n" );
91 foreach ( $res as $row ) {
92 $this->cleanupArticle( $row->el_from, $spec );
94 if ( $count ) {
95 $this->output( "Done\n" );
100 private function cleanupArticle( $id, $domain ) {
101 $title = Title::newFromID( $id );
102 if ( !$title ) {
103 $this->error( "Internal error: no page for ID $id" );
105 return;
108 $this->output( $title->getPrefixedDBkey() . " ..." );
109 $rev = Revision::newFromTitle( $title );
110 $currentRevId = $rev->getId();
112 while ( $rev && ( $rev->isDeleted( Revision::DELETED_TEXT )
113 || LinkFilter::matchEntry( $rev->getContent( Revision::RAW ), $domain ) )
115 $rev = $rev->getPrevious();
118 if ( $rev && $rev->getId() == $currentRevId ) {
119 // The regex didn't match the current article text
120 // This happens e.g. when a link comes from a template rather than the page itself
121 $this->output( "False match\n" );
122 } else {
123 $dbw = $this->getDB( DB_MASTER );
124 $this->beginTransaction( $dbw, __METHOD__ );
125 $page = WikiPage::factory( $title );
126 if ( $rev ) {
127 // Revert to this revision
128 $content = $rev->getContent( Revision::RAW );
130 $this->output( "reverting\n" );
131 $page->doEditContent(
132 $content,
133 wfMessage( 'spam_reverting', $domain )->inContentLanguage()->text(),
134 EDIT_UPDATE,
135 $rev->getId()
137 } elseif ( $this->hasOption( 'delete' ) ) {
138 // Didn't find a non-spammy revision, blank the page
139 $this->output( "deleting\n" );
140 $page->doDeleteArticle(
141 wfMessage( 'spam_deleting', $domain )->inContentLanguage()->text()
143 } else {
144 // Didn't find a non-spammy revision, blank the page
145 $handler = ContentHandler::getForTitle( $title );
146 $content = $handler->makeEmptyContent();
148 $this->output( "blanking\n" );
149 $page->doEditContent(
150 $content,
151 wfMessage( 'spam_blanking', $domain )->inContentLanguage()->text()
154 $this->commitTransaction( $dbw, __METHOD__ );
159 $maintClass = "CleanupSpam";
160 require_once RUN_MAINTENANCE_IF_MAIN;