Debian Codegraph @

includes/logging/RightsLogFormatter.php

raw symbols SCIP
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 reset( $groups );
133 reset( $serializedUGMs );
134 while ( current( $groups ) ) {
135 $group = current( $groups );
136
137 if ( current( $serializedUGMs ) &&
138 isset( current( $serializedUGMs )['expiry'] ) &&
139 current( $serializedUGMs )['expiry']
140 ) {
141 // there is an expiry date; format the group and expiry into a friendly string
142 $expiry = current( $serializedUGMs )['expiry'];
143 $expiryFormatted = $uiLanguage->userTimeAndDate( $expiry, $uiUser );
144 $expiryFormattedD = $uiLanguage->userDate( $expiry, $uiUser );
145 $expiryFormattedT = $uiLanguage->userTime( $expiry, $uiUser );
146 $tempList[] = $this->msg( 'rightslogentry-temporary-group' )->params( $group,
147 $expiryFormatted, $expiryFormattedD, $expiryFormattedT )->parse();
148 } else {
149 // the right does not expire; just insert the group name
150 $permList[] = htmlspecialchars( $group );
151 }
152
153 next( $groups );
154 next( $serializedUGMs );
155 }
156
157 // place all temporary memberships first, to avoid the ambiguity of
158 // "administrator, bureaucrat and importer (temporary, until X time)"
159 return $uiLanguage->listToText( array_merge( $tempList, $permList ) );
160 }
161
162 protected function getParametersForApi() {
163 $entry = $this->entry;
164 $params = $entry->getParameters();
165
166 static $map = [
167 '4:array:oldgroups',
168 '5:array:newgroups',
169 '4::oldgroups' => '4:array:oldgroups',
170 '5::newgroups' => '5:array:newgroups',
171 ];
172 foreach ( $map as $index => $key ) {
173 if ( isset( $params[$index] ) ) {
174 $params[$key] = $params[$index];
175 unset( $params[$index] );
176 }
177 }
178
179 // Really old entries do not have log params, so form them from whatever info
180 // we have.
181 // Also walk through the parallel arrays of groups and metadata, combining each
182 // metadata array with the name of the group it pertains to
183 if ( isset( $params['4:array:oldgroups'] ) ) {
184 $params['4:array:oldgroups'] = $this->makeGroupArray( $params['4:array:oldgroups'] );
185
186 $oldmetadata =& $params['oldmetadata'];
187 // unset old metadata entry to ensure metadata goes at the end of the params array
188 unset( $params['oldmetadata'] );
189 $params['oldmetadata'] = array_map( static function ( $index ) use ( $params, $oldmetadata ) {
190 $result = [ 'group' => $params['4:array:oldgroups'][$index] ];
191 if ( isset( $oldmetadata[$index] ) ) {
192 $result += $oldmetadata[$index];
193 }
194 $result['expiry'] = ApiResult::formatExpiry( $result['expiry'] ?? null );
195
196 return $result;
197 }, array_keys( $params['4:array:oldgroups'] ) );
198 }
199
200 if ( isset( $params['5:array:newgroups'] ) ) {
201 $params['5:array:newgroups'] = $this->makeGroupArray( $params['5:array:newgroups'] );
202
203 $newmetadata =& $params['newmetadata'];
204 // unset old metadata entry to ensure metadata goes at the end of the params array
205 unset( $params['newmetadata'] );
206 $params['newmetadata'] = array_map( static function ( $index ) use ( $params, $newmetadata ) {
207 $result = [ 'group' => $params['5:array:newgroups'][$index] ];
208 if ( isset( $newmetadata[$index] ) ) {
209 $result += $newmetadata[$index];
210 }
211 $result['expiry'] = ApiResult::formatExpiry( $result['expiry'] ?? null );
212
213 return $result;
214 }, array_keys( $params['5:array:newgroups'] ) );
215 }
216
217 return $params;
218 }
219
220 public function formatParametersForApi() {
221 $ret = parent::formatParametersForApi();
222 if ( isset( $ret['oldgroups'] ) ) {
223 ApiResult::setIndexedTagName( $ret['oldgroups'], 'g' );
224 }
225 if ( isset( $ret['newgroups'] ) ) {
226 ApiResult::setIndexedTagName( $ret['newgroups'], 'g' );
227 }
228 if ( isset( $ret['oldmetadata'] ) ) {
229 ApiResult::setArrayType( $ret['oldmetadata'], 'array' );
230 ApiResult::setIndexedTagName( $ret['oldmetadata'], 'g' );
231 }
232 if ( isset( $ret['newmetadata'] ) ) {
233 ApiResult::setArrayType( $ret['newmetadata'], 'array' );
234 ApiResult::setIndexedTagName( $ret['newmetadata'], 'g' );
235 }
236 return $ret;
237 }
238
239 private function makeGroupArray( $group ) {
240 // Migrate old group params from string to array
241 if ( $group === '' ) {
242 $group = [];
243 } elseif ( is_string( $group ) ) {
244 $group = array_map( 'trim', explode( ',', $group ) );
245 }
246 return $group;
247 }
248 }