Merge "Adding aliases for speacial pages in Hebrew"
[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 'infiniteblock',
266 );
267 $msg = array_combine( $msg, array_map( array( $this, 'msg' ), $msg ) );
268 }
269
270 /** @var $row object */
271 $row = $this->mCurrentRow;
272
273 $formatted = '';
274
275 switch ( $name ) {
276 case 'ipb_timestamp':
277 $formatted = $this->getLanguage()->userTimeAndDate( $value, $this->getUser() );
278 break;
279
280 case 'ipb_target':
281 if ( $row->ipb_auto ) {
282 $formatted = $this->msg( 'autoblockid', $row->ipb_id )->parse();
283 } else {
284 list( $target, $type ) = Block::parseTarget( $row->ipb_address );
285 switch ( $type ) {
286 case Block::TYPE_USER:
287 case Block::TYPE_IP:
288 $formatted = Linker::userLink( $target->getId(), $target );
289 $formatted .= Linker::userToolLinks(
290 $target->getId(),
291 $target,
292 false,
293 Linker::TOOL_LINKS_NOBLOCK
294 );
295 break;
296 case Block::TYPE_RANGE:
297 $formatted = htmlspecialchars( $target );
298 }
299 }
300 break;
301
302 case 'ipb_expiry':
303 $formatted = $this->getLanguage()->formatExpiry( $value, /* User preference timezone */true );
304 if ( $this->getUser()->isAllowed( 'block' ) ) {
305 if ( $row->ipb_auto ) {
306 $links[] = Linker::linkKnown(
307 SpecialPage::getTitleFor( 'Unblock' ),
308 $msg['unblocklink'],
309 array(),
310 array( 'wpTarget' => "#{$row->ipb_id}" )
311 );
312 } else {
313 $links[] = Linker::linkKnown(
314 SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
315 $msg['unblocklink']
316 );
317 $links[] = Linker::linkKnown(
318 SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
319 $msg['change-blocklink']
320 );
321 }
322 $formatted .= ' ' . Html::rawElement(
323 'span',
324 array( 'class' => 'mw-blocklist-actions' ),
325 $this->msg( 'parentheses' )->rawParams(
326 $this->getLanguage()->pipeList( $links ) )->escaped()
327 );
328 }
329 break;
330
331 case 'ipb_by':
332 if ( isset( $row->by_user_name ) ) {
333 $formatted = Linker::userLink( $value, $row->by_user_name );
334 $formatted .= Linker::userToolLinks( $value, $row->by_user_name );
335 } else {
336 $formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user?
337 }
338 break;
339
340 case 'ipb_reason':
341 $formatted = Linker::formatComment( $value );
342 break;
343
344 case 'ipb_params':
345 $properties = array();
346 if ( $row->ipb_anon_only ) {
347 $properties[] = $msg['anononlyblock'];
348 }
349 if ( $row->ipb_create_account ) {
350 $properties[] = $msg['createaccountblock'];
351 }
352 if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
353 $properties[] = $msg['noautoblockblock'];
354 }
355
356 if ( $row->ipb_block_email ) {
357 $properties[] = $msg['emailblock'];
358 }
359
360 if ( !$row->ipb_allow_usertalk ) {
361 $properties[] = $msg['blocklist-nousertalk'];
362 }
363
364 $formatted = $this->getLanguage()->commaList( $properties );
365 break;
366
367 default:
368 $formatted = "Unable to format $name";
369 break;
370 }
371
372 return $formatted;
373 }
374
375 function getQueryInfo() {
376 $info = array(
377 'tables' => array( 'ipblocks', 'user' ),
378 'fields' => array(
379 'ipb_id',
380 'ipb_address',
381 'ipb_user',
382 'ipb_by',
383 'ipb_by_text',
384 'by_user_name' => 'user_name',
385 'ipb_reason',
386 'ipb_timestamp',
387 'ipb_auto',
388 'ipb_anon_only',
389 'ipb_create_account',
390 'ipb_enable_autoblock',
391 'ipb_expiry',
392 'ipb_range_start',
393 'ipb_range_end',
394 'ipb_deleted',
395 'ipb_block_email',
396 'ipb_allow_usertalk',
397 ),
398 'conds' => $this->conds,
399 'join_conds' => array( 'user' => array( 'LEFT JOIN', 'user_id = ipb_by' ) )
400 );
401
402 # Is the user allowed to see hidden blocks?
403 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
404 $info['conds']['ipb_deleted'] = 0;
405 }
406
407 return $info;
408 }
409
410 public function getTableClass() {
411 return parent::getTableClass() . ' mw-blocklist';
412 }
413
414 function getIndexField() {
415 return 'ipb_timestamp';
416 }
417
418 function getDefaultSort() {
419 return 'ipb_timestamp';
420 }
421
422 function isFieldSortable( $name ) {
423 return false;
424 }
425
426 /**
427 * Do a LinkBatch query to minimise database load when generating all these links
428 * @param ResultWrapper $result
429 */
430 function preprocessResults( $result ) {
431 wfProfileIn( __METHOD__ );
432 # Do a link batch query
433 $lb = new LinkBatch;
434 $lb->setCaller( __METHOD__ );
435
436 $userids = array();
437
438 foreach ( $result as $row ) {
439 $userids[] = $row->ipb_by;
440
441 # Usernames and titles are in fact related by a simple substitution of space -> underscore
442 # The last few lines of Title::secureAndSplit() tell the story.
443 $name = str_replace( ' ', '_', $row->ipb_address );
444 $lb->add( NS_USER, $name );
445 $lb->add( NS_USER_TALK, $name );
446 }
447
448 $ua = UserArray::newFromIDs( $userids );
449 foreach ( $ua as $user ) {
450 $name = str_replace( ' ', '_', $user->getName() );
451 $lb->add( NS_USER, $name );
452 $lb->add( NS_USER_TALK, $name );
453 }
454
455 $lb->execute();
456 wfProfileOut( __METHOD__ );
457 }
458 }