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 $entry LogEntry
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 ) );
80 /// Integer constant for handling log_deleted
81 protected $audience = self
::FOR_PUBLIC
;
83 /// 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;
95 protected $irctext = false;
97 protected function __construct( LogEntry
$entry ) {
98 $this->entry
= $entry;
99 $this->context
= RequestContext
::getMain();
103 * Replace the default context
104 * @param $context IContextSource
106 public function setContext( IContextSource
$context ) {
107 $this->context
= $context;
111 * Set the visibility restrictions for displaying content.
112 * If set to public, and an item is deleted, then it will be replaced
113 * with a placeholder even if the context user is allowed to view it.
114 * @param $audience integer self::FOR_THIS_USER or self::FOR_PUBLIC
116 public function setAudience( $audience ) {
117 $this->audience
= ( $audience == self
::FOR_THIS_USER
)
118 ? self
::FOR_THIS_USER
123 * Check if a log item can be displayed
124 * @param $field integer LogPage::DELETED_* constant
127 protected function canView( $field ) {
128 if ( $this->audience
== self
::FOR_THIS_USER
) {
129 return LogEventsList
::userCanBitfield(
130 $this->entry
->getDeleted(), $field, $this->context
->getUser() );
132 return !$this->entry
->isDeleted( $field );
137 * If set to true, will produce user tool links after
138 * the user name. This should be replaced with generic
140 * @param $value boolean
142 public function setShowUserToolLinks( $value ) {
143 $this->linkFlood
= $value;
147 * Ugly hack to produce plaintext version of the message.
148 * Usually you also want to set extraneous request context
149 * to avoid formatting for any particular user.
150 * @see getActionText()
151 * @return string text
153 public function getPlainActionText() {
154 $this->plaintext
= true;
155 $text = $this->getActionText();
156 $this->plaintext
= false;
162 * Even uglier hack to maintain backwards compatibilty with IRC bots
164 * @see getActionText()
165 * @return string text
167 public function getIRCActionComment() {
168 $actionComment = $this->getIRCActionText();
169 $comment = $this->entry
->getComment();
171 if ( $comment != '' ) {
172 if ( $actionComment == '' ) {
173 $actionComment = $comment;
175 $actionComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $comment;
179 return $actionComment;
183 * Even uglier hack to maintain backwards compatibilty with IRC bots
185 * @see getActionText()
186 * @return string text
188 public function getIRCActionText() {
189 $this->plaintext
= true;
190 $this->irctext
= true;
192 $entry = $this->entry
;
193 $parameters = $entry->getParameters();
194 // @see LogPage::actionText()
195 // Text of title the action is aimed at.
196 $target = $entry->getTarget()->getPrefixedText();
198 switch ( $entry->getType() ) {
200 switch ( $entry->getSubtype() ) {
202 $movesource = $parameters['4::target'];
203 $text = wfMessage( '1movedto2' )
204 ->rawParams( $target, $movesource )->inContentLanguage()->escaped();
207 $movesource = $parameters['4::target'];
208 $text = wfMessage( '1movedto2_redir' )
209 ->rawParams( $target, $movesource )->inContentLanguage()->escaped();
211 case 'move-noredirect':
213 case 'move_redir-noredirect':
219 switch ( $entry->getSubtype() ) {
221 $text = wfMessage( 'deletedarticle' )
222 ->rawParams( $target )->inContentLanguage()->escaped();
225 $text = wfMessage( 'undeletedarticle' )
226 ->rawParams( $target )->inContentLanguage()->escaped();
228 //case 'revision': // Revision deletion
229 //case 'event': // Log deletion
230 // see https://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/includes/LogPage.php?&pathrev=97044&r1=97043&r2=97044
236 // https://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/includes/PatrolLog.php?&pathrev=97495&r1=97494&r2=97495
237 // Create a diff link to the patrolled revision
238 if ( $entry->getSubtype() === 'patrol' ) {
239 $diffLink = htmlspecialchars(
240 wfMessage( 'patrol-log-diff', $parameters['4::curid'] )
241 ->inContentLanguage()->text() );
242 $text = wfMessage( 'patrol-log-line', $diffLink, "[[$target]]", "" )
243 ->inContentLanguage()->text();
250 switch ( $entry->getSubtype() ) {
252 $text = wfMessage( 'protectedarticle' )
253 ->rawParams( $target . ' ' . $parameters[0] )->inContentLanguage()->escaped();
256 $text = wfMessage( 'unprotectedarticle' )
257 ->rawParams( $target )->inContentLanguage()->escaped();
260 $text = wfMessage( 'modifiedarticleprotection' )
261 ->rawParams( $target . ' ' . $parameters[0] )->inContentLanguage()->escaped();
267 switch ( $entry->getSubtype() ) {
270 $text = wfMessage( 'newuserlog-create-entry' )
271 ->inContentLanguage()->escaped();
275 $text = wfMessage( 'newuserlog-create2-entry' )
276 ->rawParams( $target )->inContentLanguage()->escaped();
279 $text = wfMessage( 'newuserlog-autocreate-entry' )
280 ->inContentLanguage()->escaped();
286 switch ( $entry->getSubtype() ) {
288 $text = wfMessage( 'uploadedimage' )
289 ->rawParams( $target )->inContentLanguage()->escaped();
292 $text = wfMessage( 'overwroteimage' )
293 ->rawParams( $target )->inContentLanguage()->escaped();
299 if ( count( $parameters['4::oldgroups'] ) ) {
300 $oldgroups = implode( ', ', $parameters['4::oldgroups'] );
302 $oldgroups = wfMessage( 'rightsnone' )->inContentLanguage()->escaped();
304 if ( count( $parameters['5::newgroups'] ) ) {
305 $newgroups = implode( ', ', $parameters['5::newgroups'] );
307 $newgroups = wfMessage( 'rightsnone' )->inContentLanguage()->escaped();
309 switch ( $entry->getSubtype() ) {
311 $text = wfMessage( 'rightslogentry' )
312 ->rawParams( $target, $oldgroups, $newgroups )->inContentLanguage()->escaped();
315 $text = wfMessage( 'rightslogentry-autopromote' )
316 ->rawParams( $target, $oldgroups, $newgroups )->inContentLanguage()->escaped();
320 // case 'suppress' --private log -- aaron (sign your messages so we know who to blame in a few years :-D)
323 if ( is_null( $text ) ) {
324 $text = $this->getPlainActionText();
327 $this->plaintext
= false;
328 $this->irctext
= false;
334 * Gets the log action, including username.
335 * @return string HTML
337 public function getActionText() {
338 if ( $this->canView( LogPage
::DELETED_ACTION
) ) {
339 $element = $this->getActionMessage();
340 if ( $element instanceof Message
) {
341 $element = $this->plaintext ?
$element->text() : $element->escaped();
343 if ( $this->entry
->isDeleted( LogPage
::DELETED_ACTION
) ) {
344 $element = $this->styleRestricedElement( $element );
347 $performer = $this->getPerformerElement() . $this->msg( 'word-separator' )->text();
348 $element = $performer . $this->getRestrictedElement( 'rev-deleted-event' );
355 * Returns a sentence describing the log action. Usually
356 * a Message object is returned, but old style log types
357 * and entries might return pre-escaped html string.
358 * @return Message|string pre-escaped html
360 protected function getActionMessage() {
361 $message = $this->msg( $this->getMessageKey() );
362 $message->params( $this->getMessageParameters() );
368 * Returns a key to be used for formatting the action sentence.
369 * Default is logentry-TYPE-SUBTYPE for modern logs. Legacy log
370 * types will use custom keys, and subclasses can also alter the
371 * key depending on the entry itself.
372 * @return string message key
374 protected function getMessageKey() {
375 $type = $this->entry
->getType();
376 $subtype = $this->entry
->getSubtype();
378 return "logentry-$type-$subtype";
382 * Returns extra links that comes after the action text, like "revert", etc.
386 public function getActionLinks() {
391 * Extracts the optional extra parameters for use in action messages.
392 * The array indexes start from number 3.
395 protected function extractParameters() {
396 $entry = $this->entry
;
399 if ( $entry->isLegacy() ) {
400 foreach ( $entry->getParameters() as $index => $value ) {
401 $params[$index +
3] = $value;
405 // Filter out parameters which are not in format #:foo
406 foreach ( $entry->getParameters() as $key => $value ) {
407 if ( strpos( $key, ':' ) === false ) {
410 list( $index, $type, ) = explode( ':', $key, 3 );
411 $params[$index - 1] = $this->formatParameterValue( $type, $value );
414 /* Message class doesn't like non consecutive numbering.
415 * Fill in missing indexes with empty strings to avoid
416 * incorrect renumbering.
418 if ( count( $params ) ) {
419 $max = max( array_keys( $params ) );
420 for ( $i = 4; $i < $max; $i++
) {
421 if ( !isset( $params[$i] ) ) {
431 * Formats parameters intented for action message from
432 * array of all parameters. There are three hardcoded
433 * parameters (array is zero-indexed, this list not):
434 * - 1: user name with premade link
435 * - 2: usable for gender magic function
436 * - 3: target page with premade link
439 protected function getMessageParameters() {
440 if ( isset( $this->parsedParameters
) ) {
441 return $this->parsedParameters
;
444 $entry = $this->entry
;
445 $params = $this->extractParameters();
446 $params[0] = Message
::rawParam( $this->getPerformerElement() );
447 $params[1] = $this->canView( LogPage
::DELETED_USER
) ?
$entry->getPerformer()->getName() : '';
448 $params[2] = Message
::rawParam( $this->makePageLink( $entry->getTarget() ) );
450 // Bad things happens if the numbers are not in correct order
453 return $this->parsedParameters
= $params;
457 * Formats parameters values dependent to their type
458 * @param string $type The type of the value.
459 * Valid are currently:
460 * * - (empty) or plain: The value is returned as-is
461 * * raw: The value will be added to the log message
462 * as raw parameter (e.g. no escaping)
463 * Use this only if there is no other working
464 * type like user-link or title-link
465 * * msg: The value is a message-key, the output is
466 * the message in user language
467 * * msg-content: The value is a message-key, the output
468 * is the message in content language
469 * * user: The value is a user name, e.g. for GENDER
470 * * user-link: The value is a user name, returns a
472 * * title: The value is a page title,
473 * returns name of page
474 * * title-link: The value is a page title,
475 * returns link to this page
476 * * number: Format value as number
477 * @param string $value The parameter value that should
479 * @return string or Message::numParam or Message::rawParam
483 protected function formatParameterValue( $type, $value ) {
484 $saveLinkFlood = $this->linkFlood
;
486 switch ( strtolower( trim( $type ) ) ) {
488 $value = Message
::rawParam( $value );
491 $value = $this->msg( $value )->text();
494 $value = $this->msg( $value )->inContentLanguage()->text();
497 $value = Message
::numParam( $value );
500 $user = User
::newFromName( $value );
501 $value = $user->getName();
504 $this->setShowUserToolLinks( false );
506 $user = User
::newFromName( $value );
507 $value = Message
::rawParam( $this->makeUserLink( $user ) );
509 $this->setShowUserToolLinks( $saveLinkFlood );
512 $title = Title
::newFromText( $value );
513 $value = $title->getPrefixedText();
516 $title = Title
::newFromText( $value );
517 $value = Message
::rawParam( $this->makePageLink( $title ) );
520 // Plain text, nothing to do
522 // Catch other types and use the old behavior (return as-is)
529 * Helper to make a link to the page, taking the plaintext
530 * value in consideration.
531 * @param $title Title the page
532 * @param array $parameters query parameters
533 * @throws MWException
536 protected function makePageLink( Title
$title = null, $parameters = array() ) {
537 if ( !$this->plaintext
) {
538 $link = Linker
::link( $title, null, array(), $parameters );
540 if ( !$title instanceof Title
) {
541 throw new MWException( "Expected title, got null" );
543 $link = '[[' . $title->getPrefixedText() . ']]';
550 * Provides the name of the user who performed the log action.
551 * Used as part of log action message or standalone, depending
552 * which parts of the log entry has been hidden.
555 public function getPerformerElement() {
556 if ( $this->canView( LogPage
::DELETED_USER
) ) {
557 $performer = $this->entry
->getPerformer();
558 $element = $this->makeUserLink( $performer );
559 if ( $this->entry
->isDeleted( LogPage
::DELETED_USER
) ) {
560 $element = $this->styleRestricedElement( $element );
563 $element = $this->getRestrictedElement( 'rev-deleted-user' );
570 * Gets the luser provided comment
571 * @return string HTML
573 public function getComment() {
574 if ( $this->canView( LogPage
::DELETED_COMMENT
) ) {
575 $comment = Linker
::commentBlock( $this->entry
->getComment() );
576 // No hard coded spaces thanx
577 $element = ltrim( $comment );
578 if ( $this->entry
->isDeleted( LogPage
::DELETED_COMMENT
) ) {
579 $element = $this->styleRestricedElement( $element );
582 $element = $this->getRestrictedElement( 'rev-deleted-comment' );
589 * Helper method for displaying restricted element.
590 * @param $message string
591 * @return string HTML or wikitext
593 protected function getRestrictedElement( $message ) {
594 if ( $this->plaintext
) {
595 return $this->msg( $message )->text();
598 $content = $this->msg( $message )->escaped();
599 $attribs = array( 'class' => 'history-deleted' );
601 return Html
::rawElement( 'span', $attribs, $content );
605 * Helper method for styling restricted element.
606 * @param $content string
607 * @return string HTML or wikitext
609 protected function styleRestricedElement( $content ) {
610 if ( $this->plaintext
) {
613 $attribs = array( 'class' => 'history-deleted' );
615 return Html
::rawElement( 'span', $attribs, $content );
619 * Shortcut for wfMessage which honors local context.
620 * @todo Would it be better to require replacing the global context instead?
624 protected function msg( $key ) {
625 return $this->context
->msg( $key );
628 protected function makeUserLink( User
$user ) {
629 if ( $this->plaintext
) {
630 $element = $user->getName();
632 $element = Linker
::userLink(
637 if ( $this->linkFlood
) {
638 $element .= Linker
::userToolLinksRedContribs(
641 $user->getEditCount()
650 * @return Array of titles that should be preloaded with LinkBatch.
652 public function getPreloadTitles() {
657 * @return Output of getMessageParameters() for testing
659 public function getMessageParametersForTesting() {
660 // This function was added because getMessageParameters() is
661 // protected and a change from protected to public caused
662 // problems with extensions
663 return $this->getMessageParameters();
668 * This class formats all log entries for log types
669 * which have not been converted to the new system.
670 * This is not about old log entries which store
671 * parameters in a different format - the new
672 * LogFormatter classes have code to support formatting
676 class LegacyLogFormatter
extends LogFormatter
{
679 * Backward compatibility for extension changing the comment from
680 * the LogLine hook. This will be set by the first call on getComment(),
681 * then it might be modified by the hook when calling getActionLinks(),
682 * so that the modified value will be returned when calling getComment()
687 private $comment = null;
690 * Cache for the result of getActionLinks() so that it does not need to
691 * run multiple times depending on the order that getComment() and
692 * getActionLinks() are called.
696 private $revert = null;
698 public function getComment() {
699 if ( $this->comment
=== null ) {
700 $this->comment
= parent
::getComment();
703 // Make sure we execute the LogLine hook so that we immediately return
704 // the correct value.
705 if ( $this->revert
=== null ) {
706 $this->getActionLinks();
709 return $this->comment
;
712 protected function getActionMessage() {
713 $entry = $this->entry
;
714 $action = LogPage
::actionText(
716 $entry->getSubtype(),
718 $this->plaintext ?
null : $this->context
->getSkin(),
719 (array)$entry->getParameters(),
720 !$this->plaintext
// whether to filter [[]] links
723 $performer = $this->getPerformerElement();
724 if ( !$this->irctext
) {
725 $action = $performer . $this->msg( 'word-separator' )->text() . $action;
731 public function getActionLinks() {
732 if ( $this->revert
!== null ) {
733 return $this->revert
;
736 if ( $this->entry
->isDeleted( LogPage
::DELETED_ACTION
) ) {
737 return $this->revert
= '';
740 $title = $this->entry
->getTarget();
741 $type = $this->entry
->getType();
742 $subtype = $this->entry
->getSubtype();
744 // Show unblock/change block link
745 if ( ( $type == 'block' ||
$type == 'suppress' ) && ( $subtype == 'block' ||
$subtype == 'reblock' ) ) {
746 if ( !$this->context
->getUser()->isAllowed( 'block' ) ) {
752 SpecialPage
::getTitleFor( 'Unblock', $title->getDBkey() ),
753 $this->msg( 'unblocklink' )->escaped()
756 SpecialPage
::getTitleFor( 'Block', $title->getDBkey() ),
757 $this->msg( 'change-blocklink' )->escaped()
761 return $this->msg( 'parentheses' )->rawParams(
762 $this->context
->getLanguage()->pipeList( $links ) )->escaped();
763 // Show change protection link
764 } elseif ( $type == 'protect' && ( $subtype == 'protect' ||
$subtype == 'modify' ||
$subtype == 'unprotect' ) ) {
766 Linker
::link( $title,
767 $this->msg( 'hist' )->escaped(),
770 'action' => 'history',
771 'offset' => $this->entry
->getTimestamp()
775 if ( $this->context
->getUser()->isAllowed( 'protect' ) ) {
776 $links[] = Linker
::linkKnown(
778 $this->msg( 'protect_change' )->escaped(),
780 array( 'action' => 'protect' )
784 return $this->msg( 'parentheses' )->rawParams(
785 $this->context
->getLanguage()->pipeList( $links ) )->escaped();
787 } elseif ( $type == 'merge' && $subtype == 'merge' ) {
788 if ( !$this->context
->getUser()->isAllowed( 'mergehistory' ) ) {
792 $params = $this->extractParameters();
793 $revert = Linker
::linkKnown(
794 SpecialPage
::getTitleFor( 'MergeHistory' ),
795 $this->msg( 'revertmerge' )->escaped(),
798 'target' => $params[3],
799 'dest' => $title->getPrefixedDBkey(),
800 'mergepoint' => $params[4]
804 return $this->msg( 'parentheses' )->rawParams( $revert )->escaped();
807 // Do nothing. The implementation is handled by the hook modifiying the
808 // passed-by-ref parameters. This also changes the default value so that
809 // getComment() and getActionLinks() do not call them indefinitely.
812 // This is to populate the $comment member of this instance so that it
813 // can be modified when calling the hook just below.
814 if ( $this->comment
=== null ) {
818 $params = $this->entry
->getParameters();
820 wfRunHooks( 'LogLine', array( $type, $subtype, $title, $params,
821 &$this->comment
, &$this->revert
, $this->entry
->getTimestamp() ) );
823 return $this->revert
;