MediaWiki  master
BlockErrorFormatter.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Block;
22 
24 use Language;
27 use Message;
28 use TitleFormatter;
29 
37 
39  private $titleFormatter;
40 
44  public function __construct(
46  ) {
47  $this->titleFormatter = $titleFormatter;
48  }
49 
61  public function getMessage(
62  Block $block,
63  UserIdentity $user,
64  Language $language,
65  $ip
66  ) {
67  $key = $this->getBlockErrorMessageKey( $block );
68  $params = $this->getBlockErrorMessageParams( $block, $user, $language, $ip );
69  return new Message( $key, $params );
70  }
71 
84  private function getBlockErrorInfo( Block $block ) {
85  $blocker = $block->getBlocker();
86  return [
87  'identifier' => $block->getIdentifier(),
88  'targetName' => $block->getTargetName(),
89  'blockerName' => $blocker ? $blocker->getName() : '',
90  'reason' => $block->getReasonComment(),
91  'expiry' => $block->getExpiry(),
92  'timestamp' => $block->getTimestamp(),
93  ];
94  }
95 
106  private function getFormattedBlockErrorInfo(
107  Block $block,
108  UserIdentity $user,
109  Language $language
110  ) {
111  $info = $this->getBlockErrorInfo( $block );
112 
113  $info['expiry'] = $language->formatExpiry( $info['expiry'], true, 'infinity', $user );
114  $info['timestamp'] = $language->userTimeAndDate( $info['timestamp'], $user );
115  $info['blockerName'] = $language->embedBidi( $info['blockerName'] );
116  $info['targetName'] = $language->embedBidi( $info['targetName'] );
117 
118  $info['reason'] = $this->formatBlockReason( $info['reason'], $language );
119 
120  return $info;
121  }
122 
130  private function formatBlockReason( CommentStoreComment $reason, Language $language ) {
131  if ( $reason->text === '' ) {
132  $message = new Message( 'blockednoreason', [], $language );
133  return $message->plain();
134  }
135  return $reason->message->inLanguage( $language )->plain();
136  }
137 
147  private function formatBlockerLink( ?UserIdentity $blocker, Language $language ) {
148  if ( !$blocker ) {
149  // TODO should we say something? This is just matching the code before
150  // the refactoring in late July 2021
151  return '';
152  }
153 
154  if ( $blocker->getId() === 0 ) {
155  // Foreign user
156  // TODO what about blocks placed by IPs? Shouldn't we check based on
157  // $blocker's wiki instead? This is just matching the code before the
158  // refactoring in late July 2021.
159  return $language->embedBidi( $blocker->getName() );
160  }
161 
162  $blockerUserpage = PageReferenceValue::localReference( NS_USER, $blocker->getName() );
163  $blockerText = $language->embedBidi(
164  $this->titleFormatter->getText( $blockerUserpage )
165  );
166  $prefixedText = $this->titleFormatter->getPrefixedText( $blockerUserpage );
167  return "[[{$prefixedText}|{$blockerText}]]";
168  }
169 
176  private function getBlockErrorMessageKey( Block $block ) {
177  $key = 'blockedtext';
178  if ( $block instanceof DatabaseBlock ) {
179  if ( $block->getType() === Block::TYPE_AUTO ) {
180  $key = 'autoblockedtext';
181  } elseif ( !$block->isSitewide() ) {
182  $key = 'blockedtext-partial';
183  }
184  } elseif ( $block instanceof SystemBlock ) {
185  $key = 'systemblockedtext';
186  } elseif ( $block instanceof CompositeBlock ) {
187  $key = 'blockedtext-composite';
188  }
189  return $key;
190  }
191 
210  private function getBlockErrorMessageParams(
211  Block $block,
212  UserIdentity $user,
213  Language $language,
214  $ip
215  ) {
216  $info = $this->getFormattedBlockErrorInfo( $block, $user, $language );
217 
218  // Add params that are specific to the standard block errors
219  $info['ip'] = $ip;
220  $info['blockerLink'] = $this->formatBlockerLink(
221  $block->getBlocker(),
222  $language
223  );
224 
225  // Display the CompositeBlock identifier as a message containing relevant block IDs
226  if ( $block instanceof CompositeBlock ) {
227  $ids = $language->commaList( array_map(
228  static function ( $id ) {
229  return '#' . $id;
230  },
231  array_filter( $info['identifier'], 'is_int' )
232  ) );
233  if ( $ids === '' ) {
234  $idsMsg = new Message( 'blockedtext-composite-no-ids', [], $language );
235  } else {
236  $idsMsg = new Message( 'blockedtext-composite-ids', [ $ids ], $language );
237  }
238  $info['identifier'] = $idsMsg->plain();
239  }
240 
241  // Messages expect the params in this order
242  $order = [
243  'blockerLink',
244  'reason',
245  'ip',
246  'blockerName',
247  'identifier',
248  'expiry',
249  'targetName',
250  'timestamp',
251  ];
252 
253  $params = [];
254  foreach ( $order as $item ) {
255  $params[] = $info[$item];
256  }
257 
258  return $params;
259  }
260 }
MediaWiki\Block\BlockErrorFormatter\$titleFormatter
TitleFormatter $titleFormatter
Definition: BlockErrorFormatter.php:39
Language\commaList
commaList(array $list)
Take a list of strings and build a locale-friendly comma-separated list, using the local comma-separa...
Definition: Language.php:3505
MediaWiki\Block\BlockErrorFormatter\getBlockErrorMessageKey
getBlockErrorMessageKey(Block $block)
Determine the block error message key by examining the block.
Definition: BlockErrorFormatter.php:176
MediaWiki\Block
Definition: AbstractBlock.php:21
MediaWiki\Block\Block\getIdentifier
getIdentifier()
Get the information that identifies this block, such that a user could look up everything that can be...
MediaWiki\Block\BlockErrorFormatter\getMessage
getMessage(Block $block, UserIdentity $user, Language $language, $ip)
Get a block error message.
Definition: BlockErrorFormatter.php:61
MediaWiki\Block\BlockErrorFormatter
A service class for getting formatted information about a block.
Definition: BlockErrorFormatter.php:36
MediaWiki\User\UserIdentity\getId
getId( $wikiId=self::LOCAL)
Language\embedBidi
embedBidi( $text='')
Wraps argument with unicode control characters for directionality safety.
Definition: Language.php:4080
MediaWiki\Block\Block\getBlocker
getBlocker()
Get the user who applied this block.
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
MediaWiki\Block\DatabaseBlock
A DatabaseBlock (unlike a SystemBlock) is stored in the database, may give rise to autoblocks and may...
Definition: DatabaseBlock.php:50
MediaWiki\Block\BlockErrorFormatter\getBlockErrorInfo
getBlockErrorInfo(Block $block)
Get a standard set of block details for building a block error message.
Definition: BlockErrorFormatter.php:84
MediaWiki\Block\Block\getType
getType()
Get the type of target for this particular block.
MediaWiki\Block\CompositeBlock
Multiple Block class.
Definition: CompositeBlock.php:36
MediaWiki\Block\Block\isSitewide
isSitewide()
Indicates that the block is a sitewide block.
Page\PageReferenceValue\localReference
static localReference(int $namespace, string $dbKey)
Create PageReference for a local page.
Definition: PageReferenceValue.php:83
MediaWiki\User\UserIdentity\getName
getName()
MediaWiki\Block\BlockErrorFormatter\getBlockErrorMessageParams
getBlockErrorMessageParams(Block $block, UserIdentity $user, Language $language, $ip)
Get the formatted parameters needed to build the block error messages handled by getBlockErrorMessage...
Definition: BlockErrorFormatter.php:210
MediaWiki\Block\BlockErrorFormatter\__construct
__construct(TitleFormatter $titleFormatter)
Definition: BlockErrorFormatter.php:44
MediaWiki\Block\Block
Represents a block that may prevent users from performing specific operations.
Definition: Block.php:37
MediaWiki\Block\SystemBlock
System blocks are temporary blocks that are created on enforcement (e.g.
Definition: SystemBlock.php:35
MediaWiki\Block\Block\TYPE_AUTO
const TYPE_AUTO
Definition: Block.php:44
NS_USER
const NS_USER
Definition: Defines.php:66
MediaWiki\Block\BlockErrorFormatter\getFormattedBlockErrorInfo
getFormattedBlockErrorInfo(Block $block, UserIdentity $user, Language $language)
Get a standard set of block details for building a block error message, formatted for a specified use...
Definition: BlockErrorFormatter.php:106
Language\userTimeAndDate
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:2481
MediaWiki\Block\Block\getTimestamp
getTimestamp()
Get the timestamp indicating when the block was created.
MediaWiki\Block\BlockErrorFormatter\formatBlockReason
formatBlockReason(CommentStoreComment $reason, Language $language)
Format the block reason as plain wikitext in the specified language.
Definition: BlockErrorFormatter.php:130
Page\PageReferenceValue
Immutable value object representing a page reference.
Definition: PageReferenceValue.php:42
TitleFormatter
A title formatter service for MediaWiki.
Definition: TitleFormatter.php:35
Message
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition: Message.php:139
MediaWiki\Block\Block\getExpiry
getExpiry()
Get the block expiry time.
Language\formatExpiry
formatExpiry( $expiry, $format=true, $infinity='infinity', $user=null)
Decode an expiry (block, protection, etc) which has come from the DB.
Definition: Language.php:4585
MediaWiki\Block\Block\getTargetName
getTargetName()
Return the name of the block target as a string.
MediaWiki\Block\Block\getReasonComment
getReasonComment()
Get the reason for creating the block.
MediaWiki\Block\BlockErrorFormatter\formatBlockerLink
formatBlockerLink(?UserIdentity $blocker, Language $language)
Create a link to the blocker's user page.
Definition: BlockErrorFormatter.php:147
CommentStoreComment
Value object for a comment stored by CommentStore.
Definition: CommentStoreComment.php:30
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:42