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