MediaWiki REL1_35
BlockLogFormatter.php
Go to the documentation of this file.
1<?php
26
33 protected function getMessageParameters() {
34 $params = parent::getMessageParameters();
35
36 $title = $this->entry->getTarget();
37 if ( substr( $title->getText(), 0, 1 ) === '#' ) {
38 // autoblock - no user link possible
39 $params[2] = $title->getText();
40 $params[3] = ''; // no user name for gender use
41 } else {
42 // Create a user link for the blocked
43 $username = $title->getText();
44 // @todo Store the user identifier in the parameters
45 // to make this faster for future log entries
46 $targetUser = User::newFromName( $username, false );
47 $params[2] = Message::rawParam( $this->makeUserLink( $targetUser, Linker::TOOL_LINKS_NOBLOCK ) );
48 $params[3] = $username; // plain user name for gender use
49 }
50
51 $subtype = $this->entry->getSubtype();
52 if ( $subtype === 'block' || $subtype === 'reblock' ) {
53 if ( !isset( $params[4] ) ) {
54 // Very old log entry without duration: means infinite
55 $params[4] = 'infinite';
56 }
57 // Localize the duration, and add a tooltip
58 // in English to help visitors from other wikis.
59 // The lrm is needed to make sure that the number
60 // is shown on the correct side of the tooltip text.
61 $durationTooltip = '&lrm;' . htmlspecialchars( $params[4] );
62 $blockExpiry = $this->context->getLanguage()->translateBlockExpiry(
63 $params[4],
64 $this->context->getUser(),
65 wfTimestamp( TS_UNIX, $this->entry->getTimestamp() )
66 );
67 if ( $this->plaintext ) {
68 $params[4] = Message::rawParam( $blockExpiry );
69 } else {
70 $params[4] = Message::rawParam(
71 "<span class=\"blockExpiry\" title=\"$durationTooltip\">" .
72 htmlspecialchars( $blockExpiry ) .
73 '</span>'
74 );
75 }
76 $params[5] = isset( $params[5] ) ?
77 self::formatBlockFlags( $params[5], $this->context->getLanguage() ) : '';
78
79 // block restrictions
80 if ( isset( $params[6] ) ) {
81 $pages = $params[6]['pages'] ?? [];
82 $pages = array_map( function ( $page ) {
83 return $this->makePageLink( Title::newFromText( $page ) );
84 }, $pages );
85
86 $namespaces = $params[6]['namespaces'] ?? [];
87 $namespaces = array_map( function ( $ns ) {
88 $text = (int)$ns === NS_MAIN
89 ? $this->msg( 'blanknamespace' )->escaped()
90 : htmlspecialchars( $this->context->getLanguage()->getFormattedNsText( $ns ) );
91 $params = [ 'namespace' => $ns ];
92
93 return $this->makePageLink( SpecialPage::getTitleFor( 'Allpages' ), $params, $text );
94 }, $namespaces );
95
96 $restrictions = [];
97 if ( $pages ) {
98 $restrictions[] = $this->msg( 'logentry-partialblock-block-page' )
99 ->numParams( count( $pages ) )
100 ->rawParams( $this->context->getLanguage()->listToText( $pages ) )->escaped();
101 }
102
103 if ( $namespaces ) {
104 $restrictions[] = $this->msg( 'logentry-partialblock-block-ns' )
105 ->numParams( count( $namespaces ) )
106 ->rawParams( $this->context->getLanguage()->listToText( $namespaces ) )->escaped();
107 }
108
109 $params[6] = Message::rawParam( $this->context->getLanguage()->listToText( $restrictions ) );
110 }
111 }
112
113 return $params;
114 }
115
116 protected function extractParameters() {
117 $params = parent::extractParameters();
118 // Legacy log params returning the params in index 3 and 4, moved to 4 and 5
119 if ( $this->entry->isLegacy() && isset( $params[3] ) ) {
120 if ( isset( $params[4] ) ) {
121 $params[5] = $params[4];
122 }
123 $params[4] = $params[3];
124 $params[3] = '';
125 }
126 return $params;
127 }
128
129 public function getPreloadTitles() {
130 $title = $this->entry->getTarget();
131 $preload = [];
132 // Preload user page for non-autoblocks
133 if ( substr( $title->getText(), 0, 1 ) !== '#' && $title->canExist() ) {
134 $preload[] = $title->getTalkPage();
135 }
136 // Preload page restriction
137 $params = $this->extractParameters();
138 if ( isset( $params[6]['pages'] ) ) {
139 foreach ( $params[6]['pages'] as $page ) {
140 $preload[] = Title::newFromText( $page );
141 }
142 }
143 return $preload;
144 }
145
146 public function getActionLinks() {
147 $subtype = $this->entry->getSubtype();
149 if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) // Action is hidden
150 || !( $subtype === 'block' || $subtype === 'reblock' )
151 || !MediaWikiServices::getInstance()
152 ->getPermissionManager()
153 ->userHasRight( $this->context->getUser(), 'block' )
154 ) {
155 return '';
156 }
157
158 // Show unblock/change block link
159 $title = $this->entry->getTarget();
160 $links = [
162 SpecialPage::getTitleFor( 'Unblock', $title->getDBkey() ),
163 $this->msg( 'unblocklink' )->text()
164 ),
166 SpecialPage::getTitleFor( 'Block', $title->getDBkey() ),
167 $this->msg( 'change-blocklink' )->text()
168 )
169 ];
170
171 return $this->msg( 'parentheses' )->rawParams(
172 $this->context->getLanguage()->pipeList( $links ) )->escaped();
173 }
174
183 public static function formatBlockFlags( $flags, Language $lang ) {
184 $flags = trim( $flags );
185 if ( $flags === '' ) {
186 return ''; // nothing to do
187 }
188 $flags = explode( ',', $flags );
189 $flagsCount = count( $flags );
190
191 for ( $i = 0; $i < $flagsCount; $i++ ) {
192 $flags[$i] = self::formatBlockFlag( $flags[$i], $lang );
193 }
194
195 return wfMessage( 'parentheses' )->inLanguage( $lang )
196 ->rawParams( $lang->commaList( $flags ) )->escaped();
197 }
198
206 public static function formatBlockFlag( $flag, Language $lang ) {
207 static $messages = [];
208
209 if ( !isset( $messages[$flag] ) ) {
210 $messages[$flag] = htmlspecialchars( $flag ); // Fallback
211
212 // For grepping. The following core messages can be used here:
213 // * block-log-flags-angry-autoblock
214 // * block-log-flags-anononly
215 // * block-log-flags-hiddenname
216 // * block-log-flags-noautoblock
217 // * block-log-flags-nocreate
218 // * block-log-flags-noemail
219 // * block-log-flags-nousertalk
220 $msg = wfMessage( 'block-log-flags-' . $flag )->inLanguage( $lang );
221
222 if ( $msg->exists() ) {
223 $messages[$flag] = $msg->escaped();
224 }
225 }
226
227 return $messages[$flag];
228 }
229
230 protected function getParametersForApi() {
232 $params = $entry->getParameters();
233
234 static $map = [
235 // While this looks wrong to be starting at 5 rather than 4, it's
236 // because getMessageParameters uses $4 for its own purposes.
237 '5::duration',
238 '6:array:flags',
239 '6::flags' => '6:array:flags',
240 ];
241
242 foreach ( $map as $index => $key ) {
243 if ( isset( $params[$index] ) ) {
244 $params[$key] = $params[$index];
245 unset( $params[$index] );
246 }
247 }
248
249 ksort( $params );
250
251 $subtype = $entry->getSubtype();
252 if ( $subtype === 'block' || $subtype === 'reblock' ) {
253 // Defaults for old log entries missing some fields
254 $params += [
255 '5::duration' => 'infinite',
256 '6:array:flags' => [],
257 ];
258
259 if ( !is_array( $params['6:array:flags'] ) ) {
260 // @phan-suppress-next-line PhanSuspiciousValueComparison
261 $params['6:array:flags'] = $params['6:array:flags'] === ''
262 ? []
263 : explode( ',', $params['6:array:flags'] );
264 }
265
266 if ( !wfIsInfinity( $params['5::duration'] ) ) {
267 $ts = wfTimestamp( TS_UNIX, $entry->getTimestamp() );
268 $expiry = strtotime( $params['5::duration'], $ts );
269 if ( $expiry !== false && $expiry > 0 ) {
270 $params[':timestamp:expiry'] = $expiry;
271 }
272 }
273 }
274
275 return $params;
276 }
277
282 public function formatParametersForApi() {
283 $ret = parent::formatParametersForApi();
284 if ( isset( $ret['flags'] ) ) {
285 ApiResult::setIndexedTagName( $ret['flags'], 'f' );
286 }
287
288 if ( isset( $ret['restrictions']['pages'] ) ) {
289 $ret['restrictions']['pages'] = array_map( function ( $title ) {
290 return $this->formatParameterValueForApi( 'page', 'title-link', $title );
291 }, $ret['restrictions']['pages'] );
292 ApiResult::setIndexedTagName( $ret['restrictions']['pages'], 'p' );
293 }
294
295 if ( isset( $ret['restrictions']['namespaces'] ) ) {
296 ApiResult::setIndexedTagName( $ret['restrictions']['namespaces'], 'ns' );
297 }
298
299 return $ret;
300 }
301
302 protected function getMessageKey() {
303 $type = $this->entry->getType();
304 $subtype = $this->entry->getSubtype();
305 $sitewide = $this->entry->getParameters()['sitewide'] ?? true;
306
307 $key = "logentry-$type-$subtype";
308 if ( ( $subtype === 'block' || $subtype === 'reblock' ) && !$sitewide ) {
309 // $this->getMessageParameters is doing too much. We just need
310 // to check the presence of restrictions ($param[6]) and calling
311 // on parent gives us that
312 $params = parent::getMessageParameters();
313
314 // message changes depending on whether there are editing restrictions or not
315 if ( isset( $params[6] ) ) {
316 $key = "logentry-partial$type-$subtype";
317 } else {
318 $key = "logentry-non-editing-$type-$subtype";
319 }
320 }
321
322 return $key;
323 }
324}
wfIsInfinity( $str)
Determine input string is represents as infinity.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
This class formats block log entries.
getPreloadTitles()
Stable to override.
getMessageKey()
Returns a key to be used for formatting the action sentence.
getMessageParameters()
Formats parameters intented for action message from array of all parameters.
static formatBlockFlags( $flags, Language $lang)
Convert a comma-delimited list of block log flags into a more readable (and translated) form.
static formatBlockFlag( $flag, Language $lang)
Translate a block log flag if possible.
getActionLinks()
Returns extra links that comes after the action text, like "revert", etc.
getParametersForApi()
Get the array of parameters, converted from legacy format if necessary.
formatParametersForApi()
Format parameters for API output.The result array should generally map named keys to values....
extractParameters()
Extracts the optional extra parameters for use in action messages.
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition Language.php:41
const TOOL_LINKS_NOBLOCK
Flags for userToolLinks()
Definition Linker.php:40
Implements the default log formatting.
LogEntryBase $entry
msg( $key,... $params)
Shortcut for wfMessage which honors local context.
makeUserLink(User $user, $toolFlags=0)
formatParameterValueForApi( $name, $type, $value)
Format a single parameter value for API output.
LinkRenderer null $linkRenderer
makePageLink(Title $title=null, $parameters=[], $html=null)
Helper to make a link to the page, taking the plaintext value in consideration.
const DELETED_ACTION
Definition LogPage.php:38
makeKnownLink(LinkTarget $target, $text=null, array $extraAttribs=[], array $query=[])
MediaWikiServices is the service locator for the application scope of MediaWiki.
static rawParam( $raw)
Definition Message.php:1053
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:541
const NS_MAIN
Definition Defines.php:70
getParameters()
Get the extra parameters stored for this message.
getTimestamp()
Get the timestamp when the action was executed.
getSubtype()
The log subtype.
if(!isset( $args[0])) $lang