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 GPL-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 $blockExpiry = $this->context
->getLanguage()->translateBlockExpiry(
64 $this->context
->getUser(),
65 wfTimestamp( TS_UNIX
, $this->entry
->getTimestamp() )
67 if ( $this->plaintext
) {
68 $params[4] = Message
::rawParam( $blockExpiry );
70 $params[4] = Message
::rawParam(
71 "<span class=\"blockExpiry\" title=\"$durationTooltip\">" .
76 $params[5] = isset( $params[5] ) ?
77 self
::formatBlockFlags( $params[5], $this->context
->getLanguage() ) : '';
80 if ( isset( $params[6] ) ) {
81 $pages = $params[6]['pages'] ??
[];
82 $pages = array_map( function ( $page ) {
83 return $this->makePageLink( Title
::newFromText( $page ) );
86 $namespaces = $params[6]['namespaces'] ??
[];
87 $namespaces = array_map( function ( $ns ) {
88 $text = (int)$ns === NS_MAIN
89 ?
$this->msg( 'blanknamespace' )->text()
90 : $this->context
->getLanguage()->getFormattedNsText( $ns );
91 $params = [ 'namespace' => $ns ];
93 return $this->makePageLink( SpecialPage
::getTitleFor( 'Allpages' ), $params, $text );
98 $restrictions[] = $this->msg( 'logentry-partialblock-block-page' )
99 ->numParams( count( $pages ) )
100 ->rawParams( $this->context
->getLanguage()->listToText( $pages ) )->text();
104 $restrictions[] = $this->msg( 'logentry-partialblock-block-ns' )
105 ->numParams( count( $namespaces ) )
106 ->rawParams( $this->context
->getLanguage()->listToText( $namespaces ) )->text();
109 $params[6] = Message
::rawParam( $this->context
->getLanguage()->listToText( $restrictions ) );
116 protected function extractParameters() {
117 $params = parent
::extractParameters();
118 // Legacy log params returning the params in index 3 and 4, moved to 4 and 5
119 if ( $this->entry
->isLegacy() && isset( $params[3] ) ) {
120 if ( isset( $params[4] ) ) {
121 $params[5] = $params[4];
123 $params[4] = $params[3];
129 public function getPreloadTitles() {
130 $title = $this->entry
->getTarget();
131 // Preload user page for non-autoblocks
132 if ( substr( $title->getText(), 0, 1 ) !== '#' && $title->isValid() ) {
133 return [ $title->getTalkPage() ];
138 public function getActionLinks() {
139 $subtype = $this->entry
->getSubtype();
140 $linkRenderer = $this->getLinkRenderer();
141 if ( $this->entry
->isDeleted( LogPage
::DELETED_ACTION
) // Action is hidden
142 ||
!( $subtype === 'block' ||
$subtype === 'reblock' )
143 ||
!MediaWikiServices
::getInstance()
144 ->getPermissionManager()
145 ->userHasRight( $this->context
->getUser(), 'block' )
150 // Show unblock/change block link
151 $title = $this->entry
->getTarget();
153 $linkRenderer->makeKnownLink(
154 SpecialPage
::getTitleFor( 'Unblock', $title->getDBkey() ),
155 $this->msg( 'unblocklink' )->text()
157 $linkRenderer->makeKnownLink(
158 SpecialPage
::getTitleFor( 'Block', $title->getDBkey() ),
159 $this->msg( 'change-blocklink' )->text()
163 return $this->msg( 'parentheses' )->rawParams(
164 $this->context
->getLanguage()->pipeList( $links ) )->escaped();
168 * Convert a comma-delimited list of block log flags
169 * into a more readable (and translated) form
171 * @param string $flags Flags to format
172 * @param Language $lang
175 public static function formatBlockFlags( $flags, Language
$lang ) {
176 $flags = trim( $flags );
177 if ( $flags === '' ) {
178 return ''; // nothing to do
180 $flags = explode( ',', $flags );
181 $flagsCount = count( $flags );
183 for ( $i = 0; $i < $flagsCount; $i++
) {
184 $flags[$i] = self
::formatBlockFlag( $flags[$i], $lang );
187 return wfMessage( 'parentheses' )->inLanguage( $lang )
188 ->rawParams( $lang->commaList( $flags ) )->escaped();
192 * Translate a block log flag if possible
194 * @param int $flag Flag to translate
195 * @param Language $lang Language object to use
198 public static function formatBlockFlag( $flag, Language
$lang ) {
199 static $messages = [];
201 if ( !isset( $messages[$flag] ) ) {
202 $messages[$flag] = htmlspecialchars( $flag ); // Fallback
204 // For grepping. The following core messages can be used here:
205 // * block-log-flags-angry-autoblock
206 // * block-log-flags-anononly
207 // * block-log-flags-hiddenname
208 // * block-log-flags-noautoblock
209 // * block-log-flags-nocreate
210 // * block-log-flags-noemail
211 // * block-log-flags-nousertalk
212 $msg = wfMessage( 'block-log-flags-' . $flag )->inLanguage( $lang );
214 if ( $msg->exists() ) {
215 $messages[$flag] = $msg->escaped();
219 return $messages[$flag];
222 protected function getParametersForApi() {
223 $entry = $this->entry
;
224 $params = $entry->getParameters();
227 // While this looks wrong to be starting at 5 rather than 4, it's
228 // because getMessageParameters uses $4 for its own purposes.
231 '6::flags' => '6:array:flags',
234 foreach ( $map as $index => $key ) {
235 if ( isset( $params[$index] ) ) {
236 $params[$key] = $params[$index];
237 unset( $params[$index] );
243 $subtype = $entry->getSubtype();
244 if ( $subtype === 'block' ||
$subtype === 'reblock' ) {
245 // Defaults for old log entries missing some fields
247 '5::duration' => 'infinite',
248 '6:array:flags' => [],
251 if ( !is_array( $params['6:array:flags'] ) ) {
252 $params['6:array:flags'] = $params['6:array:flags'] === ''
254 : explode( ',', $params['6:array:flags'] );
257 if ( !wfIsInfinity( $params['5::duration'] ) ) {
258 $ts = wfTimestamp( TS_UNIX
, $entry->getTimestamp() );
259 $expiry = strtotime( $params['5::duration'], $ts );
260 if ( $expiry !== false && $expiry > 0 ) {
261 $params[':timestamp:expiry'] = $expiry;
271 * @suppress PhanTypeInvalidDimOffset
273 public function formatParametersForApi() {
274 $ret = parent
::formatParametersForApi();
275 if ( isset( $ret['flags'] ) ) {
276 ApiResult
::setIndexedTagName( $ret['flags'], 'f' );
279 if ( isset( $ret['restrictions']['pages'] ) ) {
280 $ret['restrictions']['pages'] = array_map( function ( $title ) {
281 return $this->formatParameterValueForApi( 'page', 'title-link', $title );
282 }, $ret['restrictions']['pages'] );
283 ApiResult
::setIndexedTagName( $ret['restrictions']['pages'], 'p' );
286 if ( isset( $ret['restrictions']['namespaces'] ) ) {
287 ApiResult
::setIndexedTagName( $ret['restrictions']['namespaces'], 'ns' );
293 protected function getMessageKey() {
294 $type = $this->entry
->getType();
295 $subtype = $this->entry
->getSubtype();
296 $sitewide = $this->entry
->getParameters()['sitewide'] ??
true;
298 $key = "logentry-$type-$subtype";
299 if ( ( $subtype === 'block' ||
$subtype === 'reblock' ) && !$sitewide ) {
300 // $this->getMessageParameters is doing too much. We just need
301 // to check the presence of restrictions ($param[6]) and calling
302 // on parent gives us that
303 $params = parent
::getMessageParameters();
305 // message changes depending on whether there are editing restrictions or not
306 if ( isset( $params[6] ) ) {
307 $key = "logentry-partial$type-$subtype";
309 $key = "logentry-non-editing-$type-$subtype";