MediaWiki  master
BlockListPager.php
Go to the documentation of this file.
1 <?php
31 use Wikimedia\IPUtils;
34 
38 class BlockListPager extends TablePager {
39 
40  protected $conds;
41 
47  protected $restrictions = [];
48 
51 
54 
57 
60 
62  private $actorMigration;
63 
65  private $commentStore;
66 
78  public function __construct(
79  $page,
80  $conds,
84  ILoadBalancer $loadBalancer,
88  ) {
89  $this->mDb = $loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA );
90  parent::__construct( $page->getContext(), $page->getLinkRenderer() );
91  $this->conds = $conds;
92  $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
93  $this->linkBatchFactory = $linkBatchFactory;
94  $this->blockRestrictionStore = $blockRestrictionStore;
95  $this->permissionManager = $permissionManager;
96  $this->specialPageFactory = $specialPageFactory;
97  $this->actorMigration = $actorMigration;
98  $this->commentStore = $commentStore;
99  }
100 
101  protected function getFieldNames() {
102  static $headers = null;
103 
104  if ( $headers === null ) {
105  $headers = [
106  'ipb_timestamp' => 'blocklist-timestamp',
107  'ipb_target' => 'blocklist-target',
108  'ipb_expiry' => 'blocklist-expiry',
109  'ipb_by' => 'blocklist-by',
110  'ipb_params' => 'blocklist-params',
111  'ipb_reason' => 'blocklist-reason',
112  ];
113  foreach ( $headers as $key => $val ) {
114  $headers[$key] = $this->msg( $val )->text();
115  }
116  }
117 
118  return $headers;
119  }
120 
127  public function formatValue( $name, $value ) {
128  static $msg = null;
129  if ( $msg === null ) {
130  $keys = [
131  'anononlyblock',
132  'createaccountblock',
133  'noautoblockblock',
134  'emailblock',
135  'blocklist-nousertalk',
136  'unblocklink',
137  'change-blocklink',
138  'blocklist-editing',
139  'blocklist-editing-sitewide',
140  ];
141 
142  foreach ( $keys as $key ) {
143  $msg[$key] = $this->msg( $key )->text();
144  }
145  }
146  '@phan-var string[] $msg';
147 
149  $row = $this->mCurrentRow;
150 
151  $language = $this->getLanguage();
152 
153  $formatted = '';
154 
155  $linkRenderer = $this->getLinkRenderer();
156 
157  switch ( $name ) {
158  case 'ipb_timestamp':
159  $formatted = htmlspecialchars( $language->userTimeAndDate( $value, $this->getUser() ) );
160  break;
161 
162  case 'ipb_target':
163  if ( $row->ipb_auto ) {
164  $formatted = $this->msg( 'autoblockid', $row->ipb_id )->parse();
165  } else {
166  list( $target, $type ) = DatabaseBlock::parseTarget( $row->ipb_address );
167 
168  if ( is_string( $target ) ) {
169  if ( IPUtils::isValidRange( $target ) ) {
170  $target = User::newFromName( $target, false );
171  } else {
172  $formatted = $target;
173  }
174  }
175 
176  if ( $target instanceof UserIdentity ) {
177  $formatted = Linker::userLink( $target->getId(), $target->getName() );
178  $formatted .= Linker::userToolLinks(
179  $target->getId(),
180  $target->getName(),
181  false,
183  );
184  }
185  }
186  break;
187 
188  case 'ipb_expiry':
189  $formatted = htmlspecialchars( $language->formatExpiry(
190  $value,
191  /* User preference timezone */true
192  ) );
193  if ( $this->permissionManager->userHasRight( $this->getUser(), 'block' ) ) {
194  $links = [];
195  if ( $row->ipb_auto ) {
196  $links[] = $linkRenderer->makeKnownLink(
197  $this->specialPageFactory->getTitleForAlias( 'Unblock' ),
198  $msg['unblocklink'],
199  [],
200  [ 'wpTarget' => "#{$row->ipb_id}" ]
201  );
202  } else {
203  $links[] = $linkRenderer->makeKnownLink(
204  $this->specialPageFactory->getTitleForAlias( 'Unblock/' . $row->ipb_address ),
205  $msg['unblocklink']
206  );
207  $links[] = $linkRenderer->makeKnownLink(
208  $this->specialPageFactory->getTitleForAlias( 'Block/' . $row->ipb_address ),
209  $msg['change-blocklink']
210  );
211  }
212  $formatted .= ' ' . Html::rawElement(
213  'span',
214  [ 'class' => 'mw-blocklist-actions' ],
215  $this->msg( 'parentheses' )->rawParams(
216  $language->pipeList( $links ) )->escaped()
217  );
218  }
219  if ( $value !== 'infinity' ) {
220  $timestamp = new MWTimestamp( $value );
221  $formatted .= '<br />' . $this->msg(
222  'ipb-blocklist-duration-left',
223  $language->formatDuration(
224  $timestamp->getTimestamp() - MWTimestamp::time(),
225  // reasonable output
226  [
227  'minutes',
228  'hours',
229  'days',
230  'years',
231  ]
232  )
233  )->escaped();
234  }
235  break;
236 
237  case 'ipb_by':
238  if ( isset( $row->by_user_name ) ) {
239  $formatted = Linker::userLink( $value, $row->by_user_name );
240  $formatted .= Linker::userToolLinks( $value, $row->by_user_name );
241  } else {
242  $formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user?
243  }
244  break;
245 
246  case 'ipb_reason':
247  $value = $this->commentStore->getComment( 'ipb_reason', $row )->text;
248  $formatted = Linker::formatComment( $value );
249  break;
250 
251  case 'ipb_params':
252  $properties = [];
253 
254  if ( $row->ipb_sitewide ) {
255  $properties[] = htmlspecialchars( $msg['blocklist-editing-sitewide'] );
256  }
257 
258  if ( !$row->ipb_sitewide && $this->restrictions ) {
259  $list = $this->getRestrictionListHTML( $row );
260  if ( $list ) {
261  $properties[] = htmlspecialchars( $msg['blocklist-editing'] ) . $list;
262  }
263  }
264 
265  if ( $row->ipb_anon_only ) {
266  $properties[] = htmlspecialchars( $msg['anononlyblock'] );
267  }
268  if ( $row->ipb_create_account ) {
269  $properties[] = htmlspecialchars( $msg['createaccountblock'] );
270  }
271  if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
272  $properties[] = htmlspecialchars( $msg['noautoblockblock'] );
273  }
274 
275  if ( $row->ipb_block_email ) {
276  $properties[] = htmlspecialchars( $msg['emailblock'] );
277  }
278 
279  if ( !$row->ipb_allow_usertalk ) {
280  $properties[] = htmlspecialchars( $msg['blocklist-nousertalk'] );
281  }
282 
283  $formatted = Html::rawElement(
284  'ul',
285  [],
286  implode( '', array_map( function ( $prop ) {
287  return Html::rawElement(
288  'li',
289  [],
290  $prop
291  );
292  }, $properties ) )
293  );
294  break;
295 
296  default:
297  $formatted = "Unable to format $name";
298  break;
299  }
300 
301  return $formatted;
302  }
303 
311  private function getRestrictionListHTML( stdClass $row ) {
312  $items = [];
313  $linkRenderer = $this->getLinkRenderer();
314 
315  foreach ( $this->restrictions as $restriction ) {
316  if ( $restriction->getBlockId() !== (int)$row->ipb_id ) {
317  continue;
318  }
319 
320  switch ( $restriction->getType() ) {
321  case PageRestriction::TYPE:
322  '@phan-var PageRestriction $restriction';
323  if ( $restriction->getTitle() ) {
324  $items[$restriction->getType()][] = Html::rawElement(
325  'li',
326  [],
327  $linkRenderer->makeLink( $restriction->getTitle() )
328  );
329  }
330  break;
331  case NamespaceRestriction::TYPE:
332  $text = $restriction->getValue() === NS_MAIN
333  ? $this->msg( 'blanknamespace' )->text()
334  : $this->getLanguage()->getFormattedNsText(
335  $restriction->getValue()
336  );
337  $items[$restriction->getType()][] = Html::rawElement(
338  'li',
339  [],
340  $linkRenderer->makeLink(
341  $this->specialPageFactory->getTitleForAlias( 'Allpages' ),
342  $text,
343  [],
344  [
345  'namespace' => $restriction->getValue()
346  ]
347  )
348  );
349  break;
350  }
351  }
352 
353  if ( empty( $items ) ) {
354  return '';
355  }
356 
357  $sets = [];
358  foreach ( $items as $key => $value ) {
359  $sets[] = Html::rawElement(
360  'li',
361  [],
362  $this->msg( 'blocklist-editing-' . $key ) . Html::rawElement(
363  'ul',
364  [],
365  implode( '', $value )
366  )
367  );
368  }
369 
370  return Html::rawElement(
371  'ul',
372  [],
373  implode( '', $sets )
374  );
375  }
376 
377  public function getQueryInfo() {
378  $commentQuery = $this->commentStore->getJoin( 'ipb_reason' );
379  $actorQuery = $this->actorMigration->getJoin( 'ipb_by' );
380 
381  $info = [
382  'tables' => array_merge(
383  [ 'ipblocks' ], $commentQuery['tables'], $actorQuery['tables'], [ 'user' ]
384  ),
385  'fields' => [
386  'ipb_id',
387  'ipb_address',
388  'ipb_user',
389  'by_user_name' => 'user_name',
390  'ipb_timestamp',
391  'ipb_auto',
392  'ipb_anon_only',
393  'ipb_create_account',
394  'ipb_enable_autoblock',
395  'ipb_expiry',
396  'ipb_range_start',
397  'ipb_range_end',
398  'ipb_deleted',
399  'ipb_block_email',
400  'ipb_allow_usertalk',
401  'ipb_sitewide',
402  ] + $commentQuery['fields'] + $actorQuery['fields'],
403  'conds' => $this->conds,
404  'join_conds' => [
405  'user' => [ 'LEFT JOIN', 'user_id = ' . $actorQuery['fields']['ipb_by'] ]
406  ] + $commentQuery['joins'] + $actorQuery['joins']
407  ];
408 
409  # Filter out any expired blocks
410  $db = $this->getDatabase();
411  $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
412 
413  # Is the user allowed to see hidden blocks?
414  if ( !$this->permissionManager->userHasRight( $this->getUser(), 'hideuser' ) ) {
415  $info['conds']['ipb_deleted'] = 0;
416  }
417 
418  return $info;
419  }
420 
426  public function getTotalAutoblocks() {
427  $dbr = $this->getDatabase();
428  $res = $dbr->selectField( 'ipblocks',
429  'COUNT(*)',
430  [
431  'ipb_auto' => '1',
432  'ipb_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ),
433  ],
434  __METHOD__
435  );
436  if ( $res ) {
437  return $res;
438  }
439  return 0; // We found nothing
440  }
441 
442  protected function getTableClass() {
443  return parent::getTableClass() . ' mw-blocklist';
444  }
445 
446  public function getIndexField() {
447  return [ [ 'ipb_timestamp', 'ipb_id' ] ];
448  }
449 
450  public function getDefaultSort() {
451  return '';
452  }
453 
454  protected function isFieldSortable( $name ) {
455  return false;
456  }
457 
462  public function preprocessResults( $result ) {
463  # Do a link batch query
464  $lb = $this->linkBatchFactory->newLinkBatch();
465  $lb->setCaller( __METHOD__ );
466 
467  $partialBlocks = [];
468  foreach ( $result as $row ) {
469  $lb->add( NS_USER, $row->ipb_address );
470  $lb->add( NS_USER_TALK, $row->ipb_address );
471 
472  if ( isset( $row->by_user_name ) ) {
473  $lb->add( NS_USER, $row->by_user_name );
474  $lb->add( NS_USER_TALK, $row->by_user_name );
475  }
476 
477  if ( !$row->ipb_sitewide ) {
478  $partialBlocks[] = $row->ipb_id;
479  }
480  }
481 
482  if ( $partialBlocks ) {
483  // Mutations to the $row object are not persisted. The restrictions will
484  // need be stored in a separate store.
485  $this->restrictions = $this->blockRestrictionStore->loadByBlockId( $partialBlocks );
486 
487  foreach ( $this->restrictions as $restriction ) {
488  if ( $restriction->getType() === PageRestriction::TYPE ) {
489  '@phan-var PageRestriction $restriction';
490  $title = $restriction->getTitle();
491  if ( $title !== null ) {
492  $lb->addObj( $title );
493  }
494  }
495  }
496  }
497 
498  $lb->execute();
499  }
500 
501 }
MWTimestamp
Library for creating and parsing MW-style timestamps.
Definition: MWTimestamp.php:34
Linker\TOOL_LINKS_NOBLOCK
const TOOL_LINKS_NOBLOCK
Flags for userToolLinks()
Definition: Linker.php:40
BlockListPager\isFieldSortable
isFieldSortable( $name)
Return true if the named field should be sortable by the UI, false otherwise.
Definition: BlockListPager.php:454
Linker\userLink
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition: Linker.php:896
BlockListPager
Definition: BlockListPager.php:38
MediaWiki\SpecialPage\SpecialPageFactory
Factory for handling the special page list and generating SpecialPage objects.
Definition: SpecialPageFactory.php:61
Linker\userToolLinks
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:941
BlockListPager\$blockRestrictionStore
BlockRestrictionStore $blockRestrictionStore
Definition: BlockListPager.php:53
IndexPager\getDatabase
getDatabase()
Get the Database object in use.
Definition: IndexPager.php:244
CommentStore
Handle database storage of comments such as edit summaries and log reasons.
Definition: CommentStore.php:42
BlockListPager\getRestrictionListHTML
getRestrictionListHTML(stdClass $row)
Get Restriction List HTML.
Definition: BlockListPager.php:311
User\newFromName
static newFromName( $name, $validate='valid')
Definition: User.php:545
BlockListPager\$conds
$conds
Definition: BlockListPager.php:40
IndexPager\$linkRenderer
LinkRenderer $linkRenderer
Definition: IndexPager.php:167
ActorMigration
This class handles the logic for the actor table migration and should always be used in lieu of direc...
Definition: ActorMigration.php:41
$res
$res
Definition: testCompression.php:57
Wikimedia\Rdbms\ILoadBalancer\getConnectionRef
getConnectionRef( $i, $groups=[], $domain=false, $flags=0)
Get a live database handle reference for a real or virtual (DB_MASTER/DB_REPLICA) server index.
BlockListPager\$linkBatchFactory
LinkBatchFactory $linkBatchFactory
Definition: BlockListPager.php:50
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
$dbr
$dbr
Definition: testCompression.php:54
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:143
BlockListPager\$commentStore
CommentStore $commentStore
Definition: BlockListPager.php:65
NS_MAIN
const NS_MAIN
Definition: Defines.php:69
MediaWiki\Block\DatabaseBlock
A DatabaseBlock (unlike a SystemBlock) is stored in the database, may give rise to autoblocks and may...
Definition: DatabaseBlock.php:50
Wikimedia\Rdbms\IResultWrapper
Result wrapper for grabbing data queried from an IDatabase object.
Definition: IResultWrapper.php:24
MediaWiki\Cache\LinkBatchFactory
Definition: LinkBatchFactory.php:38
BlockListPager\getTableClass
getTableClass()
TablePager relies on mw-datatable for styling, see T214208.
Definition: BlockListPager.php:442
TablePager
Table-based display with a user-selectable sort order Stable to extend.
Definition: TablePager.php:31
MediaWiki\Block\Restriction\Restriction
Definition: Restriction.php:25
TablePager\$mCurrentRow
stdClass $mCurrentRow
Definition: TablePager.php:36
BlockListPager\getFieldNames
getFieldNames()
An array mapping database field names to a textual description of the field name, for use in the tabl...
Definition: BlockListPager.php:101
BlockListPager\$restrictions
Restriction[] $restrictions
Array of restrictions.
Definition: BlockListPager.php:47
BlockListPager\getDefaultSort
getDefaultSort()
The database field name used as a default sort order.
Definition: BlockListPager.php:450
$title
$title
Definition: testCompression.php:38
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:187
BlockListPager\getIndexField
getIndexField()
Returns the name of the index field.If the pager supports multiple orders, it may return an array of ...
Definition: BlockListPager.php:446
MediaWiki\Permissions\PermissionManager
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Definition: PermissionManager.php:50
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:72
BlockListPager\__construct
__construct( $page, $conds, LinkBatchFactory $linkBatchFactory, BlockRestrictionStore $blockRestrictionStore, PermissionManager $permissionManager, ILoadBalancer $loadBalancer, SpecialPageFactory $specialPageFactory, ActorMigration $actorMigration, CommentStore $commentStore)
Definition: BlockListPager.php:78
BlockListPager\getQueryInfo
getQueryInfo()
Provides all parameters needed for the main paged query.
Definition: BlockListPager.php:377
BlockListPager\getTotalAutoblocks
getTotalAutoblocks()
Get total number of autoblocks at any given time.
Definition: BlockListPager.php:426
IndexPager\getLinkRenderer
getLinkRenderer()
Definition: IndexPager.php:976
Linker\formatComment
static formatComment( $comment, $title=null, $local=false, $wikiId=null)
This function is called by all recent changes variants, by the page history, and by the user contribu...
Definition: Linker.php:1199
MediaWiki\Block\Restriction\NamespaceRestriction
Definition: NamespaceRestriction.php:25
IndexPager\DIR_DESCENDING
const DIR_DESCENDING
Backwards-compatible constant for $mDefaultDirection field (do not change)
Definition: IndexPager.php:80
MediaWiki\Block\Restriction\PageRestriction
Definition: PageRestriction.php:25
BlockListPager\preprocessResults
preprocessResults( $result)
Do a LinkBatch query to minimise database load when generating all these links.
Definition: BlockListPager.php:462
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:212
$keys
$keys
Definition: testCompression.php:72
NS_USER
const NS_USER
Definition: Defines.php:71
BlockListPager\$permissionManager
PermissionManager $permissionManager
Definition: BlockListPager.php:56
BlockListPager\$actorMigration
ActorMigration $actorMigration
Definition: BlockListPager.php:62
BlockListPager\$specialPageFactory
SpecialPageFactory $specialPageFactory
Definition: BlockListPager.php:59
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81
BlockListPager\formatValue
formatValue( $name, $value)
Definition: BlockListPager.php:127
MediaWiki\Block\BlockRestrictionStore
Definition: BlockRestrictionStore.php:34
$type
$type
Definition: testCompression.php:52