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.
29 * Can be overridden by subclassing and setting:
31 * $wgLogActionsHandlers['type/subtype'] = 'class'; or
32 * $wgLogActionsHandlers['type/*'] = 'class';
37 // Audience options for viewing usernames, comments, and actions
39 const FOR_THIS_USER
= 2;
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() . '/*';
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 );
68 * Handy shortcut for constructing a formatter directly from
71 * @see DatabaseLogEntry::getSelectQueryData
72 * @return LogFormatter
74 public static function newFromRow( $row ) {
75 return self
::newFromEntry( DatabaseLogEntry
::newFromRow( $row ) );
80 /** @var LogEntryBase */
83 /** @var int Constant for handling log_deleted */
84 protected $audience = self
::FOR_PUBLIC
;
86 /** @var IContextSource Context for logging */
89 /** @var bool Whether to output user tool links */
90 protected $linkFlood = false;
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.
99 protected $plaintext = false;
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
130 * Check if a log item can be displayed
131 * @param int $field LogPage::DELETED_* constant
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() );
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
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;
169 * Even uglier hack to maintain backwards compatibilty with IRC bots
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;
182 $actionComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $comment;
186 return $actionComment;
190 * Even uglier hack to maintain backwards compatibilty with IRC bots
192 * @see getActionText()
193 * @return string Text
195 public function getIRCActionText() {
196 $this->plaintext
= true;
197 $this->irctext
= true;
199 $entry = $this->entry
;
200 $parameters = $entry->getParameters();
201 // @see LogPage::actionText()
202 // Text of title the action is aimed at.
203 $target = $entry->getTarget()->getPrefixedText();
205 switch ( $entry->getType() ) {
207 switch ( $entry->getSubtype() ) {
209 $movesource = $parameters['4::target'];
210 $text = wfMessage( '1movedto2' )
211 ->rawParams( $target, $movesource )->inContentLanguage()->escaped();
214 $movesource = $parameters['4::target'];
215 $text = wfMessage( '1movedto2_redir' )
216 ->rawParams( $target, $movesource )->inContentLanguage()->escaped();
218 case 'move-noredirect':
220 case 'move_redir-noredirect':
226 switch ( $entry->getSubtype() ) {
228 $text = wfMessage( 'deletedarticle' )
229 ->rawParams( $target )->inContentLanguage()->escaped();
232 $text = wfMessage( 'undeletedarticle' )
233 ->rawParams( $target )->inContentLanguage()->escaped();
235 // @codingStandardsIgnoreStart Long line
236 //case 'revision': // Revision deletion
237 //case 'event': // Log deletion
238 // see https://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/includes/LogPage.php?&pathrev=97044&r1=97043&r2=97044
240 // @codingStandardsIgnoreEnd
245 // @codingStandardsIgnoreStart Long line
246 // https://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/includes/PatrolLog.php?&pathrev=97495&r1=97494&r2=97495
247 // @codingStandardsIgnoreEnd
248 // Create a diff link to the patrolled revision
249 if ( $entry->getSubtype() === 'patrol' ) {
250 $diffLink = htmlspecialchars(
251 wfMessage( 'patrol-log-diff', $parameters['4::curid'] )
252 ->inContentLanguage()->text() );
253 $text = wfMessage( 'patrol-log-line', $diffLink, "[[$target]]", "" )
254 ->inContentLanguage()->text();
261 switch ( $entry->getSubtype() ) {
263 $text = wfMessage( 'protectedarticle' )
264 ->rawParams( $target . ' ' . $parameters[0] )->inContentLanguage()->escaped();
267 $text = wfMessage( 'unprotectedarticle' )
268 ->rawParams( $target )->inContentLanguage()->escaped();
271 $text = wfMessage( 'modifiedarticleprotection' )
272 ->rawParams( $target . ' ' . $parameters[0] )->inContentLanguage()->escaped();
278 switch ( $entry->getSubtype() ) {
281 $text = wfMessage( 'newuserlog-create-entry' )
282 ->inContentLanguage()->escaped();
286 $text = wfMessage( 'newuserlog-create2-entry' )
287 ->rawParams( $target )->inContentLanguage()->escaped();
290 $text = wfMessage( 'newuserlog-autocreate-entry' )
291 ->inContentLanguage()->escaped();
297 switch ( $entry->getSubtype() ) {
299 $text = wfMessage( 'uploadedimage' )
300 ->rawParams( $target )->inContentLanguage()->escaped();
303 $text = wfMessage( 'overwroteimage' )
304 ->rawParams( $target )->inContentLanguage()->escaped();
310 if ( count( $parameters['4::oldgroups'] ) ) {
311 $oldgroups = implode( ', ', $parameters['4::oldgroups'] );
313 $oldgroups = wfMessage( 'rightsnone' )->inContentLanguage()->escaped();
315 if ( count( $parameters['5::newgroups'] ) ) {
316 $newgroups = implode( ', ', $parameters['5::newgroups'] );
318 $newgroups = wfMessage( 'rightsnone' )->inContentLanguage()->escaped();
320 switch ( $entry->getSubtype() ) {
322 $text = wfMessage( 'rightslogentry' )
323 ->rawParams( $target, $oldgroups, $newgroups )->inContentLanguage()->escaped();
326 $text = wfMessage( 'rightslogentry-autopromote' )
327 ->rawParams( $target, $oldgroups, $newgroups )->inContentLanguage()->escaped();
333 $text = wfMessage( 'pagemerge-logentry' )
334 ->rawParams( $target, $parameters['4::dest'], $parameters['5::mergepoint'] )
335 ->inContentLanguage()->escaped();
339 switch ( $entry->getSubtype() ) {
342 // Keep compatibility with extensions by checking for
343 // new key (5::duration/6::flags) or old key (0/optional 1)
344 if ( $entry->isLegacy() ) {
345 $rawDuration = $parameters[0];
346 $rawFlags = isset( $parameters[1] ) ?
$parameters[1] : '';
348 $rawDuration = $parameters['5::duration'];
349 $rawFlags = $parameters['6::flags'];
351 $duration = $wgContLang->translateBlockExpiry( $rawDuration );
352 $flags = BlockLogFormatter
::formatBlockFlags( $rawFlags, $wgContLang );
353 $text = wfMessage( 'blocklogentry' )
354 ->rawParams( $target, $duration, $flags )->inContentLanguage()->escaped();
357 $text = wfMessage( 'unblocklogentry' )
358 ->rawParams( $target )->inContentLanguage()->escaped();
362 $duration = $wgContLang->translateBlockExpiry( $parameters['5::duration'] );
363 $flags = BlockLogFormatter
::formatBlockFlags( $parameters['6::flags'], $wgContLang );
364 $text = wfMessage( 'reblock-logentry' )
365 ->rawParams( $target, $duration, $flags )->inContentLanguage()->escaped();
371 switch ( $entry->getSubtype() ) {
373 $text = wfMessage( 'import-logentry-upload' )
374 ->rawParams( $target )->inContentLanguage()->escaped();
377 $text = wfMessage( 'import-logentry-interwiki' )
378 ->rawParams( $target )->inContentLanguage()->escaped();
382 // case 'suppress' --private log -- aaron (so we know who to blame in a few years :-D)
385 if ( is_null( $text ) ) {
386 $text = $this->getPlainActionText();
389 $this->plaintext
= false;
390 $this->irctext
= false;
396 * Gets the log action, including username.
397 * @return string HTML
399 public function getActionText() {
400 if ( $this->canView( LogPage
::DELETED_ACTION
) ) {
401 $element = $this->getActionMessage();
402 if ( $element instanceof Message
) {
403 $element = $this->plaintext ?
$element->text() : $element->escaped();
405 if ( $this->entry
->isDeleted( LogPage
::DELETED_ACTION
) ) {
406 $element = $this->styleRestricedElement( $element );
409 $sep = $this->msg( 'word-separator' );
410 $sep = $this->plaintext ?
$sep->text() : $sep->escaped();
411 $performer = $this->getPerformerElement();
412 $element = $performer . $sep . $this->getRestrictedElement( 'rev-deleted-event' );
419 * Returns a sentence describing the log action. Usually
420 * a Message object is returned, but old style log types
421 * and entries might return pre-escaped HTML string.
422 * @return Message|string Pre-escaped HTML
424 protected function getActionMessage() {
425 $message = $this->msg( $this->getMessageKey() );
426 $message->params( $this->getMessageParameters() );
432 * Returns a key to be used for formatting the action sentence.
433 * Default is logentry-TYPE-SUBTYPE for modern logs. Legacy log
434 * types will use custom keys, and subclasses can also alter the
435 * key depending on the entry itself.
436 * @return string Message key
438 protected function getMessageKey() {
439 $type = $this->entry
->getType();
440 $subtype = $this->entry
->getSubtype();
442 return "logentry-$type-$subtype";
446 * Returns extra links that comes after the action text, like "revert", etc.
450 public function getActionLinks() {
455 * Extracts the optional extra parameters for use in action messages.
456 * The array indexes start from number 3.
459 protected function extractParameters() {
460 $entry = $this->entry
;
463 if ( $entry->isLegacy() ) {
464 foreach ( $entry->getParameters() as $index => $value ) {
465 $params[$index +
3] = $value;
469 // Filter out parameters which are not in format #:foo
470 foreach ( $entry->getParameters() as $key => $value ) {
471 if ( strpos( $key, ':' ) === false ) {
474 list( $index, $type, ) = explode( ':', $key, 3 );
475 $params[$index - 1] = $this->formatParameterValue( $type, $value );
478 /* Message class doesn't like non consecutive numbering.
479 * Fill in missing indexes with empty strings to avoid
480 * incorrect renumbering.
482 if ( count( $params ) ) {
483 $max = max( array_keys( $params ) );
484 // index 0 to 2 are added in getMessageParameters
485 for ( $i = 3; $i < $max; $i++
) {
486 if ( !isset( $params[$i] ) ) {
496 * Formats parameters intented for action message from
497 * array of all parameters. There are three hardcoded
498 * parameters (array is zero-indexed, this list not):
499 * - 1: user name with premade link
500 * - 2: usable for gender magic function
501 * - 3: target page with premade link
504 protected function getMessageParameters() {
505 if ( isset( $this->parsedParameters
) ) {
506 return $this->parsedParameters
;
509 $entry = $this->entry
;
510 $params = $this->extractParameters();
511 $params[0] = Message
::rawParam( $this->getPerformerElement() );
512 $params[1] = $this->canView( LogPage
::DELETED_USER
) ?
$entry->getPerformer()->getName() : '';
513 $params[2] = Message
::rawParam( $this->makePageLink( $entry->getTarget() ) );
515 // Bad things happens if the numbers are not in correct order
518 $this->parsedParameters
= $params;
519 return $this->parsedParameters
;
523 * Formats parameters values dependent to their type
524 * @param string $type The type of the value.
525 * Valid are currently:
526 * * - (empty) or plain: The value is returned as-is
527 * * raw: The value will be added to the log message
528 * as raw parameter (e.g. no escaping)
529 * Use this only if there is no other working
530 * type like user-link or title-link
531 * * msg: The value is a message-key, the output is
532 * the message in user language
533 * * msg-content: The value is a message-key, the output
534 * is the message in content language
535 * * user: The value is a user name, e.g. for GENDER
536 * * user-link: The value is a user name, returns a
538 * * title: The value is a page title,
539 * returns name of page
540 * * title-link: The value is a page title,
541 * returns link to this page
542 * * number: Format value as number
543 * @param string $value The parameter value that should
545 * @return string|array Formated value
548 protected function formatParameterValue( $type, $value ) {
549 $saveLinkFlood = $this->linkFlood
;
551 switch ( strtolower( trim( $type ) ) ) {
553 $value = Message
::rawParam( $value );
556 $value = $this->msg( $value )->text();
559 $value = $this->msg( $value )->inContentLanguage()->text();
562 $value = Message
::numParam( $value );
565 $user = User
::newFromName( $value );
566 $value = $user->getName();
569 $this->setShowUserToolLinks( false );
571 $user = User
::newFromName( $value );
572 $value = Message
::rawParam( $this->makeUserLink( $user ) );
574 $this->setShowUserToolLinks( $saveLinkFlood );
577 $title = Title
::newFromText( $value );
578 $value = $title->getPrefixedText();
581 $title = Title
::newFromText( $value );
582 $value = Message
::rawParam( $this->makePageLink( $title ) );
585 // Plain text, nothing to do
587 // Catch other types and use the old behavior (return as-is)
594 * Helper to make a link to the page, taking the plaintext
595 * value in consideration.
596 * @param Title $title The page
597 * @param array $parameters Query parameters
598 * @throws MWException
601 protected function makePageLink( Title
$title = null, $parameters = array() ) {
602 if ( !$this->plaintext
) {
603 $link = Linker
::link( $title, null, array(), $parameters );
605 if ( !$title instanceof Title
) {
606 throw new MWException( "Expected title, got null" );
608 $link = '[[' . $title->getPrefixedText() . ']]';
615 * Provides the name of the user who performed the log action.
616 * Used as part of log action message or standalone, depending
617 * which parts of the log entry has been hidden.
620 public function getPerformerElement() {
621 if ( $this->canView( LogPage
::DELETED_USER
) ) {
622 $performer = $this->entry
->getPerformer();
623 $element = $this->makeUserLink( $performer );
624 if ( $this->entry
->isDeleted( LogPage
::DELETED_USER
) ) {
625 $element = $this->styleRestricedElement( $element );
628 $element = $this->getRestrictedElement( 'rev-deleted-user' );
635 * Gets the user provided comment
636 * @return string HTML
638 public function getComment() {
639 if ( $this->canView( LogPage
::DELETED_COMMENT
) ) {
640 $comment = Linker
::commentBlock( $this->entry
->getComment() );
641 // No hard coded spaces thanx
642 $element = ltrim( $comment );
643 if ( $this->entry
->isDeleted( LogPage
::DELETED_COMMENT
) ) {
644 $element = $this->styleRestricedElement( $element );
647 $element = $this->getRestrictedElement( 'rev-deleted-comment' );
654 * Helper method for displaying restricted element.
655 * @param string $message
656 * @return string HTML or wiki text
658 protected function getRestrictedElement( $message ) {
659 if ( $this->plaintext
) {
660 return $this->msg( $message )->text();
663 $content = $this->msg( $message )->escaped();
664 $attribs = array( 'class' => 'history-deleted' );
666 return Html
::rawElement( 'span', $attribs, $content );
670 * Helper method for styling restricted element.
671 * @param string $content
672 * @return string HTML or wiki text
674 protected function styleRestricedElement( $content ) {
675 if ( $this->plaintext
) {
678 $attribs = array( 'class' => 'history-deleted' );
680 return Html
::rawElement( 'span', $attribs, $content );
684 * Shortcut for wfMessage which honors local context.
688 protected function msg( $key ) {
689 return $this->context
->msg( $key );
692 protected function makeUserLink( User
$user, $toolFlags = 0 ) {
693 if ( $this->plaintext
) {
694 $element = $user->getName();
696 $element = Linker
::userLink(
701 if ( $this->linkFlood
) {
702 $element .= Linker
::userToolLinks(
705 true, // redContribsWhenNoEdits
707 $user->getEditCount()
716 * @return array Array of titles that should be preloaded with LinkBatch
718 public function getPreloadTitles() {
723 * @return array Output of getMessageParameters() for testing
725 public function getMessageParametersForTesting() {
726 // This function was added because getMessageParameters() is
727 // protected and a change from protected to public caused
728 // problems with extensions
729 return $this->getMessageParameters();
734 * This class formats all log entries for log types
735 * which have not been converted to the new system.
736 * This is not about old log entries which store
737 * parameters in a different format - the new
738 * LogFormatter classes have code to support formatting
742 class LegacyLogFormatter
extends LogFormatter
{
744 * Backward compatibility for extension changing the comment from
745 * the LogLine hook. This will be set by the first call on getComment(),
746 * then it might be modified by the hook when calling getActionLinks(),
747 * so that the modified value will be returned when calling getComment()
752 private $comment = null;
755 * Cache for the result of getActionLinks() so that it does not need to
756 * run multiple times depending on the order that getComment() and
757 * getActionLinks() are called.
761 private $revert = null;
763 public function getComment() {
764 if ( $this->comment
=== null ) {
765 $this->comment
= parent
::getComment();
768 // Make sure we execute the LogLine hook so that we immediately return
769 // the correct value.
770 if ( $this->revert
=== null ) {
771 $this->getActionLinks();
774 return $this->comment
;
777 protected function getActionMessage() {
778 $entry = $this->entry
;
779 $action = LogPage
::actionText(
781 $entry->getSubtype(),
783 $this->plaintext ?
null : $this->context
->getSkin(),
784 (array)$entry->getParameters(),
785 !$this->plaintext
// whether to filter [[]] links
788 $performer = $this->getPerformerElement();
789 if ( !$this->irctext
) {
790 $sep = $this->msg( 'word-separator' );
791 $sep = $this->plaintext ?
$sep->text() : $sep->escaped();
792 $action = $performer . $sep . $action;
798 public function getActionLinks() {
799 if ( $this->revert
!== null ) {
800 return $this->revert
;
803 if ( $this->entry
->isDeleted( LogPage
::DELETED_ACTION
) ) {
805 return $this->revert
;
808 $title = $this->entry
->getTarget();
809 $type = $this->entry
->getType();
810 $subtype = $this->entry
->getSubtype();
812 if ( $type == 'protect'
813 && ( $subtype == 'protect' ||
$subtype == 'modify' ||
$subtype == 'unprotect' )
816 Linker
::link( $title,
817 $this->msg( 'hist' )->escaped(),
820 'action' => 'history',
821 'offset' => $this->entry
->getTimestamp()
825 if ( $this->context
->getUser()->isAllowed( 'protect' ) ) {
826 $links[] = Linker
::linkKnown(
828 $this->msg( 'protect_change' )->escaped(),
830 array( 'action' => 'protect' )
834 return $this->msg( 'parentheses' )->rawParams(
835 $this->context
->getLanguage()->pipeList( $links ) )->escaped();
838 // Do nothing. The implementation is handled by the hook modifiying the
839 // passed-by-ref parameters. This also changes the default value so that
840 // getComment() and getActionLinks() do not call them indefinitely.
843 // This is to populate the $comment member of this instance so that it
844 // can be modified when calling the hook just below.
845 if ( $this->comment
=== null ) {
849 $params = $this->entry
->getParameters();
851 Hooks
::run( 'LogLine', array( $type, $subtype, $title, $params,
852 &$this->comment
, &$this->revert
, $this->entry
->getTimestamp() ) );
854 return $this->revert
;