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