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 user page for non-autoblocks
132  if ( substr( $title->getText(), 0, 1 ) !== '#' && $title->canExist() ) {
133  return [ $title->getTalkPage() ];
134  }
135  return [];
136  }
137 
138  public function getActionLinks() {
139  $subtype = $this->entry->getSubtype();
140  $linkRenderer = $this->getLinkRenderer();
141  if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) // Action is hidden
142  || !( $subtype === 'block' || $subtype === 'reblock' )
143  || !MediaWikiServices::getInstance()
144  ->getPermissionManager()
145  ->userHasRight( $this->context->getUser(), 'block' )
146  ) {
147  return '';
148  }
149 
150  // Show unblock/change block link
151  $title = $this->entry->getTarget();
152  $links = [
153  $linkRenderer->makeKnownLink(
154  SpecialPage::getTitleFor( 'Unblock', $title->getDBkey() ),
155  $this->msg( 'unblocklink' )->text()
156  ),
157  $linkRenderer->makeKnownLink(
158  SpecialPage::getTitleFor( 'Block', $title->getDBkey() ),
159  $this->msg( 'change-blocklink' )->text()
160  )
161  ];
162 
163  return $this->msg( 'parentheses' )->rawParams(
164  $this->context->getLanguage()->pipeList( $links ) )->escaped();
165  }
166 
175  public static function formatBlockFlags( $flags, Language $lang ) {
176  $flags = trim( $flags );
177  if ( $flags === '' ) {
178  return ''; // nothing to do
179  }
180  $flags = explode( ',', $flags );
181  $flagsCount = count( $flags );
182 
183  for ( $i = 0; $i < $flagsCount; $i++ ) {
184  $flags[$i] = self::formatBlockFlag( $flags[$i], $lang );
185  }
186 
187  return wfMessage( 'parentheses' )->inLanguage( $lang )
188  ->rawParams( $lang->commaList( $flags ) )->escaped();
189  }
190 
198  public static function formatBlockFlag( $flag, Language $lang ) {
199  static $messages = [];
200 
201  if ( !isset( $messages[$flag] ) ) {
202  $messages[$flag] = htmlspecialchars( $flag ); // Fallback
203 
204  // For grepping. The following core messages can be used here:
205  // * block-log-flags-angry-autoblock
206  // * block-log-flags-anononly
207  // * block-log-flags-hiddenname
208  // * block-log-flags-noautoblock
209  // * block-log-flags-nocreate
210  // * block-log-flags-noemail
211  // * block-log-flags-nousertalk
212  $msg = wfMessage( 'block-log-flags-' . $flag )->inLanguage( $lang );
213 
214  if ( $msg->exists() ) {
215  $messages[$flag] = $msg->escaped();
216  }
217  }
218 
219  return $messages[$flag];
220  }
221 
222  protected function getParametersForApi() {
224  $params = $entry->getParameters();
225 
226  static $map = [
227  // While this looks wrong to be starting at 5 rather than 4, it's
228  // because getMessageParameters uses $4 for its own purposes.
229  '5::duration',
230  '6:array:flags',
231  '6::flags' => '6:array:flags',
232  ];
233 
234  foreach ( $map as $index => $key ) {
235  if ( isset( $params[$index] ) ) {
236  $params[$key] = $params[$index];
237  unset( $params[$index] );
238  }
239  }
240 
241  ksort( $params );
242 
243  $subtype = $entry->getSubtype();
244  if ( $subtype === 'block' || $subtype === 'reblock' ) {
245  // Defaults for old log entries missing some fields
246  $params += [
247  '5::duration' => 'infinite',
248  '6:array:flags' => [],
249  ];
250 
251  if ( !is_array( $params['6:array:flags'] ) ) {
252  $params['6:array:flags'] = $params['6:array:flags'] === ''
253  ? []
254  : explode( ',', $params['6:array:flags'] );
255  }
256 
257  if ( !wfIsInfinity( $params['5::duration'] ) ) {
258  $ts = wfTimestamp( TS_UNIX, $entry->getTimestamp() );
259  $expiry = strtotime( $params['5::duration'], $ts );
260  if ( $expiry !== false && $expiry > 0 ) {
261  $params[':timestamp:expiry'] = $expiry;
262  }
263  }
264  }
265 
266  return $params;
267  }
268 
273  public function formatParametersForApi() {
274  $ret = parent::formatParametersForApi();
275  if ( isset( $ret['flags'] ) ) {
276  ApiResult::setIndexedTagName( $ret['flags'], 'f' );
277  }
278 
279  if ( isset( $ret['restrictions']['pages'] ) ) {
280  $ret['restrictions']['pages'] = array_map( function ( $title ) {
281  return $this->formatParameterValueForApi( 'page', 'title-link', $title );
282  }, $ret['restrictions']['pages'] );
283  ApiResult::setIndexedTagName( $ret['restrictions']['pages'], 'p' );
284  }
285 
286  if ( isset( $ret['restrictions']['namespaces'] ) ) {
287  ApiResult::setIndexedTagName( $ret['restrictions']['namespaces'], 'ns' );
288  }
289 
290  return $ret;
291  }
292 
293  protected function getMessageKey() {
294  $type = $this->entry->getType();
295  $subtype = $this->entry->getSubtype();
296  $sitewide = $this->entry->getParameters()['sitewide'] ?? true;
297 
298  $key = "logentry-$type-$subtype";
299  if ( ( $subtype === 'block' || $subtype === 'reblock' ) && !$sitewide ) {
300  // $this->getMessageParameters is doing too much. We just need
301  // to check the presence of restrictions ($param[6]) and calling
302  // on parent gives us that
303  $params = parent::getMessageParameters();
304 
305  // message changes depending on whether there are editing restrictions or not
306  if ( isset( $params[6] ) ) {
307  $key = "logentry-partial$type-$subtype";
308  } else {
309  $key = "logentry-non-editing-$type-$subtype";
310  }
311  }
312 
313  return $key;
314  }
315 }
static rawParam( $raw)
Definition: Message.php:1027
makeUserLink(User $user, $toolFlags=0)
const NS_MAIN
Definition: Defines.php:60
LogEntryBase $entry
if(!isset( $args[0])) $lang
wfIsInfinity( $str)
Determine input string is represents as infinity.
getSubtype()
The log subtype.
LinkRenderer null $linkRenderer
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Definition: ApiResult.php:616
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
formatParametersForApi()
PhanTypeInvalidDimOffset
msg( $key,... $params)
Shortcut for wfMessage which honors local context.
const TOOL_LINKS_NOBLOCK
Flags for userToolLinks()
Definition: Linker.php:39
formatParameterValueForApi( $name, $type, $value)
Format a single parameter value for API output.
This class formats block log entries.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don&#39;t need a full Title object...
Definition: SpecialPage.php:83
Implements the default log formatting.
getParameters()
Get the extra parameters stored for this message.
static formatBlockFlags( $flags, Language $lang)
Convert a comma-delimited list of block log flags into a more readable (and translated) form...
makePageLink(Title $title=null, $parameters=[], $html=null)
Helper to make a link to the page, taking the plaintext value in consideration.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
const DELETED_ACTION
Definition: LogPage.php:34
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:515
static formatBlockFlag( $flag, Language $lang)
Translate a block log flag if possible.
commaList(array $list)
Take a list of strings and build a locale-friendly comma-separated list, using the local comma-separa...
Definition: Language.php:3356
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:319
getTimestamp()
Get the timestamp when the action was executed.