Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / logging / RightsLogFormatter.php
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 http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
23 * @since 1.22
24 */
25
26 /**
27 * This class formats rights log entries.
28 *
29 * @since 1.21
30 */
31 class RightsLogFormatter extends LogFormatter {
32 protected function makePageLink( Title $title = null, $parameters = [], $html = null ) {
33 global $wgContLang, $wgUserrightsInterwikiDelimiter;
34
35 if ( !$this->plaintext ) {
36 $text = $wgContLang->ucfirst( $title->getText() );
37 $parts = explode( $wgUserrightsInterwikiDelimiter, $text, 2 );
38
39 if ( count( $parts ) === 2 ) {
40 $titleLink = WikiMap::foreignUserLink( $parts[1], $parts[0],
41 htmlspecialchars( $title->getText() ) );
42
43 if ( $titleLink !== false ) {
44 return $titleLink;
45 }
46 }
47 }
48
49 return parent::makePageLink( $title, $parameters, $title ? $title->getText() : null );
50 }
51
52 protected function getMessageKey() {
53 $key = parent::getMessageKey();
54 $params = $this->getMessageParameters();
55 if ( !isset( $params[3] ) && !isset( $params[4] ) ) {
56 // Messages: logentry-rights-rights-legacy
57 $key .= '-legacy';
58 }
59
60 return $key;
61 }
62
63 protected function getMessageParameters() {
64 $params = parent::getMessageParameters();
65
66 // Really old entries
67 if ( !isset( $params[3] ) && !isset( $params[4] ) ) {
68 return $params;
69 }
70
71 $oldGroups = $this->makeGroupArray( $params[3] );
72 $newGroups = $this->makeGroupArray( $params[4] );
73
74 $userName = $this->entry->getTarget()->getText();
75 if ( !$this->plaintext && count( $oldGroups ) ) {
76 foreach ( $oldGroups as &$group ) {
77 $group = User::getGroupMember( $group, $userName );
78 }
79 }
80 if ( !$this->plaintext && count( $newGroups ) ) {
81 foreach ( $newGroups as &$group ) {
82 $group = User::getGroupMember( $group, $userName );
83 }
84 }
85
86 $lang = $this->context->getLanguage();
87 if ( count( $oldGroups ) ) {
88 $params[3] = $lang->listToText( $oldGroups );
89 } else {
90 $params[3] = $this->msg( 'rightsnone' )->text();
91 }
92 if ( count( $newGroups ) ) {
93 // Array_values is used here because of bug 42211
94 // see use of array_unique in UserrightsPage::doSaveUserGroups on $newGroups.
95 $params[4] = $lang->listToText( array_values( $newGroups ) );
96 } else {
97 $params[4] = $this->msg( 'rightsnone' )->text();
98 }
99
100 return $params;
101 }
102
103 protected function getParametersForApi() {
104 $entry = $this->entry;
105 $params = $entry->getParameters();
106
107 static $map = [
108 '4:array:oldgroups',
109 '5:array:newgroups',
110 '4::oldgroups' => '4:array:oldgroups',
111 '5::newgroups' => '5:array:newgroups',
112 ];
113 foreach ( $map as $index => $key ) {
114 if ( isset( $params[$index] ) ) {
115 $params[$key] = $params[$index];
116 unset( $params[$index] );
117 }
118 }
119
120 // Really old entries does not have log params
121 if ( isset( $params['4:array:oldgroups'] ) ) {
122 $params['4:array:oldgroups'] = $this->makeGroupArray( $params['4:array:oldgroups'] );
123 }
124 if ( isset( $params['5:array:newgroups'] ) ) {
125 $params['5:array:newgroups'] = $this->makeGroupArray( $params['5:array:newgroups'] );
126 }
127
128 return $params;
129 }
130
131 public function formatParametersForApi() {
132 $ret = parent::formatParametersForApi();
133 if ( isset( $ret['oldgroups'] ) ) {
134 ApiResult::setIndexedTagName( $ret['oldgroups'], 'g' );
135 }
136 if ( isset( $ret['newgroups'] ) ) {
137 ApiResult::setIndexedTagName( $ret['newgroups'], 'g' );
138 }
139 return $ret;
140 }
141
142 private function makeGroupArray( $group ) {
143 // Migrate old group params from string to array
144 if ( $group === '' ) {
145 $group = [];
146 } elseif ( is_string( $group ) ) {
147 $group = array_map( 'trim', explode( ',', $group ) );
148 }
149 return $group;
150 }
151 }