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