MediaWiki  master
RightsLogFormatter.php
Go to the documentation of this file.
1 <?php
28 
35  protected function makePageLink( Title $title = null, $parameters = [], $html = null ) {
36  $userrightsInterwikiDelimiter = $this->context->getConfig()
37  ->get( MainConfigNames::UserrightsInterwikiDelimiter );
38 
39  if ( !$this->plaintext ) {
40  $text = MediaWikiServices::getInstance()->getContentLanguage()->
41  ucfirst( $title->getDBkey() );
42  $parts = explode( $userrightsInterwikiDelimiter, $text, 2 );
43 
44  if ( count( $parts ) === 2 ) {
45  // @phan-suppress-next-line SecurityCheck-DoubleEscaped
46  $titleLink = WikiMap::foreignUserLink(
47  $parts[1],
48  $parts[0],
49  htmlspecialchars(
50  strtr( $parts[0], '_', ' ' ) .
51  $userrightsInterwikiDelimiter .
52  $parts[1]
53  )
54  );
55 
56  if ( $titleLink !== false ) {
57  return $titleLink;
58  }
59  }
60  }
61 
62  return parent::makePageLink( $title, $parameters, $title ? $title->getText() : null );
63  }
64 
65  protected function getMessageKey() {
66  $key = parent::getMessageKey();
67  $params = $this->getMessageParameters();
68  if ( !isset( $params[3] ) && !isset( $params[4] ) ) {
69  // Messages: logentry-rights-rights-legacy
70  $key .= '-legacy';
71  }
72 
73  return $key;
74  }
75 
76  protected function getMessageParameters() {
77  $params = parent::getMessageParameters();
78 
79  // Really old entries that lack old/new groups
80  if ( !isset( $params[3] ) && !isset( $params[4] ) ) {
81  return $params;
82  }
83 
84  $oldGroups = $this->makeGroupArray( $params[3] );
85  $newGroups = $this->makeGroupArray( $params[4] );
86 
87  $userName = $this->entry->getTarget()->getText();
88  $lang = $this->context->getLanguage();
89  if ( !$this->plaintext && count( $oldGroups ) ) {
90  foreach ( $oldGroups as &$group ) {
91  $group = $lang->getGroupMemberName( $group, $userName );
92  }
93  }
94  if ( !$this->plaintext && count( $newGroups ) ) {
95  foreach ( $newGroups as &$group ) {
96  $group = $lang->getGroupMemberName( $group, $userName );
97  }
98  }
99 
100  // fetch the metadata about each group membership
101  $allParams = $this->entry->getParameters();
102 
103  if ( count( $oldGroups ) ) {
104  $params[3] = Message::rawParam( $this->formatRightsList( $oldGroups,
105  $allParams['oldmetadata'] ?? [] ) );
106  } else {
107  $params[3] = $this->msg( 'rightsnone' )->text();
108  }
109  if ( count( $newGroups ) ) {
110  // Array_values is used here because of T44211
111  // see use of array_unique in UserrightsPage::doSaveUserGroups on $newGroups.
112  $params[4] = Message::rawParam( $this->formatRightsList( array_values( $newGroups ),
113  $allParams['newmetadata'] ?? [] ) );
114  } else {
115  $params[4] = $this->msg( 'rightsnone' )->text();
116  }
117 
118  $params[5] = $userName;
119 
120  return $params;
121  }
122 
123  protected function formatRightsList( $groups, $serializedUGMs = [] ) {
124  $uiLanguage = $this->context->getLanguage();
125  $uiUser = $this->context->getUser();
126  // separate arrays of temporary and permanent memberships
127  $tempList = $permList = [];
128 
129  reset( $groups );
130  reset( $serializedUGMs );
131  while ( current( $groups ) ) {
132  $group = current( $groups );
133 
134  if ( current( $serializedUGMs ) &&
135  isset( current( $serializedUGMs )['expiry'] ) &&
136  current( $serializedUGMs )['expiry']
137  ) {
138  // there is an expiry date; format the group and expiry into a friendly string
139  $expiry = current( $serializedUGMs )['expiry'];
140  $expiryFormatted = $uiLanguage->userTimeAndDate( $expiry, $uiUser );
141  $expiryFormattedD = $uiLanguage->userDate( $expiry, $uiUser );
142  $expiryFormattedT = $uiLanguage->userTime( $expiry, $uiUser );
143  $tempList[] = $this->msg( 'rightslogentry-temporary-group' )->params( $group,
144  $expiryFormatted, $expiryFormattedD, $expiryFormattedT )->parse();
145  } else {
146  // the right does not expire; just insert the group name
147  $permList[] = $group;
148  }
149 
150  next( $groups );
151  next( $serializedUGMs );
152  }
153 
154  // place all temporary memberships first, to avoid the ambiguity of
155  // "administrator, bureaucrat and importer (temporary, until X time)"
156  return $uiLanguage->listToText( array_merge( $tempList, $permList ) );
157  }
158 
159  protected function getParametersForApi() {
161  $params = $entry->getParameters();
162 
163  static $map = [
164  '4:array:oldgroups',
165  '5:array:newgroups',
166  '4::oldgroups' => '4:array:oldgroups',
167  '5::newgroups' => '5:array:newgroups',
168  ];
169  foreach ( $map as $index => $key ) {
170  if ( isset( $params[$index] ) ) {
171  $params[$key] = $params[$index];
172  unset( $params[$index] );
173  }
174  }
175 
176  // Really old entries do not have log params, so form them from whatever info
177  // we have.
178  // Also walk through the parallel arrays of groups and metadata, combining each
179  // metadata array with the name of the group it pertains to
180  if ( isset( $params['4:array:oldgroups'] ) ) {
181  $params['4:array:oldgroups'] = $this->makeGroupArray( $params['4:array:oldgroups'] );
182 
183  $oldmetadata =& $params['oldmetadata'];
184  // unset old metadata entry to ensure metadata goes at the end of the params array
185  unset( $params['oldmetadata'] );
186  $params['oldmetadata'] = array_map( static function ( $index ) use ( $params, $oldmetadata ) {
187  $result = [ 'group' => $params['4:array:oldgroups'][$index] ];
188  if ( isset( $oldmetadata[$index] ) ) {
189  $result += $oldmetadata[$index];
190  }
191  $result['expiry'] = ApiResult::formatExpiry( $result['expiry'] ?? null );
192 
193  return $result;
194  }, array_keys( $params['4:array:oldgroups'] ) );
195  }
196 
197  if ( isset( $params['5:array:newgroups'] ) ) {
198  $params['5:array:newgroups'] = $this->makeGroupArray( $params['5:array:newgroups'] );
199 
200  $newmetadata =& $params['newmetadata'];
201  // unset old metadata entry to ensure metadata goes at the end of the params array
202  unset( $params['newmetadata'] );
203  $params['newmetadata'] = array_map( static function ( $index ) use ( $params, $newmetadata ) {
204  $result = [ 'group' => $params['5:array:newgroups'][$index] ];
205  if ( isset( $newmetadata[$index] ) ) {
206  $result += $newmetadata[$index];
207  }
208  $result['expiry'] = ApiResult::formatExpiry( $result['expiry'] ?? null );
209 
210  return $result;
211  }, array_keys( $params['5:array:newgroups'] ) );
212  }
213 
214  return $params;
215  }
216 
217  public function formatParametersForApi() {
218  $ret = parent::formatParametersForApi();
219  if ( isset( $ret['oldgroups'] ) ) {
220  ApiResult::setIndexedTagName( $ret['oldgroups'], 'g' );
221  }
222  if ( isset( $ret['newgroups'] ) ) {
223  ApiResult::setIndexedTagName( $ret['newgroups'], 'g' );
224  }
225  if ( isset( $ret['oldmetadata'] ) ) {
226  ApiResult::setArrayType( $ret['oldmetadata'], 'array' );
227  ApiResult::setIndexedTagName( $ret['oldmetadata'], 'g' );
228  }
229  if ( isset( $ret['newmetadata'] ) ) {
230  ApiResult::setArrayType( $ret['newmetadata'], 'array' );
231  ApiResult::setIndexedTagName( $ret['newmetadata'], 'g' );
232  }
233  return $ret;
234  }
235 
236  private function makeGroupArray( $group ) {
237  // Migrate old group params from string to array
238  if ( $group === '' ) {
239  $group = [];
240  } elseif ( is_string( $group ) ) {
241  $group = array_map( 'trim', explode( ',', $group ) );
242  }
243  return $group;
244  }
245 }
static setArrayType(array &$arr, $type, $kvpKeyName=null)
Set the array data type.
Definition: ApiResult.php:716
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Definition: ApiResult.php:604
static formatExpiry( $expiry, $infinity='infinity')
Format an expiry timestamp for API output.
Definition: ApiResult.php:1199
Implements the default log formatting.
LogEntryBase $entry
msg( $key,... $params)
Shortcut for wfMessage which honors local context.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
static rawParam( $raw)
Definition: Message.php:1133
This class formats rights log entries.
getMessageKey()
Returns a key to be used for formatting the action sentence.
formatRightsList( $groups, $serializedUGMs=[])
getParametersForApi()
Get the array of parameters, converted from legacy format if necessary.
makePageLink(Title $title=null, $parameters=[], $html=null)
Helper to make a link to the page, taking the plaintext value in consideration.
formatParametersForApi()
Format parameters for API output.
getMessageParameters()
Formats parameters intended for action message from array of all parameters.
Represents a title within MediaWiki.
Definition: Title.php:52
static foreignUserLink( $wikiID, $user, $text=null)
Convenience to get a link to a user page on a foreign wiki.
Definition: WikiMap.php:137
getParameters()
Get the extra parameters stored for this message.
if(!isset( $args[0])) $lang