Look mum, no globals! Implement a RequestContext for HTMLForm, passed in the constru...
[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, $this->getContext() );
83 $form->setMethod( 'get' );
84 $form->setWrapperLegend( wfMsg( 'ipblocklist-legend' ) );
85 $form->setSubmitText( wfMsg( 'ipblocklist-submit' ) );
86 $form->prepareForm();
87
88 $form->displayForm( '' );
89 $this->showList();
90 }
91
92 function showList() {
93 global $wgOut, $wgUser;
94
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 ( !$wgUser->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 $conds['ipb_id'] = $target;
112 break;
113
114 case Block::TYPE_IP:
115 case Block::TYPE_RANGE:
116 list( $start, $end ) = IP::parseRange( $target );
117 $dbr = wfGetDB( DB_SLAVE );
118 $conds[] = $dbr->makeList(
119 array(
120 'ipb_address' => $target,
121 Block::getRangeCond( $start, $end )
122 ),
123 LIST_OR
124 );
125 $conds['ipb_auto'] = 0;
126 break;
127
128 case Block::TYPE_USER:
129 $conds['ipb_address'] = (string)$this->target;
130 $conds['ipb_auto'] = 0;
131 break;
132 }
133 }
134
135 # Apply filters
136 if( in_array( 'userblocks', $this->options ) ) {
137 $conds['ipb_user'] = 0;
138 }
139 if( in_array( 'tempblocks', $this->options ) ) {
140 $conds['ipb_expiry'] = 'infinity';
141 }
142 if( in_array( 'addressblocks', $this->options ) ) {
143 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
144 }
145
146 # Check for other blocks, i.e. global/tor blocks
147 $otherBlockLink = array();
148 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockLink, $this->target ) );
149
150 # Show additional header for the local block only when other blocks exists.
151 # Not necessary in a standard installation without such extensions enabled
152 if( count( $otherBlockLink ) ) {
153 $wgOut->addHTML(
154 Html::rawElement( 'h2', array(), wfMsg( 'ipblocklist-localblock' ) ) . "\n"
155 );
156 }
157
158 $pager = new BlockListPager( $this, $conds );
159 if ( $pager->getNumRows() ) {
160 $wgOut->addHTML(
161 $pager->getNavigationBar() .
162 $pager->getBody().
163 $pager->getNavigationBar()
164 );
165
166 } elseif ( $this->target ) {
167 $wgOut->addWikiMsg( 'ipblocklist-no-results' );
168
169 } else {
170 $wgOut->addWikiMsg( 'ipblocklist-empty' );
171 }
172
173 if( count( $otherBlockLink ) ) {
174 $wgOut->addHTML(
175 Html::rawElement(
176 'h2',
177 array(),
178 wfMsgExt(
179 'ipblocklist-otherblocks',
180 'parseinline',
181 count( $otherBlockLink )
182 )
183 ) . "\n"
184 );
185 $list = '';
186 foreach( $otherBlockLink as $link ) {
187 $list .= Html::rawElement( 'li', array(), $link ) . "\n";
188 }
189 $wgOut->addHTML( Html::rawElement( 'ul', array( 'class' => 'mw-ipblocklist-otherblocks' ), $list ) . "\n" );
190 }
191 }
192 }
193
194 class BlockListPager extends TablePager {
195 protected $conds;
196 protected $page;
197
198 function __construct( $page, $conds ) {
199 $this->page = $page;
200 $this->conds = $conds;
201 $this->mDefaultDirection = true;
202 parent::__construct();
203 }
204
205 function getFieldNames() {
206 static $headers = null;
207
208 if ( $headers == array() ) {
209 $headers = array(
210 'ipb_timestamp' => 'blocklist-timestamp',
211 'ipb_target' => 'blocklist-target',
212 'ipb_expiry' => 'blocklist-expiry',
213 'ipb_by' => 'blocklist-by',
214 'ipb_params' => 'blocklist-params',
215 'ipb_reason' => 'blocklist-reason',
216 );
217 $headers = array_map( 'wfMsg', $headers );
218 }
219
220 return $headers;
221 }
222
223 function formatValue( $name, $value ) {
224 global $wgLang, $wgUser;
225
226 static $sk, $msg;
227 if ( empty( $sk ) ) {
228 $sk = $wgUser->getSkin();
229 $msg = array(
230 'anononlyblock',
231 'createaccountblock',
232 'noautoblockblock',
233 'emailblock',
234 'blocklist-nousertalk',
235 'unblocklink',
236 'change-blocklink',
237 'infiniteblock',
238 );
239 $msg = array_combine( $msg, array_map( 'wfMessage', $msg ) );
240 }
241
242 $row = $this->mCurrentRow;
243 $formatted = '';
244
245 switch( $name ) {
246 case 'ipb_timestamp':
247 $formatted = $wgLang->timeanddate( $value );
248 break;
249
250 case 'ipb_target':
251 if( $row->ipb_auto ){
252 $formatted = wfMessage( 'autoblockid', $row->ipb_id );
253 } else {
254 list( $target, $type ) = Block::parseTarget( $row->ipb_address );
255 switch( $type ){
256 case Block::TYPE_USER:
257 case Block::TYPE_IP:
258 $formatted = $sk->userLink( $target->getId(), $target );
259 $formatted .= $sk->userToolLinks(
260 $target->getId(),
261 $target,
262 false,
263 Linker::TOOL_LINKS_NOBLOCK
264 );
265 break;
266 case Block::TYPE_RANGE:
267 $formatted = htmlspecialchars( $target );
268 }
269 }
270 break;
271
272 case 'ipb_expiry':
273 $formatted = $wgLang->formatExpiry( $value );
274 if( $wgUser->isAllowed( 'block' ) ){
275 if( $row->ipb_auto ){
276 $links[] = $sk->linkKnown(
277 SpecialPage::getTitleFor( 'Unblock' ),
278 $msg['unblocklink'],
279 array(),
280 array( 'wpTarget' => "#{$row->ipb_id}" )
281 );
282 } else {
283 $links[] = $sk->linkKnown(
284 SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
285 $msg['unblocklink']
286 );
287 $links[] = $sk->linkKnown(
288 SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
289 $msg['change-blocklink']
290 );
291 }
292 $formatted .= ' ' . Html::rawElement(
293 'span',
294 array( 'class' => 'mw-blocklist-actions' ),
295 wfMsg( 'parentheses', $wgLang->pipeList( $links ) )
296 );
297 }
298 break;
299
300 case 'ipb_by':
301 $user = User::newFromId( $value );
302 if( $user instanceof User ){
303 $formatted = $sk->userLink( $user->getId(), $user->getName() );
304 $formatted .= $sk->userToolLinks( $user->getId(), $user->getName() );
305 }
306 break;
307
308 case 'ipb_reason':
309 $formatted = $sk->commentBlock( $value );
310 break;
311
312 case 'ipb_params':
313 $properties = array();
314 if ( $row->ipb_anon_only ) {
315 $properties[] = $msg['anononlyblock'];
316 }
317 if ( $row->ipb_create_account ) {
318 $properties[] = $msg['createaccountblock'];
319 }
320 if ( !$row->ipb_enable_autoblock ) {
321 $properties[] = $msg['noautoblockblock'];
322 }
323
324 if ( $row->ipb_block_email ) {
325 $properties[] = $msg['emailblock'];
326 }
327
328 if ( !$row->ipb_allow_usertalk ) {
329 $properties[] = $msg['blocklist-nousertalk'];
330 }
331
332 $formatted = $wgLang->commaList( $properties );
333 break;
334
335 default:
336 $formatted = "Unable to format $name";
337 break;
338 }
339
340 return $formatted;
341 }
342
343 function getQueryInfo() {
344 $info = array(
345 'tables' => array( 'ipblocks' ),
346 'fields' => array(
347 'ipb_id',
348 'ipb_address',
349 'ipb_by',
350 'ipb_reason',
351 'ipb_timestamp',
352 'ipb_auto',
353 'ipb_anon_only',
354 'ipb_create_account',
355 'ipb_enable_autoblock',
356 'ipb_expiry',
357 'ipb_range_start',
358 'ipb_range_end',
359 'ipb_deleted',
360 'ipb_block_email',
361 'ipb_allow_usertalk',
362 ),
363 'conds' => $this->conds,
364 );
365
366 global $wgUser;
367 # Is the user allowed to see hidden blocks?
368 if ( !$wgUser->isAllowed( 'hideuser' ) ){
369 $conds['ipb_deleted'] = 0;
370 }
371
372 return $info;
373 }
374
375 public function getTableClass(){
376 return 'TablePager mw-blocklist';
377 }
378
379 function getIndexField() {
380 return 'ipb_timestamp';
381 }
382
383 function getDefaultSort() {
384 return 'ipb_timestamp';
385 }
386
387 function isFieldSortable( $name ) {
388 return false;
389 }
390
391 function getTitle() {
392 return $this->page->getTitle();
393 }
394 }