Maintenance script to find and remove links to a given domain (cleanupSpam.php)
[lhc/web/wiklou.git] / maintenance / cleanupSpam.php
1 <?php
2
3 require_once( 'commandLine.inc' );
4 require_once( "$IP/includes/LinkFilter.php" );
5
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;
11 }
12
13 print $title->getPrefixedDBkey() . " ...";
14 $rev = Revision::newFromTitle( $title );
15 $reverted = false;
16 $revId = $rev->getId();
17 $currentRevId = $revId;
18 $regex = LinkFilter::makeRegex( $domain );
19
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;
28 }
29 }
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 );
43
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 );
49 }
50 $dbw->immediateCommit();
51 wfDoUpdates();
52 }
53 }
54 //------------------------------------------------------------------------------
55
56 $username = wfMsg( 'spambot_username' );
57 $fname = $username;
58 $wgUser = User::newFromName( $username );
59 // Create the user if necessary
60 if ( !$wgUser->getID() ) {
61 $wgUser->addToDatabase();
62 }
63
64 if ( !isset( $args[0] ) ) {
65 print "Usage: php cleanupSpam.php <hostname>\n";
66 exit(1);
67 }
68 $spec = $args[0];
69 $like = LinkFilter::makeLike( $spec );
70 if ( !$like ) {
71 print "Not a valid hostname specification: $spec\n";
72 exit(1);
73 }
74
75 $dbr =& wfGetDB( DB_SLAVE );
76
77 $res = $dbr->select( 'externallinks', array( 'el_from' ),
78 array( 'el_index LIKE ' . $dbr->addQuotes( $like ) ), $fname );
79 $count = $dbr->numRows( $res );
80 print "Found $count articles containing $spec\n";
81 while ( $row = $dbr->fetchObject( $res ) ) {
82 cleanupArticle( $row->el_from, $spec );
83 }
84 if ( $count ) {
85 print "Done\n";
86 }
87
88 ?>