Debian Codegraph @

extensions/Echo/includes/Special/SpecialDisplayNotificationsConfiguration.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications\Special;
4
5 use MediaWiki\Extension\Notifications\AttributeManager;
6 use MediaWiki\Extension\Notifications\Hooks as EchoHooks;
7 use MediaWiki\Html\Html;
8 use MediaWiki\HTMLForm\OOUIHTMLForm;
9 use MediaWiki\SpecialPage\UnlistedSpecialPage;
10 use MediaWiki\User\Options\UserOptionsManager;
11 use MediaWiki\User\User;
12
13 class SpecialDisplayNotificationsConfiguration extends UnlistedSpecialPage {
14 /**
15 * AttributeManager to access notification configuration
16 *
17 * @var AttributeManager
18 */
19 protected $attributeManager;
20
21 /**
22 * Category names, mapping internal name to HTML-formatted name
23 *
24 * @var string[]
25 */
26 protected $categoryNames;
27
28 // Should be one mapping text (friendly) name to internal name, but there
29 // is no friendly name
30 /**
31 * Notification type names. Mapping HTML-formatted internal name to internal name
32 *
33 * @var string[]
34 */
35 protected $notificationTypeNames;
36
37 /**
38 * Notify types, mapping internal name to HTML-formatted name
39 *
40 * @var string[]
41 */
42 protected $notifyTypes;
43
44 // Due to how HTMLForm works, it's convenient to have both directions
45 /**
46 * Category names, mapping HTML-formatted name to internal name
47 *
48 * @var string[]
49 */
50 protected $flippedCategoryNames;
51
52 /**
53 * Notify types, mapping HTML-formatted name to internal name
54 *
55 * @var string[]
56 */
57 protected $flippedNotifyTypes;
58
59 /**
60 * @var UserOptionsManager
61 */
62 private $userOptionsManager;
63
64 public function __construct(
65 AttributeManager $attributeManager,
66 UserOptionsManager $userOptionsManager
67 ) {
68 parent::__construct( 'DisplayNotificationsConfiguration' );
69
70 $this->attributeManager = $attributeManager;
71 $this->userOptionsManager = $userOptionsManager;
72 }
73
74 public function execute( $subPage ) {
75 $this->setHeaders();
76 $this->checkPermissions();
77
78 $config = $this->getConfig();
79
80 $internalCategoryNames = $this->attributeManager->getInternalCategoryNames();
81 $this->categoryNames = [];
82
83 foreach ( $internalCategoryNames as $internalCategoryName ) {
84 $formattedFriendlyCategoryName = Html::element(
85 'strong',
86 [],
87 $this->msg( 'echo-category-title-' . $internalCategoryName )->numParams( 1 )->text()
88 );
89
90 $formattedInternalCategoryName = $this->msg( 'parentheses' )->rawParams(
91 Html::element(
92 'em',
93 [],
94 $internalCategoryName
95 )
96 )->parse();
97
98 $this->categoryNames[$internalCategoryName] = $formattedFriendlyCategoryName . ' '
99 . $formattedInternalCategoryName;
100 }
101
102 $this->flippedCategoryNames = array_flip( $this->categoryNames );
103
104 $this->notifyTypes = [];
105 foreach ( $config->get( 'EchoNotifiers' ) as $notifyType => $notifier ) {
106 $this->notifyTypes[$notifyType] = $this->msg( 'echo-pref-' . $notifyType )->escaped();
107 }
108
109 $this->flippedNotifyTypes = array_flip( $this->notifyTypes );
110
111 $notificationTypes = array_keys( $config->get( 'EchoNotifications' ) );
112 $this->notificationTypeNames = array_combine(
113 array_map( 'htmlspecialchars', $notificationTypes ),
114 $notificationTypes
115 );
116
117 $this->getOutput()->setPageTitleMsg( $this->msg( 'echo-displaynotificationsconfiguration' ) );
118 $this->outputHeader( 'echo-displaynotificationsconfiguration-summary' );
119 $this->outputConfiguration();
120 }
121
122 /**
123 * Outputs the Echo configuration
124 */
125 protected function outputConfiguration() {
126 $this->outputNotificationsInCategories();
127 $this->outputNotificationsInSections();
128 $this->outputAvailability();
129 $this->outputMandatory();
130 $this->outputEnabledDefault();
131 }
132
133 /**
134 * Displays a checkbox matrix, using an HTMLForm
135 *
136 * @param string $id Arbitrary ID
137 * @param string $legendMsgKey Message key for an explanatory legend. For example,
138 * "We wrote this feature because in the days of yore, there was but one notification badge"
139 * @param array $rowLabelMapping Associative array mapping label to tag
140 * @param array $columnLabelMapping Associative array mapping label to tag
141 * @param array $value Array consisting of strings in the format '$columnTag-$rowTag'
142 */
143 protected function outputCheckMatrix(
144 $id,
145 $legendMsgKey,
146 array $rowLabelMapping,
147 array $columnLabelMapping,
148 array $value
149 ) {
150 $form = new OOUIHTMLForm(
151 [
152 $id => [
153 'type' => 'checkmatrix',
154 'rows' => $rowLabelMapping,
155 'columns' => $columnLabelMapping,
156 'default' => $value,
157 'disabled' => true,
158 ]
159 ],
160 $this->getContext()
161 );
162
163 $form->setTitle( $this->getPageTitle() )
164 ->prepareForm()
165 ->suppressDefaultSubmit()
166 ->setWrapperLegendMsg( $legendMsgKey )
167 ->displayForm( false );
168 }
169
170 /**
171 * Outputs the notification types in each category
172 */
173 protected function outputNotificationsInCategories() {
174 $notificationsByCategory = $this->attributeManager->getEventsByCategory();
175
176 $out = $this->getOutput();
177 $out->addHTML( Html::element(
178 'h2',
179 [ 'id' => 'mw-echo-displaynotificationsconfiguration-notifications-by-category' ],
180 $this->msg( 'echo-displaynotificationsconfiguration-notifications-by-category-header' )->text()
181 ) );
182
183 $out->addHTML( Html::openElement( 'ul' ) );
184 foreach ( $notificationsByCategory as $categoryName => $notificationTypes ) {
185 $implodedTypes = Html::element(
186 'span',
187 [],
188 implode( $this->msg( 'comma-separator' )->text(), $notificationTypes )
189 );
190
191 $out->addHTML(
192 Html::rawElement(
193 'li',
194 [],
195 $this->categoryNames[$categoryName] . $this->msg( 'colon-separator' )->escaped() . ' '
196 . $implodedTypes
197 )
198 );
199 }
200 $out->addHTML( Html::closeElement( 'ul' ) );
201 }
202
203 /**
204 * Output the notification types in each section (alert/message)
205 */
206 protected function outputNotificationsInSections() {
207 $this->getOutput()->addHTML( Html::element(
208 'h2',
209 [ 'id' => 'mw-echo-displaynotificationsconfiguration-sorting-by-section' ],
210 $this->msg( 'echo-displaynotificationsconfiguration-sorting-by-section-header' )->text()
211 ) );
212
213 $bySectionValue = [];
214
215 $flippedSectionNames = [];
216
217 foreach ( AttributeManager::$sections as $section ) {
218 $types = $this->attributeManager->getEventsForSection( $section );
219 // echo-notification-alert-text-only, echo-notification-notice-text-only
220 $msgSection = $section == 'message' ? 'notice' : $section;
221 $flippedSectionNames[$this->msg( 'echo-notification-' . $msgSection . '-text-only' )->escaped()]
222 = $section;
223 foreach ( $types as $type ) {
224 $bySectionValue[] = "$section-$type";
225 }
226 }
227
228 $this->outputCheckMatrix(
229 'type-by-section',
230 'echo-displaynotificationsconfiguration-sorting-by-section-legend',
231 $this->notificationTypeNames,
232 $flippedSectionNames,
233 $bySectionValue
234 );
235 }
236
237 /**
238 * Output which notify types are available for each category
239 */
240 protected function outputAvailability() {
241 $this->getOutput()->addHTML( Html::element(
242 'h2',
243 [ 'id' => 'mw-echo-displaynotificationsconfiguration-available-notification-methods' ],
244 $this->msg( 'echo-displaynotificationsconfiguration-available-notification-methods-header' )->text()
245 ) );
246
247 $byCategoryValue = [];
248
249 foreach ( $this->notifyTypes as $notifyType => $displayNotifyType ) {
250 foreach ( $this->categoryNames as $category => $displayCategory ) {
251 if ( $this->attributeManager->isNotifyTypeAvailableForCategory( $category, $notifyType ) ) {
252 $byCategoryValue[] = "$notifyType-$category";
253 }
254 }
255 }
256
257 $this->outputCheckMatrix(
258 'availability-by-category',
259 'echo-displaynotificationsconfiguration-available-notification-methods-by-category-legend',
260 $this->flippedCategoryNames,
261 $this->flippedNotifyTypes,
262 $byCategoryValue
263 );
264 }
265
266 /**
267 * View-only overrides of notification preferences for new users
268 *
269 * @todo (Likely) remove the underlying functionality, see
270 * https://phabricator.wikimedia.org/T357219.
271 * @return bool[]
272 */
273 private static function getNewUserPreferenceOverrides(): array {
274 return [
275 'echo-subscriptions-web-reverted' => false,
276 'echo-subscriptions-web-article-linked' => true,
277 'echo-subscriptions-email-mention' => true,
278 'echo-subscriptions-email-article-linked' => true,
279 ];
280 }
281
282 /**
283 * Output which notification categories are turned on by default, for each notify type
284 */
285 protected function outputEnabledDefault() {
286 $this->getOutput()->addHTML( Html::element(
287 'h2',
288 [ 'id' => 'mw-echo-displaynotificationsconfiguration-enabled-default' ],
289 $this->msg( 'echo-displaynotificationsconfiguration-enabled-default-header' )->text()
290 ) );
291
292 // Some of the preferences are mapped to existing ones defined in core MediaWiki
293 $virtualOptions = EchoHooks::getVirtualUserOptions();
294
295 // In reality, anon users are not relevant to Echo, but this lets us easily query default options.
296 $anonUser = new User;
297
298 $byCategoryValueExisting = [];
299 foreach ( $this->notifyTypes as $notifyType => $displayNotifyType ) {
300 foreach ( $this->categoryNames as $category => $displayCategory ) {
301 $prefKey = "echo-subscriptions-$notifyType-$category";
302 if ( isset( $virtualOptions[ $prefKey ] ) ) {
303 $prefKey = $virtualOptions[ $prefKey ];
304 }
305 if ( $this->userOptionsManager->getOption( $anonUser, $prefKey ) ) {
306 $byCategoryValueExisting[] = "$notifyType-$category";
307 }
308 }
309 }
310
311 $this->outputCheckMatrix(
312 'enabled-by-default-generic',
313 'echo-displaynotificationsconfiguration-enabled-default-existing-users-legend',
314 $this->flippedCategoryNames,
315 $this->flippedNotifyTypes,
316 $byCategoryValueExisting
317 );
318
319 $loggedInUser = new User;
320
321 // NOTE: This is not reliable, and will break if other variables than those hardcoded in
322 // the method below are changed for new users, either via a hook or via conditional
323 // defaults. See T357219 for details.
324 $overrides = $this->getNewUserPreferenceOverrides();
325 foreach ( $overrides as $prefKey => $value ) {
326 $this->userOptionsManager->setOption( $loggedInUser, $prefKey, $value );
327 }
328
329 $byCategoryValueNew = [];
330 foreach ( $this->notifyTypes as $notifyType => $displayNotifyType ) {
331 foreach ( $this->categoryNames as $category => $displayCategory ) {
332 $prefKey = "echo-subscriptions-$notifyType-$category";
333 if ( isset( $virtualOptions[ $prefKey ] ) ) {
334 $prefKey = $virtualOptions[ $prefKey ];
335 }
336 if ( $this->userOptionsManager->getOption( $loggedInUser, $prefKey ) ) {
337 $byCategoryValueNew[] = "$notifyType-$category";
338 }
339 }
340 }
341
342 $this->outputCheckMatrix(
343 'enabled-by-default-new',
344 'echo-displaynotificationsconfiguration-enabled-default-new-users-legend',
345 $this->flippedCategoryNames,
346 $this->flippedNotifyTypes,
347 $byCategoryValueNew
348 );
349 }
350
351 /**
352 * Output which notify types are mandatory for each category
353 */
354 protected function outputMandatory() {
355 $byCategoryValue = [];
356
357 $this->getOutput()->addHTML( Html::element(
358 'h2',
359 [ 'id' => 'mw-echo-displaynotificationsconfiguration-mandatory-notification-methods' ],
360 $this->msg( 'echo-displaynotificationsconfiguration-mandatory-notification-methods-header' )->text()
361 ) );
362
363 foreach ( $this->notifyTypes as $notifyType => $displayNotifyType ) {
364 foreach ( $this->categoryNames as $category => $displayCategory ) {
365 if ( !$this->attributeManager->isNotifyTypeDismissableForCategory( $category, $notifyType ) ) {
366 $byCategoryValue[] = "$notifyType-$category";
367 }
368 }
369 }
370
371 $this->outputCheckMatrix(
372 'mandatory',
373 'echo-displaynotificationsconfiguration-mandatory-notification-methods-by-category-legend',
374 $this->flippedCategoryNames,
375 $this->flippedNotifyTypes,
376 $byCategoryValue
377 );
378 }
379 }