MediaWiki  master
BlockListPager.php
Go to the documentation of this file.
1 <?php
32 
33 class BlockListPager extends TablePager {
34 
35  protected $conds;
36 
42  protected $restrictions = [];
43 
48  public function __construct( $page, $conds ) {
49  parent::__construct( $page->getContext(), $page->getLinkRenderer() );
50  $this->conds = $conds;
51  $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
52  }
53 
54  function getFieldNames() {
55  static $headers = null;
56 
57  if ( $headers === null ) {
58  $headers = [
59  'ipb_timestamp' => 'blocklist-timestamp',
60  'ipb_target' => 'blocklist-target',
61  'ipb_expiry' => 'blocklist-expiry',
62  'ipb_by' => 'blocklist-by',
63  'ipb_params' => 'blocklist-params',
64  'ipb_reason' => 'blocklist-reason',
65  ];
66  foreach ( $headers as $key => $val ) {
67  $headers[$key] = $this->msg( $val )->text();
68  }
69  }
70 
71  return $headers;
72  }
73 
80  function formatValue( $name, $value ) {
81  static $msg = null;
82  if ( $msg === null ) {
83  $keys = [
84  'anononlyblock',
85  'createaccountblock',
86  'noautoblockblock',
87  'emailblock',
88  'blocklist-nousertalk',
89  'unblocklink',
90  'change-blocklink',
91  'blocklist-editing',
92  'blocklist-editing-sitewide',
93  ];
94 
95  foreach ( $keys as $key ) {
96  $msg[$key] = $this->msg( $key )->text();
97  }
98  }
99  '@phan-var string[] $msg';
100 
102  $row = $this->mCurrentRow;
103 
104  $language = $this->getLanguage();
105 
106  $formatted = '';
107 
108  $linkRenderer = $this->getLinkRenderer();
109 
110  switch ( $name ) {
111  case 'ipb_timestamp':
112  $formatted = htmlspecialchars( $language->userTimeAndDate( $value, $this->getUser() ) );
113  break;
114 
115  case 'ipb_target':
116  if ( $row->ipb_auto ) {
117  $formatted = $this->msg( 'autoblockid', $row->ipb_id )->parse();
118  } else {
119  list( $target, $type ) = DatabaseBlock::parseTarget( $row->ipb_address );
120 
121  if ( is_string( $target ) ) {
122  if ( IP::isValidRange( $target ) ) {
123  $target = User::newFromName( $target, false );
124  } else {
125  $formatted = $target;
126  }
127  }
128 
129  if ( $target instanceof UserIdentity ) {
130  $formatted = Linker::userLink( $target->getId(), $target->getName() );
131  $formatted .= Linker::userToolLinks(
132  $target->getId(),
133  $target->getName(),
134  false,
136  );
137  }
138  }
139  break;
140 
141  case 'ipb_expiry':
142  $formatted = htmlspecialchars( $language->formatExpiry(
143  $value,
144  /* User preference timezone */true
145  ) );
146  if ( MediaWikiServices::getInstance()
147  ->getPermissionManager()
148  ->userHasRight( $this->getUser(), 'block' )
149  ) {
150  $links = [];
151  if ( $row->ipb_auto ) {
152  $links[] = $linkRenderer->makeKnownLink(
153  SpecialPage::getTitleFor( 'Unblock' ),
154  $msg['unblocklink'],
155  [],
156  [ 'wpTarget' => "#{$row->ipb_id}" ]
157  );
158  } else {
159  $links[] = $linkRenderer->makeKnownLink(
160  SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
161  $msg['unblocklink']
162  );
163  $links[] = $linkRenderer->makeKnownLink(
164  SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
165  $msg['change-blocklink']
166  );
167  }
168  $formatted .= ' ' . Html::rawElement(
169  'span',
170  [ 'class' => 'mw-blocklist-actions' ],
171  $this->msg( 'parentheses' )->rawParams(
172  $language->pipeList( $links ) )->escaped()
173  );
174  }
175  if ( $value !== 'infinity' ) {
176  $timestamp = new MWTimestamp( $value );
177  $formatted .= '<br />' . $this->msg(
178  'ipb-blocklist-duration-left',
179  $language->formatDuration(
180  $timestamp->getTimestamp() - MWTimestamp::time(),
181  // reasonable output
182  [
183  'minutes',
184  'hours',
185  'days',
186  'years',
187  ]
188  )
189  )->escaped();
190  }
191  break;
192 
193  case 'ipb_by':
194  if ( isset( $row->by_user_name ) ) {
195  $formatted = Linker::userLink( $value, $row->by_user_name );
196  $formatted .= Linker::userToolLinks( $value, $row->by_user_name );
197  } else {
198  $formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user?
199  }
200  break;
201 
202  case 'ipb_reason':
203  $value = CommentStore::getStore()->getComment( 'ipb_reason', $row )->text;
204  $formatted = Linker::formatComment( $value );
205  break;
206 
207  case 'ipb_params':
208  $properties = [];
209 
210  if ( $this->getConfig()->get( 'EnablePartialBlocks' ) && $row->ipb_sitewide ) {
211  $properties[] = htmlspecialchars( $msg['blocklist-editing-sitewide'] );
212  }
213 
214  if ( !$row->ipb_sitewide && $this->restrictions ) {
215  $list = $this->getRestrictionListHTML( $row );
216  if ( $list ) {
217  $properties[] = htmlspecialchars( $msg['blocklist-editing'] ) . $list;
218  }
219  }
220 
221  if ( $row->ipb_anon_only ) {
222  $properties[] = htmlspecialchars( $msg['anononlyblock'] );
223  }
224  if ( $row->ipb_create_account ) {
225  $properties[] = htmlspecialchars( $msg['createaccountblock'] );
226  }
227  if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
228  $properties[] = htmlspecialchars( $msg['noautoblockblock'] );
229  }
230 
231  if ( $row->ipb_block_email ) {
232  $properties[] = htmlspecialchars( $msg['emailblock'] );
233  }
234 
235  if ( !$row->ipb_allow_usertalk ) {
236  $properties[] = htmlspecialchars( $msg['blocklist-nousertalk'] );
237  }
238 
239  $formatted = Html::rawElement(
240  'ul',
241  [],
242  implode( '', array_map( function ( $prop ) {
243  return Html::rawElement(
244  'li',
245  [],
246  $prop
247  );
248  }, $properties ) )
249  );
250  break;
251 
252  default:
253  $formatted = "Unable to format $name";
254  break;
255  }
256 
257  return $formatted;
258  }
259 
267  private function getRestrictionListHTML( stdClass $row ) {
268  $items = [];
269  $linkRenderer = $this->getLinkRenderer();
270 
271  foreach ( $this->restrictions as $restriction ) {
272  if ( $restriction->getBlockId() !== (int)$row->ipb_id ) {
273  continue;
274  }
275 
276  switch ( $restriction->getType() ) {
277  case PageRestriction::TYPE:
278  '@phan-var PageRestriction $restriction';
279  if ( $restriction->getTitle() ) {
280  $items[$restriction->getType()][] = Html::rawElement(
281  'li',
282  [],
283  $linkRenderer->makeLink( $restriction->getTitle() )
284  );
285  }
286  break;
287  case NamespaceRestriction::TYPE:
288  $text = $restriction->getValue() === NS_MAIN
289  ? $this->msg( 'blanknamespace' )->text()
290  : $this->getLanguage()->getFormattedNsText(
291  $restriction->getValue()
292  );
293  $items[$restriction->getType()][] = Html::rawElement(
294  'li',
295  [],
296  $linkRenderer->makeLink(
297  SpecialPage::getTitleValueFor( 'Allpages' ),
298  $text,
299  [],
300  [
301  'namespace' => $restriction->getValue()
302  ]
303  )
304  );
305  break;
306  }
307  }
308 
309  if ( empty( $items ) ) {
310  return '';
311  }
312 
313  $sets = [];
314  foreach ( $items as $key => $value ) {
315  $sets[] = Html::rawElement(
316  'li',
317  [],
318  $this->msg( 'blocklist-editing-' . $key ) . Html::rawElement(
319  'ul',
320  [],
321  implode( '', $value )
322  )
323  );
324  }
325 
326  return Html::rawElement(
327  'ul',
328  [],
329  implode( '', $sets )
330  );
331  }
332 
333  function getQueryInfo() {
334  $commentQuery = CommentStore::getStore()->getJoin( 'ipb_reason' );
335  $actorQuery = ActorMigration::newMigration()->getJoin( 'ipb_by' );
336 
337  $info = [
338  'tables' => array_merge(
339  [ 'ipblocks' ], $commentQuery['tables'], $actorQuery['tables'], [ 'user' ]
340  ),
341  'fields' => [
342  'ipb_id',
343  'ipb_address',
344  'ipb_user',
345  'by_user_name' => 'user_name',
346  'ipb_timestamp',
347  'ipb_auto',
348  'ipb_anon_only',
349  'ipb_create_account',
350  'ipb_enable_autoblock',
351  'ipb_expiry',
352  'ipb_range_start',
353  'ipb_range_end',
354  'ipb_deleted',
355  'ipb_block_email',
356  'ipb_allow_usertalk',
357  'ipb_sitewide',
358  ] + $commentQuery['fields'] + $actorQuery['fields'],
359  'conds' => $this->conds,
360  'join_conds' => [
361  'user' => [ 'LEFT JOIN', 'user_id = ' . $actorQuery['fields']['ipb_by'] ]
362  ] + $commentQuery['joins'] + $actorQuery['joins']
363  ];
364 
365  # Filter out any expired blocks
366  $db = $this->getDatabase();
367  $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
368 
369  # Is the user allowed to see hidden blocks?
370  if ( !MediaWikiServices::getInstance()
371  ->getPermissionManager()
372  ->userHasRight( $this->getUser(), 'hideuser' )
373  ) {
374  $info['conds']['ipb_deleted'] = 0;
375  }
376 
377  return $info;
378  }
379 
385  function getTotalAutoblocks() {
386  $dbr = $this->getDatabase();
387  $res = $dbr->selectField( 'ipblocks',
388  'COUNT(*)',
389  [
390  'ipb_auto' => '1',
391  'ipb_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ),
392  ]
393  );
394  if ( $res ) {
395  return $res;
396  }
397  return 0; // We found nothing
398  }
399 
400  protected function getTableClass() {
401  return parent::getTableClass() . ' mw-blocklist';
402  }
403 
404  function getIndexField() {
405  return 'ipb_timestamp';
406  }
407 
408  function getDefaultSort() {
409  return 'ipb_timestamp';
410  }
411 
412  function isFieldSortable( $name ) {
413  return false;
414  }
415 
420  function preprocessResults( $result ) {
421  # Do a link batch query
422  $lb = new LinkBatch;
423  $lb->setCaller( __METHOD__ );
424 
425  $partialBlocks = [];
426  foreach ( $result as $row ) {
427  $lb->add( NS_USER, $row->ipb_address );
428  $lb->add( NS_USER_TALK, $row->ipb_address );
429 
430  if ( isset( $row->by_user_name ) ) {
431  $lb->add( NS_USER, $row->by_user_name );
432  $lb->add( NS_USER_TALK, $row->by_user_name );
433  }
434 
435  if ( !$row->ipb_sitewide ) {
436  $partialBlocks[] = $row->ipb_id;
437  }
438  }
439 
440  if ( $partialBlocks ) {
441  // Mutations to the $row object are not persisted. The restrictions will
442  // need be stored in a separate store.
443  $blockRestrictionStore = MediaWikiServices::getInstance()->getBlockRestrictionStore();
444  $this->restrictions = $blockRestrictionStore->loadByBlockId( $partialBlocks );
445  }
446 
447  $lb->execute();
448  }
449 
450 }
static getTitleValueFor( $name, $subpage=false, $fragment='')
Get a localised TitleValue object for a specified special page name.
Definition: SpecialPage.php:98
const DIR_DESCENDING
Backwards-compatible constant for $mDefaultDirection field (do not change)
Definition: IndexPager.php:76
stdClass $mCurrentRow
Definition: TablePager.php:35
setCaller( $caller)
Use ->setCaller( METHOD ) to indicate which code is using this class.
Definition: LinkBatch.php:62
static isValidRange( $ipRange)
Validate an IP range (valid address with a valid CIDR prefix).
Definition: IP.php:125
Table-based display with a user-selectable sort order.
Definition: TablePager.php:30
const NS_MAIN
Definition: Defines.php:60
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
LinkRenderer $linkRenderer
Definition: IndexPager.php:162
getDatabase()
Get the Database object in use.
Definition: IndexPager.php:231
Interface for objects representing user identity.
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:34
formatValue( $name, $value)
static newMigration()
Static constructor.
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
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
const TOOL_LINKS_NOBLOCK
Flags for userToolLinks()
Definition: Linker.php:39
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don&#39;t need a full Title object...
Definition: SpecialPage.php:83
__construct( $page, $conds)
static userLink( $userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition: Linker.php:898
static getStore()
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
preprocessResults( $result)
Do a LinkBatch query to minimise database load when generating all these links.
Restriction [] $restrictions
Array of restrictions.
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:536
const NS_USER_TALK
Definition: Defines.php:63
isFieldSortable( $name)
getRestrictionListHTML(stdClass $row)
Get Restriction List HTML.
getTotalAutoblocks()
Get total number of autoblocks at any given time.