3 * Implements Special:BlockList
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
24 use MediaWiki\Block\DatabaseBlock
;
25 use MediaWiki\MediaWikiServices
;
26 use Wikimedia\Rdbms\IDatabase
;
29 * A special page that lists existing blocks
31 * @ingroup SpecialPage
33 class SpecialBlockList
extends SpecialPage
{
40 function __construct() {
41 parent
::__construct( 'BlockList' );
45 * @param string|null $par Title fragment
47 public function execute( $par ) {
49 $this->outputHeader();
50 $this->addHelpLink( 'Help:Blocking_users' );
51 $out = $this->getOutput();
52 $out->setPageTitle( $this->msg( 'ipblocklist' ) );
53 $out->addModuleStyles( [ 'mediawiki.special' ] );
55 $request = $this->getRequest();
56 $par = $request->getVal( 'ip', $par );
57 $this->target
= trim( $request->getVal( 'wpTarget', $par ) );
59 $this->options
= $request->getArray( 'wpOptions', [] );
60 $this->blockType
= $request->getVal( 'blockType' );
62 $action = $request->getText( 'action' );
64 if ( $action == 'unblock' ||
$action == 'submit' && $request->wasPosted() ) {
65 # B/C @since 1.18: Unblock interface is now at Special:Unblock
66 $title = SpecialPage
::getTitleFor( 'Unblock', $this->target
);
67 $out->redirect( $title->getFullURL() );
72 # setup BlockListPager here to get the actual default Limit
73 $pager = $this->getBlockListPager();
75 # Just show the block list
79 'label-message' => 'ipaddressorusername',
82 'default' => $this->target
,
85 'type' => 'multiselect',
86 'options-messages' => [
87 'blocklist-tempblocks' => 'tempblocks',
88 'blocklist-indefblocks' => 'indefblocks',
89 'blocklist-userblocks' => 'userblocks',
90 'blocklist-addressblocks' => 'addressblocks',
91 'blocklist-rangeblocks' => 'rangeblocks',
97 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
98 $fields['BlockType'] = [
100 'label-message' => 'blocklist-type',
102 $this->msg( 'blocklist-type-opt-all' )->escaped() => '',
103 $this->msg( 'blocklist-type-opt-sitewide' )->escaped() => 'sitewide',
104 $this->msg( 'blocklist-type-opt-partial' )->escaped() => 'partial',
106 'name' => 'blockType',
107 'cssclass' => 'mw-field-block-type',
112 'type' => 'limitselect',
113 'label-message' => 'table_pager_limit_label',
114 'options' => $pager->getLimitSelectList(),
116 'default' => $pager->getLimit(),
117 'cssclass' => $this->getConfig()->get( 'EnablePartialBlocks' ) ?
118 'mw-field-limit mw-has-field-block-type' :
122 $context = new DerivativeContext( $this->getContext() );
123 $context->setTitle( $this->getPageTitle() ); // Remove subpage
124 $form = HTMLForm
::factory( 'ooui', $fields, $context );
127 ->setFormIdentifier( 'blocklist' )
128 ->setWrapperLegendMsg( 'ipblocklist-legend' )
129 ->setSubmitTextMsg( 'ipblocklist-submit' )
131 ->displayForm( false );
133 $this->showList( $pager );
137 * Setup a new BlockListPager instance.
138 * @return BlockListPager
140 protected function getBlockListPager() {
142 $db = $this->getDB();
143 # Is the user allowed to see hidden blocks?
144 if ( !MediaWikiServices
::getInstance()
145 ->getPermissionManager()
146 ->userHasRight( $this->getUser(), 'hideuser' )
148 $conds['ipb_deleted'] = 0;
151 if ( $this->target
!== '' ) {
152 list( $target, $type ) = DatabaseBlock
::parseTarget( $this->target
);
155 case DatabaseBlock
::TYPE_ID
:
156 case DatabaseBlock
::TYPE_AUTO
:
157 $conds['ipb_id'] = $target;
160 case DatabaseBlock
::TYPE_IP
:
161 case DatabaseBlock
::TYPE_RANGE
:
162 list( $start, $end ) = IP
::parseRange( $target );
163 $conds[] = $db->makeList(
165 'ipb_address' => $target,
166 DatabaseBlock
::getRangeCond( $start, $end )
170 $conds['ipb_auto'] = 0;
173 case DatabaseBlock
::TYPE_USER
:
174 $conds['ipb_address'] = $target->getName();
175 $conds['ipb_auto'] = 0;
181 if ( in_array( 'userblocks', $this->options
) ) {
182 $conds['ipb_user'] = 0;
184 if ( in_array( 'addressblocks', $this->options
) ) {
185 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
187 if ( in_array( 'rangeblocks', $this->options
) ) {
188 $conds[] = "ipb_range_end = ipb_range_start";
191 $hideTemp = in_array( 'tempblocks', $this->options
);
192 $hideIndef = in_array( 'indefblocks', $this->options
);
193 if ( $hideTemp && $hideIndef ) {
194 // If both types are hidden, ensure query doesn't produce any results
196 } elseif ( $hideTemp ) {
197 $conds['ipb_expiry'] = $db->getInfinity();
198 } elseif ( $hideIndef ) {
199 $conds[] = "ipb_expiry != " . $db->addQuotes( $db->getInfinity() );
202 if ( $this->blockType
=== 'sitewide' ) {
203 $conds['ipb_sitewide'] = 1;
204 } elseif ( $this->blockType
=== 'partial' ) {
205 $conds['ipb_sitewide'] = 0;
208 return new BlockListPager( $this, $conds );
212 * Show the list of blocked accounts matching the actual filter.
213 * @param BlockListPager $pager The BlockListPager instance for this page
215 protected function showList( BlockListPager
$pager ) {
216 $out = $this->getOutput();
218 # Check for other blocks, i.e. global/tor blocks
219 $otherBlockLink = [];
220 Hooks
::run( 'OtherBlockLogLink', [ &$otherBlockLink, $this->target
] );
222 # Show additional header for the local block only when other blocks exists.
223 # Not necessary in a standard installation without such extensions enabled
224 if ( count( $otherBlockLink ) ) {
226 Html
::element( 'h2', [], $this->msg( 'ipblocklist-localblock' )->text() ) . "\n"
230 if ( $pager->getNumRows() ) {
231 $out->addParserOutputContent( $pager->getFullOutput() );
232 } elseif ( $this->target
) {
233 $out->addWikiMsg( 'ipblocklist-no-results' );
235 $out->addWikiMsg( 'ipblocklist-empty' );
238 if ( count( $otherBlockLink ) ) {
243 $this->msg( 'ipblocklist-otherblocks', count( $otherBlockLink ) )->parse()
247 foreach ( $otherBlockLink as $link ) {
248 $list .= Html
::rawElement( 'li', [], $link ) . "\n";
250 $out->addHTML( Html
::rawElement(
252 [ 'class' => 'mw-ipblocklist-otherblocks' ],
258 protected function getGroupName() {
263 * Return a IDatabase object for reading
267 protected function getDB() {
268 return wfGetDB( DB_REPLICA
);