Merge "Fix positioning of jQuery.tipsy tooltip arrows"
[mediawiki.git] / includes / logging / BlockLogFormatter.php
blob72fcc3a5df8da050435a7f9684d3328b93679db0
1 <?php
2 /**
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
20 * @file
21 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
22 * @since 1.25
25 /**
26 * This class formats block log entries.
28 * @since 1.25
30 class BlockLogFormatter extends LogFormatter {
31 protected function getMessageParameters() {
32 $params = parent::getMessageParameters();
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
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';
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] ) . '</span>' );
62 $params[5] = isset( $params[5] ) ?
63 self::formatBlockFlags( $params[5], $this->context->getLanguage() ) : '';
66 return $params;
69 protected function extractParameters() {
70 $params = parent::extractParameters();
71 // Legacy log params returning the params in index 3 and 4, moved to 4 and 5
72 if ( $this->entry->isLegacy() && isset( $params[3] ) ) {
73 if ( isset( $params[4] ) ) {
74 $params[5] = $params[4];
76 $params[4] = $params[3];
77 $params[3] = '';
79 return $params;
82 public function getPreloadTitles() {
83 $title = $this->entry->getTarget();
84 // Preload user page for non-autoblocks
85 if ( substr( $title->getText(), 0, 1 ) !== '#' ) {
86 return array( $title->getTalkPage() );
88 return array();
91 public function getActionLinks() {
92 $subtype = $this->entry->getSubtype();
93 if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) // Action is hidden
94 || !( $subtype === 'block' || $subtype === 'reblock' )
95 || !$this->context->getUser()->isAllowed( 'block' )
96 ) {
97 return '';
100 // Show unblock/change block link
101 $title = $this->entry->getTarget();
102 $links = array(
103 Linker::linkKnown(
104 SpecialPage::getTitleFor( 'Unblock', $title->getDBkey() ),
105 $this->msg( 'unblocklink' )->escaped()
107 Linker::linkKnown(
108 SpecialPage::getTitleFor( 'Block', $title->getDBkey() ),
109 $this->msg( 'change-blocklink' )->escaped()
113 return $this->msg( 'parentheses' )->rawParams(
114 $this->context->getLanguage()->pipeList( $links ) )->escaped();
118 * Convert a comma-delimited list of block log flags
119 * into a more readable (and translated) form
121 * @param string $flags Flags to format
122 * @param Language $lang
123 * @return string
125 public static function formatBlockFlags( $flags, $lang ) {
126 $flags = trim( $flags );
127 if ( $flags === '' ) {
128 return ''; // nothing to do
130 $flags = explode( ',', $flags );
131 $flagsCount = count( $flags );
133 for ( $i = 0; $i < $flagsCount; $i++ ) {
134 $flags[$i] = self::formatBlockFlag( $flags[$i], $lang );
137 return wfMessage( 'parentheses' )->inLanguage( $lang )
138 ->rawParams( $lang->commaList( $flags ) )->escaped();
142 * Translate a block log flag if possible
144 * @param int $flag Flag to translate
145 * @param Language $lang Language object to use
146 * @return string
148 public static function formatBlockFlag( $flag, $lang ) {
149 static $messages = array();
151 if ( !isset( $messages[$flag] ) ) {
152 $messages[$flag] = htmlspecialchars( $flag ); // Fallback
154 // For grepping. The following core messages can be used here:
155 // * block-log-flags-angry-autoblock
156 // * block-log-flags-anononly
157 // * block-log-flags-hiddenname
158 // * block-log-flags-noautoblock
159 // * block-log-flags-nocreate
160 // * block-log-flags-noemail
161 // * block-log-flags-nousertalk
162 $msg = wfMessage( 'block-log-flags-' . $flag )->inLanguage( $lang );
164 if ( $msg->exists() ) {
165 $messages[$flag] = $msg->escaped();
169 return $messages[$flag];
172 protected function getParametersForApi() {
173 $entry = $this->entry;
174 $params = $entry->getParameters();
176 static $map = array(
177 // While this looks wrong to be starting at 5 rather than 4, it's
178 // because getMessageParameters uses $4 for its own purposes.
179 '5::duration',
180 '6:array:flags',
181 '6::flags' => '6:array:flags',
183 foreach ( $map as $index => $key ) {
184 if ( isset( $params[$index] ) ) {
185 $params[$key] = $params[$index];
186 unset( $params[$index] );
190 $subtype = $entry->getSubtype();
191 if ( $subtype === 'block' || $subtype === 'reblock' ) {
192 // Defaults for old log entries missing some fields
193 $params += array(
194 '5::duration' => 'infinite',
195 '6:array:flags' => array(),
198 if ( !is_array( $params['6:array:flags'] ) ) {
199 $params['6:array:flags'] = $params['6:array:flags'] === ''
200 ? array()
201 : explode( ',', $params['6:array:flags'] );
204 if ( !wfIsInfinity( $params['5::duration'] ) ) {
205 $ts = wfTimestamp( TS_UNIX, $entry->getTimestamp() );
206 $expiry = strtotime( $params['5::duration'], $ts );
207 if ( $expiry !== false && $expiry > 0 ) {
208 $params[':timestamp:expiry'] = $expiry;
213 return $params;
216 public function formatParametersForApi() {
217 $ret = parent::formatParametersForApi();
218 if ( isset( $ret['flags'] ) ) {
219 ApiResult::setIndexedTagName( $ret['flags'], 'f' );
221 return $ret;