Fix whitespace
[lhc/web/wiklou.git] / includes / logging / LogFormatter.php
1 <?php
2 /**
3 * Contains classes for formatting log entries
4 *
5 * @file
6 * @author Niklas Laxström
7 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
8 * @since 1.19
9 */
10
11 /**
12 * Implements the default log formatting.
13 * Can be overridden by subclassing and setting
14 * $wgLogActionsHandlers['type/subtype'] = 'class'; or
15 * $wgLogActionsHandlers['type/*'] = 'class';
16 * @since 1.19
17 */
18 class LogFormatter {
19
20 // Static->
21
22 /**
23 * Constructs a new formatter suitable for given entry.
24 * @param $entry LogEntry
25 * @return LogFormatter
26 */
27 public static function newFromEntry( LogEntry $entry ) {
28 global $wgLogActionsHandlers;
29 $fulltype = $entry->getFullType();
30 $wildcard = $entry->getType() . '/*';
31 $handler = '';
32
33 if ( isset( $wgLogActionsHandlers[$fulltype] ) ) {
34 $handler = $wgLogActionsHandlers[$fulltype];
35 } elseif ( isset( $wgLogActionsHandlers[$wildcard] ) ) {
36 $handler = $wgLogActionsHandlers[$wildcard];
37 }
38
39 if ( $handler !== '' && class_exists( $handler ) ) {
40 return new $handler( $entry );
41 }
42
43 return new LegacyLogFormatter( $entry );
44 }
45
46 /**
47 * Handy shortcut for constructing a formatter directly from
48 * database row.
49 * @param $row
50 * @see DatabaseLogEntry::getSelectQueryData
51 * @return LogFormatter
52 */
53 public static function newFromRow( $row ) {
54 return self::newFromEntry( DatabaseLogEntry::newFromRow( $row ) );
55 }
56
57 // Nonstatic->
58
59 /**
60 * @var LogEntry
61 */
62 protected $entry;
63
64 /// Whether to output user tool links
65 protected $linkFlood = false;
66
67 /**
68 * Set to true if we are constructing a message text that is going to
69 * be included in page history or send to IRC feed. Links are replaced
70 * with plaintext or with [[pagename]] kind of syntax, that is parsed
71 * by page histories and IRC feeds.
72 * @var boolean
73 */
74 protected $plaintext = false;
75
76 protected function __construct( LogEntry $entry ) {
77 $this->entry = $entry;
78 $this->context = RequestContext::getMain();
79 }
80
81 /**
82 * Replace the default context
83 * @param $context RequestContext
84 */
85 public function setContext( RequestContext $context ) {
86 $this->context = $context;
87 }
88
89 /**
90 * If set to true, will produce user tool links after
91 * the user name. This should be replaced with generic
92 * CSS/JS solution.
93 * @param $value boolean
94 */
95 public function setShowUserToolLinks( $value ) {
96 $this->linkFlood = $value;
97 }
98
99 /**
100 * Ugly hack to produce plaintext version of the message.
101 * Usually you also want to set extraneous request context
102 * to avoid formatting for any particular user.
103 * @see getActionText()
104 * @return string text
105 */
106 public function getPlainActionText() {
107 $this->plaintext = true;
108 $text = $this->getActionText();
109 $this->plaintext = false;
110 return $text;
111 }
112
113 /**
114 * Gets the log action, including username.
115 * @return string HTML
116 */
117 public function getActionText() {
118 $element = $this->getActionMessage();
119 if ( $element instanceof Message ) {
120 $element = $this->plaintext ? $element->text() : $element->escaped();
121 }
122
123 if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) ) {
124 $performer = $this->getPerformerElement() . $this->msg( 'word-separator' )->text();
125 $element = $performer . self::getRestrictedElement( 'rev-deleted-event' );
126 }
127
128 return $element;
129 }
130
131 /**
132 * Returns a sentence describing the log action. Usually
133 * a Message object is returned, but old style log types
134 * and entries might return pre-escaped html string.
135 * @return Message|pre-escaped html
136 */
137 protected function getActionMessage() {
138 $message = $this->msg( $this->getMessageKey() );
139 $message->params( $this->getMessageParameters() );
140 return $message;
141 }
142
143 /**
144 * Returns a key to be used for formatting the action sentence.
145 * Default is logentry-TYPE-SUBTYPE for modern logs. Legacy log
146 * types will use custom keys, and subclasses can also alter the
147 * key depending on the entry itself.
148 * @return string message key
149 */
150 protected function getMessageKey() {
151 $type = $this->entry->getType();
152 $subtype = $this->entry->getSubtype();
153 $key = "logentry-$type-$subtype";
154 return $key;
155 }
156
157 /**
158 * Extract parameters intented for action message from
159 * array of all parameters. The are three hardcoded
160 * parameters (array zero-indexed, this list not):
161 * - 1: user name with premade link
162 * - 2: usable for gender magic function
163 * - 3: target page with premade link
164 * @return array
165 */
166 protected function getMessageParameters() {
167 $entry = $this->entry;
168
169 $params = array();
170 $params[0] = Message::rawParam( $this->getPerformerElement() );
171 $params[1] = $entry->getPerformer()->getName();
172 $params[2] = Message::rawParam( $this->makePageLink( $entry->getTarget() ) );
173
174 if ( $entry->isLegacy() ) {
175 foreach ( $entry->getParameters() as $index => $value ) {
176 $params[$index + 3] = $value;
177 }
178 }
179
180 // Filter out parameters which are not in format #:foo
181 foreach ( $entry->getParameters() as $key => $value ) {
182 if ( strpos( $key, ':' ) === false ) continue;
183 list( $index, $type, $name ) = explode( ':', $key, 3 );
184 $params[$index - 1] = $value;
185 }
186
187 /* Message class doesn't like non consecutive numbering.
188 * Fill in missing indexes with empty strings to avoid
189 * incorrect renumbering.
190 */
191 $max = max( array_keys( $params ) );
192 for ( $i = 4; $i < $max; $i++ ) {
193 if ( !isset( $params[$i] ) ) {
194 $params[$i] = '';
195 }
196 }
197
198 return $params;
199 }
200
201 /**
202 * Helper to make a link to the page, taking the plaintext
203 * value in consideration.
204 * @param $title Title the page
205 * @param $parameters array query parameters
206 * @return String
207 */
208 protected function makePageLink( Title $title, $parameters = array() ) {
209 if ( !$this->plaintext ) {
210 $link = Linker::link( $title, null, array(), $parameters );
211 } else {
212 $link = '[[' . $title->getPrefixedText() . ']]';
213 }
214 return $link;
215 }
216
217 /**
218 * Provides the name of the user who performed the log action.
219 * Used as part of log action message or standalone, depending
220 * which parts of the log entry has been hidden.
221 */
222 public function getPerformerElement() {
223 $performer = $this->entry->getPerformer();
224
225 if ( $this->plaintext ) {
226 $element = $performer->getName();
227 } else {
228 $element = Linker::userLink(
229 $performer->getId(),
230 $performer->getName()
231 );
232
233 if ( $this->linkFlood ) {
234 $element .= Linker::userToolLinks(
235 $performer->getId(),
236 $performer->getName(),
237 true, // Red if no edits
238 0, // Flags
239 $performer->getEditCount()
240 );
241 }
242 }
243
244 if ( $this->entry->isDeleted( LogPage::DELETED_USER ) ) {
245 $element = self::getRestrictedElement( 'rev-deleted-user' );
246 }
247
248 return $element;
249 }
250
251 /**
252 * Gets the luser provided comment
253 * @return string HTML
254 */
255 public function getComment() {
256 $lang = $this->context->getLang();
257 $element = $lang->getDirMark() . Linker::commentBlock( $this->entry->getComment() );
258
259 if ( $this->entry->isDeleted( LogPage::DELETED_COMMENT ) ) {
260 $element = self::getRestrictedElement( 'rev-deleted-comment' );
261 }
262
263 return $element;
264 }
265
266 /**
267 * Helper method for displaying restricted element.
268 * @param $message string
269 * @return string HTML
270 */
271 protected function getRestrictedElement( $message ) {
272 if ( $this->plaintext ) {
273 return $this->msg( $message )->text();
274 }
275
276 $content = $this->msg( $message )->escaped();
277 $attribs = array( 'class' => 'history-deleted' );
278 return Html::rawElement( 'span', $attribs, $content );
279 }
280
281 /**
282 * Shortcut for wfMessage which honors local context.
283 * @todo Would it be better to require replacing the global context instead?
284 * @param $key string
285 * @return Message
286 */
287 protected function msg( $key ) {
288 return wfMessage( $key )
289 ->inLanguage( $this->context->getLang() )
290 ->title( $this->context->getTitle() );
291 }
292
293 }
294
295 /**
296 * This class formats all log entries for log types
297 * which have not been converted to the new system.
298 * This is not about old log entries which store
299 * parameters in a different format - the new
300 * LogFormatter classes have code to support formatting
301 * those too.
302 * @since 1.19
303 */
304 class LegacyLogFormatter extends LogFormatter {
305 protected function getActionMessage() {
306 $entry = $this->entry;
307 $action = LogPage::actionText(
308 $entry->getType(),
309 $entry->getSubtype(),
310 $entry->getTarget(),
311 $this->context->getSkin(),
312 (array)$entry->getParameters(),
313 true
314 );
315
316 $performer = $this->getPerformerElement();
317 return $performer . $this->msg( 'word-separator' )->text() . $action;
318 }
319
320 }
321
322 /**
323 * This class formats Block log entries.
324 * @since 1.19
325 */
326 class BlockLogFormatter extends LogFormatter {
327 protected function getMessageKey() {
328 $key = parent::getMessageKey();
329 $params = $this->getMessageParameters();
330 if ( isset( $params[4] ) && $params[4] === '1' ) {
331 $key .= '-noredirect';
332 }
333 return $key;
334 }
335
336 protected function getMessageParameters() {
337 $params = parent::getMessageParameters();
338 $oldname = $this->makePageLink( $this->entry->getTarget(), array( 'redirect' => 'no' ) );
339 $newname = $this->makePageLink( Title::newFromText( $params[3] ) );
340 $params[2] = Message::rawParam( $oldname );
341 $params[3] = Message::rawParam( $newname );
342 return $params;
343 }
344 }