partial bug 3456) Disable auto redirect to Main Page after account creation
[mediawiki.git] / maintenance / cleanupSpam.php
blob01b1f631cb8d3e71a9fb7a6d0253c6b2dc994603
1 <?php
3 require_once( 'commandLine.inc' );
4 require_once( "$IP/includes/LinkFilter.php" );
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;
13 print $title->getPrefixedDBkey() . " ...";
14 $rev = Revision::newFromTitle( $title );
15 $reverted = false;
16 $revId = $rev->getId();
17 $currentRevId = $revId;
18 $regex = LinkFilter::makeRegex( $domain );
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;
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 );
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 );
50 $dbw->immediateCommit();
51 wfDoUpdates();
54 //------------------------------------------------------------------------------
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();
64 if ( !isset( $args[0] ) ) {
65 print "Usage: php cleanupSpam.php <hostname>\n";
66 exit(1);
68 $spec = $args[0];
69 $like = LinkFilter::makeLike( $spec );
70 if ( !$like ) {
71 print "Not a valid hostname specification: $spec\n";
72 exit(1);
75 $dbr =& wfGetDB( DB_SLAVE );
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 );
84 if ( $count ) {
85 print "Done\n";