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