Merge "Add CollationFa"
[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 http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
22 * @since 1.25
23 */
24
25 use MediaWiki\MediaWikiServices;
26
27 /**
28 * This class formats block log entries.
29 *
30 * @since 1.25
31 */
32 class BlockLogFormatter extends LogFormatter {
33 protected function getMessageParameters() {
34 $params = parent::getMessageParameters();
35
36 $title = $this->entry->getTarget();
37 if ( substr( $title->getText(), 0, 1 ) === '#' ) {
38 // autoblock - no user link possible
39 $params[2] = $title->getText();
40 $params[3] = ''; // no user name for gender use
41 } else {
42 // Create a user link for the blocked
43 $username = $title->getText();
44 // @todo Store the user identifier in the parameters
45 // to make this faster for future log entries
46 $targetUser = User::newFromName( $username, false );
47 $params[2] = Message::rawParam( $this->makeUserLink( $targetUser, Linker::TOOL_LINKS_NOBLOCK ) );
48 $params[3] = $username; // plain user name for gender use
49 }
50
51 $subtype = $this->entry->getSubtype();
52 if ( $subtype === 'block' || $subtype === 'reblock' ) {
53 if ( !isset( $params[4] ) ) {
54 // Very old log entry without duration: means infinite
55 $params[4] = 'infinite';
56 }
57 // Localize the duration, and add a tooltip
58 // in English to help visitors from other wikis.
59 // The lrm is needed to make sure that the number
60 // is shown on the correct side of the tooltip text.
61 $durationTooltip = '&lrm;' . htmlspecialchars( $params[4] );
62 $params[4] = Message::rawParam( "<span class='blockExpiry' title='$durationTooltip'>" .
63 $this->context->getLanguage()->translateBlockExpiry( $params[4],
64 $this->context->getUser() ) . '</span>' );
65 $params[5] = isset( $params[5] ) ?
66 self::formatBlockFlags( $params[5], $this->context->getLanguage() ) : '';
67 }
68
69 return $params;
70 }
71
72 protected function extractParameters() {
73 $params = parent::extractParameters();
74 // Legacy log params returning the params in index 3 and 4, moved to 4 and 5
75 if ( $this->entry->isLegacy() && isset( $params[3] ) ) {
76 if ( isset( $params[4] ) ) {
77 $params[5] = $params[4];
78 }
79 $params[4] = $params[3];
80 $params[3] = '';
81 }
82 return $params;
83 }
84
85 public function getPreloadTitles() {
86 $title = $this->entry->getTarget();
87 // Preload user page for non-autoblocks
88 if ( substr( $title->getText(), 0, 1 ) !== '#' ) {
89 return [ $title->getTalkPage() ];
90 }
91 return [];
92 }
93
94 public function getActionLinks() {
95 $subtype = $this->entry->getSubtype();
96 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
97 if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) // Action is hidden
98 || !( $subtype === 'block' || $subtype === 'reblock' )
99 || !$this->context->getUser()->isAllowed( 'block' )
100 ) {
101 return '';
102 }
103
104 // Show unblock/change block link
105 $title = $this->entry->getTarget();
106 $links = [
107 $linkRenderer->makeKnownLink(
108 SpecialPage::getTitleFor( 'Unblock', $title->getDBkey() ),
109 $this->msg( 'unblocklink' )->text()
110 ),
111 $linkRenderer->makeKnownLink(
112 SpecialPage::getTitleFor( 'Block', $title->getDBkey() ),
113 $this->msg( 'change-blocklink' )->text()
114 )
115 ];
116
117 return $this->msg( 'parentheses' )->rawParams(
118 $this->context->getLanguage()->pipeList( $links ) )->escaped();
119 }
120
121 /**
122 * Convert a comma-delimited list of block log flags
123 * into a more readable (and translated) form
124 *
125 * @param string $flags Flags to format
126 * @param Language $lang
127 * @return string
128 */
129 public static function formatBlockFlags( $flags, $lang ) {
130 $flags = trim( $flags );
131 if ( $flags === '' ) {
132 return ''; // nothing to do
133 }
134 $flags = explode( ',', $flags );
135 $flagsCount = count( $flags );
136
137 for ( $i = 0; $i < $flagsCount; $i++ ) {
138 $flags[$i] = self::formatBlockFlag( $flags[$i], $lang );
139 }
140
141 return wfMessage( 'parentheses' )->inLanguage( $lang )
142 ->rawParams( $lang->commaList( $flags ) )->escaped();
143 }
144
145 /**
146 * Translate a block log flag if possible
147 *
148 * @param int $flag Flag to translate
149 * @param Language $lang Language object to use
150 * @return string
151 */
152 public static function formatBlockFlag( $flag, $lang ) {
153 static $messages = [];
154
155 if ( !isset( $messages[$flag] ) ) {
156 $messages[$flag] = htmlspecialchars( $flag ); // Fallback
157
158 // For grepping. The following core messages can be used here:
159 // * block-log-flags-angry-autoblock
160 // * block-log-flags-anononly
161 // * block-log-flags-hiddenname
162 // * block-log-flags-noautoblock
163 // * block-log-flags-nocreate
164 // * block-log-flags-noemail
165 // * block-log-flags-nousertalk
166 $msg = wfMessage( 'block-log-flags-' . $flag )->inLanguage( $lang );
167
168 if ( $msg->exists() ) {
169 $messages[$flag] = $msg->escaped();
170 }
171 }
172
173 return $messages[$flag];
174 }
175
176 protected function getParametersForApi() {
177 $entry = $this->entry;
178 $params = $entry->getParameters();
179
180 static $map = [
181 // While this looks wrong to be starting at 5 rather than 4, it's
182 // because getMessageParameters uses $4 for its own purposes.
183 '5::duration',
184 '6:array:flags',
185 '6::flags' => '6:array:flags',
186 ];
187 foreach ( $map as $index => $key ) {
188 if ( isset( $params[$index] ) ) {
189 $params[$key] = $params[$index];
190 unset( $params[$index] );
191 }
192 }
193
194 $subtype = $entry->getSubtype();
195 if ( $subtype === 'block' || $subtype === 'reblock' ) {
196 // Defaults for old log entries missing some fields
197 $params += [
198 '5::duration' => 'infinite',
199 '6:array:flags' => [],
200 ];
201
202 if ( !is_array( $params['6:array:flags'] ) ) {
203 $params['6:array:flags'] = $params['6:array:flags'] === ''
204 ? []
205 : explode( ',', $params['6:array:flags'] );
206 }
207
208 if ( !wfIsInfinity( $params['5::duration'] ) ) {
209 $ts = wfTimestamp( TS_UNIX, $entry->getTimestamp() );
210 $expiry = strtotime( $params['5::duration'], $ts );
211 if ( $expiry !== false && $expiry > 0 ) {
212 $params[':timestamp:expiry'] = $expiry;
213 }
214 }
215 }
216
217 return $params;
218 }
219
220 public function formatParametersForApi() {
221 $ret = parent::formatParametersForApi();
222 if ( isset( $ret['flags'] ) ) {
223 ApiResult::setIndexedTagName( $ret['flags'], 'f' );
224 }
225 return $ret;
226 }
227
228 }