* Use double quoted consistently on attributes in linker output; preparing for new...
[mediawiki.git] / maintenance / cleanupSpam.php
blob65d6bc4d34bef472542dfc72e24e44ba63ed7607
1 <?php
3 require_once( 'commandLine.inc' );
4 require_once( "$IP/includes/LinkFilter.php" );
6 function cleanupArticle( $id, $domain ) {
7 $title = Title::newFromID( $id );
8 if ( !$title ) {
9 print "Internal error: no page for ID $id\n";
10 return;
13 print $title->getPrefixedDBkey() . " ...";
14 $rev = Revision::newFromTitle( $title );
15 $reverted = false;
16 $revId = $rev->getId();
17 $currentRevId = $revId;
18 $regex = LinkFilter::makeRegex( $domain );
20 while ( $rev && preg_match( $regex, $rev->getText() ) ) {
21 # Revision::getPrevious can't be used in this way before MW 1.6 (Revision.php 1.26)
22 #$rev = $rev->getPrevious();
23 $revId = $title->getPreviousRevisionID( $revId );
24 if ( $revId ) {
25 $rev = Revision::newFromTitle( $title, $revId );
26 } else {
27 $rev = false;
30 if ( $revId == $currentRevId ) {
31 // The regex didn't match the current article text
32 // This happens e.g. when a link comes from a template rather than the page itself
33 print "False match\n";
34 } else {
35 $dbw =& wfGetDB( DB_MASTER );
36 $dbw->immediateBegin();
37 if ( !$rev ) {
38 // Didn't find a non-spammy revision, blank the page
39 print "blanking\n";
40 $article = new Article( $title );
41 $article->updateArticle( '', wfMsg( 'spam_blanking', $domain ),
42 false, false );
44 } else {
45 // Revert to this revision
46 print "reverting\n";
47 $article = new Article( $title );
48 $article->updateArticle( $rev->getText(), wfMsg( 'spam_reverting', $domain ), false, false );
50 $dbw->immediateCommit();
51 wfDoUpdates();
54 //------------------------------------------------------------------------------
59 $username = wfMsg( 'spambot_username' );
60 $fname = $username;
61 $wgUser = User::newFromName( $username );
62 // Create the user if necessary
63 if ( !$wgUser->getID() ) {
64 $wgUser->addToDatabase();
67 if ( !isset( $args[0] ) ) {
68 print "Usage: php cleanupSpam.php <hostname>\n";
69 exit(1);
71 $spec = $args[0];
72 $like = LinkFilter::makeLike( $spec );
73 if ( !$like ) {
74 print "Not a valid hostname specification: $spec\n";
75 exit(1);
78 $dbr =& wfGetDB( DB_SLAVE );
80 if ( $options['all'] ) {
81 // Clean up spam on all wikis
82 $dbr =& wfGetDB( DB_SLAVE );
83 print "Finding spam on " . count($wgLocalDatabases) . " wikis\n";
84 $found = false;
85 foreach ( $wgLocalDatabases as $db ) {
86 $count = $dbr->selectField( "`$db`.externallinks", 'COUNT(*)',
87 array( 'el_index LIKE ' . $dbr->addQuotes( $like ) ), $fname );
88 if ( $count ) {
89 $found = true;
90 passthru( "php cleanupSpam.php $db $spec | sed s/^/$db: /" );
93 if ( $found ) {
94 print "All done\n";
95 } else {
96 print "None found\n";
98 } else {
99 // Clean up spam on this wiki
100 $res = $dbr->select( 'externallinks', array( 'DISTINCT el_from' ),
101 array( 'el_index LIKE ' . $dbr->addQuotes( $like ) ), $fname );
102 $count = $dbr->numRows( $res );
103 print "Found $count articles containing $spec\n";
104 while ( $row = $dbr->fetchObject( $res ) ) {
105 cleanupArticle( $row->el_from, $spec );
107 if ( $count ) {
108 print "Done\n";