MediaWiki  REL1_22
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 $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                     passthru( "php cleanupSpam.php --wiki='$wikiID' $spec | sed 's/^/$wikiID:  /'" );
00071                 }
00072             }
00073             if ( $found ) {
00074                 $this->output( "All done\n" );
00075             } else {
00076                 $this->output( "None found\n" );
00077             }
00078         } else {
00079             // Clean up spam on this wiki
00080 
00081             $dbr = wfGetDB( DB_SLAVE );
00082             $res = $dbr->select( 'externallinks', array( 'DISTINCT el_from' ),
00083                 array( 'el_index' . $dbr->buildLike( $like ) ), __METHOD__ );
00084             $count = $dbr->numRows( $res );
00085             $this->output( "Found $count articles containing $spec\n" );
00086             foreach ( $res as $row ) {
00087                 $this->cleanupArticle( $row->el_from, $spec );
00088             }
00089             if ( $count ) {
00090                 $this->output( "Done\n" );
00091             }
00092         }
00093     }
00094 
00095     private function cleanupArticle( $id, $domain ) {
00096         $title = Title::newFromID( $id );
00097         if ( !$title ) {
00098             $this->error( "Internal error: no page for ID $id" );
00099             return;
00100         }
00101 
00102         $this->output( $title->getPrefixedDBkey() . " ..." );
00103         $rev = Revision::newFromTitle( $title );
00104         $currentRevId = $rev->getId();
00105 
00106         while ( $rev && ( $rev->isDeleted( Revision::DELETED_TEXT )
00107                         || LinkFilter::matchEntry( $rev->getContent( Revision::RAW ), $domain ) ) ) {
00108             $rev = $rev->getPrevious();
00109         }
00110 
00111         if ( $rev && $rev->getId() == $currentRevId ) {
00112             // The regex didn't match the current article text
00113             // This happens e.g. when a link comes from a template rather than the page itself
00114             $this->output( "False match\n" );
00115         } else {
00116             $dbw = wfGetDB( DB_MASTER );
00117             $dbw->begin( __METHOD__ );
00118             $page = WikiPage::factory( $title );
00119             if ( $rev ) {
00120                 // Revert to this revision
00121                 $content = $rev->getContent( Revision::RAW );
00122 
00123                 $this->output( "reverting\n" );
00124                 $page->doEditContent( $content, wfMessage( 'spam_reverting', $domain )->inContentLanguage()->text(),
00125                     EDIT_UPDATE, $rev->getId() );
00126             } elseif ( $this->hasOption( 'delete' ) ) {
00127                 // Didn't find a non-spammy revision, blank the page
00128                 $this->output( "deleting\n" );
00129                 $page->doDeleteArticle( wfMessage( 'spam_deleting', $domain )->inContentLanguage()->text() );
00130             } else {
00131                 // Didn't find a non-spammy revision, blank the page
00132                 $handler = ContentHandler::getForTitle( $title );
00133                 $content = $handler->makeEmptyContent();
00134 
00135                 $this->output( "blanking\n" );
00136                 $page->doEditContent( $content, wfMessage( 'spam_blanking', $domain )->inContentLanguage()->text() );
00137             }
00138             $dbw->commit( __METHOD__ );
00139         }
00140     }
00141 }
00142 
00143 $maintClass = "CleanupSpam";
00144 require_once RUN_MAINTENANCE_IF_MAIN;