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