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