3 * Formatter for block log entries.
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.
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.
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
21 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
25 use MediaWiki\MediaWikiServices
;
28 * This class formats block log entries.
32 class BlockLogFormatter
extends LogFormatter
{
33 protected function getMessageParameters() {
34 $params = parent
::getMessageParameters();
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
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
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';
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 = '‎' . 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() ) : '';
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];
79 $params[4] = $params[3];
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() ];
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' )
104 // Show unblock/change block link
105 $title = $this->entry
->getTarget();
107 $linkRenderer->makeKnownLink(
108 SpecialPage
::getTitleFor( 'Unblock', $title->getDBkey() ),
109 $this->msg( 'unblocklink' )->text()
111 $linkRenderer->makeKnownLink(
112 SpecialPage
::getTitleFor( 'Block', $title->getDBkey() ),
113 $this->msg( 'change-blocklink' )->text()
117 return $this->msg( 'parentheses' )->rawParams(
118 $this->context
->getLanguage()->pipeList( $links ) )->escaped();
122 * Convert a comma-delimited list of block log flags
123 * into a more readable (and translated) form
125 * @param string $flags Flags to format
126 * @param Language $lang
129 public static function formatBlockFlags( $flags, $lang ) {
130 $flags = trim( $flags );
131 if ( $flags === '' ) {
132 return ''; // nothing to do
134 $flags = explode( ',', $flags );
135 $flagsCount = count( $flags );
137 for ( $i = 0; $i < $flagsCount; $i++
) {
138 $flags[$i] = self
::formatBlockFlag( $flags[$i], $lang );
141 return wfMessage( 'parentheses' )->inLanguage( $lang )
142 ->rawParams( $lang->commaList( $flags ) )->escaped();
146 * Translate a block log flag if possible
148 * @param int $flag Flag to translate
149 * @param Language $lang Language object to use
152 public static function formatBlockFlag( $flag, $lang ) {
153 static $messages = [];
155 if ( !isset( $messages[$flag] ) ) {
156 $messages[$flag] = htmlspecialchars( $flag ); // Fallback
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 );
168 if ( $msg->exists() ) {
169 $messages[$flag] = $msg->escaped();
173 return $messages[$flag];
176 protected function getParametersForApi() {
177 $entry = $this->entry
;
178 $params = $entry->getParameters();
181 // While this looks wrong to be starting at 5 rather than 4, it's
182 // because getMessageParameters uses $4 for its own purposes.
185 '6::flags' => '6:array:flags',
187 foreach ( $map as $index => $key ) {
188 if ( isset( $params[$index] ) ) {
189 $params[$key] = $params[$index];
190 unset( $params[$index] );
194 $subtype = $entry->getSubtype();
195 if ( $subtype === 'block' ||
$subtype === 'reblock' ) {
196 // Defaults for old log entries missing some fields
198 '5::duration' => 'infinite',
199 '6:array:flags' => [],
202 if ( !is_array( $params['6:array:flags'] ) ) {
203 $params['6:array:flags'] = $params['6:array:flags'] === ''
205 : explode( ',', $params['6:array:flags'] );
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;
220 public function formatParametersForApi() {
221 $ret = parent
::formatParametersForApi();
222 if ( isset( $ret['flags'] ) ) {
223 ApiResult
::setIndexedTagName( $ret['flags'], 'f' );