summaryrefslogtreecommitdiff
blob: c644c043cbeb4e1de886be95419bb803428d38a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php

class SpecialDisplayNotificationsConfiguration extends UnlistedSpecialPage {
	/**
	 * EchoAttributeManager to access notification configuration
	 *
	 * @var EchoAttributeManager $attributeManager;
	 */
	protected $attributeManager;

	/**
	 * Notification controller
	 *
	 * @var EchoNotificationController $notificationController;
	 */
	protected $notificationController;

	/**
	 * Category names, mapping internal name to HTML-formatted name
	 *
	 * @var string[] $categoryNames
	 */
	protected $categoryNames;

	// Should be one mapping text (friendly) name to internal name, but there
	// is no friendly name
	/**
	 * Notification type names.  Mapping HTML-formatted internal name to internal name
	 *
	 * @var string[] $notificationTypeNames
	 */
	protected $notificationTypeNames;

	/**
	 * Notify types, mapping internal name to HTML-formatted name
	 *
	 * @var string[] $notifyTypes
	 */
	protected $notifyTypes;

	// Due to how HTMLForm works, it's convenient to have both directions
	/**
	 * Category names, mapping HTML-formatted name to internal name
	 *
	 * @var string[] $flippedCategoryNames
	 */
	protected $flippedCategoryNames;

	/**
	 * Notify types, mapping HTML-formatted name to internal name
	 *
	 * @var string[] $flippedNotifyTypes
	 */
	protected $flippedNotifyTypes;

	public function __construct() {
		parent::__construct( 'DisplayNotificationsConfiguration' );

		$this->attributeManager = EchoAttributeManager::newFromGlobalVars();
		$this->notificationController = new EchoNotificationController();
	}

	public function execute( $subPage ) {
		global $wgEchoNotifiers, $wgEchoNotifications;

		$this->setHeaders();
		$this->checkPermissions();

		$internalCategoryNames = $this->attributeManager->getInternalCategoryNames();
		$this->categoryNames = [];

		foreach ( $internalCategoryNames as $internalCategoryName ) {
			$formattedFriendlyCategoryName = Html::element(
				'strong',
				[],
				$this->msg( 'echo-category-title-' . $internalCategoryName )->numParams( 1 )->text()
			);

			$formattedInternalCategoryName = $this->msg( 'parentheses' )->rawParams(
				Html::element(
					'em',
					[],
					$internalCategoryName
				)
			)->parse();

			$this->categoryNames[$internalCategoryName] = $formattedFriendlyCategoryName . ' '
				. $formattedInternalCategoryName;
		}

		$this->flippedCategoryNames = array_flip( $this->categoryNames );

		$this->notifyTypes = [];
		foreach ( $wgEchoNotifiers as $notifyType => $notifier ) {
			$this->notifyTypes[$notifyType] = $this->msg( 'echo-pref-' . $notifyType )->escaped();
		}

		$this->flippedNotifyTypes = array_flip( $this->notifyTypes );

		$notificationTypes = array_keys( $wgEchoNotifications );
		$this->notificationTypeNames = array_combine(
			array_map( 'htmlspecialchars', $notificationTypes ),
			$notificationTypes
		);

		$this->getOutput()->setPageTitle( $this->msg( 'echo-displaynotificationsconfiguration' )->text() );
		$this->outputHeader( 'echo-displaynotificationsconfiguration-summary' );
		$this->outputConfiguration();
	}

	/**
	 * Outputs the Echo configuration
	 */
	protected function outputConfiguration() {
		$this->outputNotificationsInCategories();
		$this->outputNotificationsInSections();
		$this->outputAvailability();
		$this->outputMandatory();
		$this->outputEnabledDefault();
	}

	/**
	 * Displays a checkbox matrix, using an HTMLForm
	 *
	 * @param string $id Arbitrary ID
	 * @param string $legendMsgKey Message key for an explanatory legend.  For example,
	 *   "We wrote this feature because in the days of yore, there was but one notification badge"
	 * @param array $rowLabelMapping Associative array mapping label to tag
	 * @param array $columnLabelMapping Associative array mapping label to tag
	 * @param array $value Array consisting of strings in the format '$columnTag-$rowTag'
	 */
	protected function outputCheckMatrix(
		$id,
		$legendMsgKey,
		array $rowLabelMapping,
		array $columnLabelMapping,
		array $value
	) {
		$form = new HTMLForm(
			[
				$id => [
					'type' => 'checkmatrix',
					'rows' => $rowLabelMapping,
					'columns' => $columnLabelMapping,
					'default' => $value,
					'disabled' => true,
				]
			],
			$this->getContext()
		);

		$form->setTitle( $this->getPageTitle() )
			->prepareForm()
			->suppressDefaultSubmit()
			->setWrapperLegendMsg( $legendMsgKey )
			->displayForm( false );
	}

	/**
	 * Outputs the notification types in each category
	 */
	protected function outputNotificationsInCategories() {
		$notificationsByCategory = $this->attributeManager->getEventsByCategory();

		$out = $this->getOutput();
		$out->addHTML( Html::element(
			'h2',
			[ 'id' => 'mw-echo-displaynotificationsconfiguration-notifications-by-category' ],
			$this->msg( 'echo-displaynotificationsconfiguration-notifications-by-category-header' )->text()
		) );

		$out->addHTML( Html::openElement( 'ul' ) );
		foreach ( $notificationsByCategory as $categoryName => $notificationTypes ) {
			$implodedTypes = Html::element(
				'span',
				[],
				implode( $this->msg( 'comma-separator' )->text(), $notificationTypes )
			);

			$out->addHTML(
				Html::rawElement(
					'li',
					[],
					$this->categoryNames[$categoryName] . $this->msg( 'colon-separator' )->escaped() . ' '
						. $implodedTypes
				)
			);
		}
		$out->addHTML( Html::closeElement( 'ul' ) );
	}

