MediaWiki  master
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  $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' )->text()
90  : $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 ) )->text();
101  }
102 
103  if ( $namespaces ) {
104  $restrictions[] = $this->msg( 'logentry-partialblock-block-ns' )
105  ->numParams( count( $namespaces ) )
106  ->rawParams( $this->context->getLanguage()->listToText( $namespaces ) )->text();
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();
148  $linkRenderer = $this->getLinkRenderer();
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 = [
161  $linkRenderer->makeKnownLink(
162  SpecialPage::getTitleFor( 'Unblock', $title->getDBkey() ),
163  $this->msg( 'unblocklink' )->text()
164  ),
165  $linkRenderer->makeKnownLink(
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 }
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:329
Linker\TOOL_LINKS_NOBLOCK
const TOOL_LINKS_NOBLOCK
Flags for userToolLinks()
Definition: Linker.php:40
LogEntry\getTimestamp
getTimestamp()
Get the timestamp when the action was executed.
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:152
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
LogFormatter\makeUserLink
makeUserLink(User $user, $toolFlags=0)
Definition: LogFormatter.php:771
LogEntry\getParameters
getParameters()
Get the extra parameters stored for this message.
LogFormatter\$entry
LogEntryBase $entry
Definition: LogFormatter.php:77
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1811
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:533
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1222
SpecialPage\getTitleFor
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,...
Definition: SpecialPage.php:90
BlockLogFormatter\formatParametersForApi
formatParametersForApi()
Format parameters for API output.The result array should generally map named keys to values....
Definition: BlockLogFormatter.php:282
NS_MAIN
const NS_MAIN
Definition: Defines.php:69
$title
$title
Definition: testCompression.php:38
BlockLogFormatter\getPreloadTitles
getPreloadTitles()
Definition: BlockLogFormatter.php:129
LogPage\DELETED_ACTION
const DELETED_ACTION
Definition: LogPage.php:34
LogFormatter\formatParameterValueForApi
formatParameterValueForApi( $name, $type, $value)
Format a single parameter value for API output.
Definition: LogFormatter.php:866
ApiResult\setIndexedTagName
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Definition: ApiResult.php:604
BlockLogFormatter\getMessageKey
getMessageKey()
Returns a key to be used for formatting the action sentence.
Definition: BlockLogFormatter.php:302
Message\rawParam
static rawParam( $raw)
Definition: Message.php:1035
wfIsInfinity
wfIsInfinity( $str)
Determine input string is represents as infinity.
Definition: GlobalFunctions.php:2832
BlockLogFormatter\getMessageParameters
getMessageParameters()
Formats parameters intented for action message from array of all parameters.
Definition: BlockLogFormatter.php:33
BlockLogFormatter\extractParameters
extractParameters()
Extracts the optional extra parameters for use in action messages.
Definition: BlockLogFormatter.php:116
LogEntry\getSubtype
getSubtype()
The log subtype.
LogFormatter
Implements the default log formatting.
Definition: LogFormatter.php:38
BlockLogFormatter\formatBlockFlags
static formatBlockFlags( $flags, Language $lang)
Convert a comma-delimited list of block log flags into a more readable (and translated) form.
Definition: BlockLogFormatter.php:183
LogFormatter\$linkRenderer
LinkRenderer null $linkRenderer
Definition: LogFormatter.php:103
BlockLogFormatter\getActionLinks
getActionLinks()
Returns extra links that comes after the action text, like "revert", etc.
Definition: BlockLogFormatter.php:146
LogFormatter\msg
msg( $key,... $params)
Shortcut for wfMessage which honors local context.
Definition: LogFormatter.php:761
BlockLogFormatter
This class formats block log entries.
Definition: BlockLogFormatter.php:32
LogFormatter\getLinkRenderer
getLinkRenderer()
Definition: LogFormatter.php:136
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:41
BlockLogFormatter\formatBlockFlag
static formatBlockFlag( $flag, Language $lang)
Translate a block log flag if possible.
Definition: BlockLogFormatter.php:206
LogFormatter\makePageLink
makePageLink(Title $title=null, $parameters=[], $html=null)
Helper to make a link to the page, taking the plaintext value in consideration.
Definition: LogFormatter.php:665
BlockLogFormatter\getParametersForApi
getParametersForApi()
Get the array of parameters, converted from legacy format if necessary.
Definition: BlockLogFormatter.php:230
$type
$type
Definition: testCompression.php:52