Fix API output formatting (change lines delimited with * as bold)
[lhc/web/wiklou.git] / includes / logging / LogFormatter.php
1 <?php
2 /**
3 * Contains classes for formatting log entries
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @author Niklas Laxström
22 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
23 * @since 1.19
24 */
25
26 /**
27 * Implements the default log formatting.
28 * Can be overridden by subclassing and setting
29 * $wgLogActionsHandlers['type/subtype'] = 'class'; or
30 * $wgLogActionsHandlers['type/*'] = 'class';
31 * @since 1.19
32 */
33 class LogFormatter {
34 // Audience options for viewing usernames, comments, and actions
35 const FOR_PUBLIC = 1;
36 const FOR_THIS_USER = 2;
37
38 // Static->
39
40 /**
41 * Constructs a new formatter suitable for given entry.
42 * @param $entry LogEntry
43 * @return LogFormatter
44 */
45 public static function newFromEntry( LogEntry $entry ) {
46 global $wgLogActionsHandlers;
47 $fulltype = $entry->getFullType();
48 $wildcard = $entry->getType() . '/*';
49 $handler = '';
50
51 if ( isset( $wgLogActionsHandlers[$fulltype] ) ) {
52 $handler = $wgLogActionsHandlers[$fulltype];
53 } elseif ( isset( $wgLogActionsHandlers[$wildcard] ) ) {
54 $handler = $wgLogActionsHandlers[$wildcard];
55 }
56
57 if ( $handler !== '' && is_string( $handler ) && class_exists( $handler ) ) {
58 return new $handler( $entry );
59 }
60
61 return new LegacyLogFormatter( $entry );
62 }
63
64 /**
65 * Handy shortcut for constructing a formatter directly from
66 * database row.
67 * @param $row
68 * @see DatabaseLogEntry::getSelectQueryData
69 * @return LogFormatter
70 */
71 public static function newFromRow( $row ) {
72 return self::newFromEntry( DatabaseLogEntry::newFromRow( $row ) );
73 }
74
75 // Nonstatic->
76
77 /// @var LogEntry
78 protected $entry;
79
80 /// Integer constant for handling log_deleted
81 protected $audience = self::FOR_PUBLIC;
82
83 /// Whether to output user tool links
84 protected $linkFlood = false;
85
86 /**
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.
91 * @var boolean
92 */
93 protected $plaintext = false;
94
95 protected $irctext = false;
96
97 protected function __construct( LogEntry $entry ) {
98 $this->entry = $entry;
99 $this->context = RequestContext::getMain();
100 }
101
102 /**
103 * Replace the default context
104 * @param $context IContextSource
105 */
106 public function setContext( IContextSource $context ) {
107 $this->context = $context;
108 }
109
110 /**
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
115 */
116 public function setAudience( $audience ) {
117 $this->audience = ( $audience == self::FOR_THIS_USER )
118 ? self::FOR_THIS_USER
119 : self::FOR_PUBLIC;
120 }
121
122 /**
123 * Check if a log item can be displayed
124 * @param $field integer LogPage::DELETED_* constant
125 * @return bool
126 */
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() );
131 } else {
132 return !$this->entry->isDeleted( $field );
133 }
134 }
135
136 /**
137 * If set to true, will produce user tool links after
138 * the user name. This should be replaced with generic
139 * CSS/JS solution.
140 * @param $value boolean
141 */
142 public function setShowUserToolLinks( $value ) {
143 $this->linkFlood = $value;
144 }
145
146 /**
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
152 */
153 public function getPlainActionText() {
154 $this->plaintext = true;
155 $text = $this->getActionText();
156 $this->plaintext = false;
157 return $text;
158 }
159
160 /**
161 * Even uglier hack to maintain backwards compatibilty with IRC bots
162 * (bug 34508).
163 * @see getActionText()
164 * @return string text
165 */
166 public function getIRCActionComment() {
167 $actionComment = $this->getIRCActionText();
168 $comment = $this->entry->getComment();
169
170 if ( $comment != '' ) {
171 if ( $actionComment == '' ) {
172 $actionComment = $comment;
173 } else {
174 $actionComment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $comment;
175 }
176 }
177
178 return $actionComment;
179 }
180
181 /**
182 * Even uglier hack to maintain backwards compatibilty with IRC bots
183 * (bug 34508).
184 * @see getActionText()
185 * @return string text
186 */
187 public function getIRCActionText() {
188 $this->plaintext = true;
189 $this->irctext = true;
190
191 $entry = $this->entry;
192 $parameters = $entry->getParameters();
193 // @see LogPage::actionText()
194 // Text of title the action is aimed at.
195 $target = $entry->getTarget()->getPrefixedText() ;
196 $text = null;
197 switch( $entry->getType() ) {
198 case 'move':
199 switch( $entry->getSubtype() ) {
200 case 'move':
201 $movesource = $parameters['4::target'];
202 $text = wfMessage( '1movedto2' )
203 ->rawParams( $target, $movesource )->inContentLanguage()->escaped();
204 break;
205 case 'move_redir':
206 $movesource = $parameters['4::target'];
207 $text = wfMessage( '1movedto2_redir' )
208 ->rawParams( $target, $movesource )->inContentLanguage()->escaped();
209 break;
210 case 'move-noredirect':
211 break;
212 case 'move_redir-noredirect':
213 break;
214 }
215 break;
216
217 case 'delete':
218 switch( $entry->getSubtype() ) {
219 case 'delete':
220 $text = wfMessage( 'deletedarticle' )
221 ->rawParams( $target )->inContentLanguage()->escaped();
222 break;
223 case 'restore':
224 $text = wfMessage( 'undeletedarticle' )
225 ->rawParams( $target )->inContentLanguage()->escaped();
226 break;
227 //case 'revision': // Revision deletion
228 //case 'event': // Log deletion
229 // see https://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/includes/LogPage.php?&pathrev=97044&r1=97043&r2=97044
230 //default:
231 }
232 break;
233
234 case 'patrol':
235 // https://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/includes/PatrolLog.php?&pathrev=97495&r1=97494&r2=97495
236 // Create a diff link to the patrolled revision
237 if ( $entry->getSubtype() === 'patrol' ) {
238 $diffLink = htmlspecialchars(
239 wfMessage( 'patrol-log-diff', $parameters['4::curid'] )
240 ->inContentLanguage()->text() );
241 $text = wfMessage( 'patrol-log-line', $diffLink, "[[$target]]", "" )
242 ->inContentLanguage()->text();
243 } else {
244 // broken??
245 }
246 break;
247
248 case 'protect':
249 switch( $entry->getSubtype() ) {
250 case 'protect':
251 $text = wfMessage( 'protectedarticle' )
252 ->rawParams( $target . ' ' . $parameters[0] )->inContentLanguage()->escaped();
253 break;
254 case 'unprotect':
255 $text = wfMessage( 'unprotectedarticle' )
256 ->rawParams( $target )->inContentLanguage()->escaped();
257 break;
258 case 'modify':
259 $text = wfMessage( 'modifiedarticleprotection' )
260 ->rawParams( $target . ' ' . $parameters[0] )->inContentLanguage()->escaped();
261 break;
262 }
263 break;
264
265 case 'newusers':
266 switch( $entry->getSubtype() ) {
267 case 'newusers':
268 case 'create':
269 $text = wfMessage( 'newuserlog-create-entry' )
270 ->inContentLanguage()->escaped();
271 break;
272 case 'create2':
273 $text = wfMessage( 'newuserlog-create2-entry' )
274 ->rawParams( $target )->inContentLanguage()->escaped();
275 break;
276 case 'autocreate':
277 $text = wfMessage( 'newuserlog-autocreate-entry' )
278 ->inContentLanguage()->escaped();
279 break;
280 }
281 break;
282
283 case 'upload':
284 switch( $entry->getSubtype() ) {
285 case 'upload':
286 $text = wfMessage( 'uploadedimage' )
287 ->rawParams( $target )->inContentLanguage()->escaped();
288 break;
289 case 'overwrite':
290 $text = wfMessage( 'overwroteimage' )
291 ->rawParams( $target )->inContentLanguage()->escaped();
292 break;
293 }
294 break;
295
296 case 'rights':
297 if ( count( $parameters['4::oldgroups'] ) ) {
298 $oldgroups = implode( ', ', $parameters['4::oldgroups'] );
299 } else {
300 $oldgroups = wfMessage( 'rightsnone' )->inContentLanguage()->escaped();
301 }
302 if ( count( $parameters['5::newgroups'] ) ) {
303 $newgroups = implode( ', ', $parameters['5::newgroups'] );
304 } else {
305 $newgroups = wfMessage( 'rightsnone' )->inContentLanguage()->escaped();
306 }
307 switch( $entry->getSubtype() ) {
308 case 'rights':
309 $text = wfMessage( 'rightslogentry' )
310 ->rawParams( $target, $oldgroups, $newgroups )->inContentLanguage()->escaped();
311 break;
312 case 'autopromote':
313 $text = wfMessage( 'rightslogentry-autopromote' )
314 ->rawParams( $target, $oldgroups, $newgroups )->inContentLanguage()->escaped();
315 break;
316 }
317 break;
318
319 // case 'suppress' --private log -- aaron (sign your messages so we know who to blame in a few years :-D)
320 // default:
321 }
322 if( is_null( $text ) ) {
323 $text = $this->getPlainActionText();
324 }
325
326 $this->plaintext = false;
327 $this->irctext = false;
328 return $text;
329 }
330
331 /**
332 * Gets the log action, including username.
333 * @return string HTML
334 */
335 public function getActionText() {
336 if ( $this->canView( LogPage::DELETED_ACTION ) ) {
337 $element = $this->getActionMessage();
338 if ( $element instanceof Message ) {
339 $element = $this->plaintext ? $element->text() : $element->escaped();
340 }
341 if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) ) {
342 $element = $this->styleRestricedElement( $element );
343 }
344 } else {
345 $performer = $this->getPerformerElement() . $this->msg( 'word-separator' )->text();
346 $element = $performer . $this->getRestrictedElement( 'rev-deleted-event' );
347 }
348
349 return $element;
350 }
351
352 /**
353 * Returns a sentence describing the log action. Usually
354 * a Message object is returned, but old style log types
355 * and entries might return pre-escaped html string.
356 * @return Message|string pre-escaped html
357 */
358 protected function getActionMessage() {
359 $message = $this->msg( $this->getMessageKey() );
360 $message->params( $this->getMessageParameters() );
361 return $message;
362 }
363
364 /**
365 * Returns a key to be used for formatting the action sentence.
366 * Default is logentry-TYPE-SUBTYPE for modern logs. Legacy log
367 * types will use custom keys, and subclasses can also alter the
368 * key depending on the entry itself.
369 * @return string message key
370 */
371 protected function getMessageKey() {
372 $type = $this->entry->getType();
373 $subtype = $this->entry->getSubtype();
374
375 return "logentry-$type-$subtype";
376 }
377
378 /**
379 * Returns extra links that comes after the action text, like "revert", etc.
380 *
381 * @return string
382 */
383 public function getActionLinks() {
384 return '';
385 }
386
387 /**
388 * Extracts the optional extra parameters for use in action messages.
389 * The array indexes start from number 3.
390 * @return array
391 */
392 protected function extractParameters() {
393 $entry = $this->entry;
394 $params = array();
395
396 if ( $entry->isLegacy() ) {
397 foreach ( $entry->getParameters() as $index => $value ) {
398 $params[$index + 3] = $value;
399 }
400 }
401
402 // Filter out parameters which are not in format #:foo
403 foreach ( $entry->getParameters() as $key => $value ) {
404 if ( strpos( $key, ':' ) === false ) continue;
405 list( $index, $type, $name ) = explode( ':', $key, 3 );
406 $params[$index - 1] = $this->formatParameterValue( $type, $value );
407 }
408
409 /* Message class doesn't like non consecutive numbering.
410 * Fill in missing indexes with empty strings to avoid
411 * incorrect renumbering.
412 */
413 if ( count( $params ) ) {
414 $max = max( array_keys( $params ) );
415 for ( $i = 4; $i < $max; $i++ ) {
416 if ( !isset( $params[$i] ) ) {
417 $params[$i] = '';
418 }
419 }
420 }
421 return $params;
422 }
423
424 /**
425 * Formats parameters intented for action message from
426 * array of all parameters. There are three hardcoded
427 * parameters (array is zero-indexed, this list not):
428 * - 1: user name with premade link
429 * - 2: usable for gender magic function
430 * - 3: target page with premade link
431 * @return array
432 */
433 protected function getMessageParameters() {
434 if ( isset( $this->parsedParameters ) ) {
435 return $this->parsedParameters;
436 }
437
438 $entry = $this->entry;
439 $params = $this->extractParameters();
440 $params[0] = Message::rawParam( $this->getPerformerElement() );
441 $params[1] = $entry->getPerformer()->getName();
442 $params[2] = Message::rawParam( $this->makePageLink( $entry->getTarget() ) );
443
444 // Bad things happens if the numbers are not in correct order
445 ksort( $params );
446 return $this->parsedParameters = $params;
447 }
448
449 /**
450 * Formats parameters values dependent to their type
451 * @param $type string The type of the value.
452 * Valid are currently:
453 * * - (empty) or plain: The value is returned as-is
454 * * raw: The value will be added to the log message
455 * as raw parameter (e.g. no escaping)
456 * Use this only if there is no other working
457 * type like user-link or title-link
458 * * msg: The value is a message-key, the output is
459 * the message in user language
460 * * msg-content: The value is a message-key, the output
461 * is the message in content language
462 * * user: The value is a user name, e.g. for GENDER
463 * * user-link: The value is a user name, returns a
464 * link for the user
465 * * title: The value is a page title,
466 * returns name of page
467 * * title-link: The value is a page title,
468 * returns link to this page
469 * * number: Format value as number
470 * @param $value string The parameter value that should
471 * be formated
472 * @return string or Message::numParam or Message::rawParam
473 * Formated value
474 * @since 1.21
475 */
476 protected function formatParameterValue( $type, $value ) {
477 $saveLinkFlood = $this->linkFlood;
478
479 switch( strtolower( trim( $type ) ) ) {
480 case 'raw':
481 $value = Message::rawParam( $value );
482 break;
483 case 'msg':
484 $value = $this->msg( $value )->text();
485 break;
486 case 'msg-content':
487 $value = $this->msg( $value )->inContentLanguage()->text();
488 break;
489 case 'number':
490 $value = Message::numParam( $value );
491 break;
492 case 'user':
493 $user = User::newFromName( $value );
494 $value = $user->getName();
495 break;
496 case 'user-link':
497 $this->setShowUserToolLinks( false );
498
499 $user = User::newFromName( $value );
500 $value = Message::rawParam( $this->makeUserLink( $user ) );
501
502 $this->setShowUserToolLinks( $saveLinkFlood );
503 break;
504 case 'title':
505 $title = Title::newFromText( $value );
506 $value = $title->getPrefixedText();
507 break;
508 case 'title-link':
509 $title = Title::newFromText( $value );
510 $value = Message::rawParam( $this->makePageLink( $title ) );
511 break;
512 case 'plain':
513 // Plain text, nothing to do
514 default:
515 // Catch other types and use the old behavior (return as-is)
516 }
517
518 return $value;
519 }
520
521 /**
522 * Helper to make a link to the page, taking the plaintext
523 * value in consideration.
524 * @param $title Title the page
525 * @param $parameters array query parameters
526 * @throws MWException
527 * @return String
528 */
529 protected function makePageLink( Title $title = null, $parameters = array() ) {
530 if ( !$this->plaintext ) {
531 $link = Linker::link( $title, null, array(), $parameters );
532 } else {
533 if ( !$title instanceof Title ) {
534 throw new MWException( "Expected title, got null" );
535 }
536 $link = '[[' . $title->getPrefixedText() . ']]';
537 }
538 return $link;
539 }
540
541 /**
542 * Provides the name of the user who performed the log action.
543 * Used as part of log action message or standalone, depending
544 * which parts of the log entry has been hidden.
545 * @return String
546 */
547 public function getPerformerElement() {
548 if ( $this->canView( LogPage::DELETED_USER ) ) {
549 $performer = $this->entry->getPerformer();
550 $element = $this->makeUserLink( $performer );
551 if ( $this->entry->isDeleted( LogPage::DELETED_USER ) ) {
552 $element = $this->styleRestricedElement( $element );
553 }
554 } else {
555 $element = $this->getRestrictedElement( 'rev-deleted-user' );
556 }
557
558 return $element;
559 }
560
561 /**
562 * Gets the luser provided comment
563 * @return string HTML
564 */
565 public function getComment() {
566 if ( $this->canView( LogPage::DELETED_COMMENT ) ) {
567 $comment = Linker::commentBlock( $this->entry->getComment() );
568 // No hard coded spaces thanx
569 $element = ltrim( $comment );
570 if ( $this->entry->isDeleted( LogPage::DELETED_COMMENT ) ) {
571 $element = $this->styleRestricedElement( $element );
572 }
573 } else {
574 $element = $this->getRestrictedElement( 'rev-deleted-comment' );
575 }
576
577 return $element;
578 }
579
580 /**
581 * Helper method for displaying restricted element.
582 * @param $message string
583 * @return string HTML or wikitext
584 */
585 protected function getRestrictedElement( $message ) {
586 if ( $this->plaintext ) {
587 return $this->msg( $message )->text();
588 }
589
590 $content = $this->msg( $message )->escaped();
591 $attribs = array( 'class' => 'history-deleted' );
592 return Html::rawElement( 'span', $attribs, $content );
593 }
594
595 /**
596 * Helper method for styling restricted element.
597 * @param $content string
598 * @return string HTML or wikitext
599 */
600 protected function styleRestricedElement( $content ) {
601 if ( $this->plaintext ) {
602 return $content;
603 }
604 $attribs = array( 'class' => 'history-deleted' );
605 return Html::rawElement( 'span', $attribs, $content );
606 }
607
608 /**
609 * Shortcut for wfMessage which honors local context.
610 * @todo Would it be better to require replacing the global context instead?
611 * @param $key string
612 * @return Message
613 */
614 protected function msg( $key ) {
615 return $this->context->msg( $key );
616 }
617
618 protected function makeUserLink( User $user ) {
619 if ( $this->plaintext ) {
620 $element = $user->getName();
621 } else {
622 $element = Linker::userLink(
623 $user->getId(),
624 $user->getName()
625 );
626
627 if ( $this->linkFlood ) {
628 $element .= Linker::userToolLinksRedContribs(
629 $user->getId(),
630 $user->getName(),
631 $user->getEditCount()
632 );
633 }
634 }
635 return $element;
636 }
637
638 /**
639 * @return Array of titles that should be preloaded with LinkBatch.
640 */
641 public function getPreloadTitles() {
642 return array();
643 }
644
645 /**
646 * @return Output of getMessageParameters() for testing
647 */
648 public function getMessageParametersForTesting() {
649 // This function was added because getMessageParameters() is
650 // protected and a change from protected to public caused
651 // problems with extensions
652 return $this->getMessageParameters();
653 }
654
655 }
656
657 /**
658 * This class formats all log entries for log types
659 * which have not been converted to the new system.
660 * This is not about old log entries which store
661 * parameters in a different format - the new
662 * LogFormatter classes have code to support formatting
663 * those too.
664 * @since 1.19
665 */
666 class LegacyLogFormatter extends LogFormatter {
667
668 /**
669 * Backward compatibility for extension changing the comment from
670 * the LogLine hook. This will be set by the first call on getComment(),
671 * then it might be modified by the hook when calling getActionLinks(),
672 * so that the modified value will be returned when calling getComment()
673 * a second time.
674 *
675 * @var string|null
676 */
677 private $comment = null;
678
679 /**
680 * Cache for the result of getActionLinks() so that it does not need to
681 * run multiple times depending on the order that getComment() and
682 * getActionLinks() are called.
683 *
684 * @var string|null
685 */
686 private $revert = null;
687
688 public function getComment() {
689 if ( $this->comment === null ) {
690 $this->comment = parent::getComment();
691 }
692
693 // Make sure we execute the LogLine hook so that we immediately return
694 // the correct value.
695 if ( $this->revert === null ) {
696 $this->getActionLinks();
697 }
698
699 return $this->comment;
700 }
701
702 protected function getActionMessage() {
703 $entry = $this->entry;
704 $action = LogPage::actionText(
705 $entry->getType(),
706 $entry->getSubtype(),
707 $entry->getTarget(),
708 $this->plaintext ? null : $this->context->getSkin(),
709 (array)$entry->getParameters(),
710 !$this->plaintext // whether to filter [[]] links
711 );
712
713 $performer = $this->getPerformerElement();
714 if ( !$this->irctext ) {
715 $action = $performer . $this->msg( 'word-separator' )->text() . $action;
716 }
717
718 return $action;
719 }
720
721 public function getActionLinks() {
722 if ( $this->revert !== null ) {
723 return $this->revert;
724 }
725
726 if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) ) {
727 return $this->revert = '';
728 }
729
730 $title = $this->entry->getTarget();
731 $type = $this->entry->getType();
732 $subtype = $this->entry->getSubtype();
733
734 // Show unblock/change block link
735 if ( ( $type == 'block' || $type == 'suppress' ) && ( $subtype == 'block' || $subtype == 'reblock' ) ) {
736 if ( !$this->context->getUser()->isAllowed( 'block' ) ) {
737 return '';
738 }
739
740 $links = array(
741 Linker::linkKnown(
742 SpecialPage::getTitleFor( 'Unblock', $title->getDBkey() ),
743 $this->msg( 'unblocklink' )->escaped()
744 ),
745 Linker::linkKnown(
746 SpecialPage::getTitleFor( 'Block', $title->getDBkey() ),
747 $this->msg( 'change-blocklink' )->escaped()
748 )
749 );
750 return $this->msg( 'parentheses' )->rawParams(
751 $this->context->getLanguage()->pipeList( $links ) )->escaped();
752 // Show change protection link
753 } elseif ( $type == 'protect' && ( $subtype == 'protect' || $subtype == 'modify' || $subtype == 'unprotect' ) ) {
754 $links = array(
755 Linker::link( $title,
756 $this->msg( 'hist' )->escaped(),
757 array(),
758 array(
759 'action' => 'history',
760 'offset' => $this->entry->getTimestamp()
761 )
762 )
763 );
764 if ( $this->context->getUser()->isAllowed( 'protect' ) ) {
765 $links[] = Linker::linkKnown(
766 $title,
767 $this->msg( 'protect_change' )->escaped(),
768 array(),
769 array( 'action' => 'protect' )
770 );
771 }
772 return $this->msg( 'parentheses' )->rawParams(
773 $this->context->getLanguage()->pipeList( $links ) )->escaped();
774 // Show unmerge link
775 } elseif( $type == 'merge' && $subtype == 'merge' ) {
776 if ( !$this->context->getUser()->isAllowed( 'mergehistory' ) ) {
777 return '';
778 }
779
780 $params = $this->extractParameters();
781 $revert = Linker::linkKnown(
782 SpecialPage::getTitleFor( 'MergeHistory' ),
783 $this->msg( 'revertmerge' )->escaped(),
784 array(),
785 array(
786 'target' => $params[3],
787 'dest' => $title->getPrefixedDBkey(),
788 'mergepoint' => $params[4]
789 )
790 );
791 return $this->msg( 'parentheses' )->rawParams( $revert )->escaped();
792 }
793
794 // Do nothing. The implementation is handled by the hook modifiying the
795 // passed-by-ref parameters. This also changes the default value so that
796 // getComment() and getActionLinks() do not call them indefinitely.
797 $this->revert = '';
798
799 // This is to populate the $comment member of this instance so that it
800 // can be modified when calling the hook just below.
801 if ( $this->comment === null ) {
802 $this->getComment();
803 }
804
805 $params = $this->entry->getParameters();
806
807 wfRunHooks( 'LogLine', array( $type, $subtype, $title, $params,
808 &$this->comment, &$this->revert, $this->entry->getTimestamp() ) );
809
810 return $this->revert;
811 }
812 }
813
814 /**
815 * This class formats move log entries.
816 * @since 1.19
817 */
818 class MoveLogFormatter extends LogFormatter {
819 public function getPreloadTitles() {
820 $params = $this->extractParameters();
821 return array( Title::newFromText( $params[3] ) );
822 }
823
824 protected function getMessageKey() {
825 $key = parent::getMessageKey();
826 $params = $this->getMessageParameters();
827 if ( isset( $params[4] ) && $params[4] === '1' ) {
828 $key .= '-noredirect';
829 }
830 return $key;
831 }
832
833 protected function getMessageParameters() {
834 $params = parent::getMessageParameters();
835 $oldname = $this->makePageLink( $this->entry->getTarget(), array( 'redirect' => 'no' ) );
836 $newname = $this->makePageLink( Title::newFromText( $params[3] ) );
837 $params[2] = Message::rawParam( $oldname );
838 $params[3] = Message::rawParam( $newname );
839 return $params;
840 }
841
842 public function getActionLinks() {
843 if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) // Action is hidden
844 || $this->entry->getSubtype() !== 'move'
845 || !$this->context->getUser()->isAllowed( 'move' ) )
846 {
847 return '';
848 }
849
850 $params = $this->extractParameters();
851 $destTitle = Title::newFromText( $params[3] );
852 if ( !$destTitle ) {
853 return '';
854 }
855
856 $revert = Linker::linkKnown(
857 SpecialPage::getTitleFor( 'Movepage' ),
858 $this->msg( 'revertmove' )->escaped(),
859 array(),
860 array(
861 'wpOldTitle' => $destTitle->getPrefixedDBkey(),
862 'wpNewTitle' => $this->entry->getTarget()->getPrefixedDBkey(),
863 'wpReason' => $this->msg( 'revertmove' )->inContentLanguage()->text(),
864 'wpMovetalk' => 0
865 )
866 );
867 return $this->msg( 'parentheses' )->rawParams( $revert )->escaped();
868 }
869 }
870
871 /**
872 * This class formats delete log entries.
873 * @since 1.19
874 */
875 class DeleteLogFormatter extends LogFormatter {
876 protected function getMessageKey() {
877 $key = parent::getMessageKey();
878 if ( in_array( $this->entry->getSubtype(), array( 'event', 'revision' ) ) ) {
879 if ( count( $this->getMessageParameters() ) < 5 ) {
880 return "$key-legacy";
881 }
882 }
883 return $key;
884 }
885
886 protected function getMessageParameters() {
887 if ( isset( $this->parsedParametersDeleteLog ) ) {
888 return $this->parsedParametersDeleteLog;
889 }
890
891 $params = parent::getMessageParameters();
892 $subtype = $this->entry->getSubtype();
893 if ( in_array( $subtype, array( 'event', 'revision' ) ) ) {
894 // $params[3] here is 'revision' for page revisions, 'oldimage' for file versions, or a comma-separated list of log_ids for log entries.
895 // $subtype here is 'revision' for page revisions and file versions, or 'event' for log entries.
896 if (
897 ( $subtype === 'event' && count( $params ) === 6 ) ||
898 ( $subtype === 'revision' && isset( $params[3] ) && ( $params[3] === 'revision' || $params[3] === 'oldimage' ) )
899 ) {
900 $paramStart = $subtype === 'revision' ? 4 : 3;
901
902 $old = $this->parseBitField( $params[$paramStart+1] );
903 $new = $this->parseBitField( $params[$paramStart+2] );
904 list( $hid, $unhid, $extra ) = RevisionDeleter::getChanges( $new, $old );
905 $changes = array();
906 foreach ( $hid as $v ) {
907 $changes[] = $this->msg( "$v-hid" )->plain();
908 }
909 foreach ( $unhid as $v ) {
910 $changes[] = $this->msg( "$v-unhid" )->plain();
911 }
912 foreach ( $extra as $v ) {
913 $changes[] = $this->msg( $v )->plain();
914 }
915 $changeText = $this->context->getLanguage()->listToText( $changes );
916
917
918 $newParams = array_slice( $params, 0, 3 );
919 $newParams[3] = $changeText;
920 $count = count( explode( ',', $params[$paramStart] ) );
921 $newParams[4] = $this->context->getLanguage()->formatNum( $count );
922 return $this->parsedParametersDeleteLog = $newParams;
923 } else {
924 return $this->parsedParametersDeleteLog = array_slice( $params, 0, 3 );
925 }
926 }
927
928 return $this->parsedParametersDeleteLog = $params;
929 }
930
931 protected function parseBitField( $string ) {
932 // Input is like ofield=2134 or just the number
933 if ( strpos( $string, 'field=' ) === 1 ) {
934 list( , $field ) = explode( '=', $string );
935 return (int) $field;
936 } else {
937 return (int) $string;
938 }
939 }
940
941 public function getActionLinks() {
942 $user = $this->context->getUser();
943 if ( !$user->isAllowed( 'deletedhistory' ) || $this->entry->isDeleted( LogPage::DELETED_ACTION ) ) {
944 return '';
945 }
946
947 switch ( $this->entry->getSubtype() ) {
948 case 'delete': // Show undelete link
949 if( $user->isAllowed( 'undelete' ) ) {
950 $message = 'undeletelink';
951 } else {
952 $message = 'undeleteviewlink';
953 }
954 $revert = Linker::linkKnown(
955 SpecialPage::getTitleFor( 'Undelete' ),
956 $this->msg( $message )->escaped(),
957 array(),
958 array( 'target' => $this->entry->getTarget()->getPrefixedDBkey() )
959 );
960 return $this->msg( 'parentheses' )->rawParams( $revert )->escaped();
961
962 case 'revision': // If an edit was hidden from a page give a review link to the history
963 $params = $this->extractParameters();
964 if ( !isset( $params[3] ) || !isset( $params[4] ) ) {
965 return '';
966 }
967
968 // Different revision types use different URL params...
969 $key = $params[3];
970 // This is a CSV of the IDs
971 $ids = explode( ',', $params[4] );
972
973 $links = array();
974
975 // If there's only one item, we can show a diff link
976 if ( count( $ids ) == 1 ) {
977 // Live revision diffs...
978 if ( $key == 'oldid' || $key == 'revision' ) {
979 $links[] = Linker::linkKnown(
980 $this->entry->getTarget(),
981 $this->msg( 'diff' )->escaped(),
982 array(),
983 array(
984 'diff' => intval( $ids[0] ),
985 'unhide' => 1
986 )
987 );
988 // Deleted revision diffs...
989 } elseif ( $key == 'artimestamp' || $key == 'archive' ) {
990 $links[] = Linker::linkKnown(
991 SpecialPage::getTitleFor( 'Undelete' ),
992 $this->msg( 'diff' )->escaped(),
993 array(),
994 array(
995 'target' => $this->entry->getTarget()->getPrefixedDBKey(),
996 'diff' => 'prev',
997 'timestamp' => $ids[0]
998 )
999 );
1000 }
1001 }
1002
1003 // View/modify link...
1004 $links[] = Linker::linkKnown(
1005 SpecialPage::getTitleFor( 'Revisiondelete' ),
1006 $this->msg( 'revdel-restore' )->escaped(),
1007 array(),
1008 array(
1009 'target' => $this->entry->getTarget()->getPrefixedText(),
1010 'type' => $key,
1011 'ids' => implode( ',', $ids ),
1012 )
1013 );
1014
1015 return $this->msg( 'parentheses' )->rawParams(
1016 $this->context->getLanguage()->pipeList( $links ) )->escaped();
1017
1018 case 'event': // Hidden log items, give review link
1019 $params = $this->extractParameters();
1020 if ( !isset( $params[3] ) ) {
1021 return '';
1022 }
1023 // This is a CSV of the IDs
1024 $query = $params[3];
1025 // Link to each hidden object ID, $params[1] is the url param
1026 $revert = Linker::linkKnown(
1027 SpecialPage::getTitleFor( 'Revisiondelete' ),
1028 $this->msg( 'revdel-restore' )->escaped(),
1029 array(),
1030 array(
1031 'target' => $this->entry->getTarget()->getPrefixedText(),
1032 'type' => 'logging',
1033 'ids' => $query
1034 )
1035 );
1036 return $this->msg( 'parentheses' )->rawParams( $revert )->escaped();
1037 default:
1038 return '';
1039 }
1040 }
1041 }
1042
1043 /**
1044 * This class formats patrol log entries.
1045 * @since 1.19
1046 */
1047 class PatrolLogFormatter extends LogFormatter {
1048 protected function getMessageKey() {
1049 $key = parent::getMessageKey();
1050 $params = $this->getMessageParameters();
1051 if ( isset( $params[5] ) && $params[5] ) {
1052 $key .= '-auto';
1053 }
1054 return $key;
1055 }
1056
1057 protected function getMessageParameters() {
1058 $params = parent::getMessageParameters();
1059
1060 $target = $this->entry->getTarget();
1061 $oldid = $params[3];
1062 $revision = $this->context->getLanguage()->formatNum( $oldid, true );
1063
1064 if ( $this->plaintext ) {
1065 $revlink = $revision;
1066 } elseif ( $target->exists() ) {
1067 $query = array(
1068 'oldid' => $oldid,
1069 'diff' => 'prev'
1070 );
1071 $revlink = Linker::link( $target, htmlspecialchars( $revision ), array(), $query );
1072 } else {
1073 $revlink = htmlspecialchars( $revision );
1074 }
1075
1076 $params[3] = Message::rawParam( $revlink );
1077 return $params;
1078 }
1079 }
1080
1081 /**
1082 * This class formats new user log entries.
1083 * @since 1.19
1084 */
1085 class NewUsersLogFormatter extends LogFormatter {
1086 protected function getMessageParameters() {
1087 $params = parent::getMessageParameters();
1088 if ( $this->entry->getSubtype() === 'create2' ) {
1089 if ( isset( $params[3] ) ) {
1090 $target = User::newFromId( $params[3] );
1091 } else {
1092 $target = User::newFromName( $this->entry->getTarget()->getText(), false );
1093 }
1094 $params[2] = Message::rawParam( $this->makeUserLink( $target ) );
1095 $params[3] = $target->getName();
1096 }
1097 return $params;
1098 }
1099
1100 public function getComment() {
1101 $timestamp = wfTimestamp( TS_MW, $this->entry->getTimestamp() );
1102 if ( $timestamp < '20080129000000' ) {
1103 # Suppress $comment from old entries (before 2008-01-29),
1104 # not needed and can contain incorrect links
1105 return '';
1106 }
1107 return parent::getComment();
1108 }
1109
1110 public function getPreloadTitles() {
1111 if ( $this->entry->getSubtype() === 'create2' ) {
1112 //add the user talk to LinkBatch for the userLink
1113 return array( Title::makeTitle( NS_USER_TALK, $this->entry->getTarget()->getText() ) );
1114 }
1115 return array();
1116 }
1117 }
1118
1119 /**
1120 * This class formats rights log entries.
1121 * @since 1.21
1122 */
1123 class RightsLogFormatter extends LogFormatter {
1124 protected function makePageLink( Title $title = null, $parameters = array() ) {
1125 global $wgContLang, $wgUserrightsInterwikiDelimiter;
1126
1127 if ( !$this->plaintext ) {
1128 $text = $wgContLang->ucfirst( $title->getText() );
1129 $parts = explode( $wgUserrightsInterwikiDelimiter, $text, 2 );
1130
1131 if ( count( $parts ) === 2 ) {
1132 $titleLink = WikiMap::foreignUserLink( $parts[1], $parts[0],
1133 htmlspecialchars( $title->getPrefixedText() ) );
1134
1135 if ( $titleLink !== false ) {
1136 return $titleLink;
1137 }
1138 }
1139 }
1140
1141 return parent::makePageLink( $title, $parameters );
1142 }
1143
1144 protected function getMessageKey() {
1145 $key = parent::getMessageKey();
1146 $params = $this->getMessageParameters();
1147 if ( !isset( $params[3] ) && !isset( $params[4] ) ) {
1148 $key .= '-legacy';
1149 }
1150 return $key;
1151 }
1152
1153 protected function getMessageParameters() {
1154 $params = parent::getMessageParameters();
1155
1156 // Really old entries
1157 if ( !isset( $params[3] ) && !isset( $params[4] ) ) {
1158 return $params;
1159 }
1160
1161 $oldGroups = $params[3];
1162 $newGroups = $params[4];
1163
1164 // Less old entries
1165 if ( $oldGroups === '' ) {
1166 $oldGroups = array();
1167 } elseif ( is_string( $oldGroups ) ) {
1168 $oldGroups = array_map( 'trim', explode( ',', $oldGroups ) );
1169 }
1170 if ( $newGroups === '' ) {
1171 $newGroups = array();
1172 } elseif ( is_string( $newGroups ) ) {
1173 $newGroups = array_map( 'trim', explode( ',', $newGroups ) );
1174 }
1175
1176 $userName = $this->entry->getTarget()->getText();
1177 if ( !$this->plaintext && count( $oldGroups ) ) {
1178 foreach ( $oldGroups as &$group ) {
1179 $group = User::getGroupMember( $group, $userName );
1180 }
1181 }
1182 if ( !$this->plaintext && count( $newGroups ) ) {
1183 foreach ( $newGroups as &$group ) {
1184 $group = User::getGroupMember( $group, $userName );
1185 }
1186 }
1187
1188 $lang = $this->context->getLanguage();
1189 if ( count( $oldGroups ) ) {
1190 $params[3] = $lang->listToText( $oldGroups );
1191 } else {
1192 $params[3] = $this->msg( 'rightsnone' )->text();
1193 }
1194 if ( count( $newGroups ) ) {
1195 // Array_values is used here because of bug 42211
1196 // see use of array_unique in UserrightsPage::doSaveUserGroups on $newGroups.
1197 $params[4] = $lang->listToText( array_values( $newGroups ) );
1198 } else {
1199 $params[4] = $this->msg( 'rightsnone' )->text();
1200 }
1201
1202 return $params;
1203 }
1204 }