MediaWiki REL1_37
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}
$wgUserrightsInterwikiDelimiter
Character used as a delimiter when testing for interwiki userrights (In Special:UserRights,...
Implements the default log formatting.
LogEntryBase $entry
msg( $key,... $params)
Shortcut for wfMessage which honors local context.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static rawParam( $raw)
Definition Message.php:1090
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 intented for action message from array of all parameters.
Represents a title within MediaWiki.
Definition Title.php:48
getParameters()
Get the extra parameters stored for this message.