MediaWiki REL1_35
BlockErrorFormatter.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Block;
22
24use Language;
25use Message;
26use 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
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
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}
CommentStoreComment represents a comment stored by CommentStore.
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition Language.php:41
formatExpiry( $expiry, $format=true, $infinity='infinity')
Decode an expiry (block, protection, etc) which has come from the DB.
userTimeAndDate( $ts, User $user, array $options=[])
Get the formatted date and time for the given timestamp and formatted for the given user.
embedBidi( $text='')
Wraps argument with unicode control characters for directionality safety.
commaList(array $list)
Take a list of strings and build a locale-friendly comma-separated list, using the local comma-separa...
getTarget()
Get the target for this particular block.
getBy()
Get the user id of the blocking sysop.
getIdentifier()
Get the information that identifies this block, such that a user could look up everything that can be...
getByName()
Get the username of the blocking sysop.
getTimestamp()
Get the timestamp indicating when the block was created.
getReasonComment()
Get the reason for creating the block.
getType()
Get the type of target for this particular block.
isSitewide( $x=null)
Indicates that the block is a sitewide block.
getExpiry()
Get the block expiry time.
A service class for getting formatted information about a block.
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...
getBlockErrorMessageKey(AbstractBlock $block)
Determine the block error message key by examining the block.
formatBlockerLink( $blockerName, $blockerId, Language $language)
Create a link to the blocker's user page.
formatBlockReason(CommentStoreComment $reason, Language $language)
Format the block reason as plain wikitext in the specified language.
getMessage(AbstractBlock $block, User $user, Language $language, $ip)
Get a block error message.
getBlockErrorInfo(AbstractBlock $block)
Get a standard set of block details for building a block error message.
getBlockErrorMessageParams(AbstractBlock $block, User $user, Language $language, $ip)
Get the formatted parameters needed to build the block error messages handled by getBlockErrorMessage...
A DatabaseBlock (unlike a SystemBlock) is stored in the database, may give rise to autoblocks and may...
System blocks are temporary blocks that are created on enforcement (e.g.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:161
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:60
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition User.php:565