3 * Contains classes for formatting log entries
6 * @author Niklas Laxström
7 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
12 * Implements the default log formatting.
13 * Can be overridden by subclassing and setting
14 * $wgLogActionsHandlers['type/subtype'] = 'class'; or
15 * $wgLogActionsHandlers['type/*'] = 'class';
19 // Audience options for viewing usernames, comments, and actions
21 const FOR_THIS_USER
= 2;
26 * Constructs a new formatter suitable for given entry.
27 * @param $entry LogEntry
28 * @return LogFormatter
30 public static function newFromEntry( LogEntry
$entry ) {
31 global $wgLogActionsHandlers;
32 $fulltype = $entry->getFullType();
33 $wildcard = $entry->getType() . '/*';
36 if ( isset( $wgLogActionsHandlers[$fulltype] ) ) {
37 $handler = $wgLogActionsHandlers[$fulltype];
38 } elseif ( isset( $wgLogActionsHandlers[$wildcard] ) ) {
39 $handler = $wgLogActionsHandlers[$wildcard];
42 if ( $handler !== '' && is_string( $handler ) && class_exists( $handler ) ) {
43 return new $handler( $entry );
46 return new LegacyLogFormatter( $entry );
50 * Handy shortcut for constructing a formatter directly from
53 * @see DatabaseLogEntry::getSelectQueryData
54 * @return LogFormatter
56 public static function newFromRow( $row ) {
57 return self
::newFromEntry( DatabaseLogEntry
::newFromRow( $row ) );
65 /// Integer constant for handling log_deleted
66 protected $audience = self
::FOR_PUBLIC
;
68 /// Whether to output user tool links
69 protected $linkFlood = false;
72 * Set to true if we are constructing a message text that is going to
73 * be included in page history or send to IRC feed. Links are replaced
74 * with plaintext or with [[pagename]] kind of syntax, that is parsed
75 * by page histories and IRC feeds.
78 protected $plaintext = false;
80 protected $irctext = false;
82 protected function __construct( LogEntry
$entry ) {
83 $this->entry
= $entry;
84 $this->context
= RequestContext
::getMain();
88 * Replace the default context
89 * @param $context IContextSource
91 public function setContext( IContextSource
$context ) {
92 $this->context
= $context;
96 * Set the visibility restrictions for displaying content.
97 * If set to public, and an item is deleted, then it will be replaced
98 * with a placeholder even if the context user is allowed to view it.
99 * @param $audience integer self::FOR_THIS_USER or self::FOR_PUBLIC
101 public function setAudience( $audience ) {
102 $this->audience
= ( $audience == self
::FOR_THIS_USER
)
103 ? self
::FOR_THIS_USER
108 * Check if a log item can be displayed
109 * @param $field integer LogPage::DELETED_* constant
112 protected function canView( $field ) {
113 if ( $this->audience
== self
::FOR_THIS_USER
) {
114 return LogEventsList
::userCanBitfield(
115 $this->entry
->getDeleted(), $field, $this->context
->getUser() );
117 return !$this->entry
->isDeleted( $field );
122 * If set to true, will produce user tool links after
123 * the user name. This should be replaced with generic
125 * @param $value boolean
127 public function setShowUserToolLinks( $value ) {
128 $this->linkFlood
= $value;
132 * Ugly hack to produce plaintext version of the message.
133 * Usually you also want to set extraneous request context
134 * to avoid formatting for any particular user.
135 * @see getActionText()
136 * @return string text
138 public function getPlainActionText() {
139 $this->plaintext
= true;
140 $text = $this->getActionText();
141 $this->plaintext
= false;
146 * Even uglier hack to maintain backwards compatibilty with IRC bots
148 * @see getActionText()
149 * @return string text
151 public function getIRCActionComment() {
152 $actionComment = $this->getIRCActionText();
153 $comment = $this->entry
->getComment();
155 if ( $comment != '' ) {
156 if ( $actionComment == '' ) {
157 $actionComment = $comment;
159 $actionComment .= wfMsgForContent( 'colon-separator' ) . $comment;
163 return $actionComment;
167 * Even uglier hack to maintain backwards compatibilty with IRC bots
169 * @see getActionText()
170 * @return string text
172 public function getIRCActionText() {
173 $this->plaintext
= true;
174 $this->irctext
= true;
175 $text = $this->getActionText();
177 $entry = $this->entry
;
178 $parameters = $entry->getParameters();
179 // @see LogPage::actionText()
180 $msgOpts = array( 'parsemag', 'escape', 'replaceafter', 'content' );
181 // Text of title the action is aimed at.
182 $target = $entry->getTarget()->getPrefixedText() ;
184 switch( $entry->getType() ) {
186 switch( $entry->getSubtype() ) {
188 $movesource = $parameters['4::target'];
189 $text = wfMsgExt( '1movedto2', $msgOpts, $target, $movesource );
192 $movesource = $parameters['4::target'];
193 $text = wfMsgExt( '1movedto2_redir', $msgOpts, $target, $movesource );
195 case 'move-noredirect':
197 case 'move_redir-noredirect':
203 switch( $entry->getSubtype() ) {
205 $text = wfMsgExt( 'deletedarticle', $msgOpts, $target );
208 $text = wfMsgExt( 'undeletedarticle', $msgOpts, $target );
210 //case 'revision': // Revision deletion
211 //case 'event': // Log deletion
212 // see https://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/includes/LogPage.php?&pathrev=97044&r1=97043&r2=97044
218 // https://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/includes/PatrolLog.php?&pathrev=97495&r1=97494&r2=97495
219 // Create a diff link to the patrolled revision
220 if ( $entry->getSubtype() === 'patrol' ) {
221 $diffLink = htmlspecialchars(
222 wfMsgForContent( 'patrol-log-diff', $parameters['4::curid'] ) );
223 $text = wfMsgForContent( 'patrol-log-line', $diffLink, "[[$target]]", "" );
230 switch( $entry->getSubtype() ) {
233 $text = wfMsgExt( 'newuserlog-create-entry', $msgOpts /* no params */ );
236 $text = wfMsgExt( 'newuserlog-create2-entry', $msgOpts, $target );
239 $text = wfMsgExt( 'newuserlog-autocreate-entry', $msgOpts /* no params */ );
245 switch( $entry->getSubtype() ) {
247 $text = wfMsgExt( 'uploadedimage', $msgOpts, $target );
250 $text = wfMsgExt( 'overwroteimage', $msgOpts, $target );
256 // case 'suppress' --private log -- aaron (sign your messages so we know who to blame in a few years :-D)
259 if( is_null( $text ) ) {
260 $text = $this->getPlainActionText();
263 $this->plaintext
= false;
264 $this->irctext
= false;
269 * Gets the log action, including username.
270 * @return string HTML
272 public function getActionText() {
273 if ( $this->canView( LogPage
::DELETED_ACTION
) ) {
274 $element = $this->getActionMessage();
275 if ( $element instanceof Message
) {
276 $element = $this->plaintext ?
$element->text() : $element->escaped();
278 if ( $this->entry
->isDeleted( LogPage
::DELETED_ACTION
) ) {
279 $element = $this->styleRestricedElement( $element );
282 $performer = $this->getPerformerElement() . $this->msg( 'word-separator' )->text();
283 $element = $performer . $this->getRestrictedElement( 'rev-deleted-event' );
290 * Returns a sentence describing the log action. Usually
291 * a Message object is returned, but old style log types
292 * and entries might return pre-escaped html string.
293 * @return Message|string pre-escaped html
295 protected function getActionMessage() {
296 $message = $this->msg( $this->getMessageKey() );
297 $message->params( $this->getMessageParameters() );
302 * Returns a key to be used for formatting the action sentence.
303 * Default is logentry-TYPE-SUBTYPE for modern logs. Legacy log
304 * types will use custom keys, and subclasses can also alter the
305 * key depending on the entry itself.
306 * @return string message key
308 protected function getMessageKey() {
309 $type = $this->entry
->getType();
310 $subtype = $this->entry
->getSubtype();
312 return "logentry-$type-$subtype";
316 * Extracts the optional extra parameters for use in action messages.
317 * The array indexes start from number 3.
320 protected function extractParameters() {
321 $entry = $this->entry
;
324 if ( $entry->isLegacy() ) {
325 foreach ( $entry->getParameters() as $index => $value ) {
326 $params[$index +
3] = $value;
330 // Filter out parameters which are not in format #:foo
331 foreach ( $entry->getParameters() as $key => $value ) {
332 if ( strpos( $key, ':' ) === false ) continue;
333 list( $index, $type, $name ) = explode( ':', $key, 3 );
334 $params[$index - 1] = $value;
337 /* Message class doesn't like non consecutive numbering.
338 * Fill in missing indexes with empty strings to avoid
339 * incorrect renumbering.
341 if ( count( $params ) ) {
342 $max = max( array_keys( $params ) );
343 for ( $i = 4; $i < $max; $i++
) {
344 if ( !isset( $params[$i] ) ) {
353 * Formats parameters intented for action message from
354 * array of all parameters. There are three hardcoded
355 * parameters (array is zero-indexed, this list not):
356 * - 1: user name with premade link
357 * - 2: usable for gender magic function
358 * - 3: target page with premade link
361 protected function getMessageParameters() {
362 if ( isset( $this->parsedParameters
) ) {
363 return $this->parsedParameters
;
366 $entry = $this->entry
;
367 $params = $this->extractParameters();
368 $params[0] = Message
::rawParam( $this->getPerformerElement() );
369 $params[1] = $entry->getPerformer()->getName();
370 $params[2] = Message
::rawParam( $this->makePageLink( $entry->getTarget() ) );
372 // Bad things happens if the numbers are not in correct order
374 return $this->parsedParameters
= $params;
378 * Helper to make a link to the page, taking the plaintext
379 * value in consideration.
380 * @param $title Title the page
381 * @param $parameters array query parameters
384 protected function makePageLink( Title
$title = null, $parameters = array() ) {
385 if ( !$this->plaintext
) {
386 $link = Linker
::link( $title, null, array(), $parameters );
388 if ( !$title instanceof Title
) {
389 throw new MWException( "Expected title, got null" );
391 $link = '[[' . $title->getPrefixedText() . ']]';
397 * Provides the name of the user who performed the log action.
398 * Used as part of log action message or standalone, depending
399 * which parts of the log entry has been hidden.
402 public function getPerformerElement() {
403 if ( $this->canView( LogPage
::DELETED_USER
) ) {
404 $performer = $this->entry
->getPerformer();
405 $element = $this->makeUserLink( $performer );
406 if ( $this->entry
->isDeleted( LogPage
::DELETED_USER
) ) {
407 $element = $this->styleRestricedElement( $element );
410 $element = $this->getRestrictedElement( 'rev-deleted-user' );
417 * Gets the luser provided comment
418 * @return string HTML
420 public function getComment() {
421 if ( $this->canView( LogPage
::DELETED_COMMENT
) ) {
422 $comment = Linker
::commentBlock( $this->entry
->getComment() );
423 // No hard coded spaces thanx
424 $element = ltrim( $comment );
425 if ( $this->entry
->isDeleted( LogPage
::DELETED_COMMENT
) ) {
426 $element = $this->styleRestricedElement( $element );
429 $element = $this->getRestrictedElement( 'rev-deleted-comment' );
436 * Helper method for displaying restricted element.
437 * @param $message string
438 * @return string HTML or wikitext
440 protected function getRestrictedElement( $message ) {
441 if ( $this->plaintext
) {
442 return $this->msg( $message )->text();
445 $content = $this->msg( $message )->escaped();
446 $attribs = array( 'class' => 'history-deleted' );
447 return Html
::rawElement( 'span', $attribs, $content );
451 * Helper method for styling restricted element.
452 * @param $content string
453 * @return string HTML or wikitext
455 protected function styleRestricedElement( $content ) {
456 if ( $this->plaintext
) {
459 $attribs = array( 'class' => 'history-deleted' );
460 return Html
::rawElement( 'span', $attribs, $content );
464 * Shortcut for wfMessage which honors local context.
465 * @todo Would it be better to require replacing the global context instead?
469 protected function msg( $key ) {
470 return wfMessage( $key )
471 ->inLanguage( $this->context
->getLanguage() )
472 ->title( $this->context
->getTitle() );
475 protected function makeUserLink( User
$user ) {
476 if ( $this->plaintext
) {
477 $element = $user->getName();
479 $element = Linker
::userLink(
484 if ( $this->linkFlood
) {
485 $element .= Linker
::userToolLinks(
488 true, // Red if no edits
490 $user->getEditCount()
498 * @return Array of titles that should be preloaded with LinkBatch.
500 public function getPreloadTitles() {
507 * This class formats all log entries for log types
508 * which have not been converted to the new system.
509 * This is not about old log entries which store
510 * parameters in a different format - the new
511 * LogFormatter classes have code to support formatting
515 class LegacyLogFormatter
extends LogFormatter
{
516 protected function getActionMessage() {
517 $entry = $this->entry
;
518 $action = LogPage
::actionText(
520 $entry->getSubtype(),
522 $this->plaintext ?
null : $this->context
->getSkin(),
523 (array)$entry->getParameters(),
524 !$this->plaintext
// whether to filter [[]] links
527 $performer = $this->getPerformerElement();
528 if ( !$this->irctext
) {
529 $action = $performer . $this->msg( 'word-separator' )->text() . $action;
538 * This class formats move log entries.
541 class MoveLogFormatter
extends LogFormatter
{
542 public function getPreloadTitles() {
543 $params = $this->extractParameters();
544 return array( Title
::newFromText( $params[3] ) );
547 protected function getMessageKey() {
548 $key = parent
::getMessageKey();
549 $params = $this->getMessageParameters();
550 if ( isset( $params[4] ) && $params[4] === '1' ) {
551 $key .= '-noredirect';
556 protected function getMessageParameters() {
557 $params = parent
::getMessageParameters();
558 $oldname = $this->makePageLink( $this->entry
->getTarget(), array( 'redirect' => 'no' ) );
559 $newname = $this->makePageLink( Title
::newFromText( $params[3] ) );
560 $params[2] = Message
::rawParam( $oldname );
561 $params[3] = Message
::rawParam( $newname );
567 * This class formats delete log entries.
570 class DeleteLogFormatter
extends LogFormatter
{
571 protected function getMessageKey() {
572 $key = parent
::getMessageKey();
573 if ( in_array( $this->entry
->getSubtype(), array( 'event', 'revision' ) ) ) {
574 if ( count( $this->getMessageParameters() ) < 5 ) {
575 return "$key-legacy";
581 protected function getMessageParameters() {
582 if ( isset( $this->parsedParametersDeleteLog
) ) {
583 return $this->parsedParametersDeleteLog
;
586 $params = parent
::getMessageParameters();
587 $subtype = $this->entry
->getSubtype();
588 if ( in_array( $subtype, array( 'event', 'revision' ) ) ) {
590 ($subtype === 'event' && count( $params ) === 6 ) ||
591 ($subtype === 'revision' && isset( $params[3] ) && $params[3] === 'revision' )
593 $paramStart = $subtype === 'revision' ?
4 : 3;
595 $old = $this->parseBitField( $params[$paramStart+
1] );
596 $new = $this->parseBitField( $params[$paramStart+
2] );
597 list( $hid, $unhid, $extra ) = RevisionDeleter
::getChanges( $new, $old );
599 foreach ( $hid as $v ) {
600 $changes[] = $this->msg( "$v-hid" )->plain();
602 foreach ( $unhid as $v ) {
603 $changes[] = $this->msg( "$v-unhid" )->plain();
605 foreach ( $extra as $v ) {
606 $changes[] = $this->msg( $v )->plain();
608 $changeText = $this->context
->getLanguage()->listToText( $changes );
611 $newParams = array_slice( $params, 0, 3 );
612 $newParams[3] = $changeText;
613 $count = count( explode( ',', $params[$paramStart] ) );
614 $newParams[4] = $this->context
->getLanguage()->formatNum( $count );
615 return $this->parsedParametersDeleteLog
= $newParams;
617 return $this->parsedParametersDeleteLog
= array_slice( $params, 0, 3 );
621 return $this->parsedParametersDeleteLog
= $params;
624 protected function parseBitField( $string ) {
625 // Input is like ofield=2134 or just the number
626 if ( strpos( $string, 'field=' ) === 1 ) {
627 list( , $field ) = explode( '=', $string );
630 return (int) $string;
636 * This class formats patrol log entries.
639 class PatrolLogFormatter
extends LogFormatter
{
640 protected function getMessageKey() {
641 $key = parent
::getMessageKey();
642 $params = $this->getMessageParameters();
643 if ( isset( $params[5] ) && $params[5] ) {
649 protected function getMessageParameters() {
650 $params = parent
::getMessageParameters();
651 $newParams = array_slice( $params, 0, 3 );
653 $target = $this->entry
->getTarget();
655 $revision = $this->context
->getLanguage()->formatNum( $oldid, true );
657 if ( $this->plaintext
) {
658 $revlink = $revision;
659 } elseif ( $target->exists() ) {
664 $revlink = Linker
::link( $target, htmlspecialchars( $revision ), array(), $query );
666 $revlink = htmlspecialchars( $revision );
669 $newParams[3] = Message
::rawParam( $revlink );
675 * This class formats new user log entries.
678 class NewUsersLogFormatter
extends LogFormatter
{
679 protected function getMessageParameters() {
680 $params = parent
::getMessageParameters();
681 if ( $this->entry
->getSubtype() === 'create2' ) {
682 if ( isset( $params[3] ) ) {
683 $target = User
::newFromId( $params[3] );
685 $target = User
::newFromName( $this->entry
->getTarget()->getText(), false );
687 $params[2] = Message
::rawParam( $this->makeUserLink( $target ) );
688 $params[3] = $target->getName();
693 public function getComment() {
694 $timestamp = wfTimestamp( TS_MW
, $this->entry
->getTimestamp() );
695 if ( $timestamp < '20080129000000' ) {
696 # Suppress $comment from old entries (before 2008-01-29),
697 # not needed and can contain incorrect links
700 return parent
::getComment();