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
;
28 * A special page that lists existing blocks
30 * @ingroup SpecialPage
32 class SpecialBlockList
extends SpecialPage
{
39 function __construct() {
40 parent
::__construct( 'BlockList' );
44 * @param string|null $par Title fragment
46 public function execute( $par ) {
48 $this->outputHeader();
49 $this->addHelpLink( 'Help:Blocking_users' );
50 $out = $this->getOutput();
51 $out->setPageTitle( $this->msg( 'ipblocklist' ) );
52 $out->addModuleStyles( [ 'mediawiki.special' ] );
54 $request = $this->getRequest();
55 $par = $request->getVal( 'ip', $par );
56 $this->target
= trim( $request->getVal( 'wpTarget', $par ) );
58 $this->options
= $request->getArray( 'wpOptions', [] );
59 $this->blockType
= $request->getVal( 'blockType' );
61 $action = $request->getText( 'action' );
63 if ( $action == 'unblock' ||
$action == 'submit' && $request->wasPosted() ) {
64 # B/C @since 1.18: Unblock interface is now at Special:Unblock
65 $title = SpecialPage
::getTitleFor( 'Unblock', $this->target
);
66 $out->redirect( $title->getFullURL() );
71 # setup BlockListPager here to get the actual default Limit
72 $pager = $this->getBlockListPager();
74 # Just show the block list
78 'label-message' => 'ipaddressorusername',
81 'default' => $this->target
,
84 'type' => 'multiselect',
85 'options-messages' => [
86 'blocklist-tempblocks' => 'tempblocks',
87 'blocklist-indefblocks' => 'indefblocks',
88 'blocklist-userblocks' => 'userblocks',
89 'blocklist-addressblocks' => 'addressblocks',
90 'blocklist-rangeblocks' => 'rangeblocks',
96 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
97 $fields['BlockType'] = [
99 'label-message' => 'blocklist-type',
101 $this->msg( 'blocklist-type-opt-all' )->escaped() => '',
102 $this->msg( 'blocklist-type-opt-sitewide' )->escaped() => 'sitewide',
103 $this->msg( 'blocklist-type-opt-partial' )->escaped() => 'partial',
105 'name' => 'blockType',
106 'cssclass' => 'mw-field-block-type',
111 'type' => 'limitselect',
112 'label-message' => 'table_pager_limit_label',
113 'options' => $pager->getLimitSelectList(),
115 'default' => $pager->getLimit(),
116 'cssclass' => $this->getConfig()->get( 'EnablePartialBlocks' ) ?
117 'mw-field-limit mw-has-field-block-type' :
121 $context = new DerivativeContext( $this->getContext() );
122 $context->setTitle( $this->getPageTitle() ); // Remove subpage
123 $form = HTMLForm
::factory( 'ooui', $fields, $context );
126 ->setFormIdentifier( 'blocklist' )
127 ->setWrapperLegendMsg( 'ipblocklist-legend' )
128 ->setSubmitTextMsg( 'ipblocklist-submit' )
130 ->displayForm( false );
132 $this->showList( $pager );
136 * Setup a new BlockListPager instance.
137 * @return BlockListPager
139 protected function getBlockListPager() {
141 $db = $this->getDB();
142 # Is the user allowed to see hidden blocks?
143 if ( !MediaWikiServices
::getInstance()
144 ->getPermissionManager()
145 ->userHasRight( $this->getUser(), 'hideuser' )
147 $conds['ipb_deleted'] = 0;
150 if ( $this->target
!== '' ) {
151 list( $target, $type ) = DatabaseBlock
::parseTarget( $this->target
);
154 case DatabaseBlock
::TYPE_ID
:
155 case DatabaseBlock
::TYPE_AUTO
:
156 $conds['ipb_id'] = $target;
159 case DatabaseBlock
::TYPE_IP
:
160 case DatabaseBlock
::TYPE_RANGE
:
161 list( $start, $end ) = IP
::parseRange( $target );
162 $conds[] = $db->makeList(
164 'ipb_address' => $target,
165 DatabaseBlock
::getRangeCond( $start, $end )
169 $conds['ipb_auto'] = 0;
172 case DatabaseBlock
::TYPE_USER
:
173 $conds['ipb_address'] = $target->getName();
174 $conds['ipb_auto'] = 0;
180 if ( in_array( 'userblocks', $this->options
) ) {
181 $conds['ipb_user'] = 0;
183 if ( in_array( 'addressblocks', $this->options
) ) {
184 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
186 if ( in_array( 'rangeblocks', $this->options
) ) {
187 $conds[] = "ipb_range_end = ipb_range_start";
190 $hideTemp = in_array( 'tempblocks', $this->options
);
191 $hideIndef = in_array( 'indefblocks', $this->options
);
192 if ( $hideTemp && $hideIndef ) {
193 // If both types are hidden, ensure query doesn't produce any results
195 } elseif ( $hideTemp ) {
196 $conds['ipb_expiry'] = $db->getInfinity();
197 } elseif ( $hideIndef ) {
198 $conds[] = "ipb_expiry != " . $db->addQuotes( $db->getInfinity() );
201 if ( $this->blockType
=== 'sitewide' ) {
202 $conds['ipb_sitewide'] = 1;
203 } elseif ( $this->blockType
=== 'partial' ) {
204 $conds['ipb_sitewide'] = 0;
207 return new BlockListPager( $this, $conds );
211 * Show the list of blocked accounts matching the actual filter.
212 * @param BlockListPager $pager The BlockListPager instance for this page
214 protected function showList( BlockListPager
$pager ) {
215 $out = $this->getOutput();
217 # Check for other blocks, i.e. global/tor blocks
218 $otherBlockLink = [];
219 Hooks
::run( 'OtherBlockLogLink', [ &$otherBlockLink, $this->target
] );
221 # Show additional header for the local block only when other blocks exists.
222 # Not necessary in a standard installation without such extensions enabled
223 if ( count( $otherBlockLink ) ) {
225 Html
::element( 'h2', [], $this->msg( 'ipblocklist-localblock' )->text() ) . "\n"
229 if ( $pager->getNumRows() ) {
230 $out->addParserOutputContent( $pager->getFullOutput() );
231 } elseif ( $this->target
) {
232 $out->addWikiMsg( 'ipblocklist-no-results' );
234 $out->addWikiMsg( 'ipblocklist-empty' );
237 if ( count( $otherBlockLink ) ) {
242 $this->msg( 'ipblocklist-otherblocks', count( $otherBlockLink ) )->parse()
246 foreach ( $otherBlockLink as $link ) {
247 $list .= Html
::rawElement( 'li', [], $link ) . "\n";
249 $out->addHTML( Html
::rawElement(
251 [ 'class' => 'mw-ipblocklist-otherblocks' ],
257 protected function getGroupName() {
262 * Return a IDatabase object for reading
266 protected function getDB() {
267 return wfGetDB( DB_REPLICA
);