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
21 * @author Alexandre Emsenhuber
22 * @license GPL-2.0-or-later
26 use MediaWiki\MediaWikiServices
;
29 * This class formats rights log entries.
33 class RightsLogFormatter
extends LogFormatter
{
34 protected function makePageLink( Title
$title = null, $parameters = [], $html = null ) {
35 global $wgUserrightsInterwikiDelimiter;
37 if ( !$this->plaintext
) {
38 $text = MediaWikiServices
::getInstance()->getContentLanguage()->
39 ucfirst( $title->getDBkey() );
40 $parts = explode( $wgUserrightsInterwikiDelimiter, $text, 2 );
42 if ( count( $parts ) === 2 ) {
43 $titleLink = WikiMap
::foreignUserLink(
47 strtr( $parts[0], '_', ' ' ) .
48 $wgUserrightsInterwikiDelimiter .
53 if ( $titleLink !== false ) {
59 return parent
::makePageLink( $title, $parameters, $title ?
$title->getText() : null );
62 protected function getMessageKey() {
63 $key = parent
::getMessageKey();
64 $params = $this->getMessageParameters();
65 if ( !isset( $params[3] ) && !isset( $params[4] ) ) {
66 // Messages: logentry-rights-rights-legacy
73 protected function getMessageParameters() {
74 $params = parent
::getMessageParameters();
76 // Really old entries that lack old/new groups
77 if ( !isset( $params[3] ) && !isset( $params[4] ) ) {
81 $oldGroups = $this->makeGroupArray( $params[3] );
82 $newGroups = $this->makeGroupArray( $params[4] );
84 $userName = $this->entry
->getTarget()->getText();
85 if ( !$this->plaintext
&& count( $oldGroups ) ) {
86 foreach ( $oldGroups as &$group ) {
87 $group = UserGroupMembership
::getGroupMemberName( $group, $userName );
90 if ( !$this->plaintext
&& count( $newGroups ) ) {
91 foreach ( $newGroups as &$group ) {
92 $group = UserGroupMembership
::getGroupMemberName( $group, $userName );
96 // fetch the metadata about each group membership
97 $allParams = $this->entry
->getParameters();
99 if ( count( $oldGroups ) ) {
100 $params[3] = [ 'raw' => $this->formatRightsList( $oldGroups,
101 $allParams['oldmetadata'] ??
[] ) ];
103 $params[3] = $this->msg( 'rightsnone' )->text();
105 if ( count( $newGroups ) ) {
106 // Array_values is used here because of T44211
107 // see use of array_unique in UserrightsPage::doSaveUserGroups on $newGroups.
108 $params[4] = [ 'raw' => $this->formatRightsList( array_values( $newGroups ),
109 $allParams['newmetadata'] ??
[] ) ];
111 $params[4] = $this->msg( 'rightsnone' )->text();
114 $params[5] = $userName;
119 protected function formatRightsList( $groups, $serializedUGMs = [] ) {
120 $uiLanguage = $this->context
->getLanguage();
121 $uiUser = $this->context
->getUser();
122 // separate arrays of temporary and permanent memberships
123 $tempList = $permList = [];
126 reset( $serializedUGMs );
127 while ( current( $groups ) ) {
128 $group = current( $groups );
130 if ( current( $serializedUGMs ) &&
131 isset( current( $serializedUGMs )['expiry'] ) &&
132 current( $serializedUGMs )['expiry']
134 // there is an expiry date; format the group and expiry into a friendly string
135 $expiry = current( $serializedUGMs )['expiry'];
136 $expiryFormatted = $uiLanguage->userTimeAndDate( $expiry, $uiUser );
137 $expiryFormattedD = $uiLanguage->userDate( $expiry, $uiUser );
138 $expiryFormattedT = $uiLanguage->userTime( $expiry, $uiUser );
139 $tempList[] = $this->msg( 'rightslogentry-temporary-group' )->params( $group,
140 $expiryFormatted, $expiryFormattedD, $expiryFormattedT )->parse();
142 // the right does not expire; just insert the group name
143 $permList[] = $group;
147 next( $serializedUGMs );
150 // place all temporary memberships first, to avoid the ambiguity of
151 // "adinistrator, bureaucrat and importer (temporary, until X time)"
152 return $uiLanguage->listToText( array_merge( $tempList, $permList ) );
155 protected function getParametersForApi() {
156 $entry = $this->entry
;
157 $params = $entry->getParameters();
162 '4::oldgroups' => '4:array:oldgroups',
163 '5::newgroups' => '5:array:newgroups',
165 foreach ( $map as $index => $key ) {
166 if ( isset( $params[$index] ) ) {
167 $params[$key] = $params[$index];
168 unset( $params[$index] );
172 // Really old entries do not have log params, so form them from whatever info
174 // Also walk through the parallel arrays of groups and metadata, combining each
175 // metadata array with the name of the group it pertains to
176 if ( isset( $params['4:array:oldgroups'] ) ) {
177 $params['4:array:oldgroups'] = $this->makeGroupArray( $params['4:array:oldgroups'] );
179 $oldmetadata =& $params['oldmetadata'];
180 // unset old metadata entry to ensure metadata goes at the end of the params array
181 unset( $params['oldmetadata'] );
182 $params['oldmetadata'] = array_map( function ( $index ) use ( $params, $oldmetadata ) {
183 $result = [ 'group' => $params['4:array:oldgroups'][$index] ];
184 if ( isset( $oldmetadata[$index] ) ) {
185 $result +
= $oldmetadata[$index];
187 $result['expiry'] = ApiResult
::formatExpiry( $result['expiry'] ??
null );
190 }, array_keys( $params['4:array:oldgroups'] ) );
193 if ( isset( $params['5:array:newgroups'] ) ) {
194 $params['5:array:newgroups'] = $this->makeGroupArray( $params['5:array:newgroups'] );
196 $newmetadata =& $params['newmetadata'];
197 // unset old metadata entry to ensure metadata goes at the end of the params array
198 unset( $params['newmetadata'] );
199 $params['newmetadata'] = array_map( function ( $index ) use ( $params, $newmetadata ) {
200 $result = [ 'group' => $params['5:array:newgroups'][$index] ];
201 if ( isset( $newmetadata[$index] ) ) {
202 $result +
= $newmetadata[$index];
204 $result['expiry'] = ApiResult
::formatExpiry( $result['expiry'] ??
null );
207 }, array_keys( $params['5:array:newgroups'] ) );
213 public function formatParametersForApi() {
214 $ret = parent
::formatParametersForApi();
215 if ( isset( $ret['oldgroups'] ) ) {
216 ApiResult
::setIndexedTagName( $ret['oldgroups'], 'g' );
218 if ( isset( $ret['newgroups'] ) ) {
219 ApiResult
::setIndexedTagName( $ret['newgroups'], 'g' );
221 if ( isset( $ret['oldmetadata'] ) ) {
222 ApiResult
::setArrayType( $ret['oldmetadata'], 'array' );
223 ApiResult
::setIndexedTagName( $ret['oldmetadata'], 'g' );
225 if ( isset( $ret['newmetadata'] ) ) {
226 ApiResult
::setArrayType( $ret['newmetadata'], 'array' );
227 ApiResult
::setIndexedTagName( $ret['newmetadata'], 'g' );
232 private function makeGroupArray( $group ) {
233 // Migrate old group params from string to array
234 if ( $group === '' ) {
236 } elseif ( is_string( $group ) ) {
237 $group = array_map( 'trim', explode( ',', $group ) );