MediaWiki  master
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  '@phan-var string[] $msg';
99 
101  $row = $this->mCurrentRow;
102 
103  $language = $this->getLanguage();
104 
105  $formatted = '';
106 
107  $linkRenderer = $this->getLinkRenderer();
108 
109  switch ( $name ) {
110  case 'ipb_timestamp':
111  $formatted = htmlspecialchars( $language->userTimeAndDate( $value, $this->getUser() ) );
112  break;
113 
114  case 'ipb_target':
115  if ( $row->ipb_auto ) {
116  $formatted = $this->msg( 'autoblockid', $row->ipb_id )->parse();
117  } else {
118  list( $target, $type ) = DatabaseBlock::parseTarget( $row->ipb_address );
119  if ( $type === DatabaseBlock::TYPE_RANGE ) {
120  $user = User::newFromName( $target, false );
121  } else {
122  $user = $target;
123  }
124  $formatted = Linker::userLink( $user->getId(), $user );
125  $formatted .= Linker::userToolLinks(
126  $user->getId(),
127  $user,
128  false,
130  );
131  }
132  break;
133 
134  case 'ipb_expiry':
135  $formatted = htmlspecialchars( $language->formatExpiry(
136  $value,
137  /* User preference timezone */true
138  ) );
139  if ( MediaWikiServices::getInstance()
140  ->getPermissionManager()
141  ->userHasRight( $this->getUser(), 'block' )
142  ) {
143  $links = [];
144  if ( $row->ipb_auto ) {
145  $links[] = $linkRenderer->makeKnownLink(
146  SpecialPage::getTitleFor( 'Unblock' ),
147  $msg['unblocklink'],
148  [],
149  [ 'wpTarget' => "#{$row->ipb_id}" ]
150  );
151  } else {
152  $links[] = $linkRenderer->makeKnownLink(
153  SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
154  $msg['unblocklink']
155  );
156  $links[] = $linkRenderer->makeKnownLink(
157  SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
158  $msg['change-blocklink']
159  );
160  }
161  $formatted .= ' ' . Html::rawElement(
162  'span',
163  [ 'class' => 'mw-blocklist-actions' ],
164  $this->msg( 'parentheses' )->rawParams(
165  $language->pipeList( $links ) )->escaped()
166  );
167  }
168  if ( $value !== 'infinity' ) {
169  $timestamp = new MWTimestamp( $value );
170  $formatted .= '<br />' . $this->msg(
171  'ipb-blocklist-duration-left',
172  $language->formatDuration(
173  $timestamp->getTimestamp() - MWTimestamp::time(),
174  // reasonable output
175  [
176  'minutes',
177  'hours',
178  'days',
179  'years',
180  ]
181  )
182  )->escaped();
183  }
184  break;
185 
186  case 'ipb_by':
187  if ( isset( $row->by_user_name ) ) {
188  $formatted = Linker::userLink( $value, $row->by_user_name );
189  $formatted .= Linker::userToolLinks( $value, $row->by_user_name );
190  } else {
191  $formatted = htmlspecialchars( $row->ipb_by_text ); // foreign user?
192  }
193  break;
194 
195  case 'ipb_reason':
196  $value = CommentStore::getStore()->getComment( 'ipb_reason', $row )->text;
197  $formatted = Linker::formatComment( $value );
198  break;
199 
200  case 'ipb_params':
201  $properties = [];
202 
203  if ( $this->getConfig()->get( 'EnablePartialBlocks' ) && $row->ipb_sitewide ) {
204  $properties[] = htmlspecialchars( $msg['blocklist-editing-sitewide'] );
205  }
206 
207  if ( !$row->ipb_sitewide && $this->restrictions ) {
208  $list = $this->getRestrictionListHTML( $row );
209  if ( $list ) {
210  $properties[] = htmlspecialchars( $msg['blocklist-editing'] ) . $list;
211  }
212  }
213 
214  if ( $row->ipb_anon_only ) {
215  $properties[] = htmlspecialchars( $msg['anononlyblock'] );
216  }
217  if ( $row->ipb_create_account ) {
218  $properties[] = htmlspecialchars( $msg['createaccountblock'] );
219  }
220  if ( $row->ipb_user && !$row->ipb_enable_autoblock ) {
221  $properties[] = htmlspecialchars( $msg['noautoblockblock'] );
222  }
223 
224  if ( $row->ipb_block_email ) {
225  $properties[] = htmlspecialchars( $msg['emailblock'] );
226  }
227 
228  if ( !$row->ipb_allow_usertalk ) {
229  $properties[] = htmlspecialchars( $msg['blocklist-nousertalk'] );
230  }
231 
232  $formatted = Html::rawElement(
233  'ul',
234  [],
235  implode( '', array_map( function ( $prop ) {
236  return Html::rawElement(
237  'li',
238  [],
239  $prop
240  );
241  }, $properties ) )
242  );
243  break;
244 
245  default:
246  $formatted = "Unable to format $name";
247  break;
248  }
249 
250  return $formatted;
251  }
252 
260  private function getRestrictionListHTML( stdClass $row ) {
261  $items = [];
262  $linkRenderer = $this->getLinkRenderer();
263 
264  foreach ( $this->restrictions as $restriction ) {
265  if ( $restriction->getBlockId() !== (int)$row->ipb_id ) {
266  continue;
267  }
268 
269  switch ( $restriction->getType() ) {
270  case PageRestriction::TYPE:
271  '@phan-var PageRestriction $restriction';
272  if ( $restriction->getTitle() ) {
273  $items[$restriction->getType()][] = Html::rawElement(
274  'li',
275  [],
276  $linkRenderer->makeLink( $restriction->getTitle() )
277  );
278  }
279  break;
280  case NamespaceRestriction::TYPE:
281  $text = $restriction->getValue() === NS_MAIN
282  ? $this->msg( 'blanknamespace' )->text()
283  : $this->getLanguage()->getFormattedNsText(
284  $restriction->getValue()
285  );
286  $items[$restriction->getType()][] = Html::rawElement(
287  'li',
288  [],
289  $linkRenderer->makeLink(
290  SpecialPage::getTitleValueFor( 'Allpages' ),
291  $text,
292  [],
293  [
294  'namespace' => $restriction->getValue()
295  ]
296  )
297  );
298  break;
299  }
300  }
301 
302  if ( empty( $items ) ) {
303  return '';
304  }
305 
306  $sets = [];
307  foreach ( $items as $key => $value ) {
308  $sets[] = Html::rawElement(
309  'li',
310  [],
311  $this->msg( 'blocklist-editing-' . $key ) . Html::rawElement(
312  'ul',
313  [],
314  implode( '', $value )
315  )
316  );
317  }
318 
319  return Html::rawElement(
320  'ul',
321  [],
322  implode( '', $sets )
323  );
324  }
325 
326  function getQueryInfo() {
327  $commentQuery = CommentStore::getStore()->getJoin( 'ipb_reason' );
328  $actorQuery = ActorMigration::newMigration()->getJoin( 'ipb_by' );
329 
330  $info = [
331  'tables' => array_merge(
332  [ 'ipblocks' ], $commentQuery['tables'], $actorQuery['tables'], [ 'user' ]
333  ),
334  'fields' => [
335  'ipb_id',
336  'ipb_address',
337  'ipb_user',
338  'by_user_name' => 'user_name',
339  'ipb_timestamp',
340  'ipb_auto',
341  'ipb_anon_only',
342  'ipb_create_account',
343  'ipb_enable_autoblock',
344  'ipb_expiry',
345  'ipb_range_start',
346  'ipb_range_end',
347  'ipb_deleted',
348  'ipb_block_email',
349  'ipb_allow_usertalk',
350  'ipb_sitewide',
351  ] + $commentQuery['fields'] + $actorQuery['fields'],
352  'conds' => $this->conds,
353  'join_conds' => [
354  'user' => [ 'LEFT JOIN', 'user_id = ' . $actorQuery['fields']['ipb_by'] ]
355  ] + $commentQuery['joins'] + $actorQuery['joins']
356  ];
357 
358  # Filter out any expired blocks
359  $db = $this->getDatabase();
360  $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
361 
362  # Is the user allowed to see hidden blocks?
363  if ( !MediaWikiServices::getInstance()
364  ->getPermissionManager()
365  ->userHasRight( $this->getUser(), 'hideuser' )
366  ) {
367  $info['conds']['ipb_deleted'] = 0;
368  }
369 
370  return $info;
371  }
372 
378  function getTotalAutoblocks() {
379  $dbr = $this->getDatabase();
380  $res = $dbr->selectField( 'ipblocks',
381  'COUNT(*)',
382  [
383  'ipb_auto' => '1',
384  'ipb_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ),
385  ]
386  );
387  if ( $res ) {
388  return $res;
389  }
390  return 0; // We found nothing
391  }
392 
393  protected function getTableClass() {
394  return parent::getTableClass() . ' mw-blocklist';
395  }
396 
397  function getIndexField() {
398  return 'ipb_timestamp';
399  }
400 
401  function getDefaultSort() {
402  return 'ipb_timestamp';
403  }
404 
405  function isFieldSortable( $name ) {
406  return false;
407  }
408 
413  function preprocessResults( $result ) {
414  # Do a link batch query
415  $lb = new LinkBatch;
416  $lb->setCaller( __METHOD__ );
417 
418  $partialBlocks = [];
419  foreach ( $result as $row ) {
420  $lb->add( NS_USER, $row->ipb_address );
421  $lb->add( NS_USER_TALK, $row->ipb_address );
422 
423  if ( isset( $row->by_user_name ) ) {
424  $lb->add( NS_USER, $row->by_user_name );
425  $lb->add( NS_USER_TALK, $row->by_user_name );
426  }
427 
428  if ( !$row->ipb_sitewide ) {
429  $partialBlocks[] = $row->ipb_id;
430  }
431  }
432 
433  if ( $partialBlocks ) {
434  // Mutations to the $row object are not persisted. The restrictions will
435  // need be stored in a separate store.
436  $blockRestrictionStore = MediaWikiServices::getInstance()->getBlockRestrictionStore();
437  $this->restrictions = $blockRestrictionStore->loadByBlockId( $partialBlocks );
438  }
439 
440  $lb->execute();
441  }
442 
443 }
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
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:224
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:515
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.