Use IContextSource instead of RequestContext inside type hints and instanceof checks...
[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 /// @var LogEntry
60 protected $entry;
61
62 /// Whether to output user tool links
63 protected $linkFlood = false;
64
65 /**
66 * Set to true if we are constructing a message text that is going to
67 * be included in page history or send to IRC feed. Links are replaced
68 * with plaintext or with [[pagename]] kind of syntax, that is parsed
69 * by page histories and IRC feeds.
70 * @var boolean
71 */
72 protected $plaintext = false;
73
74 protected function __construct( LogEntry $entry ) {
75 $this->entry = $entry;
76 $this->context = RequestContext::getMain();
77 }
78
79 /**
80 * Replace the default context
81 * @param $context IContextSource
82 */
83 public function setContext( IContextSource $context ) {
84 $this->context = $context;
85 }
86
87 /**
88 * If set to true, will produce user tool links after
89 * the user name. This should be replaced with generic
90 * CSS/JS solution.
91 * @param $value boolean
92 */
93 public function setShowUserToolLinks( $value ) {
94 $this->linkFlood = $value;
95 }
96
97 /**
98 * Ugly hack to produce plaintext version of the message.
99 * Usually you also want to set extraneous request context
100 * to avoid formatting for any particular user.
101 * @see getActionText()
102 * @return string text
103 */
104 public function getPlainActionText() {
105 $this->plaintext = true;
106 $text = $this->getActionText();
107 $this->plaintext = false;
108 return $text;
109 }
110
111 /**
112 * Gets the log action, including username.
113 * @return string HTML
114 */
115 public function getActionText() {
116 $element = $this->getActionMessage();
117 if ( $element instanceof Message ) {
118 $element = $this->plaintext ? $element->text() : $element->escaped();
119 }
120
121 if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) ) {
122 $performer = $this->getPerformerElement() . $this->msg( 'word-separator' )->text();
123 $element = $performer . self::getRestrictedElement( 'rev-deleted-event' );
124 }
125
126 return $element;
127 }
128
129 /**
130 * Returns a sentence describing the log action. Usually
131 * a Message object is returned, but old style log types
132 * and entries might return pre-escaped html string.
133 * @return Message|pre-escaped html
134 */
135 protected function getActionMessage() {
136 $message = $this->msg( $this->getMessageKey() );
137 $message->params( $this->getMessageParameters() );
138 return $message;
139 }
140
141 /**
142 * Returns a key to be used for formatting the action sentence.
143 * Default is logentry-TYPE-SUBTYPE for modern logs. Legacy log
144 * types will use custom keys, and subclasses can also alter the
145 * key depending on the entry itself.
146 * @return string message key
147 */
148 protected function getMessageKey() {
149 $type = $this->entry->getType();
150 $subtype = $this->entry->getSubtype();
151 $key = "logentry-$type-$subtype";
152 return $key;
153 }
154
155 /**
156 * Extract parameters intented for action message from
157 * array of all parameters. The are three hardcoded
158 * parameters (array zero-indexed, this list not):
159 * - 1: user name with premade link
160 * - 2: usable for gender magic function
161 * - 3: target page with premade link
162 * @return array
163 */
164 protected function getMessageParameters() {
165 if ( isset( $this->parsedParameters ) ) {
166 return $this->parsedParameters;
167 }
168
169 $entry = $this->entry;
170
171 $params = array();
172 $params[0] = Message::rawParam( $this->getPerformerElement() );
173 $params[1] = $entry->getPerformer()->getName();
174 $params[2] = Message::rawParam( $this->makePageLink( $entry->getTarget() ) );
175
176 if ( $entry->isLegacy() ) {
177 foreach ( $entry->getParameters() as $index => $value ) {
178 $params[$index + 3] = $value;
179 }
180 }
181
182 // Filter out parameters which are not in format #:foo
183 foreach ( $entry->getParameters() as $key => $value ) {
184 if ( strpos( $key, ':' ) === false ) continue;
185 list( $index, $type, $name ) = explode( ':', $key, 3 );
186 $params[$index - 1] = $value;
187 }
188
189 /* Message class doesn't like non consecutive numbering.
190 * Fill in missing indexes with empty strings to avoid
191 * incorrect renumbering.
192 */
193 $max = max( array_keys( $params ) );
194 for ( $i = 4; $i < $max; $i++ ) {
195 if ( !isset( $params[$i] ) ) {
196 $params[$i] = '';
197 }
198 }
199
200 return $this->parsedParameters = $params;
201 }
202
203 /**
204 * Helper to make a link to the page, taking the plaintext
205 * value in consideration.
206 * @param $title Title the page
207 * @param $parameters array query parameters
208 * @return String
209 */
210 protected function makePageLink( Title $title = null, $parameters = array() ) {
211 if ( !$this->plaintext ) {
212 $link = Linker::link( $title, null, array(), $parameters );
213 } else {
214 if ( !$title instanceof Title ) {
215 throw new MWException( "Expected title, got null" );
216 }
217 $link = '[[' . $title->getPrefixedText() . ']]';
218 }
219 return $link;
220 }
221
222 /**
223 * Provides the name of the user who performed the log action.
224 * Used as part of log action message or standalone, depending
225 * which parts of the log entry has been hidden.
226 */
227 public function getPerformerElement() {
228 $performer = $this->entry->getPerformer();
229
230 if ( $this->plaintext ) {
231 $element = $performer->getName();
232 } else {
233 $element = Linker::userLink(
234 $performer->getId(),
235 $performer->getName()
236 );
237
238 if ( $this->linkFlood ) {
239 $element .= Linker::userToolLinks(
240 $performer->getId(),
241 $performer->getName(),
242 true, // Red if no edits
243 0, // Flags
244 $performer->getEditCount()
245 );
246 }
247 }
248
249 if ( $this->entry->isDeleted( LogPage::DELETED_USER ) ) {
250 $element = self::getRestrictedElement( 'rev-deleted-user' );
251 }
252
253 return $element;
254 }
255
256 /**
257 * Gets the luser provided comment
258 * @return string HTML
259 */
260 public function getComment() {
261 $comment = Linker::commentBlock( $this->entry->getComment() );
262 // No hard coded spaces thanx
263 $element = ltrim( $comment );
264
265 if ( $this->entry->isDeleted( LogPage::DELETED_COMMENT ) ) {
266 $element = self::getRestrictedElement( 'rev-deleted-comment' );
267 }
268
269 return $element;
270 }
271
272 /**
273 * Helper method for displaying restricted element.
274 * @param $message string
275 * @return string HTML
276 */
277 protected function getRestrictedElement( $message ) {
278 if ( $this->plaintext ) {
279 return $this->msg( $message )->text();
280 }
281
282 $content = $this->msg( $message )->escaped();
283 $attribs = array( 'class' => 'history-deleted' );
284 return Html::rawElement( 'span', $attribs, $content );
285 }
286
287 /**
288 * Shortcut for wfMessage which honors local context.
289 * @todo Would it be better to require replacing the global context instead?
290 * @param $key string
291 * @return Message
292 */
293 protected function msg( $key ) {
294 return wfMessage( $key )
295 ->inLanguage( $this->context->getLang() )
296 ->title( $this->context->getTitle() );
297 }
298
299 }
300
301 /**
302 * This class formats all log entries for log types
303 * which have not been converted to the new system.
304 * This is not about old log entries which store
305 * parameters in a different format - the new
306 * LogFormatter classes have code to support formatting
307 * those too.
308 * @since 1.19
309 */
310 class LegacyLogFormatter extends LogFormatter {
311 protected function getActionMessage() {
312 $entry = $this->entry;
313 $action = LogPage::actionText(
314 $entry->getType(),
315 $entry->getSubtype(),
316 $entry->getTarget(),
317 $this->context->getSkin(),
318 (array)$entry->getParameters(),
319 true
320 );
321
322 $performer = $this->getPerformerElement();
323 return $performer . $this->msg( 'word-separator' )->text() . $action;
324 }
325
326 }
327
328 /**
329 * This class formats move log entries.
330 * @since 1.19
331 */
332 class MoveLogFormatter extends LogFormatter {
333 protected function getMessageKey() {
334 $key = parent::getMessageKey();
335 $params = $this->getMessageParameters();
336 if ( isset( $params[4] ) && $params[4] === '1' ) {
337 $key .= '-noredirect';
338 }
339 return $key;
340 }
341
342 protected function getMessageParameters() {
343 $params = parent::getMessageParameters();
344 $oldname = $this->makePageLink( $this->entry->getTarget(), array( 'redirect' => 'no' ) );
345 $newname = $this->makePageLink( Title::newFromText( $params[3] ) );
346 $params[2] = Message::rawParam( $oldname );
347 $params[3] = Message::rawParam( $newname );
348 return $params;
349 }
350 }
351
352 /**
353 * This class formats delete log entries.
354 * @since 1.19
355 */
356 class DeleteLogFormatter extends LogFormatter {
357 protected function getMessageKey() {
358 $key = parent::getMessageKey();
359 if ( in_array( $this->entry->getSubtype(), array( 'event', 'revision' ) ) ) {
360 if ( count( $this->getMessageParameters() ) < 5 ) {
361 return "$key-legacy";
362 }
363 }
364 return $key;
365 }
366
367 protected function getMessageParameters() {
368 if ( isset( $this->parsedParametersDeleteLog ) ) {
369 return $this->parsedParametersDeleteLog;
370 }
371
372 $params = parent::getMessageParameters();
373 $subtype = $this->entry->getSubtype();
374 if ( in_array( $subtype, array( 'event', 'revision' ) ) ) {
375 if (
376 ($subtype === 'event' && count( $params ) === 6 ) ||
377 ($subtype === 'revision' && $params[3] === 'revision' )
378 ) {
379 $paramStart = $subtype === 'revision' ? 4 : 3;
380
381 $old = $this->parseBitField( $params[$paramStart+1] );
382 $new = $this->parseBitField( $params[$paramStart+2] );
383 list( $hid, $unhid, $extra ) = RevisionDeleter::getChanges( $new, $old );
384 $changes = array();
385 foreach ( $hid as $v ) {
386 $changes[] = $this->msg( "$v-hid" )->plain();
387 }
388 foreach ( $unhid as $v ) {
389 $changes[] = $this->msg( "$v-unhid" )->plain();
390 }
391 foreach ( $extra as $v ) {
392 $changes[] = $this->msg( $v )->plain();
393 }
394 $changeText = $this->context->getLang()->listToText( $changes );
395
396
397 $newParams = array_slice( $params, 0, 3 );
398 $newParams[3] = $changeText;
399 $count = count( explode( ',', $params[$paramStart] ) );
400 $newParams[4] = $this->context->getLang()->formatNum( $count );
401 return $this->parsedParametersDeleteLog = $newParams;
402 } else {
403 return $this->parsedParametersDeleteLog = array_slice( $params, 0, 3 );
404 }
405 }
406
407 return $this->parsedParametersDeleteLog = $params;
408 }
409
410 protected function parseBitField( $string ) {
411 // Input is like ofield=2134 or just the number
412 if ( strpos( $string, 'field=' ) === 1 ) {
413 list( , $field ) = explode( '=', $string );
414 return (int) $field;
415 } else {
416 return (int) $string;
417 }
418 }
419 }