Move remaining LoadBalancer classes to Rdbms
[mediawiki.git] / includes / logging / LogFormatter.php
blobb5af7836ca531fabc1e821f92171c6561544bfa4
1 <?php
2 /**
3 * Contains classes for formatting 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 * @author Niklas Laxström
22 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
23 * @since 1.19
26 /**
27 * Implements the default log formatting.
29 * Can be overridden by subclassing and setting:
31 * $wgLogActionsHandlers['type/subtype'] = 'class'; or
32 * $wgLogActionsHandlers['type/*'] = 'class';
34 * @since 1.19
36 class LogFormatter {
37 // Audience options for viewing usernames, comments, and actions
38 const FOR_PUBLIC = 1;
39 const FOR_THIS_USER = 2;
41 // Static->
43 /**
44 * Constructs a new formatter suitable for given entry.
45 * @param LogEntry $entry
46 * @return LogFormatter
48 public static function newFromEntry( LogEntry $entry ) {
49 global $wgLogActionsHandlers;
50 $fulltype = $entry->getFullType();
51 $wildcard = $entry->getType() . '/*';
52 $handler = '';
54 if ( isset( $wgLogActionsHandlers[$fulltype] ) ) {
55 $handler = $wgLogActionsHandlers[$fulltype];
56 } elseif ( isset( $wgLogActionsHandlers[$wildcard] ) ) {
57 $handler = $wgLogActionsHandlers[$wildcard];
60 if ( $handler !== '' && is_string( $handler ) && class_exists( $handler ) ) {
61 return new $handler( $entry );
64 return new LegacyLogFormatter( $entry );
67 /**
68 * Handy shortcut for constructing a formatter directly from
69 * database row.
70 * @param stdClass|array $row
71 * @see DatabaseLogEntry::getSelectQueryData
72 * @return LogFormatter
74 public static function newFromRow( $row ) {
75 return self::newFromEntry( DatabaseLogEntry::newFromRow( $row ) );
78 // Nonstatic->
80 /** @var LogEntryBase */
81 protected $entry;
83 /** @var int Constant for handling log_deleted */
84 protected $audience = self::FOR_PUBLIC;
86 /** @var IContextSource Context for logging */
87 public $context;
89 /** @var bool Whether to output user tool links */
90 protected $linkFlood = false;
92 /**
93 * Set to true if we are constructing a message text that is going to
94 * be included in page history or send to IRC feed. Links are replaced
95 * with plaintext or with [[pagename]] kind of syntax, that is parsed
96 * by page histories and IRC feeds.
97 * @var string
99 protected $plaintext = false;
101 /** @var string */
102 protected $irctext = false;
104 protected function __construct( LogEntry $entry ) {
105 $this->entry = $entry;
106 $this->context = RequestContext::getMain();
110 * Replace the default context
111 * @param IContextSource $context
113 public function setContext( IContextSource $context ) {
114 $this->context = $context;
118 * Set the visibility restrictions for displaying content.
119 * If set to public, and an item is deleted, then it will be replaced
120 * with a placeholder even if the context user is allowed to view it.
121 * @param int $audience Const self::FOR_THIS_USER or self::FOR_PUBLIC
123 public function setAudience( $audience ) {
124 $this->audience = ( $audience == self::FOR_THIS_USER )
125 ? self::FOR_THIS_USER
126 : self::FOR_PUBLIC;
130 * Check if a log item can be displayed
131 * @param int $field LogPage::DELETED_* constant
132 * @return bool
134 protected function canView( $field ) {
135 if ( $this->audience == self::FOR_THIS_USER ) {
136 return LogEventsList::userCanBitfield(
137 $this->entry->getDeleted(), $field, $this->context->getUser() );
138 } else {
139 return !$this->entry->isDeleted( $field );
144 * If set to true, will produce user tool links after
145 * the user name. This should be replaced with generic
146 * CSS/JS solution.
147 * @param bool $value
149 public function setShowUserToolLinks( $value ) {
150 $this->linkFlood = $value;
154 * Ugly hack to produce plaintext version of the message.
155 * Usually you also want to set extraneous request context
156 * to avoid formatting for any particular user.
157 * @see getActionText()
158 * @return string Plain text
160 public function getPlainActionText() {
161 $this->plaintext = true;
162 $text = $this->getActionText();
163 $this->plaintext = false;
165 return $text;
169 * Even uglier hack to maintain backwards compatibilty with IRC bots
170 * (bug 34508).
171 * @see getActionText()
172 * @return string Text
174 public function getIRCActionComment() {
175 $actionComment = $this->getIRCActionText();
176 $comment = $this->entry->getComment();
178 if ( $comment != '' ) {
179 if ( $actionComment == '' ) {
180 $actionComment = $comment;
181 } else {
182 $actionComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $comment;
186 return $actionComment;
190 * Even uglier hack to maintain backwards compatibilty with IRC bots
191 * (bug 34508).
192 * @see getActionText()
193 * @return string Text
195 public function getIRCActionText() {
196 global $wgContLang;
198 $this->plaintext = true;
199 $this->irctext = true;
201 $entry = $this->entry;
202 $parameters = $entry->getParameters();
203 // @see LogPage::actionText()
204 // Text of title the action is aimed at.
205 $target = $entry->getTarget()->getPrefixedText();
206 $text = null;
207 switch ( $entry->getType() ) {
208 case 'move':
209 switch ( $entry->getSubtype() ) {
210 case 'move':
211 $movesource = $parameters['4::target'];
212 $text = wfMessage( '1movedto2' )
213 ->rawParams( $target, $movesource )->inContentLanguage()->escaped();
214 break;
215 case 'move_redir':
216 $movesource = $parameters['4::target'];
217 $text = wfMessage( '1movedto2_redir' )
218 ->rawParams( $target, $movesource )->inContentLanguage()->escaped();
219 break;
220 case 'move-noredirect':
221 break;
222 case 'move_redir-noredirect':
223 break;
225 break;
227 case 'delete':
228 switch ( $entry->getSubtype() ) {
229 case 'delete':
230 $text = wfMessage( 'deletedarticle' )
231 ->rawParams( $target )->inContentLanguage()->escaped();
232 break;
233 case 'restore':
234 $text = wfMessage( 'undeletedarticle' )
235 ->rawParams( $target )->inContentLanguage()->escaped();
236 break;
237 // @codingStandardsIgnoreStart Long line
238 //case 'revision': // Revision deletion
239 //case 'event': // Log deletion
240 // see https://github.com/wikimedia/mediawiki/commit/a9c243b7b5289dad204278dbe7ed571fd914e395
241 //default:
242 // @codingStandardsIgnoreEnd
244 break;
246 case 'patrol':
247 // @codingStandardsIgnoreStart Long line
248 // https://github.com/wikimedia/mediawiki/commit/1a05f8faf78675dc85984f27f355b8825b43efff
249 // @codingStandardsIgnoreEnd
250 // Create a diff link to the patrolled revision
251 if ( $entry->getSubtype() === 'patrol' ) {
252 $diffLink = htmlspecialchars(
253 wfMessage( 'patrol-log-diff', $parameters['4::curid'] )
254 ->inContentLanguage()->text() );
255 $text = wfMessage( 'patrol-log-line', $diffLink, "[[$target]]", "" )
256 ->inContentLanguage()->text();
257 } else {
258 // broken??
260 break;
262 case 'protect':
263 switch ( $entry->getSubtype() ) {
264 case 'protect':
265 $text = wfMessage( 'protectedarticle' )
266 ->rawParams( $target . ' ' . $parameters['4::description'] )->inContentLanguage()->escaped();
267 break;
268 case 'unprotect':
269 $text = wfMessage( 'unprotectedarticle' )
270 ->rawParams( $target )->inContentLanguage()->escaped();
271 break;
272 case 'modify':
273 $text = wfMessage( 'modifiedarticleprotection' )
274 ->rawParams( $target . ' ' . $parameters['4::description'] )->inContentLanguage()->escaped();
275 break;
276 case 'move_prot':
277 $text = wfMessage( 'movedarticleprotection' )
278 ->rawParams( $target, $parameters['4::oldtitle'] )->inContentLanguage()->escaped();
279 break;
281 break;
283 case 'newusers':
284 switch ( $entry->getSubtype() ) {
285 case 'newusers':
286 case 'create':
287 $text = wfMessage( 'newuserlog-create-entry' )
288 ->inContentLanguage()->escaped();
289 break;
290 case 'create2':
291 case 'byemail':
292 $text = wfMessage( 'newuserlog-create2-entry' )
293 ->rawParams( $target )->inContentLanguage()->escaped();
294 break;
295 case 'autocreate':
296 $text = wfMessage( 'newuserlog-autocreate-entry' )
297 ->inContentLanguage()->escaped();
298 break;
300 break;
302 case 'upload':
303 switch ( $entry->getSubtype() ) {
304 case 'upload':
305 $text = wfMessage( 'uploadedimage' )
306 ->rawParams( $target )->inContentLanguage()->escaped();
307 break;
308 case 'overwrite':
309 $text = wfMessage( 'overwroteimage' )
310 ->rawParams( $target )->inContentLanguage()->escaped();
311 break;
313 break;
315 case 'rights':
316 if ( count( $parameters['4::oldgroups'] ) ) {
317 $oldgroups = implode( ', ', $parameters['4::oldgroups'] );
318 } else {
319 $oldgroups = wfMessage( 'rightsnone' )->inContentLanguage()->escaped();
321 if ( count( $parameters['5::newgroups'] ) ) {
322 $newgroups = implode( ', ', $parameters['5::newgroups'] );
323 } else {
324 $newgroups = wfMessage( 'rightsnone' )->inContentLanguage()->escaped();
326 switch ( $entry->getSubtype() ) {
327 case 'rights':
328 $text = wfMessage( 'rightslogentry' )
329 ->rawParams( $target, $oldgroups, $newgroups )->inContentLanguage()->escaped();
330 break;
331 case 'autopromote':
332 $text = wfMessage( 'rightslogentry-autopromote' )
333 ->rawParams( $target, $oldgroups, $newgroups )->inContentLanguage()->escaped();
334 break;
336 break;
338 case 'merge':
339 $text = wfMessage( 'pagemerge-logentry' )
340 ->rawParams( $target, $parameters['4::dest'], $parameters['5::mergepoint'] )
341 ->inContentLanguage()->escaped();
342 break;
344 case 'block':
345 switch ( $entry->getSubtype() ) {
346 case 'block':
347 // Keep compatibility with extensions by checking for
348 // new key (5::duration/6::flags) or old key (0/optional 1)
349 if ( $entry->isLegacy() ) {
350 $rawDuration = $parameters[0];
351 $rawFlags = isset( $parameters[1] ) ? $parameters[1] : '';
352 } else {
353 $rawDuration = $parameters['5::duration'];
354 $rawFlags = $parameters['6::flags'];
356 $duration = $wgContLang->translateBlockExpiry(
357 $rawDuration,
358 null,
359 wfTimestamp( TS_UNIX, $entry->getTimestamp() )
361 $flags = BlockLogFormatter::formatBlockFlags( $rawFlags, $wgContLang );
362 $text = wfMessage( 'blocklogentry' )
363 ->rawParams( $target, $duration, $flags )->inContentLanguage()->escaped();
364 break;
365 case 'unblock':
366 $text = wfMessage( 'unblocklogentry' )
367 ->rawParams( $target )->inContentLanguage()->escaped();
368 break;
369 case 'reblock':
370 $duration = $wgContLang->translateBlockExpiry(
371 $parameters['5::duration'],
372 null,
373 wfTimestamp( TS_UNIX, $entry->getTimestamp() )
375 $flags = BlockLogFormatter::formatBlockFlags( $parameters['6::flags'], $wgContLang );
376 $text = wfMessage( 'reblock-logentry' )
377 ->rawParams( $target, $duration, $flags )->inContentLanguage()->escaped();
378 break;
380 break;
382 case 'import':
383 switch ( $entry->getSubtype() ) {
384 case 'upload':
385 $text = wfMessage( 'import-logentry-upload' )
386 ->rawParams( $target )->inContentLanguage()->escaped();
387 break;
388 case 'interwiki':
389 $text = wfMessage( 'import-logentry-interwiki' )
390 ->rawParams( $target )->inContentLanguage()->escaped();
391 break;
393 break;
394 // case 'suppress' --private log -- aaron (so we know who to blame in a few years :-D)
395 // default:
397 if ( is_null( $text ) ) {
398 $text = $this->getPlainActionText();
401 $this->plaintext = false;
402 $this->irctext = false;
404 return $text;
408 * Gets the log action, including username.
409 * @return string HTML
411 public function getActionText() {
412 if ( $this->canView( LogPage::DELETED_ACTION ) ) {
413 $element = $this->getActionMessage();
414 if ( $element instanceof Message ) {
415 $element = $this->plaintext ? $element->text() : $element->escaped();
417 if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) ) {
418 $element = $this->styleRestricedElement( $element );
420 } else {
421 $sep = $this->msg( 'word-separator' );
422 $sep = $this->plaintext ? $sep->text() : $sep->escaped();
423 $performer = $this->getPerformerElement();
424 $element = $performer . $sep . $this->getRestrictedElement( 'rev-deleted-event' );
427 return $element;
431 * Returns a sentence describing the log action. Usually
432 * a Message object is returned, but old style log types
433 * and entries might return pre-escaped HTML string.
434 * @return Message|string Pre-escaped HTML
436 protected function getActionMessage() {
437 $message = $this->msg( $this->getMessageKey() );
438 $message->params( $this->getMessageParameters() );
440 return $message;
444 * Returns a key to be used for formatting the action sentence.
445 * Default is logentry-TYPE-SUBTYPE for modern logs. Legacy log
446 * types will use custom keys, and subclasses can also alter the
447 * key depending on the entry itself.
448 * @return string Message key
450 protected function getMessageKey() {
451 $type = $this->entry->getType();
452 $subtype = $this->entry->getSubtype();
454 return "logentry-$type-$subtype";
458 * Returns extra links that comes after the action text, like "revert", etc.
460 * @return string
462 public function getActionLinks() {
463 return '';
467 * Extracts the optional extra parameters for use in action messages.
468 * The array indexes start from number 3.
469 * @return array
471 protected function extractParameters() {
472 $entry = $this->entry;
473 $params = [];
475 if ( $entry->isLegacy() ) {
476 foreach ( $entry->getParameters() as $index => $value ) {
477 $params[$index + 3] = $value;
481 // Filter out parameters which are not in format #:foo
482 foreach ( $entry->getParameters() as $key => $value ) {
483 if ( strpos( $key, ':' ) === false ) {
484 continue;
486 list( $index, $type, ) = explode( ':', $key, 3 );
487 if ( ctype_digit( $index ) ) {
488 $params[$index - 1] = $this->formatParameterValue( $type, $value );
492 /* Message class doesn't like non consecutive numbering.
493 * Fill in missing indexes with empty strings to avoid
494 * incorrect renumbering.
496 if ( count( $params ) ) {
497 $max = max( array_keys( $params ) );
498 // index 0 to 2 are added in getMessageParameters
499 for ( $i = 3; $i < $max; $i++ ) {
500 if ( !isset( $params[$i] ) ) {
501 $params[$i] = '';
506 return $params;
510 * Formats parameters intented for action message from
511 * array of all parameters. There are three hardcoded
512 * parameters (array is zero-indexed, this list not):
513 * - 1: user name with premade link
514 * - 2: usable for gender magic function
515 * - 3: target page with premade link
516 * @return array
518 protected function getMessageParameters() {
519 if ( isset( $this->parsedParameters ) ) {
520 return $this->parsedParameters;
523 $entry = $this->entry;
524 $params = $this->extractParameters();
525 $params[0] = Message::rawParam( $this->getPerformerElement() );
526 $params[1] = $this->canView( LogPage::DELETED_USER ) ? $entry->getPerformer()->getName() : '';
527 $params[2] = Message::rawParam( $this->makePageLink( $entry->getTarget() ) );
529 // Bad things happens if the numbers are not in correct order
530 ksort( $params );
532 $this->parsedParameters = $params;
533 return $this->parsedParameters;
537 * Formats parameters values dependent to their type
538 * @param string $type The type of the value.
539 * Valid are currently:
540 * * - (empty) or plain: The value is returned as-is
541 * * raw: The value will be added to the log message
542 * as raw parameter (e.g. no escaping)
543 * Use this only if there is no other working
544 * type like user-link or title-link
545 * * msg: The value is a message-key, the output is
546 * the message in user language
547 * * msg-content: The value is a message-key, the output
548 * is the message in content language
549 * * user: The value is a user name, e.g. for GENDER
550 * * user-link: The value is a user name, returns a
551 * link for the user
552 * * title: The value is a page title,
553 * returns name of page
554 * * title-link: The value is a page title,
555 * returns link to this page
556 * * number: Format value as number
557 * * list: Format value as a comma-separated list
558 * @param mixed $value The parameter value that should be formatted
559 * @return string|array Formated value
560 * @since 1.21
562 protected function formatParameterValue( $type, $value ) {
563 $saveLinkFlood = $this->linkFlood;
565 switch ( strtolower( trim( $type ) ) ) {
566 case 'raw':
567 $value = Message::rawParam( $value );
568 break;
569 case 'list':
570 $value = $this->context->getLanguage()->commaList( $value );
571 break;
572 case 'msg':
573 $value = $this->msg( $value )->text();
574 break;
575 case 'msg-content':
576 $value = $this->msg( $value )->inContentLanguage()->text();
577 break;
578 case 'number':
579 $value = Message::numParam( $value );
580 break;
581 case 'user':
582 $user = User::newFromName( $value );
583 $value = $user->getName();
584 break;
585 case 'user-link':
586 $this->setShowUserToolLinks( false );
588 $user = User::newFromName( $value );
589 $value = Message::rawParam( $this->makeUserLink( $user ) );
591 $this->setShowUserToolLinks( $saveLinkFlood );
592 break;
593 case 'title':
594 $title = Title::newFromText( $value );
595 $value = $title->getPrefixedText();
596 break;
597 case 'title-link':
598 $title = Title::newFromText( $value );
599 $value = Message::rawParam( $this->makePageLink( $title ) );
600 break;
601 case 'plain':
602 // Plain text, nothing to do
603 default:
604 // Catch other types and use the old behavior (return as-is)
607 return $value;
611 * Helper to make a link to the page, taking the plaintext
612 * value in consideration.
613 * @param Title $title The page
614 * @param array $parameters Query parameters
615 * @param string|null $html Linktext of the link as raw html
616 * @throws MWException
617 * @return string
619 protected function makePageLink( Title $title = null, $parameters = [], $html = null ) {
620 if ( !$this->plaintext ) {
621 $link = Linker::link( $title, $html, [], $parameters );
622 } else {
623 if ( !$title instanceof Title ) {
624 throw new MWException( "Expected title, got null" );
626 $link = '[[' . $title->getPrefixedText() . ']]';
629 return $link;
633 * Provides the name of the user who performed the log action.
634 * Used as part of log action message or standalone, depending
635 * which parts of the log entry has been hidden.
636 * @return string
638 public function getPerformerElement() {
639 if ( $this->canView( LogPage::DELETED_USER ) ) {
640 $performer = $this->entry->getPerformer();
641 $element = $this->makeUserLink( $performer );
642 if ( $this->entry->isDeleted( LogPage::DELETED_USER ) ) {
643 $element = $this->styleRestricedElement( $element );
645 } else {
646 $element = $this->getRestrictedElement( 'rev-deleted-user' );
649 return $element;
653 * Gets the user provided comment
654 * @return string HTML
656 public function getComment() {
657 if ( $this->canView( LogPage::DELETED_COMMENT ) ) {
658 $comment = Linker::commentBlock( $this->entry->getComment() );
659 // No hard coded spaces thanx
660 $element = ltrim( $comment );
661 if ( $this->entry->isDeleted( LogPage::DELETED_COMMENT ) ) {
662 $element = $this->styleRestricedElement( $element );
664 } else {
665 $element = $this->getRestrictedElement( 'rev-deleted-comment' );
668 return $element;
672 * Helper method for displaying restricted element.
673 * @param string $message
674 * @return string HTML or wiki text
676 protected function getRestrictedElement( $message ) {
677 if ( $this->plaintext ) {
678 return $this->msg( $message )->text();
681 $content = $this->msg( $message )->escaped();
682 $attribs = [ 'class' => 'history-deleted' ];
684 return Html::rawElement( 'span', $attribs, $content );
688 * Helper method for styling restricted element.
689 * @param string $content
690 * @return string HTML or wiki text
692 protected function styleRestricedElement( $content ) {
693 if ( $this->plaintext ) {
694 return $content;
696 $attribs = [ 'class' => 'history-deleted' ];
698 return Html::rawElement( 'span', $attribs, $content );
702 * Shortcut for wfMessage which honors local context.
703 * @param string $key
704 * @return Message
706 protected function msg( $key ) {
707 return $this->context->msg( $key );
710 protected function makeUserLink( User $user, $toolFlags = 0 ) {
711 if ( $this->plaintext ) {
712 $element = $user->getName();
713 } else {
714 $element = Linker::userLink(
715 $user->getId(),
716 $user->getName()
719 if ( $this->linkFlood ) {
720 $element .= Linker::userToolLinks(
721 $user->getId(),
722 $user->getName(),
723 true, // redContribsWhenNoEdits
724 $toolFlags,
725 $user->getEditCount()
730 return $element;
734 * @return array Array of titles that should be preloaded with LinkBatch
736 public function getPreloadTitles() {
737 return [];
741 * @return array Output of getMessageParameters() for testing
743 public function getMessageParametersForTesting() {
744 // This function was added because getMessageParameters() is
745 // protected and a change from protected to public caused
746 // problems with extensions
747 return $this->getMessageParameters();
751 * Get the array of parameters, converted from legacy format if necessary.
752 * @since 1.25
753 * @return array
755 protected function getParametersForApi() {
756 return $this->entry->getParameters();
760 * Format parameters for API output
762 * The result array should generally map named keys to values. Index and
763 * type should be omitted, e.g. "4::foo" should be returned as "foo" in the
764 * output. Values should generally be unformatted.
766 * Renames or removals of keys besides from the legacy numeric format to
767 * modern named style should be avoided. Any renames should be announced to
768 * the mediawiki-api-announce mailing list.
770 * @since 1.25
771 * @return array
773 public function formatParametersForApi() {
774 $logParams = [];
775 foreach ( $this->getParametersForApi() as $key => $value ) {
776 $vals = explode( ':', $key, 3 );
777 if ( count( $vals ) !== 3 ) {
778 $logParams[$key] = $value;
779 continue;
781 $logParams += $this->formatParameterValueForApi( $vals[2], $vals[1], $value );
783 ApiResult::setIndexedTagName( $logParams, 'param' );
784 ApiResult::setArrayType( $logParams, 'assoc' );
786 return $logParams;
790 * Format a single parameter value for API output
792 * @since 1.25
793 * @param string $name
794 * @param string $type
795 * @param string $value
796 * @return array
798 protected function formatParameterValueForApi( $name, $type, $value ) {
799 $type = strtolower( trim( $type ) );
800 switch ( $type ) {
801 case 'bool':
802 $value = (bool)$value;
803 break;
805 case 'number':
806 if ( ctype_digit( $value ) || is_int( $value ) ) {
807 $value = (int)$value;
808 } else {
809 $value = (float)$value;
811 break;
813 case 'array':
814 case 'assoc':
815 case 'kvp':
816 if ( is_array( $value ) ) {
817 ApiResult::setArrayType( $value, $type );
819 break;
821 case 'timestamp':
822 $value = wfTimestamp( TS_ISO_8601, $value );
823 break;
825 case 'msg':
826 case 'msg-content':
827 $msg = $this->msg( $value );
828 if ( $type === 'msg-content' ) {
829 $msg->inContentLanguage();
831 $value = [];
832 $value["{$name}_key"] = $msg->getKey();
833 if ( $msg->getParams() ) {
834 $value["{$name}_params"] = $msg->getParams();
836 $value["{$name}_text"] = $msg->text();
837 return $value;
839 case 'title':
840 case 'title-link':
841 $title = Title::newFromText( $value );
842 if ( $title ) {
843 $value = [];
844 ApiQueryBase::addTitleInfo( $value, $title, "{$name}_" );
846 return $value;
848 case 'user':
849 case 'user-link':
850 $user = User::newFromName( $value );
851 if ( $user ) {
852 $value = $user->getName();
854 break;
856 default:
857 // do nothing
858 break;
861 return [ $name => $value ];
866 * This class formats all log entries for log types
867 * which have not been converted to the new system.
868 * This is not about old log entries which store
869 * parameters in a different format - the new
870 * LogFormatter classes have code to support formatting
871 * those too.
872 * @since 1.19
874 class LegacyLogFormatter extends LogFormatter {
876 * Backward compatibility for extension changing the comment from
877 * the LogLine hook. This will be set by the first call on getComment(),
878 * then it might be modified by the hook when calling getActionLinks(),
879 * so that the modified value will be returned when calling getComment()
880 * a second time.
882 * @var string|null
884 private $comment = null;
887 * Cache for the result of getActionLinks() so that it does not need to
888 * run multiple times depending on the order that getComment() and
889 * getActionLinks() are called.
891 * @var string|null
893 private $revert = null;
895 public function getComment() {
896 if ( $this->comment === null ) {
897 $this->comment = parent::getComment();
900 // Make sure we execute the LogLine hook so that we immediately return
901 // the correct value.
902 if ( $this->revert === null ) {
903 $this->getActionLinks();
906 return $this->comment;
909 protected function getActionMessage() {
910 $entry = $this->entry;
911 $action = LogPage::actionText(
912 $entry->getType(),
913 $entry->getSubtype(),
914 $entry->getTarget(),
915 $this->plaintext ? null : $this->context->getSkin(),
916 (array)$entry->getParameters(),
917 !$this->plaintext // whether to filter [[]] links
920 $performer = $this->getPerformerElement();
921 if ( !$this->irctext ) {
922 $sep = $this->msg( 'word-separator' );
923 $sep = $this->plaintext ? $sep->text() : $sep->escaped();
924 $action = $performer . $sep . $action;
927 return $action;
930 public function getActionLinks() {
931 if ( $this->revert !== null ) {
932 return $this->revert;
935 if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) ) {
936 $this->revert = '';
937 return $this->revert;
940 $title = $this->entry->getTarget();
941 $type = $this->entry->getType();
942 $subtype = $this->entry->getSubtype();
944 // Do nothing. The implementation is handled by the hook modifiying the
945 // passed-by-ref parameters. This also changes the default value so that
946 // getComment() and getActionLinks() do not call them indefinitely.
947 $this->revert = '';
949 // This is to populate the $comment member of this instance so that it
950 // can be modified when calling the hook just below.
951 if ( $this->comment === null ) {
952 $this->getComment();
955 $params = $this->entry->getParameters();
957 Hooks::run( 'LogLine', [ $type, $subtype, $title, $params,
958 &$this->comment, &$this->revert, $this->entry->getTimestamp() ] );
960 return $this->revert;