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