Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / logging / RightsLogFormatter.php
blob2206ce680bdbc71eaa2e811bd88f1dad163879f4
1 <?php
2 /**
3 * Formatter for user rights log entries.
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.
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.
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
20 * @file
21 * @author Alexandre Emsenhuber
22 * @license GPL-2.0-or-later
23 * @since 1.22
26 use MediaWiki\Api\ApiResult;
27 use MediaWiki\MainConfigNames;
28 use MediaWiki\Message\Message;
29 use MediaWiki\Title\Title;
30 use MediaWiki\WikiMap\WikiMap;
32 /**
33 * This class formats rights log entries.
35 * @since 1.21
37 class RightsLogFormatter extends LogFormatter {
38 protected function makePageLink( ?Title $title = null, $parameters = [], $html = null ) {
39 $userrightsInterwikiDelimiter = $this->context->getConfig()
40 ->get( MainConfigNames::UserrightsInterwikiDelimiter );
42 if ( !$this->plaintext ) {
43 $text = $this->getContentLanguage()->
44 ucfirst( $title->getDBkey() );
45 $parts = explode( $userrightsInterwikiDelimiter, $text, 2 );
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]
59 if ( $titleLink !== false ) {
60 return $titleLink;
65 return parent::makePageLink( $title, $parameters, $title ? $title->getText() : null );
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';
76 return $key;
79 protected function getMessageParameters() {
80 $params = parent::getMessageParameters();
82 // Really old entries that lack old/new groups
83 if ( !isset( $params[3] ) && !isset( $params[4] ) ) {
84 return $params;
87 $oldGroups = $this->makeGroupArray( $params[3] );
88 $newGroups = $this->makeGroupArray( $params[4] );
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 );
97 if ( !$this->plaintext && count( $newGroups ) ) {
98 foreach ( $newGroups as &$group ) {
99 $group = $lang->getGroupMemberName( $group, $userName );
103 // fetch the metadata about each group membership
104 $allParams = $this->entry->getParameters();
106 if ( count( $oldGroups ) ) {
107 $params[3] = Message::rawParam( $this->formatRightsList( $oldGroups,
108 $allParams['oldmetadata'] ?? [] ) );
109 } else {
110 $params[3] = $this->msg( 'rightsnone' )->text();
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();
121 $params[5] = $userName;
123 return $params;
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 = [];
132 foreach (
133 array_map( null, $groups, $serializedUGMs )
134 as [ $group, $serializedUGM ]
136 if ( $serializedUGM &&
137 isset( $serializedUGM['expiry'] ) &&
138 $serializedUGM['expiry']
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 );
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 ) );
158 protected function getParametersForApi() {
159 $entry = $this->entry;
160 $params = $entry->getParameters();
162 static $map = [
163 '4:array:oldgroups',
164 '5:array:newgroups',
165 '4::oldgroups' => '4:array:oldgroups',
166 '5::newgroups' => '5:array:newgroups',
168 foreach ( $map as $index => $key ) {
169 if ( isset( $params[$index] ) ) {
170 $params[$key] = $params[$index];
171 unset( $params[$index] );
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'] );
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];
190 $result['expiry'] = ApiResult::formatExpiry( $result['expiry'] ?? null );
192 return $result;
193 }, array_keys( $params['4:array:oldgroups'] ) );
196 if ( isset( $params['5:array:newgroups'] ) ) {
197 $params['5:array:newgroups'] = $this->makeGroupArray( $params['5:array:newgroups'] );
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];
207 $result['expiry'] = ApiResult::formatExpiry( $result['expiry'] ?? null );
209 return $result;
210 }, array_keys( $params['5:array:newgroups'] ) );
213 return $params;
216 public function formatParametersForApi() {
217 $ret = parent::formatParametersForApi();
218 if ( isset( $ret['oldgroups'] ) ) {
219 ApiResult::setIndexedTagName( $ret['oldgroups'], 'g' );
221 if ( isset( $ret['newgroups'] ) ) {
222 ApiResult::setIndexedTagName( $ret['newgroups'], 'g' );
224 if ( isset( $ret['oldmetadata'] ) ) {
225 ApiResult::setArrayType( $ret['oldmetadata'], 'array' );
226 ApiResult::setIndexedTagName( $ret['oldmetadata'], 'g' );
228 if ( isset( $ret['newmetadata'] ) ) {
229 ApiResult::setArrayType( $ret['newmetadata'], 'array' );
230 ApiResult::setIndexedTagName( $ret['newmetadata'], 'g' );
232 return $ret;
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 ) );
242 return $group;