MediaWiki  master
BlockListPager.php
Go to the documentation of this file.
1 <?php
30 use Wikimedia\IPUtils;
33 
37 class BlockListPager extends TablePager {
38 
39  protected $conds;
40 
46  protected $restrictions = [];
47 
50 
53 
56 
58  private $actorMigration;
59 
61  private $commentStore;
62 
73  public function __construct(
74  $page,
75  $conds,
78  ILoadBalancer $loadBalancer,
82  ) {
83  $this->mDb = $loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA );
84  parent::__construct( $page->getContext(), $page->getLinkRenderer() );
85  $this->conds = $conds;
86  $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
87  $this->linkBatchFactory = $linkBatchFactory;
88  $this->blockRestrictionStore = $blockRestrictionStore;
89  $this->specialPageFactory = $specialPageFactory;
90  $this->actorMigration = $actorMigration;
91  $this->commentStore = $commentStore;
92  }
93 
94  protected function getFieldNames() {
95  static $headers = null;
96 
97  if ( $headers === null ) {
98  $headers = [
99  'ipb_timestamp' => 'blocklist-timestamp',
100  'ipb_target' => 'blocklist-target',
101  'ipb_expiry' => 'blocklist-expiry',
102  'ipb_by' => 'blocklist-by',
103  'ipb_params' => 'blocklist-params',
104  'ipb_reason' => 'blocklist-reason',
105  ];
106  foreach ( $headers as $key => $val ) {
107  $headers[$key] = $this->msg( $val )->text();
108  }
109  }
110 
111  return $headers;
112  }
113 
120  public function formatValue( $name, $value ) {
121  static $msg = null;
122  if ( $msg === null ) {
123  $keys = [
124  'anononlyblock',
125  'createaccountblock',
126  'noautoblockblock',
127  'emailblock',
128  'blocklist-nousertalk',
129  'unblocklink',
130  'change-blocklink',
131  'blocklist-editing',
132  'blocklist-editing-sitewide',
133  ];
134 
135  foreach ( $keys as $key ) {
136  $msg[$key] = $this->msg( $key )->text();
137  }
138  }
139  '@phan-var string[] $msg';
140 
142  $row = $this->mCurrentRow;
143 
144  $language = $this->getLanguage();
145 
146  $formatted = '';
147 
148  $linkRenderer = $this->getLinkRenderer();
149 
150  switch ( $name ) {
151  case 'ipb_timestamp':
152  $formatted = htmlspecialchars( $language->userTimeAndDate( $value, $this->getUser() ) );
153  break;
154 
155  case 'ipb_target':
156  if ( $row->ipb_auto ) {
157  $formatted = $this->msg( 'autoblockid', $row->ipb_id )->parse();
158  } else {
159  list( $target, $type ) = DatabaseBlock::parseTarget( $row->ipb_address );
160 
161  if ( is_string( $target ) ) {
162  if ( IPUtils::isValidRange( $target ) ) {
163  $target = User::newFromName( $target, false );
164  } else {
165  $formatted = $target;
166  }
167  }
168 
169  if ( $target instanceof UserIdentity ) {
170  $formatted = Linker::userLink( $target->getId(), $target->getName() );
171  $formatted .= Linker::userToolLinks(
172  $target->getId(),
173  $target->getName(),
174  false,
176  );
177  }
178  }
179  break;
180 
181  case 'ipb_expiry':
182  $formatted = htmlspecialchars( $language->formatExpiry(
183  $value,
184  /* User preference timezone */true
185  ) );
186  if ( $this->getAuthority()->isAllowed( 'block' ) ) {
187  $links = [];
188  if ( $row->ipb_auto ) {
189  $links[] = $linkRenderer->makeKnownLink(
190  $this->specialPageFactory->getTitleForAlias( 'Unblock' ),
191  $msg['unblocklink'],
192  [],
193  [ 'wpTarget' => "#{$row->ipb_id}" ]
194  );
195  } else {
196  $links[] = $linkRenderer->makeKnownLink(
197  $this->specialPageFactory->getTitleForAlias( 'Unblock/' . $row->ipb_address ),
198  $msg['unblocklink']
199  );
200  $links[] = $linkRenderer->makeKnownLink(
201  $this->specialPageFactory->getTitleForAlias( 'Block/' . $row->ipb_address ),
202  $msg['change-blocklink']
203  );
204  }
205  $formatted .= ' ' . Html::rawElement(
206  'span',
207  [ 'class' => 'mw-blocklist-actions' ],
208  $this->msg( 'parentheses' )->rawParams(
209  $language->pipeList( $links ) )->escaped()
210  );
211  }
212  if ( $value !== 'infinity' ) {
213  $timestamp = new MWTimestamp( $value );
214  $formatted .= '<br />' . $this->msg(
215  'ipb-blocklist-duration-left',
216  $language->formatDuration(
217  $timestamp->getTimestamp() - MWTimestamp::time(),
218  // reasonable output
219  [
220  'minutes',
221  'hours',
222  'days',
223  'years',
224  ]
225  )
226  )->escaped();
227  }
228  break;
229 
230  case 'ipb_by':
231  if ( isset( $row->by_user_name ) ) {
232  $formatted = Linker::userLink( $value, $row->by_user_name );
233  $formatted .= Linker::userToolLinks( $value, $row->by_user_name );
234  } else {
235  $formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user?
236  }
237  break;
238 
239  case 'ipb_reason':
240  $value = $this->commentStore->getComment( 'ipb_reason', $row )->text;
241  $formatted = Linker::formatComment( $value );
242  break;
243 
244  case 'ipb_params':
245  $properties = [];
246 
247  if ( $row->ipb_sitewide ) {
248  $properties[] = htmlspecialchars( $msg['blocklist-editing-sitewide'] );
249  }
250 
251  if ( !$row->ipb_sitewide && $this->restrictions ) {
252  $list = $this->getRestrictionListHTML( $row );
253  if ( $list ) {
254  $properties[] = htmlspecialchars( $msg['blocklist-editing'] ) . $list;
255  }
256  }
257 
258  if ( $row->ipb_anon_only ) {
259  $properties[] = htmlspecialchars( $msg['anononlyblock'] );
260  }
261  if ( $row->ipb_create_account ) {
262  $properties[] = htmlspecialchars( $msg['createaccountblock'] );
263  }
264  if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
265  $properties[] = htmlspecialchars( $msg['noautoblockblock'] );
266  }
267 
268  if ( $row->ipb_block_email ) {
269  $properties[] = htmlspecialchars( $msg['emailblock'] );
270  }
271 
272  if ( !$row->ipb_allow_usertalk ) {
273  $properties[] = htmlspecialchars( $msg['blocklist-nousertalk'] );
274  }
275 
276  $formatted = Html::rawElement(
277  'ul',
278  [],
279  implode( '', array_map( static function ( $prop ) {
280  return Html::rawElement(
281  'li',
282  [],
283  $prop
284  );
285  }, $properties ) )
286  );
287  break;
288 
289  default:
290  $formatted = "Unable to format $name";
291  break;
292  }
293 
294  return $formatted;
295  }
296 
304  private function getRestrictionListHTML( stdClass $row ) {
305  $items = [];
306  $linkRenderer = $this->getLinkRenderer();
307 
308  foreach ( $this->restrictions as $restriction ) {
309  if ( $restriction->getBlockId() !== (int)$row->ipb_id ) {
310  continue;
311  }
312 
313  switch ( $restriction->getType() ) {
314  case PageRestriction::TYPE:
315  '@phan-var PageRestriction $restriction';
316  if ( $restriction->getTitle() ) {
317  $items[$restriction->getType()][] = Html::rawElement(
318  'li',
319  [],
320  $linkRenderer->makeLink( $restriction->getTitle() )
321  );
322  }
323  break;
324  case NamespaceRestriction::TYPE:
325  $text = $restriction->getValue() === NS_MAIN
326  ? $this->msg( 'blanknamespace' )->text()
327  : $this->getLanguage()->getFormattedNsText(
328  $restriction->getValue()
329  );
330  $items[$restriction->getType()][] = Html::rawElement(
331  'li',
332  [],
333  $linkRenderer->makeLink(
334  $this->specialPageFactory->getTitleForAlias( 'Allpages' ),
335  $text,
336  [],
337  [
338  'namespace' => $restriction->getValue()
339  ]
340  )
341  );
342  break;
343  }
344  }
345 
346  if ( empty( $items ) ) {
347  return '';
348  }
349 
350  $sets = [];
351  foreach ( $items as $key => $value ) {
352  $sets[] = Html::rawElement(
353  'li',
354  [],
355  $this->msg( 'blocklist-editing-' . $key ) . Html::rawElement(
356  'ul',
357  [],
358  implode( '', $value )
359  )
360  );
361  }
362 
363  return Html::rawElement(
364  'ul',
365  [],
366  implode( '', $sets )
367  );
368  }
369 
370  public function getQueryInfo() {
371  $commentQuery = $this->commentStore->getJoin( 'ipb_reason' );
372  $actorQuery = $this->actorMigration->getJoin( 'ipb_by' );
373 
374  $info = [
375  'tables' => array_merge(
376  [ 'ipblocks' ], $commentQuery['tables'], $actorQuery['tables'], [ 'user' ]
377  ),
378  'fields' => [
379  'ipb_id',
380  'ipb_address',
381  'ipb_user',
382  'by_user_name' => 'user_name',
383  'ipb_timestamp',
384  'ipb_auto',
385  'ipb_anon_only',
386  'ipb_create_account',
387  'ipb_enable_autoblock',
388  'ipb_expiry',
389  'ipb_range_start',
390  'ipb_range_end',
391  'ipb_deleted',
392  'ipb_block_email',
393  'ipb_allow_usertalk',
394  'ipb_sitewide',
395  ] + $commentQuery['fields'] + $actorQuery['fields'],
396  'conds' => $this->conds,
397  'join_conds' => [
398  'user' => [ 'LEFT JOIN', 'user_id = ' . $actorQuery['fields']['ipb_by'] ]
399  ] + $commentQuery['joins'] + $actorQuery['joins']
400  ];
401 
402  # Filter out any expired blocks
403  $db = $this->getDatabase();
404  $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
405 
406  # Is the user allowed to see hidden blocks?
407  if ( !$this->getAuthority()->isAllowed( 'hideuser' ) ) {
408  $info['conds']['ipb_deleted'] = 0;
409  }
410 
411  return $info;
412  }
413 
419  public function getTotalAutoblocks() {
420  $dbr = $this->getDatabase();
421  $res = $dbr->selectField( 'ipblocks',
422  'COUNT(*)',
423  [
424  'ipb_auto' => '1',
425  'ipb_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ),
426  ],
427  __METHOD__
428  );
429  if ( $res ) {
430  return $res;
431  }
432  return 0; // We found nothing
433  }
434 
435  protected function getTableClass() {
436  return parent::getTableClass() . ' mw-blocklist';
437  }
438 
439  public function getIndexField() {
440  return [ [ 'ipb_timestamp', 'ipb_id' ] ];
441  }
442 
443  public function getDefaultSort() {
444  return '';
445  }
446 
447  protected function isFieldSortable( $name ) {
448  return false;
449  }
450 
455  public function preprocessResults( $result ) {
456  # Do a link batch query
457  $lb = $this->linkBatchFactory->newLinkBatch();
458  $lb->setCaller( __METHOD__ );
459 
460  $partialBlocks = [];
461  foreach ( $result as $row ) {
462  $lb->add( NS_USER, $row->ipb_address );
463  $lb->add( NS_USER_TALK, $row->ipb_address );
464 
465  if ( isset( $row->by_user_name ) ) {
466  $lb->add( NS_USER, $row->by_user_name );
467  $lb->add( NS_USER_TALK, $row->by_user_name );
468  }
469 
470  if ( !$row->ipb_sitewide ) {
471  $partialBlocks[] = $row->ipb_id;
472  }
473  }
474 
475  if ( $partialBlocks ) {
476  // Mutations to the $row object are not persisted. The restrictions will
477  // need be stored in a separate store.
478  $this->restrictions = $this->blockRestrictionStore->loadByBlockId( $partialBlocks );
479 
480  foreach ( $this->restrictions as $restriction ) {
481  if ( $restriction->getType() === PageRestriction::TYPE ) {
482  '@phan-var PageRestriction $restriction';
483  $title = $restriction->getTitle();
484  if ( $title !== null ) {
485  $lb->addObj( $title );
486  }
487  }
488  }
489  }
490 
491  $lb->execute();
492  }
493 
494 }
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:41
BlockListPager\isFieldSortable
isFieldSortable( $name)
Return true if the named field should be sortable by the UI, false otherwise.
Definition: BlockListPager.php:447
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:37
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:52
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:304
User\newFromName
static newFromName( $name, $validate='valid')
Definition: User.php:584
BlockListPager\$conds
$conds
Definition: BlockListPager.php:39
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:42
$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 server index.
BlockListPager\$linkBatchFactory
LinkBatchFactory $linkBatchFactory
Definition: BlockListPager.php:49
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:35
NS_MAIN
const NS_MAIN
Definition: Defines.php:64
$dbr
$dbr
Definition: testCompression.php:54
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:151
BlockListPager\$commentStore
CommentStore $commentStore
Definition: BlockListPager.php:61
BlockListPager\__construct
__construct( $page, $conds, LinkBatchFactory $linkBatchFactory, BlockRestrictionStore $blockRestrictionStore, ILoadBalancer $loadBalancer, SpecialPageFactory $specialPageFactory, ActorMigration $actorMigration, CommentStore $commentStore)
Definition: BlockListPager.php:73
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:435
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:94
BlockListPager\$restrictions
Restriction[] $restrictions
Array of restrictions.
Definition: BlockListPager.php:46
BlockListPager\getDefaultSort
getDefaultSort()
The database field name used as a default sort order.
Definition: BlockListPager.php:443
$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:195
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:439
ContextSource\getAuthority
getAuthority()
Definition: ContextSource.php:142
BlockListPager\getQueryInfo
getQueryInfo()
Provides all parameters needed for the main paged query.
Definition: BlockListPager.php:370
BlockListPager\getTotalAutoblocks
getTotalAutoblocks()
Get total number of autoblocks at any given time.
Definition: BlockListPager.php:419
IndexPager\getLinkRenderer
getLinkRenderer()
Definition: IndexPager.php:980
NS_USER
const NS_USER
Definition: Defines.php:66
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:1191
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:455
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:212
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:67
$keys
$keys
Definition: testCompression.php:72
BlockListPager\$actorMigration
ActorMigration $actorMigration
Definition: BlockListPager.php:58
BlockListPager\$specialPageFactory
SpecialPageFactory $specialPageFactory
Definition: BlockListPager.php:55
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:120
MediaWiki\Block\BlockRestrictionStore
Definition: BlockRestrictionStore.php:34
$type
$type
Definition: testCompression.php:52