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