As suggested to me by email: very active members of a wiki community can end up recei...
[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 wfMsg( 'blocklist-rangeblocks' ) => 'rangeblocks',
79 ),
80 'flatlist' => true,
81 ),
82 );
83 $form = new HTMLForm( $fields, $this->getContext() );
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 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 # Show additional header for the local block only when other blocks exists.
155 # Not necessary in a standard installation without such extensions enabled
156 if( count( $otherBlockLink ) ) {
157 $wgOut->addHTML(
158 Html::rawElement( 'h2', array(), wfMsg( 'ipblocklist-localblock' ) ) . "\n"
159 );
160 }
161
162 $pager = new BlockListPager( $this, $conds );
163 if ( $pager->getNumRows() ) {
164 $wgOut->addHTML(
165 $pager->getNavigationBar() .
166 $pager->getBody().
167 $pager->getNavigationBar()
168 );
169
170 } elseif ( $this->target ) {
171 $wgOut->addWikiMsg( 'ipblocklist-no-results' );
172
173 } else {
174 $wgOut->addWikiMsg( 'ipblocklist-empty' );
175 }
176
177 if( count( $otherBlockLink ) ) {
178 $wgOut->addHTML(
179 Html::rawElement(
180 'h2',
181 array(),
182 wfMsgExt(
183 'ipblocklist-otherblocks',
184 'parseinline',
185 count( $otherBlockLink )
186 )
187 ) . "\n"
188 );
189 $list = '';
190 foreach( $otherBlockLink as $link ) {
191 $list .= Html::rawElement( 'li', array(), $link ) . "\n";
192 }
193 $wgOut->addHTML( Html::rawElement( 'ul', array( 'class' => 'mw-ipblocklist-otherblocks' ), $list ) . "\n" );
194 }
195 }
196 }
197
198 class BlockListPager extends TablePager {
199 protected $conds;
200 protected $page;
201
202 function __construct( $page, $conds ) {
203 $this->page = $page;
204 $this->conds = $conds;
205 $this->mDefaultDirection = true;
206 parent::__construct();
207 }
208
209 function getFieldNames() {
210 static $headers = null;
211
212 if ( $headers == array() ) {
213 $headers = array(
214 'ipb_timestamp' => 'blocklist-timestamp',
215 'ipb_target' => 'blocklist-target',
216 'ipb_expiry' => 'blocklist-expiry',
217 'ipb_by' => 'blocklist-by',
218 'ipb_params' => 'blocklist-params',
219 'ipb_reason' => 'blocklist-reason',
220 );
221 $headers = array_map( 'wfMsg', $headers );
222 }
223
224 return $headers;
225 }
226
227 function formatValue( $name, $value ) {
228 global $wgLang, $wgUser;
229
230 static $sk, $msg;
231 if ( empty( $sk ) ) {
232 $sk = $this->getSkin();
233 $msg = array(
234 'anononlyblock',
235 'createaccountblock',
236 'noautoblockblock',
237 'emailblock',
238 'blocklist-nousertalk',
239 'unblocklink',
240 'change-blocklink',
241 'infiniteblock',
242 );
243 $msg = array_combine( $msg, array_map( 'wfMessage', $msg ) );
244 }
245
246 $row = $this->mCurrentRow;
247 $formatted = '';
248
249 switch( $name ) {
250 case 'ipb_timestamp':
251 $formatted = $wgLang->timeanddate( $value );
252 break;
253
254 case 'ipb_target':
255 if( $row->ipb_auto ){
256 $formatted = wfMessage( 'autoblockid', $row->ipb_id )->parse();
257 } else {
258 list( $target, $type ) = Block::parseTarget( $row->ipb_address );
259 switch( $type ){
260 case Block::TYPE_USER:
261 case Block::TYPE_IP:
262 $formatted = $sk->userLink( $target->getId(), $target );
263 $formatted .= $sk->userToolLinks(
264 $target->getId(),
265 $target,
266 false,
267 Linker::TOOL_LINKS_NOBLOCK
268 );
269 break;
270 case Block::TYPE_RANGE:
271 $formatted = htmlspecialchars( $target );
272 }
273 }
274 break;
275
276 case 'ipb_expiry':
277 $formatted = $wgLang->formatExpiry( $value );
278 if( $wgUser->isAllowed( 'block' ) ){
279 if( $row->ipb_auto ){
280 $links[] = $sk->linkKnown(
281 SpecialPage::getTitleFor( 'Unblock' ),
282 $msg['unblocklink'],
283 array(),
284 array( 'wpTarget' => "#{$row->ipb_id}" )
285 );
286 } else {
287 $links[] = $sk->linkKnown(
288 SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
289 $msg['unblocklink']
290 );
291 $links[] = $sk->linkKnown(
292 SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
293 $msg['change-blocklink']
294 );
295 }
296 $formatted .= ' ' . Html::rawElement(
297 'span',
298 array( 'class' => 'mw-blocklist-actions' ),
299 wfMsg( 'parentheses', $wgLang->pipeList( $links ) )
300 );
301 }
302 break;
303
304 case 'ipb_by':
305 $user = User::newFromId( $value );
306 if( $user instanceof User ){
307 $formatted = $sk->userLink( $user->getId(), $user->getName() );
308 $formatted .= $sk->userToolLinks( $user->getId(), $user->getName() );
309 }
310 break;
311
312 case 'ipb_reason':
313 $formatted = $sk->commentBlock( $value );
314 break;
315
316 case 'ipb_params':
317 $properties = array();
318 if ( $row->ipb_anon_only ) {
319 $properties[] = $msg['anononlyblock'];
320 }
321 if ( $row->ipb_create_account ) {
322 $properties[] = $msg['createaccountblock'];
323 }
324 if ( !$row->ipb_enable_autoblock ) {
325 $properties[] = $msg['noautoblockblock'];
326 }
327
328 if ( $row->ipb_block_email ) {
329 $properties[] = $msg['emailblock'];
330 }
331
332 if ( !$row->ipb_allow_usertalk ) {
333 $properties[] = $msg['blocklist-nousertalk'];
334 }
335
336 $formatted = $wgLang->commaList( $properties );
337 break;
338
339 default:
340 $formatted = "Unable to format $name";
341 break;
342 }
343
344 return $formatted;
345 }
346
347 function getQueryInfo() {
348 $info = array(
349 'tables' => array( 'ipblocks' ),
350 'fields' => array(
351 'ipb_id',
352 'ipb_address',
353 'ipb_by',
354 'ipb_reason',
355 'ipb_timestamp',
356 'ipb_auto',
357 'ipb_anon_only',
358 'ipb_create_account',
359 'ipb_enable_autoblock',
360 'ipb_expiry',
361 'ipb_range_start',
362 'ipb_range_end',
363 'ipb_deleted',
364 'ipb_block_email',
365 'ipb_allow_usertalk',
366 ),
367 'conds' => $this->conds,
368 );
369
370 global $wgUser;
371 # Is the user allowed to see hidden blocks?
372 if ( !$wgUser->isAllowed( 'hideuser' ) ){
373 $conds['ipb_deleted'] = 0;
374 }
375
376 return $info;
377 }
378
379 public function getTableClass(){
380 return 'TablePager mw-blocklist';
381 }
382
383 function getIndexField() {
384 return 'ipb_timestamp';
385 }
386
387 function getDefaultSort() {
388 return 'ipb_timestamp';
389 }
390
391 function isFieldSortable( $name ) {
392 return false;
393 }
394
395 function getTitle() {
396 return $this->page->getTitle();
397 }
398 }