00001 <?php
00007 require_once( 'commandLine.inc' );
00008 require_once( "$IP/includes/LinkFilter.php" );
00009
00010 function cleanupArticle( $id, $domain ) {
00011 $title = Title::newFromID( $id );
00012 if ( !$title ) {
00013 print "Internal error: no page for ID $id\n";
00014 return;
00015 }
00016
00017 print $title->getPrefixedDBkey() . " ...";
00018 $rev = Revision::newFromTitle( $title );
00019 $revId = $rev->getId();
00020 $currentRevId = $revId;
00021
00022 while ( $rev && LinkFilter::matchEntry( $rev->getText() , $domain ) ) {
00023 # Revision::getPrevious can't be used in this way before MW 1.6 (Revision.php 1.26)
00024 #$rev = $rev->getPrevious();
00025 $revId = $title->getPreviousRevisionID( $revId );
00026 if ( $revId ) {
00027 $rev = Revision::newFromTitle( $title, $revId );
00028 } else {
00029 $rev = false;
00030 }
00031 }
00032 if ( $revId == $currentRevId ) {
00033
00034
00035 print "False match\n";
00036 } else {
00037 $dbw = wfGetDB( DB_MASTER );
00038 $dbw->immediateBegin();
00039 if ( !$rev ) {
00040
00041 print "blanking\n";
00042 $article = new Article( $title );
00043 $article->updateArticle( '', wfMsg( 'spam_blanking', $domain ),
00044 false, false );
00045
00046 } else {
00047
00048 print "reverting\n";
00049 $article = new Article( $title );
00050 $article->updateArticle( $rev->getText(), wfMsg( 'spam_reverting', $domain ), false, false );
00051 }
00052 $dbw->immediateCommit();
00053 wfDoUpdates();
00054 }
00055 }
00056
00057
00058
00059
00060
00061 $username = wfMsg( 'spambot_username' );
00062 $fname = $username;
00063 $wgUser = User::newFromName( $username );
00064
00065 if ( !$wgUser->getId() ) {
00066 $wgUser->addToDatabase();
00067 }
00068
00069 if ( !isset( $args[0] ) ) {
00070 print "Usage: php cleanupSpam.php <hostname>\n";
00071 exit(1);
00072 }
00073 $spec = $args[0];
00074 $like = LinkFilter::makeLike( $spec );
00075 if ( !$like ) {
00076 print "Not a valid hostname specification: $spec\n";
00077 exit(1);
00078 }
00079
00080 $dbr = wfGetDB( DB_SLAVE );
00081
00082 if ( isset($options['all']) ) {
00083
00084 $dbr = wfGetDB( DB_SLAVE );
00085 print "Finding spam on " . count($wgLocalDatabases) . " wikis\n";
00086 $found = false;
00087 foreach ( $wgLocalDatabases as $db ) {
00088 $count = $dbr->selectField( "`$db`.externallinks", 'COUNT(*)',
00089 array( 'el_index LIKE ' . $dbr->addQuotes( $like ) ), $fname );
00090 if ( $count ) {
00091 $found = true;
00092 passthru( "php cleanupSpam.php $db $spec | sed s/^/$db: /" );
00093 }
00094 }
00095 if ( $found ) {
00096 print "All done\n";
00097 } else {
00098 print "None found\n";
00099 }
00100 } else {
00101
00102 $res = $dbr->select( 'externallinks', array( 'DISTINCT el_from' ),
00103 array( 'el_index LIKE ' . $dbr->addQuotes( $like ) ), $fname );
00104 $count = $dbr->numRows( $res );
00105 print "Found $count articles containing $spec\n";
00106 while ( $row = $dbr->fetchObject( $res ) ) {
00107 cleanupArticle( $row->el_from, $spec );
00108 }
00109 if ( $count ) {
00110 print "Done\n";
00111 }
00112 }
00113
00114