MediaWiki  1.29.1
cleanup.php
Go to the documentation of this file.
1 <?php
2 
9 require_once( '../../maintenance/commandLine.inc' );
10 require_once( 'SpamBlacklist_body.php' );
11 
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 ) {
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 ) {
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, array( 'steal' => true ) );
65 } else {
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', array( 'user_password' => 'nologin' ),
73  array( '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 
95 $maxID = $dbr->selectField( 'page', 'MAX(page_id)' );
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 );
130 
$wgUser
$wgUser
Definition: Setup.php:781
$maxID
$maxID
Definition: cleanup.php:95
SpamBlacklist
Definition: SpamBlacklist_body.php:9
$regexes
if( $wgSpamBlacklistFiles) $regexes
Definition: cleanup.php:88
$status
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1049
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:36
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:556
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:34
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
cleanupArticle
cleanupArticle(Revision $rev, $regexes, $match)
An aggressive spam cleanup script.
Definition: cleanup.php:15
User\newSystemUser
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:684
Revision
Definition: Revision.php:33
Revision\newFromTitle
static newFromTitle(LinkTarget $linkTarget, $id=0, $flags=0)
Load either the current, or a specified, revision that's attached to a given link target.
Definition: Revision.php:134
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3060
$matches
$matches
Definition: NoLocalSettings.php:24
Revision\loadFromPageId
static loadFromPageId( $db, $pageid, $id=0)
Load either the current, or a specified, revision that's attached to a given page.
Definition: Revision.php:262
DB_MASTER
const DB_MASTER
Definition: defines.php:26
ContentHandler\makeContent
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
Definition: ContentHandler.php:129
ContentHandler\getContentText
static getContentText(Content $content=null)
Convenience function for getting flat text from a Content object.
Definition: ContentHandler.php:79
$dbr
if(! $regexes) $dbr
Definition: cleanup.php:94
$rev
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition: hooks.txt:1741
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$revId
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context $revId
Definition: hooks.txt:1049
$username
$username
Definition: cleanup.php:62
$reportingInterval
$reportingInterval
Definition: cleanup.php:96
$options
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition: hooks.txt:1049
array
the array() calling protocol came about after MediaWiki 1.4rc1.
$sb
$sb
Definition: cleanup.php:84