MediaWiki  master
BlockErrorFormatter.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Block;
22 
24 use Language;
25 use Message;
26 use User;
27 
46  public function getMessage(
47  AbstractBlock $block,
48  User $user,
49  Language $language,
50  $ip
51  ) {
52  $key = $this->getBlockErrorMessageKey( $block );
53  $params = $this->getBlockErrorMessageParams( $block, $user, $language, $ip );
54  return new Message( $key, $params );
55  }
56 
70  private function getBlockErrorInfo( AbstractBlock $block ) {
71  return [
72  'identifier' => $block->getIdentifier(),
73  'targetName' => (string)$block->getTarget(),
74  'blockerName' => $block->getByName(),
75  'blockerId' => $block->getBy(),
76  'reason' => $block->getReasonComment(),
77  'expiry' => $block->getExpiry(),
78  'timestamp' => $block->getTimestamp(),
79  ];
80  }
81 
92  private function getFormattedBlockErrorInfo(
93  AbstractBlock $block,
94  User $user,
95  Language $language
96  ) {
97  $info = $this->getBlockErrorInfo( $block );
98 
99  $info['expiry'] = $language->formatExpiry( $info['expiry'] );
100  $info['timestamp'] = $language->userTimeAndDate( $info['timestamp'], $user );
101  $info['blockerName'] = $language->embedBidi( $info['blockerName'] );
102  $info['targetName'] = $language->embedBidi( $info['targetName'] );
103 
104  $info['reason'] = $this->formatBlockReason( $info['reason'], $language );
105 
106  return $info;
107  }
108 
116  private function formatBlockReason( CommentStoreComment $reason, Language $language ) {
117  if ( $reason->text === '' ) {
118  $message = new Message( 'blockednoreason', [], $language );
119  return $message->text();
120  }
121  return $reason->message->inLanguage( $language )->plain();
122  }
123 
134  private function formatBlockerLink( $blockerName, $blockerId, Language $language ) {
135  if ( $blockerId === 0 ) {
136  // Foreign user
137  return $blockerName;
138  }
139 
140  $blocker = User::newFromId( $blockerId );
141  $blockerUserpage = $blocker->getUserPage();
142  $blockerText = $language->embedBidi( $blockerUserpage->getText() );
143  return "[[{$blockerUserpage->getPrefixedText()}|{$blockerText}]]";
144  }
145 
152  private function getBlockErrorMessageKey( AbstractBlock $block ) {
153  $key = 'blockedtext';
154  if ( $block instanceof DatabaseBlock ) {
155  if ( $block->getType() === AbstractBlock::TYPE_AUTO ) {
156  $key = 'autoblockedtext';
157  } elseif ( !$block->isSitewide() ) {
158  $key = 'blockedtext-partial';
159  }
160  } elseif ( $block instanceof SystemBlock ) {
161  $key = 'systemblockedtext';
162  } elseif ( $block instanceof CompositeBlock ) {
163  $key = 'blockedtext-composite';
164  }
165  return $key;
166  }
167 
186  private function getBlockErrorMessageParams(
187  AbstractBlock $block,
188  User $user,
189  Language $language,
190  $ip
191  ) {
192  $info = $this->getFormattedBlockErrorInfo( $block, $user, $language );
193 
194  // Add params that are specific to the standard block errors
195  $info['ip'] = $ip;
196  $info['blockerLink'] = $this->formatBlockerLink(
197  $info['blockerName'],
198  $info['blockerId'],
199  $language
200  );
201 
202  // Display the CompositeBlock identifier as a message containing relevant block IDs
203  if ( $block instanceof CompositeBlock ) {
204  $ids = $language->commaList( array_map(
205  function ( $id ) {
206  return '#' . $id;
207  },
208  array_filter( $info['identifier'], 'is_int' )
209  ) );
210  if ( $ids === '' ) {
211  $idsMsg = new Message( 'blockedtext-composite-no-ids', [], $language );
212  } else {
213  $idsMsg = new Message( 'blockedtext-composite-ids', [ $ids ], $language );
214  }
215  $info['identifier'] = $idsMsg->plain();
216  }
217 
218  // Messages expect the params in this order
219  $order = [
220  'blockerLink',
221  'reason',
222  'ip',
223  'blockerName',
224  'identifier',
225  'expiry',
226  'targetName',
227  'timestamp',
228  ];
229 
230  $params = [];
231  foreach ( $order as $item ) {
232  $params[] = $info[$item];
233  }
234 
235  return $params;
236  }
237 }
MediaWiki\Block\AbstractBlock\getBy
getBy()
Get the user id of the blocking sysop.
User\newFromId
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:562
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:3449
MediaWiki\Block
Definition: AbstractBlock.php:21
MediaWiki\Block\BlockErrorFormatter
A service class for getting formatted information about a block.
Definition: BlockErrorFormatter.php:34
MediaWiki\Block\AbstractBlock\TYPE_AUTO
const TYPE_AUTO
Definition: AbstractBlock.php:91
Language\embedBidi
embedBidi( $text='')
Wraps argument with unicode control characters for directionality safety.
Definition: Language.php:4024
Language\formatExpiry
formatExpiry( $expiry, $format=true, $infinity='infinity')
Decode an expiry (block, protection, etc) which has come from the DB.
Definition: Language.php:4587
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:92
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:2520
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:186
MediaWiki\Block\DatabaseBlock
A DatabaseBlock (unlike a SystemBlock) is stored in the database, may give rise to autoblocks and may...
Definition: DatabaseBlock.php:52
MediaWiki\Block\BlockErrorFormatter\getMessage
getMessage(AbstractBlock $block, User $user, Language $language, $ip)
Get a block error message.
Definition: BlockErrorFormatter.php:46
MediaWiki\Block\BlockErrorFormatter\getBlockErrorMessageKey
getBlockErrorMessageKey(AbstractBlock $block)
Determine the block error message key by examining the block.
Definition: BlockErrorFormatter.php:152
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:134
MediaWiki\Block\AbstractBlock\getReasonComment
getReasonComment()
Get the reason for creating the block.
Definition: AbstractBlock.php:172
MediaWiki\Block\AbstractBlock\getType
getType()
Get the type of target for this particular block.
Definition: AbstractBlock.php:435
MediaWiki\Block\AbstractBlock\isSitewide
isSitewide( $x=null)
Indicates that the block is a sitewide block.
Definition: AbstractBlock.php:215
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:469
MediaWiki\Block\AbstractBlock\getTimestamp
getTimestamp()
Get the timestamp indicating when the block was created.
Definition: AbstractBlock.php:489
MediaWiki\Block\BlockErrorFormatter\formatBlockReason
formatBlockReason(CommentStoreComment $reason, Language $language)
Format the block reason as plain wikitext in the specified language.
Definition: BlockErrorFormatter.php:116
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:38
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:55
MediaWiki\Block\AbstractBlock\getTarget
getTarget()
Get the target for this particular block.
Definition: AbstractBlock.php:459
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:41
MediaWiki\Block\BlockErrorFormatter\getBlockErrorInfo
getBlockErrorInfo(AbstractBlock $block)
Get a standard set of block details for building a block error message.
Definition: BlockErrorFormatter.php:70