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