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
21 * @author Niklas Laxström
22 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
27 * Implements the default log formatting.
28 * Can be overridden by subclassing and setting
29 * $wgLogActionsHandlers['type/subtype'] = 'class'; or
30 * $wgLogActionsHandlers['type/*'] = 'class';
34 // Audience options for viewing usernames, comments, and actions
36 const FOR_THIS_USER
= 2;
41 * Constructs a new formatter suitable for given entry.
42 * @param LogEntry $entry
43 * @return LogFormatter
45 public static function newFromEntry( LogEntry
$entry ) {
46 global $wgLogActionsHandlers;
47 $fulltype = $entry->getFullType();
48 $wildcard = $entry->getType() . '/*';
51 if ( isset( $wgLogActionsHandlers[$fulltype] ) ) {
52 $handler = $wgLogActionsHandlers[$fulltype];
53 } elseif ( isset( $wgLogActionsHandlers[$wildcard] ) ) {
54 $handler = $wgLogActionsHandlers[$wildcard];
57 if ( $handler !== '' && is_string( $handler ) && class_exists( $handler ) ) {
58 return new $handler( $entry );
61 return new LegacyLogFormatter( $entry );
65 * Handy shortcut for constructing a formatter directly from
68 * @see DatabaseLogEntry::getSelectQueryData
69 * @return LogFormatter
71 public static function newFromRow( $row ) {
72 return self
::newFromEntry( DatabaseLogEntry
::newFromRow( $row ) );
77 /** @var LogEntryBase */
80 /** @var int Constant for handling log_deleted */
81 protected $audience = self
::FOR_PUBLIC
;
83 /** @var IContextSource Context for logging */
86 /** @var bool Whether to output user tool links */
87 protected $linkFlood = false;
90 * Set to true if we are constructing a message text that is going to
91 * be included in page history or send to IRC feed. Links are replaced
92 * with plaintext or with [[pagename]] kind of syntax, that is parsed
93 * by page histories and IRC feeds.
96 protected $plaintext = false;
99 protected $irctext = false;
101 protected function __construct( LogEntry
$entry ) {
102 $this->entry
= $entry;
103 $this->context
= RequestContext
::getMain();
107 * Replace the default context
108 * @param IContextSource $context
110 public function setContext( IContextSource
$context ) {
111 $this->context
= $context;
115 * Set the visibility restrictions for displaying content.
116 * If set to public, and an item is deleted, then it will be replaced
117 * with a placeholder even if the context user is allowed to view it.
118 * @param int $audience Const self::FOR_THIS_USER or self::FOR_PUBLIC
120 public function setAudience( $audience ) {
121 $this->audience
= ( $audience == self
::FOR_THIS_USER
)
122 ? self
::FOR_THIS_USER
127 * Check if a log item can be displayed
128 * @param int $field LogPage::DELETED_* constant
131 protected function canView( $field ) {
132 if ( $this->audience
== self
::FOR_THIS_USER
) {
133 return LogEventsList
::userCanBitfield(
134 $this->entry
->getDeleted(), $field, $this->context
->getUser() );
136 return !$this->entry
->isDeleted( $field );
141 * If set to true, will produce user tool links after
142 * the user name. This should be replaced with generic
146 public function setShowUserToolLinks( $value ) {
147 $this->linkFlood
= $value;
151 * Ugly hack to produce plaintext version of the message.
152 * Usually you also want to set extraneous request context
153 * to avoid formatting for any particular user.
154 * @see getActionText()
155 * @return string Plain text
157 public function getPlainActionText() {
158 $this->plaintext
= true;
159 $text = $this->getActionText();
160 $this->plaintext
= false;
166 * Even uglier hack to maintain backwards compatibilty with IRC bots
168 * @see getActionText()
169 * @return string Text
171 public function getIRCActionComment() {
172 $actionComment = $this->getIRCActionText();
173 $comment = $this->entry
->getComment();
175 if ( $comment != '' ) {
176 if ( $actionComment == '' ) {
177 $actionComment = $comment;
179 $actionComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $comment;
183 return $actionComment;
187 * Even uglier hack to maintain backwards compatibilty with IRC bots
189 * @see getActionText()
190 * @return string Text
192 public function getIRCActionText() {
193 $this->plaintext
= true;
194 $this->irctext
= true;
196 $entry = $this->entry
;
197 $parameters = $entry->getParameters();
198 // @see LogPage::actionText()
199 // Text of title the action is aimed at.
200 $target = $entry->getTarget()->getPrefixedText();
202 switch ( $entry->getType() ) {
204 switch ( $entry->getSubtype() ) {
206 $movesource = $parameters['4::target'];
207 $text = wfMessage( '1movedto2' )
208 ->rawParams( $target, $movesource )->inContentLanguage()->escaped();
211 $movesource = $parameters['4::target'];
212 $text = wfMessage( '1movedto2_redir' )
213 ->rawParams( $target, $movesource )->inContentLanguage()->escaped();
215 case 'move-noredirect':
217 case 'move_redir-noredirect':
223 switch ( $entry->getSubtype() ) {
225 $text = wfMessage( 'deletedarticle' )
226 ->rawParams( $target )->inContentLanguage()->escaped();
229 $text = wfMessage( 'undeletedarticle' )
230 ->rawParams( $target )->inContentLanguage()->escaped();
232 // @codingStandardsIgnoreStart Long line
233 //case 'revision': // Revision deletion
234 //case 'event': // Log deletion
235 // see https://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/includes/LogPage.php?&pathrev=97044&r1=97043&r2=97044
237 // @codingStandardsIgnoreEnd
242 // @codingStandardsIgnoreStart Long line
243 // https://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/includes/PatrolLog.php?&pathrev=97495&r1=97494&r2=97495
244 // @codingStandardsIgnoreEnd
245 // Create a diff link to the patrolled revision
246 if ( $entry->getSubtype() === 'patrol' ) {
247 $diffLink = htmlspecialchars(
248 wfMessage( 'patrol-log-diff', $parameters['4::curid'] )
249 ->inContentLanguage()->text() );
250 $text = wfMessage( 'patrol-log-line', $diffLink, "[[$target]]", "" )
251 ->inContentLanguage()->text();
258 switch ( $entry->getSubtype() ) {
260 $text = wfMessage( 'protectedarticle' )
261 ->rawParams( $target . ' ' . $parameters[0] )->inContentLanguage()->escaped();
264 $text = wfMessage( 'unprotectedarticle' )
265 ->rawParams( $target )->inContentLanguage()->escaped();
268 $text = wfMessage( 'modifiedarticleprotection' )
269 ->rawParams( $target . ' ' . $parameters[0] )->inContentLanguage()->escaped();
275 switch ( $entry->getSubtype() ) {
278 $text = wfMessage( 'newuserlog-create-entry' )
279 ->inContentLanguage()->escaped();
283 $text = wfMessage( 'newuserlog-create2-entry' )
284 ->rawParams( $target )->inContentLanguage()->escaped();
287 $text = wfMessage( 'newuserlog-autocreate-entry' )
288 ->inContentLanguage()->escaped();
294 switch ( $entry->getSubtype() ) {
296 $text = wfMessage( 'uploadedimage' )
297 ->rawParams( $target )->inContentLanguage()->escaped();
300 $text = wfMessage( 'overwroteimage' )
301 ->rawParams( $target )->inContentLanguage()->escaped();
307 if ( count( $parameters['4::oldgroups'] ) ) {
308 $oldgroups = implode( ', ', $parameters['4::oldgroups'] );
310 $oldgroups = wfMessage( 'rightsnone' )->inContentLanguage()->escaped();
312 if ( count( $parameters['5::newgroups'] ) ) {
313 $newgroups = implode( ', ', $parameters['5::newgroups'] );
315 $newgroups = wfMessage( 'rightsnone' )->inContentLanguage()->escaped();
317 switch ( $entry->getSubtype() ) {
319 $text = wfMessage( 'rightslogentry' )
320 ->rawParams( $target, $oldgroups, $newgroups )->inContentLanguage()->escaped();
323 $text = wfMessage( 'rightslogentry-autopromote' )
324 ->rawParams( $target, $oldgroups, $newgroups )->inContentLanguage()->escaped();
330 $text = wfMessage( 'pagemerge-logentry' )
331 ->rawParams( $target, $parameters['4::dest'], $parameters['5::mergepoint'] )
332 ->inContentLanguage()->escaped();
336 switch ( $entry->getSubtype() ) {
339 // Keep compatibility with extensions by checking for
340 // new key (5::duration/6::flags) or old key (0/optional 1)
341 if ( $entry->isLegacy() ) {
342 $rawDuration = $parameters[0];
343 $rawFlags = isset( $parameters[1] ) ?
$parameters[1] : '';
345 $rawDuration = $parameters['5::duration'];
346 $rawFlags = $parameters['6::flags'];
348 $duration = $wgContLang->translateBlockExpiry( $rawDuration );
349 $flags = BlockLogFormatter
::formatBlockFlags( $rawFlags, $wgContLang );
350 $text = wfMessage( 'blocklogentry' )
351 ->rawParams( $target, $duration, $flags )->inContentLanguage()->escaped();
354 $text = wfMessage( 'unblocklogentry' )
355 ->rawParams( $target )->inContentLanguage()->escaped();
359 $duration = $wgContLang->translateBlockExpiry( $parameters['5::duration'] );
360 $flags = BlockLogFormatter
::formatBlockFlags( $parameters['6::flags'], $wgContLang );
361 $text = wfMessage( 'reblock-logentry' )
362 ->rawParams( $target, $duration, $flags )->inContentLanguage()->escaped();
366 // case 'suppress' --private log -- aaron (so we know who to blame in a few years :-D)
369 if ( is_null( $text ) ) {
370 $text = $this->getPlainActionText();
373 $this->plaintext
= false;
374 $this->irctext
= false;
380 * Gets the log action, including username.
381 * @return string HTML
383 public function getActionText() {
384 if ( $this->canView( LogPage
::DELETED_ACTION
) ) {
385 $element = $this->getActionMessage();
386 if ( $element instanceof Message
) {
387 $element = $this->plaintext ?
$element->text() : $element->escaped();
389 if ( $this->entry
->isDeleted( LogPage
::DELETED_ACTION
) ) {
390 $element = $this->styleRestricedElement( $element );
393 $sep = $this->msg( 'word-separator' );
394 $sep = $this->plaintext ?
$sep->text() : $sep->escaped();
395 $performer = $this->getPerformerElement();
396 $element = $performer . $sep . $this->getRestrictedElement( 'rev-deleted-event' );
403 * Returns a sentence describing the log action. Usually
404 * a Message object is returned, but old style log types
405 * and entries might return pre-escaped HTML string.
406 * @return Message|string Pre-escaped HTML
408 protected function getActionMessage() {
409 $message = $this->msg( $this->getMessageKey() );
410 $message->params( $this->getMessageParameters() );
416 * Returns a key to be used for formatting the action sentence.
417 * Default is logentry-TYPE-SUBTYPE for modern logs. Legacy log
418 * types will use custom keys, and subclasses can also alter the
419 * key depending on the entry itself.
420 * @return string Message key
422 protected function getMessageKey() {
423 $type = $this->entry
->getType();
424 $subtype = $this->entry
->getSubtype();
426 return "logentry-$type-$subtype";
430 * Returns extra links that comes after the action text, like "revert", etc.
434 public function getActionLinks() {
439 * Extracts the optional extra parameters for use in action messages.
440 * The array indexes start from number 3.
443 protected function extractParameters() {
444 $entry = $this->entry
;
447 if ( $entry->isLegacy() ) {
448 foreach ( $entry->getParameters() as $index => $value ) {
449 $params[$index +
3] = $value;
453 // Filter out parameters which are not in format #:foo
454 foreach ( $entry->getParameters() as $key => $value ) {
455 if ( strpos( $key, ':' ) === false ) {
458 list( $index, $type, ) = explode( ':', $key, 3 );
459 $params[$index - 1] = $this->formatParameterValue( $type, $value );
462 /* Message class doesn't like non consecutive numbering.
463 * Fill in missing indexes with empty strings to avoid
464 * incorrect renumbering.
466 if ( count( $params ) ) {
467 $max = max( array_keys( $params ) );
468 // index 0 to 2 are added in getMessageParameters
469 for ( $i = 3; $i < $max; $i++
) {
470 if ( !isset( $params[$i] ) ) {
480 * Formats parameters intented for action message from
481 * array of all parameters. There are three hardcoded
482 * parameters (array is zero-indexed, this list not):
483 * - 1: user name with premade link
484 * - 2: usable for gender magic function
485 * - 3: target page with premade link
488 protected function getMessageParameters() {
489 if ( isset( $this->parsedParameters
) ) {
490 return $this->parsedParameters
;
493 $entry = $this->entry
;
494 $params = $this->extractParameters();
495 $params[0] = Message
::rawParam( $this->getPerformerElement() );
496 $params[1] = $this->canView( LogPage
::DELETED_USER
) ?
$entry->getPerformer()->getName() : '';
497 $params[2] = Message
::rawParam( $this->makePageLink( $entry->getTarget() ) );
499 // Bad things happens if the numbers are not in correct order
502 $this->parsedParameters
= $params;
503 return $this->parsedParameters
;
507 * Formats parameters values dependent to their type
508 * @param string $type The type of the value.
509 * Valid are currently:
510 * * - (empty) or plain: The value is returned as-is
511 * * raw: The value will be added to the log message
512 * as raw parameter (e.g. no escaping)
513 * Use this only if there is no other working
514 * type like user-link or title-link
515 * * msg: The value is a message-key, the output is
516 * the message in user language
517 * * msg-content: The value is a message-key, the output
518 * is the message in content language
519 * * user: The value is a user name, e.g. for GENDER
520 * * user-link: The value is a user name, returns a
522 * * title: The value is a page title,
523 * returns name of page
524 * * title-link: The value is a page title,
525 * returns link to this page
526 * * number: Format value as number
527 * @param string $value The parameter value that should
529 * @return string|array Formated value
532 protected function formatParameterValue( $type, $value ) {
533 $saveLinkFlood = $this->linkFlood
;
535 switch ( strtolower( trim( $type ) ) ) {
537 $value = Message
::rawParam( $value );
540 $value = $this->msg( $value )->text();
543 $value = $this->msg( $value )->inContentLanguage()->text();
546 $value = Message
::numParam( $value );
549 $user = User
::newFromName( $value );
550 $value = $user->getName();
553 $this->setShowUserToolLinks( false );
555 $user = User
::newFromName( $value );
556 $value = Message
::rawParam( $this->makeUserLink( $user ) );
558 $this->setShowUserToolLinks( $saveLinkFlood );
561 $title = Title
::newFromText( $value );
562 $value = $title->getPrefixedText();
565 $title = Title
::newFromText( $value );
566 $value = Message
::rawParam( $this->makePageLink( $title ) );
569 // Plain text, nothing to do
571 // Catch other types and use the old behavior (return as-is)
578 * Helper to make a link to the page, taking the plaintext
579 * value in consideration.
580 * @param Title $title The page
581 * @param array $parameters Query parameters
582 * @throws MWException
585 protected function makePageLink( Title
$title = null, $parameters = array() ) {
586 if ( !$this->plaintext
) {
587 $link = Linker
::link( $title, null, array(), $parameters );
589 if ( !$title instanceof Title
) {
590 throw new MWException( "Expected title, got null" );
592 $link = '[[' . $title->getPrefixedText() . ']]';
599 * Provides the name of the user who performed the log action.
600 * Used as part of log action message or standalone, depending
601 * which parts of the log entry has been hidden.
604 public function getPerformerElement() {
605 if ( $this->canView( LogPage
::DELETED_USER
) ) {
606 $performer = $this->entry
->getPerformer();
607 $element = $this->makeUserLink( $performer );
608 if ( $this->entry
->isDeleted( LogPage
::DELETED_USER
) ) {
609 $element = $this->styleRestricedElement( $element );
612 $element = $this->getRestrictedElement( 'rev-deleted-user' );
619 * Gets the user provided comment
620 * @return string HTML
622 public function getComment() {
623 if ( $this->canView( LogPage
::DELETED_COMMENT
) ) {
624 $comment = Linker
::commentBlock( $this->entry
->getComment() );
625 // No hard coded spaces thanx
626 $element = ltrim( $comment );
627 if ( $this->entry
->isDeleted( LogPage
::DELETED_COMMENT
) ) {
628 $element = $this->styleRestricedElement( $element );
631 $element = $this->getRestrictedElement( 'rev-deleted-comment' );
638 * Helper method for displaying restricted element.
639 * @param string $message
640 * @return string HTML or wiki text
642 protected function getRestrictedElement( $message ) {
643 if ( $this->plaintext
) {
644 return $this->msg( $message )->text();
647 $content = $this->msg( $message )->escaped();
648 $attribs = array( 'class' => 'history-deleted' );
650 return Html
::rawElement( 'span', $attribs, $content );
654 * Helper method for styling restricted element.
655 * @param string $content
656 * @return string HTML or wiki text
658 protected function styleRestricedElement( $content ) {
659 if ( $this->plaintext
) {
662 $attribs = array( 'class' => 'history-deleted' );
664 return Html
::rawElement( 'span', $attribs, $content );
668 * Shortcut for wfMessage which honors local context.
672 protected function msg( $key ) {
673 return $this->context
->msg( $key );
676 protected function makeUserLink( User
$user, $toolFlags = 0 ) {
677 if ( $this->plaintext
) {
678 $element = $user->getName();
680 $element = Linker
::userLink(
685 if ( $this->linkFlood
) {
686 $element .= Linker
::userToolLinks(
689 true, // redContribsWhenNoEdits
691 $user->getEditCount()
700 * @return array Array of titles that should be preloaded with LinkBatch
702 public function getPreloadTitles() {
707 * @return array Output of getMessageParameters() for testing
709 public function getMessageParametersForTesting() {
710 // This function was added because getMessageParameters() is
711 // protected and a change from protected to public caused
712 // problems with extensions
713 return $this->getMessageParameters();
718 * This class formats all log entries for log types
719 * which have not been converted to the new system.
720 * This is not about old log entries which store
721 * parameters in a different format - the new
722 * LogFormatter classes have code to support formatting
726 class LegacyLogFormatter
extends LogFormatter
{
728 * Backward compatibility for extension changing the comment from
729 * the LogLine hook. This will be set by the first call on getComment(),
730 * then it might be modified by the hook when calling getActionLinks(),
731 * so that the modified value will be returned when calling getComment()
736 private $comment = null;
739 * Cache for the result of getActionLinks() so that it does not need to
740 * run multiple times depending on the order that getComment() and
741 * getActionLinks() are called.
745 private $revert = null;
747 public function getComment() {
748 if ( $this->comment
=== null ) {
749 $this->comment
= parent
::getComment();
752 // Make sure we execute the LogLine hook so that we immediately return
753 // the correct value.
754 if ( $this->revert
=== null ) {
755 $this->getActionLinks();
758 return $this->comment
;
761 protected function getActionMessage() {
762 $entry = $this->entry
;
763 $action = LogPage
::actionText(
765 $entry->getSubtype(),
767 $this->plaintext ?
null : $this->context
->getSkin(),
768 (array)$entry->getParameters(),
769 !$this->plaintext
// whether to filter [[]] links
772 $performer = $this->getPerformerElement();
773 if ( !$this->irctext
) {
774 $sep = $this->msg( 'word-separator' );
775 $sep = $this->plaintext ?
$sep->text() : $sep->escaped();
776 $action = $performer . $sep . $action;
782 public function getActionLinks() {
783 if ( $this->revert
!== null ) {
784 return $this->revert
;
787 if ( $this->entry
->isDeleted( LogPage
::DELETED_ACTION
) ) {
789 return $this->revert
;
792 $title = $this->entry
->getTarget();
793 $type = $this->entry
->getType();
794 $subtype = $this->entry
->getSubtype();
796 if ( $type == 'protect'
797 && ( $subtype == 'protect' ||
$subtype == 'modify' ||
$subtype == 'unprotect' )
800 Linker
::link( $title,
801 $this->msg( 'hist' )->escaped(),
804 'action' => 'history',
805 'offset' => $this->entry
->getTimestamp()
809 if ( $this->context
->getUser()->isAllowed( 'protect' ) ) {
810 $links[] = Linker
::linkKnown(
812 $this->msg( 'protect_change' )->escaped(),
814 array( 'action' => 'protect' )
818 return $this->msg( 'parentheses' )->rawParams(
819 $this->context
->getLanguage()->pipeList( $links ) )->escaped();
822 // Do nothing. The implementation is handled by the hook modifiying the
823 // passed-by-ref parameters. This also changes the default value so that
824 // getComment() and getActionLinks() do not call them indefinitely.
827 // This is to populate the $comment member of this instance so that it
828 // can be modified when calling the hook just below.
829 if ( $this->comment
=== null ) {
833 $params = $this->entry
->getParameters();
835 Hooks
::run( 'LogLine', array( $type, $subtype, $title, $params,
836 &$this->comment
, &$this->revert
, $this->entry
->getTimestamp() ) );
838 return $this->revert
;