2018-10-08 17:49:06 +00:00
|
|
|
<?php
|
2016-04-19 03:06:03 +00:00
|
|
|
|
2022-11-02 21:15:29 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications\Special;
|
|
|
|
|
|
|
|
use Html;
|
2022-11-12 07:19:00 +00:00
|
|
|
use MediaWiki\Extension\Notifications\AttributeManager;
|
2022-04-08 00:28:15 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Hooks as EchoHooks;
|
2021-03-30 22:14:16 +00:00
|
|
|
use MediaWiki\User\UserOptionsManager;
|
2022-11-02 21:15:29 +00:00
|
|
|
use OOUIHTMLForm;
|
|
|
|
use UnlistedSpecialPage;
|
|
|
|
use User;
|
2021-03-30 22:14:16 +00:00
|
|
|
|
2016-04-19 03:06:03 +00:00
|
|
|
class SpecialDisplayNotificationsConfiguration extends UnlistedSpecialPage {
|
|
|
|
/**
|
2022-11-12 07:19:00 +00:00
|
|
|
* AttributeManager to access notification configuration
|
2016-04-19 03:06:03 +00:00
|
|
|
*
|
2022-11-12 07:19:00 +00:00
|
|
|
* @var AttributeManager
|
2016-04-19 03:06:03 +00:00
|
|
|
*/
|
|
|
|
protected $attributeManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Category names, mapping internal name to HTML-formatted name
|
|
|
|
*
|
2020-08-10 10:53:14 +00:00
|
|
|
* @var string[]
|
2016-04-19 03:06:03 +00:00
|
|
|
*/
|
|
|
|
protected $categoryNames;
|
|
|
|
|
2016-04-27 21:43:00 +00:00
|
|
|
// Should be one mapping text (friendly) name to internal name, but there
|
|
|
|
// is no friendly name
|
|
|
|
/**
|
2018-09-03 19:10:13 +00:00
|
|
|
* Notification type names. Mapping HTML-formatted internal name to internal name
|
2016-04-27 21:43:00 +00:00
|
|
|
*
|
2020-08-10 10:53:14 +00:00
|
|
|
* @var string[]
|
2016-04-27 21:43:00 +00:00
|
|
|
*/
|
|
|
|
protected $notificationTypeNames;
|
|
|
|
|
2016-04-19 03:06:03 +00:00
|
|
|
/**
|
2018-09-03 19:10:13 +00:00
|
|
|
* Notify types, mapping internal name to HTML-formatted name
|
2016-04-19 03:06:03 +00:00
|
|
|
*
|
2020-08-10 10:53:14 +00:00
|
|
|
* @var string[]
|
2016-04-19 03:06:03 +00:00
|
|
|
*/
|
|
|
|
protected $notifyTypes;
|
|
|
|
|
|
|
|
// Due to how HTMLForm works, it's convenient to have both directions
|
|
|
|
/**
|
|
|
|
* Category names, mapping HTML-formatted name to internal name
|
|
|
|
*
|
2020-08-10 10:53:14 +00:00
|
|
|
* @var string[]
|
2016-04-19 03:06:03 +00:00
|
|
|
*/
|
|
|
|
protected $flippedCategoryNames;
|
|
|
|
|
|
|
|
/**
|
2018-09-03 19:10:13 +00:00
|
|
|
* Notify types, mapping HTML-formatted name to internal name
|
2016-04-19 03:06:03 +00:00
|
|
|
*
|
2020-08-10 10:53:14 +00:00
|
|
|
* @var string[]
|
2016-04-19 03:06:03 +00:00
|
|
|
*/
|
|
|
|
protected $flippedNotifyTypes;
|
|
|
|
|
2021-03-30 22:14:16 +00:00
|
|
|
/**
|
|
|
|
* @var UserOptionsManager
|
|
|
|
*/
|
|
|
|
private $userOptionsManager;
|
|
|
|
|
|
|
|
/**
|
2022-11-12 07:19:00 +00:00
|
|
|
* @param AttributeManager $attributeManager
|
2021-03-30 22:14:16 +00:00
|
|
|
* @param UserOptionsManager $userOptionsManager
|
|
|
|
*/
|
2021-06-29 13:43:25 +00:00
|
|
|
public function __construct(
|
2022-11-12 07:19:00 +00:00
|
|
|
AttributeManager $attributeManager,
|
2021-06-29 13:43:25 +00:00
|
|
|
UserOptionsManager $userOptionsManager
|
|
|
|
) {
|
2016-04-19 03:06:03 +00:00
|
|
|
parent::__construct( 'DisplayNotificationsConfiguration' );
|
|
|
|
|
2021-06-29 13:43:25 +00:00
|
|
|
$this->attributeManager = $attributeManager;
|
2021-03-30 22:14:16 +00:00
|
|
|
$this->userOptionsManager = $userOptionsManager;
|
2016-04-19 03:06:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function execute( $subPage ) {
|
|
|
|
$this->setHeaders();
|
|
|
|
$this->checkPermissions();
|
|
|
|
|
2022-11-12 06:37:37 +00:00
|
|
|
$config = $this->getConfig();
|
|
|
|
|
2016-04-19 03:06:03 +00:00
|
|
|
$internalCategoryNames = $this->attributeManager->getInternalCategoryNames();
|
2016-12-05 18:51:07 +00:00
|
|
|
$this->categoryNames = [];
|
2016-04-19 03:06:03 +00:00
|
|
|
|
|
|
|
foreach ( $internalCategoryNames as $internalCategoryName ) {
|
|
|
|
$formattedFriendlyCategoryName = Html::element(
|
|
|
|
'strong',
|
2016-12-05 18:51:07 +00:00
|
|
|
[],
|
2016-04-19 03:06:03 +00:00
|
|
|
$this->msg( 'echo-category-title-' . $internalCategoryName )->numParams( 1 )->text()
|
|
|
|
);
|
|
|
|
|
|
|
|
$formattedInternalCategoryName = $this->msg( 'parentheses' )->rawParams(
|
|
|
|
Html::element(
|
|
|
|
'em',
|
2016-12-05 18:51:07 +00:00
|
|
|
[],
|
2016-04-19 03:06:03 +00:00
|
|
|
$internalCategoryName
|
|
|
|
)
|
|
|
|
)->parse();
|
|
|
|
|
2020-06-27 10:05:03 +00:00
|
|
|
$this->categoryNames[$internalCategoryName] = $formattedFriendlyCategoryName . ' '
|
|
|
|
. $formattedInternalCategoryName;
|
2016-04-19 03:06:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->flippedCategoryNames = array_flip( $this->categoryNames );
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$this->notifyTypes = [];
|
2022-11-12 06:37:37 +00:00
|
|
|
foreach ( $config->get( 'EchoNotifiers' ) as $notifyType => $notifier ) {
|
2018-09-03 19:10:13 +00:00
|
|
|
$this->notifyTypes[$notifyType] = $this->msg( 'echo-pref-' . $notifyType )->escaped();
|
2016-04-19 03:06:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->flippedNotifyTypes = array_flip( $this->notifyTypes );
|
|
|
|
|
2022-11-12 06:37:37 +00:00
|
|
|
$notificationTypes = array_keys( $config->get( 'EchoNotifications' ) );
|
2018-09-03 19:10:13 +00:00
|
|
|
$this->notificationTypeNames = array_combine(
|
|
|
|
array_map( 'htmlspecialchars', $notificationTypes ),
|
|
|
|
$notificationTypes
|
|
|
|
);
|
2016-04-27 21:43:00 +00:00
|
|
|
|
2016-04-19 03:06:03 +00:00
|
|
|
$this->getOutput()->setPageTitle( $this->msg( 'echo-displaynotificationsconfiguration' )->text() );
|
|
|
|
$this->outputHeader( 'echo-displaynotificationsconfiguration-summary' );
|
|
|
|
$this->outputConfiguration();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Outputs the Echo configuration
|
|
|
|
*/
|
|
|
|
protected function outputConfiguration() {
|
|
|
|
$this->outputNotificationsInCategories();
|
2016-04-27 21:43:00 +00:00
|
|
|
$this->outputNotificationsInSections();
|
2016-04-19 03:06:03 +00:00
|
|
|
$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'
|
|
|
|
*/
|
2020-06-27 10:05:03 +00:00
|
|
|
protected function outputCheckMatrix(
|
|
|
|
$id,
|
|
|
|
$legendMsgKey,
|
|
|
|
array $rowLabelMapping,
|
|
|
|
array $columnLabelMapping,
|
|
|
|
array $value
|
|
|
|
) {
|
2021-07-27 17:30:10 +00:00
|
|
|
$form = new OOUIHTMLForm(
|
2016-12-05 18:51:07 +00:00
|
|
|
[
|
|
|
|
$id => [
|
2016-04-19 03:06:03 +00:00
|
|
|
'type' => 'checkmatrix',
|
|
|
|
'rows' => $rowLabelMapping,
|
|
|
|
'columns' => $columnLabelMapping,
|
|
|
|
'default' => $value,
|
|
|
|
'disabled' => true,
|
2016-12-05 18:51:07 +00:00
|
|
|
]
|
|
|
|
],
|
2018-08-10 22:20:41 +00:00
|
|
|
$this->getContext()
|
2016-04-19 03:06:03 +00:00
|
|
|
);
|
|
|
|
|
2018-02-02 20:20:48 +00:00
|
|
|
$form->setTitle( $this->getPageTitle() )
|
2016-04-19 03:06:03 +00:00
|
|
|
->prepareForm()
|
|
|
|
->suppressDefaultSubmit()
|
|
|
|
->setWrapperLegendMsg( $legendMsgKey )
|
|
|
|
->displayForm( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Outputs the notification types in each category
|
|
|
|
*/
|
|
|
|
protected function outputNotificationsInCategories() {
|
|
|
|
$notificationsByCategory = $this->attributeManager->getEventsByCategory();
|
|
|
|
|
|
|
|
$out = $this->getOutput();
|
2018-08-22 14:13:10 +00:00
|
|
|
$out->addHTML( Html::element(
|
|
|
|
'h2',
|
|
|
|
[ 'id' => 'mw-echo-displaynotificationsconfiguration-notifications-by-category' ],
|
|
|
|
$this->msg( 'echo-displaynotificationsconfiguration-notifications-by-category-header' )->text()
|
|
|
|
) );
|
2016-04-19 03:06:03 +00:00
|
|
|
|
|
|
|
$out->addHTML( Html::openElement( 'ul' ) );
|
|
|
|
foreach ( $notificationsByCategory as $categoryName => $notificationTypes ) {
|
|
|
|
$implodedTypes = Html::element(
|
|
|
|
'span',
|
2016-12-05 18:51:07 +00:00
|
|
|
[],
|
2016-04-19 03:06:03 +00:00
|
|
|
implode( $this->msg( 'comma-separator' )->text(), $notificationTypes )
|
|
|
|
);
|
|
|
|
|
|
|
|
$out->addHTML(
|
|
|
|
Html::rawElement(
|
|
|
|
'li',
|
2016-12-05 18:51:07 +00:00
|
|
|
[],
|
2020-06-27 10:05:03 +00:00
|
|
|
$this->categoryNames[$categoryName] . $this->msg( 'colon-separator' )->escaped() . ' '
|
|
|
|
. $implodedTypes
|
2016-04-19 03:06:03 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$out->addHTML( Html::closeElement( 'ul' ) );
|
|
|
|
}
|
|
|
|
|
2016-04-27 21:43:00 +00:00
|
|
|
/**
|
|
|
|
* Output the notification types in each section (alert/message)
|
|
|
|
*/
|
|
|
|
protected function outputNotificationsInSections() {
|
2018-08-22 14:13:10 +00:00
|
|
|
$this->getOutput()->addHTML( Html::element(
|
|
|
|
'h2',
|
|
|
|
[ 'id' => 'mw-echo-displaynotificationsconfiguration-sorting-by-section' ],
|
|
|
|
$this->msg( 'echo-displaynotificationsconfiguration-sorting-by-section-header' )->text()
|
|
|
|
) );
|
2016-04-27 21:43:00 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$bySectionValue = [];
|
2016-04-27 21:43:00 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$flippedSectionNames = [];
|
2016-04-27 21:43:00 +00:00
|
|
|
|
2022-11-12 07:19:00 +00:00
|
|
|
foreach ( AttributeManager::$sections as $section ) {
|
2016-04-27 21:43:00 +00:00
|
|
|
$types = $this->attributeManager->getEventsForSection( $section );
|
2016-07-07 22:05:56 +00:00
|
|
|
// echo-notification-alert-text-only, echo-notification-notice-text-only
|
|
|
|
$msgSection = $section == 'message' ? 'notice' : $section;
|
2020-06-27 10:05:03 +00:00
|
|
|
$flippedSectionNames[$this->msg( 'echo-notification-' . $msgSection . '-text-only' )->escaped()]
|
|
|
|
= $section;
|
2016-04-27 21:43:00 +00:00
|
|
|
foreach ( $types as $type ) {
|
|
|
|
$bySectionValue[] = "$section-$type";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 14:13:10 +00:00
|
|
|
$this->outputCheckMatrix(
|
|
|
|
'type-by-section',
|
|
|
|
'echo-displaynotificationsconfiguration-sorting-by-section-legend',
|
|
|
|
$this->notificationTypeNames,
|
|
|
|
$flippedSectionNames,
|
|
|
|
$bySectionValue
|
|
|
|
);
|
2016-04-27 21:43:00 +00:00
|
|
|
}
|
|
|
|
|
2016-04-19 03:06:03 +00:00
|
|
|
/**
|
|
|
|
* Output which notify types are available for each category
|
|
|
|
*/
|
|
|
|
protected function outputAvailability() {
|
2018-08-22 14:13:10 +00:00
|
|
|
$this->getOutput()->addHTML( Html::element(
|
|
|
|
'h2',
|
|
|
|
[ 'id' => 'mw-echo-displaynotificationsconfiguration-available-notification-methods' ],
|
|
|
|
$this->msg( 'echo-displaynotificationsconfiguration-available-notification-methods-header' )->text()
|
|
|
|
) );
|
2016-04-19 03:06:03 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$byCategoryValue = [];
|
2016-04-19 03:06:03 +00:00
|
|
|
|
|
|
|
foreach ( $this->notifyTypes as $notifyType => $displayNotifyType ) {
|
|
|
|
foreach ( $this->categoryNames as $category => $displayCategory ) {
|
|
|
|
if ( $this->attributeManager->isNotifyTypeAvailableForCategory( $category, $notifyType ) ) {
|
|
|
|
$byCategoryValue[] = "$notifyType-$category";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 14:13:10 +00:00
|
|
|
$this->outputCheckMatrix(
|
|
|
|
'availability-by-category',
|
|
|
|
'echo-displaynotificationsconfiguration-available-notification-methods-by-category-legend',
|
|
|
|
$this->flippedCategoryNames,
|
|
|
|
$this->flippedNotifyTypes,
|
|
|
|
$byCategoryValue
|
|
|
|
);
|
2016-04-19 03:06:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output which notification categories are turned on by default, for each notify type
|
|
|
|
*/
|
|
|
|
protected function outputEnabledDefault() {
|
2018-08-22 14:13:10 +00:00
|
|
|
$this->getOutput()->addHTML( Html::element(
|
|
|
|
'h2',
|
|
|
|
[ 'id' => 'mw-echo-displaynotificationsconfiguration-enabled-default' ],
|
|
|
|
$this->msg( 'echo-displaynotificationsconfiguration-enabled-default-header' )->text()
|
|
|
|
) );
|
2016-04-19 03:06:03 +00:00
|
|
|
|
2021-07-27 17:53:58 +00:00
|
|
|
// Some of the preferences are mapped to existing ones defined in core MediaWiki
|
|
|
|
$virtualOptions = EchoHooks::getVirtualUserOptions();
|
|
|
|
|
2016-04-19 03:06:03 +00:00
|
|
|
// In reality, anon users are not relevant to Echo, but this lets us easily query default options.
|
|
|
|
$anonUser = new User;
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$byCategoryValueExisting = [];
|
2016-04-19 03:06:03 +00:00
|
|
|
foreach ( $this->notifyTypes as $notifyType => $displayNotifyType ) {
|
|
|
|
foreach ( $this->categoryNames as $category => $displayCategory ) {
|
2021-07-27 17:53:58 +00:00
|
|
|
$prefKey = "echo-subscriptions-$notifyType-$category";
|
|
|
|
if ( isset( $virtualOptions[ $prefKey ] ) ) {
|
|
|
|
$prefKey = $virtualOptions[ $prefKey ];
|
|
|
|
}
|
2021-12-01 18:29:00 +00:00
|
|
|
if ( $this->userOptionsManager->getOption( $anonUser, $prefKey ) ) {
|
2016-04-19 03:06:03 +00:00
|
|
|
$byCategoryValueExisting[] = "$notifyType-$category";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 14:13:10 +00:00
|
|
|
$this->outputCheckMatrix(
|
|
|
|
'enabled-by-default-generic',
|
|
|
|
'echo-displaynotificationsconfiguration-enabled-default-existing-users-legend',
|
|
|
|
$this->flippedCategoryNames,
|
|
|
|
$this->flippedNotifyTypes,
|
|
|
|
$byCategoryValueExisting
|
|
|
|
);
|
2016-04-19 03:06:03 +00:00
|
|
|
|
|
|
|
$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 ) {
|
2021-03-30 22:14:16 +00:00
|
|
|
$this->userOptionsManager->setOption( $loggedInUser, $prefKey, $value );
|
2016-04-19 03:06:03 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$byCategoryValueNew = [];
|
2016-04-19 03:06:03 +00:00
|
|
|
foreach ( $this->notifyTypes as $notifyType => $displayNotifyType ) {
|
|
|
|
foreach ( $this->categoryNames as $category => $displayCategory ) {
|
2021-07-27 17:53:58 +00:00
|
|
|
$prefKey = "echo-subscriptions-$notifyType-$category";
|
|
|
|
if ( isset( $virtualOptions[ $prefKey ] ) ) {
|
|
|
|
$prefKey = $virtualOptions[ $prefKey ];
|
|
|
|
}
|
2021-12-01 18:29:00 +00:00
|
|
|
if ( $this->userOptionsManager->getOption( $loggedInUser, $prefKey ) ) {
|
2016-04-19 03:06:03 +00:00
|
|
|
$byCategoryValueNew[] = "$notifyType-$category";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 14:13:10 +00:00
|
|
|
$this->outputCheckMatrix(
|
|
|
|
'enabled-by-default-new',
|
|
|
|
'echo-displaynotificationsconfiguration-enabled-default-new-users-legend',
|
|
|
|
$this->flippedCategoryNames,
|
|
|
|
$this->flippedNotifyTypes,
|
|
|
|
$byCategoryValueNew
|
|
|
|
);
|
2016-04-19 03:06:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output which notify types are mandatory for each category
|
|
|
|
*/
|
|
|
|
protected function outputMandatory() {
|
2016-12-05 18:51:07 +00:00
|
|
|
$byCategoryValue = [];
|
2016-04-19 03:06:03 +00:00
|
|
|
|
2018-08-22 14:13:10 +00:00
|
|
|
$this->getOutput()->addHTML( Html::element(
|
|
|
|
'h2',
|
|
|
|
[ 'id' => 'mw-echo-displaynotificationsconfiguration-mandatory-notification-methods' ],
|
|
|
|
$this->msg( 'echo-displaynotificationsconfiguration-mandatory-notification-methods-header' )->text()
|
|
|
|
) );
|
2016-04-19 03:06:03 +00:00
|
|
|
|
|
|
|
foreach ( $this->notifyTypes as $notifyType => $displayNotifyType ) {
|
|
|
|
foreach ( $this->categoryNames as $category => $displayCategory ) {
|
|
|
|
if ( !$this->attributeManager->isNotifyTypeDismissableForCategory( $category, $notifyType ) ) {
|
|
|
|
$byCategoryValue[] = "$notifyType-$category";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 14:13:10 +00:00
|
|
|
$this->outputCheckMatrix(
|
|
|
|
'mandatory',
|
|
|
|
'echo-displaynotificationsconfiguration-mandatory-notification-methods-by-category-legend',
|
|
|
|
$this->flippedCategoryNames,
|
|
|
|
$this->flippedNotifyTypes,
|
|
|
|
$byCategoryValue
|
|
|
|
);
|
2016-04-19 03:06:03 +00:00
|
|
|
}
|
|
|
|
}
|