Debian Codegraph @

extensions/Echo/includes/Special/SpecialNotifications.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications\Special;
4
5 use MediaWiki\Extension\Notifications\DataOutputFormatter;
6 use MediaWiki\Extension\Notifications\NotifUser;
7 use MediaWiki\Extension\Notifications\OOUI\LabelIconWidget;
8 use MediaWiki\Extension\Notifications\SeenTime;
9 use MediaWiki\Html\Html;
10 use MediaWiki\SpecialPage\SpecialPage;
11 use OOUI;
12
13 class SpecialNotifications extends SpecialPage {
14
15 /**
16 * Number of notification records to display per page/load
17 */
18 private const DISPLAY_NUM = 20;
19
20 public function __construct() {
21 parent::__construct( 'Notifications', 'echo-read-notifications' );
22 }
23
24 /**
25 * @param string|null $par
26 */
27 public function execute( $par ) {
28 if ( !$this->getUser()->isRegistered() ) {
29 // Anonymous users cannot have their notification, even if they technically have
30 // the right permission. Redirect them to login instead of erroring on permissions.
31 // NOTE: This has to be before parent::execute(), as the parent is responsible for
32 // permission checks.
33 $this->requireLogin( 'echo-notification-loginrequired' );
34 return;
35 }
36
37 parent::execute( $par );
38
39 $out = $this->getOutput();
40 $out->setPageTitleMsg( $this->msg( 'echo-specialpage' ) );
41
42 $this->addHelpLink( 'Help:Notifications/Special:Notifications' );
43
44 $out->addJsConfigVars( 'wgNotificationsSpecialPageLinks', [
45 'preferences' => SpecialPage::getTitleFor( 'Preferences', false, 'mw-prefsection-echo' )->getLinkURL(),
46 ] );
47
48 $out->addSubtitle( $this->buildSubtitle() );
49
50 $out->enableOOUI();
51
52 $pager = new NotificationPager( $this->getContext() );
53 $pager->setOffset( $this->getRequest()->getVal( 'offset' ) );
54 $pager->setLimit( $this->getRequest()->getInt( 'limit', self::DISPLAY_NUM ) );
55 $notifications = $pager->getNotifications();
56
57 $noJSDiv = new OOUI\Tag();
58 $noJSDiv->addClasses( [ 'mw-echo-special-nojs' ] );
59
60 // If there are no notifications, display a message saying so
61 if ( !$notifications ) {
62 // Wrap this with nojs so it is still hidden if JS is loading
63 $noJSDiv->appendContent(
64 new OOUI\LabelWidget( [ 'label' => $this->msg( 'echo-none' )->text() ] )
65 );
66 $out->addHTML( $noJSDiv );
67 $out->addModules( [ 'ext.echo.special' ] );
68 return;
69 }
70
71 $notif = [];
72 foreach ( $notifications as $notification ) {
73 $output = DataOutputFormatter::formatOutput(
74 $notification, 'special', $this->getUser(), $this->getLanguage()
75 );
76 if ( $output ) {
77 $notif[] = $output;
78 }
79 }
80
81 // Add the notifications to the page (interspersed with date headers)
82 $dateHeader = '';
83 $anyUnread = false;
84 $seenTime = SeenTime::newFromUser( $this->getUser() )->getTime();
85 $notifArray = [];
86 foreach ( $notif as $row ) {
87 if ( !$row['*'] ) {
88 continue;
89 }
90
91 $classes = [ 'mw-echo-notification' ];
92
93 if ( $seenTime !== null && $row['timestamp']['mw'] > $seenTime ) {
94 $classes[] = 'mw-echo-notification-unseen';
95 }
96
97 // Output the date header if it has not been displayed
98 if ( $dateHeader !== $row['timestamp']['date'] ) {
99 $dateHeader = $row['timestamp']['date'];
100 }
101 // Collect unread IDs
102 if ( !isset( $row['read'] ) ) {
103 $classes[] = 'mw-echo-notification-unread';
104 $anyUnread = true;
105 $notifArray[ $dateHeader ][ 'unread' ][] = $row['id'];
106 }
107
108 $li = new OOUI\Tag( 'li' );
109 $li
110 ->addClasses( $classes )
111 ->setAttributes( [
112 'data-notification-category' => $row['category'],
113 'data-notification-event' => $row['id'],
114 'data-notification-type' => $row['type']
115 ] )
116 ->appendContent( new OOUI\HtmlSnippet( $row['*'] ) );
117
118 // Store
119 $notifArray[ $dateHeader ][ 'notices' ][] = $li;
120 }
121
122 $markAllAsReadFormWrapper = '';
123 // Ensure there are some unread notifications
124 if ( $anyUnread ) {
125 $markReadSpecialPage = new SpecialNotificationsMarkRead();
126 $markReadSpecialPage->setContext( $this->getContext() );
127 $notifUser = NotifUser::newFromUser( $this->getUser() );
128 $unreadCount = $notifUser->getAlertCount() + $notifUser->getMessageCount();
129
130 $markAllAsReadText = $this
131 ->msg( 'echo-mark-all-as-read' )
132 ->numParams( $unreadCount )
133 ->text();
134 $markAllAsReadLabelIcon = new LabelIconWidget( [
135 'label' => $markAllAsReadText,
136 'icon' => 'checkAll',
137 ] );
138
139 $markAllAsReadForm = $markReadSpecialPage->getMinimalForm(
140 [ 'ALL' ],
141 $markAllAsReadText,
142 true,
143 $markAllAsReadLabelIcon->toString()
144 );
145
146 // First submission attempt
147 $formHtml = $markAllAsReadForm->prepareForm()->getHTML( false );
148
149 $markAllAsReadFormWrapper = new OOUI\Tag();
150 $markAllAsReadFormWrapper
151 ->addClasses( [ 'mw-echo-special-markAllReadButton' ] )
152 ->appendContent( new OOUI\HtmlSnippet( $formHtml ) );
153 }
154
155 // Build the list
156 $notices = new OOUI\Tag( 'ul' );
157 $notices->addClasses( [ 'mw-echo-special-notifications' ] );
158
159 $markReadSpecialPage = new SpecialNotificationsMarkRead();
160 $markReadSpecialPage->setContext( $this->getContext() );
161 foreach ( $notifArray as $section => $data ) {
162 $heading = ( new OOUI\Tag( 'li' ) )->addClasses( [ 'mw-echo-date-section' ] );
163
164 $dateTitle = new OOUI\LabelWidget( [
165 'classes' => [ 'mw-echo-date-section-text' ],
166 'label' => $section
167 ] );
168
169 $heading->appendContent( $dateTitle );
170
171 // Mark all read button
172 if ( isset( $data[ 'unread' ] ) ) {
173 // tell the UI to show 'unread' notifications only (instead of 'all')
174 $out->addJsConfigVars( 'wgEchoReadState', 'unread' );
175
176 $markReadSectionText = $this->msg( 'echo-specialpage-section-markread' )->text();
177 $markAsReadLabelIcon = new LabelIconWidget( [
178 'label' => $markReadSectionText,
179 'icon' => 'checkAll',
180 ] );
181
182 // There are unread notices. Add the 'mark section as read' button
183 $markSectionAsReadForm = $markReadSpecialPage->getMinimalForm(
184 $data[ 'unread' ],
185 $markReadSectionText,
186 true,
187 $markAsReadLabelIcon->toString()
188 );
189
190 // First submission attempt
191 $formHtml = $markSectionAsReadForm->prepareForm()->getHTML( false );
192
193 $formWrapper = new OOUI\Tag();
194 $formWrapper
195 ->addClasses( [ 'mw-echo-markAsReadSectionButton' ] )
196 ->appendContent( new OOUI\HtmlSnippet( $formHtml ) );
197
198 $heading->appendContent( $formWrapper );
199 }
200
201 // These two must be separate, because $data[ 'notices' ]
202 // is an array
203 $notices
204 ->appendContent( $heading )
205 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset https://github.com/phan/phan/issues/4735
206 ->appendContent( $data[ 'notices' ] );
207 }
208
209 $navBar = $pager->getNavigationBar();
210
211 $navTop = new OOUI\Tag();
212 $navBottom = new OOUI\Tag();
213 $container = new OOUI\Tag();
214
215 $navTop
216 ->addClasses( [ 'mw-echo-special-navbar-top' ] )
217 ->appendContent( new OOUI\HtmlSnippet( $navBar ) );
218 $navBottom
219 ->addClasses( [ 'mw-echo-special-navbar-bottom' ] )
220 ->appendContent( new OOUI\HtmlSnippet( $navBar ) );
221
222 // Put it all together
223 $container
224 ->addClasses( [ 'mw-echo-special-container' ] )
225 ->appendContent(
226 $navTop,
227 $markAllAsReadFormWrapper,
228 $notices,
229 $navBottom
230 );
231
232 // Wrap with nojs div
233 $noJSDiv->appendContent( $container );
234
235 $out->addHTML( $noJSDiv );
236
237 $out->addModules( [ 'ext.echo.special' ] );
238
239 // For no-js support
240 $out->addModuleStyles( [
241 'ext.echo.styles.notifications',
242 'ext.echo.styles.special',
243 'oojs-ui.styles.icons-alerts',
244 'oojs-ui.styles.icons-interactions',
245 ] );
246 }
247
248 /**
249 * Build the subtitle (more info and preference links)
250 * @return string HTML for the subtitle
251 */
252 public function buildSubtitle() {
253 $lang = $this->getLanguage();
254 $subtitleLinks = [];
255 // Preferences link
256 $subtitleLinks[] = Html::element(
257 'a',
258 [
259 'href' => SpecialPage::getTitleFor( 'Preferences', false, 'mw-prefsection-echo' )->getLinkURL(),
260 'id' => 'mw-echo-pref-link',
261 'class' => 'mw-echo-special-header-link',
262 'title' => $this->msg( 'preferences' )->text()
263 ],
264 $this->msg( 'preferences' )->text()
265 );
266 // using pipeList to make it easier to add some links in the future
267 return $lang->pipeList( $subtitleLinks );
268 }
269
270 protected function getGroupName() {
271 return 'login';
272 }
273 }