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