* Made IndexPager extend ContextSource
[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( wfMsg( '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 ),
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 # Purge expired entries on one in every 10 queries
95 if ( !mt_rand( 0, 10 ) ) {
96 Block::purgeExpired();
97 }
98
99 $conds = array();
100 # Is the user allowed to see hidden blocks?
101 if ( !$this->getUser()->isAllowed( 'hideuser' ) ){
102 $conds['ipb_deleted'] = 0;
103 }
104
105 if ( $this->target !== '' ){
106 list( $target, $type ) = Block::parseTarget( $this->target );
107
108 switch( $type ){
109 case Block::TYPE_ID:
110 $conds['ipb_id'] = $target;
111 break;
112
113 case Block::TYPE_IP:
114 case Block::TYPE_RANGE:
115 list( $start, $end ) = IP::parseRange( $target );
116 $dbr = wfGetDB( DB_SLAVE );
117 $conds[] = $dbr->makeList(
118 array(
119 'ipb_address' => $target,
120 Block::getRangeCond( $start, $end )
121 ),
122 LIST_OR
123 );
124 $conds['ipb_auto'] = 0;
125 break;
126
127 case Block::TYPE_USER:
128 $conds['ipb_address'] = (string)$this->target;
129 $conds['ipb_auto'] = 0;
130 break;
131 }
132 }
133
134 # Apply filters
135 if( in_array( 'userblocks', $this->options ) ) {
136 $conds['ipb_user'] = 0;
137 }
138 if( in_array( 'tempblocks', $this->options ) ) {
139 $conds['ipb_expiry'] = 'infinity';
140 }
141 if( in_array( 'addressblocks', $this->options ) ) {
142 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
143 }
144 if( in_array( 'rangeblocks', $this->options ) ) {
145 $conds[] = "ipb_range_end = ipb_range_start";
146 }
147
148 # Check for other blocks, i.e. global/tor blocks
149 $otherBlockLink = array();
150 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockLink, $this->target ) );
151
152 $out = $this->getOutput();
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 $out->addHTML(
158 Html::rawElement( 'h2', array(), wfMsg( 'ipblocklist-localblock' ) ) . "\n"
159 );
160 }
161
162 $pager = new BlockListPager( $this, $conds );
163 if ( $pager->getNumRows() ) {
164 $out->addHTML(
165 $pager->getNavigationBar() .
166 $pager->getBody().
167 $pager->getNavigationBar()
168 );
169
170 } elseif ( $this->target ) {
171 $out->addWikiMsg( 'ipblocklist-no-results' );
172
173 } else {
174 $out->addWikiMsg( 'ipblocklist-empty' );
175 }
176
177 if( count( $otherBlockLink ) ) {
178 $out->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 $out->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( $page->getContext() );
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 static $msg = null;
229 if ( $msg === null ) {
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 = $this->getLang()->timeanddate( $value );
249 break;
250
251 case 'ipb_target':
252 if( $row->ipb_auto ){
253 $formatted = wfMessage( 'autoblockid', $row->ipb_id )->parse();
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 = Linker::userLink( $target->getId(), $target );
260 $formatted .= Linker::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 = $this->getLang()->formatExpiry( $value );
275 if( $this->getUser()->isAllowed( 'block' ) ){
276 if( $row->ipb_auto ){
277 $links[] = Linker::linkKnown(
278 SpecialPage::getTitleFor( 'Unblock' ),
279 $msg['unblocklink'],
280 array(),
281 array( 'wpTarget' => "#{$row->ipb_id}" )
282 );
283 } else {
284 $links[] = Linker::linkKnown(
285 SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
286 $msg['unblocklink']
287 );
288 $links[] = Linker::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', $this->getLang()->pipeList( $links ) )
297 );
298 }
299 break;
300
301 case 'ipb_by':
302 $user = User::newFromId( $value );
303 if( $user instanceof User ){
304 $formatted = Linker::userLink( $user->getId(), $user->getName() );
305 $formatted .= Linker::userToolLinks( $user->getId(), $user->getName() );
306 }
307 break;
308
309 case 'ipb_reason':
310 $formatted = Linker::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 = $this->getLang()->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 # Is the user allowed to see hidden blocks?
368 if ( !$this->getUser()->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 }