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