output: Narrow Title type hint to LinkTarget
[lhc/web/wiklou.git] / includes / specials / SpecialBlockList.php
1 <?php
2 /**
3 * Implements Special:BlockList
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 use MediaWiki\Block\DatabaseBlock;
25
26 /**
27 * A special page that lists existing blocks
28 *
29 * @ingroup SpecialPage
30 */
31 class SpecialBlockList extends SpecialPage {
32 protected $target;
33
34 protected $options;
35
36 protected $blockType;
37
38 function __construct() {
39 parent::__construct( 'BlockList' );
40 }
41
42 /**
43 * @param string|null $par Title fragment
44 */
45 public function execute( $par ) {
46 $this->setHeaders();
47 $this->outputHeader();
48 $this->addHelpLink( 'Help:Blocking_users' );
49 $out = $this->getOutput();
50 $out->setPageTitle( $this->msg( 'ipblocklist' ) );
51 $out->addModuleStyles( [ 'mediawiki.special' ] );
52
53 $request = $this->getRequest();
54 $par = $request->getVal( 'ip', $par );
55 $this->target = trim( $request->getVal( 'wpTarget', $par ) );
56
57 $this->options = $request->getArray( 'wpOptions', [] );
58 $this->blockType = $request->getVal( 'blockType' );
59
60 $action = $request->getText( 'action' );
61
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() );
66
67 return;
68 }
69
70 # setup BlockListPager here to get the actual default Limit
71 $pager = $this->getBlockListPager();
72
73 # Just show the block list
74 $fields = [
75 'Target' => [
76 'type' => 'user',
77 'label-message' => 'ipaddressorusername',
78 'tabindex' => '1',
79 'size' => '45',
80 'default' => $this->target,
81 ],
82 'Options' => [
83 'type' => 'multiselect',
84 'options-messages' => [
85 'blocklist-userblocks' => 'userblocks',
86 'blocklist-tempblocks' => 'tempblocks',
87 'blocklist-addressblocks' => 'addressblocks',
88 'blocklist-rangeblocks' => 'rangeblocks',
89 ],
90 'flatlist' => true,
91 ],
92 ];
93
94 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
95 $fields['BlockType'] = [
96 'type' => 'select',
97 'label-message' => 'blocklist-type',
98 'options' => [
99 $this->msg( 'blocklist-type-opt-all' )->escaped() => '',
100 $this->msg( 'blocklist-type-opt-sitewide' )->escaped() => 'sitewide',
101 $this->msg( 'blocklist-type-opt-partial' )->escaped() => 'partial',
102 ],
103 'name' => 'blockType',
104 'cssclass' => 'mw-field-block-type',
105 ];
106 }
107
108 $fields['Limit'] = [
109 'type' => 'limitselect',
110 'label-message' => 'table_pager_limit_label',
111 'options' => $pager->getLimitSelectList(),
112 'name' => 'limit',
113 'default' => $pager->getLimit(),
114 'cssclass' => $this->getConfig()->get( 'EnablePartialBlocks' ) ?
115 'mw-field-limit mw-has-field-block-type' :
116 'mw-field-limit',
117 ];
118
119 $context = new DerivativeContext( $this->getContext() );
120 $context->setTitle( $this->getPageTitle() ); // Remove subpage
121 $form = HTMLForm::factory( 'ooui', $fields, $context );
122 $form
123 ->setMethod( 'get' )
124 ->setFormIdentifier( 'blocklist' )
125 ->setWrapperLegendMsg( 'ipblocklist-legend' )
126 ->setSubmitTextMsg( 'ipblocklist-submit' )
127 ->prepareForm()
128 ->displayForm( false );
129
130 $this->showList( $pager );
131 }
132
133 /**
134 * Setup a new BlockListPager instance.
135 * @return BlockListPager
136 */
137 protected function getBlockListPager() {
138 $conds = [];
139 # Is the user allowed to see hidden blocks?
140 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
141 $conds['ipb_deleted'] = 0;
142 }
143
144 if ( $this->target !== '' ) {
145 list( $target, $type ) = DatabaseBlock::parseTarget( $this->target );
146
147 switch ( $type ) {
148 case DatabaseBlock::TYPE_ID:
149 case DatabaseBlock::TYPE_AUTO:
150 $conds['ipb_id'] = $target;
151 break;
152
153 case DatabaseBlock::TYPE_IP:
154 case DatabaseBlock::TYPE_RANGE:
155 list( $start, $end ) = IP::parseRange( $target );
156 $conds[] = wfGetDB( DB_REPLICA )->makeList(
157 [
158 'ipb_address' => $target,
159 DatabaseBlock::getRangeCond( $start, $end )
160 ],
161 LIST_OR
162 );
163 $conds['ipb_auto'] = 0;
164 break;
165
166 case DatabaseBlock::TYPE_USER:
167 $conds['ipb_address'] = $target->getName();
168 $conds['ipb_auto'] = 0;
169 break;
170 }
171 }
172
173 # Apply filters
174 if ( in_array( 'userblocks', $this->options ) ) {
175 $conds['ipb_user'] = 0;
176 }
177 if ( in_array( 'tempblocks', $this->options ) ) {
178 $conds['ipb_expiry'] = 'infinity';
179 }
180 if ( in_array( 'addressblocks', $this->options ) ) {
181 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
182 }
183 if ( in_array( 'rangeblocks', $this->options ) ) {
184 $conds[] = "ipb_range_end = ipb_range_start";
185 }
186
187 if ( $this->blockType === 'sitewide' ) {
188 $conds[] = 'ipb_sitewide = 1';
189 } elseif ( $this->blockType === 'partial' ) {
190 $conds[] = 'ipb_sitewide = 0';
191 }
192
193 return new BlockListPager( $this, $conds );
194 }
195
196 /**
197 * Show the list of blocked accounts matching the actual filter.
198 * @param BlockListPager $pager The BlockListPager instance for this page
199 */
200 protected function showList( BlockListPager $pager ) {
201 $out = $this->getOutput();
202
203 # Check for other blocks, i.e. global/tor blocks
204 $otherBlockLink = [];
205 Hooks::run( 'OtherBlockLogLink', [ &$otherBlockLink, $this->target ] );
206
207 # Show additional header for the local block only when other blocks exists.
208 # Not necessary in a standard installation without such extensions enabled
209 if ( count( $otherBlockLink ) ) {
210 $out->addHTML(
211 Html::element( 'h2', [], $this->msg( 'ipblocklist-localblock' )->text() ) . "\n"
212 );
213 }
214
215 if ( $pager->getNumRows() ) {
216 $out->addParserOutputContent( $pager->getFullOutput() );
217 } elseif ( $this->target ) {
218 $out->addWikiMsg( 'ipblocklist-no-results' );
219 } else {
220 $out->addWikiMsg( 'ipblocklist-empty' );
221 }
222
223 if ( count( $otherBlockLink ) ) {
224 $out->addHTML(
225 Html::rawElement(
226 'h2',
227 [],
228 $this->msg( 'ipblocklist-otherblocks', count( $otherBlockLink ) )->parse()
229 ) . "\n"
230 );
231 $list = '';
232 foreach ( $otherBlockLink as $link ) {
233 $list .= Html::rawElement( 'li', [], $link ) . "\n";
234 }
235 $out->addHTML( Html::rawElement(
236 'ul',
237 [ 'class' => 'mw-ipblocklist-otherblocks' ],
238 $list
239 ) . "\n" );
240 }
241 }
242
243 protected function getGroupName() {
244 return 'users';
245 }
246 }