Merge "Separate content parts of mw-usertoollinks from presentation"
[lhc/web/wiklou.git] / includes / logging / BlockLogFormatter.php
1 <?php
2 /**
3 * Formatter for block 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.25
23 */
24
25 /**
26 * This class formats block log entries.
27 *
28 * @since 1.25
29 */
30 class BlockLogFormatter extends LogFormatter {
31 protected function getMessageParameters() {
32 $params = parent::getMessageParameters();
33
34 $title = $this->entry->getTarget();
35 if ( substr( $title->getText(), 0, 1 ) === '#' ) {
36 // autoblock - no user link possible
37 $params[2] = $title->getText();
38 $params[3] = ''; // no user name for gender use
39 } else {
40 // Create a user link for the blocked
41 $username = $title->getText();
42 // @todo Store the user identifier in the parameters
43 // to make this faster for future log entries
44 $targetUser = User::newFromName( $username, false );
45 $params[2] = Message::rawParam( $this->makeUserLink( $targetUser, Linker::TOOL_LINKS_NOBLOCK ) );
46 $params[3] = $username; // plain user name for gender use
47 }
48
49 $subtype = $this->entry->getSubtype();
50 if ( $subtype === 'block' || $subtype === 'reblock' ) {
51 if ( !isset( $params[4] ) ) {
52 // Very old log entry without duration: means infinite
53 $params[4] = 'infinite';
54 }
55 // Localize the duration, and add a tooltip
56 // in English to help visitors from other wikis.
57 // The lrm is needed to make sure that the number
58 // is shown on the correct side of the tooltip text.
59 $durationTooltip = '&lrm;' . htmlspecialchars( $params[4] );
60 $blockExpiry = $this->context->getLanguage()->translateBlockExpiry(
61 $params[4],
62 $this->context->getUser(),
63 wfTimestamp( TS_UNIX, $this->entry->getTimestamp() )
64 );
65 if ( $this->plaintext ) {
66 $params[4] = Message::rawParam( $blockExpiry );
67 } else {
68 $params[4] = Message::rawParam(
69 "<span class=\"blockExpiry\" title=\"$durationTooltip\">" .
70 $blockExpiry .
71 '</span>'
72 );
73 }
74 $params[5] = isset( $params[5] ) ?
75 self::formatBlockFlags( $params[5], $this->context->getLanguage() ) : '';
76
77 // block restrictions
78 if ( isset( $params[6] ) ) {
79 $pages = $params[6]['pages'] ?? [];
80 $pages = array_map( function ( $page ){
81 return $this->makePageLink( Title::newFromText( ( $page ) ) );
82 }, $pages );
83
84 $params[6] = Message::rawParam( $this->context->getLanguage()->listToText( $pages ) );
85 $params[7] = count( $pages );
86 }
87 }
88
89 return $params;
90 }
91
92 protected function extractParameters() {
93 $params = parent::extractParameters();
94 // Legacy log params returning the params in index 3 and 4, moved to 4 and 5
95 if ( $this->entry->isLegacy() && isset( $params[3] ) ) {
96 if ( isset( $params[4] ) ) {
97 $params[5] = $params[4];
98 }
99 $params[4] = $params[3];
100 $params[3] = '';
101 }
102 return $params;
103 }
104
105 public function getPreloadTitles() {
106 $title = $this->entry->getTarget();
107 // Preload user page for non-autoblocks
108 if ( substr( $title->getText(), 0, 1 ) !== '#' ) {
109 return [ $title->getTalkPage() ];
110 }
111 return [];
112 }
113
114 public function getActionLinks() {
115 $subtype = $this->entry->getSubtype();
116 $linkRenderer = $this->getLinkRenderer();
117 if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) // Action is hidden
118 || !( $subtype === 'block' || $subtype === 'reblock' )
119 || !$this->context->getUser()->isAllowed( 'block' )
120 ) {
121 return '';
122 }
123
124 // Show unblock/change block link
125 $title = $this->entry->getTarget();
126 $links = [
127 $linkRenderer->makeKnownLink(
128 SpecialPage::getTitleFor( 'Unblock', $title->getDBkey() ),
129 $this->msg( 'unblocklink' )->text()
130 ),
131 $linkRenderer->makeKnownLink(
132 SpecialPage::getTitleFor( 'Block', $title->getDBkey() ),
133 $this->msg( 'change-blocklink' )->text()
134 )
135 ];
136
137 return $this->msg( 'parentheses' )->rawParams(
138 $this->context->getLanguage()->pipeList( $links ) )->escaped();
139 }
140
141 /**
142 * Convert a comma-delimited list of block log flags
143 * into a more readable (and translated) form
144 *
145 * @param string $flags Flags to format
146 * @param Language $lang
147 * @return string
148 */
149 public static function formatBlockFlags( $flags, Language $lang ) {
150 $flags = trim( $flags );
151 if ( $flags === '' ) {
152 return ''; // nothing to do
153 }
154 $flags = explode( ',', $flags );
155 $flagsCount = count( $flags );
156
157 for ( $i = 0; $i < $flagsCount; $i++ ) {
158 $flags[$i] = self::formatBlockFlag( $flags[$i], $lang );
159 }
160
161 return wfMessage( 'parentheses' )->inLanguage( $lang )
162 ->rawParams( $lang->commaList( $flags ) )->escaped();
163 }
164
165 /**
166 * Translate a block log flag if possible
167 *
168 * @param int $flag Flag to translate
169 * @param Language $lang Language object to use
170 * @return string
171 */
172 public static function formatBlockFlag( $flag, Language $lang ) {
173 static $messages = [];
174
175 if ( !isset( $messages[$flag] ) ) {
176 $messages[$flag] = htmlspecialchars( $flag ); // Fallback
177
178 // For grepping. The following core messages can be used here:
179 // * block-log-flags-angry-autoblock
180 // * block-log-flags-anononly
181 // * block-log-flags-hiddenname
182 // * block-log-flags-noautoblock
183 // * block-log-flags-nocreate
184 // * block-log-flags-noemail
185 // * block-log-flags-nousertalk
186 $msg = wfMessage( 'block-log-flags-' . $flag )->inLanguage( $lang );
187
188 if ( $msg->exists() ) {
189 $messages[$flag] = $msg->escaped();
190 }
191 }
192
193 return $messages[$flag];
194 }
195
196 protected function getParametersForApi() {
197 $entry = $this->entry;
198 $params = $entry->getParameters();
199
200 static $map = [
201 // While this looks wrong to be starting at 5 rather than 4, it's
202 // because getMessageParameters uses $4 for its own purposes.
203 '5::duration',
204 '6:array:flags',
205 '6::flags' => '6:array:flags',
206 ];
207
208 foreach ( $map as $index => $key ) {
209 if ( isset( $params[$index] ) ) {
210 $params[$key] = $params[$index];
211 unset( $params[$index] );
212 }
213 }
214
215 ksort( $params );
216
217 $subtype = $entry->getSubtype();
218 if ( $subtype === 'block' || $subtype === 'reblock' ) {
219 // Defaults for old log entries missing some fields
220 $params += [
221 '5::duration' => 'infinite',
222 '6:array:flags' => [],
223 ];
224
225 if ( !is_array( $params['6:array:flags'] ) ) {
226 $params['6:array:flags'] = $params['6:array:flags'] === ''
227 ? []
228 : explode( ',', $params['6:array:flags'] );
229 }
230
231 if ( !wfIsInfinity( $params['5::duration'] ) ) {
232 $ts = wfTimestamp( TS_UNIX, $entry->getTimestamp() );
233 $expiry = strtotime( $params['5::duration'], $ts );
234 if ( $expiry !== false && $expiry > 0 ) {
235 $params[':timestamp:expiry'] = $expiry;
236 }
237 }
238 }
239
240 return $params;
241 }
242
243 public function formatParametersForApi() {
244 $ret = parent::formatParametersForApi();
245 if ( isset( $ret['flags'] ) ) {
246 ApiResult::setIndexedTagName( $ret['flags'], 'f' );
247 }
248
249 if ( isset( $ret['restrictions']['pages'] ) ) {
250 $ret['restrictions']['pages'] = array_map( function ( $title ) {
251 return $this->formatParameterValueForApi( 'page', 'title-link', $title );
252 }, $ret['restrictions']['pages'] );
253 ApiResult::setIndexedTagName( $ret['restrictions']['pages'], 'p' );
254 }
255
256 return $ret;
257 }
258
259 protected function getMessageKey() {
260 $type = $this->entry->getType();
261 $subtype = $this->entry->getSubtype();
262 $sitewide = $this->entry->getParameters()['sitewide'] ?? true;
263
264 $key = "logentry-$type-$subtype";
265 if ( ( $subtype === 'block' || $subtype === 'reblock' ) && !$sitewide ) {
266 // $this->getMessageParameters is doing too much. We just need
267 // to check the presence of restrictions ($param[6]) and calling
268 // on parent gives us that
269 $params = parent::getMessageParameters();
270
271 // message changes depending on whether there are editing restrictions or not
272 if ( isset( $params[6] ) ) {
273 $key = "logentry-partial$type-$subtype";
274 } else {
275 $key = "logentry-non-editing-$type-$subtype";
276 }
277 }
278
279 return $key;
280 }
281 }