Skip to content

Instantly share code, notes, and snippets.

@BjornW
Created October 23, 2012 20:02
Show Gist options
  • Save BjornW/3941183 to your computer and use it in GitHub Desktop.
Save BjornW/3941183 to your computer and use it in GitHub Desktop.
Remove spammers from Mediawiki in combination with PHPMyAdmin export as php array function. Build a query to determine the users to remove and export the user_ids.
<?php
/** Hack to remove spammers, based on removeUnusedAccounts.php by **/
/** BjornW <[email protected]> **/
/** Original header info follows **/
/**
* Remove unused user accounts from the database
* An unused account is one which has made no edits
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @ingroup Maintenance
* @author Rob Church <[email protected]>
*/
require_once( dirname( __FILE__ ) . '/Maintenance.php' );
class RemoveSpammers extends Maintenance {
public function __construct() {
parent::__construct();
$this->addOption( 'delete', 'Actually delete the spammers' );
}
public function execute() {
$del = $this->getSpammers();
$count = sizeof($del);
# If required, go back and delete each marked account
if ( $count > 0 && $this->hasOption( 'delete' ) ) {
$this->output( "\nDeleting inactive accounts..." );
$dbw = wfGetDB( DB_MASTER );
$dbw->delete( 'user', array( 'user_id' => $del ), __METHOD__ );
$this->output( "done.\n" );
# Update the site_stats.ss_users field
$users = $dbw->selectField( 'user', 'COUNT(*)', array(), __METHOD__ );
$dbw->update( 'site_stats', array( 'ss_users' => $users ), array( 'ss_row_id' => 1 ), __METHOD__ );
} elseif ( $count > 0 ) {
$user_ids_to_delete = implode("\n", $del);
$this->output( "\nUser Ids to be deleted:\n $user_ids_to_delete" );
$this->output( "\nRun the script again with --delete to remove them from the database.\n" );
}
$this->output( "\n" );
}
/*
* use require_once to include a phpmyadmin generated php array with the user_ids of the spammers
* the array should be called $user and look like:
* $user = array(
* array('user_id'=>12345),
* array('user_id'=>11238)
* );
* I've used the following sql to get spammers:
*
* SELECT * FROM `user` WHERE `user_email_authenticated`
* IS NULL AND user_registration > 20121008000000 AND user_editcount=0 AND user_email LIKE '%@hotmail.com%'
*/
private function getSpammers() {
require_once("spammers.php");
$user_ids = array();
if( isset($user) && is_array($user) ) {
foreach ($user as $user_arr) {
if( is_array($user_arr) && array_key_exists('user_id', $user_arr) ){
$user_ids[] = $user_arr['user_id'];
}
}
}
return $user_ids;
}
}
$maintClass = "RemoveSpammers";
require_once( RUN_MAINTENANCE_IF_MAIN );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment