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