Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
89.17% |
107 / 120 |
|
57.14% |
4 / 7 |
CRAP | |
0.00% |
0 / 1 |
RightsLogFormatter | |
89.17% |
107 / 120 |
|
57.14% |
4 / 7 |
40.93 | |
0.00% |
0 / 1 |
makePageLink | |
42.11% |
8 / 19 |
|
0.00% |
0 / 1 |
9.85 | |||
getMessageKey | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getMessageParameters | |
95.83% |
23 / 24 |
|
0.00% |
0 / 1 |
11 | |||
formatRightsList | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
5 | |||
getParametersForApi | |
97.14% |
34 / 35 |
|
0.00% |
0 / 1 |
7 | |||
formatParametersForApi | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
5 | |||
makeGroupArray | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | /** |
3 | * Formatter for user rights log entries. |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | * http://www.gnu.org/copyleft/gpl.html |
19 | * |
20 | * @file |
21 | * @author Alexandre Emsenhuber |
22 | * @license GPL-2.0-or-later |
23 | * @since 1.22 |
24 | */ |
25 | |
26 | use MediaWiki\MainConfigNames; |
27 | use MediaWiki\Message\Message; |
28 | use MediaWiki\Title\Title; |
29 | use MediaWiki\WikiMap\WikiMap; |
30 | |
31 | /** |
32 | * This class formats rights log entries. |
33 | * |
34 | * @since 1.21 |
35 | */ |
36 | class RightsLogFormatter extends LogFormatter { |
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 = $this->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 | } |