jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/380421 )
Change subject: Add composer.json and phpcs.xml ...................................................................... Add composer.json and phpcs.xml And also fix issues detected by the linters. Bug: T176587 Change-Id: I79cae204fe1a58c05e64404225073004b9cc9ec1 --- M .gitignore M BanPests.php M BlockAndNuke.body.php M BlockAndNuke.hooks.php A composer.json M maintenance/blockAndNukeSpammers.php A phpcs.xml 7 files changed, 163 insertions(+), 118 deletions(-) Approvals: Umherirrender: Looks good to me, approved jenkins-bot: Verified diff --git a/.gitignore b/.gitignore index 3387392..f8b8b12 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ +*.kate-swp +*~ +.*.swp +.svn /node_modules/ /vendor/ -.svn -*~ -*.kate-swp -.*.swp +composer.lock \ No newline at end of file diff --git a/BanPests.php b/BanPests.php index 46440c2..c474510 100644 --- a/BanPests.php +++ b/BanPests.php @@ -10,7 +10,8 @@ $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.' + 'You need to specify a whitelist!' + . ' $wgBaNwhitelist should point to a filename that contains the whitelist.' ); } @@ -19,46 +20,46 @@ } public static function getBannableUsers() { - $dbr = wfGetDB( DB_SLAVE ); - $cond = array( 'rc_new' => 1 ); /* Anyone creating new pages */ - $cond[] = $dbr->makeList( /* Anyone uploading stuff */ - array( + $dbr = wfGetDB( DB_REPLICA ); + $cond = [ 'rc_new' => 1 ]; /* Anyone creating new pages */ + $cond[] = $dbr->makeList( /* Anyone uploading stuff */ + [ '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 */ - array( + [ 'rc_log_action' => 'create', 'rc_log_type' => 'newusers', - ), + ], LIST_AND ); $result = $dbr->select( 'recentchanges', - array( 'DISTINCT rc_user_text' ), + [ 'DISTINCT rc_user_text' ], $dbr->makeList( $cond, LIST_OR ), __METHOD__, - array( 'ORDER BY' => 'rc_user_text ASC' ) + [ 'ORDER BY' => 'rc_user_text ASC' ] ); - $names = array(); - foreach( $result as $row ) { + $names = []; + foreach ( $result as $row ) { $names[] = $row->rc_user_text; } $whitelist = array_flip( self::getWhitelist() ); return array_filter( $names, - function( $u ) use ( $whitelist ) { + function ( $u ) use ( $whitelist ) { return !isset( $whitelist[ $u ] ); } ); } public static function getBannableIP( $user ) { - $dbr = wfGetDB( DB_SLAVE ); - $ip = array(); - if( is_array( $user ) ) { - foreach( $user as $u ) { + $dbr = wfGetDB( DB_REPLICA ); + $ip = []; + if ( is_array( $user ) ) { + foreach ( $user as $u ) { if ( $u ) { $ip = array_merge( $ip, self::getBannableIP( User::newFromName( $u ) ) ); } @@ -66,12 +67,12 @@ } elseif ( is_object( $user ) ) { $result = $dbr->select( 'recentchanges', - array( 'DISTINCT rc_ip' ), - array( 'rc_user_text' => $user->getName() ), + [ 'DISTINCT rc_ip' ], + [ 'rc_user_text' => $user->getName() ], __METHOD__, - array( 'ORDER BY' => 'rc_ip ASC' ) + [ 'ORDER BY' => 'rc_ip ASC' ] ); - foreach( $result as $row ) { + foreach ( $result as $row ) { $ip[] = $row->rc_ip; } } else { @@ -79,33 +80,33 @@ } $whitelist = array_flip( self::getWhitelist() ); return array_filter( $ip, - function( $u ) use ( $whitelist ) { + function ( $u ) use ( $whitelist ) { return !isset( $whitelist[ $u ] ); } ); } public static function getBannablePages( $user ) { - $dbr = wfGetDB( DB_SLAVE ); + $dbr = wfGetDB( DB_REPLICA ); $result = null; - if( $user ) { + if ( $user ) { $result = $dbr->select( 'recentchanges', - array( 'rc_namespace', 'rc_title', 'rc_timestamp', 'COUNT(*) AS edits' ), - array( + [ 'rc_namespace', 'rc_title', 'rc_timestamp', 'COUNT(*) AS edits' ], + [ 'rc_user_text' => $user, '(rc_new = 1) OR (rc_log_type = \'upload\' AND rc_log_action = \'upload\')' - ), + ], __METHOD__, - array( + [ 'ORDER BY' => 'rc_timestamp DESC', 'GROUP BY' => 'rc_namespace, rc_title' - ) + ] ); } - $pages = array(); - if( $result ) { - foreach( $result as $row ) { + $pages = []; + if ( $result ) { + foreach ( $result as $row ) { $pages[] = Title::makeTitle( $row->rc_namespace, $row->rc_title ); } } @@ -114,30 +115,30 @@ } public static function banIPs( $ips, $banningUser, $sp = null ) { - $ret = array(); - foreach( (array)$ips as $ip ) { - if( !Block::newFromTarget( $ip ) ) { + $ret = []; + foreach ( (array)$ips as $ip ) { + if ( !Block::newFromTarget( $ip ) ) { $blk = new Block( - array( + [ 'address' => $ip, 'by' => $banningUser->getID(), 'reason' => wfMessage( 'blockandnuke-message' )->text(), - 'expiry' => wfGetDB( DB_SLAVE )->getInfinity(), + 'expiry' => wfGetDB( DB_REPLICA )->getInfinity(), 'createAccount' => true, 'blockEmail' => true - ) + ] ); $blk->isAutoBlocking( true ); - if( $blk->insert() ) { - $log = new LogPage('block'); + if ( $blk->insert() ) { + $log = new LogPage( 'block' ); $log->addEntry( 'block', Title::makeTitle( NS_USER, $ip ), 'Blocked through Special:BlockandNuke', - array( 'infinite', $ip, 'nocreate' ) + [ 'infinite', $ip, 'nocreate' ] ); $ret[] = $ip; - if( $sp ) { + if ( $sp ) { $sp->getOutput()->addHTML( wfMessage( "blockandnuke-banned-ip", $ip ) ); } } @@ -161,26 +162,27 @@ $um = new MergeUser( $spammer, $user ); $ret = $um->merge( $banningUser, __METHOD__ ); } else { - if( !Block::newFromTarget( $user->getName() ) ) { + if ( !Block::newFromTarget( $user->getName() ) ) { $blk = new Block( - array( + [ 'address' => $user->getName(), 'user' => $user->getID(), 'by' => $banningUser->getID(), 'reason' => wfMessage( 'blockandnuke-message' )->text(), - 'expiry' => wfGetDB( DB_SLAVE )->getInfinity(), + 'expiry' => wfGetDB( DB_REPLICA )->getInfinity(), 'createAccount' => true, 'blockEmail' => true - ) + ] ); $blk->isAutoBlocking( true ); - if($ret = $blk->insert()) { - $log = new LogPage('block'); + $ret = $blk->insert(); + if ( $ret ) { + $log = new LogPage( 'block' ); $log->addEntry( 'block', Title::makeTitle( NS_USER, $user->getName() ), 'Blocked through Special:BlockandNuke', - array( 'infinite', $user->getName(), 'nocreate' ) + [ 'infinite', $user->getName(), 'nocreate' ] ); } } @@ -188,12 +190,13 @@ return $ret; } - public static function blockUser($user, $user_id, $banningUser, $spammer ) { - $ret = array(); - for($c = 0; $c < max( count($user), count($user_id) ); $c++ ){ - if( isset( $user[$c] ) ) { + public static function blockUser( $user, $user_id, $banningUser, $spammer ) { + $ret = []; + $max = max( count( $user ), count( $user_id ) ); + for ( $c = 0; $c < $max; $c++ ) { + if ( isset( $user[$c] ) ) { $thisUserObj = User::newFromName( $user[$c] ); - } elseif( isset( $user_id[$c] ) ) { + } elseif ( isset( $user_id[$c] ) ) { $thisUserObj = User::newFromId( $user_id[$c] ); } $ret[] = self::banUser( $thisUserObj, $banningUser, $spammer ); @@ -206,15 +209,15 @@ $ret = null; $file = $title->getNamespace() == NS_FILE ? wfLocalFile( $title ) : false; if ( $file ) { - $reason= wfMessage( "blockandnuke-delete-file" )->text(); + $reason = wfMessage( "blockandnuke-delete-file" )->text(); $oldimage = null; // Must be passed by reference $ret = FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, false ); } else { $reason = wfMessage( "blockandnuke-delete-article" )->text(); - if( $title->isKnown() ) { + if ( $title->isKnown() ) { $article = new Article( $title ); $ret = $article->doDelete( $reason ); - if( $ret && $sp ) { + if ( $ret && $sp ) { $sp->getOutput()->addHTML( wfMessage( "blockandnuke-deleted-page", $title ) ); } } @@ -223,8 +226,8 @@ } public static function deletePages( $pages, $sp = null ) { - $ret = array(); - foreach( (array)$pages as $page ) { + $ret = []; + foreach ( (array)$pages as $page ) { $ret[] = self::deletePage( Title::newFromText( $page ), $sp ); } $ret = array_filter( $ret ); diff --git a/BlockAndNuke.body.php b/BlockAndNuke.body.php index 98801e9..b49efcf 100644 --- a/BlockAndNuke.body.php +++ b/BlockAndNuke.body.php @@ -1,18 +1,19 @@ <?php -if( !defined( 'MEDIAWIKI' ) ) +if ( !defined( 'MEDIAWIKI' ) ) { die( 'Not an entry point.' ); +} class SpecialBlock_Nuke extends SpecialPage { function __construct() { - //restrict access only to users with blockandnuke right + // restrict access only to users with blockandnuke right parent::__construct( 'blockandnuke', 'blockandnuke' ); } - function execute( $par ){ + function execute( $par ) { global $wgUser, $wgRequest, $wgOut, $wgBaNSpamUser; - if( !$this->userCanExecute( $wgUser ) ){ + if ( !$this->userCanExecute( $wgUser ) ) { $this->displayRestrictionError(); return; } @@ -23,23 +24,23 @@ $um = null; $spammer = User::newFromName( $wgBaNSpamUser ); $posted = $wgRequest->wasPosted(); - if( $posted ) { + 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 ( $user ) { $wgOut->addHTML( $this->msg( 'blockandnuke-banhammer' )->escaped() ); - $this->getNewPages($user); - } elseif( count( $pages ) || count( $user_2 ) || count( $ips ) ) { + $this->getNewPages( $user ); + } elseif ( count( $pages ) || count( $user_2 ) || count( $ips ) ) { $wgOut->addHTML( $this->msg( 'blockandnuke-banning' )->escaped() ); $v = false; $v = BanPests::blockUser( $user_2, $user_id, $wgUser, $spammer ) || BanPests::deletePages( $pages, $this ) || BanPests::banIPs( $ips, $wgUser, $this ); - if( !$v ) { + if ( !$v ) { $wgOut->addHTML( $this->msg( 'blockandnuke-nothing-to-do' )->escaped() ); } } else { @@ -58,22 +59,22 @@ $wgOut->addWikiMsg( 'blockandnuke-tools' ); $wgOut->addHTML( - Xml::openElement( 'form', array( + Xml::openElement( 'form', [ 'action' => $this->getTitle()->getLocalURL( 'action=submit' ), - 'method' => 'post' ) + 'method' => 'post' ] ). Html::hidden( 'wpEditToken', $wgUser->getEditToken() ). ( '<ul>' ) ); - //make into links $sk = $wgUser->getSkin(); + // make into links $sk = $wgUser->getSkin(); - foreach($names as $user){ + foreach ( $names as $user ) { if ( !in_array( $user, $whitelist ) ) { $wgOut->addHTML( '<li>' . Xml::check( 'names[]', true, - array( 'value' => $user ) + [ 'value' => $user ] ) . $user . "</li>\n" @@ -88,16 +89,16 @@ ); } - function getNewPages($user) { + 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() ) . '<ul>' @@ -107,41 +108,41 @@ $ips = BanPests::getBannableIP( $user ); $linkRenderer = $this->getLinkRenderer(); - if( count( $pages ) ) { + if ( count( $pages ) ) { $wgOut->addHTML( "<h2>" . $this->msg( "blockandnuke-pages" )->escaped() . "</h2>" ); $wgOut->addHtml( "<ul>" ); - foreach( $pages as $title ) { + foreach ( $pages as $title ) { $wgOut->addHtml( "<li>". $linkRenderer->makeLink( $title ) ); $wgOut->addHtml( Html::hidden( 'pages[]', $title ) ); } $wgOut->addHtml( "</ul>\n" ); } - if( count( $user ) ) { + if ( count( $user ) ) { $wgOut->addHTML( "<h2>" . $this->msg( "blockandnuke-users" )->escaped() . "</h2>" ); - foreach($user as $users){ - $dbr = wfGetDB( DB_SLAVE ); + foreach ( $user as $users ) { + $dbr = wfGetDB( DB_REPLICA ); $result = $dbr->select( 'recentchanges', - array( 'rc_user', 'rc_user_text' ), - array( 'rc_user_text' => $users ), + [ 'rc_user', 'rc_user_text' ], + [ 'rc_user_text' => $users ], __METHOD__, - array( + [ 'ORDER BY' => 'rc_user ASC', - ) + ] ); - $name = array(); - foreach( $result as $row ) { - $name[] = array( $row->rc_user_text, $row->rc_user ); + $name = []; + foreach ( $result as $row ) { + $name[] = [ $row->rc_user_text, $row->rc_user ]; } $wgOut->addHtml( "<ul>" ); - $seen = array(); - foreach( $name as $infos ) { + $seen = []; + foreach ( $name as $infos ) { list( $user_2, $user_id ) = $infos; - if( !isset( $seen[$user_2] ) ) { + if ( !isset( $seen[$user_2] ) ) { $seen[$user_2] = true; $wgOut->addHtml( "<li>" . @@ -157,13 +158,13 @@ } } - if( $ips ) { + if ( $ips ) { $wgOut->addHTML( "<h2>" . $this->msg( "blockandnuke-ip-addresses" )->escaped() . "</h2>" ); - foreach( $ips as $ip ) { + foreach ( $ips as $ip ) { $wgOut->addHtml( "<ul>" ); - $seen = array(); - if( !isset( $seen[$ip] ) ) { + $seen = []; + if ( !isset( $seen[$ip] ) ) { $seen[$ip] = true; $wgOut->addHtml( "<li>" . diff --git a/BlockAndNuke.hooks.php b/BlockAndNuke.hooks.php index 37f7778..016666e 100644 --- a/BlockAndNuke.hooks.php +++ b/BlockAndNuke.hooks.php @@ -4,4 +4,4 @@ public static function onPerformRetroactiveAutoblock( $block, $blockIds ) { return true; } -} \ No newline at end of file +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..cbb6402 --- /dev/null +++ b/composer.json @@ -0,0 +1,26 @@ +{ + "name": "mediawiki/BlockAndNuke", + "description": "MediaWiki BlockAndNuke Extension", + "type": "mediawiki-extension", + "keywords": [ + "mediawiki", + "blockandnuke" + ], + "homepage": "https://www.mediawiki.org/wiki/Extension:BlockAndNuke", + "license": "GPL-3.0+", + "support": { + "issues": "https://phabricator.wikimedia.org" + }, + "require-dev": { + "jakub-onderka/php-console-highlighter": "0.3.2", + "jakub-onderka/php-parallel-lint": "0.9.2", + "mediawiki/mediawiki-codesniffer": "13.0.0" + }, + "scripts": { + "test": [ + "parallel-lint . --exclude vendor", + "phpcs -p -s" + ], + "fix": "phpcbf" + } +} diff --git a/maintenance/blockAndNukeSpammers.php b/maintenance/blockAndNukeSpammers.php index 0b5c821..dbcd5d2 100644 --- a/maintenance/blockAndNukeSpammers.php +++ b/maintenance/blockAndNukeSpammers.php @@ -10,7 +10,7 @@ * @license GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.html> */ -require_once( dirname( dirname( dirname( __DIR__ ) ) ) . '/maintenance/Maintenance.php' ); +require_once dirname( dirname( dirname( __DIR__ ) ) ) . '/maintenance/Maintenance.php'; class BanHammer extends Maintenance { public function __construct() { @@ -22,7 +22,7 @@ } public function maybeOutput( $str ) { - if(!$this->hasOption( "brief" ) ){ + if ( !$this->hasOption( "brief" ) ) { $this->output( $str ); } } @@ -30,10 +30,10 @@ public function execute() { global $wgBaNSpamUser; - $this->output( "Starting "); + $this->output( "Starting " ); $real = $this->hasOption( "hammer" ); $brief = $this->hasOption( "brief" ); - if( !$real ) { + if ( !$real ) { $this->output( "dry run\n" ); } else { $this->output( "\n" ); @@ -47,12 +47,12 @@ "Found %d bannable users and %d pages:\n", count( $bannable ), count( $pages ) ) ); - if( count( $pages ) ) { + if ( count( $pages ) ) { $this->maybeOutput( "Pages\n" ); - foreach( $pages as $page ) { - if( $page ) { + foreach ( $pages as $page ) { + if ( $page ) { $this->maybeOutput( "\t$page" ); - if( $real ) { + if ( $real ) { $this->maybeOutput( " ... deleting\n" ); BanPests::deletePage( $page ); } else { @@ -64,30 +64,30 @@ $spammer = User::newFromName( $wgBaNSpamUser ); $banningUser = User::newFromName( "WikiSysop" ); - if( count( $bannable ) ) { + if ( count( $bannable ) ) { $this->maybeOutput( "Users\n" ); - foreach( $bannable as $user ) { + foreach ( $bannable as $user ) { $this->maybeOutput( "\t$user" ); $u = User::newFromName( $user ); if ( $u === false ) { - $ips = array( $user ); + $ips = [ $user ]; } else { $ips = BanPests::getBannableIP( $u ); } - if( $real ) { + if ( $real ) { $this->maybeOutput( " ... banning\n" ); - if( $u !== false ) { + if ( $u !== false ) { BanPests::banUser( $u, $banningUser, $spammer ); } - if( $ips ) { - foreach($ips as $ip) { + if ( $ips ) { + foreach ( $ips as $ip ) { $this->maybeOutput( "\t\tEnsuring ban on $ip\n" ); - BanPests::banIPs( $ip, $banningUser ); + BanPests::banIPs( $ip, $banningUser ); } } } else { $this->maybeOutput( "\n" ); - foreach($ips as $ip) { + foreach ( $ips as $ip ) { $this->maybeOutput( "\t\t{$ip}\n" ); } } @@ -97,4 +97,4 @@ } $maintClass = "BanHammer"; -require_once( RUN_MAINTENANCE_IF_MAIN ); +require_once RUN_MAINTENANCE_IF_MAIN; diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..a58fadc --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,14 @@ +<?xml version="1.0"?> +<ruleset> + <rule ref="./vendor/mediawiki/mediawiki-codesniffer/MediaWiki"> + <exclude name="MediaWiki.Files.ClassMatchesFilename.NotMatch" /> + <exclude name="Squiz.Classes.ValidClassName.NotCamelCaps" /> + <exclude name="MediaWiki.Commenting.FunctionComment.MissingDocumentationPublic" /> + <exclude name="MediaWiki.Commenting.FunctionComment.MissingDocumentationProtected" /> + <exclude name="MediaWiki.WhiteSpace.SpaceBeforeSingleLineComment.NewLineComment" /> + </rule> + <file>.</file> + <arg name="bootstrap" value="./vendor/mediawiki/mediawiki-codesniffer/utils/bootstrap-ci.php"/> + <arg name="extensions" value="php,php5,inc"/> + <arg name="encoding" value="UTF-8"/> +</ruleset> -- To view, visit https://gerrit.wikimedia.org/r/380421 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I79cae204fe1a58c05e64404225073004b9cc9ec1 Gerrit-PatchSet: 11 Gerrit-Project: mediawiki/extensions/BlockAndNuke Gerrit-Branch: master Gerrit-Owner: MarcoAurelio <maure...@tools.wmflabs.org> Gerrit-Reviewer: Legoktm <lego...@member.fsf.org> Gerrit-Reviewer: MarcoAurelio <maure...@tools.wmflabs.org> Gerrit-Reviewer: SamanthaNguyen <samanthanguyen1...@gmail.com> Gerrit-Reviewer: Umherirrender <umherirrender_de...@web.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits