7586bb6548d569ee7373abd056a3845ba82b25ae
[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
297 // case 'suppress' --private log -- aaron (sign your messages so we know who to blame in a few years :-D)
298 // default:
299 }
300 if( is_null( $text ) ) {
301 $text = $this->getPlainActionText();
302 }
303
304 $this->plaintext = false;
305 $this->irctext = false;
306 return $text;
307 }
308
309 /**
310 * Gets the log action, including username.
311 * @return string HTML
312 */
313 public function getActionText() {
314 if ( $this->canView( LogPage::DELETED_ACTION ) ) {
315 $element = $this->getActionMessage();
316 if ( $element instanceof Message ) {
317 $element = $this->plaintext ? $element->text() : $element->escaped();
318 }
319 if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) ) {
320 $element = $this->styleRestricedElement( $element );
321 }
322 } else {
323 $performer = $this->getPerformerElement() . $this->msg( 'word-separator' )->text();
324 $element = $performer . $this->getRestrictedElement( 'rev-deleted-event' );
325 }
326
327 return $element;
328 }
329
330 /**
331 * Returns a sentence describing the log action. Usually
332 * a Message object is returned, but old style log types
333 * and entries might return pre-escaped html string.
334 * @return Message|string pre-escaped html
335 */
336 protected function getActionMessage() {
337 $message = $this->msg( $this->getMessageKey() );
338 $message->params( $this->getMessageParameters() );
339 return $message;
340 }
341
342 /**
343 * Returns a key to be used for formatting the action sentence.
344 * Default is logentry-TYPE-SUBTYPE for modern logs. Legacy log
345 * types will use custom keys, and subclasses can also alter the
346 * key depending on the entry itself.
347 * @return string message key
348 */
349 protected function getMessageKey() {
350 $type = $this->entry->getType();
351 $subtype = $this->entry->getSubtype();
352
353 return "logentry-$type-$subtype";
354 }
355
356 /**
357 * Returns extra links that comes after the action text, like "revert", etc.
358 *
359 * @return string
360 */
361 public function getActionLinks() {
362 return '';
363 }
364
365 /**
366 * Extracts the optional extra parameters for use in action messages.
367 * The array indexes start from number 3.
368 * @return array
369 */
370 protected function extractParameters() {
371 $entry = $this->entry;
372 $params = array();
373
374 if ( $entry->isLegacy() ) {
375 foreach ( $entry->getParameters() as $index => $value ) {
376 $params[$index + 3] = $value;
377 }
378 }
379
380 // Filter out parameters which are not in format #:foo
381 foreach ( $entry->getParameters() as $key => $value ) {
382 if ( strpos( $key, ':' ) === false ) continue;
383 list( $index, $type, $name ) = explode( ':', $key, 3 );
384 $params[$index - 1] = $value;
385 }
386
387 /* Message class doesn't like non consecutive numbering.
388 * Fill in missing indexes with empty strings to avoid
389 * incorrect renumbering.
390 */
391 if ( count( $params ) ) {
392 $max = max( array_keys( $params ) );
393 for ( $i = 4; $i < $max; $i++ ) {
394 if ( !isset( $params[$i] ) ) {
395 $params[$i] = '';
396 }
397 }
398 }
399 return $params;
400 }
401
402 /**
403 * Formats parameters intented for action message from
404 * array of all parameters. There are three hardcoded
405 * parameters (array is zero-indexed, this list not):
406 * - 1: user name with premade link
407 * - 2: usable for gender magic function
408 * - 3: target page with premade link
409 * @return array
410 */
411 protected function getMessageParameters() {
412 if ( isset( $this->parsedParameters ) ) {
413 return $this->parsedParameters;
414 }
415
416 $entry = $this->entry;
417 $params = $this->extractParameters();
418 $params[0] = Message::rawParam( $this->getPerformerElement() );
419 $params[1] = $entry->getPerformer()->getName();
420 $params[2] = Message::rawParam( $this->makePageLink( $entry->getTarget() ) );
421
422 // Bad things happens if the numbers are not in correct order
423 ksort( $params );
424 return $this->parsedParameters = $params;
425 }
426
427 /**
428 * Helper to make a link to the page, taking the plaintext
429 * value in consideration.
430 * @param $title Title the page
431 * @param $parameters array query parameters
432 * @return String
433 */
434 protected function makePageLink( Title $title = null, $parameters = array() ) {
435 if ( !$this->plaintext ) {
436 $link = Linker::link( $title, null, array(), $parameters );
437 } else {
438 if ( !$title instanceof Title ) {
439 throw new MWException( "Expected title, got null" );
440 }
441 $link = '[[' . $title->getPrefixedText() . ']]';
442 }
443 return $link;
444 }
445
446 /**
447 * Provides the name of the user who performed the log action.
448 * Used as part of log action message or standalone, depending
449 * which parts of the log entry has been hidden.
450 * @return String
451 */
452 public function getPerformerElement() {
453 if ( $this->canView( LogPage::DELETED_USER ) ) {
454 $performer = $this->entry->getPerformer();
455 $element = $this->makeUserLink( $performer );
456 if ( $this->entry->isDeleted( LogPage::DELETED_USER ) ) {
457 $element = $this->styleRestricedElement( $element );
458 }
459 } else {
460 $element = $this->getRestrictedElement( 'rev-deleted-user' );
461 }
462
463 return $element;
464 }
465
466 /**
467 * Gets the luser provided comment
468 * @return string HTML
469 */
470 public function getComment() {
471 if ( $this->canView( LogPage::DELETED_COMMENT ) ) {
472 $comment = Linker::commentBlock( $this->entry->getComment() );
473 // No hard coded spaces thanx
474 $element = ltrim( $comment );
475 if ( $this->entry->isDeleted( LogPage::DELETED_COMMENT ) ) {
476 $element = $this->styleRestricedElement( $element );
477 }
478 } else {
479 $element = $this->getRestrictedElement( 'rev-deleted-comment' );
480 }
481
482 return $element;
483 }
484
485 /**
486 * Helper method for displaying restricted element.
487 * @param $message string
488 * @return string HTML or wikitext
489 */
490 protected function getRestrictedElement( $message ) {
491 if ( $this->plaintext ) {
492 return $this->msg( $message )->text();
493 }
494
495 $content = $this->msg( $message )->escaped();
496 $attribs = array( 'class' => 'history-deleted' );
497 return Html::rawElement( 'span', $attribs, $content );
498 }
499
500 /**
501 * Helper method for styling restricted element.
502 * @param $content string
503 * @return string HTML or wikitext
504 */
505 protected function styleRestricedElement( $content ) {
506 if ( $this->plaintext ) {
507 return $content;
508 }
509 $attribs = array( 'class' => 'history-deleted' );
510 return Html::rawElement( 'span', $attribs, $content );
511 }
512
513 /**
514 * Shortcut for wfMessage which honors local context.
515 * @todo Would it be better to require replacing the global context instead?
516 * @param $key string
517 * @return Message
518 */
519 protected function msg( $key ) {
520 return $this->context->msg( $key );
521 }
522
523 protected function makeUserLink( User $user ) {
524 if ( $this->plaintext ) {
525 $element = $user->getName();
526 } else {
527 $element = Linker::userLink(
528 $user->getId(),
529 $user->getName()
530 );
531
532 if ( $this->linkFlood ) {
533 $element .= Linker::userToolLinksRedContribs(
534 $user->getId(),
535 $user->getName(),
536 $user->getEditCount()
537 );
538 }
539 }
540 return $element;
541 }
542
543 /**
544 * @return Array of titles that should be preloaded with LinkBatch.
545 */
546 public function getPreloadTitles() {
547 return array();
548 }
549
550 }
551
552 /**
553 * This class formats all log entries for log types
554 * which have not been converted to the new system.
555 * This is not about old log entries which store
556 * parameters in a different format - the new
557 * LogFormatter classes have code to support formatting
558 * those too.
559 * @since 1.19
560 */
561 class LegacyLogFormatter extends LogFormatter {
562
563 /**
564 * Backward compatibility for extension changing the comment from
565 * the LogLine hook. This will be set by the first call on getComment(),
566 * then it might be modified by the hook when calling getActionLinks(),
567 * so that the modified value will be returned when calling getComment()
568 * a second time.
569 *
570 * @var string|null
571 */
572 private $comment = null;
573
574 /**
575 * Cache for the result of getActionLinks() so that it does not need to
576 * run multiple times depending on the order that getComment() and
577 * getActionLinks() are called.
578 *
579 * @var string|null
580 */
581 private $revert = null;
582
583 public function getComment() {
584 if ( $this->comment === null ) {
585 $this->comment = parent::getComment();
586 }
587
588 // Make sure we execute the LogLine hook so that we immediately return
589 // the correct value.
590 if ( $this->revert === null ) {
591 $this->getActionLinks();
592 }
593
594 return $this->comment;
595 }
596
597 protected function getActionMessage() {
598 $entry = $this->entry;
599 $action = LogPage::actionText(
600 $entry->getType(),
601 $entry->getSubtype(),
602 $entry->getTarget(),
603 $this->plaintext ? null : $this->context->getSkin(),
604 (array)$entry->getParameters(),
605 !$this->plaintext // whether to filter [[]] links
606 );
607
608 $performer = $this->getPerformerElement();
609 if ( !$this->irctext ) {
610 $action = $performer . $this->msg( 'word-separator' )->text() . $action;
611 }
612
613 return $action;
614 }
615
616 public function getActionLinks() {
617 if ( $this->revert !== null ) {
618 return $this->revert;
619 }
620
621 if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) ) {
622 return $this->revert = '';
623 }
624
625 $title = $this->entry->getTarget();
626 $type = $this->entry->getType();
627 $subtype = $this->entry->getSubtype();
628
629 // Show unblock/change block link
630 if ( ( $type == 'block' || $type == 'suppress' ) && ( $subtype == 'block' || $subtype == 'reblock' ) ) {
631 if ( !$this->context->getUser()->isAllowed( 'block' ) ) {
632 return '';
633 }
634
635 $links = array(
636 Linker::linkKnown(
637 SpecialPage::getTitleFor( 'Unblock', $title->getDBkey() ),
638 $this->msg( 'unblocklink' )->escaped()
639 ),
640 Linker::linkKnown(
641 SpecialPage::getTitleFor( 'Block', $title->getDBkey() ),
642 $this->msg( 'change-blocklink' )->escaped()
643 )
644 );
645 return $this->msg( 'parentheses' )->rawParams(
646 $this->context->getLanguage()->pipeList( $links ) )->escaped();
647 // Show change protection link
648 } elseif ( $type == 'protect' && ( $subtype == 'protect' || $subtype == 'modify' || $subtype == 'unprotect' ) ) {
649 $links = array(
650 Linker::link( $title,
651 $this->msg( 'hist' )->escaped(),
652 array(),
653 array(
654 'action' => 'history',
655 'offset' => $this->entry->getTimestamp()
656 )
657 )
658 );
659 if ( $this->context->getUser()->isAllowed( 'protect' ) ) {
660 $links[] = Linker::linkKnown(
661 $title,
662 $this->msg( 'protect_change' )->escaped(),
663 array(),
664 array( 'action' => 'protect' )
665 );
666 }
667 return $this->msg( 'parentheses' )->rawParams(
668 $this->context->getLanguage()->pipeList( $links ) )->escaped();
669 // Show unmerge link
670 } elseif( $type == 'merge' && $subtype == 'merge' ) {
671 if ( !$this->context->getUser()->isAllowed( 'mergehistory' ) ) {
672 return '';
673 }
674
675 $params = $this->extractParameters();
676 $revert = Linker::linkKnown(
677 SpecialPage::getTitleFor( 'MergeHistory' ),
678 $this->msg( 'revertmerge' )->escaped(),
679 array(),
680 array(
681 'target' => $params[3],
682 'dest' => $title->getPrefixedDBkey(),
683 'mergepoint' => $params[4]
684 )
685 );
686 return $this->msg( 'parentheses' )->rawParams( $revert )->escaped();
687 }
688
689 // Do nothing. The implementation is handled by the hook modifiying the
690 // passed-by-ref parameters. This also changes the default value so that
691 // getComment() and getActionLinks() do not call them indefinitely.
692 $this->revert = '';
693
694 // This is to populate the $comment member of this instance so that it
695 // can be modified when calling the hook just below.
696 if ( $this->comment === null ) {
697 $this->getComment();
698 }
699
700 $params = $this->entry->getParameters();
701
702 wfRunHooks( 'LogLine', array( $type, $subtype, $title, $params,
703 &$this->comment, &$this->revert, $this->entry->getTimestamp() ) );
704
705 return $this->revert;
706 }
707 }
708
709 /**
710 * This class formats move log entries.
711 * @since 1.19
712 */
713 class MoveLogFormatter extends LogFormatter {
714 public function getPreloadTitles() {
715 $params = $this->extractParameters();
716 return array( Title::newFromText( $params[3] ) );
717 }
718
719 protected function getMessageKey() {
720 $key = parent::getMessageKey();
721 $params = $this->getMessageParameters();
722 if ( isset( $params[4] ) && $params[4] === '1' ) {
723 $key .= '-noredirect';
724 }
725 return $key;
726 }
727
728 protected function getMessageParameters() {
729 $params = parent::getMessageParameters();
730 $oldname = $this->makePageLink( $this->entry->getTarget(), array( 'redirect' => 'no' ) );
731 $newname = $this->makePageLink( Title::newFromText( $params[3] ) );
732 $params[2] = Message::rawParam( $oldname );
733 $params[3] = Message::rawParam( $newname );
734 return $params;
735 }
736
737 public function getActionLinks() {
738 if ( $this->entry->isDeleted( LogPage::DELETED_ACTION ) // Action is hidden
739 || $this->entry->getSubtype() !== 'move'
740 || !$this->context->getUser()->isAllowed( 'move' ) )
741 {
742 return '';
743 }
744
745 $params = $this->extractParameters();
746 $destTitle = Title::newFromText( $params[3] );
747 if ( !$destTitle ) {
748 return '';
749 }
750
751 $revert = Linker::linkKnown(
752 SpecialPage::getTitleFor( 'Movepage' ),
753 $this->msg( 'revertmove' )->escaped(),
754 array(),
755 array(
756 'wpOldTitle' => $destTitle->getPrefixedDBkey(),
757 'wpNewTitle' => $this->entry->getTarget()->getPrefixedDBkey(),
758 'wpReason' => $this->msg( 'revertmove' )->inContentLanguage()->text(),
759 'wpMovetalk' => 0
760 )
761 );
762 return $this->msg( 'parentheses' )->rawParams( $revert )->escaped();
763 }
764 }
765
766 /**
767 * This class formats delete log entries.
768 * @since 1.19
769 */
770 class DeleteLogFormatter extends LogFormatter {
771 protected function getMessageKey() {
772 $key = parent::getMessageKey();
773 if ( in_array( $this->entry->getSubtype(), array( 'event', 'revision' ) ) ) {
774 if ( count( $this->getMessageParameters() ) < 5 ) {
775 return "$key-legacy";
776 }
777 }
778 return $key;
779 }
780
781 protected function getMessageParameters() {
782 if ( isset( $this->parsedParametersDeleteLog ) ) {
783 return $this->parsedParametersDeleteLog;
784 }
785
786 $params = parent::getMessageParameters();
787 $subtype = $this->entry->getSubtype();
788 if ( in_array( $subtype, array( 'event', 'revision' ) ) ) {
789 if (
790 ($subtype === 'event' && count( $params ) === 6 ) ||
791 ($subtype === 'revision' && isset( $params[3] ) && $params[3] === 'revision' )
792 ) {
793 $paramStart = $subtype === 'revision' ? 4 : 3;
794
795 $old = $this->parseBitField( $params[$paramStart+1] );
796 $new = $this->parseBitField( $params[$paramStart+2] );
797 list( $hid, $unhid, $extra ) = RevisionDeleter::getChanges( $new, $old );
798 $changes = array();
799 foreach ( $hid as $v ) {
800 $changes[] = $this->msg( "$v-hid" )->plain();
801 }
802 foreach ( $unhid as $v ) {
803 $changes[] = $this->msg( "$v-unhid" )->plain();
804 }
805 foreach ( $extra as $v ) {
806 $changes[] = $this->msg( $v )->plain();
807 }
808 $changeText = $this->context->getLanguage()->listToText( $changes );
809
810
811 $newParams = array_slice( $params, 0, 3 );
812 $newParams[3] = $changeText;
813 $count = count( explode( ',', $params[$paramStart] ) );
814 $newParams[4] = $this->context->getLanguage()->formatNum( $count );
815 return $this->parsedParametersDeleteLog = $newParams;
816 } else {
817 return $this->parsedParametersDeleteLog = array_slice( $params, 0, 3 );
818 }
819 }
820
821 return $this->parsedParametersDeleteLog = $params;
822 }
823
824 protected function parseBitField( $string ) {
825 // Input is like ofield=2134 or just the number
826 if ( strpos( $string, 'field=' ) === 1 ) {
827 list( , $field ) = explode( '=', $string );
828 return (int) $field;
829 } else {
830 return (int) $string;
831 }
832 }
833
834 public function getActionLinks() {
835 $user = $this->context->getUser();
836 if ( !$user->isAllowed( 'deletedhistory' ) || $this->entry->isDeleted( LogPage::DELETED_ACTION ) ) {
837 return '';
838 }
839
840 switch ( $this->entry->getSubtype() ) {
841 case 'delete': // Show undelete link
842 if( $user->isAllowed( 'undelete' ) ) {
843 $message = 'undeletelink';
844 } else {
845 $message = 'undeleteviewlink';
846 }
847 $revert = Linker::linkKnown(
848 SpecialPage::getTitleFor( 'Undelete' ),
849 $this->msg( $message )->escaped(),
850 array(),
851 array( 'target' => $this->entry->getTarget()->getPrefixedDBkey() )
852 );
853 return $this->msg( 'parentheses' )->rawParams( $revert )->escaped();
854
855 case 'revision': // If an edit was hidden from a page give a review link to the history
856 $params = $this->extractParameters();
857 if ( !isset( $params[3] ) || !isset( $params[4] ) ) {
858 return '';
859 }
860
861 // Different revision types use different URL params...
862 $key = $params[3];
863 // This is a CSV of the IDs
864 $ids = explode( ',', $params[4] );
865
866 $links = array();
867
868 // If there's only one item, we can show a diff link
869 if ( count( $ids ) == 1 ) {
870 // Live revision diffs...
871 if ( $key == 'oldid' || $key == 'revision' ) {
872 $links[] = Linker::linkKnown(
873 $this->entry->getTarget(),
874 $this->msg( 'diff' )->escaped(),
875 array(),
876 array(
877 'diff' => intval( $ids[0] ),
878 'unhide' => 1
879 )
880 );
881 // Deleted revision diffs...
882 } elseif ( $key == 'artimestamp' || $key == 'archive' ) {
883 $links[] = Linker::linkKnown(
884 SpecialPage::getTitleFor( 'Undelete' ),
885 $this->msg( 'diff' )->escaped(),
886 array(),
887 array(
888 'target' => $this->entry->getTarget()->getPrefixedDBKey(),
889 'diff' => 'prev',
890 'timestamp' => $ids[0]
891 )
892 );
893 }
894 }
895
896 // View/modify link...
897 $links[] = Linker::linkKnown(
898 SpecialPage::getTitleFor( 'Revisiondelete' ),
899 $this->msg( 'revdel-restore' )->escaped(),
900 array(),
901 array(
902 'target' => $this->entry->getTarget()->getPrefixedText(),
903 'type' => $key,
904 'ids' => implode( ',', $ids ),
905 )
906 );
907
908 return $this->msg( 'parentheses' )->rawParams(
909 $this->context->getLanguage()->pipeList( $links ) )->escaped();
910
911 case 'event': // Hidden log items, give review link
912 $params = $this->extractParameters();
913 if ( !isset( $params[3] ) ) {
914 return '';
915 }
916 // This is a CSV of the IDs
917 $query = $params[3];
918 // Link to each hidden object ID, $params[1] is the url param
919 $revert = Linker::linkKnown(
920 SpecialPage::getTitleFor( 'Revisiondelete' ),
921 $this->msg( 'revdel-restore' )->escaped(),
922 array(),
923 array(
924 'target' => $this->entry->getTarget()->getPrefixedText(),
925 'type' => 'logging',
926 'ids' => $query
927 )
928 );
929 return $this->msg( 'parentheses' )->rawParams( $revert )->escaped();
930 default:
931 return '';
932 }
933 }
934 }
935
936 /**
937 * This class formats patrol log entries.
938 * @since 1.19
939 */
940 class PatrolLogFormatter extends LogFormatter {
941 protected function getMessageKey() {
942 $key = parent::getMessageKey();
943 $params = $this->getMessageParameters();
944 if ( isset( $params[5] ) && $params[5] ) {
945 $key .= '-auto';
946 }
947 return $key;
948 }
949
950 protected function getMessageParameters() {
951 $params = parent::getMessageParameters();
952
953 $target = $this->entry->getTarget();
954 $oldid = $params[3];
955 $revision = $this->context->getLanguage()->formatNum( $oldid, true );
956
957 if ( $this->plaintext ) {
958 $revlink = $revision;
959 } elseif ( $target->exists() ) {
960 $query = array(
961 'oldid' => $oldid,
962 'diff' => 'prev'
963 );
964 $revlink = Linker::link( $target, htmlspecialchars( $revision ), array(), $query );
965 } else {
966 $revlink = htmlspecialchars( $revision );
967 }
968
969 $params[3] = Message::rawParam( $revlink );
970 return $params;
971 }
972 }
973
974 /**
975 * This class formats new user log entries.
976 * @since 1.19
977 */
978 class NewUsersLogFormatter extends LogFormatter {
979 protected function getMessageParameters() {
980 $params = parent::getMessageParameters();
981 if ( $this->entry->getSubtype() === 'create2' ) {
982 if ( isset( $params[3] ) ) {
983 $target = User::newFromId( $params[3] );
984 } else {
985 $target = User::newFromName( $this->entry->getTarget()->getText(), false );
986 }
987 $params[2] = Message::rawParam( $this->makeUserLink( $target ) );
988 $params[3] = $target->getName();
989 }
990 return $params;
991 }
992
993 public function getComment() {
994 $timestamp = wfTimestamp( TS_MW, $this->entry->getTimestamp() );
995 if ( $timestamp < '20080129000000' ) {
996 # Suppress $comment from old entries (before 2008-01-29),
997 # not needed and can contain incorrect links
998 return '';
999 }
1000 return parent::getComment();
1001 }
1002
1003 public function getPreloadTitles() {
1004 if ( $this->entry->getSubtype() === 'create2' ) {
1005 //add the user talk to LinkBatch for the userLink
1006 return array( Title::makeTitle( NS_USER_TALK, $this->entry->getTarget()->getText() ) );
1007 }
1008 return array();
1009 }
1010 }