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