MediaWiki  master
BlockErrorFormatter.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Block;
22 
24 use Language;
29 use Message;
30 use TitleFormatter;
31 
39 
41  private $titleFormatter;
42 
44  private $hookRunner;
45 
50  public function __construct(
51  TitleFormatter $titleFormatter,
52  HookContainer $hookContainer
53  ) {
54  $this->titleFormatter = $titleFormatter;
55  $this->hookRunner = new HookRunner( $hookContainer );
56  }
57 
69  public function getMessage(
70  Block $block,
71  UserIdentity $user,
72  Language $language,
73  $ip
74  ) {
75  $key = $this->getBlockErrorMessageKey( $block );
76  $params = $this->getBlockErrorMessageParams( $block, $user, $language, $ip );
77  return new Message( $key, $params );
78  }
79 
92  private function getBlockErrorInfo( Block $block ) {
93  $blocker = $block->getBlocker();
94  return [
95  'identifier' => $block->getIdentifier(),
96  'targetName' => $block->getTargetName(),
97  'blockerName' => $blocker ? $blocker->getName() : '',
98  'reason' => $block->getReasonComment(),
99  'expiry' => $block->getExpiry(),
100  'timestamp' => $block->getTimestamp(),
101  ];
102  }
103 
114  private function getFormattedBlockErrorInfo(
115  Block $block,
116  UserIdentity $user,
117  Language $language
118  ) {
119  $info = $this->getBlockErrorInfo( $block );
120 
121  $info['expiry'] = $language->formatExpiry( $info['expiry'], true, 'infinity', $user );
122  $info['timestamp'] = $language->userTimeAndDate( $info['timestamp'], $user );
123  $info['blockerName'] = $language->embedBidi( $info['blockerName'] );
124  $info['targetName'] = $language->embedBidi( $info['targetName'] );
125 
126  $info['reason'] = $this->formatBlockReason( $info['reason'], $language );
127 
128  return $info;
129  }
130 
138  private function formatBlockReason( CommentStoreComment $reason, Language $language ) {
139  if ( $reason->text === '' ) {
140  $message = new Message( 'blockednoreason', [], $language );
141  return $message->plain();
142  }
143  return $reason->message->inLanguage( $language )->plain();
144  }
145 
155  private function formatBlockerLink( ?UserIdentity $blocker, Language $language ) {
156  if ( !$blocker ) {
157  // TODO should we say something? This is just matching the code before
158  // the refactoring in late July 2021
159  return '';
160  }
161 
162  if ( $blocker->getId() === 0 ) {
163  // Foreign user
164  // TODO what about blocks placed by IPs? Shouldn't we check based on
165  // $blocker's wiki instead? This is just matching the code before the
166  // refactoring in late July 2021.
167  return $language->embedBidi( $blocker->getName() );
168  }
169 
170  $blockerUserpage = PageReferenceValue::localReference( NS_USER, $blocker->getName() );
171  $blockerText = $language->embedBidi(
172  $this->titleFormatter->getText( $blockerUserpage )
173  );
174  $prefixedText = $this->titleFormatter->getPrefixedText( $blockerUserpage );
175  return "[[{$prefixedText}|{$blockerText}]]";
176  }
177 
184  private function getBlockErrorMessageKey( Block $block ) {
185  $key = 'blockedtext';
186  if ( $block instanceof DatabaseBlock ) {
187  if ( $block->getType() === Block::TYPE_AUTO ) {
188  $key = 'autoblockedtext';
189  } elseif ( !$block->isSitewide() ) {
190  $key = 'blockedtext-partial';
191  }
192  } elseif ( $block instanceof SystemBlock ) {
193  $key = 'systemblockedtext';
194  } elseif ( $block instanceof CompositeBlock ) {
195  $key = 'blockedtext-composite';
196  }
197 
198  // Allow extensions to modify the block error message
199  $this->hookRunner->onGetBlockErrorMessageKey( $block, $key );
200 
201  return $key;
202  }
203 
222  private function getBlockErrorMessageParams(
223  Block $block,
224  UserIdentity $user,
225  Language $language,
226  $ip
227  ) {
228  $info = $this->getFormattedBlockErrorInfo( $block, $user, $language );
229 
230  // Add params that are specific to the standard block errors
231  $info['ip'] = $ip;
232  $info['blockerLink'] = $this->formatBlockerLink(
233  $block->getBlocker(),
234  $language
235  );
236 
237  // Display the CompositeBlock identifier as a message containing relevant block IDs
238  if ( $block instanceof CompositeBlock ) {
239  $ids = $language->commaList( array_map(
240  static function ( $id ) {
241  return '#' . $id;
242  },
243  array_filter( $info['identifier'], 'is_int' )
244  ) );
245  if ( $ids === '' ) {
246  $idsMsg = new Message( 'blockedtext-composite-no-ids', [], $language );
247  } else {
248  $idsMsg = new Message( 'blockedtext-composite-ids', [ $ids ], $language );
249  }
250  $info['identifier'] = $idsMsg->plain();
251  }
252 
253  // Messages expect the params in this order
254  $order = [
255  'blockerLink',
256  'reason',
257  'ip',
258  'blockerName',
259  'identifier',
260  'expiry',
261  'targetName',
262  'timestamp',
263  ];
264 
265  $params = [];
266  foreach ( $order as $item ) {
267  $params[] = $info[$item];
268  }
269 
270  return $params;
271  }
272 }
const NS_USER
Definition: Defines.php:66
Value object for a comment stored by CommentStore.
Base class for language-specific code.
Definition: Language.php:54
formatExpiry( $expiry, $format=true, $infinity='infinity', $user=null)
Decode an expiry (block, protection, etc) which has come from the DB.
Definition: Language.php:4572
embedBidi( $text='')
Wraps argument with unicode control characters for directionality safety.
Definition: Language.php:4068
userTimeAndDate( $ts, UserIdentity $user, array $options=[])
Get the formatted date and time for the given timestamp and formatted for the given user.
Definition: Language.php:2465
commaList(array $list)
Take a list of strings and build a locale-friendly comma-separated list, using the local comma-separa...
Definition: Language.php:3486
A service class for getting formatted information about a block.
getMessage(Block $block, UserIdentity $user, Language $language, $ip)
Get a block error message.
__construct(TitleFormatter $titleFormatter, HookContainer $hookContainer)
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:560
Immutable value object representing a page reference.
static localReference(int $namespace, string $dbKey)
Create PageReference for a local page.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition: Message.php:142
Represents a block that may prevent users from performing specific operations.
Definition: Block.php:40
getBlocker()
Get the user who applied this block.
getTimestamp()
Get the timestamp indicating when the block was created.
getTargetName()
Return the name of the block target as a string.
getIdentifier( $wikiId=self::LOCAL)
Get the information that identifies this block, such that a user could look up everything that can be...
getReasonComment()
Get the reason for creating the block.
getExpiry()
Get the block expiry time.
Interface for objects representing user identity.
A title formatter service for MediaWiki.