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