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