Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 77 |
|
0.00% |
0 / 11 |
CRAP | |
0.00% |
0 / 1 |
EchoCoreThanksPresentationModel | |
0.00% |
0 / 77 |
|
0.00% |
0 / 11 |
992 | |
0.00% |
0 / 1 |
canRender | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
getIconType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getHeaderMessage | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
getCompactHeaderMessage | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getBodyMessage | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
getRevisionEditSummary | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
getRevOrLogComment | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
42 | |||
getPrimaryLink | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
getSecondaryLinks | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getLogEntry | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
getThankType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\Thanks; |
4 | |
5 | use DatabaseLogEntry; |
6 | use LogEntry; |
7 | use LogFormatter; |
8 | use LogPage; |
9 | use MediaWiki\Extension\Notifications\Formatters\EchoEventPresentationModel; |
10 | use MediaWiki\Language\RawMessage; |
11 | use MediaWiki\MediaWikiServices; |
12 | use MediaWiki\Parser\Sanitizer; |
13 | use MediaWiki\Revision\RevisionRecord; |
14 | use MediaWiki\SpecialPage\SpecialPage; |
15 | |
16 | class EchoCoreThanksPresentationModel extends EchoEventPresentationModel { |
17 | /** @var LogEntry|bool|null */ |
18 | private $logEntry; |
19 | |
20 | public function canRender() { |
21 | $hasTitle = (bool)$this->event->getTitle(); |
22 | if ( $hasTitle && $this->getThankType() === 'log' ) { |
23 | $logEntry = $this->getLogEntry(); |
24 | return $logEntry && !( |
25 | // the notification renders the message on Special:Log without the comment, |
26 | // so check $logEntry is not deleted, or only its comment is deleted |
27 | $logEntry->getDeleted() & ~LogPage::DELETED_COMMENT |
28 | ); |
29 | } |
30 | return $hasTitle; |
31 | } |
32 | |
33 | public function getIconType() { |
34 | return 'thanks'; |
35 | } |
36 | |
37 | public function getHeaderMessage() { |
38 | $type = $this->getThankType(); |
39 | if ( $this->isBundled() ) { |
40 | // Message is either notification-bundle-header-rev-thank |
41 | // or notification-bundle-header-log-thank. |
42 | $msg = $this->msg( "notification-bundle-header-$type-thank" ); |
43 | $msg->params( $this->getBundleCount() ); |
44 | $msg->params( $this->getTruncatedTitleText( $this->event->getTitle(), true ) ); |
45 | $msg->params( $this->getViewingUserForGender() ); |
46 | return $msg; |
47 | } else { |
48 | if ( $this->event->getExtraParam( 'revcreation', null ) ) { |
49 | // This is a thank on a page creation revision. |
50 | $msg = $this->getMessageWithAgent( "notification-header-creation-thank" ); |
51 | } else { |
52 | // Message is either notification-header-rev-thank or notification-header-log-thank. |
53 | $msg = $this->getMessageWithAgent( "notification-header-$type-thank" ); |
54 | } |
55 | $msg->params( $this->getTruncatedTitleText( $this->event->getTitle(), true ) ); |
56 | $msg->params( $this->getViewingUserForGender() ); |
57 | return $msg; |
58 | } |
59 | } |
60 | |
61 | public function getCompactHeaderMessage() { |
62 | // The following message is used here: |
63 | // * notification-compact-header-edit-thank |
64 | $msg = parent::getCompactHeaderMessage(); |
65 | $msg->params( $this->getViewingUserForGender() ); |
66 | return $msg; |
67 | } |
68 | |
69 | public function getBodyMessage() { |
70 | $comment = $this->getRevOrLogComment(); |
71 | if ( $comment !== '' ) { |
72 | $msg = new RawMessage( '$1' ); |
73 | $msg->plaintextParams( $comment ); |
74 | return $msg; |
75 | } |
76 | return false; |
77 | } |
78 | |
79 | /** |
80 | * @return string|bool The comment or false if it could not be retrieved. |
81 | */ |
82 | private function getRevisionEditSummary() { |
83 | if ( !$this->userCan( RevisionRecord::DELETED_COMMENT ) ) { |
84 | return false; |
85 | } |
86 | |
87 | $revision = $this->event->getRevision(); |
88 | if ( !$revision ) { |
89 | return false; |
90 | } |
91 | |
92 | $summary = $revision->getComment( RevisionRecord::RAW ); |
93 | return $summary ? $summary->text : false; |
94 | } |
95 | |
96 | /** |
97 | * Get the comment/summary/excerpt of the log entry or revision, |
98 | * for use in the notification body. |
99 | * @return string |
100 | */ |
101 | protected function getRevOrLogComment(): string { |
102 | if ( $this->event->getExtraParam( 'logid' ) ) { |
103 | $logEntry = $this->getLogEntry(); |
104 | if ( !$logEntry ) { |
105 | return ''; |
106 | } |
107 | $formatter = LogFormatter::newFromEntry( $logEntry ); |
108 | $excerpt = $formatter->getPlainActionText(); |
109 | // Turn wikitext into plaintext |
110 | $excerpt = MediaWikiServices::getInstance()->getCommentFormatter()->format( $excerpt ); |
111 | return Sanitizer::stripAllTags( $excerpt ); |
112 | } else { |
113 | // Try to get edit summary. |
114 | $summary = $this->getRevisionEditSummary(); |
115 | if ( $summary !== false && $summary !== '' ) { |
116 | return $summary; |
117 | } |
118 | // Fallback on edit excerpt. |
119 | if ( $this->userCan( RevisionRecord::DELETED_TEXT ) ) { |
120 | return $this->event->getExtraParam( 'excerpt', '' ); |
121 | } |
122 | return ''; |
123 | } |
124 | } |
125 | |
126 | public function getPrimaryLink() { |
127 | $logId = $this->event->getExtraParam( 'logid' ); |
128 | if ( $logId ) { |
129 | $url = SpecialPage::getTitleFor( 'Log' )->getLocalURL( [ 'logid' => $logId ] ); |
130 | $label = 'notification-link-text-view-logentry'; |
131 | } else { |
132 | $url = $this->event->getTitle()->getLocalURL( [ |
133 | 'oldid' => 'prev', |
134 | 'diff' => $this->event->getExtraParam( 'revid' ) |
135 | ] ); |
136 | $label = 'notification-link-text-view-edit'; |
137 | } |
138 | return [ |
139 | 'url' => $url, |
140 | // Label is only used for non-JS clients. |
141 | 'label' => $this->msg( $label )->text(), |
142 | ]; |
143 | } |
144 | |
145 | public function getSecondaryLinks() { |
146 | $pageLink = $this->getPageLink( $this->event->getTitle(), '', true ); |
147 | if ( $this->isBundled() ) { |
148 | return [ $pageLink ]; |
149 | } else { |
150 | return [ $this->getAgentLink(), $pageLink ]; |
151 | } |
152 | } |
153 | |
154 | /** |
155 | * @return LogEntry|false |
156 | */ |
157 | private function getLogEntry() { |
158 | if ( $this->logEntry !== null ) { |
159 | return $this->logEntry; |
160 | } |
161 | $logId = $this->event->getExtraParam( 'logid' ); |
162 | if ( !$logId ) { |
163 | $this->logEntry = false; |
164 | } else { |
165 | $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase(); |
166 | $this->logEntry = DatabaseLogEntry::newFromId( $logId, $dbr ) ?: false; |
167 | } |
168 | return $this->logEntry; |
169 | } |
170 | |
171 | /** |
172 | * Returns thank type |
173 | * |
174 | * @return string 'log' or 'rev' |
175 | */ |
176 | private function getThankType() { |
177 | return $this->event->getExtraParam( 'logid' ) ? 'log' : 'rev'; |
178 | } |
179 | } |