Merge "Move disable of preference 'enotifminoredits'"
[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 /**
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 $params[4] = Message::rawParam( "<span class='blockExpiry' title='$durationTooltip'>" .
61 $this->context->getLanguage()->translateBlockExpiry( $params[4],
62 $this->context->getUser() ) . '</span>' );
63 $params[5] = isset( $params[5] ) ?
64 self::formatBlockFlags( $params[5], $this->context->getLanguage() ) : '';
65 }
66
67 return $params;
68 }
69
70 protected function extractParameters() {
71 $params = parent::extractParameters();
72 // Legacy log params returning the params in index 3 and 4, moved to 4 and 5
73 if ( $this->entry->isLegacy() && isset( $params[3] ) ) {
74 if ( isset( $params[4] ) ) {
75 $params[5] = $params[4];
76 }
77 $params[4] = $params[3];
78 $params[3] = '';
79 }
80 return $params;
81 }
82
83 public function getPreloadTitles() {
84 $title = $this->entry->getTarget();
85 // Preload user page for non-autoblocks
86 if ( substr( $title->getText(), 0, 1 ) !== '#' ) {
87 return [ $title->getTalkPage() ];
88 }
89 return [];
90 }
91
92 public function getActionLinks() {
93 $subtype = $this->entry->getSubtype();
94 if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) // Action is hidden
95 || !( $subtype === 'block' || $subtype === 'reblock' )
96 || !$this->context->getUser()->isAllowed( 'block' )
97 ) {
98 return '';
99 }
100
101 // Show unblock/change block link
102 $title = $this->entry->getTarget();
103 $links = [
104 Linker::linkKnown(
105 SpecialPage::getTitleFor( 'Unblock', $title->getDBkey() ),
106 $this->msg( 'unblocklink' )->escaped()
107 ),
108 Linker::linkKnown(
109 SpecialPage::getTitleFor( 'Block', $title->getDBkey() ),
110 $this->msg( 'change-blocklink' )->escaped()
111 )
112 ];
113
114 return $this->msg( 'parentheses' )->rawParams(
115 $this->context->getLanguage()->pipeList( $links ) )->escaped();
116 }
117
118 /**
119 * Convert a comma-delimited list of block log flags
120 * into a more readable (and translated) form
121 *
122 * @param string $flags Flags to format
123 * @param Language $lang
124 * @return string
125 */
126 public static function formatBlockFlags( $flags, $lang ) {
127 $flags = trim( $flags );
128 if ( $flags === '' ) {
129 return ''; // nothing to do
130 }
131 $flags = explode( ',', $flags );
132 $flagsCount = count( $flags );
133
134 for ( $i = 0; $i < $flagsCount; $i++ ) {
135 $flags[$i] = self::formatBlockFlag( $flags[$i], $lang );
136 }
137
138 return wfMessage( 'parentheses' )->inLanguage( $lang )
139 ->rawParams( $lang->commaList( $flags ) )->escaped();
140 }
141
142 /**
143 * Translate a block log flag if possible
144 *
145 * @param int $flag Flag to translate
146 * @param Language $lang Language object to use
147 * @return string
148 */
149 public static function formatBlockFlag( $flag, $lang ) {
150 static $messages = [];
151
152 if ( !isset( $messages[$flag] ) ) {
153 $messages[$flag] = htmlspecialchars( $flag ); // Fallback
154
155 // For grepping. The following core messages can be used here:
156 // * block-log-flags-angry-autoblock
157 // * block-log-flags-anononly
158 // * block-log-flags-hiddenname
159 // * block-log-flags-noautoblock
160 // * block-log-flags-nocreate
161 // * block-log-flags-noemail
162 // * block-log-flags-nousertalk
163 $msg = wfMessage( 'block-log-flags-' . $flag )->inLanguage( $lang );
164
165 if ( $msg->exists() ) {
166 $messages[$flag] = $msg->escaped();
167 }
168 }
169
170 return $messages[$flag];
171 }
172
173 protected function getParametersForApi() {
174 $entry = $this->entry;
175 $params = $entry->getParameters();
176
177 static $map = [
178 // While this looks wrong to be starting at 5 rather than 4, it's
179 // because getMessageParameters uses $4 for its own purposes.
180 '5::duration',
181 '6:array:flags',
182 '6::flags' => '6:array:flags',
183 ];
184 foreach ( $map as $index => $key ) {
185 if ( isset( $params[$index] ) ) {
186 $params[$key] = $params[$index];
187 unset( $params[$index] );
188 }
189 }
190
191 $subtype = $entry->getSubtype();
192 if ( $subtype === 'block' || $subtype === 'reblock' ) {
193 // Defaults for old log entries missing some fields
194 $params += [
195 '5::duration' => 'infinite',
196 '6:array:flags' => [],
197 ];
198
199 if ( !is_array( $params['6:array:flags'] ) ) {
200 $params['6:array:flags'] = $params['6:array:flags'] === ''
201 ? []
202 : explode( ',', $params['6:array:flags'] );
203 }
204
205 if ( !wfIsInfinity( $params['5::duration'] ) ) {
206 $ts = wfTimestamp( TS_UNIX, $entry->getTimestamp() );
207 $expiry = strtotime( $params['5::duration'], $ts );
208 if ( $expiry !== false && $expiry > 0 ) {
209 $params[':timestamp:expiry'] = $expiry;
210 }
211 }
212 }
213
214 return $params;
215 }
216
217 public function formatParametersForApi() {
218 $ret = parent::formatParametersForApi();
219 if ( isset( $ret['flags'] ) ) {
220 ApiResult::setIndexedTagName( $ret['flags'], 'f' );
221 }
222 return $ret;
223 }
224
225 }