Merge "Migrate block log to new log system"
[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 // Localize the duration, and add a tooltip
52 // in English to help visitors from other wikis.
53 // The lrm is needed to make sure that the number
54 // is shown on the correct side of the tooltip text.
55 $durationTooltip = '&lrm;' . htmlspecialchars( $params[4] );
56 $params[4] = Message::rawParam( "<span class='blockExpiry' title='$durationTooltip'>" .
57 $this->context->getLanguage()->translateBlockExpiry( $params[4] ) . '</span>' );
58 $params[5] = isset( $params[5] ) ?
59 self::formatBlockFlags( $params[5], $this->context->getLanguage() ) : '';
60 }
61
62 return $params;
63 }
64
65 public function getPreloadTitles() {
66 $title = $this->entry->getTarget();
67 // Preload user page for non-autoblocks
68 if ( substr( $title->getText(), 0, 1 ) !== '#' ) {
69 return array( $title->getTalkPage() );
70 }
71 return array();
72 }
73
74 public function getActionLinks() {
75 $subtype = $this->entry->getSubtype();
76 if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) // Action is hidden
77 || !( $subtype === 'block' || $subtype === 'reblock' )
78 || !$this->context->getUser()->isAllowed( 'block' )
79 ) {
80 return '';
81 }
82
83 // Show unblock/change block link
84 $title = $this->entry->getTarget();
85 $links = array(
86 Linker::linkKnown(
87 SpecialPage::getTitleFor( 'Unblock', $title->getDBkey() ),
88 $this->msg( 'unblocklink' )->escaped()
89 ),
90 Linker::linkKnown(
91 SpecialPage::getTitleFor( 'Block', $title->getDBkey() ),
92 $this->msg( 'change-blocklink' )->escaped()
93 )
94 );
95
96 return $this->msg( 'parentheses' )->rawParams(
97 $this->context->getLanguage()->pipeList( $links ) )->escaped();
98 }
99
100 /**
101 * Convert a comma-delimited list of block log flags
102 * into a more readable (and translated) form
103 *
104 * @param string $flags Flags to format
105 * @param Language $lang
106 * @return string
107 */
108 public static function formatBlockFlags( $flags, $lang ) {
109 $flags = trim( $flags );
110 if ( $flags === '' ) {
111 return ''; //nothing to do
112 }
113 $flags = explode( ',', $flags );
114 $flagsCount = count( $flags );
115
116 for ( $i = 0; $i < $flagsCount; $i++ ) {
117 $flags[$i] = self::formatBlockFlag( $flags[$i], $lang );
118 }
119
120 return wfMessage( 'parentheses' )->inLanguage( $lang )
121 ->rawParams( $lang->commaList( $flags ) )->escaped();
122 }
123
124 /**
125 * Translate a block log flag if possible
126 *
127 * @param int $flag Flag to translate
128 * @param Language $lang Language object to use
129 * @return string
130 */
131 public static function formatBlockFlag( $flag, $lang ) {
132 static $messages = array();
133
134 if ( !isset( $messages[$flag] ) ) {
135 $messages[$flag] = htmlspecialchars( $flag ); // Fallback
136
137 // For grepping. The following core messages can be used here:
138 // * block-log-flags-angry-autoblock
139 // * block-log-flags-anononly
140 // * block-log-flags-hiddenname
141 // * block-log-flags-noautoblock
142 // * block-log-flags-nocreate
143 // * block-log-flags-noemail
144 // * block-log-flags-nousertalk
145 $msg = wfMessage( 'block-log-flags-' . $flag )->inLanguage( $lang );
146
147 if ( $msg->exists() ) {
148 $messages[$flag] = $msg->escaped();
149 }
150 }
151
152 return $messages[$flag];
153 }
154 }