Escape unescaped content shown in Special:BlockList
[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-messages' => array(
81 'blocklist-userblocks' => 'userblocks',
82 'blocklist-tempblocks' => 'tempblocks',
83 'blocklist-addressblocks' => 'addressblocks',
84 'blocklist-rangeblocks' => '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 $conds = array();
117 # Is the user allowed to see hidden blocks?
118 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
119 $conds['ipb_deleted'] = 0;
120 }
121
122 if ( $this->target !== '' ) {
123 list( $target, $type ) = Block::parseTarget( $this->target );
124
125 switch ( $type ) {
126 case Block::TYPE_ID:
127 case Block::TYPE_AUTO:
128 $conds['ipb_id'] = $target;
129 break;
130
131 case Block::TYPE_IP:
132 case Block::TYPE_RANGE:
133 list( $start, $end ) = IP::parseRange( $target );
134 $dbr = wfGetDB( DB_SLAVE );
135 $conds[] = $dbr->makeList(
136 array(
137 'ipb_address' => $target,
138 Block::getRangeCond( $start, $end )
139 ),
140 LIST_OR
141 );
142 $conds['ipb_auto'] = 0;
143 break;
144
145 case Block::TYPE_USER:
146 $conds['ipb_address'] = $target->getName();
147 $conds['ipb_auto'] = 0;
148 break;
149 }
150 }
151
152 # Apply filters
153 if ( in_array( 'userblocks', $this->options ) ) {
154 $conds['ipb_user'] = 0;
155 }
156 if ( in_array( 'tempblocks', $this->options ) ) {
157 $conds['ipb_expiry'] = 'infinity';
158 }
159 if ( in_array( 'addressblocks', $this->options ) ) {
160 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
161 }
162 if ( in_array( 'rangeblocks', $this->options ) ) {
163 $conds[] = "ipb_range_end = ipb_range_start";
164 }
165
166 # Check for other blocks, i.e. global/tor blocks
167 $otherBlockLink = array();
168 Hooks::run( 'OtherBlockLogLink', array( &$otherBlockLink, $this->target ) );
169
170 $out = $this->getOutput();
171
172 # Show additional header for the local block only when other blocks exists.
173 # Not necessary in a standard installation without such extensions enabled
174 if ( count( $otherBlockLink ) ) {
175 $out->addHTML(
176 Html::element( 'h2', array(), $this->msg( 'ipblocklist-localblock' )->text() ) . "\n"
177 );
178 }
179
180 $pager = new BlockListPager( $this, $conds );
181 if ( $pager->getNumRows() ) {
182 $out->addParserOutputContent( $pager->getFullOutput() );
183 } elseif ( $this->target ) {
184 $out->addWikiMsg( 'ipblocklist-no-results' );
185 } else {
186 $out->addWikiMsg( 'ipblocklist-empty' );
187 }
188
189 if ( count( $otherBlockLink ) ) {
190 $out->addHTML(
191 Html::rawElement(
192 'h2',
193 array(),
194 $this->msg( 'ipblocklist-otherblocks', count( $otherBlockLink ) )->parse()
195 ) . "\n"
196 );
197 $list = '';
198 foreach ( $otherBlockLink as $link ) {
199 $list .= Html::rawElement( 'li', array(), $link ) . "\n";
200 }
201 $out->addHTML( Html::rawElement(
202 'ul',
203 array( 'class' => 'mw-ipblocklist-otherblocks' ),
204 $list
205 ) . "\n" );
206 }
207 }
208
209 protected function getGroupName() {
210 return 'users';
211 }
212 }
213
214 class BlockListPager extends TablePager {
215 protected $conds;
216 protected $page;
217
218 /**
219 * @param SpecialPage $page
220 * @param array $conds
221 */
222 function __construct( $page, $conds ) {
223 $this->page = $page;
224 $this->conds = $conds;
225 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
226 parent::__construct( $page->getContext() );
227 }
228
229 function getFieldNames() {
230 static $headers = null;
231
232 if ( $headers === null ) {
233 $headers = array(
234 'ipb_timestamp' => 'blocklist-timestamp',
235 'ipb_target' => 'blocklist-target',
236 'ipb_expiry' => 'blocklist-expiry',
237 'ipb_by' => 'blocklist-by',
238 'ipb_params' => 'blocklist-params',
239 'ipb_reason' => 'blocklist-reason',
240 );
241 foreach ( $headers as $key => $val ) {
242 $headers[$key] = $this->msg( $val )->text();
243 }
244 }
245
246 return $headers;
247 }
248
249 function formatValue( $name, $value ) {
250 static $msg = null;
251 if ( $msg === null ) {
252 $keys = array(
253 'anononlyblock',
254 'createaccountblock',
255 'noautoblockblock',
256 'emailblock',
257 'blocklist-nousertalk',
258 'unblocklink',
259 'change-blocklink',
260 );
261
262 foreach ( $keys as $key ) {
263 $msg[$key] = $this->msg( $key )->escaped();
264 }
265 }
266
267 /** @var $row object */
268 $row = $this->mCurrentRow;
269
270 $language = $this->getLanguage();
271
272 $formatted = '';
273
274 switch ( $name ) {
275 case 'ipb_timestamp':
276 $formatted = htmlspecialchars( $language->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 = htmlspecialchars( $language->formatExpiry(
303 $value,
304 /* User preference timezone */true
305 ) );
306 if ( $this->getUser()->isAllowed( 'block' ) ) {
307 if ( $row->ipb_auto ) {
308 $links[] = Linker::linkKnown(
309 SpecialPage::getTitleFor( 'Unblock' ),
310 $msg['unblocklink'],
311 array(),
312 array( 'wpTarget' => "#{$row->ipb_id}" )
313 );
314 } else {
315 $links[] = Linker::linkKnown(
316 SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
317 $msg['unblocklink']
318 );
319 $links[] = Linker::linkKnown(
320 SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
321 $msg['change-blocklink']
322 );
323 }
324 $formatted .= ' ' . Html::rawElement(
325 'span',
326 array( 'class' => 'mw-blocklist-actions' ),
327 $this->msg( 'parentheses' )->rawParams(
328 $language->pipeList( $links ) )->escaped()
329 );
330 }
331 break;
332
333 case 'ipb_by':
334 if ( isset( $row->by_user_name ) ) {
335 $formatted = Linker::userLink( $value, $row->by_user_name );
336 $formatted .= Linker::userToolLinks( $value, $row->by_user_name );
337 } else {
338 $formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user?
339 }
340 break;
341
342 case 'ipb_reason':
343 $formatted = Linker::formatComment( $value );
344 break;
345
346 case 'ipb_params':
347 $properties = array();
348 if ( $row->ipb_anon_only ) {
349 $properties[] = $msg['anononlyblock'];
350 }
351 if ( $row->ipb_create_account ) {
352 $properties[] = $msg['createaccountblock'];
353 }
354 if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
355 $properties[] = $msg['noautoblockblock'];
356 }
357
358 if ( $row->ipb_block_email ) {
359 $properties[] = $msg['emailblock'];
360 }
361
362 if ( !$row->ipb_allow_usertalk ) {
363 $properties[] = $msg['blocklist-nousertalk'];
364 }
365
366 $formatted = $language->commaList( $properties );
367 break;
368
369 default:
370 $formatted = "Unable to format $name";
371 break;
372 }
373
374 return $formatted;
375 }
376
377 function getQueryInfo() {
378 $info = array(
379 'tables' => array( 'ipblocks', 'user' ),
380 'fields' => array(
381 'ipb_id',
382 'ipb_address',
383 'ipb_user',
384 'ipb_by',
385 'ipb_by_text',
386 'by_user_name' => 'user_name',
387 'ipb_reason',
388 'ipb_timestamp',
389 'ipb_auto',
390 'ipb_anon_only',
391 'ipb_create_account',
392 'ipb_enable_autoblock',
393 'ipb_expiry',
394 'ipb_range_start',
395 'ipb_range_end',
396 'ipb_deleted',
397 'ipb_block_email',
398 'ipb_allow_usertalk',
399 ),
400 'conds' => $this->conds,
401 'join_conds' => array( 'user' => array( 'LEFT JOIN', 'user_id = ipb_by' ) )
402 );
403
404 # Filter out any expired blocks
405 $db = $this->getDatabase();
406 $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
407
408 # Is the user allowed to see hidden blocks?
409 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
410 $info['conds']['ipb_deleted'] = 0;
411 }
412
413 return $info;
414 }
415
416 public function getTableClass() {
417 return parent::getTableClass() . ' mw-blocklist';
418 }
419
420 function getIndexField() {
421 return 'ipb_timestamp';
422 }
423
424 function getDefaultSort() {
425 return 'ipb_timestamp';
426 }
427
428 function isFieldSortable( $name ) {
429 return false;
430 }
431
432 /**
433 * Do a LinkBatch query to minimise database load when generating all these links
434 * @param ResultWrapper $result
435 */
436 function preprocessResults( $result ) {
437 # Do a link batch query
438 $lb = new LinkBatch;
439 $lb->setCaller( __METHOD__ );
440
441 foreach ( $result as $row ) {
442 $lb->add( NS_USER, $row->ipb_address );
443 $lb->add( NS_USER_TALK, $row->ipb_address );
444
445 if ( isset( $row->by_user_name ) ) {
446 $lb->add( NS_USER, $row->by_user_name );
447 $lb->add( NS_USER_TALK, $row->by_user_name );
448 }
449 }
450
451 $lb->execute();
452 }
453 }