Debian Codegraph @

extensions/Echo/includes/Formatters/EchoPageLinkedPresentationModel.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications\Formatters;
4
5 use MediaWiki\Extension\Notifications\Controller\NotificationController;
6 use MediaWiki\Extension\Notifications\Model\Event;
7 use MediaWiki\MediaWikiServices;
8 use MediaWiki\SpecialPage\SpecialPage;
9 use MediaWiki\Title\Title;
10
11 class EchoPageLinkedPresentationModel extends EchoEventPresentationModel {
12
13 /** @var Title|null */
14 private $pageFrom;
15
16 public function getIconType() {
17 return 'linked';
18 }
19
20 /**
21 * The page containing the link may be a new page
22 * that is not yet replicated.
23 * This event won't be rendered unless/until
24 * both pages are available.
25 * @return bool
26 */
27 public function canRender() {
28 $pageTo = $this->event->getTitle();
29 $pageFrom = $this->getPageFrom();
30 return (bool)$pageTo && (bool)$pageFrom;
31 }
32
33 public function getPrimaryLink() {
34 if ( $this->isBundled() ) {
35 return false;
36 } else {
37 $params = [];
38 if ( $this->event->getExtraParam( 'revid' ) ) {
39 $params = [
40 'oldid' => 'prev',
41 'diff' => $this->event->getExtraParam( 'revid' )
42 ];
43 }
44
45 return [
46 'url' => $this->getPageFrom()->getFullURL( $params ),
47 'label' => $this->msg( 'notification-link-text-view-page' )->text(),
48 ];
49 }
50 }
51
52 public function getSecondaryLinks() {
53 $whatLinksHereLink = [
54 'url' => SpecialPage::getTitleFor( 'Whatlinkshere', $this->event->getTitle()->getPrefixedText() )
55 ->getFullURL(),
56 'label' => $this->msg( 'notification-link-text-what-links-here' )->text(),
57 'description' => '',
58 'icon' => 'linked',
59 'prioritized' => true
60 ];
61
62 return [ $whatLinksHereLink, $this->getMuteLink() ];
63 }
64
65 protected function getMuteLink() {
66 if ( !MediaWikiServices::getInstance()->getMainConfig()->get( 'EchoPerUserBlacklist' ) ) {
67 return null;
68 }
69 $title = $this->event->getTitle();
70 $isPageMuted = NotificationController::isPageLinkedTitleMutedByUser( $title, $this->getUser() );
71 $action = $isPageMuted ? 'unmute' : 'mute';
72 $prefTitle = SpecialPage::getTitleFor( 'Preferences', false, 'mw-prefsection-echo-mutedpageslist' );
73 $data = [
74 'tokenType' => 'csrf',
75 'params' => [
76 'action' => 'echomute',
77 'type' => 'page-linked-title',
78 ],
79 'messages' => [
80 'confirmation' => [
81 // notification-dynamic-actions-mute-page-linked-confirmation
82 // notification-dynamic-actions-unmute-page-linked-confirmation
83 'title' => $this
84 ->msg( 'notification-dynamic-actions-' . $action . '-page-linked-confirmation' )
85 ->params(
86 $this->getTruncatedTitleText( $title ),
87 $this->getViewingUserForGender()
88 ),
89 // notification-dynamic-actions-mute-page-linked-confirmation-description
90 // notification-dynamic-actions-unmute-page-linked-confirmation-description
91 'description' => $this
92 ->msg( 'notification-dynamic-actions-' . $action . '-page-linked-confirmation-description' )
93 ->params(
94 $prefTitle->getFullURL(),
95 $this->getViewingUserForGender()
96 )
97 ]
98 ]
99 ];
100 $data['params'][$isPageMuted ? 'unmute' : 'mute'] = $title->getPrefixedText();
101
102 return $this->getDynamicActionLink(
103 $prefTitle,
104 $isPageMuted ? 'bell' : 'unbell',
105 // notification-dynamic-actions-mute-page-linked
106 // notification-dynamic-actions-unmute-page-linked
107 $this->msg( 'notification-dynamic-actions-' . $action . '-page-linked' )
108 ->params(
109 $this->getTruncatedTitleText( $title ),
110 $this->getViewingUserForGender()
111 )->text(),
112 null,
113 $data,
114 []
115 );
116 }
117
118 protected function getHeaderMessageKey() {
119 if ( $this->getBundleCount( true, [ $this, 'getLinkedPageId' ] ) > 1 ) {
120 return 'notification-bundle-header-page-linked';
121 }
122 return 'notification-header-page-linked';
123 }
124
125 public function getHeaderMessage() {
126 $msg = parent::getHeaderMessage();
127 $msg->params( $this->getTruncatedTitleText( $this->event->getTitle(), true ) );
128 $msg->params( $this->getTruncatedTitleText( $this->getPageFrom(), true ) );
129 $count =
130 $this->getNotificationCountForOutput( true, [ $this, 'getLinkedPageId' ] );
131 $msg->numParams( $count );
132 return $msg;
133 }
134
135 public function getCompactHeaderMessage() {
136 $msg = $this->msg( parent::getCompactHeaderMessageKey() );
137 $msg->params( $this->getTruncatedTitleText( $this->getPageFrom(), true ) );
138 return $msg;
139 }
140
141 /**
142 * Get the page ID of the linked-from page for a given event.
143 * @param Event $event page-linked event
144 * @return int Page ID, or 0 if the page doesn't exist
145 */
146 public function getLinkedPageId( Event $event ) {
147 $extra = $event->getExtra();
148 if ( isset( $extra['link-from-page-id'] ) ) {
149 return $extra['link-from-page-id'];
150 }
151 // Backwards compatibility for events from before https://gerrit.wikimedia.org/r/#/c/63076
152 if ( isset( $extra['link-from-namespace'] ) && isset( $extra['link-from-title'] ) ) {
153 $title = Title::makeTitleSafe( $extra['link-from-namespace'], $extra['link-from-title'] );
154 if ( $title ) {
155 return $title->getArticleID();
156 }
157 }
158 return 0;
159 }
160
161 private function getPageFrom() {
162 if ( !$this->pageFrom ) {
163 $this->pageFrom = Title::newFromID( $this->getLinkedPageId( $this->event ) );
164 }
165 return $this->pageFrom;
166 }
167
168 protected function getSubjectMessageKey() {
169 return 'notification-page-linked-email-subject';
170 }
171 }