MediaWiki  master
BlockErrorFormatter.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Block;
22 
24 use Language;
26 use Message;
27 use User;
28 
36 
38  private $userFactory;
39 
43  public function __construct( UserFactory $userFactory ) {
44  $this->userFactory = $userFactory;
45  }
46 
58  public function getMessage(
59  AbstractBlock $block,
60  User $user,
61  Language $language,
62  $ip
63  ) {
64  $key = $this->getBlockErrorMessageKey( $block );
65  $params = $this->getBlockErrorMessageParams( $block, $user, $language, $ip );
66  return new Message( $key, $params );
67  }
68 
82  private function getBlockErrorInfo( AbstractBlock $block ) {
83  return [
84  'identifier' => $block->getIdentifier(),
85  'targetName' => (string)$block->getTarget(),
86  'blockerName' => $block->getByName(),
87  'blockerId' => $block->getBy(),
88  'reason' => $block->getReasonComment(),
89  'expiry' => $block->getExpiry(),
90  'timestamp' => $block->getTimestamp(),
91  ];
92  }
93 
104  private function getFormattedBlockErrorInfo(
105  AbstractBlock $block,
106  User $user,
107  Language $language
108  ) {
109  $info = $this->getBlockErrorInfo( $block );
110 
111  $info['expiry'] = $language->formatExpiry( $info['expiry'] );
112  $info['timestamp'] = $language->userTimeAndDate( $info['timestamp'], $user );
113  $info['blockerName'] = $language->embedBidi( $info['blockerName'] );
114  $info['targetName'] = $language->embedBidi( $info['targetName'] );
115 
116  $info['reason'] = $this->formatBlockReason( $info['reason'], $language );
117 
118  return $info;
119  }
120 
128  private function formatBlockReason( CommentStoreComment $reason, Language $language ) {
129  if ( $reason->text === '' ) {
130  $message = new Message( 'blockednoreason', [], $language );
131  return $message->text();
132  }
133  return $reason->message->inLanguage( $language )->plain();
134  }
135 
146  private function formatBlockerLink( $blockerName, $blockerId, Language $language ) {
147  if ( $blockerId === 0 ) {
148  // Foreign user
149  return $blockerName;
150  }
151 
152  $blocker = $this->userFactory->newFromId( (int)$blockerId );
153  $blockerUserpage = $blocker->getUserPage();
154  $blockerText = $language->embedBidi( $blockerUserpage->getText() );
155  return "[[{$blockerUserpage->getPrefixedText()}|{$blockerText}]]";
156  }
157 
164  private function getBlockErrorMessageKey( AbstractBlock $block ) {
165  $key = 'blockedtext';
166  if ( $block instanceof DatabaseBlock ) {
167  if ( $block->getType() === AbstractBlock::TYPE_AUTO ) {
168  $key = 'autoblockedtext';
169  } elseif ( !$block->isSitewide() ) {
170  $key = 'blockedtext-partial';
171  }
172  } elseif ( $block instanceof SystemBlock ) {
173  $key = 'systemblockedtext';
174  } elseif ( $block instanceof CompositeBlock ) {
175  $key = 'blockedtext-composite';
176  }
177  return $key;
178  }
179 
198  private function getBlockErrorMessageParams(
199  AbstractBlock $block,
200  User $user,
201  Language $language,
202  $ip
203  ) {
204  $info = $this->getFormattedBlockErrorInfo( $block, $user, $language );
205 
206  // Add params that are specific to the standard block errors
207  $info['ip'] = $ip;
208  $info['blockerLink'] = $this->formatBlockerLink(
209  $info['blockerName'],
210  $info['blockerId'],
211  $language
212  );
213 
214  // Display the CompositeBlock identifier as a message containing relevant block IDs
215  if ( $block instanceof CompositeBlock ) {
216  $ids = $language->commaList( array_map(
217  function ( $id ) {
218  return '#' . $id;
219  },
220  array_filter( $info['identifier'], 'is_int' )
221  ) );
222  if ( $ids === '' ) {
223  $idsMsg = new Message( 'blockedtext-composite-no-ids', [], $language );
224  } else {
225  $idsMsg = new Message( 'blockedtext-composite-ids', [ $ids ], $language );
226  }
227  $info['identifier'] = $idsMsg->plain();
228  }
229 
230  // Messages expect the params in this order
231  $order = [
232  'blockerLink',
233  'reason',
234  'ip',
235  'blockerName',
236  'identifier',
237  'expiry',
238  'targetName',
239  'timestamp',
240  ];
241 
242  $params = [];
243  foreach ( $order as $item ) {
244  $params[] = $info[$item];
245  }
246 
247  return $params;
248  }
249 }
MediaWiki\Block\AbstractBlock\getBy
getBy()
Get the user id of the blocking sysop.
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:3547
MediaWiki\Block
Definition: AbstractBlock.php:21
MediaWiki\Block\BlockErrorFormatter
A service class for getting formatted information about a block.
Definition: BlockErrorFormatter.php:35
MediaWiki\Block\AbstractBlock\TYPE_AUTO
const TYPE_AUTO
Definition: AbstractBlock.php:94
Language\embedBidi
embedBidi( $text='')
Wraps argument with unicode control characters for directionality safety.
Definition: Language.php:4121
Language\formatExpiry
formatExpiry( $expiry, $format=true, $infinity='infinity')
Decode an expiry (block, protection, etc) which has come from the DB.
Definition: Language.php:4687
MediaWiki\Block\BlockErrorFormatter\getFormattedBlockErrorInfo
getFormattedBlockErrorInfo(AbstractBlock $block, User $user, Language $language)
Get a standard set of block details for building a block error message, formatted for a specified use...
Definition: BlockErrorFormatter.php:104
Language\userTimeAndDate
userTimeAndDate( $ts, User $user, array $options=[])
Get the formatted date and time for the given timestamp and formatted for the given user.
Definition: Language.php:2517
MediaWiki\Block\BlockErrorFormatter\getBlockErrorMessageParams
getBlockErrorMessageParams(AbstractBlock $block, User $user, Language $language, $ip)
Get the formatted parameters needed to build the block error messages handled by getBlockErrorMessage...
Definition: BlockErrorFormatter.php:198
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\__construct
__construct(UserFactory $userFactory)
Definition: BlockErrorFormatter.php:43
MediaWiki\Block\BlockErrorFormatter\$userFactory
UserFactory $userFactory
Definition: BlockErrorFormatter.php:38
MediaWiki\Block\BlockErrorFormatter\getMessage
getMessage(AbstractBlock $block, User $user, Language $language, $ip)
Get a block error message.
Definition: BlockErrorFormatter.php:58
MediaWiki\Block\BlockErrorFormatter\getBlockErrorMessageKey
getBlockErrorMessageKey(AbstractBlock $block)
Determine the block error message key by examining the block.
Definition: BlockErrorFormatter.php:164
MediaWiki\Block\CompositeBlock
Multiple Block class.
Definition: CompositeBlock.php:35
MediaWiki\Block\BlockErrorFormatter\formatBlockerLink
formatBlockerLink( $blockerName, $blockerId, Language $language)
Create a link to the blocker's user page.
Definition: BlockErrorFormatter.php:146
MediaWiki\Block\AbstractBlock\getReasonComment
getReasonComment()
Get the reason for creating the block.
Definition: AbstractBlock.php:177
MediaWiki\Block\AbstractBlock\getType
getType()
Get the type of target for this particular block.
Definition: AbstractBlock.php:350
MediaWiki\Block\AbstractBlock\isSitewide
isSitewide( $x=null)
Indicates that the block is a sitewide block.
Definition: AbstractBlock.php:220
MediaWiki\Block\AbstractBlock\getIdentifier
getIdentifier()
Get the information that identifies this block, such that a user could look up everything that can be...
MediaWiki\Block\SystemBlock
System blocks are temporary blocks that are created on enforcement (e.g.
Definition: SystemBlock.php:33
MediaWiki\Block\AbstractBlock\getExpiry
getExpiry()
Get the block expiry time.
Definition: AbstractBlock.php:384
MediaWiki\Block\AbstractBlock\getTimestamp
getTimestamp()
Get the timestamp indicating when the block was created.
Definition: AbstractBlock.php:404
MediaWiki\Block\BlockErrorFormatter\formatBlockReason
formatBlockReason(CommentStoreComment $reason, Language $language)
Format the block reason as plain wikitext in the specified language.
Definition: BlockErrorFormatter.php:128
Message
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition: Message.php:161
MediaWiki\Block\AbstractBlock
Definition: AbstractBlock.php:37
MediaWiki\Block\AbstractBlock\getByName
getByName()
Get the username of the blocking sysop.
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:56
MediaWiki\Block\AbstractBlock\getTarget
getTarget()
Get the target for this particular block.
Definition: AbstractBlock.php:374
MediaWiki\User\UserFactory
Creates User objects.
Definition: UserFactory.php:40
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
MediaWiki\Block\BlockErrorFormatter\getBlockErrorInfo
getBlockErrorInfo(AbstractBlock $block)
Get a standard set of block details for building a block error message.
Definition: BlockErrorFormatter.php:82