Merge "lockmanager: sort key by bucket in QuorumLockManager::doLockByType"
[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\DatabaseBlock;
26 use MediaWiki\Block\Restriction\Restriction;
27 use MediaWiki\Block\Restriction\PageRestriction;
28 use MediaWiki\Block\Restriction\NamespaceRestriction;
29 use MediaWiki\MediaWikiServices;
30 use Wikimedia\Rdbms\IResultWrapper;
31
32 class BlockListPager extends TablePager {
33
34 protected $conds;
35
36 /**
37 * Array of restrictions.
38 *
39 * @var Restriction[]
40 */
41 protected $restrictions = [];
42
43 /**
44 * @param SpecialPage $page
45 * @param array $conds
46 */
47 public function __construct( $page, $conds ) {
48 parent::__construct( $page->getContext(), $page->getLinkRenderer() );
49 $this->conds = $conds;
50 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
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 /**
74 * @param string $name
75 * @param string $value
76 * @return string
77 * @suppress PhanTypeArraySuspiciousNullable
78 */
79 function formatValue( $name, $value ) {
80 static $msg = null;
81 if ( $msg === null ) {
82 $keys = [
83 'anononlyblock',
84 'createaccountblock',
85 'noautoblockblock',
86 'emailblock',
87 'blocklist-nousertalk',
88 'unblocklink',
89 'change-blocklink',
90 'blocklist-editing',
91 'blocklist-editing-sitewide',
92 ];
93
94 foreach ( $keys as $key ) {
95 $msg[$key] = $this->msg( $key )->text();
96 }
97 }
98
99 /** @var object $row */
100 $row = $this->mCurrentRow;
101
102 $language = $this->getLanguage();
103
104 $formatted = '';
105
106 $linkRenderer = $this->getLinkRenderer();
107
108 switch ( $name ) {
109 case 'ipb_timestamp':
110 $formatted = htmlspecialchars( $language->userTimeAndDate( $value, $this->getUser() ) );
111 break;
112
113 case 'ipb_target':
114 if ( $row->ipb_auto ) {
115 $formatted = $this->msg( 'autoblockid', $row->ipb_id )->parse();
116 } else {
117 list( $target, $type ) = DatabaseBlock::parseTarget( $row->ipb_address );
118 switch ( $type ) {
119 case DatabaseBlock::TYPE_USER:
120 case DatabaseBlock::TYPE_IP:
121 $formatted = Linker::userLink( $target->getId(), $target );
122 $formatted .= Linker::userToolLinks(
123 $target->getId(),
124 $target,
125 false,
126 Linker::TOOL_LINKS_NOBLOCK
127 );
128 break;
129 case DatabaseBlock::TYPE_RANGE:
130 $formatted = htmlspecialchars( $target );
131 }
132 }
133 break;
134
135 case 'ipb_expiry':
136 $formatted = htmlspecialchars( $language->formatExpiry(
137 $value,
138 /* User preference timezone */true
139 ) );
140 if ( $this->getUser()->isAllowed( 'block' ) ) {
141 if ( $row->ipb_auto ) {
142 $links[] = $linkRenderer->makeKnownLink(
143 SpecialPage::getTitleFor( 'Unblock' ),
144 $msg['unblocklink'],
145 [],
146 [ 'wpTarget' => "#{$row->ipb_id}" ]
147 );
148 } else {
149 $links[] = $linkRenderer->makeKnownLink(
150 SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
151 $msg['unblocklink']
152 );
153 $links[] = $linkRenderer->makeKnownLink(
154 SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
155 $msg['change-blocklink']
156 );
157 }
158 $formatted .= ' ' . Html::rawElement(
159 'span',
160 [ 'class' => 'mw-blocklist-actions' ],
161 $this->msg( 'parentheses' )->rawParams(
162 $language->pipeList( $links ) )->escaped()
163 );
164 }
165 if ( $value !== 'infinity' ) {
166 $timestamp = new MWTimestamp( $value );
167 $formatted .= '<br />' . $this->msg(
168 'ipb-blocklist-duration-left',
169 $language->formatDuration(
170 $timestamp->getTimestamp() - MWTimestamp::time(),
171 // reasonable output
172 [
173 'minutes',
174 'hours',
175 'days',
176 'years',
177 ]
178 )
179 )->escaped();
180 }
181 break;
182
183 case 'ipb_by':
184 if ( isset( $row->by_user_name ) ) {
185 $formatted = Linker::userLink( $value, $row->by_user_name );
186 $formatted .= Linker::userToolLinks( $value, $row->by_user_name );
187 } else {
188 $formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user?
189 }
190 break;
191
192 case 'ipb_reason':
193 $value = CommentStore::getStore()->getComment( 'ipb_reason', $row )->text;
194 $formatted = Linker::formatComment( $value );
195 break;
196
197 case 'ipb_params':
198 $properties = [];
199
200 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) && $row->ipb_sitewide ) {
201 $properties[] = htmlspecialchars( $msg['blocklist-editing-sitewide'] );
202 }
203
204 if ( !$row->ipb_sitewide && $this->restrictions ) {
205 $list = $this->getRestrictionListHTML( $row );
206 if ( $list ) {
207 $properties[] = htmlspecialchars( $msg['blocklist-editing'] ) . $list;
208 }
209 }
210
211 if ( $row->ipb_anon_only ) {
212 $properties[] = htmlspecialchars( $msg['anononlyblock'] );
213 }
214 if ( $row->ipb_create_account ) {
215 $properties[] = htmlspecialchars( $msg['createaccountblock'] );
216 }
217 if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
218 $properties[] = htmlspecialchars( $msg['noautoblockblock'] );
219 }
220
221 if ( $row->ipb_block_email ) {
222 $properties[] = htmlspecialchars( $msg['emailblock'] );
223 }
224
225 if ( !$row->ipb_allow_usertalk ) {
226 $properties[] = htmlspecialchars( $msg['blocklist-nousertalk'] );
227 }
228
229 $formatted = Html::rawElement(
230 'ul',
231 [],
232 implode( '', array_map( function ( $prop ) {
233 return Html::rawElement(
234 'li',
235 [],
236 $prop
237 );
238 }, $properties ) )
239 );
240 break;
241
242 default:
243 $formatted = "Unable to format $name";
244 break;
245 }
246
247 return $formatted;
248 }
249
250 /**
251 * Get Restriction List HTML
252 *
253 * @param stdClass $row
254 *
255 * @return string
256 */
257 private function getRestrictionListHTML( stdClass $row ) {
258 $items = [];
259 $linkRenderer = $this->getLinkRenderer();
260
261 foreach ( $this->restrictions as $restriction ) {
262 if ( $restriction->getBlockId() !== (int)$row->ipb_id ) {
263 continue;
264 }
265
266 switch ( $restriction->getType() ) {
267 case PageRestriction::TYPE:
268 if ( $restriction->getTitle() ) {
269 $items[$restriction->getType()][] = Html::rawElement(
270 'li',
271 [],
272 $linkRenderer->makeLink( $restriction->getTitle() )
273 );
274 }
275 break;
276 case NamespaceRestriction::TYPE:
277 $text = $restriction->getValue() === NS_MAIN
278 ? $this->msg( 'blanknamespace' )->text()
279 : $this->getLanguage()->getFormattedNsText(
280 $restriction->getValue()
281 );
282 $items[$restriction->getType()][] = Html::rawElement(
283 'li',
284 [],
285 $linkRenderer->makeLink(
286 SpecialPage::getTitleValueFor( 'Allpages' ),
287 $text,
288 [],
289 [
290 'namespace' => $restriction->getValue()
291 ]
292 )
293 );
294 break;
295 }
296 }
297
298 if ( empty( $items ) ) {
299 return '';
300 }
301
302 $sets = [];
303 foreach ( $items as $key => $value ) {
304 $sets[] = Html::rawElement(
305 'li',
306 [],
307 $this->msg( 'blocklist-editing-' . $key ) . Html::rawElement(
308 'ul',
309 [],
310 implode( '', $value )
311 )
312 );
313 }
314
315 return Html::rawElement(
316 'ul',
317 [],
318 implode( '', $sets )
319 );
320 }
321
322 function getQueryInfo() {
323 $commentQuery = CommentStore::getStore()->getJoin( 'ipb_reason' );
324 $actorQuery = ActorMigration::newMigration()->getJoin( 'ipb_by' );
325
326 $info = [
327 'tables' => array_merge(
328 [ 'ipblocks' ], $commentQuery['tables'], $actorQuery['tables'], [ 'user' ]
329 ),
330 'fields' => [
331 'ipb_id',
332 'ipb_address',
333 'ipb_user',
334 'by_user_name' => 'user_name',
335 'ipb_timestamp',
336 'ipb_auto',
337 'ipb_anon_only',
338 'ipb_create_account',
339 'ipb_enable_autoblock',
340 'ipb_expiry',
341 'ipb_range_start',
342 'ipb_range_end',
343 'ipb_deleted',
344 'ipb_block_email',
345 'ipb_allow_usertalk',
346 'ipb_sitewide',
347 ] + $commentQuery['fields'] + $actorQuery['fields'],
348 'conds' => $this->conds,
349 'join_conds' => [
350 'user' => [ 'LEFT JOIN', 'user_id = ' . $actorQuery['fields']['ipb_by'] ]
351 ] + $commentQuery['joins'] + $actorQuery['joins']
352 ];
353
354 # Filter out any expired blocks
355 $db = $this->getDatabase();
356 $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
357
358 # Is the user allowed to see hidden blocks?
359 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
360 $info['conds']['ipb_deleted'] = 0;
361 }
362
363 return $info;
364 }
365
366 /**
367 * Get total number of autoblocks at any given time
368 *
369 * @return int Total number of unexpired active autoblocks
370 */
371 function getTotalAutoblocks() {
372 $dbr = $this->getDatabase();
373 $res = $dbr->selectField( 'ipblocks',
374 'COUNT(*)',
375 [
376 'ipb_auto' => '1',
377 'ipb_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ),
378 ]
379 );
380 if ( $res ) {
381 return $res;
382 }
383 return 0; // We found nothing
384 }
385
386 protected function getTableClass() {
387 return parent::getTableClass() . ' mw-blocklist';
388 }
389
390 function getIndexField() {
391 return 'ipb_timestamp';
392 }
393
394 function getDefaultSort() {
395 return 'ipb_timestamp';
396 }
397
398 function isFieldSortable( $name ) {
399 return false;
400 }
401
402 /**
403 * Do a LinkBatch query to minimise database load when generating all these links
404 * @param IResultWrapper $result
405 */
406 function preprocessResults( $result ) {
407 # Do a link batch query
408 $lb = new LinkBatch;
409 $lb->setCaller( __METHOD__ );
410
411 $partialBlocks = [];
412 foreach ( $result as $row ) {
413 $lb->add( NS_USER, $row->ipb_address );
414 $lb->add( NS_USER_TALK, $row->ipb_address );
415
416 if ( isset( $row->by_user_name ) ) {
417 $lb->add( NS_USER, $row->by_user_name );
418 $lb->add( NS_USER_TALK, $row->by_user_name );
419 }
420
421 if ( !$row->ipb_sitewide ) {
422 $partialBlocks[] = $row->ipb_id;
423 }
424 }
425
426 if ( $partialBlocks ) {
427 // Mutations to the $row object are not persisted. The restrictions will
428 // need be stored in a separate store.
429 $blockRestrictionStore = MediaWikiServices::getInstance()->getBlockRestrictionStore();
430 $this->restrictions = $blockRestrictionStore->loadByBlockId( $partialBlocks );
431 }
432
433 $lb->execute();
434 }
435
436 }