MediaWiki  1.23.2
cleanupSpam.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
31 class CleanupSpam extends Maintenance {
32 
33  public function __construct() {
34  parent::__construct();
35  $this->mDescription = "Cleanup all spam from a given hostname";
36  $this->addOption( 'all', 'Check all wikis in $wgLocalDatabases' );
37  $this->addOption( 'delete', 'Delete pages containing only spam instead of blanking them' );
38  $this->addArg( 'hostname', 'Hostname that was spamming, single * wildcard in the beginning allowed' );
39  }
40 
41  public function execute() {
42  global $IP, $wgLocalDatabases, $wgUser;
43 
44  $username = wfMessage( 'spambot_username' )->text();
45  $wgUser = User::newFromName( $username );
46  if ( !$wgUser ) {
47  $this->error( "Invalid username specified in 'spambot_username' message: $username", true );
48  }
49  // Create the user if necessary
50  if ( !$wgUser->getId() ) {
51  $wgUser->addToDatabase();
52  }
53  $spec = $this->getArg();
54  $like = LinkFilter::makeLikeArray( $spec );
55  if ( !$like ) {
56  $this->error( "Not a valid hostname specification: $spec", true );
57  }
58 
59  if ( $this->hasOption( 'all' ) ) {
60  // Clean up spam on all wikis
61  $this->output( "Finding spam on " . count( $wgLocalDatabases ) . " wikis\n" );
62  $found = false;
63  foreach ( $wgLocalDatabases as $wikiID ) {
64  $dbr = wfGetDB( DB_SLAVE, array(), $wikiID );
65 
66  $count = $dbr->selectField( 'externallinks', 'COUNT(*)',
67  array( 'el_index' . $dbr->buildLike( $like ) ), __METHOD__ );
68  if ( $count ) {
69  $found = true;
70  $cmd = wfShellWikiCmd( "$IP/maintenance/cleanupSpam.php",
71  array( '--wiki', $wikiID, $spec ) );
72  passthru( "$cmd | sed 's/^/$wikiID: /'" );
73  }
74  }
75  if ( $found ) {
76  $this->output( "All done\n" );
77  } else {
78  $this->output( "None found\n" );
79  }
80  } else {
81  // Clean up spam on this wiki
82 
83  $dbr = wfGetDB( DB_SLAVE );
84  $res = $dbr->select( 'externallinks', array( 'DISTINCT el_from' ),
85  array( 'el_index' . $dbr->buildLike( $like ) ), __METHOD__ );
86  $count = $dbr->numRows( $res );
87  $this->output( "Found $count articles containing $spec\n" );
88  foreach ( $res as $row ) {
89  $this->cleanupArticle( $row->el_from, $spec );
90  }
91  if ( $count ) {
92  $this->output( "Done\n" );
93  }
94  }
95  }
96 
97  private function cleanupArticle( $id, $domain ) {
98  $title = Title::newFromID( $id );
99  if ( !$title ) {
100  $this->error( "Internal error: no page for ID $id" );
101  return;
102  }
103 
104  $this->output( $title->getPrefixedDBkey() . " ..." );
106  $currentRevId = $rev->getId();
107 
108  while ( $rev && ( $rev->isDeleted( Revision::DELETED_TEXT )
109  || LinkFilter::matchEntry( $rev->getContent( Revision::RAW ), $domain ) ) ) {
110  $rev = $rev->getPrevious();
111  }
112 
113  if ( $rev && $rev->getId() == $currentRevId ) {
114  // The regex didn't match the current article text
115  // This happens e.g. when a link comes from a template rather than the page itself
116  $this->output( "False match\n" );
117  } else {
118  $dbw = wfGetDB( DB_MASTER );
119  $dbw->begin( __METHOD__ );
120  $page = WikiPage::factory( $title );
121  if ( $rev ) {
122  // Revert to this revision
123  $content = $rev->getContent( Revision::RAW );
124 
125  $this->output( "reverting\n" );
126  $page->doEditContent( $content, wfMessage( 'spam_reverting', $domain )->inContentLanguage()->text(),
127  EDIT_UPDATE, $rev->getId() );
128  } elseif ( $this->hasOption( 'delete' ) ) {
129  // Didn't find a non-spammy revision, blank the page
130  $this->output( "deleting\n" );
131  $page->doDeleteArticle( wfMessage( 'spam_deleting', $domain )->inContentLanguage()->text() );
132  } else {
133  // Didn't find a non-spammy revision, blank the page
134  $handler = ContentHandler::getForTitle( $title );
135  $content = $handler->makeEmptyContent();
136 
137  $this->output( "blanking\n" );
138  $page->doEditContent( $content, wfMessage( 'spam_blanking', $domain )->inContentLanguage()->text() );
139  }
140  $dbw->commit( __METHOD__ );
141  }
142  }
143 }
144 
145 $maintClass = "CleanupSpam";
146 require_once RUN_MAINTENANCE_IF_MAIN;
$wgUser
$wgUser
Definition: Setup.php:552
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
CleanupSpam
Maintenance script to cleanup all spam from a given hostname.
Definition: cleanupSpam.php:31
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3650
text
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition: design.txt:12
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false)
Add a parameter to the script.
Definition: Maintenance.php:169
LinkFilter\matchEntry
static matchEntry(Content $content, $filterEntry)
Check whether $content contains a link to $filterEntry.
Definition: LinkFilter.php:42
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:388
ContentHandler\getForTitle
static getForTitle(Title $title)
Returns the appropriate ContentHandler singleton for the given title.
Definition: ContentHandler.php:259
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
$maintClass
$maintClass
Definition: cleanupSpam.php:145
CleanupSpam\cleanupArticle
cleanupArticle( $id, $domain)
Definition: cleanupSpam.php:97
$dbr
$dbr
Definition: testCompression.php:48
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:103
wfMessage
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing after in associative array form externallinks including delete and has completed for all link tables default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
LinkFilter\makeLikeArray
static makeLikeArray( $filterEntry, $protocol='http://')
Make an array to be used for calls to DatabaseBase::buildLike(), which will match the specified strin...
Definition: LinkFilter.php:94
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
wfShellWikiCmd
wfShellWikiCmd( $script, array $parameters=array(), array $options=array())
Generate a shell-escaped command line string to run a MediaWiki cli script.
Definition: GlobalFunctions.php:3062
Revision\newFromTitle
static newFromTitle( $title, $id=0, $flags=0)
Load either the current, or a specified, revision that's attached to a given title.
Definition: Revision.php:106
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
EDIT_UPDATE
const EDIT_UPDATE
Definition: Defines.php:190
Revision\RAW
const RAW
Definition: Revision.php:74
CleanupSpam\__construct
__construct()
Default constructor.
Definition: cleanupSpam.php:33
$count
$count
Definition: UtfNormalTest2.php:96
$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:1337
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
Maintenance\addArg
addArg( $arg, $description, $required=true)
Add some args that are needed.
Definition: Maintenance.php:207
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
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:333
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:181
CleanupSpam\execute
execute()
Do the actual work.
Definition: cleanupSpam.php:41
Maintenance\getArg
getArg( $argId=0, $default=null)
Get an argument.
Definition: Maintenance.php:246
$IP
$IP
Definition: WebStart.php:88
Title\newFromID
static newFromID( $id, $flags=0)
Create a new Title from an article ID.
Definition: Title.php:297
$res
$res
Definition: database.txt:21
Revision\DELETED_TEXT
const DELETED_TEXT
Definition: Revision.php:65