FU r83909: restore preprocessing stage to cache link existence using LinkBatch; other...
[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 /**
204 * Getting the user names from the userids stored in the ipb_by column can be
205 * expensive, so we cache the data here.
206 * @var Array of ID => Name
207 */
208 private $userNameCache;
209
210 /**
211 * @param $page SpecialPage
212 * @param $conds Array
213 */
214 function __construct( $page, $conds ) {
215 $this->page = $page;
216 $this->conds = $conds;
217 $this->mDefaultDirection = true;
218 parent::__construct( $page->getContext() );
219 }
220
221 function getFieldNames() {
222 static $headers = null;
223
224 if ( $headers == array() ) {
225 $headers = array(
226 'ipb_timestamp' => 'blocklist-timestamp',
227 'ipb_target' => 'blocklist-target',
228 'ipb_expiry' => 'blocklist-expiry',
229 'ipb_by' => 'blocklist-by',
230 'ipb_params' => 'blocklist-params',
231 'ipb_reason' => 'blocklist-reason',
232 );
233 $headers = array_map( 'wfMsg', $headers );
234 }
235
236 return $headers;
237 }
238
239 function formatValue( $name, $value ) {
240 static $msg = null;
241 if ( $msg === null ) {
242 $msg = array(
243 'anononlyblock',
244 'createaccountblock',
245 'noautoblockblock',
246 'emailblock',
247 'blocklist-nousertalk',
248 'unblocklink',
249 'change-blocklink',
250 'infiniteblock',
251 );
252 $msg = array_combine( $msg, array_map( 'wfMessage', $msg ) );
253 }
254
255 /** @var $row object */
256 $row = $this->mCurrentRow;
257
258 $formatted = '';
259
260 switch( $name ) {
261 case 'ipb_timestamp':
262 $formatted = $this->getLang()->timeanddate( $value, /* User preference timezome */ true );
263 break;
264
265 case 'ipb_target':
266 if( $row->ipb_auto ){
267 $formatted = wfMessage( 'autoblockid', $row->ipb_id )->parse();
268 } else {
269 list( $target, $type ) = Block::parseTarget( $row->ipb_address );
270 switch( $type ){
271 case Block::TYPE_USER:
272 case Block::TYPE_IP:
273 $formatted = Linker::userLink( $target->getId(), $target );
274 $formatted .= Linker::userToolLinks(
275 $target->getId(),
276 $target,
277 false,
278 Linker::TOOL_LINKS_NOBLOCK
279 );
280 break;
281 case Block::TYPE_RANGE:
282 $formatted = htmlspecialchars( $target );
283 }
284 }
285 break;
286
287 case 'ipb_expiry':
288 $formatted = $this->getLang()->formatExpiry( $value, /* User preference timezome */ true );
289 if( $this->getUser()->isAllowed( 'block' ) ){
290 if( $row->ipb_auto ){
291 $links[] = Linker::linkKnown(
292 SpecialPage::getTitleFor( 'Unblock' ),
293 $msg['unblocklink'],
294 array(),
295 array( 'wpTarget' => "#{$row->ipb_id}" )
296 );
297 } else {
298 $links[] = Linker::linkKnown(
299 SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
300 $msg['unblocklink']
301 );
302 $links[] = Linker::linkKnown(
303 SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
304 $msg['change-blocklink']
305 );
306 }
307 $formatted .= ' ' . Html::rawElement(
308 'span',
309 array( 'class' => 'mw-blocklist-actions' ),
310 wfMsg( 'parentheses', $this->getLang()->pipeList( $links ) )
311 );
312 }
313 break;
314
315 case 'ipb_by':
316 $username = array_key_exists( $value, $this->userNameCache )
317 ? $this->userNameCache[$value]
318 : User::newFromId( $value )->getName();
319
320 $formatted = Linker::userLink( $value, $username );
321 $formatted .= Linker::userToolLinks( $value, $username );
322 break;
323
324 case 'ipb_reason':
325 $formatted = Linker::commentBlock( $value );
326 break;
327
328 case 'ipb_params':
329 $properties = array();
330 if ( $row->ipb_anon_only ) {
331 $properties[] = $msg['anononlyblock'];
332 }
333 if ( $row->ipb_create_account ) {
334 $properties[] = $msg['createaccountblock'];
335 }
336 if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
337 $properties[] = $msg['noautoblockblock'];
338 }
339
340 if ( $row->ipb_block_email ) {
341 $properties[] = $msg['emailblock'];
342 }
343
344 if ( !$row->ipb_allow_usertalk ) {
345 $properties[] = $msg['blocklist-nousertalk'];
346 }
347
348 $formatted = $this->getLang()->commaList( $properties );
349 break;
350
351 default:
352 $formatted = "Unable to format $name";
353 break;
354 }
355
356 return $formatted;
357 }
358
359 function getQueryInfo() {
360 $info = array(
361 'tables' => array( 'ipblocks' ),
362 'fields' => array(
363 'ipb_id',
364 'ipb_address',
365 'ipb_user',
366 'ipb_by',
367 'ipb_reason',
368 'ipb_timestamp',
369 'ipb_auto',
370 'ipb_anon_only',
371 'ipb_create_account',
372 'ipb_enable_autoblock',
373 'ipb_expiry',
374 'ipb_range_start',
375 'ipb_range_end',
376 'ipb_deleted',
377 'ipb_block_email',
378 'ipb_allow_usertalk',
379 ),
380 'conds' => $this->conds,
381 );
382
383 # Is the user allowed to see hidden blocks?
384 if ( !$this->getUser()->isAllowed( 'hideuser' ) ){
385 $info['conds']['ipb_deleted'] = 0;
386 }
387
388 return $info;
389 }
390
391 public function getTableClass(){
392 return 'TablePager mw-blocklist';
393 }
394
395 function getIndexField() {
396 return 'ipb_timestamp';
397 }
398
399 function getDefaultSort() {
400 return 'ipb_timestamp';
401 }
402
403 function isFieldSortable( $name ) {
404 return false;
405 }
406
407 /**
408 * Do a LinkBatch query to minimise database load when generating all these links
409 * @param $result
410 */
411 function preprocessResults( $result ){
412 wfProfileIn( __METHOD__ );
413 # Do a link batch query
414 $lb = new LinkBatch;
415 $lb->setCaller( __METHOD__ );
416
417 $userids = array();
418
419 foreach ( $result as $row ) {
420 $userids[] = $row->ipb_by;
421
422 # Usernames and titles are in fact related by a simple substitution of space -> underscore
423 # The last few lines of Title::secureAndSplit() tell the story.
424 $name = str_replace( ' ', '_', $row->ipb_address );
425 $lb->add( NS_USER, $name );
426 $lb->add( NS_USER_TALK, $name );
427 }
428
429 $ua = UserArray::newFromIDs( $userids );
430 foreach( $ua as $user ){
431 /* @var $user User */
432 $this->userNameCache[$user->getID()] = $user->getName();
433
434 $name = str_replace( ' ', '_', $user->getName() );
435 $lb->add( NS_USER, $name );
436 $lb->add( NS_USER_TALK, $name );
437 }
438
439 $lb->execute();
440 wfProfileOut( __METHOD__ );
441 }
442 }