MediaWiki  REL1_20
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", 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 ) || LinkFilter::matchEntry( $rev->getText() , $domain ) ) ) {
00107                         $rev = $rev->getPrevious();
00108                 }
00109 
00110                 if ( $rev && $rev->getId() == $currentRevId ) {
00111                         // The regex didn't match the current article text
00112                         // This happens e.g. when a link comes from a template rather than the page itself
00113                         $this->output( "False match\n" );
00114                 } else {
00115                         $dbw = wfGetDB( DB_MASTER );
00116                         $dbw->begin( __METHOD__ );
00117                         $page = WikiPage::factory( $title );
00118                         if ( $rev ) {
00119                                 // Revert to this revision
00120                                 $this->output( "reverting\n" );
00121                                 $page->doEdit( $rev->getText(), wfMessage( 'spam_reverting', $domain )->inContentLanguage()->text(),
00122                                         EDIT_UPDATE, $rev->getId() );
00123                         } elseif ( $this->hasOption( 'delete' ) ) {
00124                                 // Didn't find a non-spammy revision, blank the page
00125                                 $this->output( "deleting\n" );
00126                                 $page->doDeleteArticle( wfMessage( 'spam_deleting', $domain )->inContentLanguage()->text() );
00127                         } else {
00128                                 // Didn't find a non-spammy revision, blank the page
00129                                 $this->output( "blanking\n" );
00130                                 $page->doEdit( '', wfMessage( 'spam_blanking', $domain )->inContentLanguage()->text() );
00131                         }
00132                         $dbw->commit( __METHOD__ );
00133                 }
00134         }
00135 }
00136 
00137 $maintClass = "CleanupSpam";
00138 require_once( RUN_MAINTENANCE_IF_MAIN );