Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
77.57% |
83 / 107 |
|
37.50% |
3 / 8 |
CRAP | |
0.00% |
0 / 1 |
ProtectLogFormatter | |
77.57% |
83 / 107 |
|
37.50% |
3 / 8 |
52.45 | |
0.00% |
0 / 1 |
getPreloadTitles | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
getMessageKey | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getMessageParameters | |
93.33% |
14 / 15 |
|
0.00% |
0 / 1 |
7.01 | |||
getActionLinks | |
67.86% |
19 / 28 |
|
0.00% |
0 / 1 |
7.20 | |||
getParametersForApi | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
8 | |||
formatParametersForApi | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
createProtectDescription | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
4.01 | |||
formatExpiry | |
20.00% |
2 / 10 |
|
0.00% |
0 / 1 |
4.05 |
1 | <?php |
2 | /** |
3 | * Formatter for protect 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 | * @license GPL-2.0-or-later |
22 | * @since 1.26 |
23 | */ |
24 | |
25 | use MediaWiki\Title\Title; |
26 | |
27 | /** |
28 | * This class formats protect log entries. |
29 | * |
30 | * @since 1.26 |
31 | */ |
32 | class ProtectLogFormatter extends LogFormatter { |
33 | public function getPreloadTitles() { |
34 | $subtype = $this->entry->getSubtype(); |
35 | if ( $subtype === 'move_prot' ) { |
36 | $params = $this->extractParameters(); |
37 | return [ Title::newFromText( $params[3] ) ]; |
38 | } |
39 | return []; |
40 | } |
41 | |
42 | protected function getMessageKey() { |
43 | $key = parent::getMessageKey(); |
44 | $params = $this->extractParameters(); |
45 | if ( isset( $params[4] ) && $params[4] ) { |
46 | // Messages: logentry-protect-protect-cascade, logentry-protect-modify-cascade |
47 | $key .= '-cascade'; |
48 | } |
49 | |
50 | return $key; |
51 | } |
52 | |
53 | protected function getMessageParameters() { |
54 | $params = parent::getMessageParameters(); |
55 | |
56 | $subtype = $this->entry->getSubtype(); |
57 | if ( $subtype === 'protect' || $subtype === 'modify' ) { |
58 | $rawParams = $this->entry->getParameters(); |
59 | if ( isset( $rawParams['details'] ) ) { |
60 | $params[3] = $this->createProtectDescription( $rawParams['details'] ); |
61 | } elseif ( isset( $params[3] ) ) { |
62 | // Old way of Restrictions and expiries |
63 | $params[3] = $this->context->getLanguage()->getDirMark() . $params[3]; |
64 | } else { |
65 | // Very old way (nothing set) |
66 | $params[3] = ''; |
67 | } |
68 | // Cascading flag |
69 | if ( isset( $params[4] ) ) { |
70 | // handled in getMessageKey |
71 | unset( $params[4] ); |
72 | } |
73 | } elseif ( $subtype === 'move_prot' ) { |
74 | $oldname = $this->makePageLink( Title::newFromText( $params[3] ), [ 'redirect' => 'no' ] ); |
75 | $params[3] = Message::rawParam( $oldname ); |
76 | } |
77 | |
78 | return $params; |
79 | } |
80 | |
81 | public function getActionLinks() { |
82 | $linkRenderer = $this->getLinkRenderer(); |
83 | $subtype = $this->entry->getSubtype(); |
84 | if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) // Action is hidden |
85 | || $subtype === 'move_prot' // the move log entry has the right action link |
86 | ) { |
87 | return ''; |
88 | } |
89 | |
90 | // Show history link for pages that exist otherwise show nothing |
91 | $title = $this->entry->getTarget(); |
92 | $links = []; |
93 | if ( $title->exists() ) { |
94 | $links[] = $linkRenderer->makeLink( $title, |
95 | $this->msg( 'hist' )->text(), |
96 | [], |
97 | [ |
98 | 'action' => 'history', |
99 | 'offset' => $this->entry->getTimestamp(), |
100 | ] |
101 | ); |
102 | } |
103 | |
104 | // Show change protection link |
105 | if ( $this->context->getAuthority()->isAllowed( 'protect' ) ) { |
106 | $links[] = $linkRenderer->makeKnownLink( |
107 | $title, |
108 | $this->msg( 'protect_change' )->text(), |
109 | [], |
110 | [ 'action' => 'protect' ] |
111 | ); |
112 | } |
113 | |
114 | if ( !$links ) { |
115 | return ''; |
116 | } else { |
117 | return $this->msg( 'parentheses' )->rawParams( |
118 | $this->context->getLanguage()->pipeList( $links ) |
119 | )->escaped(); |
120 | } |
121 | } |
122 | |
123 | protected function getParametersForApi() { |
124 | $entry = $this->entry; |
125 | $subtype = $this->entry->getSubtype(); |
126 | $params = $entry->getParameters(); |
127 | |
128 | $map = []; |
129 | if ( $subtype === 'protect' || $subtype === 'modify' ) { |
130 | $map = [ |
131 | '4::description', |
132 | '5:bool:cascade', |
133 | 'details' => ':array:details', |
134 | ]; |
135 | } elseif ( $subtype === 'move_prot' ) { |
136 | $map = [ |
137 | '4:title:oldtitle', |
138 | '4::oldtitle' => '4:title:oldtitle', |
139 | ]; |
140 | } |
141 | foreach ( $map as $index => $key ) { |
142 | if ( isset( $params[$index] ) ) { |
143 | $params[$key] = $params[$index]; |
144 | unset( $params[$index] ); |
145 | } |
146 | } |
147 | |
148 | // Change string to explicit boolean |
149 | if ( isset( $params['5:bool:cascade'] ) && is_string( $params['5:bool:cascade'] ) ) { |
150 | $params['5:bool:cascade'] = $params['5:bool:cascade'] === 'cascade'; |
151 | } |
152 | |
153 | return $params; |
154 | } |
155 | |
156 | public function formatParametersForApi() { |
157 | $ret = parent::formatParametersForApi(); |
158 | if ( isset( $ret['details'] ) && is_array( $ret['details'] ) ) { |
159 | $contLang = $this->getContentLanguage(); |
160 | foreach ( $ret['details'] as &$detail ) { |
161 | if ( isset( $detail['expiry'] ) ) { |
162 | $detail['expiry'] = $contLang-> |
163 | formatExpiry( $detail['expiry'], TS_ISO_8601, 'infinite' ); |
164 | } |
165 | } |
166 | } |
167 | |
168 | return $ret; |
169 | } |
170 | |
171 | /** |
172 | * Create the protect description to show in the log formatter |
173 | * |
174 | * @param array[] $details |
175 | * @return string |
176 | */ |
177 | public function createProtectDescription( array $details ) { |
178 | $protectDescription = ''; |
179 | |
180 | foreach ( $details as $param ) { |
181 | $expiryText = $this->formatExpiry( $param['expiry'] ); |
182 | |
183 | // Messages: restriction-edit, restriction-move, restriction-create, |
184 | // restriction-upload |
185 | $action = $this->context->msg( 'restriction-' . $param['type'] )->escaped(); |
186 | |
187 | $protectionLevel = $param['level']; |
188 | // Messages: protect-level-autoconfirmed, protect-level-sysop |
189 | $message = $this->context->msg( 'protect-level-' . $protectionLevel ); |
190 | if ( $message->isDisabled() ) { |
191 | // Require "$1" permission |
192 | $restrictions = $this->context->msg( "protect-fallback", $protectionLevel )->parse(); |
193 | } else { |
194 | $restrictions = $message->escaped(); |
195 | } |
196 | |
197 | if ( $protectDescription !== '' ) { |
198 | $protectDescription .= $this->context->msg( 'word-separator' )->escaped(); |
199 | } |
200 | |
201 | $protectDescription .= $this->context->msg( 'protect-summary-desc' ) |
202 | ->params( $action, $restrictions, $expiryText )->escaped(); |
203 | } |
204 | |
205 | return $protectDescription; |
206 | } |
207 | |
208 | private function formatExpiry( $expiry ) { |
209 | if ( wfIsInfinity( $expiry ) ) { |
210 | return $this->context->msg( 'protect-expiry-indefinite' )->text(); |
211 | } |
212 | $lang = $this->context->getLanguage(); |
213 | $user = $this->context->getUser(); |
214 | return $this->context->msg( |
215 | 'protect-expiring-local', |
216 | $lang->userTimeAndDate( $expiry, $user ), |
217 | $lang->userDate( $expiry, $user ), |
218 | $lang->userTime( $expiry, $user ) |
219 | )->text(); |
220 | } |
221 | |
222 | } |