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