Merge "HtmlForm: Use mw-ui-checkbox only if wgUseMediaWikiUIEverywhere is enabled"
[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 /**
25 * A special page that lists existing blocks
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialBlockList extends SpecialPage {
30 protected $target;
31
32 protected $options;
33
34 function __construct() {
35 parent::__construct( 'BlockList' );
36 }
37
38 /**
39 * Main execution point
40 *
41 * @param string $par Title fragment
42 */
43 public function execute( $par ) {
44 $this->setHeaders();
45 $this->outputHeader();
46 $out = $this->getOutput();
47 $lang = $this->getLanguage();
48 $out->setPageTitle( $this->msg( 'ipblocklist' ) );
49 $out->addModuleStyles( 'mediawiki.special' );
50
51 $request = $this->getRequest();
52 $par = $request->getVal( 'ip', $par );
53 $this->target = trim( $request->getVal( 'wpTarget', $par ) );
54
55 $this->options = $request->getArray( 'wpOptions', array() );
56
57 $action = $request->getText( 'action' );
58
59 if ( $action == 'unblock' || $action == 'submit' && $request->wasPosted() ) {
60 # B/C @since 1.18: Unblock interface is now at Special:Unblock
61 $title = SpecialPage::getTitleFor( 'Unblock', $this->target );
62 $out->redirect( $title->getFullURL() );
63
64 return;
65 }
66
67 # Just show the block list
68 $fields = array(
69 'Target' => array(
70 'type' => 'text',
71 'label-message' => 'ipaddressorusername',
72 'tabindex' => '1',
73 'size' => '45',
74 'default' => $this->target,
75 ),
76 'Options' => array(
77 'type' => 'multiselect',
78 'options' => array(
79 $this->msg( 'blocklist-userblocks' )->text() => 'userblocks',
80 $this->msg( 'blocklist-tempblocks' )->text() => 'tempblocks',
81 $this->msg( 'blocklist-addressblocks' )->text() => 'addressblocks',
82 $this->msg( 'blocklist-rangeblocks' )->text() => 'rangeblocks',
83 ),
84 'flatlist' => true,
85 ),
86 'Limit' => array(
87 'type' => 'limitselect',
88 'label-message' => 'table_pager_limit_label',
89 'options' => array(
90 $lang->formatNum( 20 ) => 20,
91 $lang->formatNum( 50 ) => 50,
92 $lang->formatNum( 100 ) => 100,
93 $lang->formatNum( 250 ) => 250,
94 $lang->formatNum( 500 ) => 500,
95 ),
96 'name' => 'limit',
97 'default' => 50,
98 ),
99 );
100 $context = new DerivativeContext( $this->getContext() );
101 $context->setTitle( $this->getPageTitle() ); // Remove subpage
102 $form = new HTMLForm( $fields, $context );
103 $form->setMethod( 'get' );
104 $form->setWrapperLegendMsg( 'ipblocklist-legend' );
105 $form->setSubmitTextMsg( 'ipblocklist-submit' );
106 $form->setSubmitProgressive();
107 $form->prepareForm();
108
109 $form->displayForm( '' );
110 $this->showList();
111 }
112
113 function showList() {
114 # Purge expired entries on one in every 10 queries
115 if ( !mt_rand( 0, 10 ) ) {
116 Block::purgeExpired();
117 }
118
119 $conds = array();
120 # Is the user allowed to see hidden blocks?
121 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
122 $conds['ipb_deleted'] = 0;
123 }
124
125 if ( $this->target !== '' ) {
126 list( $target, $type ) = Block::parseTarget( $this->target );
127
128 switch ( $type ) {
129 case Block::TYPE_ID:
130 case Block::TYPE_AUTO:
131 $conds['ipb_id'] = $target;
132 break;
133
134 case Block::TYPE_IP:
135 case Block::TYPE_RANGE:
136 list( $start, $end ) = IP::parseRange( $target );
137 $dbr = wfGetDB( DB_SLAVE );
138 $conds[] = $dbr->makeList(
139 array(
140 'ipb_address' => $target,
141 Block::getRangeCond( $start, $end )
142 ),
143 LIST_OR
144 );
145 $conds['ipb_auto'] = 0;
146 break;
147
148 case Block::TYPE_USER:
149 $conds['ipb_address'] = $target->getName();
150 $conds['ipb_auto'] = 0;
151 break;
152 }
153 }
154
155 # Apply filters
156 if ( in_array( 'userblocks', $this->options ) ) {
157 $conds['ipb_user'] = 0;
158 }
159 if ( in_array( 'tempblocks', $this->options ) ) {
160 $conds['ipb_expiry'] = 'infinity';
161 }
162 if ( in_array( 'addressblocks', $this->options ) ) {
163 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
164 }
165 if ( in_array( 'rangeblocks', $this->options ) ) {
166 $conds[] = "ipb_range_end = ipb_range_start";
167 }
168
169 # Check for other blocks, i.e. global/tor blocks
170 $otherBlockLink = array();
171 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockLink, $this->target ) );
172
173 $out = $this->getOutput();
174
175 # Show additional header for the local block only when other blocks exists.
176 # Not necessary in a standard installation without such extensions enabled
177 if ( count( $otherBlockLink ) ) {
178 $out->addHTML(
179 Html::element( 'h2', array(), $this->msg( 'ipblocklist-localblock' )->text() ) . "\n"
180 );
181 }
182
183 $pager = new BlockListPager( $this, $conds );
184 if ( $pager->getNumRows() ) {
185 $out->addParserOutputContent( $pager->getFullOutput() );
186 } elseif ( $this->target ) {
187 $out->addWikiMsg( 'ipblocklist-no-results' );
188 } else {
189 $out->addWikiMsg( 'ipblocklist-empty' );
190 }
191
192 if ( count( $otherBlockLink ) ) {
193 $out->addHTML(
194 Html::rawElement(
195 'h2',
196 array(),
197 $this->msg( 'ipblocklist-otherblocks', count( $otherBlockLink ) )->parse()
198 ) . "\n"
199 );
200 $list = '';
201 foreach ( $otherBlockLink as $link ) {
202 $list .= Html::rawElement( 'li', array(), $link ) . "\n";
203 }
204 $out->addHTML( Html::rawElement(
205 'ul',
206 array( 'class' => 'mw-ipblocklist-otherblocks' ),
207 $list
208 ) . "\n" );
209 }
210 }
211
212 protected function getGroupName() {
213 return 'users';
214 }
215 }
216
217 class BlockListPager extends TablePager {
218 protected $conds;
219 protected $page;
220
221 /**
222 * @param SpecialPage $page
223 * @param array $conds
224 */
225 function __construct( $page, $conds ) {
226 $this->page = $page;
227 $this->conds = $conds;
228 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
229 parent::__construct( $page->getContext() );
230 }
231
232 function getFieldNames() {
233 static $headers = null;
234
235 if ( $headers === null ) {
236 $headers = array(
237 'ipb_timestamp' => 'blocklist-timestamp',
238 'ipb_target' => 'blocklist-target',
239 'ipb_expiry' => 'blocklist-expiry',
240 'ipb_by' => 'blocklist-by',
241 'ipb_params' => 'blocklist-params',
242 'ipb_reason' => 'blocklist-reason',
243 );
244 foreach ( $headers as $key => $val ) {
245 $headers[$key] = $this->msg( $val )->text();
246 }
247 }
248
249 return $headers;
250 }
251
252 function formatValue( $name, $value ) {
253 static $msg = null;
254 if ( $msg === null ) {
255 $msg = array(
256 'anononlyblock',
257 'createaccountblock',
258 'noautoblockblock',
259 'emailblock',
260 'blocklist-nousertalk',
261 'unblocklink',
262 'change-blocklink',
263 'infiniteblock',
264 );
265 $msg = array_combine( $msg, array_map( array( $this, 'msg' ), $msg ) );
266 }
267
268 /** @var $row object */
269 $row = $this->mCurrentRow;
270
271 $formatted = '';
272
273 switch ( $name ) {
274 case 'ipb_timestamp':
275 $formatted = $this->getLanguage()->userTimeAndDate( $value, $this->getUser() );
276 break;
277
278 case 'ipb_target':
279 if ( $row->ipb_auto ) {
280 $formatted = $this->msg( 'autoblockid', $row->ipb_id )->parse();
281 } else {
282 list( $target, $type ) = Block::parseTarget( $row->ipb_address );
283 switch ( $type ) {
284 case Block::TYPE_USER:
285 case Block::TYPE_IP:
286 $formatted = Linker::userLink( $target->getId(), $target );
287 $formatted .= Linker::userToolLinks(
288 $target->getId(),
289 $target,
290 false,
291 Linker::TOOL_LINKS_NOBLOCK
292 );
293 break;
294 case Block::TYPE_RANGE:
295 $formatted = htmlspecialchars( $target );
296 }
297 }
298 break;
299
300 case 'ipb_expiry':
301 $formatted = $this->getLanguage()->formatExpiry( $value, /* User preference timezone */true );
302 if ( $this->getUser()->isAllowed( 'block' ) ) {
303 if ( $row->ipb_auto ) {
304 $links[] = Linker::linkKnown(
305 SpecialPage::getTitleFor( 'Unblock' ),
306 $msg['unblocklink'],
307 array(),
308 array( 'wpTarget' => "#{$row->ipb_id}" )
309 );
310 } else {
311 $links[] = Linker::linkKnown(
312 SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
313 $msg['unblocklink']
314 );
315 $links[] = Linker::linkKnown(
316 SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
317 $msg['change-blocklink']
318 );
319 }
320 $formatted .= ' ' . Html::rawElement(
321 'span',
322 array( 'class' => 'mw-blocklist-actions' ),
323 $this->msg( 'parentheses' )->rawParams(
324 $this->getLanguage()->pipeList( $links ) )->escaped()
325 );
326 }
327 break;
328
329 case 'ipb_by':
330 if ( isset( $row->by_user_name ) ) {
331 $formatted = Linker::userLink( $value, $row->by_user_name );
332 $formatted .= Linker::userToolLinks( $value, $row->by_user_name );
333 } else {
334 $formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user?
335 }
336 break;
337
338 case 'ipb_reason':
339 $formatted = Linker::formatComment( $value );
340 break;
341
342 case 'ipb_params':
343 $properties = array();
344 if ( $row->ipb_anon_only ) {
345 $properties[] = $msg['anononlyblock'];
346 }
347 if ( $row->ipb_create_account ) {
348 $properties[] = $msg['createaccountblock'];
349 }
350 if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
351 $properties[] = $msg['noautoblockblock'];
352 }
353
354 if ( $row->ipb_block_email ) {
355 $properties[] = $msg['emailblock'];
356 }
357
358 if ( !$row->ipb_allow_usertalk ) {
359 $properties[] = $msg['blocklist-nousertalk'];
360 }
361
362 $formatted = $this->getLanguage()->commaList( $properties );
363 break;
364
365 default:
366 $formatted = "Unable to format $name";
367 break;
368 }
369
370 return $formatted;
371 }
372
373 function getQueryInfo() {
374 $info = array(
375 'tables' => array( 'ipblocks', 'user' ),
376 'fields' => array(
377 'ipb_id',
378 'ipb_address',
379 'ipb_user',
380 'ipb_by',
381 'ipb_by_text',
382 'by_user_name' => 'user_name',
383 'ipb_reason',
384 'ipb_timestamp',
385 'ipb_auto',
386 'ipb_anon_only',
387 'ipb_create_account',
388 'ipb_enable_autoblock',
389 'ipb_expiry',
390 'ipb_range_start',
391 'ipb_range_end',
392 'ipb_deleted',
393 'ipb_block_email',
394 'ipb_allow_usertalk',
395 ),
396 'conds' => $this->conds,
397 'join_conds' => array( 'user' => array( 'LEFT JOIN', 'user_id = ipb_by' ) )
398 );
399
400 # Is the user allowed to see hidden blocks?
401 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
402 $info['conds']['ipb_deleted'] = 0;
403 }
404
405 return $info;
406 }
407
408 public function getTableClass() {
409 return parent::getTableClass() . ' mw-blocklist';
410 }
411
412 function getIndexField() {
413 return 'ipb_timestamp';
414 }
415
416 function getDefaultSort() {
417 return 'ipb_timestamp';
418 }
419
420 function isFieldSortable( $name ) {
421 return false;
422 }
423
424 /**
425 * Do a LinkBatch query to minimise database load when generating all these links
426 * @param ResultWrapper $result
427 */
428 function preprocessResults( $result ) {
429 wfProfileIn( __METHOD__ );
430 # Do a link batch query
431 $lb = new LinkBatch;
432 $lb->setCaller( __METHOD__ );
433
434 $userids = array();
435
436 foreach ( $result as $row ) {
437 $userids[] = $row->ipb_by;
438
439 # Usernames and titles are in fact related by a simple substitution of space -> underscore
440 # The last few lines of Title::secureAndSplit() tell the story.
441 $name = str_replace( ' ', '_', $row->ipb_address );
442 $lb->add( NS_USER, $name );
443 $lb->add( NS_USER_TALK, $name );
444 }
445
446 $ua = UserArray::newFromIDs( $userids );
447 foreach ( $ua as $user ) {
448 $name = str_replace( ' ', '_', $user->getName() );
449 $lb->add( NS_USER, $name );
450 $lb->add( NS_USER_TALK, $name );
451 }
452
453 $lb->execute();
454 wfProfileOut( __METHOD__ );
455 }
456 }