Merge "debug: Allow the DBQuery channel to be used"
[lhc/web/wiklou.git] / includes / specials / pagers / BlockListPager.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Pager
20 */
21
22 /**
23 * @ingroup Pager
24 */
25 use MediaWiki\Block\BlockRestriction;
26 use MediaWiki\Block\Restriction\Restriction;
27 use MediaWiki\MediaWikiServices;
28 use Wikimedia\Rdbms\IResultWrapper;
29
30 class BlockListPager extends TablePager {
31
32 protected $conds;
33 protected $page;
34
35 /**
36 * Array of restrictions.
37 *
38 * @var Restriction[]
39 */
40 protected $restrictions = [];
41
42 /**
43 * @param SpecialPage $page
44 * @param array $conds
45 */
46 function __construct( $page, $conds ) {
47 $this->page = $page;
48 $this->conds = $conds;
49 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
50 parent::__construct( $page->getContext() );
51 }
52
53 function getFieldNames() {
54 static $headers = null;
55
56 if ( $headers === null ) {
57 $headers = [
58 'ipb_timestamp' => 'blocklist-timestamp',
59 'ipb_target' => 'blocklist-target',
60 'ipb_expiry' => 'blocklist-expiry',
61 'ipb_by' => 'blocklist-by',
62 'ipb_params' => 'blocklist-params',
63 'ipb_reason' => 'blocklist-reason',
64 ];
65 foreach ( $headers as $key => $val ) {
66 $headers[$key] = $this->msg( $val )->text();
67 }
68 }
69
70 return $headers;
71 }
72
73 function formatValue( $name, $value ) {
74 static $msg = null;
75 if ( $msg === null ) {
76 $keys = [
77 'anononlyblock',
78 'createaccountblock',
79 'noautoblockblock',
80 'emailblock',
81 'blocklist-nousertalk',
82 'unblocklink',
83 'change-blocklink',
84 'blocklist-editing',
85 'blocklist-editing-sitewide',
86 ];
87
88 foreach ( $keys as $key ) {
89 $msg[$key] = $this->msg( $key )->text();
90 }
91 }
92
93 /** @var object $row */
94 $row = $this->mCurrentRow;
95
96 $language = $this->getLanguage();
97
98 $formatted = '';
99
100 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
101
102 switch ( $name ) {
103 case 'ipb_timestamp':
104 $formatted = htmlspecialchars( $language->userTimeAndDate( $value, $this->getUser() ) );
105 break;
106
107 case 'ipb_target':
108 if ( $row->ipb_auto ) {
109 $formatted = $this->msg( 'autoblockid', $row->ipb_id )->parse();
110 } else {
111 list( $target, $type ) = Block::parseTarget( $row->ipb_address );
112 switch ( $type ) {
113 case Block::TYPE_USER:
114 case Block::TYPE_IP:
115 $formatted = Linker::userLink( $target->getId(), $target );
116 $formatted .= Linker::userToolLinks(
117 $target->getId(),
118 $target,
119 false,
120 Linker::TOOL_LINKS_NOBLOCK
121 );
122 break;
123 case Block::TYPE_RANGE:
124 $formatted = htmlspecialchars( $target );
125 }
126 }
127 break;
128
129 case 'ipb_expiry':
130 $formatted = htmlspecialchars( $language->formatExpiry(
131 $value,
132 /* User preference timezone */true
133 ) );
134 if ( $this->getUser()->isAllowed( 'block' ) ) {
135 if ( $row->ipb_auto ) {
136 $links[] = $linkRenderer->makeKnownLink(
137 SpecialPage::getTitleFor( 'Unblock' ),
138 $msg['unblocklink'],
139 [],
140 [ 'wpTarget' => "#{$row->ipb_id}" ]
141 );
142 } else {
143 $links[] = $linkRenderer->makeKnownLink(
144 SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
145 $msg['unblocklink']
146 );
147 $links[] = $linkRenderer->makeKnownLink(
148 SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
149 $msg['change-blocklink']
150 );
151 }
152 $formatted .= ' ' . Html::rawElement(
153 'span',
154 [ 'class' => 'mw-blocklist-actions' ],
155 $this->msg( 'parentheses' )->rawParams(
156 $language->pipeList( $links ) )->escaped()
157 );
158 }
159 if ( $value !== 'infinity' ) {
160 $timestamp = new MWTimestamp( $value );
161 $formatted .= '<br />' . $this->msg(
162 'ipb-blocklist-duration-left',
163 $language->formatDuration(
164 $timestamp->getTimestamp() - time(),
165 // reasonable output
166 [
167 'minutes',
168 'hours',
169 'days',
170 'years',
171 ]
172 )
173 )->escaped();
174 }
175 break;
176
177 case 'ipb_by':
178 if ( isset( $row->by_user_name ) ) {
179 $formatted = Linker::userLink( $value, $row->by_user_name );
180 $formatted .= Linker::userToolLinks( $value, $row->by_user_name );
181 } else {
182 $formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user?
183 }
184 break;
185
186 case 'ipb_reason':
187 $value = CommentStore::getStore()->getComment( 'ipb_reason', $row )->text;
188 $formatted = Linker::formatComment( $value );
189 break;
190
191 case 'ipb_params':
192 $properties = [];
193
194 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
195 if ( $row->ipb_sitewide ) {
196 $properties[] = htmlspecialchars( $msg['blocklist-editing-sitewide'] );
197 }
198 }
199
200 if ( !$row->ipb_sitewide && $this->restrictions ) {
201 $list = $this->getRestrictionListHTML( $this->restrictions, $row );
202 $properties[] = htmlspecialchars( $msg['blocklist-editing'] ) . $list;
203 }
204
205 if ( $row->ipb_anon_only ) {
206 $properties[] = htmlspecialchars( $msg['anononlyblock'] );
207 }
208 if ( $row->ipb_create_account ) {
209 $properties[] = htmlspecialchars( $msg['createaccountblock'] );
210 }
211 if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
212 $properties[] = htmlspecialchars( $msg['noautoblockblock'] );
213 }
214
215 if ( $row->ipb_block_email ) {
216 $properties[] = htmlspecialchars( $msg['emailblock'] );
217 }
218
219 if ( !$row->ipb_allow_usertalk ) {
220 $properties[] = htmlspecialchars( $msg['blocklist-nousertalk'] );
221 }
222
223 $formatted = Html::rawElement(
224 'ul',
225 [],
226 implode( '', array_map( function ( $prop ) {
227 return HTML::rawElement(
228 'li',
229 [],
230 $prop
231 );
232 }, $properties ) )
233 );
234 break;
235
236 default:
237 $formatted = "Unable to format $name";
238 break;
239 }
240
241 return $formatted;
242 }
243
244 /**
245 * Get Restriction List HTML
246 *
247 * @param Restriction[] $restrictions
248 * @param stdClass $row
249 *
250 * @return string
251 */
252 private static function getRestrictionListHTML(
253 array $restrictions,
254 stdClass $row
255 ) {
256 $items = [];
257
258 foreach ( $restrictions as $restriction ) {
259 if ( $restriction->getBlockId() !== (int)$row->ipb_id ) {
260 continue;
261 }
262
263 if ( $restriction->getType() !== 'page' ) {
264 continue;
265 }
266
267 $items[] = HTML::rawElement(
268 'li',
269 [],
270 Linker::link( $restriction->getTitle() )
271 );
272 }
273
274 if ( empty( $items ) ) {
275 return '';
276 }
277
278 return Html::rawElement(
279 'ul',
280 [],
281 implode( '', $items )
282 );
283 }
284
285 function getQueryInfo() {
286 $commentQuery = CommentStore::getStore()->getJoin( 'ipb_reason' );
287 $actorQuery = ActorMigration::newMigration()->getJoin( 'ipb_by' );
288
289 $info = [
290 'tables' => array_merge(
291 [ 'ipblocks' ], $commentQuery['tables'], $actorQuery['tables'], [ 'user' ]
292 ),
293 'fields' => [
294 'ipb_id',
295 'ipb_address',
296 'ipb_user',
297 'by_user_name' => 'user_name',
298 'ipb_timestamp',
299 'ipb_auto',
300 'ipb_anon_only',
301 'ipb_create_account',
302 'ipb_enable_autoblock',
303 'ipb_expiry',
304 'ipb_range_start',
305 'ipb_range_end',
306 'ipb_deleted',
307 'ipb_block_email',
308 'ipb_allow_usertalk',
309 'ipb_sitewide',
310 ] + $commentQuery['fields'] + $actorQuery['fields'],
311 'conds' => $this->conds,
312 'join_conds' => [
313 'user' => [ 'LEFT JOIN', 'user_id = ' . $actorQuery['fields']['ipb_by'] ]
314 ] + $commentQuery['joins'] + $actorQuery['joins']
315 ];
316
317 # Filter out any expired blocks
318 $db = $this->getDatabase();
319 $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
320
321 # Is the user allowed to see hidden blocks?
322 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
323 $info['conds']['ipb_deleted'] = 0;
324 }
325
326 return $info;
327 }
328
329 /**
330 * Get total number of autoblocks at any given time
331 *
332 * @return int Total number of unexpired active autoblocks
333 */
334 function getTotalAutoblocks() {
335 $dbr = $this->getDatabase();
336 $res = $dbr->selectField( 'ipblocks',
337 [ 'COUNT(*) AS totalautoblocks' ],
338 [
339 'ipb_auto' => '1',
340 'ipb_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ),
341 ]
342 );
343 if ( $res ) {
344 return $res;
345 }
346 return 0; // We found nothing
347 }
348
349 protected function getTableClass() {
350 return parent::getTableClass() . ' mw-blocklist';
351 }
352
353 function getIndexField() {
354 return 'ipb_timestamp';
355 }
356
357 function getDefaultSort() {
358 return 'ipb_timestamp';
359 }
360
361 function isFieldSortable( $name ) {
362 return false;
363 }
364
365 /**
366 * Do a LinkBatch query to minimise database load when generating all these links
367 * @param IResultWrapper $result
368 */
369 function preprocessResults( $result ) {
370 # Do a link batch query
371 $lb = new LinkBatch;
372 $lb->setCaller( __METHOD__ );
373
374 $partialBlocks = [];
375 foreach ( $result as $row ) {
376 $lb->add( NS_USER, $row->ipb_address );
377 $lb->add( NS_USER_TALK, $row->ipb_address );
378
379 if ( isset( $row->by_user_name ) ) {
380 $lb->add( NS_USER, $row->by_user_name );
381 $lb->add( NS_USER_TALK, $row->by_user_name );
382 }
383
384 if ( !$row->ipb_sitewide ) {
385 $partialBlocks[] = $row->ipb_id;
386 }
387 }
388
389 if ( $partialBlocks ) {
390 // Mutations to the $row object are not persisted. The restrictions will
391 // need be stored in a separate store.
392 $this->restrictions = BlockRestriction::loadByBlockId( $partialBlocks );
393 }
394
395 $lb->execute();
396 }
397
398 }