7 require_once( 'commandLine.inc' );
8 require_once( "$IP/includes/LinkFilter.php" );
10 function cleanupArticle( $id, $domain ) {
11 $title = Title
::newFromID( $id );
13 print "Internal error: no page for ID $id\n";
17 print $title->getPrefixedDBkey() . " ...";
18 $rev = Revision
::newFromTitle( $title );
19 $revId = $rev->getId();
20 $currentRevId = $revId;
22 while ( $rev && LinkFilter
::matchEntry( $rev->getText() , $domain ) ) {
23 # Revision::getPrevious can't be used in this way before MW 1.6 (Revision.php 1.26)
24 #$rev = $rev->getPrevious();
25 $revId = $title->getPreviousRevisionID( $revId );
27 $rev = Revision
::newFromTitle( $title, $revId );
32 if ( $revId == $currentRevId ) {
33 // The regex didn't match the current article text
34 // This happens e.g. when a link comes from a template rather than the page itself
35 print "False match\n";
37 $dbw = wfGetDB( DB_MASTER
);
38 $dbw->immediateBegin();
40 // Didn't find a non-spammy revision, blank the page
42 $article = new Article( $title );
43 $article->updateArticle( '', wfMsg( 'spam_blanking', $domain ),
47 // Revert to this revision
49 $article = new Article( $title );
50 $article->updateArticle( $rev->getText(), wfMsg( 'spam_reverting', $domain ), false, false );
52 $dbw->immediateCommit();
56 //------------------------------------------------------------------------------
61 $username = wfMsg( 'spambot_username' );
63 $wgUser = User
::newFromName( $username );
64 // Create the user if necessary
65 if ( !$wgUser->getId() ) {
66 $wgUser->addToDatabase();
69 if ( !isset( $args[0] ) ) {
70 print "Usage: php cleanupSpam.php <hostname>\n";
74 $like = LinkFilter
::makeLike( $spec );
76 print "Not a valid hostname specification: $spec\n";
80 $dbr = wfGetDB( DB_SLAVE
);
82 if ( isset($options['all']) ) {
83 // Clean up spam on all wikis
84 $dbr = wfGetDB( DB_SLAVE
);
85 print "Finding spam on " . count($wgLocalDatabases) . " wikis\n";
87 foreach ( $wgLocalDatabases as $db ) {
88 $count = $dbr->selectField( "`$db`.externallinks", 'COUNT(*)',
89 array( 'el_index LIKE ' . $dbr->addQuotes( $like ) ), $fname );
92 passthru( "php cleanupSpam.php $db $spec | sed s/^/$db: /" );
101 // Clean up spam on this wiki
102 $res = $dbr->select( 'externallinks', array( 'DISTINCT el_from' ),
103 array( 'el_index LIKE ' . $dbr->addQuotes( $like ) ), $fname );
104 $count = $dbr->numRows( $res );
105 print "Found $count articles containing $spec\n";
106 while ( $row = $dbr->fetchObject( $res ) ) {
107 cleanupArticle( $row->el_from
, $spec );