]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - extensions/SpamBlacklist/maintenance/cleanup.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / extensions / SpamBlacklist / maintenance / cleanup.php
1 <?php
2
3 /**
4  * An aggressive spam cleanup script.
5  * Searches the database for matching pages, and reverts them to the last non-spammed revision.
6  * If all revisions contain spam, deletes the page
7  */
8
9 require_once '../../maintenance/commandLine.inc';
10 require_once 'SpamBlacklist_body.php';
11
12 /**
13  * Find the latest revision of the article that does not contain spam and revert to it
14  */
15 function cleanupArticle( Revision $rev, $regexes, $match ) {
16         $title = $rev->getTitle();
17         $revId = $rev->getId();
18         while ( $rev ) {
19                 $matches = false;
20                 foreach ( $regexes as $regex ) {
21                         $matches = $matches
22                                 || preg_match(
23                                         $regex,
24                                         ContentHandler::getContentText( $rev->getContent() )
25                                 );
26                 }
27                 if ( !$matches ) {
28                         // Didn't find any spam
29                         break;
30                 }
31                 # Revision::getPrevious can't be used in this way before MW 1.6 (Revision.php 1.26)
32                 # $rev = $rev->getPrevious();
33                 $revId = $title->getPreviousRevisionID( $revId );
34                 if ( $revId ) {
35                         $rev = Revision::newFromTitle( $title, $revId );
36                 } else {
37                         $rev = false;
38                 }
39         }
40         if ( !$rev ) {
41                 // Didn't find a non-spammy revision, delete the page
42                 /*
43                 print "All revisions are spam, deleting...\n";
44                 $article = new Article( $title );
45                 $article->doDeleteArticle( "All revisions matched the spam blacklist" );
46                 */
47                 // Too scary, blank instead
48                 print "All revisions are spam, blanking...\n";
49                 $text = '';
50                 $comment = "All revisions matched the spam blacklist ($match), blanking";
51         } else {
52                 // Revert to this revision
53                 $text = ContentHandler::getContentText( $rev->getContent() );
54                 $comment = "Cleaning up links to $match";
55         }
56         $wikiPage = new WikiPage( $title );
57         $wikiPage->doEditContent( ContentHandler::makeContent( $text, $title ), $comment );
58 }
59
60 // ------------------------------------------------------------------------------
61
62 $username = 'Spam cleanup script';
63 if ( method_exists( 'User', 'newSystemUser' ) ) {
64         $wgUser = User::newSystemUser( $username, [ 'steal' => true ] );
65 } else {
66         $wgUser = User::newFromName( $username );
67         if ( $wgUser->idForName() == 0 ) {
68                 // Create the user
69                 $status = $wgUser->addToDatabase();
70                 if ( $status === null || $status->isOK() ) {
71                         $dbw = wfGetDB( DB_MASTER );
72                         $dbw->update( 'user', [ 'user_password' => 'nologin' ],
73                                 [ 'user_name' => $username ], $username );
74                 }
75         }
76 }
77
78 if ( isset( $options['n'] ) ) {
79         $dryRun = true;
80 } else {
81         $dryRun = false;
82 }
83
84 $sb = new SpamBlacklist( $wgSpamBlacklistSettings );
85 if ( $wgSpamBlacklistFiles ) {
86         $sb->files = $wgSpamBlacklistFiles;
87 }
88 $regexes = $sb->getBlacklists();
89 if ( !$regexes ) {
90         print "Invalid regex, can't clean up spam\n";
91         exit( 1 );
92 }
93
94 $dbr = wfGetDB( DB_SLAVE );
95 $maxID = $dbr->selectField( 'page', 'MAX(page_id)' );
96 $reportingInterval = 100;
97
98 print "Regexes are " . implode( ', ', array_map( 'count', $regexes ) ) . " bytes\n";
99 print "Searching for spam in $maxID pages...\n";
100 if ( $dryRun ) {
101         print "Dry run only\n";
102 }
103
104 for ( $id = 1; $id <= $maxID; $id++ ) {
105         if ( $id % $reportingInterval == 0 ) {
106                 printf( "%-8d  %-5.2f%%\r", $id, $id / $maxID * 100 );
107         }
108         $revision = Revision::loadFromPageId( $dbr, $id );
109         if ( $revision ) {
110                 $text = ContentHandler::getContentText( $revision->getContent() );
111                 if ( $text ) {
112                         foreach ( $regexes as $regex ) {
113                                 if ( preg_match( $regex, $text, $matches ) ) {
114                                         $title = $revision->getTitle();
115                                         $titleText = $title->getPrefixedText();
116                                         if ( $dryRun ) {
117                                                 print "\nFound spam in [[$titleText]]\n";
118                                         } else {
119                                                 print "\nCleaning up links to {$matches[0]} in [[$titleText]]\n";
120                                                 $match = str_replace( 'http://', '', $matches[0] );
121                                                 cleanupArticle( $revision, $regexes, $match );
122                                         }
123                                 }
124                         }
125                 }
126         }
127 }
128 // Just for satisfaction
129 printf( "%-8d  %-5.2f%%\n", $id - 1, ( $id - 1 ) / $maxID * 100 );