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