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