MediaWiki 1.41.2
BlockErrorFormatter.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Block;
22
23use Language;
31use Message;
32
40
42 private $titleFormatter;
43
45 private $hookRunner;
46
50 private $userIdentityUtils;
51
57 public function __construct(
58 TitleFormatter $titleFormatter,
59 HookContainer $hookContainer,
60 UserIdentityUtils $userIdentityUtils
61 ) {
62 $this->titleFormatter = $titleFormatter;
63 $this->hookRunner = new HookRunner( $hookContainer );
64 $this->userIdentityUtils = $userIdentityUtils;
65 }
66
78 public function getMessage(
79 Block $block,
80 UserIdentity $user,
81 Language $language,
82 $ip
83 ) {
84 $key = $this->getBlockErrorMessageKey( $block, $user );
85 $params = $this->getBlockErrorMessageParams( $block, $user, $language, $ip );
86 return new Message( $key, $params );
87 }
88
101 private function getBlockErrorInfo( Block $block ) {
102 $blocker = $block->getBlocker();
103 return [
104 'identifier' => $block->getIdentifier(),
105 'targetName' => $block->getTargetName(),
106 'blockerName' => $blocker ? $blocker->getName() : '',
107 'reason' => $block->getReasonComment(),
108 'expiry' => $block->getExpiry(),
109 'timestamp' => $block->getTimestamp(),
110 ];
111 }
112
123 private function getFormattedBlockErrorInfo(
124 Block $block,
125 UserIdentity $user,
126 Language $language
127 ) {
128 $info = $this->getBlockErrorInfo( $block );
129
130 $info['expiry'] = $language->formatExpiry( $info['expiry'], true, 'infinity', $user );
131 $info['timestamp'] = $language->userTimeAndDate( $info['timestamp'], $user );
132 $info['blockerName'] = $language->embedBidi( $info['blockerName'] );
133 $info['targetName'] = $language->embedBidi( $info['targetName'] );
134
135 $info['reason'] = $this->formatBlockReason( $info['reason'], $language );
136
137 return $info;
138 }
139
147 private function formatBlockReason( CommentStoreComment $reason, Language $language ) {
148 if ( $reason->text === '' ) {
149 $message = new Message( 'blockednoreason', [], $language );
150 return $message->plain();
151 }
152 return $reason->message->inLanguage( $language )->plain();
153 }
154
164 private function formatBlockerLink( ?UserIdentity $blocker, Language $language ) {
165 if ( !$blocker ) {
166 // TODO should we say something? This is just matching the code before
167 // the refactoring in late July 2021
168 return '';
169 }
170
171 if ( $blocker->getId() === 0 ) {
172 // Foreign user
173 // TODO what about blocks placed by IPs? Shouldn't we check based on
174 // $blocker's wiki instead? This is just matching the code before the
175 // refactoring in late July 2021.
176 return $language->embedBidi( $blocker->getName() );
177 }
178
179 $blockerUserpage = PageReferenceValue::localReference( NS_USER, $blocker->getName() );
180 $blockerText = $language->embedBidi(
181 $this->titleFormatter->getText( $blockerUserpage )
182 );
183 $prefixedText = $this->titleFormatter->getPrefixedText( $blockerUserpage );
184 return "[[{$prefixedText}|{$blockerText}]]";
185 }
186
194 private function getBlockErrorMessageKey( Block $block, UserIdentity $user ) {
195 $isTempUser = $this->userIdentityUtils->isTemp( $user );
196 $key = $isTempUser ? 'blockedtext-tempuser' : 'blockedtext';
197 if ( $block instanceof DatabaseBlock ) {
198 if ( $block->getType() === Block::TYPE_AUTO ) {
199 $key = $isTempUser ? 'autoblockedtext-tempuser' : 'autoblockedtext';
200 } elseif ( !$block->isSitewide() ) {
201 $key = 'blockedtext-partial';
202 }
203 } elseif ( $block instanceof SystemBlock ) {
204 $key = 'systemblockedtext';
205 } elseif ( $block instanceof CompositeBlock ) {
206 $key = 'blockedtext-composite';
207 }
208
209 // Allow extensions to modify the block error message
210 $this->hookRunner->onGetBlockErrorMessageKey( $block, $key );
211
212 return $key;
213 }
214
233 private function getBlockErrorMessageParams(
234 Block $block,
235 UserIdentity $user,
236 Language $language,
237 $ip
238 ) {
239 $info = $this->getFormattedBlockErrorInfo( $block, $user, $language );
240
241 // Add params that are specific to the standard block errors
242 $info['ip'] = $ip;
243 $info['blockerLink'] = $this->formatBlockerLink(
244 $block->getBlocker(),
245 $language
246 );
247
248 // Display the CompositeBlock identifier as a message containing relevant block IDs
249 if ( $block instanceof CompositeBlock ) {
250 $ids = $language->commaList( array_map(
251 static function ( $id ) {
252 return '#' . $id;
253 },
254 array_filter( $info['identifier'], 'is_int' )
255 ) );
256 if ( $ids === '' ) {
257 $idsMsg = new Message( 'blockedtext-composite-no-ids', [], $language );
258 } else {
259 $idsMsg = new Message( 'blockedtext-composite-ids', [ $ids ], $language );
260 }
261 $info['identifier'] = $idsMsg->plain();
262 }
263
264 // Messages expect the params in this order
265 $order = [
266 'blockerLink',
267 'reason',
268 'ip',
269 'blockerName',
270 'identifier',
271 'expiry',
272 'targetName',
273 'timestamp',
274 ];
275
276 $params = [];
277 foreach ( $order as $item ) {
278 $params[] = $info[$item];
279 }
280
281 return $params;
282 }
283}
const NS_USER
Definition Defines.php:66
Base class for language-specific code.
Definition Language.php:63
formatExpiry( $expiry, $format=true, $infinity='infinity', $user=null)
Decode an expiry (block, protection, etc.) which has come from the DB.
embedBidi( $text='')
Wraps argument with unicode control characters for directionality safety.
userTimeAndDate( $ts, UserIdentity $user, array $options=[])
Get the formatted date and time for the given timestamp and formatted for the given user.
commaList(array $list)
Take a list of strings and build a locale-friendly comma-separated list, using the local comma-separa...
A service class for getting formatted information about a block.
__construct(TitleFormatter $titleFormatter, HookContainer $hookContainer, UserIdentityUtils $userIdentityUtils)
getMessage(Block $block, UserIdentity $user, Language $language, $ip)
Get a block error message.
Value object for a comment stored by CommentStore.
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Immutable value object representing a page reference.
Convenience functions for interpreting UserIdentity objects using additional services or config.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:144
Represents a block that may prevent users from performing specific operations.
Definition Block.php:41
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.
A title formatter service for MediaWiki.
Interface for objects representing user identity.