	/**
	 * Output the notification types in each section (alert/message)
	 */
	protected function outputNotificationsInSections() {
		$this->getOutput()->addHTML( Html::element(
			'h2',
			[ 'id' => 'mw-echo-displaynotificationsconfiguration-sorting-by-section' ],
			$this->msg( 'echo-displaynotificationsconfiguration-sorting-by-section-header' )->text()
		) );

		$bySectionValue = [];

		$flippedSectionNames = [];

		foreach ( EchoAttributeManager::$sections as $section ) {
			$types = $this->attributeManager->getEventsForSection( $section );
			// echo-notification-alert-text-only, echo-notification-notice-text-only
			$msgSection = $section == 'message' ? 'notice' : $section;
			$flippedSectionNames[$this->msg( 'echo-notification-' . $msgSection . '-text-only' )->escaped()]
				= $section;
			foreach ( $types as $type ) {
				$bySectionValue[] = "$section-$type";
			}
		}

		$this->outputCheckMatrix(
			'type-by-section',
			'echo-displaynotificationsconfiguration-sorting-by-section-legend',
			$this->notificationTypeNames,
			$flippedSectionNames,
			$bySectionValue
		);
	}

	/**
	 * Output which notify types are available for each category
	 */
	protected function outputAvailability() {
		$this->getOutput()->addHTML( Html::element(
			'h2',
			[ 'id' => 'mw-echo-displaynotificationsconfiguration-available-notification-methods' ],
			$this->msg( 'echo-displaynotificationsconfiguration-available-notification-methods-header' )->text()
		) );

		$byCategoryValue = [];

		foreach ( $this->notifyTypes as $notifyType => $displayNotifyType ) {
			foreach ( $this->categoryNames as $category => $displayCategory ) {
				if ( $this->attributeManager->isNotifyTypeAvailableForCategory( $category, $notifyType ) ) {
					$byCategoryValue[] = "$notifyType-$category";
				}
			}
		}

		$this->outputCheckMatrix(
			'availability-by-category',
			'echo-displaynotificationsconfiguration-available-notification-methods-by-category-legend',
			$this->flippedCategoryNames,
			$this->flippedNotifyTypes,
			$byCategoryValue
		);
	}

	/**
	 * Output which notification categories are turned on by default, for each notify type
	 */
	protected function outputEnabledDefault() {
		$this->getOutput()->addHTML( Html::element(
			'h2',
			[ 'id' => 'mw-echo-displaynotificationsconfiguration-enabled-default' ],
			$this->msg( 'echo-displaynotificationsconfiguration-enabled-default-header' )->text()
		) );

		// In reality, anon users are not relevant to Echo, but this lets us easily query default options.
		$anonUser = new User;

		$byCategoryValueExisting = [];
		foreach ( $this->notifyTypes as $notifyType => $displayNotifyType ) {
			foreach ( $this->categoryNames as $category => $displayCategory ) {
				$tag = "$notifyType-$category";
				if ( $anonUser->getOption( "echo-subscriptions-$tag" ) ) {
					$byCategoryValueExisting[] = "$notifyType-$category";
				}
			}
		}

		$this->outputCheckMatrix(
			'enabled-by-default-generic',
			'echo-displaynotificationsconfiguration-enabled-default-existing-users-legend',
			$this->flippedCategoryNames,
			$this->flippedNotifyTypes,
			$byCategoryValueExisting
		);

		$loggedInUser = new User;
		// This might not catch if there are other hooks that do similar.
		// We can't run the actual hook, to avoid side effects.
		$overrides = EchoHooks::getNewUserPreferenceOverrides();
		foreach ( $overrides as $prefKey => $value ) {
			$loggedInUser->setOption( $prefKey, $value );
		}

		$byCategoryValueNew = [];
		foreach ( $this->notifyTypes as $notifyType => $displayNotifyType ) {
			foreach ( $this->categoryNames as $category => $displayCategory ) {
				$tag = "$notifyType-$category";
				if ( $loggedInUser->getOption( "echo-subscriptions-$tag" ) ) {
					$byCategoryValueNew[] = "$notifyType-$category";
				}
			}
		}

		$this->outputCheckMatrix(
			'enabled-by-default-new',
			'echo-displaynotificationsconfiguration-enabled-default-new-users-legend',
			$this->flippedCategoryNames,
			$this->flippedNotifyTypes,
			$byCategoryValueNew
		);
	}

	/**
	 * Output which notify types are mandatory for each category
	 */
	protected function outputMandatory() {
		$byCategoryValue = [];

		$this->getOutput()->addHTML( Html::element(
			'h2',
			[ 'id' => 'mw-echo-displaynotificationsconfiguration-mandatory-notification-methods' ],
			$this->msg( 'echo-displaynotificationsconfiguration-mandatory-notification-methods-header' )->text()
		) );

		foreach ( $this->notifyTypes as $notifyType => $displayNotifyType ) {
			foreach ( $this->categoryNames as $category => $displayCategory ) {
				if ( !$this->attributeManager->isNotifyTypeDismissableForCategory( $category, $notifyType ) ) {
					$byCategoryValue[] = "$notifyType-$category";
				}
			}
		}

		$this->outputCheckMatrix(
			'mandatory',
			'echo-displaynotificationsconfiguration-mandatory-notification-methods-by-category-legend',
			$this->flippedCategoryNames,
			$this->flippedNotifyTypes,
			$byCategoryValue
		);
	}
}