Follow-up r83907: abstract the flatlist mode into a descriptor option, and make it...
[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 global $wgOut, $wgRequest;
44
45 $this->setHeaders();
46 $this->outputHeader();
47 $wgOut->setPageTitle( wfMsg( 'ipblocklist' ) );
48 $wgOut->addModuleStyles( 'mediawiki.special' );
49
50 $par = $wgRequest->getVal( 'ip', $par );
51 $this->target = trim( $wgRequest->getVal( 'wpTarget', $par ) );
52
53 $this->options = $wgRequest->getArray( 'wpOptions', array() );
54
55 $action = $wgRequest->getText( 'action' );
56
57 if( $action == 'unblock' || $action == 'submit' && $wgRequest->wasPosted() ) {
58 # B/C @since 1.18: Unblock interface is now at Special:Unblock
59 $title = SpecialPage::getTitleFor( 'Unblock', $this->target );
60 $wgOut->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 ),
72 'Options' => array(
73 'type' => 'multiselect',
74 'options' => array(
75 wfMsg( 'blocklist-userblocks' ) => 'userblocks',
76 wfMsg( 'blocklist-tempblocks' ) => 'tempblocks',
77 wfMsg( 'blocklist-addressblocks' ) => 'addressblocks',
78 ),
79 'flatlist' => true,
80 ),
81 );
82 $form = new HTMLForm( $fields );
83 $form->setTitle( $this->getTitle() );
84 $form->setMethod( 'get' );
85 $form->setWrapperLegend( wfMsg( 'ipblocklist-legend' ) );
86 $form->setSubmitText( wfMsg( 'ipblocklist-submit' ) );
87 $form->prepareForm();
88
89 $form->displayForm( '' );
90 $this->showList();
91 }
92
93 function showList() {
94 global $wgOut, $wgUser;
95
96 # Purge expired entries on one in every 10 queries
97 if ( !mt_rand( 0, 10 ) ) {
98 Block::purgeExpired();
99 }
100
101 $conds = array();
102 # Is the user allowed to see hidden blocks?
103 if ( !$wgUser->isAllowed( 'hideuser' ) ){
104 $conds['ipb_deleted'] = 0;
105 }
106
107 if ( $this->target !== '' ){
108 list( $target, $type ) = Block::parseTarget( $this->target );
109
110 switch( $type ){
111 case Block::TYPE_ID:
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
147 # Check for other blocks, i.e. global/tor blocks
148 $otherBlockLink = array();
149 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockLink, $this->target ) );
150
151 # Show additional header for the local block only when other blocks exists.
152 # Not necessary in a standard installation without such extensions enabled
153 if( count( $otherBlockLink ) ) {
154 $wgOut->addHTML(
155 Html::rawElement( 'h2', array(), wfMsg( 'ipblocklist-localblock' ) ) . "\n"
156 );
157 }
158
159 $pager = new BlockListPager( $this, $conds );
160 if ( $pager->getNumRows() ) {
161 $wgOut->addHTML(
162 $pager->getNavigationBar() .
163 $pager->getBody().
164 $pager->getNavigationBar()
165 );
166
167 } elseif ( $this->target ) {
168 $wgOut->addWikiMsg( 'ipblocklist-no-results' );
169
170 } else {
171 $wgOut->addWikiMsg( 'ipblocklist-empty' );
172 }
173
174 if( count( $otherBlockLink ) ) {
175 $wgOut->addHTML(
176 Html::rawElement(
177 'h2',
178 array(),
179 wfMsgExt(
180 'ipblocklist-otherblocks',
181 'parseinline',
182 count( $otherBlockLink )
183 )
184 ) . "\n"
185 );
186 $list = '';
187 foreach( $otherBlockLink as $link ) {
188 $list .= Html::rawElement( 'li', array(), $link ) . "\n";
189 }
190 $wgOut->addHTML( Html::rawElement( 'ul', array( 'class' => 'mw-ipblocklist-otherblocks' ), $list ) . "\n" );
191 }
192 }
193 }
194
195 class BlockListPager extends TablePager {
196 protected $conds;
197 protected $page;
198
199 function __construct( $page, $conds ) {
200 $this->page = $page;
201 $this->conds = $conds;
202 $this->mDefaultDirection = true;
203 parent::__construct();
204 }
205
206 function getFieldNames() {
207 static $headers = null;
208
209 if ( $headers == array() ) {
210 $headers = array(
211 'ipb_timestamp' => 'blocklist-timestamp',
212 'ipb_target' => 'blocklist-target',
213 'ipb_expiry' => 'blocklist-expiry',
214 'ipb_by' => 'blocklist-by',
215 'ipb_params' => 'blocklist-params',
216 'ipb_reason' => 'blocklist-reason',
217 );
218 $headers = array_map( 'wfMsg', $headers );
219 }
220
221 return $headers;
222 }
223
224 function formatValue( $name, $value ) {
225 global $wgLang, $wgUser;
226
227 static $sk, $msg;
228 if ( empty( $sk ) ) {
229 $sk = $wgUser->getSkin();
230 $msg = array(
231 'anononlyblock',
232 'createaccountblock',
233 'noautoblockblock',
234 'emailblock',
235 'blocklist-nousertalk',
236 'unblocklink',
237 'change-blocklink',
238 'infiniteblock',
239 );
240 $msg = array_combine( $msg, array_map( 'wfMessage', $msg ) );
241 }
242
243 $row = $this->mCurrentRow;
244 $formatted = '';
245
246 switch( $name ) {
247 case 'ipb_timestamp':
248 $formatted = $wgLang->timeanddate( $value );
249 break;
250
251 case 'ipb_target':
252 if( $row->ipb_auto ){
253 $formatted = wfMessage( 'autoblockid', $row->ipb_id );
254 } else {
255 list( $target, $type ) = Block::parseTarget( $row->ipb_address );
256 switch( $type ){
257 case Block::TYPE_USER:
258 case Block::TYPE_IP:
259 $formatted = $sk->userLink( $target->getId(), $target );
260 $formatted .= $sk->userToolLinks(
261 $target->getId(),
262 $target,
263 false,
264 Linker::TOOL_LINKS_NOBLOCK
265 );
266 break;
267 case Block::TYPE_RANGE:
268 $formatted = htmlspecialchars( $target );
269 }
270 }
271 break;
272
273 case 'ipb_expiry':
274 $formatted = $wgLang->formatExpiry( $value );
275 if( $wgUser->isAllowed( 'block' ) ){
276 if( $row->ipb_auto ){
277 $links[] = $sk->linkKnown(
278 SpecialPage::getTitleFor( 'Unblock' ),
279 $msg['unblocklink'],
280 array(),
281 array( 'wpTarget' => "#{$row->ipb_id}" )
282 );
283 } else {
284 $links[] = $sk->linkKnown(
285 SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
286 $msg['unblocklink']
287 );
288 $links[] = $sk->linkKnown(
289 SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
290 $msg['change-blocklink']
291 );
292 }
293 $formatted .= ' ' . Html::rawElement(
294 'span',
295 array( 'class' => 'mw-blocklist-actions' ),
296 wfMsg( 'parentheses', $wgLang->pipeList( $links ) )
297 );
298 }
299 break;
300
301 case 'ipb_by':
302 $user = User::newFromId( $value );
303 if( $user instanceof User ){
304 $formatted = $sk->userLink( $user->getId(), $user->getName() );
305 $formatted .= $sk->userToolLinks( $user->getId(), $user->getName() );
306 }
307 break;
308
309 case 'ipb_reason':
310 $formatted = $sk->commentBlock( $value );
311 break;
312
313 case 'ipb_params':
314 $properties = array();
315 if ( $row->ipb_anon_only ) {
316 $properties[] = $msg['anononlyblock'];
317 }
318 if ( $row->ipb_create_account ) {
319 $properties[] = $msg['createaccountblock'];
320 }
321 if ( !$row->ipb_enable_autoblock ) {
322 $properties[] = $msg['noautoblockblock'];
323 }
324
325 if ( $row->ipb_block_email ) {
326 $properties[] = $msg['emailblock'];
327 }
328
329 if ( !$row->ipb_allow_usertalk ) {
330 $properties[] = $msg['blocklist-nousertalk'];
331 }
332
333 $formatted = $wgLang->commaList( $properties );
334 break;
335
336 default:
337 $formatted = "Unable to format $name";
338 break;
339 }
340
341 return $formatted;
342 }
343
344 function getQueryInfo() {
345 $info = array(
346 'tables' => array( 'ipblocks' ),
347 'fields' => array(
348 'ipb_id',
349 'ipb_address',
350 'ipb_by',
351 'ipb_reason',
352 'ipb_timestamp',
353 'ipb_auto',
354 'ipb_anon_only',
355 'ipb_create_account',
356 'ipb_enable_autoblock',
357 'ipb_expiry',
358 'ipb_range_start',
359 'ipb_range_end',
360 'ipb_deleted',
361 'ipb_block_email',
362 'ipb_allow_usertalk',
363 ),
364 'conds' => $this->conds,
365 );
366
367 global $wgUser;
368 # Is the user allowed to see hidden blocks?
369 if ( !$wgUser->isAllowed( 'hideuser' ) ){
370 $conds['ipb_deleted'] = 0;
371 }
372
373 return $info;
374 }
375
376 public function getTableClass(){
377 return 'TablePager mw-blocklist';
378 }
379
380 function getIndexField() {
381 return 'ipb_timestamp';
382 }
383
384 function getDefaultSort() {
385 return 'ipb_timestamp';
386 }
387
388 function isFieldSortable( $name ) {
389 return false;
390 }
391
392 function getTitle() {
393 return $this->page->getTitle();
394 }
395 }