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