MarkAHershberger has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/120476

Change subject: * Change display order to most recent editors/spammers * Remove 
deprecation * Make $um a more readable $userManager
......................................................................

* Change display order to most recent editors/spammers
* Remove deprecation
* Make $um a more readable $userManager

Change-Id: I991eb98b5459ce46f50a86e584321a91a859ea4f
---
M BanPests.php
M BlockAndNuke.body.php
M BlockAndNuke.i18n.php
M README.md
4 files changed, 162 insertions(+), 94 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/BlockAndNuke 
refs/changes/76/120476/1

diff --git a/BanPests.php b/BanPests.php
index 23a46a1..d897a13 100644
--- a/BanPests.php
+++ b/BanPests.php
@@ -9,7 +9,8 @@
                if ( isset( $wgWhitelist ) && file_exists( $wgWhitelist ) ) {
                        $wgBaNwhitelist = $wgWhitelist;
                } elseif ( !isset( $wgBaNwhitelist ) || !file_exists( 
$wgBaNwhitelist ) ) {
-                       throw new MWException( 'You need to specify a 
whitelist!  $wgBaNwhitelist should point to a filename that contains the 
whitelist.' );
+                       throw new MWException( 'You need to specify a 
whitelist! ".
+                               "$wgBaNwhitelist should point to a filename 
that contains the whitelist.' );
                }
 
                $fh = fopen($wgBaNwhitelist, 'r');
@@ -26,7 +27,8 @@
                                'rc_log_type' => "upload",
                                'rc_log_action' => "upload"
                        ), LIST_AND );
-               $cond[] = $dbr->makeList( /* New Users older than a day who 
haven't done anything yet */
+               $cond[] = $dbr->makeList(
+                       /* New Users older than a day who haven't done anything 
yet */
                        array(
                                'rc_log_action' => 'create',
                                'rc_log_type' => 'newusers',
@@ -35,14 +37,18 @@
                        array( 'DISTINCT rc_user_text' ),
                        $dbr->makeList( $cond, LIST_OR ),
                        __METHOD__,
-                       array( 'ORDER BY' => 'rc_user_text ASC' ) );
+                       array( 'ORDER BY' => 'rc_id desc' ) );
                $names=array();
                while( $row = $dbr->fetchObject( $result ) ) {
                        $names[] = $row->rc_user_text;
                }
                $whitelist = array_flip( self::getWhitelist() );
                return array_filter( $names,
-                       function($u) use ($whitelist) { if( isset( $whitelist[ 
$u ] ) ) return false; return true; }
+                       function($u) use ($whitelist) {
+                               if( isset( $whitelist[ $u ] ) )
+                                       return false;
+                               return true;
+                       }
                );
        }
 
@@ -52,7 +58,8 @@
                if( is_array( $user ) ) {
                        foreach( $user as $u ) {
                                if ( $u ) {
-                                       $ip = array_merge( $ip, 
self::getBannableIP( User::newFromName( $u ) ) );
+                                       $ip = array_merge( $ip,
+                                               self::getBannableIP( 
User::newFromName( $u ) ) );
                                }
                        }
                } elseif ( is_object( $user ) ) {
@@ -69,7 +76,11 @@
                }
                $whitelist = array_flip( self::getWhitelist() );
                return array_filter( $ip,
-                       function($u) use ($whitelist) { if( isset( $whitelist[ 
$u ] ) ) return false; return true; }
+                       function($u) use ($whitelist) {
+                               if( isset( $whitelist[ $u ] ) )
+                                       return false;
+                               return true;
+                       }
                );
        }
 
@@ -100,35 +111,45 @@
                return $pages;
        }
 
