MediaWiki  1.34.0
BlockListPager.php
Go to the documentation of this file.
1 <?php
31 
32 class BlockListPager extends TablePager {
33 
34  protected $conds;
35 
41  protected $restrictions = [];
42 
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 
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 
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,
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 ( MediaWikiServices::getInstance()
141  ->getPermissionManager()
142  ->userHasRight( $this->getUser(), 'block' )
143  ) {
144  $links = [];
145  if ( $row->ipb_auto ) {
146  $links[] = $linkRenderer->makeKnownLink(
147  SpecialPage::getTitleFor( 'Unblock' ),
148  $msg['unblocklink'],
149  [],
150  [ 'wpTarget' => "#{$row->ipb_id}" ]
151  );
152  } else {
153  $links[] = $linkRenderer->makeKnownLink(
154  SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
155  $msg['unblocklink']
156  );
157  $links[] = $linkRenderer->makeKnownLink(
158  SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
159  $msg['change-blocklink']
160  );
161  }
162  $formatted .= ' ' . Html::rawElement(
163  'span',
164  [ 'class' => 'mw-blocklist-actions' ],
165  $this->msg( 'parentheses' )->rawParams(
166  $language->pipeList( $links ) )->escaped()
167  );
168  }
169  if ( $value !== 'infinity' ) {
170  $timestamp = new MWTimestamp( $value );
171  $formatted .= '<br />' . $this->msg(
172  'ipb-blocklist-duration-left',
173  $language->formatDuration(
174  $timestamp->getTimestamp() - MWTimestamp::time(),
175  // reasonable output
176  [
177  'minutes',
178  'hours',
179  'days',
180  'years',
181  ]
182  )
183  )->escaped();
184  }
185  break;
186 
187  case 'ipb_by':
188  if ( isset( $row->by_user_name ) ) {
189  $formatted = Linker::userLink( $value, $row->by_user_name );
190  $formatted .= Linker::userToolLinks( $value, $row->by_user_name );
191  } else {
192  $formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user?
193  }
194  break;
195 
196  case 'ipb_reason':
197  $value = CommentStore::getStore()->getComment( 'ipb_reason', $row )->text;
198  $formatted = Linker::formatComment( $value );
199  break;
200 
201  case 'ipb_params':
202  $properties = [];
203 
204  if ( $this->getConfig()->get( 'EnablePartialBlocks' ) && $row->ipb_sitewide ) {
205  $properties[] = htmlspecialchars( $msg['blocklist-editing-sitewide'] );
206  }
207 
208  if ( !$row->ipb_sitewide && $this->restrictions ) {
209  $list = $this->getRestrictionListHTML( $row );
210  if ( $list ) {
211  $properties[] = htmlspecialchars( $msg['blocklist-editing'] ) . $list;
212  }
213  }
214 
215  if ( $row->ipb_anon_only ) {
216  $properties[] = htmlspecialchars( $msg['anononlyblock'] );
217  }
218  if ( $row->ipb_create_account ) {
219  $properties[] = htmlspecialchars( $msg['createaccountblock'] );
220  }
221  if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
222  $properties[] = htmlspecialchars( $msg['noautoblockblock'] );
223  }
224 
225  if ( $row->ipb_block_email ) {
226  $properties[] = htmlspecialchars( $msg['emailblock'] );
227  }
228 
229  if ( !$row->ipb_allow_usertalk ) {
230  $properties[] = htmlspecialchars( $msg['blocklist-nousertalk'] );
231  }
232 
233  $formatted = Html::rawElement(
234  'ul',
235  [],
236  implode( '', array_map( function ( $prop ) {
237  return Html::rawElement(
238  'li',
239  [],
240  $prop
241  );
242  }, $properties ) )
243  );
244  break;
245 
246  default:
247  $formatted = "Unable to format $name";
248  break;
249  }
250 
251  return $formatted;
252  }
253 
261  private function getRestrictionListHTML( stdClass $row ) {
262  $items = [];
263  $linkRenderer = $this->getLinkRenderer();
264 
265  foreach ( $this->restrictions as $restriction ) {
266  if ( $restriction->getBlockId() !== (int)$row->ipb_id ) {
267  continue;
268  }
269 
270  switch ( $restriction->getType() ) {
271  case PageRestriction::TYPE:
272  '@phan-var PageRestriction $restriction';
273  if ( $restriction->getTitle() ) {
274  $items[$restriction->getType()][] = Html::rawElement(
275  'li',
276  [],
277  $linkRenderer->makeLink( $restriction->getTitle() )
278  );
279  }
280  break;
281  case NamespaceRestriction::TYPE:
282  $text = $restriction->getValue() === NS_MAIN
283  ? $this->msg( 'blanknamespace' )->text()
284  : $this->getLanguage()->getFormattedNsText(
285  $restriction->getValue()
286  );
287  $items[$restriction->getType()][] = Html::rawElement(
288  'li',
289  [],
290  $linkRenderer->makeLink(
291  SpecialPage::getTitleValueFor( 'Allpages' ),
292  $text,
293  [],
294  [
295  'namespace' => $restriction->getValue()
296  ]
297  )
298  );
299  break;
300  }
301  }
302 
303  if ( empty( $items ) ) {
304  return '';
305  }
306 
307  $sets = [];
308  foreach ( $items as $key => $value ) {
309  $sets[] = Html::rawElement(
310  'li',
311  [],
312  $this->msg( 'blocklist-editing-' . $key ) . Html::rawElement(
313  'ul',
314  [],
315  implode( '', $value )
316  )
317  );
318  }
319 
320  return Html::rawElement(
321  'ul',
322  [],
323  implode( '', $sets )
324  );
325  }
326 
327  function getQueryInfo() {
328  $commentQuery = CommentStore::getStore()->getJoin( 'ipb_reason' );
329  $actorQuery = ActorMigration::newMigration()->getJoin( 'ipb_by' );
330 
331  $info = [
332  'tables' => array_merge(
333  [ 'ipblocks' ], $commentQuery['tables'], $actorQuery['tables'], [ 'user' ]
334  ),
335  'fields' => [
336  'ipb_id',
337  'ipb_address',
338  'ipb_user',
339  'by_user_name' => 'user_name',
340  'ipb_timestamp',
341  'ipb_auto',
342  'ipb_anon_only',
343  'ipb_create_account',
344  'ipb_enable_autoblock',
345  'ipb_expiry',
346  'ipb_range_start',
347  'ipb_range_end',
348  'ipb_deleted',
349  'ipb_block_email',
350  'ipb_allow_usertalk',
351  'ipb_sitewide',
352  ] + $commentQuery['fields'] + $actorQuery['fields'],
353  'conds' => $this->conds,
354  'join_conds' => [
355  'user' => [ 'LEFT JOIN', 'user_id = ' . $actorQuery['fields']['ipb_by'] ]
356  ] + $commentQuery['joins'] + $actorQuery['joins']
357  ];
358 
359  # Filter out any expired blocks
360  $db = $this->getDatabase();
361  $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
362 
363  # Is the user allowed to see hidden blocks?
364  if ( !MediaWikiServices::getInstance()
365  ->getPermissionManager()
366  ->userHasRight( $this->getUser(), 'hideuser' )
367  ) {
368  $info['conds']['ipb_deleted'] = 0;
369  }
370 
371  return $info;
372  }
373 
379  function getTotalAutoblocks() {
380  $dbr = $this->getDatabase();
381  $res = $dbr->selectField( 'ipblocks',
382  'COUNT(*)',
383  [
384  'ipb_auto' => '1',
385  'ipb_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ),
386  ]
387  );
388  if ( $res ) {
389  return $res;
390  }
391  return 0; // We found nothing
392  }
393 
394  protected function getTableClass() {
395  return parent::getTableClass() . ' mw-blocklist';
396  }
397 
398  function getIndexField() {
399  return 'ipb_timestamp';
400  }
401 
402  function getDefaultSort() {
403  return 'ipb_timestamp';
404  }
405 
406  function isFieldSortable( $name ) {
407  return false;
408  }
409 
414  function preprocessResults( $result ) {
415  # Do a link batch query
416  $lb = new LinkBatch;
417  $lb->setCaller( __METHOD__ );
418 
419  $partialBlocks = [];
420  foreach ( $result as $row ) {
421  $lb->add( NS_USER, $row->ipb_address );
422  $lb->add( NS_USER_TALK, $row->ipb_address );
423 
424  if ( isset( $row->by_user_name ) ) {
425  $lb->add( NS_USER, $row->by_user_name );
426  $lb->add( NS_USER_TALK, $row->by_user_name );
427  }
428 
429  if ( !$row->ipb_sitewide ) {
430  $partialBlocks[] = $row->ipb_id;
431  }
432  }
433 
434  if ( $partialBlocks ) {
435  // Mutations to the $row object are not persisted. The restrictions will
436  // need be stored in a separate store.
437  $blockRestrictionStore = MediaWikiServices::getInstance()->getBlockRestrictionStore();
438  $this->restrictions = $blockRestrictionStore->loadByBlockId( $partialBlocks );
439  }
440 
441  $lb->execute();
442  }
443 
444 }
MWTimestamp
Library for creating and parsing MW-style timestamps.
Definition: MWTimestamp.php:32
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:63
Linker\TOOL_LINKS_NOBLOCK
const TOOL_LINKS_NOBLOCK
Flags for userToolLinks()
Definition: Linker.php:39
BlockListPager\isFieldSortable
isFieldSortable( $name)
Return true if the named field should be sortable by the UI, false otherwise.
Definition: BlockListPager.php:406
LinkBatch
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:34
Linker\userLink
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition: Linker.php:898
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
BlockListPager
Definition: BlockListPager.php:32
LinkBatch\setCaller
setCaller( $caller)
Use ->setCaller( METHOD ) to indicate which code is using this class.
Definition: LinkBatch.php:62
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:943
IndexPager\getDatabase
getDatabase()
Get the Database object in use.
Definition: IndexPager.php:224
BlockListPager\getRestrictionListHTML
getRestrictionListHTML(stdClass $row)
Get Restriction List HTML.
Definition: BlockListPager.php:261
BlockListPager\$conds
$conds
Definition: BlockListPager.php:34
IndexPager\$linkRenderer
LinkRenderer $linkRenderer
Definition: IndexPager.php:162
SpecialPage\getTitleFor
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
Definition: SpecialPage.php:83
$res
$res
Definition: testCompression.php:52
ContextSource\getUser
getUser()
Definition: ContextSource.php:120
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:136
$dbr
$dbr
Definition: testCompression.php:50
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:128
NS_MAIN
const NS_MAIN
Definition: Defines.php:60
MediaWiki\Block\DatabaseBlock
A DatabaseBlock (unlike a SystemBlock) is stored in the database, may give rise to autoblocks and may...
Definition: DatabaseBlock.php:54
Wikimedia\Rdbms\IResultWrapper
Result wrapper for grabbing data queried from an IDatabase object.
Definition: IResultWrapper.php:24
SpecialPage\getTitleValueFor
static getTitleValueFor( $name, $subpage=false, $fragment='')
Get a localised TitleValue object for a specified special page name.
Definition: SpecialPage.php:98
BlockListPager\getTableClass
getTableClass()
TablePager relies on mw-datatable for styling, see T214208.
Definition: BlockListPager.php:394
TablePager
Table-based display with a user-selectable sort order.
Definition: TablePager.php:30
MediaWiki\Block\Restriction\Restriction
Definition: Restriction.php:25
TablePager\$mCurrentRow
stdClass $mCurrentRow
Definition: TablePager.php:35
BlockListPager\__construct
__construct( $page, $conds)
Definition: BlockListPager.php:47
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:53
BlockListPager\$restrictions
Restriction[] $restrictions
Array of restrictions.
Definition: BlockListPager.php:41
BlockListPager\getDefaultSort
getDefaultSort()
The database field name used as a default sort order.
Definition: BlockListPager.php:402
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:168
BlockListPager\getIndexField
getIndexField()
Definition: BlockListPager.php:398
NS_USER_TALK
const NS_USER_TALK
Definition: Defines.php:63
BlockListPager\getQueryInfo
getQueryInfo()
This function should be overridden to provide all parameters needed for the main paged query.
Definition: BlockListPager.php:327
BlockListPager\getTotalAutoblocks
getTotalAutoblocks()
Get total number of autoblocks at any given time.
Definition: BlockListPager.php:379
IndexPager\getLinkRenderer
getLinkRenderer()
Definition: IndexPager.php:813
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:1165
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:76
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:414
$keys
$keys
Definition: testCompression.php:67
NS_USER
const NS_USER
Definition: Defines.php:62
CommentStore\getStore
static getStore()
Definition: CommentStore.php:139
BlockListPager\formatValue
formatValue( $name, $value)
Definition: BlockListPager.php:79
$type
$type
Definition: testCompression.php:48