MediaWiki  master
BlockListPager.php
Go to the documentation of this file.
1 <?php
22 namespace MediaWiki\Pager;
23 
24 use IContextSource;
43 use stdClass;
44 use Wikimedia\IPUtils;
47 
51 class BlockListPager extends TablePager {
52 
53  protected $conds;
54 
60  protected $restrictions = [];
61 
62  private BlockActionInfo $blockActionInfo;
63  private BlockRestrictionStore $blockRestrictionStore;
64  private BlockUtils $blockUtils;
65  private CommentStore $commentStore;
66  private LinkBatchFactory $linkBatchFactory;
67  private RowCommentFormatter $rowCommentFormatter;
68  private SpecialPageFactory $specialPageFactory;
69 
71  private $formattedComments = [];
72 
86  public function __construct(
87  IContextSource $context,
88  BlockActionInfo $blockActionInfo,
89  BlockRestrictionStore $blockRestrictionStore,
90  BlockUtils $blockUtils,
91  CommentStore $commentStore,
92  LinkBatchFactory $linkBatchFactory,
93  LinkRenderer $linkRenderer,
94  IConnectionProvider $dbProvider,
95  RowCommentFormatter $rowCommentFormatter,
96  SpecialPageFactory $specialPageFactory,
97  $conds
98  ) {
99  // Set database before parent constructor to avoid setting it there with wfGetDB
100  $this->mDb = $dbProvider->getReplicaDatabase();
101  parent::__construct( $context, $linkRenderer );
102  $this->blockActionInfo = $blockActionInfo;
103  $this->blockRestrictionStore = $blockRestrictionStore;
104  $this->blockUtils = $blockUtils;
105  $this->commentStore = $commentStore;
106  $this->linkBatchFactory = $linkBatchFactory;
107  $this->rowCommentFormatter = $rowCommentFormatter;
108  $this->specialPageFactory = $specialPageFactory;
109  $this->conds = $conds;
110  $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
111  }
112 
113  protected function getFieldNames() {
114  static $headers = null;
115 
116  if ( $headers === null ) {
117  $headers = [
118  'ipb_timestamp' => 'blocklist-timestamp',
119  'ipb_target' => 'blocklist-target',
120  'ipb_expiry' => 'blocklist-expiry',
121  'ipb_by' => 'blocklist-by',
122  'ipb_params' => 'blocklist-params',
123  'ipb_reason' => 'blocklist-reason',
124  ];
125  foreach ( $headers as $key => $val ) {
126  $headers[$key] = $this->msg( $val )->text();
127  }
128  }
129 
130  return $headers;
131  }
132 
139  public function formatValue( $name, $value ) {
140  static $msg = null;
141  if ( $msg === null ) {
142  $keys = [
143  'anononlyblock',
144  'createaccountblock',
145  'noautoblockblock',
146  'emailblock',
147  'blocklist-nousertalk',
148  'unblocklink',
149  'change-blocklink',
150  'blocklist-editing',
151  'blocklist-editing-sitewide',
152  ];
153 
154  foreach ( $keys as $key ) {
155  $msg[$key] = $this->msg( $key )->text();
156  }
157  }
158  '@phan-var string[] $msg';
159 
161  $row = $this->mCurrentRow;
162 
163  $language = $this->getLanguage();
164 
165  $formatted = '';
166 
167  $linkRenderer = $this->getLinkRenderer();
168 
169  switch ( $name ) {
170  case 'ipb_timestamp':
171  $formatted = htmlspecialchars( $language->userTimeAndDate( $value, $this->getUser() ) );
172  break;
173 
174  case 'ipb_target':
175  if ( $row->ipb_auto ) {
176  $formatted = $this->msg( 'autoblockid', $row->ipb_id )->parse();
177  } else {
178  [ $target, ] = $this->blockUtils->parseBlockTarget( $row->ipb_address );
179 
180  if ( is_string( $target ) ) {
181  if ( IPUtils::isValidRange( $target ) ) {
182  $target = User::newFromName( $target, false );
183  } else {
184  $formatted = $target;
185  }
186  }
187 
188  if ( $target instanceof UserIdentity ) {
189  $formatted = Linker::userLink( $target->getId(), $target->getName() );
190  $formatted .= Linker::userToolLinks(
191  $target->getId(),
192  $target->getName(),
193  false,
195  );
196  }
197  }
198  break;
199 
200  case 'ipb_expiry':
201  $formatted = htmlspecialchars( $language->formatExpiry(
202  $value,
203  /* User preference timezone */true,
204  'infinity',
205  $this->getUser()
206  ) );
207  if ( $this->getAuthority()->isAllowed( 'block' ) ) {
208  $links = [];
209  if ( $row->ipb_auto ) {
210  $links[] = $linkRenderer->makeKnownLink(
211  $this->specialPageFactory->getTitleForAlias( 'Unblock' ),
212  $msg['unblocklink'],
213  [],
214  [ 'wpTarget' => "#{$row->ipb_id}" ]
215  );
216  } else {
217  $links[] = $linkRenderer->makeKnownLink(
218  $this->specialPageFactory->getTitleForAlias( 'Unblock/' . $row->ipb_address ),
219  $msg['unblocklink']
220  );
221  $links[] = $linkRenderer->makeKnownLink(
222  $this->specialPageFactory->getTitleForAlias( 'Block/' . $row->ipb_address ),
223  $msg['change-blocklink']
224  );
225  }
226  $formatted .= ' ' . Html::rawElement(
227  'span',
228  [ 'class' => 'mw-blocklist-actions' ],
229  $this->msg( 'parentheses' )->rawParams(
230  $language->pipeList( $links ) )->escaped()
231  );
232  }
233  if ( $value !== 'infinity' ) {
234  $timestamp = new MWTimestamp( $value );
235  $formatted .= '<br />' . $this->msg(
236  'ipb-blocklist-duration-left',
237  $language->formatDuration(
238  (int)$timestamp->getTimestamp( TS_UNIX ) - MWTimestamp::time(),
239  // reasonable output
240  [
241  'minutes',
242  'hours',
243  'days',
244  'years',
245  ]
246  )
247  )->escaped();
248  }
249  break;
250 
251  case 'ipb_by':
252  $formatted = Linker::userLink( (int)$value, $row->ipb_by_text );
253  $formatted .= Linker::userToolLinks( (int)$value, $row->ipb_by_text );
254  break;
255 
256  case 'ipb_reason':
257  $formatted = $this->formattedComments[$this->getResultOffset()];
258  break;
259 
260  case 'ipb_params':
261  $properties = [];
262 
263  if ( $row->ipb_sitewide ) {
264  $properties[] = htmlspecialchars( $msg['blocklist-editing-sitewide'] );
265  }
266 
267  if ( !$row->ipb_sitewide && $this->restrictions ) {
268  $list = $this->getRestrictionListHTML( $row );
269  if ( $list ) {
270  $properties[] = htmlspecialchars( $msg['blocklist-editing'] ) . $list;
271  }
272  }
273 
274  if ( $row->ipb_anon_only ) {
275  $properties[] = htmlspecialchars( $msg['anononlyblock'] );
276  }
277  if ( $row->ipb_create_account ) {
278  $properties[] = htmlspecialchars( $msg['createaccountblock'] );
279  }
280  if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
281  $properties[] = htmlspecialchars( $msg['noautoblockblock'] );
282  }
283 
284  if ( $row->ipb_block_email ) {
285  $properties[] = htmlspecialchars( $msg['emailblock'] );
286  }
287 
288  if ( !$row->ipb_allow_usertalk ) {
289  $properties[] = htmlspecialchars( $msg['blocklist-nousertalk'] );
290  }
291 
292  $formatted = Html::rawElement(
293  'ul',
294  [],
295  implode( '', array_map( static function ( $prop ) {
296  return Html::rawElement(
297  'li',
298  [],
299  $prop
300  );
301  }, $properties ) )
302  );
303  break;
304 
305  default:
306  $formatted = "Unable to format $name";
307  break;
308  }
309 
310  return $formatted;
311  }
312 
320  private function getRestrictionListHTML( stdClass $row ) {
321  $items = [];
322  $linkRenderer = $this->getLinkRenderer();
323 
324  foreach ( $this->restrictions as $restriction ) {
325  if ( $restriction->getBlockId() !== (int)$row->ipb_id ) {
326  continue;
327  }
328 
329  switch ( $restriction->getType() ) {
331  '@phan-var PageRestriction $restriction';
332  if ( $restriction->getTitle() ) {
333  $items[$restriction->getType()][] = Html::rawElement(
334  'li',
335  [],
336  $linkRenderer->makeLink( $restriction->getTitle() )
337  );
338  }
339  break;
341  $text = $restriction->getValue() === NS_MAIN
342  ? $this->msg( 'blanknamespace' )->text()
343  : $this->getLanguage()->getFormattedNsText(
344  $restriction->getValue()
345  );
346  if ( $text ) {
347  $items[$restriction->getType()][] = Html::rawElement(
348  'li',
349  [],
350  $linkRenderer->makeLink(
351  $this->specialPageFactory->getTitleForAlias( 'Allpages' ),
352  $text,
353  [],
354  [
355  'namespace' => $restriction->getValue()
356  ]
357  )
358  );
359  }
360  break;
362  $actionName = $this->blockActionInfo->getActionFromId( $restriction->getValue() );
363  $enablePartialActionBlocks =
365  if ( $actionName && $enablePartialActionBlocks ) {
366  $items[$restriction->getType()][] = Html::rawElement(
367  'li',
368  [],
369  $this->msg( 'ipb-action-' .
370  $this->blockActionInfo->getActionFromId( $restriction->getValue() ) )->escaped()
371  );
372  }
373  break;
374  }
375  }
376 
377  if ( !$items ) {
378  return '';
379  }
380 
381  $sets = [];
382  foreach ( $items as $key => $value ) {
383  $sets[] = Html::rawElement(
384  'li',
385  [],
386  $this->msg( 'blocklist-editing-' . $key ) . Html::rawElement(
387  'ul',
388  [],
389  implode( '', $value )
390  )
391  );
392  }
393 
394  return Html::rawElement(
395  'ul',
396  [],
397  implode( '', $sets )
398  );
399  }
400 
401  public function getQueryInfo() {
402  $commentQuery = $this->commentStore->getJoin( 'ipb_reason' );
403 
404  $info = [
405  'tables' => array_merge(
406  [ 'ipblocks', 'ipblocks_by_actor' => 'actor' ],
407  $commentQuery['tables']
408  ),
409  'fields' => [
410  'ipb_id',
411  'ipb_address',
412  'ipb_user',
413  'ipb_by' => 'ipblocks_by_actor.actor_user',
414  'ipb_by_text' => 'ipblocks_by_actor.actor_name',
415  'ipb_timestamp',
416  'ipb_auto',
417  'ipb_anon_only',
418  'ipb_create_account',
419  'ipb_enable_autoblock',
420  'ipb_expiry',
421  'ipb_range_start',
422  'ipb_range_end',
423  'ipb_deleted',
424  'ipb_block_email',
425  'ipb_allow_usertalk',
426  'ipb_sitewide',
427  ] + $commentQuery['fields'],
428  'conds' => $this->conds,
429  'join_conds' => [
430  'ipblocks_by_actor' => [ 'JOIN', 'actor_id=ipb_by_actor' ]
431  ] + $commentQuery['joins']
432  ];
433 
434  # Filter out any expired blocks
435  $db = $this->getDatabase();
436  $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
437 
438  # Is the user allowed to see hidden blocks?
439  if ( !$this->getAuthority()->isAllowed( 'hideuser' ) ) {
440  $info['conds']['ipb_deleted'] = 0;
441  }
442 
443  return $info;
444  }
445 
451  public function getTotalAutoblocks() {
452  $dbr = $this->getDatabase();
453  return (int)$dbr->newSelectQueryBuilder()
454  ->select( 'COUNT(*)' )
455  ->from( 'ipblocks' )
456  ->where( [ 'ipb_auto' => '1', 'ipb_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ), ] )
457  ->caller( __METHOD__ )->fetchField();
458  }
459 
460  protected function getTableClass() {
461  return parent::getTableClass() . ' mw-blocklist';
462  }
463 
464  public function getIndexField() {
465  return [ [ 'ipb_timestamp', 'ipb_id' ] ];
466  }
467 
468  public function getDefaultSort() {
469  return '';
470  }
471 
472  protected function isFieldSortable( $name ) {
473  return false;
474  }
475 
480  public function preprocessResults( $result ) {
481  // Do a link batch query
482  $lb = $this->linkBatchFactory->newLinkBatch();
483  $lb->setCaller( __METHOD__ );
484 
485  $partialBlocks = [];
486  foreach ( $result as $row ) {
487  $lb->add( NS_USER, $row->ipb_address );
488  $lb->add( NS_USER_TALK, $row->ipb_address );
489 
490  if ( $row->ipb_by ?? null ) {
491  $lb->add( NS_USER, $row->ipb_by_text );
492  $lb->add( NS_USER_TALK, $row->ipb_by_text );
493  }
494 
495  if ( !$row->ipb_sitewide ) {
496  $partialBlocks[] = (int)$row->ipb_id;
497  }
498  }
499 
500  if ( $partialBlocks ) {
501  // Mutations to the $row object are not persisted. The restrictions will
502  // need be stored in a separate store.
503  $this->restrictions = $this->blockRestrictionStore->loadByBlockId( $partialBlocks );
504 
505  foreach ( $this->restrictions as $restriction ) {
506  if ( $restriction->getType() === PageRestriction::TYPE ) {
507  '@phan-var PageRestriction $restriction';
508  $title = $restriction->getTitle();
509  if ( $title ) {
510  $lb->addObj( $title );
511  }
512  }
513  }
514  }
515 
516  $lb->execute();
517 
518  // Format comments
519  // The keys of formattedComments will be the corresponding offset into $result
520  $this->formattedComments = $this->rowCommentFormatter->formatRows( $result, 'ipb_reason' );
521  }
522 
523 }
524 
529 class_alias( BlockListPager::class, 'BlockListPager' );
const NS_USER
Definition: Defines.php:66
const NS_MAIN
Definition: Defines.php:64
const NS_USER_TALK
Definition: Defines.php:67
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Defines the actions that can be blocked by a partial block.
Backend class for blocking utils.
Definition: BlockUtils.php:46
Restriction for partial blocks of actions.
This is basically a CommentFormatter with a CommentStore dependency, allowing it to retrieve comment ...
Handle database storage of comments such as edit summaries and log reasons.
This class is a collection of static functions that serve two purposes:
Definition: Html.php:57
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:239
Class that generates HTML for internal links.
Some internal bits split of from Skin.php.
Definition: Linker.php:65
static userToolLinks( $userId, $userText, $redContribsWhenNoEdits=false, $flags=0, $edits=null, $useParentheses=true)
Generate standard user tool links (talk, contributions, block link, etc.)
Definition: Linker.php:1248
const TOOL_LINKS_NOBLOCK
Flags for userToolLinks()
Definition: Linker.php:69
static userLink( $userId, $userName, $altUserName=false, $attributes=[])
Make user link (or user contributions for unregistered users)
Definition: Linker.php:1190
A class containing constants representing the names of configuration variables.
const EnablePartialActionBlocks
Name constant for the EnablePartialActionBlocks setting, for use with Config::get()
preprocessResults( $result)
Do a LinkBatch query to minimise database load when generating all these links.
Restriction[] $restrictions
Array of restrictions.
getTableClass()
TablePager relies on mw-datatable for styling, see T214208.
getTotalAutoblocks()
Get total number of autoblocks at any given time.
getFieldNames()
An array mapping database field names to a textual description of the field name, for use in the tabl...
__construct(IContextSource $context, BlockActionInfo $blockActionInfo, BlockRestrictionStore $blockRestrictionStore, BlockUtils $blockUtils, CommentStore $commentStore, LinkBatchFactory $linkBatchFactory, LinkRenderer $linkRenderer, IConnectionProvider $dbProvider, RowCommentFormatter $rowCommentFormatter, SpecialPageFactory $specialPageFactory, $conds)
getIndexField()
Returns the name of the index field.If the pager supports multiple orders, it may return an array of ...
getQueryInfo()
Provides all parameters needed for the main paged query.
isFieldSortable( $name)
Return true if the named field should be sortable by the UI, false otherwise.
getDefaultSort()
The database field name used as a default sort order.
getDatabase()
Get the Database object in use.
Definition: IndexPager.php:256
const DIR_DESCENDING
Backwards-compatible constant for $mDefaultDirection field (do not change)
Definition: IndexPager.php:86
Table-based display with a user-selectable sort order.
Definition: TablePager.php:36
Factory for handling the special page list and generating SpecialPage objects.
internal since 1.36
Definition: User.php:98
static newFromName( $name, $validate='valid')
Definition: User.php:600
Library for creating and parsing MW-style timestamps.
Definition: MWTimestamp.php:48
Interface for objects which can provide a MediaWiki context on request.
Interface for objects representing user identity.
Provide primary and replica IDatabase connections.
getReplicaDatabase( $domain=false, $group=null)
Get connection to a replica database.
Result wrapper for grabbing data queried from an IDatabase object.