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
;
27 * A special page that lists existing blocks
29 * @ingroup SpecialPage
31 class SpecialBlockList
extends SpecialPage
{
38 function __construct() {
39 parent
::__construct( 'BlockList' );
43 * @param string|null $par Title fragment
45 public function execute( $par ) {
47 $this->outputHeader();
48 $this->addHelpLink( 'Help:Blocking_users' );
49 $out = $this->getOutput();
50 $out->setPageTitle( $this->msg( 'ipblocklist' ) );
51 $out->addModuleStyles( [ 'mediawiki.special' ] );
53 $request = $this->getRequest();
54 $par = $request->getVal( 'ip', $par );
55 $this->target
= trim( $request->getVal( 'wpTarget', $par ) );
57 $this->options
= $request->getArray( 'wpOptions', [] );
58 $this->blockType
= $request->getVal( 'blockType' );
60 $action = $request->getText( 'action' );
62 if ( $action == 'unblock' ||
$action == 'submit' && $request->wasPosted() ) {
63 # B/C @since 1.18: Unblock interface is now at Special:Unblock
64 $title = SpecialPage
::getTitleFor( 'Unblock', $this->target
);
65 $out->redirect( $title->getFullURL() );
70 # setup BlockListPager here to get the actual default Limit
71 $pager = $this->getBlockListPager();
73 # Just show the block list
77 'label-message' => 'ipaddressorusername',
80 'default' => $this->target
,
83 'type' => 'multiselect',
84 'options-messages' => [
85 'blocklist-tempblocks' => 'tempblocks',
86 'blocklist-indefblocks' => 'indefblocks',
87 'blocklist-userblocks' => 'userblocks',
88 'blocklist-addressblocks' => 'addressblocks',
89 'blocklist-rangeblocks' => 'rangeblocks',
95 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
96 $fields['BlockType'] = [
98 'label-message' => 'blocklist-type',
100 $this->msg( 'blocklist-type-opt-all' )->escaped() => '',
101 $this->msg( 'blocklist-type-opt-sitewide' )->escaped() => 'sitewide',
102 $this->msg( 'blocklist-type-opt-partial' )->escaped() => 'partial',
104 'name' => 'blockType',
105 'cssclass' => 'mw-field-block-type',
110 'type' => 'limitselect',
111 'label-message' => 'table_pager_limit_label',
112 'options' => $pager->getLimitSelectList(),
114 'default' => $pager->getLimit(),
115 'cssclass' => $this->getConfig()->get( 'EnablePartialBlocks' ) ?
116 'mw-field-limit mw-has-field-block-type' :
120 $context = new DerivativeContext( $this->getContext() );
121 $context->setTitle( $this->getPageTitle() ); // Remove subpage
122 $form = HTMLForm
::factory( 'ooui', $fields, $context );
125 ->setFormIdentifier( 'blocklist' )
126 ->setWrapperLegendMsg( 'ipblocklist-legend' )
127 ->setSubmitTextMsg( 'ipblocklist-submit' )
129 ->displayForm( false );
131 $this->showList( $pager );
135 * Setup a new BlockListPager instance.
136 * @return BlockListPager
138 protected function getBlockListPager() {
140 $db = $this->getDB();
141 # Is the user allowed to see hidden blocks?
142 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
143 $conds['ipb_deleted'] = 0;
146 if ( $this->target
!== '' ) {
147 list( $target, $type ) = DatabaseBlock
::parseTarget( $this->target
);
150 case DatabaseBlock
::TYPE_ID
:
151 case DatabaseBlock
::TYPE_AUTO
:
152 $conds['ipb_id'] = $target;
155 case DatabaseBlock
::TYPE_IP
:
156 case DatabaseBlock
::TYPE_RANGE
:
157 list( $start, $end ) = IP
::parseRange( $target );
158 $conds[] = $db->makeList(
160 'ipb_address' => $target,
161 DatabaseBlock
::getRangeCond( $start, $end )
165 $conds['ipb_auto'] = 0;
168 case DatabaseBlock
::TYPE_USER
:
169 $conds['ipb_address'] = $target->getName();
170 $conds['ipb_auto'] = 0;
176 if ( in_array( 'userblocks', $this->options
) ) {
177 $conds['ipb_user'] = 0;
179 if ( in_array( 'addressblocks', $this->options
) ) {
180 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
182 if ( in_array( 'rangeblocks', $this->options
) ) {
183 $conds[] = "ipb_range_end = ipb_range_start";
186 $hideTemp = in_array( 'tempblocks', $this->options
);
187 $hideIndef = in_array( 'indefblocks', $this->options
);
188 if ( $hideTemp && $hideIndef ) {
189 // If both types are hidden, ensure query doesn't produce any results
191 } elseif ( $hideTemp ) {
192 $conds['ipb_expiry'] = $db->getInfinity();
193 } elseif ( $hideIndef ) {
194 $conds[] = "ipb_expiry != " . $db->addQuotes( $db->getInfinity() );
197 if ( $this->blockType
=== 'sitewide' ) {
198 $conds['ipb_sitewide'] = 1;
199 } elseif ( $this->blockType
=== 'partial' ) {
200 $conds['ipb_sitewide'] = 0;
203 return new BlockListPager( $this, $conds );
207 * Show the list of blocked accounts matching the actual filter.
208 * @param BlockListPager $pager The BlockListPager instance for this page
210 protected function showList( BlockListPager
$pager ) {
211 $out = $this->getOutput();
213 # Check for other blocks, i.e. global/tor blocks
214 $otherBlockLink = [];
215 Hooks
::run( 'OtherBlockLogLink', [ &$otherBlockLink, $this->target
] );
217 # Show additional header for the local block only when other blocks exists.
218 # Not necessary in a standard installation without such extensions enabled
219 if ( count( $otherBlockLink ) ) {
221 Html
::element( 'h2', [], $this->msg( 'ipblocklist-localblock' )->text() ) . "\n"
225 if ( $pager->getNumRows() ) {
226 $out->addParserOutputContent( $pager->getFullOutput() );
227 } elseif ( $this->target
) {
228 $out->addWikiMsg( 'ipblocklist-no-results' );
230 $out->addWikiMsg( 'ipblocklist-empty' );
233 if ( count( $otherBlockLink ) ) {
238 $this->msg( 'ipblocklist-otherblocks', count( $otherBlockLink ) )->parse()
242 foreach ( $otherBlockLink as $link ) {
243 $list .= Html
::rawElement( 'li', [], $link ) . "\n";
245 $out->addHTML( Html
::rawElement(
247 [ 'class' => 'mw-ipblocklist-otherblocks' ],
253 protected function getGroupName() {
258 * Return a IDatabase object for reading
262 protected function getDB() {
263 return wfGetDB( DB_REPLICA
);