summaryrefslogtreecommitdiff
blob: 514b14a8a6f10abe77d9ce7f1e1c9bf595885638 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
class EchoCoreThanksPresentationModel extends EchoEventPresentationModel {
	/** @var LogEntry|bool|null */
	private $logEntry;

	public function canRender() {
		$hasTitle = (bool)$this->event->getTitle();
		if ( $this->getThankType() === 'log' ) {
			$logEntry = $this->getLogEntry();
			return $hasTitle && $logEntry && !$logEntry->getDeleted();
		}
		return $hasTitle;
	}

	public function getIconType() {
		return 'thanks';
	}

	public function getHeaderMessage() {
		$type = $this->getThankType();
		if ( $this->isBundled() ) {
			// Message is either notification-bundle-header-rev-thank
			// or notification-bundle-header-log-thank.
			$msg = $this->msg( "notification-bundle-header-$type-thank" );
			$msg->params( $this->getBundleCount() );
			$msg->params( $this->getTruncatedTitleText( $this->event->getTitle(), true ) );
			$msg->params( $this->getViewingUserForGender() );
			return $msg;
		} else {
			if ( $this->event->getExtraParam( 'revcreation', null ) ) {
				// This is a thank on a page creation revision.
				$msg = $this->getMessageWithAgent( "notification-header-creation-thank" );
			} else {
				// Message is either notification-header-rev-thank or notification-header-log-thank.
				$msg = $this->getMessageWithAgent( "notification-header-$type-thank" );
			}
			$msg->params( $this->getTruncatedTitleText( $this->event->getTitle(), true ) );
			$msg->params( $this->getViewingUserForGender() );
			return $msg;
		}
	}

	public function getCompactHeaderMessage() {
		$msg = parent::getCompactHeaderMessage();
		$msg->params( $this->getViewingUserForGender() );
		return $msg;
	}

	public function getBodyMessage() {
		$comment = $this->getRevOrLogComment();
		if ( $comment ) {
			$msg = new RawMessage( '$1' );
			$msg->plaintextParams( $comment );
			return $msg;
		}
	}

	private function getRevisionEditSummary() {
		if ( !$this->userCan( Revision::DELETED_COMMENT ) ) {
			return false;
		}

		$revId = $this->event->getExtraParam( 'revid', false );
		if ( !$revId ) {
			return false;
		}

		$revision = Revision::newFromId( $revId );
		if ( !$revision ) {
			return false;
		}

		$summary = $revision->getComment( Revision::RAW );
		return $summary ?: false;
	}

	/**
	 * Get the comment/summary/excerpt of the log entry or revision,
	 * for use in the notification body.
	 * @return string|bool The comment or false if it could not be retrieved.
	 */
	protected function getRevOrLogComment() {
		if ( $this->event->getExtraParam( 'logid' ) ) {
			$logEntry = $this->getLogEntry();
			if ( !$logEntry ) {
				return '';
			}
			$formatter = LogFormatter::newFromEntry( $logEntry );
			$excerpt = $formatter->getPlainActionText();
			// Turn wikitext into plaintext
			$excerpt = Linker::formatComment( $excerpt );
			$excerpt = Sanitizer::stripAllTags( $excerpt );
			return $excerpt;
		} else {
			// Try to get edit summary.
			$summary = $this->getRevisionEditSummary();
			if ( $summary ) {
				return $summary;
			}
			// Fallback on edit excerpt.
			if ( $this->userCan( Revision::DELETED_TEXT ) ) {
				return $this->event->getExtraParam( 'excerpt', false );
			}
		}
	}

	public function getPrimaryLink() {
		$logId = $this->event->getExtraParam( 'logid' );
		if ( $logId ) {
			$url = SpecialPage::getTitleFor( 'Log' )->getLocalURL( [ 'logid' => $logId ] );
			$label = 'notification-link-text-view-logentry';
		} else {
			$url = $this->event->getTitle()->getLocalURL( [
				'oldid' => 'prev',
				'diff' => $this->event->getExtraParam( 'revid' )
			] );
			$label = 'notification-link-text-view-edit';
		}
		return [
			'url' => $url,
			// Label is only used for non-JS clients.
			'label' => $this->msg( $label )->text(),
		];
	}

	public function getSecondaryLinks() {
		$pageLink = $this->getPageLink( $this->event->getTitle(), null, true );
		if ( $this->isBundled() ) {
			return [ $pageLink ];
		} else {
			return [ $this->getAgentLink(), $pageLink ];
		}
	}

	/**
	 * @return LogEntry|false
	 */
	private function getLogEntry() {
		if ( $this->logEntry !== null ) {
			return $this->logEntry;
		}
		$logId = $this->event->getExtraParam( 'logid' );
		if ( !$logId ) {
			$this->logEntry = false;
		} else {
			$this->logEntry = DatabaseLogEntry::newFromId( $logId, wfGetDB( DB_REPLICA ) );
			if ( !$this->logEntry ) {
				$this->logEntry = false;
			}
		}
		return $this->logEntry;
	}

	/**
	 * Returns thank type
	 *
	 * @return string 'log' or 'rev'
	 */
	private function getThankType() {
		return $this->event->getExtraParam( 'logid' ) ? 'log' : 'rev';
	}
}