*/ $optionsWithArgs[] = 'delete'; require_once( 'commandLine.inc' ); /** * @addtogroup Maintenance */ function findEdits($id) { global $fname; $dbo =& wfGetDB( DB_SLAVE ); $ids = array(); $res = $dbo->select( "revision", array('rev_page'), "rev_user = '$id' GROUP BY rev_page", $fname ); while( $row = $dbo->fetchObject( $res ) ) { $ids[] = $row->rev_page; } $dbo->freeResult($res); return $ids; } function deleteUser ($olduserID) { $dbw =& wfGetDB( DB_SLAVE ); $dbw->delete( 'user_groups', array( 'ug_user' => $olduserID )); $dbw->delete( 'user', array( 'user_id' => $olduserID )); $users = $dbw->selectField( 'user', 'COUNT(*)', array() ); $admins = $dbw->selectField( 'user_groups', 'COUNT(*)', array( 'ug_group' => 'sysop' ) ); $dbw->update( 'site_stats', array( 'ss_users' => $users, 'ss_admins' => $admins ), array( 'ss_row_id' => 1 ) ); return true; } /** * Show help for the maintenance script */ function showHelp() { echo( "USAGE: php removeSpamAccountsAndPost.php [--delete] email\n" ); } if( isset($options['help'] ) ) { showHelp(); exit(); } # Do an initial scan for inactive accounts and report the result $del = array(); $dbr = wfGetDB( DB_SLAVE ); if(isset($options['delete'])) { $match = $options['delete']; } else { $match = $argv[0]; } if(!isset($match)) { showHelp(); exit; } $username = wfMsg( 'spambot_username' ); $fname = $username; $wgUser = User::newFromName( $username ); // Create the user if necessary if ( !$wgUser->getID() ) { $wgUser->addToDatabase(); } $res = $dbr->select( 'user', array( 'user_id', 'user_name', 'user_email' ), " user_email LIKE '$match' or user_name LIKE '$match'", $fname ); while( $row = $dbr->fetchObject( $res ) ) { # Check the account, but ignore it if it's the primary administrator if( $row->user_id > 1) { $user = User::newFromId($row->user_id); $user->load(); printf("%s,%s\n", $row->user_name, $row->user_email ); // find all edited articles. $edits = findEdits($row->user_id); foreach($edits as $rid) { // list all titles of edited articles. $title = Title::newFromID( $rid ); $article = new Article($title); $article->fetchContent(0); if($article->exists()) { if ($article->mRevision->mUser != $user->mId) { continue; } if($article->mRevision->getPrevious() == null) { // if the article is edited only once, delete the page. if(isset($options['delete'])) { printf(" Deleting article %s\n", $article->mTitle->mPrefixedText); $article->doDeleteArticle("Spam"); } else { printf(" Found %s\n", $article->mTitle->mPrefixedText); } } else { // if the article has old revision, revert it to last version. // check if he is the last editor if($article->mRevision->mUser == 0 || $article->mRevision->mUser == $user->mId) { while ($article->mRevision->getPrevious() != null) { $revision = $article->mRevision->getPrevious(); $article->mRevision = $revision; if($revision->mUser != $user->mId) { $u = User::newFromId($revision->mUser); if($u->loadFromId() == true) { // we found the old version. if(isset($options['delete'])) { $summary = wfMsgForContent( 'revertpage', $revision->getUserText(), $user->mName ); $flags = EDIT_UPDATE | EDIT_MINOR | EDIT_FORCE_BOT; $article->doEdit( $revision->getText(), $summary, $flags ); printf(" Rollback %s by '%s'\n", $article->mTitle->mPrefixedText, $revision->mUserText); } else { printf(" Found %s by '%s'\n", $article->mTitle->mPrefixedText, $revision->mUserText); } break; } } } if($article->mRevision->mUser == $user->mId) { if(isset($options['delete'])) { printf(" Deleting article %s\n", $article->mTitle->mPrefixedText); $article->doDeleteArticle("Spam cleanup"); } else { printf(" Found %s\n", $article->mTitle->mPrefixedText); } } } } } } if(isset($options['delete'])) { printf(" Deleting acccount %s\n", $user->mName); deleteUser($user->mId); } } }