638535940da8c6a18538c8b52871257b45c89045
[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-tempblocks' => 'tempblocks',
86 'blocklist-indefblocks' => 'indefblocks',
87 'blocklist-userblocks' => 'userblocks',
88 'blocklist-addressblocks' => 'addressblocks',
89 'blocklist-rangeblocks' => 'rangeblocks',
90 ],
91 'flatlist' => true,
92 ],
93 ];
94
95 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
96 $fields['BlockType'] = [
97 'type' => 'select',
98 'label-message' => 'blocklist-type',
99 'options' => [
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',
103 ],
104 'name' => 'blockType',
105 'cssclass' => 'mw-field-block-type',
106 ];
107 }
108
109 $fields['Limit'] = [
110 'type' => 'limitselect',
111 'label-message' => 'table_pager_limit_label',
112 'options' => $pager->getLimitSelectList(),
113 'name' => 'limit',
114 'default' => $pager->getLimit(),
115 'cssclass' => $this->getConfig()->get( 'EnablePartialBlocks' ) ?
116 'mw-field-limit mw-has-field-block-type' :
117 'mw-field-limit',
118 ];
119
120 $context = new DerivativeContext( $this->getContext() );
121 $context->setTitle( $this->getPageTitle() ); // Remove subpage
122 $form = HTMLForm::factory( 'ooui', $fields, $context );
123 $form
124 ->setMethod( 'get' )
125 ->setFormIdentifier( 'blocklist' )
126 ->setWrapperLegendMsg( 'ipblocklist-legend' )
127 ->setSubmitTextMsg( 'ipblocklist-submit' )
128 ->prepareForm()
129 ->displayForm( false );
130
131 $this->showList( $pager );
132 }
133
134 /**
135 * Setup a new BlockListPager instance.
136 * @return BlockListPager
137 */
138 protected function getBlockListPager() {
139 $conds = [];
140 $db = $this->getDB();
141 # Is the user allowed to see hidden blocks?
142 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
143 $conds['ipb_deleted'] = 0;
144 }
145
146 if ( $this->target !== '' ) {
147 list( $target, $type ) = DatabaseBlock::parseTarget( $this->target );
148
149 switch ( $type ) {
150 case DatabaseBlock::TYPE_ID:
151 case DatabaseBlock::TYPE_AUTO:
152 $conds['ipb_id'] = $target;
153 break;
154
155 case DatabaseBlock::TYPE_IP:
156 case DatabaseBlock::TYPE_RANGE:
157 list( $start, $end ) = IP::parseRange( $target );
158 $conds[] = $db->makeList(
159 [
160 'ipb_address' => $target,
161 DatabaseBlock::getRangeCond( $start, $end )
162 ],
163 LIST_OR
164 );
165 $conds['ipb_auto'] = 0;
166 break;
167
168 case DatabaseBlock::TYPE_USER:
169 $conds['ipb_address'] = $target->getName();
170 $conds['ipb_auto'] = 0;
171 break;
172 }
173 }
174
175 # Apply filters
176 if ( in_array( 'userblocks', $this->options ) ) {
177 $conds['ipb_user'] = 0;
178 }
179 if ( in_array( 'addressblocks', $this->options ) ) {
180 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
181 }
182 if ( in_array( 'rangeblocks', $this->options ) ) {
183 $conds[] = "ipb_range_end = ipb_range_start";
184 }
185
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
190 $conds[] = '1=0';
191 } elseif ( $hideTemp ) {
192 $conds['ipb_expiry'] = $db->getInfinity();
193 } elseif ( $hideIndef ) {
194 $conds[] = "ipb_expiry != " . $db->addQuotes( $db->getInfinity() );
195 }
196
197 if ( $this->blockType === 'sitewide' ) {
198 $conds['ipb_sitewide'] = 1;
199 } elseif ( $this->blockType === 'partial' ) {
200 $conds['ipb_sitewide'] = 0;
201 }
202
203 return new BlockListPager( $this, $conds );
204 }
205
206 /**
207 * Show the list of blocked accounts matching the actual filter.
208 * @param BlockListPager $pager The BlockListPager instance for this page
209 */
210 protected function showList( BlockListPager $pager ) {
211 $out = $this->getOutput();
212
213 # Check for other blocks, i.e. global/tor blocks
214 $otherBlockLink = [];
215 Hooks::run( 'OtherBlockLogLink', [ &$otherBlockLink, $this->target ] );
216
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 ) ) {
220 $out->addHTML(
221 Html::element( 'h2', [], $this->msg( 'ipblocklist-localblock' )->text() ) . "\n"
222 );
223 }
224
225 if ( $pager->getNumRows() ) {
226 $out->addParserOutputContent( $pager->getFullOutput() );
227 } elseif ( $this->target ) {
228 $out->addWikiMsg( 'ipblocklist-no-results' );
229 } else {
230 $out->addWikiMsg( 'ipblocklist-empty' );
231 }
232
233 if ( count( $otherBlockLink ) ) {
234 $out->addHTML(
235 Html::rawElement(
236 'h2',
237 [],
238 $this->msg( 'ipblocklist-otherblocks', count( $otherBlockLink ) )->parse()
239 ) . "\n"
240 );
241 $list = '';
242 foreach ( $otherBlockLink as $link ) {
243 $list .= Html::rawElement( 'li', [], $link ) . "\n";
244 }
245 $out->addHTML( Html::rawElement(
246 'ul',
247 [ 'class' => 'mw-ipblocklist-otherblocks' ],
248 $list
249 ) . "\n" );
250 }
251 }
252
253 protected function getGroupName() {
254 return 'users';
255 }
256
257 /**
258 * Return a IDatabase object for reading
259 *
260 * @return IDatabase
261 */
262 protected function getDB() {
263 return wfGetDB( DB_REPLICA );
264 }
265 }