* (bug 5355) Include skin name and style JS settings in page source;
[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
57
58
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();
65 }
66
67 if ( !isset( $args[0] ) ) {
68 print "Usage: php cleanupSpam.php <hostname>\n";
69 exit(1);
70 }
71 $spec = $args[0];
72 $like = LinkFilter::makeLike( $spec );
73 if ( !$like ) {
74 print "Not a valid hostname specification: $spec\n";
75 exit(1);
76 }
77
78 $dbr =& wfGetDB( DB_SLAVE );
79
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: /" );
91 }
92 }
93 if ( $found ) {
94 print "All done\n";
95 } else {
96 print "None found\n";
97 }
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 );
106 }
107 if ( $count ) {
108 print "Done\n";
109 }
110 }
111
112 ?>