MediaWiki REL1_39
BlockErrorFormatter.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Block;
22
24use Language;
27use Message;
29
37
39 private $titleFormatter;
40
44 public function __construct(
45 TitleFormatter $titleFormatter
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}
const NS_USER
Definition Defines.php:66
Value object for a comment stored by CommentStore.
Base class for language-specific code.
Definition Language.php:53
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)
getMessage(Block $block, UserIdentity $user, Language $language, $ip)
Get a block error message.
Immutable value object representing a page reference.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:140
Represents a block that may prevent users from performing specific operations.
Definition Block.php:40
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.
Interface for objects representing user identity.
A title formatter service for MediaWiki.