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 bool Whether to output user tool links */
84 protected $linkFlood = false;
87 * Set to true if we are constructing a message text that is going to
88 * be included in page history or send to IRC feed. Links are replaced
89 * with plaintext or with [[pagename]] kind of syntax, that is parsed
90 * by page histories and IRC feeds.
93 protected $plaintext = false;
96 protected $irctext = false;
98 protected function __construct( LogEntry
$entry ) {
99 $this->entry
= $entry;
100 $this->context
= RequestContext
::getMain();
104 * Replace the default context
105 * @param IContextSource $context
107 public function setContext( IContextSource
$context ) {
108 $this->context
= $context;
112 * Set the visibility restrictions for displaying content.
113 * If set to public, and an item is deleted, then it will be replaced
114 * with a placeholder even if the context user is allowed to view it.
115 * @param int $audience Const self::FOR_THIS_USER or self::FOR_PUBLIC
117 public function setAudience( $audience ) {
118 $this->audience
= ( $audience == self
::FOR_THIS_USER
)
119 ? self
::FOR_THIS_USER
124 * Check if a log item can be displayed
125 * @param int $field LogPage::DELETED_* constant
128 protected function canView( $field ) {
129 if ( $this->audience
== self
::FOR_THIS_USER
) {
130 return LogEventsList
::userCanBitfield(
131 $this->entry
->getDeleted(), $field, $this->context
->getUser() );
133 return !$this->entry
->isDeleted( $field );
138 * If set to true, will produce user tool links after
139 * the user name. This should be replaced with generic
143 public function setShowUserToolLinks( $value ) {
144 $this->linkFlood
= $value;
148 * Ugly hack to produce plaintext version of the message.
149 * Usually you also want to set extraneous request context
150 * to avoid formatting for any particular user.
151 * @see getActionText()
152 * @return string Plain text
154 public function getPlainActionText() {
155 $this->plaintext
= true;
156 $text = $this->getActionText();
157 $this->plaintext
= false;
163 * Even uglier hack to maintain backwards compatibilty with IRC bots
165 * @see getActionText()
166 * @return string Text
168 public function getIRCActionComment() {
169 $actionComment = $this->getIRCActionText();
170 $comment = $this->entry
->getComment();
172 if ( $comment != '' ) {
173 if ( $actionComment == '' ) {
174 $actionComment = $comment;
176 $actionComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $comment;
180 return $actionComment;
184 * Even uglier hack to maintain backwards compatibilty with IRC bots
186 * @see getActionText()
187 * @return string Text
189 public function getIRCActionText() {
190 $this->plaintext
= true;
191 $this->irctext
= true;
193 $entry = $this->entry
;
194 $parameters = $entry->getParameters();
195 // @see LogPage::actionText()
196 // Text of title the action is aimed at.
197 $target = $entry->getTarget()->getPrefixedText();
199 switch ( $entry->getType() ) {
201 switch ( $entry->getSubtype() ) {
203 $movesource = $parameters['4::target'];
204 $text = wfMessage( '1movedto2' )
205 ->rawParams( $target, $movesource )->inContentLanguage()->escaped();
208 $movesource = $parameters['4::target'];
209 $text = wfMessage( '1movedto2_redir' )
210 ->rawParams( $target, $movesource )->inContentLanguage()->escaped();
212 case 'move-noredirect':
214 case 'move_redir-noredirect':
220 switch ( $entry->getSubtype() ) {
222 $text = wfMessage( 'deletedarticle' )
223 ->rawParams( $target )->inContentLanguage()->escaped();
226 $text = wfMessage( 'undeletedarticle' )
227 ->rawParams( $target )->inContentLanguage()->escaped();
229 // @codingStandardsIgnoreStart Long line
230 //case 'revision': // Revision deletion
231 //case 'event': // Log deletion
232 // see https://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/includes/LogPage.php?&pathrev=97044&r1=97043&r2=97044
234 // @codingStandardsIgnoreEnd
239 // @codingStandardsIgnoreStart Long line
240 // https://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/includes/PatrolLog.php?&pathrev=97495&r1=97494&r2=97495
241 // @codingStandardsIgnoreEnd
242 // Create a diff link to the patrolled revision
243 if ( $entry->getSubtype() === 'patrol' ) {
244 $diffLink = htmlspecialchars(
245 wfMessage( 'patrol-log-diff', $parameters['4::curid'] )
246 ->inContentLanguage()->text() );
247 $text = wfMessage( 'patrol-log-line', $diffLink, "[[$target]]", "" )
248 ->inContentLanguage()->text();
255 switch ( $entry->getSubtype() ) {
257 $text = wfMessage( 'protectedarticle' )
258 ->rawParams( $target . ' ' . $parameters[0] )->inContentLanguage()->escaped();
261 $text = wfMessage( 'unprotectedarticle' )
262 ->rawParams( $target )->inContentLanguage()->escaped();
265 $text = wfMessage( 'modifiedarticleprotection' )
266 ->rawParams( $target . ' ' . $parameters[0] )->inContentLanguage()->escaped();
272 switch ( $entry->getSubtype() ) {
275 $text = wfMessage( 'newuserlog-create-entry' )
276 ->inContentLanguage()->escaped();
280 $text = wfMessage( 'newuserlog-create2-entry' )
281 ->rawParams( $target )->inContentLanguage()->escaped();
284 $text = wfMessage( 'newuserlog-autocreate-entry' )
285 ->inContentLanguage()->escaped();
291 switch ( $entry->getSubtype() ) {
293 $text = wfMessage( 'uploadedimage' )
294 ->rawParams( $target )->inContentLanguage()->escaped();
297 $text = wfMessage( 'overwroteimage' )
298 ->rawParams( $target )->inContentLanguage()->escaped();
304 if ( count( $parameters['4::oldgroups'] ) ) {
305 $oldgroups = implode( ', ', $parameters['4::oldgroups'] );
307 $oldgroups = wfMessage( 'rightsnone' )->inContentLanguage()->escaped();
309 if ( count( $parameters['5::newgroups'] ) ) {
310 $newgroups = implode( ', ', $parameters['5::newgroups'] );
312 $newgroups = wfMessage( 'rightsnone' )->inContentLanguage()->escaped();
314 switch ( $entry->getSubtype() ) {
316 $text = wfMessage( 'rightslogentry' )
317 ->rawParams( $target, $oldgroups, $newgroups )->inContentLanguage()->escaped();
320 $text = wfMessage( 'rightslogentry-autopromote' )
321 ->rawParams( $target, $oldgroups, $newgroups )->inContentLanguage()->escaped();
327 $text = wfMessage( 'pagemerge-logentry' )
328 ->rawParams( $target, $parameters['4::dest'], $parameters['5::mergepoint'] )
329 ->inContentLanguage()->escaped();
333 switch ( $entry->getSubtype() ) {
336 $duration = $wgContLang->translateBlockExpiry( $parameters['5::duration'] );
337 $flags = BlockLogFormatter
::formatBlockFlags( $parameters['6::flags'], $wgContLang );
338 $text = wfMessage( 'blocklogentry' )
339 ->rawParams( $target, $duration, $flags )->inContentLanguage()->escaped();
342 $text = wfMessage( 'unblocklogentry' )
343 ->rawParams( $target )->inContentLanguage()->escaped();
347 $duration = $wgContLang->translateBlockExpiry( $parameters['5::duration'] );
348 $flags = BlockLogFormatter
::formatBlockFlags( $parameters['6::flags'], $wgContLang );
349 $text = wfMessage( 'reblock-logentry' )
350 ->rawParams( $target, $duration, $flags )->inContentLanguage()->escaped();
354 // case 'suppress' --private log -- aaron (so we know who to blame in a few years :-D)
357 if ( is_null( $text ) ) {
358 $text = $this->getPlainActionText();
361 $this->plaintext
= false;
362 $this->irctext
= false;
368 * Gets the log action, including username.
369 * @return string HTML
371 public function getActionText() {
372 if ( $this->canView( LogPage
::DELETED_ACTION
) ) {
373 $element = $this->getActionMessage();
374 if ( $element instanceof Message
) {
375 $element = $this->plaintext ?
$element->text() : $element->escaped();
377 if ( $this->entry
->isDeleted( LogPage
::DELETED_ACTION
) ) {
378 $element = $this->styleRestricedElement( $element );
381 $sep = $this->msg( 'word-separator' );
382 $sep = $this->plaintext ?
$sep->text() : $sep->escaped();
383 $performer = $this->getPerformerElement();
384 $element = $performer . $sep . $this->getRestrictedElement( 'rev-deleted-event' );
391 * Returns a sentence describing the log action. Usually
392 * a Message object is returned, but old style log types
393 * and entries might return pre-escaped HTML string.
394 * @return Message|string Pre-escaped HTML
396 protected function getActionMessage() {
397 $message = $this->msg( $this->getMessageKey() );
398 $message->params( $this->getMessageParameters() );
404 * Returns a key to be used for formatting the action sentence.
405 * Default is logentry-TYPE-SUBTYPE for modern logs. Legacy log
406 * types will use custom keys, and subclasses can also alter the
407 * key depending on the entry itself.
408 * @return string Message key
410 protected function getMessageKey() {
411 $type = $this->entry
->getType();
412 $subtype = $this->entry
->getSubtype();
414 return "logentry-$type-$subtype";
418 * Returns extra links that comes after the action text, like "revert", etc.
422 public function getActionLinks() {
427 * Extracts the optional extra parameters for use in action messages.
428 * The array indexes start from number 3.
431 protected function extractParameters() {
432 $entry = $this->entry
;
435 if ( $entry->isLegacy() ) {
436 foreach ( $entry->getParameters() as $index => $value ) {
437 $params[$index +
3] = $value;
441 // Filter out parameters which are not in format #:foo
442 foreach ( $entry->getParameters() as $key => $value ) {
443 if ( strpos( $key, ':' ) === false ) {
446 list( $index, $type, ) = explode( ':', $key, 3 );
447 $params[$index - 1] = $this->formatParameterValue( $type, $value );
450 /* Message class doesn't like non consecutive numbering.
451 * Fill in missing indexes with empty strings to avoid
452 * incorrect renumbering.
454 if ( count( $params ) ) {
455 $max = max( array_keys( $params ) );
456 for ( $i = 4; $i < $max; $i++
) {
457 if ( !isset( $params[$i] ) ) {
467 * Formats parameters intented for action message from
468 * array of all parameters. There are three hardcoded
469 * parameters (array is zero-indexed, this list not):
470 * - 1: user name with premade link
471 * - 2: usable for gender magic function
472 * - 3: target page with premade link
475 protected function getMessageParameters() {
476 if ( isset( $this->parsedParameters
) ) {
477 return $this->parsedParameters
;
480 $entry = $this->entry
;
481 $params = $this->extractParameters();
482 $params[0] = Message
::rawParam( $this->getPerformerElement() );
483 $params[1] = $this->canView( LogPage
::DELETED_USER
) ?
$entry->getPerformer()->getName() : '';
484 $params[2] = Message
::rawParam( $this->makePageLink( $entry->getTarget() ) );
486 // Bad things happens if the numbers are not in correct order
489 $this->parsedParameters
= $params;
490 return $this->parsedParameters
;
494 * Formats parameters values dependent to their type
495 * @param string $type The type of the value.
496 * Valid are currently:
497 * * - (empty) or plain: The value is returned as-is
498 * * raw: The value will be added to the log message
499 * as raw parameter (e.g. no escaping)
500 * Use this only if there is no other working
501 * type like user-link or title-link
502 * * msg: The value is a message-key, the output is
503 * the message in user language
504 * * msg-content: The value is a message-key, the output
505 * is the message in content language
506 * * user: The value is a user name, e.g. for GENDER
507 * * user-link: The value is a user name, returns a
509 * * title: The value is a page title,
510 * returns name of page
511 * * title-link: The value is a page title,
512 * returns link to this page
513 * * number: Format value as number
514 * @param string $value The parameter value that should
516 * @return string|array Formated value
519 protected function formatParameterValue( $type, $value ) {
520 $saveLinkFlood = $this->linkFlood
;
522 switch ( strtolower( trim( $type ) ) ) {
524 $value = Message
::rawParam( $value );
527 $value = $this->msg( $value )->text();
530 $value = $this->msg( $value )->inContentLanguage()->text();
533 $value = Message
::numParam( $value );
536 $user = User
::newFromName( $value );
537 $value = $user->getName();
540 $this->setShowUserToolLinks( false );
542 $user = User
::newFromName( $value );
543 $value = Message
::rawParam( $this->makeUserLink( $user ) );
545 $this->setShowUserToolLinks( $saveLinkFlood );
548 $title = Title
::newFromText( $value );
549 $value = $title->getPrefixedText();
552 $title = Title
::newFromText( $value );
553 $value = Message
::rawParam( $this->makePageLink( $title ) );
556 // Plain text, nothing to do
558 // Catch other types and use the old behavior (return as-is)
565 * Helper to make a link to the page, taking the plaintext
566 * value in consideration.
567 * @param Title $title The page
568 * @param array $parameters Query parameters
569 * @throws MWException
572 protected function makePageLink( Title
$title = null, $parameters = array() ) {
573 if ( !$this->plaintext
) {
574 $link = Linker
::link( $title, null, array(), $parameters );
576 if ( !$title instanceof Title
) {
577 throw new MWException( "Expected title, got null" );
579 $link = '[[' . $title->getPrefixedText() . ']]';
586 * Provides the name of the user who performed the log action.
587 * Used as part of log action message or standalone, depending
588 * which parts of the log entry has been hidden.
591 public function getPerformerElement() {
592 if ( $this->canView( LogPage
::DELETED_USER
) ) {
593 $performer = $this->entry
->getPerformer();
594 $element = $this->makeUserLink( $performer );
595 if ( $this->entry
->isDeleted( LogPage
::DELETED_USER
) ) {
596 $element = $this->styleRestricedElement( $element );
599 $element = $this->getRestrictedElement( 'rev-deleted-user' );
606 * Gets the user provided comment
607 * @return string HTML
609 public function getComment() {
610 if ( $this->canView( LogPage
::DELETED_COMMENT
) ) {
611 $comment = Linker
::commentBlock( $this->entry
->getComment() );
612 // No hard coded spaces thanx
613 $element = ltrim( $comment );
614 if ( $this->entry
->isDeleted( LogPage
::DELETED_COMMENT
) ) {
615 $element = $this->styleRestricedElement( $element );
618 $element = $this->getRestrictedElement( 'rev-deleted-comment' );
625 * Helper method for displaying restricted element.
626 * @param string $message
627 * @return string HTML or wiki text
629 protected function getRestrictedElement( $message ) {
630 if ( $this->plaintext
) {
631 return $this->msg( $message )->text();
634 $content = $this->msg( $message )->escaped();
635 $attribs = array( 'class' => 'history-deleted' );
637 return Html
::rawElement( 'span', $attribs, $content );
641 * Helper method for styling restricted element.
642 * @param string $content
643 * @return string HTML or wiki text
645 protected function styleRestricedElement( $content ) {
646 if ( $this->plaintext
) {
649 $attribs = array( 'class' => 'history-deleted' );
651 return Html
::rawElement( 'span', $attribs, $content );
655 * Shortcut for wfMessage which honors local context.
659 protected function msg( $key ) {
660 return $this->context
->msg( $key );
663 protected function makeUserLink( User
$user, $toolFlags = 0 ) {
664 if ( $this->plaintext
) {
665 $element = $user->getName();
667 $element = Linker
::userLink(
672 if ( $this->linkFlood
) {
673 $element .= Linker
::userToolLinks(
676 true, // redContribsWhenNoEdits
678 $user->getEditCount()
687 * @return array Array of titles that should be preloaded with LinkBatch
689 public function getPreloadTitles() {
694 * @return array Output of getMessageParameters() for testing
696 public function getMessageParametersForTesting() {
697 // This function was added because getMessageParameters() is
698 // protected and a change from protected to public caused
699 // problems with extensions
700 return $this->getMessageParameters();
705 * This class formats all log entries for log types
706 * which have not been converted to the new system.
707 * This is not about old log entries which store
708 * parameters in a different format - the new
709 * LogFormatter classes have code to support formatting
713 class LegacyLogFormatter
extends LogFormatter
{
715 * Backward compatibility for extension changing the comment from
716 * the LogLine hook. This will be set by the first call on getComment(),
717 * then it might be modified by the hook when calling getActionLinks(),
718 * so that the modified value will be returned when calling getComment()
723 private $comment = null;
726 * Cache for the result of getActionLinks() so that it does not need to
727 * run multiple times depending on the order that getComment() and
728 * getActionLinks() are called.
732 private $revert = null;
734 public function getComment() {
735 if ( $this->comment
=== null ) {
736 $this->comment
= parent
::getComment();
739 // Make sure we execute the LogLine hook so that we immediately return
740 // the correct value.
741 if ( $this->revert
=== null ) {
742 $this->getActionLinks();
745 return $this->comment
;
748 protected function getActionMessage() {
749 $entry = $this->entry
;
750 $action = LogPage
::actionText(
752 $entry->getSubtype(),
754 $this->plaintext ?
null : $this->context
->getSkin(),
755 (array)$entry->getParameters(),
756 !$this->plaintext
// whether to filter [[]] links
759 $performer = $this->getPerformerElement();
760 if ( !$this->irctext
) {
761 $sep = $this->msg( 'word-separator' );
762 $sep = $this->plaintext ?
$sep->text() : $sep->escaped();
763 $action = $performer . $sep . $action;
769 public function getActionLinks() {
770 if ( $this->revert
!== null ) {
771 return $this->revert
;
774 if ( $this->entry
->isDeleted( LogPage
::DELETED_ACTION
) ) {
776 return $this->revert
;
779 $title = $this->entry
->getTarget();
780 $type = $this->entry
->getType();
781 $subtype = $this->entry
->getSubtype();
783 if ( $type == 'protect'
784 && ( $subtype == 'protect' ||
$subtype == 'modify' ||
$subtype == 'unprotect' )
787 Linker
::link( $title,
788 $this->msg( 'hist' )->escaped(),
791 'action' => 'history',
792 'offset' => $this->entry
->getTimestamp()
796 if ( $this->context
->getUser()->isAllowed( 'protect' ) ) {
797 $links[] = Linker
::linkKnown(
799 $this->msg( 'protect_change' )->escaped(),
801 array( 'action' => 'protect' )
805 return $this->msg( 'parentheses' )->rawParams(
806 $this->context
->getLanguage()->pipeList( $links ) )->escaped();
809 // Do nothing. The implementation is handled by the hook modifiying the
810 // passed-by-ref parameters. This also changes the default value so that
811 // getComment() and getActionLinks() do not call them indefinitely.
814 // This is to populate the $comment member of this instance so that it
815 // can be modified when calling the hook just below.
816 if ( $this->comment
=== null ) {
820 $params = $this->entry
->getParameters();
822 Hooks
::run( 'LogLine', array( $type, $subtype, $title, $params,
823 &$this->comment
, &$this->revert
, $this->entry
->getTimestamp() ) );
825 return $this->revert
;