MediaWiki  1.33.0
cleanup.php
Go to the documentation of this file.
1 <?php
9 $IP = getenv( 'MW_INSTALL_PATH' );
10 if ( $IP === false ) {
11  $IP = __DIR__ . '/../../..';
12 }
13 require_once "$IP/maintenance/Maintenance.php";
14 
15 class Cleanup extends Maintenance {
16  public function __construct() {
17  parent::__construct();
18  $this->requireExtension( 'SpamBlacklist' );
19  $this->addOption( 'dry-run', 'Only do a dry run' );
20  }
21 
22  public function execute() {
23  $user = User::newSystemUser( 'Spam cleanup script', [ 'steal' => true ] );
24 
26  $regexes = $sb->getBlacklists();
27  if ( !$regexes ) {
28  $this->fatalError( "Invalid regex, can't clean up spam" );
29  }
30  $dryRun = $this->hasOption( 'dry-run' );
31 
32  $dbr = wfGetDB( DB_REPLICA );
33  $maxID = (int)$dbr->selectField( 'page', 'MAX(page_id)' );
34  $reportingInterval = 100;
35 
36  $this->output( "Regexes are " . implode( ', ', array_map( 'count', $regexes ) ) . " bytes\n" );
37  $this->output( "Searching for spam in $maxID pages...\n" );
38  if ( $dryRun ) {
39  $this->output( "Dry run only\n" );
40  }
41 
42  for ( $id = 1; $id <= $maxID; $id++ ) {
43  if ( $id % $reportingInterval == 0 ) {
44  printf( "%-8d %-5.2f%%\r", $id, $id / $maxID * 100 );
45  }
46  $revision = Revision::loadFromPageId( $dbr, $id );
47  if ( $revision ) {
48  $text = ContentHandler::getContentText( $revision->getContent() );
49  if ( $text ) {
50  foreach ( $regexes as $regex ) {
51  if ( preg_match( $regex, $text, $matches ) ) {
52  $title = $revision->getTitle();
53  $titleText = $title->getPrefixedText();
54  if ( $dryRun ) {
55  $this->output( "Found spam in [[$titleText]]\n" );
56  } else {
57  $this->output( "Cleaning up links to {$matches[0]} in [[$titleText]]\n" );
58  $match = str_replace( 'http://', '', $matches[0] );
59  $this->cleanupArticle( $revision, $regexes, $match, $user );
60  }
61  }
62  }
63  }
64  }
65  }
66  // Just for satisfaction
67  printf( "%-8d %-5.2f%%\n", $id - 1, ( $id - 1 ) / $maxID * 100 );
68  }
69 
77  private function cleanupArticle( Revision $rev, $regexes, $match, User $user ) {
78  $title = $rev->getTitle();
79  while ( $rev ) {
80  $matches = false;
81  foreach ( $regexes as $regex ) {
83  || preg_match(
84  $regex,
85  ContentHandler::getContentText( $rev->getContent() )
86  );
87  }
88  if ( !$matches ) {
89  // Didn't find any spam
90  break;
91  }
92 
93  $rev = $rev->getPrevious();
94  }
95  if ( !$rev ) {
96  // Didn't find a non-spammy revision, blank the page
97  $this->output( "All revisions are spam, blanking...\n" );
98  $text = '';
99  $comment = "All revisions matched the spam blacklist ($match), blanking";
100  } else {
101  // Revert to this revision
102  $text = ContentHandler::getContentText( $rev->getContent() );
103  $comment = "Cleaning up links to $match";
104  }
105  $wikiPage = new WikiPage( $title );
106  $wikiPage->doEditContent(
107  ContentHandler::makeContent( $text, $title ), $comment,
108  0, false, $user
109  );
110  }
111 }
112 
114 require_once RUN_MAINTENANCE_IF_MAIN;
BaseBlacklist\getSpamBlacklist
static getSpamBlacklist()
Definition: BaseBlacklist.php:96
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:485
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:45
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
Cleanup
Definition: cleanup.php:15
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
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
$dbr
$dbr
Definition: testCompression.php:50
User\newSystemUser
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:813
Revision
Definition: Revision.php:40
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2636
$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:260
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:248
Maintenance\requireExtension
requireExtension( $name)
Indicate that the specified extension must be loaded before the script can run.
Definition: Maintenance.php:619
Cleanup\cleanupArticle
cleanupArticle(Revision $rev, $regexes, $match, User $user)
Find the latest revision of the article that does not contain spam and revert to it.
Definition: cleanup.php:77
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
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:133
$IP
$IP
An aggressive spam cleanup script.
Definition: cleanup.php:9
Cleanup\execute
execute()
Do the actual work.
Definition: cleanup.php:22
ContentHandler\getContentText
static getContentText(Content $content=null)
Convenience function for getting flat text from a Content object.
Definition: ContentHandler.php:83
$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:1769
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
$maintClass
$maintClass
Definition: cleanup.php:113
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
Cleanup\__construct
__construct()
Default constructor.
Definition: cleanup.php:16
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:269
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:48