MediaWiki  REL1_23
cleanupSpam.php
Go to the documentation of this file.
00001 <?php
00024 require_once __DIR__ . '/Maintenance.php';
00025 
00031 class CleanupSpam extends Maintenance {
00032 
00033     public function __construct() {
00034         parent::__construct();
00035         $this->mDescription = "Cleanup all spam from a given hostname";
00036         $this->addOption( 'all', 'Check all wikis in $wgLocalDatabases' );
00037         $this->addOption( 'delete', 'Delete pages containing only spam instead of blanking them' );
00038         $this->addArg( 'hostname', 'Hostname that was spamming, single * wildcard in the beginning allowed' );
00039     }
00040 
00041     public function execute() {
00042         global $IP, $wgLocalDatabases, $wgUser;
00043 
00044         $username = wfMessage( 'spambot_username' )->text();
00045         $wgUser = User::newFromName( $username );
00046         if ( !$wgUser ) {
00047             $this->error( "Invalid username specified in 'spambot_username' message: $username", true );
00048         }
00049         // Create the user if necessary
00050         if ( !$wgUser->getId() ) {
00051             $wgUser->addToDatabase();
00052         }
00053         $spec = $this->getArg();
00054         $like = LinkFilter::makeLikeArray( $spec );
00055         if ( !$like ) {
00056             $this->error( "Not a valid hostname specification: $spec", true );
00057         }
00058 
00059         if ( $this->hasOption( 'all' ) ) {
00060             // Clean up spam on all wikis
00061             $this->output( "Finding spam on " . count( $wgLocalDatabases ) . " wikis\n" );
00062             $found = false;
00063             foreach ( $wgLocalDatabases as $wikiID ) {
00064                 $dbr = wfGetDB( DB_SLAVE, array(), $wikiID );
00065 
00066                 $count = $dbr->selectField( 'externallinks', 'COUNT(*)',
00067                     array( 'el_index' . $dbr->buildLike( $like ) ), __METHOD__ );
00068                 if ( $count ) {
00069                     $found = true;
00070                     $cmd = wfShellWikiCmd( "$IP/maintenance/cleanupSpam.php",
00071                         array( '--wiki', $wikiID, $spec ) );
00072                     passthru( "$cmd | sed 's/^/$wikiID:  /'" );
00073                 }
00074             }
00075             if ( $found ) {
00076                 $this->output( "All done\n" );
00077             } else {
00078                 $this->output( "None found\n" );
00079             }
00080         } else {
00081             // Clean up spam on this wiki
00082 
00083             $dbr = wfGetDB( DB_SLAVE );
00084             $res = $dbr->select( 'externallinks', array( 'DISTINCT el_from' ),
00085                 array( 'el_index' . $dbr->buildLike( $like ) ), __METHOD__ );
00086             $count = $dbr->numRows( $res );
00087             $this->output( "Found $count articles containing $spec\n" );
00088             foreach ( $res as $row ) {
00089                 $this->cleanupArticle( $row->el_from, $spec );
00090             }
00091             if ( $count ) {
00092                 $this->output( "Done\n" );
00093             }
00094         }
00095     }
00096 
00097     private function cleanupArticle( $id, $domain ) {
00098         $title = Title::newFromID( $id );
00099         if ( !$title ) {
00100             $this->error( "Internal error: no page for ID $id" );
00101             return;
00102         }
00103 
00104         $this->output( $title->getPrefixedDBkey() . " ..." );
00105         $rev = Revision::newFromTitle( $title );
00106         $currentRevId = $rev->getId();
00107 
00108         while ( $rev && ( $rev->isDeleted( Revision::DELETED_TEXT )
00109                         || LinkFilter::matchEntry( $rev->getContent( Revision::RAW ), $domain ) ) ) {
00110             $rev = $rev->getPrevious();
00111         }
00112 
00113         if ( $rev && $rev->getId() == $currentRevId ) {
00114             // The regex didn't match the current article text
00115             // This happens e.g. when a link comes from a template rather than the page itself
00116             $this->output( "False match\n" );
00117         } else {
00118             $dbw = wfGetDB( DB_MASTER );
00119             $dbw->begin( __METHOD__ );
00120             $page = WikiPage::factory( $title );
00121             if ( $rev ) {
00122                 // Revert to this revision
00123                 $content = $rev->getContent( Revision::RAW );
00124 
00125                 $this->output( "reverting\n" );
00126                 $page->doEditContent( $content, wfMessage( 'spam_reverting', $domain )->inContentLanguage()->text(),
00127                     EDIT_UPDATE, $rev->getId() );
00128             } elseif ( $this->hasOption( 'delete' ) ) {
00129                 // Didn't find a non-spammy revision, blank the page
00130                 $this->output( "deleting\n" );
00131                 $page->doDeleteArticle( wfMessage( 'spam_deleting', $domain )->inContentLanguage()->text() );
00132             } else {
00133                 // Didn't find a non-spammy revision, blank the page
00134                 $handler = ContentHandler::getForTitle( $title );
00135                 $content = $handler->makeEmptyContent();
00136 
00137                 $this->output( "blanking\n" );
00138                 $page->doEditContent( $content, wfMessage( 'spam_blanking', $domain )->inContentLanguage()->text() );
00139             }
00140             $dbw->commit( __METHOD__ );
00141         }
00142     }
00143 }
00144 
00145 $maintClass = "CleanupSpam";
00146 require_once RUN_MAINTENANCE_IF_MAIN;