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