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