-       static function banIPs( $ips, $banningUser, $sp = null ) {
+       static function banIPs( $ips, $banningUser, $specialPage = null, 
$banCount = null ) {
                $ret = array();
-               foreach( (array)$ips as $ip ) {
-                       if( !Block::newFromTarget( $ip ) ) {
-                               $blk = new Block( $ip, null,
-                                       $banningUser->getID(), 
wfMsg('blockandnuke-message'),
-                                       wfTimestamp(), 0, Block::infinity(), 0, 
1, 0, 0, 1);
-                               $blk->isAutoBlocking( true );
-                               if( $blk->insert() ) {
-                                       $log = new LogPage('block');
-                                       $log->addEntry('block', 
Title::makeTitle( NS_USER, $ip ),
-                                               'Blocked through 
Special:BlockAndNuke', array('infinite', $ip,  'nocreate'));
-                                       $ret[] = $ip;
-                                       if( $sp ) {
-                                               $sp->getOutput()->addHTML( 
wfMessage( "blockandnuke-banned-ip", $ip ) );
+               if( $banCount > 0 || $banCount == null ) {
+                       foreach( (array)$ips as $ip ) {
+                               if( !Block::newFromTarget( $ip ) ) {
+                                       $blk = new Block( $ip, null,
+                                               $banningUser->getID(), 
wfMsg('blockandnuke-message'),
+                                               wfTimestamp(), 0,
+                                               wfGetDB( DB_SLAVE 
)->getInfinity(), 0, 1, 0, 0, 1);
+                                       $blk->isAutoBlocking( true );
+                                       if( $blk->insert() ) {
+                                               $log = new LogPage('block');
+                                               $log->addEntry('block', 
Title::makeTitle( NS_USER, $ip ),
+                                                       'Blocked through 
Special:BlockAndNuke',
+                                                       array( 'infinite', $ip, 
 'nocreate' ) );
+                                               $ret[] = $ip;
+                                               if( $specialPage ) {
+                                                       
$specialPage->getOutput()->addHTML(
+                                                               wfMessage( 
"blockandnuke-banned-ip", $ip ) );
+                                               }
                                        }
                                }
+                               if( --$banCount == 0 ) {
+                                       goto LASTIP;
+                               }
                        }
+                 LASTIP:
                }
                $ret = array_filter( $ret );
-               return $ret ? true : false;
+               return $ret ? false : true;
        }
 
-       static function banUser( $user, $banningUser, $spammer, $um ) {
+       static function banUser( $user, $banningUser, $spammer, $userMerger = 
null ) {
                $ret = null;
                if ( !is_object( $user ) ) {
                        /* Skip this one */
-               } elseif ( $user->getID() != 0 && $um ) {
-                       $ret = $um->merge( $user, $spammer, "block", 
$banningUser );
+                       error_log("Skipping $user");
+               } elseif ( $user->getID() != 0 && $userMerger ) {
+                       $ret = $userMerger->merge( $user, $spammer, "block", 
$banningUser );
                } else {
                        if( !Block::newFromTarget( $user->getName() ) ) {
                                $blk = new Block($user->getName(), 
$user->getId(),
@@ -138,28 +159,34 @@
                                if($ret = $blk->insert()) {
                                        $log = new LogPage('block');
                                        $log->addEntry('block', 
Title::makeTitle( NS_USER, $user->getName() ),
-                                               'Blocked through 
Special:BlockAndNuke', array('infinite', $user->getName(),  'nocreate'));
+                                               'Blocked through 
Special:BlockAndNuke',
+                                               array('infinite', 
$user->getName(),  'nocreate'));
                                }
                        }
                }
                return $ret;
        }
 
-       static function blockUser($user, $user_id, $banningUser, $spammer, $um) 
{
+       static function blockUser($user, $user_id, $banningUser, $spammer, 
$userMerger = null,
+               &$banCount = null ) {
                $ret = array();
-               for($c = 0; $c < max( count($user), count($user_id) ); $c++ ){
+               $maxUserCount = max( count($user), count($user_id) );
+               $maxBan = $banCount !== null ? min( $banCount, $maxUserCount ) 
: $maxUserCount;
+
+               for($c = 0; $c < $maxBan; $c++ ){
                        if( isset( $user[$c] ) ) {
                                $thisUserObj = User::newFromName( $user[$c] );
                        } elseif( isset( $user_id[$c] ) ) {
                                $thisUserObj = User::newFromId( $user_id[$c] );
                        }
-                       $ret[] = self::banUser( $thisUserObj, $banningUser, 
$spammer, $um );
+                       $ret[] = self::banUser( $thisUserObj, $banningUser, 
$spammer, $userMerger );
+                       if( $banCount !== null ) $banCount--;
                }
                $ret = array_filter( $ret );
                return $ret  ? true : false;
        }
 
-       static function deletePage( $title, $sp = null ) {
+       static function deletePage( $title, $specialPage = null ) {
                $ret = null;
                $file = $title->getNamespace() == NS_IMAGE ? wfLocalFile( 
$title ) : false;
                if ($file) {
@@ -171,18 +198,25 @@
                        if( $title->isKnown() ) {
                                $article = new Article( $title );
                                $ret = $article->doDelete( $reason );
-                               if( $ret && $sp ) {
-                                       $sp->getOutput()->addHTML( wfMessage( 
"blockandnuke-deleted-page", $title ) );
+                               if( $ret && $specialPage ) {
+                                       $specialPage->getOutput()->addHTML(
+                                               wfMessage( 
"blockandnuke-deleted-page", $title ) );
                                }
                        }
                }
                return $ret;
        }
 
-       static function deletePages( $pages, $sp = null ) {
+       static function deletePages( $pages, $specialPage = null, &$banCount = 
null) {
                $ret = array();
-               foreach((array)$pages as $page) {
-                       $ret[] = self::deletePage( Title::newFromURL($page), 
$sp );
+               if( $banCount > 0 && $banCount !== null ) {
+                       foreach((array)$pages as $page) {
+                               $ret[] = self::deletePage( 
Title::newFromURL($page), $specialPage );
+                               if( --$banCount === 0 ) {
+                                       goto LASTPAGE;
+                               }
+                       }
+                 LASTPAGE:
                }
                $ret = array_filter( $ret );
                return $ret  ? true : false;
diff --git a/BlockAndNuke.body.php b/BlockAndNuke.body.php
index bcec865..ca7d80a 100644
--- a/BlockAndNuke.body.php
+++ b/BlockAndNuke.body.php
@@ -6,8 +6,7 @@
 class SpecialBlock_Nuke extends SpecialPage {
        function __construct() {
                //restrict access only to admin
-               parent::__construct( 'blockandnuke', 'blockandnuke' );
-
+               parent::__construct( 'BlockAndNuke', 'blockandnuke' );
        }
 
        function execute( $par ){
@@ -21,31 +20,33 @@
                $this->setHeaders();
                $this->outputHeader();
 
-               $um = null;
+               $userMerger = null;
                $spammer = User::newFromName( $wgBaNSpamUser );
                if( class_exists( "UserMerger" ) && $spammer->getID() != 0 )  {
-                       $um = new UserMerger( $this );
+                       $userMerger = new UserMerger( $this );
                }
                $posted = $wgRequest->wasPosted();
-               if( $posted ) {
-                       $user_id = $wgRequest->getArray('userid');
-                       $user = $wgRequest->getArray('names');
-                       $pages = $wgRequest->getArray( 'pages' );
-                       $user_2 = $wgRequest->getArray('names_2');
-                       $ips = $wgRequest->getArray('ip');
 
-                       if($user){
+               if( $posted ) {
+                       $banCount = $wgRequest->getVal( 'banCount', "25" );
+                       $userId = $wgRequest->getArray( 'userid' );
+                       $user = $wgRequest->getArray( 'names' );
+                       $pages = $wgRequest->getArray( 'pages' );
+                       $user2 = $wgRequest->getArray( 'names_2' );
+                       $ips = $wgRequest->getArray( 'ip' );
+
+                       if($user && !count( $user2 ) ){
                                $wgOut->addHTML( wfMsg( 
"blockandnuke-banhammer" ) );
                                $this->getNewPages($user);
-                       } elseif( count( $pages ) || count( $user_2 ) || count( 
$ips ) ) {
+                       } elseif( count( $pages ) || count( $user2 )
+                               || count( $ips ) ) {
                                $wgOut->addHTML( wfMsg( "blockandnuke-banning" 
) );
-                               $v = false;
-                               $v = BanPests::blockUser( $user_2, $user_id, 
$wgUser, $spammer, $um )
-                                       || BanPests::deletePages( $pages, $this 
)
-                                       || BanPests::banIPs( $ips, $wgUser, 
$this );
-                               if(!$v) {
-                                       $wgOut->addHTML( wfMsg( 
'blockandnuke-nothing-to-do' ) );
-                               }
+
+                               BanPests::blockUser( $user2, $userId, $wgUser,
+                                       $spammer, $userMerger );
+                               BanPests::deletePages( $pages, $this );
+                               BanPests::banIPs( $ips, $wgUser, $this );
+                               $this->showUserForm();
                        } else {
                                $wgOut->addHTML( wfMsg( 
'blockandnuke-nothing-to-do' ) );
                        }
@@ -55,60 +56,54 @@
        }
 
        function showUserForm() {
-               global $wgOut, $wgUser;
+               global $wgOut, $wgUser, $wgRequest;
 
+               $ddPreload = "25\n50\n100\n1000";
+               $ddPreset  = "25";
+               $banCount = $wgRequest->getVal( 'banCount', $ddPreset );
                $names = BanPests::getBannableUsers();
                $whitelist = BanPests::getWhitelist();
 
                $wgOut->addWikiMsg( 'blockandnuke-tools' );
                $wgOut->addHTML(
                        Xml::openElement( 'form', array(
-                               'action' => $this->getTitle()->getLocalURL( 
'action=submit' ),
-                               'method' => 'post')).
-                       HTML::hidden( 'wpEditToken', $wgUser->editToken() ).
+                                       'action' => 
$this->getTitle()->getLocalURL( 'action=submit' ),
+                                       'method' => 'post')).
+                       $this->blockAndNukeButton( $ddPreload, $ddPreset ) .
+                       HTML::hidden( 'wpEditToken', $wgUser->getEditToken() ).
                        ( '<ul>' ) );
 
                //make into links  $sk = $wgUser->getSkin();
 
-               foreach($names as $user){
-                       if (!in_array($user, $whitelist)){
+               foreach( array_slice( $names, 0, $banCount ) as $user){
+                       if ( !in_array( $user, $whitelist ) ) {
                                $wgOut->addHTML( '<li>'.
                                        Xml::check( 'names[]', true,
                                                array( 'value' =>  $user)).
-                                       ($user).
-                                       "</li>\n" );
+                                       ( $user ).
+                                       "</li>\n" ); 
                        }
-
+               }
+               $remaining = count( $names ) - $banCount;
+               $remain = "";
+               if( $remaining > 0 ) {
+                       $remain = Xml::Element( "p", null, "(There are 
$remaining more users.)" );
                }
                $wgOut->addHTML(
                        "</ul>\n" .
-                       Xml::submitButton( wfMsg( 'blockandnuke-submit-user' ) 
).
+                       $remain .
+                       $this->blockAndNukeButton( $ddPreload, $ddPreset ) .
                        "</form>" );
        }
 
-       function getNewPages($user) {
+
+       function blockAndNukeButton( $ddPreload, $ddPreset ) {
+               return Html::hidden( 'banCount', $ddPreset ).
+                       Xml::submitButton( wfMsg( 'blockandnuke-submit-user' ) 
);
+       }
+
+       function showBannableUsers( $user ) {
                global $wgOut, $wgUser;
-
-               $wgOut->addHTML(
-                       Xml::openElement( 'form', array(
-                                       'action' => 
$this->getTitle()->getLocalURL( 'action=delete' ),
-                                       'method' => 'post')).
-                       HTML::hidden( 'wpEditToken', $wgUser->editToken() ).
-                       ( '<ul>' ) );
-
-               $pages = BanPests::getBannablePages( $user );
-               $ips = BanPests::getBannableIP( $user );
-
-               if( count( $pages ) ) {
-                       $wgOut->addHTML( "<h2>". wfMsg( "blockandnuke-pages" ) 
."</h2>" );
-
-                       $wgOut->addHtml( "<ul>" );
-                       foreach( $pages as $title ) {
-                               $wgOut->addHtml( "<li>". Linker::link( $title ) 
);
-                               $wgOut->addHtml(HTML::hidden( 'pages[]', 
$title));
-                       }
-                       $wgOut->addHtml( "</ul>\n" );
-               }
 
                if( count( $user ) ) {
                        $wgOut->addHTML( "<h2>". wfMsg( "blockandnuke-users" ) 
."</h2>" );
@@ -130,18 +125,37 @@
                                $wgOut->addHtml( "<ul>" );
                                $seen = array();
                                foreach($name as $infos) {
-                                       list($user_2, $user_id) = $infos;
-                                       if( !isset( $seen[$user_2] ) ) {
-                                               $seen[$user_2] = true;
+                                       list($user2, $userId) = $infos;
+                                       if( !isset( $seen[$user2] ) ) {
+                                               $seen[$user2] = true;
                                                $wgOut->addHtml( "<li>" .
-                                                       Linker::link( 
Title::newFromText( $user_2, NS_USER ) ) );
-                                               $wgOut->addHTML(HTML::hidden( 
'names_2[]', $user_2).
-                                                       HTML::hidden( 
'userid[]', $user_id));
+                                                       Linker::link( 
Title::newFromText( $user2, NS_USER ) ) );
+                                               $wgOut->addHTML(HTML::hidden( 
'names_2[]', $user2).
+                                                       HTML::hidden( 
'userid[]', $userId));
                                        }
                                }
                                $wgOut->addHtml( "</ul>\n" );
                        }
                }
+       }
+
+       function showBannablePages( $pages ) {
+               global $wgOut, $wgUser;
+
+               if( count( $pages ) ) {
+                       $wgOut->addHTML( "<h2>". wfMsg( "blockandnuke-pages" ) 
."</h2>" );
+
+                       $wgOut->addHtml( "<ul>" );
+                       foreach( $pages as $title ) {
+                               $wgOut->addHtml( "<li>". Linker::link( $title ) 
);
+                               $wgOut->addHtml(HTML::hidden( 'pages[]', 
$title));
+                       }
+                       $wgOut->addHtml( "</ul>\n" );
+               }
+       }
+
+       function showBannableIPs( $ips ) {
+               global $wgOut, $wgUser;
 
                if( $ips ) {
                        $wgOut->addHTML( "<h2>". wfMsg( 
"blockandnuke-ip-addresses" ) ."</h2>" );
@@ -158,6 +172,25 @@
                                $wgOut->addHtml( "</ul>\n" );
                        }
                }
+       }
+
+       function getNewPages($user) {
+               global $wgOut, $wgUser;
+
+               $wgOut->addHTML(
+                       Xml::openElement( 'form', array(
+                                       'action' => 
$this->getTitle()->getLocalURL( 'action=delete' ),
+                                       'method' => 'post')).
+                       HTML::hidden( 'wpEditToken', $wgUser->getEditToken() ).
+                       XML::submitButton( wfMsg( 'blockandnuke' ) ).
+                       '<ul>' );
+
+               $pages = BanPests::getBannablePages( $user );
+               $ips = BanPests::getBannableIP( $user );
+
+               $this->showBannableUsers( $user );
+               $this->showBannablePages( $pages );
+               $this->showBannableIPs( $ips );
 
                $wgOut->addHTML(
                        "</ul>\n" .
diff --git a/BlockAndNuke.i18n.php b/BlockAndNuke.i18n.php
index e5a5d9e..8410b75 100644
--- a/BlockAndNuke.i18n.php
+++ b/BlockAndNuke.i18n.php
@@ -11,7 +11,8 @@
  * @author Brion Vibber
  */
 $aliases['en'] = array(
-       'BlockAndNuke' => array( 'BlockAndNuke' ),
+       'BlockAndNuke' => array( 'blockandnuke' ),
+       'blockandnuke' => array( 'BlockAndNuke' ),
 );
 
 $messages = array();
@@ -35,7 +36,7 @@
        'blockandnuke-submit-delete'  => 'BlockAndNuke',
        'blockandnuke-block'          => 'Selected users have been blocked and 
their contributions deleted.',
        'blockandnuke-message'     => 'Users blocked by BlockAndNuke.',
-       'blockandnuke-banhammer'      => "To block the selected users and 
delete all their contributions click the button below.",
+       'blockandnuke-banhammer'      => "To block the selected users and the 
IP addresses they connected from as well as delete all their contributions, 
click the button below.",
        'blockandnuke-nothing-to-do'  => 'Nothing to do!',
        'blockandnuke-banning'        => 'Banning...<br>',
        'blockandnuke-deleted-page'   => "Deleted $1<br>",
diff --git a/README.md b/README.md
index d1e4983..90c5331 100644
--- a/README.md
+++ b/README.md
@@ -11,7 +11,7 @@
     Dorris
     Sam
 
-2. Go to Special:Blockandnuke. The special page is listed under 
Special:SpecialPages, under Other Special Pages as "Block and Nuke". A 
checklist shows all users who made contributions since last use of this tool 
and are not listed in whitelist.txt. By default all users are checked.
+2. Go to Special:BlockAndNuke. The special page is listed under 
Special:SpecialPages, under Other Special Pages as "Block and Nuke". A 
checklist shows all users who made contributions since last use of this tool 
and are not listed in whitelist.txt. By default all users are checked.
 
 3. Select users you would like to block - all their contributions will be 
deleted. 
 

-- 
To view, visit https://gerrit.wikimedia.org/r/120476
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I991eb98b5459ce46f50a86e584321a91a859ea4f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/BlockAndNuke
Gerrit-Branch: master
Gerrit-Owner: MarkAHershberger <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to