MediaWiki master
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
81 public function getMessage(
82 Block $block,
83 UserIdentity $user,
84 Language $language,
85 string $ip
86 ): Message {
87 $key = $this->getBlockErrorMessageKey( $block, $user );
88 $params = $this->getBlockErrorMessageParams( $block, $user, $language, $ip );
89 return new Message( $key, $params );
90 }
91
102 public function getMessages(
103 Block $block,
104 UserIdentity $user,
105 Language $language,
106 string $ip
107 ): array {
108 $messages = [];
109 foreach ( $block->toArray() as $singleBlock ) {
110 $messages[] = $this->getMessage( $singleBlock, $user, $language, $ip );
111 }
112
113 return $messages;
114 }
115
128 private function getBlockErrorInfo( Block $block ) {
129 $blocker = $block->getBlocker();
130 return [
131 'identifier' => $block->getIdentifier(),
132 'targetName' => $block->getTargetName(),
133 'blockerName' => $blocker ? $blocker->getName() : '',
134 'reason' => $block->getReasonComment(),
135 'expiry' => $block->getExpiry(),
136 'timestamp' => $block->getTimestamp(),
137 ];
138 }
139
150 private function getFormattedBlockErrorInfo(
151 Block $block,
152 UserIdentity $user,
153 Language $language
154 ) {
155 $info = $this->getBlockErrorInfo( $block );
156
157 $info['expiry'] = $language->formatExpiry( $info['expiry'], true, 'infinity', $user );
158 $info['timestamp'] = $language->userTimeAndDate( $info['timestamp'], $user );
159 $info['blockerName'] = $language->embedBidi( $info['blockerName'] );
160 $info['targetName'] = $language->embedBidi( $info['targetName'] );
161
162 $info['reason'] = $this->formatBlockReason( $info['reason'], $language );
163
164 return $info;
165 }
166
174 private function formatBlockReason( CommentStoreComment $reason, Language $language ) {
175 if ( $reason->text === '' ) {
176 $message = new Message( 'blockednoreason', [], $language );
177 return $message->plain();
178 }
179 return $reason->message->inLanguage( $language )->plain();
180 }
181
191 private function formatBlockerLink( ?UserIdentity $blocker, Language $language ) {
192 if ( !$blocker ) {
193 // TODO should we say something? This is just matching the code before
194 // the refactoring in late July 2021
195 return '';
196 }
197
198 if ( $blocker->getId() === 0 ) {
199 // Foreign user
200 // TODO what about blocks placed by IPs? Shouldn't we check based on
201 // $blocker's wiki instead? This is just matching the code before the
202 // refactoring in late July 2021.
203 return $language->embedBidi( $blocker->getName() );
204 }
205
206 $blockerUserpage = PageReferenceValue::localReference( NS_USER, $blocker->getName() );
207 $blockerText = $language->embedBidi(
208 $this->titleFormatter->getText( $blockerUserpage )
209 );
210 $prefixedText = $this->titleFormatter->getPrefixedText( $blockerUserpage );
211 return "[[{$prefixedText}|{$blockerText}]]";
212 }
213
221 private function getBlockErrorMessageKey( Block $block, UserIdentity $user ) {
222 $isTempUser = $this->userIdentityUtils->isTemp( $user );
223 $key = $isTempUser ? 'blockedtext-tempuser' : 'blockedtext';
224 if ( $block instanceof DatabaseBlock ) {
225 if ( $block->getType() === Block::TYPE_AUTO ) {
226 $key = $isTempUser ? 'autoblockedtext-tempuser' : 'autoblockedtext';
227 } elseif ( !$block->isSitewide() ) {
228 $key = 'blockedtext-partial';
229 }
230 } elseif ( $block instanceof SystemBlock ) {
231 $key = 'systemblockedtext';
232 } elseif ( $block instanceof CompositeBlock ) {
233 $key = 'blockedtext-composite';
234 }
235
236 // Allow extensions to modify the block error message
237 $this->hookRunner->onGetBlockErrorMessageKey( $block, $key );
238
239 return $key;
240 }
241
260 private function getBlockErrorMessageParams(
261 Block $block,
262 UserIdentity $user,
263 Language $language,
264 string $ip
265 ) {
266 $info = $this->getFormattedBlockErrorInfo( $block, $user, $language );
267
268 // Add params that are specific to the standard block errors
269 $info['ip'] = $ip;
270 $info['blockerLink'] = $this->formatBlockerLink(
271 $block->getBlocker(),
272 $language
273 );
274
275 // Display the CompositeBlock identifier as a message containing relevant block IDs
276 if ( $block instanceof CompositeBlock ) {
277 $ids = $language->commaList( array_map(
278 static function ( $id ) {
279 return '#' . $id;
280 },
281 array_filter( $info['identifier'], 'is_int' )
282 ) );
283 if ( $ids === '' ) {
284 $idsMsg = new Message( 'blockedtext-composite-no-ids', [], $language );
285 } else {
286 $idsMsg = new Message( 'blockedtext-composite-ids', [ $ids ], $language );
287 }
288 $info['identifier'] = $idsMsg->plain();
289 }
290
291 // Messages expect the params in this order
292 $order = [
293 'blockerLink',
294 'reason',
295 'ip',
296 'blockerName',
297 'identifier',
298 'expiry',
299 'targetName',
300 'timestamp',
301 ];
302
303 $params = [];
304 foreach ( $order as $item ) {
305 $params[] = $info[$item];
306 }
307
308 return $params;
309 }
310}
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.
getMessages(Block $block, UserIdentity $user, Language $language, string $ip)
Get block error messages for all of the blocks that apply to a user.
__construct(TitleFormatter $titleFormatter, HookContainer $hookContainer, UserIdentityUtils $userIdentityUtils)
getMessage(Block $block, UserIdentity $user, Language $language, string $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:45
toArray()
Convert a block to an array of blocks.
A title formatter service for MediaWiki.
Interface for objects representing user identity.