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