Debian Codegraph @

extensions/Echo/includes/Formatters/EchoMentionPresentationModel.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications\Formatters;
4
5 use MediaWiki\Extension\Notifications\DiscussionParser;
6 use MediaWiki\Extension\Notifications\Model\Event;
7 use MediaWiki\Language\Language;
8 use MediaWiki\Revision\RevisionRecord;
9 use MediaWiki\User\User;
10
11 class EchoMentionPresentationModel extends EchoEventPresentationModel {
12
13 /**
14 * @var EchoPresentationModelSection
15 */
16 protected $section;
17
18 /**
19 * @inheritDoc
20 */
21 protected function __construct( Event $event, Language $language, User $user, $distributionType ) {
22 parent::__construct( $event, $language, $user, $distributionType );
23 $this->section = new EchoPresentationModelSection( $event, $user, $language );
24 }
25
26 public function getIconType() {
27 return 'mention';
28 }
29
30 public function canRender() {
31 return (bool)$this->event->getTitle();
32 }
33
34 protected function getHeaderMessageKey() {
35 $hasSection = $this->section->exists();
36 if ( $this->onArticleTalkpage() ) {
37 return $hasSection ?
38 'notification-header-mention-article-talkpage' :
39 'notification-header-mention-article-talkpage-nosection';
40 } elseif ( $this->onAgentTalkpage() ) {
41 return $hasSection ?
42 'notification-header-mention-agent-talkpage' :
43 'notification-header-mention-agent-talkpage-nosection';
44 } elseif ( $this->onUserTalkpage() ) {
45 return $hasSection ?
46 'notification-header-mention-user-talkpage-v2' :
47 'notification-header-mention-user-talkpage-nosection';
48 } else {
49 return $hasSection ?
50 'notification-header-mention-other' :
51 'notification-header-mention-other-nosection';
52 }
53 }
54
55 public function getHeaderMessage() {
56 $msg = $this->getMessageWithAgent( $this->getHeaderMessageKey() );
57 $msg->params( $this->getViewingUserForGender() );
58
59 if ( $this->onArticleTalkpage() ) {
60 $msg->params( $this->getTruncatedTitleText( $this->event->getTitle() ) );
61 } elseif ( $this->onAgentTalkpage() ) {
62 // No params to add here.
63 // If we remove this check, onUserTalkpage() has to
64 // make sure it is a user talk page but NOT the agent's talk page.
65 } elseif ( $this->onUserTalkpage() ) {
66 $username = $this->event->getTitle()->getText();
67 $msg->params( $this->getTruncatedUsername( User::newFromName( $username, false ) ) );
68 $msg->params( $username );
69 } else {
70 $msg->params( $this->getTruncatedTitleText( $this->event->getTitle(), true ) );
71 }
72
73 if ( $this->section->exists() ) {
74 $msg->plaintextParams( $this->section->getTruncatedSectionTitle() );
75 }
76
77 return $msg;
78 }
79
80 public function getBodyMessage() {
81 $content = $this->event->getExtraParam( 'content' );
82 if ( $content && $this->userCan( RevisionRecord::DELETED_TEXT ) ) {
83 $msg = $this->msg( 'notification-body-mention' );
84 $msg->plaintextParams(
85 DiscussionParser::getTextSnippet(
86 $content,
87 $this->language,
88 DiscussionParser::DEFAULT_SNIPPET_LENGTH,
89 $this->event->getTitle()
90 )
91 );
92 return $msg;
93 } else {
94 return false;
95 }
96 }
97
98 public function getPrimaryLink() {
99 return [
100 // Need FullURL so the section is included
101 'url' => $this->section->getTitleWithSection()->getFullURL(),
102 'label' => $this->msg( 'notification-link-text-view-mention' )->text()
103 ];
104 }
105
106 public function getSecondaryLinks() {
107 $url = $this->event->getTitle()->getLocalURL( [
108 'oldid' => 'prev',
109 'diff' => $this->event->getExtraParam( 'revid' )
110 ] );
111 $viewChangesLink = [
112 'url' => $url,
113 'label' => $this->msg( 'notification-link-text-view-changes', $this->getViewingUserForGender() )->text(),
114 'description' => '',
115 'icon' => 'changes',
116 'prioritized' => true,
117 ];
118
119 return [ $this->getAgentLink(), $viewChangesLink ];
120 }
121
122 private function onArticleTalkpage() {
123 return $this->event->getTitle()->getNamespace() === NS_TALK;
124 }
125
126 private function onAgentTalkpage() {
127 return $this->event->getTitle()->equals( $this->event->getAgent()->getTalkPage() );
128 }
129
130 private function onUserTalkpage() {
131 $title = $this->event->getTitle();
132 return $title->getNamespace() === NS_USER_TALK && !$title->isSubpage();
133 }
134
135 protected function getSubjectMessageKey() {
136 return 'notification-mention-email-subject';
137 }
138 }
139
140 class_alias( EchoMentionPresentationModel::class, 'EchoMentionPresentationModel' );