* (bug 7681, 11559) Cookie values no longer override GET and POST variables.
[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 $revId = $rev->getId();
16 $currentRevId = $revId;
17 $regex = LinkFilter::makeRegex( $domain );
18
19 while ( $rev && preg_match( $regex, $rev->getText() ) ) {
20 # Revision::getPrevious can't be used in this way before MW 1.6 (Revision.php 1.26)
21 #$rev = $rev->getPrevious();
22 $revId = $title->getPreviousRevisionID( $revId );
23 if ( $revId ) {
24 $rev = Revision::newFromTitle( $title, $revId );
25 } else {
26 $rev = false;
27 }
28 }
29 if ( $revId == $currentRevId ) {
30 // The regex didn't match the current article text
31 // This happens e.g. when a link comes from a template rather than the page itself
32 print "False match\n";
33 } else {
34 $dbw = wfGetDB( DB_MASTER );
35 $dbw->immediateBegin();
36 if ( !$rev ) {
37 // Didn't find a non-spammy revision, blank the page
38 print "blanking\n";
39 $article = new Article( $title );
40 $article->updateArticle( '', wfMsg( 'spam_blanking', $domain ),
41 false, false );
42
43 } else {
44 // Revert to this revision
45 print "reverting\n";
46 $article = new Article( $title );
47 $article->updateArticle( $rev->getText(), wfMsg( 'spam_reverting', $domain ), false, false );
48 }
49 $dbw->immediateCommit();
50 wfDoUpdates();
51 }
52 }
53 //------------------------------------------------------------------------------
54
55
56
57
58 $username = wfMsg( 'spambot_username' );
59 $fname = $username;
60 $wgUser = User::newFromName( $username );
61 // Create the user if necessary
62 if ( !$wgUser->getID() ) {
63 $wgUser->addToDatabase();
64 }
65
66 if ( !isset( $args[0] ) ) {
67 print "Usage: php cleanupSpam.php <hostname>\n";
68 exit(1);
69 }
70 $spec = $args[0];
71 $like = LinkFilter::makeLike( $spec );
72 if ( !$like ) {
73 print "Not a valid hostname specification: $spec\n";
74 exit(1);
75 }
76
77 $dbr = wfGetDB( DB_SLAVE );
78
79 if ( isset($options['all']) ) {
80 // Clean up spam on all wikis
81 $dbr = wfGetDB( DB_SLAVE );
82 print "Finding spam on " . count($wgLocalDatabases) . " wikis\n";
83 $found = false;
84 foreach ( $wgLocalDatabases as $db ) {
85 $count = $dbr->selectField( "`$db`.externallinks", 'COUNT(*)',
86 array( 'el_index LIKE ' . $dbr->addQuotes( $like ) ), $fname );
87 if ( $count ) {
88 $found = true;
89 passthru( "php cleanupSpam.php $db $spec | sed s/^/$db: /" );
90 }
91 }
92 if ( $found ) {
93 print "All done\n";
94 } else {
95 print "None found\n";
96 }
97 } else {
98 // Clean up spam on this wiki
99 $res = $dbr->select( 'externallinks', array( 'DISTINCT el_from' ),
100 array( 'el_index LIKE ' . $dbr->addQuotes( $like ) ), $fname );
101 $count = $dbr->numRows( $res );
102 print "Found $count articles containing $spec\n";
103 while ( $row = $dbr->fetchObject( $res ) ) {
104 cleanupArticle( $row->el_from, $spec );
105 }
106 if ( $count ) {
107 print "Done\n";
108 }
109 }
110
111