Bug 32673: Keep the username in the input field if not existing
[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
31 protected $target, $options;
32
33 function __construct() {
34 parent::__construct( 'BlockList' );
35 }
36
37 /**
38 * Main execution point
39 *
40 * @param $par String title fragment
41 */
42 public function execute( $par ) {
43 $this->setHeaders();
44 $this->outputHeader();
45 $out = $this->getOutput();
46 $lang = $this->getLanguage();
47 $out->setPageTitle( $this->msg( 'ipblocklist' ) );
48 $out->addModuleStyles( 'mediawiki.special' );
49
50 $request = $this->getRequest();
51 $par = $request->getVal( 'ip', $par );
52 $this->target = trim( $request->getVal( 'wpTarget', $par ) );
53
54 $this->options = $request->getArray( 'wpOptions', array() );
55
56 $action = $request->getText( 'action' );
57
58 if( $action == 'unblock' || $action == 'submit' && $request->wasPosted() ) {
59 # B/C @since 1.18: Unblock interface is now at Special:Unblock
60 $title = SpecialPage::getTitleFor( 'Unblock', $this->target );
61 $out->redirect( $title->getFullUrl() );
62 return;
63 }
64
65 # Just show the block list
66 $fields = array(
67 'Target' => array(
68 'type' => 'text',
69 'label-message' => 'ipadressorusername',
70 'tabindex' => '1',
71 'size' => '45',
72 'default' => $this->target,
73 ),
74 'Options' => array(
75 'type' => 'multiselect',
76 'options' => array(
77 wfMsg( 'blocklist-userblocks' ) => 'userblocks',
78 wfMsg( 'blocklist-tempblocks' ) => 'tempblocks',
79 wfMsg( 'blocklist-addressblocks' ) => 'addressblocks',
80 wfMsg( 'blocklist-rangeblocks' ) => 'rangeblocks',
81 ),
82 'flatlist' => true,
83 ),
84 'Limit' => array(
85 'class' => 'HTMLBlockedUsersItemSelect',
86 'label-message' => 'table_pager_limit_label',
87 'options' => array(
88 $lang->formatNum( 20 ) => 20,
89 $lang->formatNum( 50 ) => 50,
90 $lang->formatNum( 100 ) => 100,
91 $lang->formatNum( 250 ) => 250,
92 $lang->formatNum( 500 ) => 500,
93 ),
94 'name' => 'limit',
95 'default' => 50,
96 ),
97 );
98 $form = new HTMLForm( $fields, $this->getContext() );
99 $form->setMethod( 'get' );
100 $form->setWrapperLegend( wfMsg( 'ipblocklist-legend' ) );
101 $form->setSubmitText( wfMsg( 'ipblocklist-submit' ) );
102 $form->prepareForm();
103
104 $form->displayForm( '' );
105 $this->showList();
106 }
107
108 function showList() {
109 # Purge expired entries on one in every 10 queries
110 if ( !mt_rand( 0, 10 ) ) {
111 Block::purgeExpired();
112 }
113
114 $conds = array();
115 # Is the user allowed to see hidden blocks?
116 if ( !$this->getUser()->isAllowed( 'hideuser' ) ){
117 $conds['ipb_deleted'] = 0;
118 }
119
120 if ( $this->target !== '' ){
121 list( $target, $type ) = Block::parseTarget( $this->target );
122
123 switch( $type ){
124 case Block::TYPE_ID:
125 case Block::TYPE_AUTO:
126 $conds['ipb_id'] = $target;
127 break;
128
129 case Block::TYPE_IP:
130 case Block::TYPE_RANGE:
131 list( $start, $end ) = IP::parseRange( $target );
132 $dbr = wfGetDB( DB_SLAVE );
133 $conds[] = $dbr->makeList(
134 array(
135 'ipb_address' => $target,
136 Block::getRangeCond( $start, $end )
137 ),
138 LIST_OR
139 );
140 $conds['ipb_auto'] = 0;
141 break;
142
143 case Block::TYPE_USER:
144 $conds['ipb_address'] = (string)$this->target;
145 $conds['ipb_auto'] = 0;
146 break;
147 }
148 }
149
150 # Apply filters
151 if( in_array( 'userblocks', $this->options ) ) {
152 $conds['ipb_user'] = 0;
153 }
154 if( in_array( 'tempblocks', $this->options ) ) {
155 $conds['ipb_expiry'] = 'infinity';
156 }
157 if( in_array( 'addressblocks', $this->options ) ) {
158 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
159 }
160 if( in_array( 'rangeblocks', $this->options ) ) {
161 $conds[] = "ipb_range_end = ipb_range_start";
162 }
163
164 # Check for other blocks, i.e. global/tor blocks
165 $otherBlockLink = array();
166 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockLink, $this->target ) );
167
168 $out = $this->getOutput();
169
170 # Show additional header for the local block only when other blocks exists.
171 # Not necessary in a standard installation without such extensions enabled
172 if( count( $otherBlockLink ) ) {
173 $out->addHTML(
174 Html::rawElement( 'h2', array(), wfMsg( 'ipblocklist-localblock' ) ) . "\n"
175 );
176 }
177
178 $pager = new BlockListPager( $this, $conds );
179 if ( $pager->getNumRows() ) {
180 $out->addHTML(
181 $pager->getNavigationBar() .
182 $pager->getBody().
183 $pager->getNavigationBar()
184 );
185
186 } elseif ( $this->target ) {
187 $out->addWikiMsg( 'ipblocklist-no-results' );
188
189 } else {
190 $out->addWikiMsg( 'ipblocklist-empty' );
191 }
192
193 if( count( $otherBlockLink ) ) {
194 $out->addHTML(
195 Html::rawElement(
196 'h2',
197 array(),
198 wfMsgExt(
199 'ipblocklist-otherblocks',
200 'parseinline',
201 count( $otherBlockLink )
202 )
203 ) . "\n"
204 );
205 $list = '';
206 foreach( $otherBlockLink as $link ) {
207 $list .= Html::rawElement( 'li', array(), $link ) . "\n";
208 }
209 $out->addHTML( Html::rawElement( 'ul', array( 'class' => 'mw-ipblocklist-otherblocks' ), $list ) . "\n" );
210 }
211 }
212 }
213
214 class BlockListPager extends TablePager {
215 protected $conds;
216 protected $page;
217
218 /**
219 * @param $page SpecialPage
220 * @param $conds Array
221 */
222 function __construct( $page, $conds ) {
223 $this->page = $page;
224 $this->conds = $conds;
225 $this->mDefaultDirection = true;
226 parent::__construct( $page->getContext() );
227 }
228
229 function getFieldNames() {
230 static $headers = null;
231
232 if ( $headers == array() ) {
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 $headers = array_map( 'wfMsg', $headers );
242 }
243
244 return $headers;
245 }
246
247 function formatValue( $name, $value ) {
248 static $msg = null;
249 if ( $msg === null ) {
250 $msg = array(
251 'anononlyblock',
252 'createaccountblock',
253 'noautoblockblock',
254 'emailblock',
255 'blocklist-nousertalk',
256 'unblocklink',
257 'change-blocklink',
258 'infiniteblock',
259 );
260 $msg = array_combine( $msg, array_map( 'wfMessage', $msg ) );
261 }
262
263 /** @var $row object */
264 $row = $this->mCurrentRow;
265
266 $formatted = '';
267
268 switch( $name ) {
269 case 'ipb_timestamp':
270 $formatted = $this->getLanguage()->timeanddate( $value, /* User preference timezone */ true );
271 break;
272
273 case 'ipb_target':
274 if( $row->ipb_auto ){
275 $formatted = wfMessage( 'autoblockid', $row->ipb_id )->parse();
276 } else {
277 list( $target, $type ) = Block::parseTarget( $row->ipb_address );
278 switch( $type ){
279 case Block::TYPE_USER:
280 case Block::TYPE_IP:
281 $formatted = Linker::userLink( $target->getId(), $target );
282 $formatted .= Linker::userToolLinks(
283 $target->getId(),
284 $target,
285 false,
286 Linker::TOOL_LINKS_NOBLOCK
287 );
288 break;
289 case Block::TYPE_RANGE:
290 $formatted = htmlspecialchars( $target );
291 }
292 }
293 break;
294
295 case 'ipb_expiry':
296 $formatted = $this->getLanguage()->formatExpiry( $value, /* User preference timezone */ true );
297 if( $this->getUser()->isAllowed( 'block' ) ){
298 if( $row->ipb_auto ){
299 $links[] = Linker::linkKnown(
300 SpecialPage::getTitleFor( 'Unblock' ),
301 $msg['unblocklink'],
302 array(),
303 array( 'wpTarget' => "#{$row->ipb_id}" )
304 );
305 } else {
306 $links[] = Linker::linkKnown(
307 SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
308 $msg['unblocklink']
309 );
310 $links[] = Linker::linkKnown(
311 SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
312 $msg['change-blocklink']
313 );
314 }
315 $formatted .= ' ' . Html::rawElement(
316 'span',
317 array( 'class' => 'mw-blocklist-actions' ),
318 wfMsg( 'parentheses', $this->getLanguage()->pipeList( $links ) )
319 );
320 }
321 break;
322
323 case 'ipb_by':
324 if ( isset( $row->by_user_name ) ) {
325 $formatted = Linker::userLink( $value, $row->by_user_name );
326 $formatted .= Linker::userToolLinks( $value, $row->by_user_name );
327 } else {
328 $formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user?
329 }
330 break;
331
332 case 'ipb_reason':
333 $formatted = Linker::commentBlock( $value );
334 break;
335
336 case 'ipb_params':
337 $properties = array();
338 if ( $row->ipb_anon_only ) {
339 $properties[] = $msg['anononlyblock'];
340 }
341 if ( $row->ipb_create_account ) {
342 $properties[] = $msg['createaccountblock'];
343 }
344 if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
345 $properties[] = $msg['noautoblockblock'];
346 }
347
348 if ( $row->ipb_block_email ) {
349 $properties[] = $msg['emailblock'];
350 }
351
352 if ( !$row->ipb_allow_usertalk ) {
353 $properties[] = $msg['blocklist-nousertalk'];
354 }
355
356 $formatted = $this->getLanguage()->commaList( $properties );
357 break;
358
359 default:
360 $formatted = "Unable to format $name";
361 break;
362 }
363
364 return $formatted;
365 }
366
367 function getQueryInfo() {
368 $info = array(
369 'tables' => array( 'ipblocks', 'user' ),
370 'fields' => array(
371 'ipb_id',
372 'ipb_address',
373 'ipb_user',
374 'ipb_by',
375 'ipb_by_text',
376 'user_name AS by_user_name',
377 'ipb_reason',
378 'ipb_timestamp',
379 'ipb_auto',
380 'ipb_anon_only',
381 'ipb_create_account',
382 'ipb_enable_autoblock',
383 'ipb_expiry',
384 'ipb_range_start',
385 'ipb_range_end',
386 'ipb_deleted',
387 'ipb_block_email',
388 'ipb_allow_usertalk',
389 ),
390 'conds' => $this->conds,
391 'join_conds' => array( 'user' => array( 'LEFT JOIN', 'user_id = ipb_by' ) )
392 );
393
394 # Is the user allowed to see hidden blocks?
395 if ( !$this->getUser()->isAllowed( 'hideuser' ) ){
396 $info['conds']['ipb_deleted'] = 0;
397 }
398
399 return $info;
400 }
401
402 public function getTableClass(){
403 return 'TablePager mw-blocklist';
404 }
405
406 function getIndexField() {
407 return 'ipb_timestamp';
408 }
409
410 function getDefaultSort() {
411 return 'ipb_timestamp';
412 }
413
414 function isFieldSortable( $name ) {
415 return false;
416 }
417
418 /**
419 * Do a LinkBatch query to minimise database load when generating all these links
420 * @param $result
421 */
422 function preprocessResults( $result ){
423 wfProfileIn( __METHOD__ );
424 # Do a link batch query
425 $lb = new LinkBatch;
426 $lb->setCaller( __METHOD__ );
427
428 $userids = array();
429
430 foreach ( $result as $row ) {
431 $userids[] = $row->ipb_by;
432
433 # Usernames and titles are in fact related by a simple substitution of space -> underscore
434 # The last few lines of Title::secureAndSplit() tell the story.
435 $name = str_replace( ' ', '_', $row->ipb_address );
436 $lb->add( NS_USER, $name );
437 $lb->add( NS_USER_TALK, $name );
438 }
439
440 $ua = UserArray::newFromIDs( $userids );
441 foreach( $ua as $user ){
442 $name = str_replace( ' ', '_', $user->getName() );
443 $lb->add( NS_USER, $name );
444 $lb->add( NS_USER_TALK, $name );
445 }
446
447 $lb->execute();
448 wfProfileOut( __METHOD__ );
449 }
450 }
451
452 /**
453 * Items per page dropdown. Essentially a crap workaround for bug 32603.
454 *
455 * @todo Do not release 1.19 with this.
456 */
457 class HTMLBlockedUsersItemSelect extends HTMLSelectField {
458 /**
459 * Basically don't do any validation. If it's a number that's fine. Also,
460 * add it to the list if it's not there already
461 *
462 * @param $value
463 * @param $alldata
464 * @return bool
465 */
466 function validate( $value, $alldata ) {
467 if ( $value == '' ) {
468 return true;
469 }
470
471 if ( !in_array( $value, $this->mParams['options'] ) ) {
472 $this->mParams['options'][ $this->mParent->getLanguage()->formatNum( $value ) ] = intval($value);
473 }
474
475 return true;
476 }
477
478 }