6c5b9835b8a49734a0fa0f4a88a32df7ab3bd27a
[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 case 'byemail':
274 $text = wfMessage( 'newuserlog-create2-entry' )
275 ->rawParams( $target )->inContentLanguage()->escaped();
276 break;
277 case 'autocreate':
278 $text = wfMessage( 'newuserlog-autocreate-entry' )
279 ->inContentLanguage()->escaped();
280 break;
281 }
282 break;
283
284 case 'upload':
285 switch( $entry->getSubtype() ) {
286 case 'upload':
287 $text = wfMessage( 'uploadedimage' )
288 ->rawParams( $target )->inContentLanguage()->escaped();
289 break;
290 case 'overwrite':
291 $text = wfMessage( 'overwroteimage' )
292 ->rawParams( $target )->inContentLanguage()->escaped();
293 break;
294 }
295 break;
296
297 case 'rights':
298 if ( count( $parameters['4::oldgroups'] ) ) {
299 $oldgroups = implode( ', ', $parameters['4::oldgroups'] );
300 } else {
301 $oldgroups = wfMessage( 'rightsnone' )->inContentLanguage()->escaped();
302 }
303 if ( count( $parameters['5::newgroups'] ) ) {
304 $newgroups = implode( ', ', $parameters['5::newgroups'] );
305 } else {
306 $newgroups = wfMessage( 'rightsnone' )->inContentLanguage()->escaped();
307 }
308 switch( $entry->getSubtype() ) {
309 case 'rights':
310 $text = wfMessage( 'rightslogentry' )
311 ->rawParams( $target, $oldgroups, $newgroups )->inContentLanguage()->escaped();
312 break;
313 case 'autopromote':
314 $text = wfMessage( 'rightslogentry-autopromote' )
315 ->rawParams( $target, $oldgroups, $newgroups )->inContentLanguage()->escaped();
316 break;
317 }
318 break;
319
320 // case 'suppress' --private log -- aaron (sign your messages so we know who to blame in a few years :-D)
321 // default:
322 }
323 if( is_null( $text ) ) {
324 $text = $this->getPlainActionText();
325 }
326
327 $this->plaintext = false;
328 $this->irctext = false;
329 return $text;
330 }
331
332 /**
333 * Gets the log action, including username.
334 * @return string HTML
335 */
336 public function getActionText() {
337 if ( $this->canView( LogPage::DELETED_ACTION ) ) {
338 $element = $this->getActionMessage();
339 if ( $element instanceof Message ) {
340 $element = $this->plaintext ? $element->text() : $element->escaped();
341 }
342 if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) ) {
343 $element = $this->styleRestricedElement( $element );
344 }
345 } else {
346 $performer = $this->getPerformerElement() . $this->msg( 'word-separator' )->text();
347 $element = $performer . $this->getRestrictedElement( 'rev-deleted-event' );
348 }
349
350 return $element;
351 }
352
353 /**
354 * Returns a sentence describing the log action. Usually
355 * a Message object is returned, but old style log types
356 * and entries might return pre-escaped html string.
357 * @return Message|string pre-escaped html
358 */
359 protected function getActionMessage() {
360 $message = $this->msg( $this->getMessageKey() );
361 $message->params( $this->getMessageParameters() );
362 return $message;
363 }
364
365 /**
366 * Returns a key to be used for formatting the action sentence.
367 * Default is logentry-TYPE-SUBTYPE for modern logs. Legacy log
368 * types will use custom keys, and subclasses can also alter the
369 * key depending on the entry itself.
370 * @return string message key
371 */
372 protected function getMessageKey() {
373 $type = $this->entry->getType();
374 $subtype = $this->entry->getSubtype();
375
376 return "logentry-$type-$subtype";
377 }
378
379 /**
380 * Returns extra links that comes after the action text, like "revert", etc.
381 *
382 * @return string
383 */
384 public function getActionLinks() {
385 return '';
386 }
387
388 /**
389 * Extracts the optional extra parameters for use in action messages.
390 * The array indexes start from number 3.
391 * @return array
392 */
393 protected function extractParameters() {
394 $entry = $this->entry;
395 $params = array();
396
397 if ( $entry->isLegacy() ) {
398 foreach ( $entry->getParameters() as $index => $value ) {
399 $params[$index + 3] = $value;
400 }
401 }
402
403 // Filter out parameters which are not in format #:foo
404 foreach ( $entry->getParameters() as $key => $value ) {
405 if ( strpos( $key, ':' ) === false ) continue;
406 list( $index, $type, $name ) = explode( ':', $key, 3 );
407 $params[$index - 1] = $this->formatParameterValue( $type, $value );
408 }
409
410 /* Message class doesn't like non consecutive numbering.
411 * Fill in missing indexes with empty strings to avoid
412 * incorrect renumbering.
413 */
414 if ( count( $params ) ) {
415 $max = max( array_keys( $params ) );
416 for ( $i = 4; $i < $max; $i++ ) {
417 if ( !isset( $params[$i] ) ) {
418 $params[$i] = '';
419 }
420 }
421 }
422 return $params;
423 }
424
425 /**
426 * Formats parameters intented for action message from
427 * array of all parameters. There are three hardcoded
428 * parameters (array is zero-indexed, this list not):
429 * - 1: user name with premade link
430 * - 2: usable for gender magic function
431 * - 3: target page with premade link
432 * @return array
433 */
434 protected function getMessageParameters() {
435 if ( isset( $this->parsedParameters ) ) {
436 return $this->parsedParameters;
437 }
438
439 $entry = $this->entry;
440 $params = $this->extractParameters();
441 $params[0] = Message::rawParam( $this->getPerformerElement() );
442 $params[1] = $this->canView( LogPage::DELETED_USER ) ? $entry->getPerformer()->getName() : '';
443 $params[2] = Message::rawParam( $this->makePageLink( $entry->getTarget() ) );
444
445 // Bad things happens if the numbers are not in correct order
446 ksort( $params );
447 return $this->parsedParameters = $params;
448 }
449
450 /**
451 * Formats parameters values dependent to their type
452 * @param $type string The type of the value.
453 * Valid are currently:
454 * * - (empty) or plain: The value is returned as-is
455 * * raw: The value will be added to the log message
456 * as raw parameter (e.g. no escaping)
457 * Use this only if there is no other working
458 * type like user-link or title-link
459 * * msg: The value is a message-key, the output is
460 * the message in user language
461 * * msg-content: The value is a message-key, the output
462 * is the message in content language
463 * * user: The value is a user name, e.g. for GENDER
464 * * user-link: The value is a user name, returns a
465 * link for the user
466 * * title: The value is a page title,
467 * returns name of page
468 * * title-link: The value is a page title,
469 * returns link to this page
470 * * number: Format value as number
471 * @param $value string The parameter value that should
472 * be formated
473 * @return string or Message::numParam or Message::rawParam
474 * Formated value
475 * @since 1.21
476 */
477 protected function formatParameterValue( $type, $value ) {
478 $saveLinkFlood = $this->linkFlood;
479
480 switch( strtolower( trim( $type ) ) ) {
481 case 'raw':
482 $value = Message::rawParam( $value );
483 break;
484 case 'msg':
485 $value = $this->msg( $value )->text();
486 break;
487 case 'msg-content':
488 $value = $this->msg( $value )->inContentLanguage()->text();
489 break;
490 case 'number':
491 $value = Message::numParam( $value );
492 break;
493 case 'user':
494 $user = User::newFromName( $value );
495 $value = $user->getName();
496 break;
497 case 'user-link':
498 $this->setShowUserToolLinks( false );
499
500 $user = User::newFromName( $value );
501 $value = Message::rawParam( $this->makeUserLink( $user ) );
502
503 $this->setShowUserToolLinks( $saveLinkFlood );
504 break;
505 case 'title':
506 $title = Title::newFromText( $value );
507 $value = $title->getPrefixedText();
508 break;
509 case 'title-link':
510 $title = Title::newFromText( $value );
511 $value = Message::rawParam( $this->makePageLink( $title ) );
512 break;
513 case 'plain':
514 // Plain text, nothing to do
515 default:
516 // Catch other types and use the old behavior (return as-is)
517 }
518
519 return $value;
520 }
521
522 /**
523 * Helper to make a link to the page, taking the plaintext
524 * value in consideration.
525 * @param $title Title the page
526 * @param $parameters array query parameters
527 * @throws MWException
528 * @return String
529 */
530 protected function makePageLink( Title $title = null, $parameters = array() ) {
531 if ( !$this->plaintext ) {
532 $link = Linker::link( $title, null, array(), $parameters );
533 } else {
534 if ( !$title instanceof Title ) {
535 throw new MWException( "Expected title, got null" );
536 }
537 $link = '[[' . $title->getPrefixedText() . ']]';
538 }
539 return $link;
540 }
541
542 /**
543 * Provides the name of the user who performed the log action.
544 * Used as part of log action message or standalone, depending
545 * which parts of the log entry has been hidden.
546 * @return String
547 */
548 public function getPerformerElement() {
549 if ( $this->canView( LogPage::DELETED_USER ) ) {
550 $performer = $this->entry->getPerformer();
551 $element = $this->makeUserLink( $performer );
552 if ( $this->entry->isDeleted( LogPage::DELETED_USER ) ) {
553 $element = $this->styleRestricedElement( $element );
554 }
555 } else {
556 $element = $this->getRestrictedElement( 'rev-deleted-user' );
557 }
558
559 return $element;
560 }
561
562 /**
563 * Gets the luser provided comment
564 * @return string HTML
565 */
566 public function getComment() {
567 if ( $this->canView( LogPage::DELETED_COMMENT ) ) {
568 $comment = Linker::commentBlock( $this->entry->getComment() );
569 // No hard coded spaces thanx
570 $element = ltrim( $comment );
571 if ( $this->entry->isDeleted( LogPage::DELETED_COMMENT ) ) {
572 $element = $this->styleRestricedElement( $element );
573 }
574 } else {
575 $element = $this->getRestrictedElement( 'rev-deleted-comment' );
576 }
577
578 return $element;
579 }
580
581 /**
582 * Helper method for displaying restricted element.
583 * @param $message string
584 * @return string HTML or wikitext
585 */
586 protected function getRestrictedElement( $message ) {
587 if ( $this->plaintext ) {
588 return $this->msg( $message )->text();
589 }
590
591 $content = $this->msg( $message )->escaped();
592 $attribs = array( 'class' => 'history-deleted' );
593 return Html::rawElement( 'span', $attribs, $content );
594 }
595
596 /**
597 * Helper method for styling restricted element.
598 * @param $content string
599 * @return string HTML or wikitext
600 */
601 protected function styleRestricedElement( $content ) {
602 if ( $this->plaintext ) {
603 return $content;
604 }
605 $attribs = array( 'class' => 'history-deleted' );
606 return Html::rawElement( 'span', $attribs, $content );
607 }
608
609 /**
610 * Shortcut for wfMessage which honors local context.
611 * @todo Would it be better to require replacing the global context instead?
612 * @param $key string
613 * @return Message
614 */
615 protected function msg( $key ) {
616 return $this->context->msg( $key );
617 }
618
619 protected function makeUserLink( User $user ) {
620 if ( $this->plaintext ) {
621 $element = $user->getName();
622 } else {
623 $element = Linker::userLink(
624 $user->getId(),
625 $user->getName()
626 );
627
628 if ( $this->linkFlood ) {
629 $element .= Linker::userToolLinksRedContribs(
630 $user->getId(),
631 $user->getName(),
632 $user->getEditCount()
633 );
634 }
635 }
636 return $element;
637 }
638
639 /**
640 * @return Array of titles that should be preloaded with LinkBatch.
641 */
642 public function getPreloadTitles() {
643 return array();
644 }
645
646 /**
647 * @return Output of getMessageParameters() for testing
648 */
649 public function getMessageParametersForTesting() {
650 // This function was added because getMessageParameters() is
651 // protected and a change from protected to public caused
652 // problems with extensions
653 return $this->getMessageParameters();
654 }
655
656 }
657
658 /**
659 * This class formats all log entries for log types
660 * which have not been converted to the new system.
661 * This is not about old log entries which store
662 * parameters in a different format - the new
663 * LogFormatter classes have code to support formatting
664 * those too.
665 * @since 1.19
666 */
667 class LegacyLogFormatter extends LogFormatter {
668
669 /**
670 * Backward compatibility for extension changing the comment from
671 * the LogLine hook. This will be set by the first call on getComment(),
672 * then it might be modified by the hook when calling getActionLinks(),
673 * so that the modified value will be returned when calling getComment()
674 * a second time.
675 *
676 * @var string|null
677 */
678 private $comment = null;
679
680 /**
681 * Cache for the result of getActionLinks() so that it does not need to
682 * run multiple times depending on the order that getComment() and
683 * getActionLinks() are called.
684 *
685 * @var string|null
686 */
687 private $revert = null;
688
689 public function getComment() {
690 if ( $this->comment === null ) {
691 $this->comment = parent::getComment();
692 }
693
694 // Make sure we execute the LogLine hook so that we immediately return
695 // the correct value.
696 if ( $this->revert === null ) {
697 $this->getActionLinks();
698 }
699
700 return $this->comment;
701 }
702
703 protected function getActionMessage() {
704 $entry = $this->entry;
705 $action = LogPage::actionText(
706 $entry->getType(),
707 $entry->getSubtype(),
708 $entry->getTarget(),
709 $this->plaintext ? null : $this->context->getSkin(),
710 (array)$entry->getParameters(),
711 !$this->plaintext // whether to filter [[]] links
712 );
713
714 $performer = $this->getPerformerElement();
715 if ( !$this->irctext ) {
716 $action = $performer . $this->msg( 'word-separator' )->text() . $action;
717 }
718
719 return $action;
720 }
721
722 public function getActionLinks() {
723 if ( $this->revert !== null ) {
724 return $this->revert;
725 }
726
727 if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) ) {
728 return $this->revert = '';
729 }
730
731 $title = $this->entry->getTarget();
732 $type = $this->entry->getType();
733 $subtype = $this->entry->getSubtype();
734
735 // Show unblock/change block link
736 if ( ( $type == 'block' || $type == 'suppress' ) && ( $subtype == 'block' || $subtype == 'reblock' ) ) {
737 if ( !$this->context->getUser()->isAllowed( 'block' ) ) {
738 return '';
739 }
740
741 $links = array(
742 Linker::linkKnown(
743 SpecialPage::getTitleFor( 'Unblock', $title->getDBkey() ),
744 $this->msg( 'unblocklink' )->escaped()
745 ),
746 Linker::linkKnown(
747 SpecialPage::getTitleFor( 'Block', $title->getDBkey() ),
748 $this->msg( 'change-blocklink' )->escaped()
749 )
750 );
751 return $this->msg( 'parentheses' )->rawParams(
752 $this->context->getLanguage()->pipeList( $links ) )->escaped();
753 // Show change protection link
754 } elseif ( $type == 'protect' && ( $subtype == 'protect' || $subtype == 'modify' || $subtype == 'unprotect' ) ) {
755 $links = array(
756 Linker::link( $title,
757 $this->msg( 'hist' )->escaped(),
758 array(),
759 array(
760 'action' => 'history',
761 'offset' => $this->entry->getTimestamp()
762 )
763 )
764 );
765 if ( $this->context->getUser()->isAllowed( 'protect' ) ) {
766 $links[] = Linker::linkKnown(
767 $title,
768 $this->msg( 'protect_change' )->escaped(),
769 array(),
770 array( 'action' => 'protect' )
771 );
772 }
773 return $this->msg( 'parentheses' )->rawParams(
774 $this->context->getLanguage()->pipeList( $links ) )->escaped();
775 // Show unmerge link
776 } elseif( $type == 'merge' && $subtype == 'merge' ) {
777 if ( !$this->context->getUser()->isAllowed( 'mergehistory' ) ) {
778 return '';
779 }
780
781 $params = $this->extractParameters();
782 $revert = Linker::linkKnown(
783 SpecialPage::getTitleFor( 'MergeHistory' ),
784 $this->msg( 'revertmerge' )->escaped(),
785 array(),
786 array(
787 'target' => $params[3],
788 'dest' => $title->getPrefixedDBkey(),
789 'mergepoint' => $params[4]
790 )
791 );
792 return $this->msg( 'parentheses' )->rawParams( $revert )->escaped();
793 }
794
795 // Do nothing. The implementation is handled by the hook modifiying the
796 // passed-by-ref parameters. This also changes the default value so that
797 // getComment() and getActionLinks() do not call them indefinitely.
798 $this->revert = '';
799
800 // This is to populate the $comment member of this instance so that it
801 // can be modified when calling the hook just below.
802 if ( $this->comment === null ) {
803 $this->getComment();
804 }
805
806 $params = $this->entry->getParameters();
807
808 wfRunHooks( 'LogLine', array( $type, $subtype, $title, $params,
809 &$this->comment, &$this->revert, $this->entry->getTimestamp() ) );
810
811 return $this->revert;
812 }
813 }
814
815 /**
816 * This class formats move log entries.
817 * @since 1.19
818 */
819 class MoveLogFormatter extends LogFormatter {
820 public function getPreloadTitles() {
821 $params = $this->extractParameters();
822 return array( Title::newFromText( $params[3] ) );
823 }
824
825 protected function getMessageKey() {
826 $key = parent::getMessageKey();
827 $params = $this->getMessageParameters();
828 if ( isset( $params[4] ) && $params[4] === '1' ) {
829 $key .= '-noredirect';
830 }
831 return $key;
832 }
833
834 protected function getMessageParameters() {
835 $params = parent::getMessageParameters();
836 $oldname = $this->makePageLink( $this->entry->getTarget(), array( 'redirect' => 'no' ) );
837 $newname = $this->makePageLink( Title::newFromText( $params[3] ) );
838 $params[2] = Message::rawParam( $oldname );
839 $params[3] = Message::rawParam( $newname );
840 return $params;
841 }
842
843 public function getActionLinks() {
844 if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) // Action is hidden
845 || $this->entry->getSubtype() !== 'move'
846 || !$this->context->getUser()->isAllowed( 'move' ) )
847 {
848 return '';
849 }
850
851 $params = $this->extractParameters();
852 $destTitle = Title::newFromText( $params[3] );
853 if ( !$destTitle ) {
854 return '';
855 }
856
857 $revert = Linker::linkKnown(
858 SpecialPage::getTitleFor( 'Movepage' ),
859 $this->msg( 'revertmove' )->escaped(),
860 array(),
861 array(
862 'wpOldTitle' => $destTitle->getPrefixedDBkey(),
863 'wpNewTitle' => $this->entry->getTarget()->getPrefixedDBkey(),
864 'wpReason' => $this->msg( 'revertmove' )->inContentLanguage()->text(),
865 'wpMovetalk' => 0
866 )
867 );
868 return $this->msg( 'parentheses' )->rawParams( $revert )->escaped();
869 }
870 }
871
872 /**
873 * This class formats delete log entries.
874 * @since 1.19
875 */
876 class DeleteLogFormatter extends LogFormatter {
877 protected function getMessageKey() {
878 $key = parent::getMessageKey();
879 if ( in_array( $this->entry->getSubtype(), array( 'event', 'revision' ) ) ) {
880 if ( count( $this->getMessageParameters() ) < 5 ) {
881 return "$key-legacy";
882 }
883 }
884 return $key;
885 }
886
887 protected function getMessageParameters() {
888 if ( isset( $this->parsedParametersDeleteLog ) ) {
889 return $this->parsedParametersDeleteLog;
890 }
891
892 $params = parent::getMessageParameters();
893 $subtype = $this->entry->getSubtype();
894 if ( in_array( $subtype, array( 'event', 'revision' ) ) ) {
895 // $params[3] here is 'revision' for page revisions, 'oldimage' for file versions, or a comma-separated list of log_ids for log entries.
896 // $subtype here is 'revision' for page revisions and file versions, or 'event' for log entries.
897 if (
898 ( $subtype === 'event' && count( $params ) === 6 ) ||
899 ( $subtype === 'revision' && isset( $params[3] ) && ( $params[3] === 'revision' || $params[3] === 'oldimage' ) )
900 ) {
901 $paramStart = $subtype === 'revision' ? 4 : 3;
902
903 $old = $this->parseBitField( $params[$paramStart+1] );
904 $new = $this->parseBitField( $params[$paramStart+2] );
905 list( $hid, $unhid, $extra ) = RevisionDeleter::getChanges( $new, $old );
906 $changes = array();
907 foreach ( $hid as $v ) {
908 $changes[] = $this->msg( "$v-hid" )->plain();
909 }
910 foreach ( $unhid as $v ) {
911 $changes[] = $this->msg( "$v-unhid" )->plain();
912 }
913 foreach ( $extra as $v ) {
914 $changes[] = $this->msg( $v )->plain();
915 }
916 $changeText = $this->context->getLanguage()->listToText( $changes );
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 $subtype = $this->entry->getSubtype();
1089 if ( $subtype === 'create2' || $subtype === 'byemail' ) {
1090 if ( isset( $params[3] ) ) {
1091 $target = User::newFromId( $params[3] );
1092 } else {
1093 $target = User::newFromName( $this->entry->getTarget()->getText(), false );
1094 }
1095 $params[2] = Message::rawParam( $this->makeUserLink( $target ) );
1096 $params[3] = $target->getName();
1097 }
1098 return $params;
1099 }
1100
1101 public function getComment() {
1102 $timestamp = wfTimestamp( TS_MW, $this->entry->getTimestamp() );
1103 if ( $timestamp < '20080129000000' ) {
1104 # Suppress $comment from old entries (before 2008-01-29),
1105 # not needed and can contain incorrect links
1106 return '';
1107 }
1108 return parent::getComment();
1109 }
1110
1111 public function getPreloadTitles() {
1112 $subtype = $this->entry->getSubtype();
1113 if ( $subtype === 'create2' || $subtype === 'byemail' ) {
1114 //add the user talk to LinkBatch for the userLink
1115 return array( Title::makeTitle( NS_USER_TALK, $this->entry->getTarget()->getText() ) );
1116 }
1117 return array();
1118 }
1119 }
1120
1121 /**
1122 * This class formats rights log entries.
1123 * @since 1.21
1124 */
1125 class RightsLogFormatter extends LogFormatter {
1126 protected function makePageLink( Title $title = null, $parameters = array() ) {
1127 global $wgContLang, $wgUserrightsInterwikiDelimiter;
1128
1129 if ( !$this->plaintext ) {
1130 $text = $wgContLang->ucfirst( $title->getText() );
1131 $parts = explode( $wgUserrightsInterwikiDelimiter, $text, 2 );
1132
1133 if ( count( $parts ) === 2 ) {
1134 $titleLink = WikiMap::foreignUserLink( $parts[1], $parts[0],
1135 htmlspecialchars( $title->getPrefixedText() ) );
1136
1137 if ( $titleLink !== false ) {
1138 return $titleLink;
1139 }
1140 }
1141 }
1142
1143 return parent::makePageLink( $title, $parameters );
1144 }
1145
1146 protected function getMessageKey() {
1147 $key = parent::getMessageKey();
1148 $params = $this->getMessageParameters();
1149 if ( !isset( $params[3] ) && !isset( $params[4] ) ) {
1150 $key .= '-legacy';
1151 }
1152 return $key;
1153 }
1154
1155 protected function getMessageParameters() {
1156 $params = parent::getMessageParameters();
1157
1158 // Really old entries
1159 if ( !isset( $params[3] ) && !isset( $params[4] ) ) {
1160 return $params;
1161 }
1162
1163 $oldGroups = $params[3];
1164 $newGroups = $params[4];
1165
1166 // Less old entries
1167 if ( $oldGroups === '' ) {
1168 $oldGroups = array();
1169 } elseif ( is_string( $oldGroups ) ) {
1170 $oldGroups = array_map( 'trim', explode( ',', $oldGroups ) );
1171 }
1172 if ( $newGroups === '' ) {
1173 $newGroups = array();
1174 } elseif ( is_string( $newGroups ) ) {
1175 $newGroups = array_map( 'trim', explode( ',', $newGroups ) );
1176 }
1177
1178 $userName = $this->entry->getTarget()->getText();
1179 if ( !$this->plaintext && count( $oldGroups ) ) {
1180 foreach ( $oldGroups as &$group ) {
1181 $group = User::getGroupMember( $group, $userName );
1182 }
1183 }
1184 if ( !$this->plaintext && count( $newGroups ) ) {
1185 foreach ( $newGroups as &$group ) {
1186 $group = User::getGroupMember( $group, $userName );
1187 }
1188 }
1189
1190 $lang = $this->context->getLanguage();
1191 if ( count( $oldGroups ) ) {
1192 $params[3] = $lang->listToText( $oldGroups );
1193 } else {
1194 $params[3] = $this->msg( 'rightsnone' )->text();
1195 }
1196 if ( count( $newGroups ) ) {
1197 // Array_values is used here because of bug 42211
1198 // see use of array_unique in UserrightsPage::doSaveUserGroups on $newGroups.
1199 $params[4] = $lang->listToText( array_values( $newGroups ) );
1200 } else {
1201 $params[4] = $this->msg( 'rightsnone' )->text();
1202 }
1203
1204 return $params;
1205 }
1206 }