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