From a0dc12ab56fb3a59c7111746098a96bdb88e6548 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Dziewo=C5=84ski?= Date: Fri, 5 Mar 2021 21:43:49 +0100 Subject: [PATCH] Add Special:TopicSubscriptions Bug: T273342 Change-Id: If96a0df1efbf5cadfb6bf2bf8f7ad5c9c90ea142 --- DiscussionTools.alias.php | 14 +++ extension.json | 12 ++ i18n/en.json | 6 + i18n/qqq.json | 6 + includes/SpecialTopicSubscriptions.php | 56 +++++++++ includes/TopicSubscriptionsPager.php | 153 +++++++++++++++++++++++++ 6 files changed, 247 insertions(+) create mode 100644 DiscussionTools.alias.php create mode 100644 includes/SpecialTopicSubscriptions.php create mode 100644 includes/TopicSubscriptionsPager.php diff --git a/DiscussionTools.alias.php b/DiscussionTools.alias.php new file mode 100644 index 000000000..585b1911d --- /dev/null +++ b/DiscussionTools.alias.php @@ -0,0 +1,14 @@ + [ 'TopicSubscriptions' ], +]; diff --git a/extension.json b/extension.json index ba69f54ec..08dac7742 100644 --- a/extension.json +++ b/extension.json @@ -19,6 +19,9 @@ "i18n/api" ] }, + "ExtensionMessagesFiles": { + "DiscussionToolsAlias": "DiscussionTools.alias.php" + }, "callback": "\\MediaWiki\\Extension\\DiscussionTools\\Hooks\\RegistrationHooks::onRegistration", "ResourceFileModulePaths": { "localBasePath": "modules", @@ -386,6 +389,15 @@ ] } }, + "SpecialPages": { + "TopicSubscriptions": { + "class": "\\MediaWiki\\Extension\\DiscussionTools\\SpecialTopicSubscriptions", + "services": [ + "LinkRenderer", + "LinkBatchFactory" + ] + } + }, "Hooks": { "BeforeCreateEchoEvent": "\\MediaWiki\\Extension\\DiscussionTools\\Hooks\\EchoHooks::onBeforeCreateEchoEvent", "EchoGetBundleRules": "\\MediaWiki\\Extension\\DiscussionTools\\Hooks\\EchoHooks::onEchoGetBundleRules", diff --git a/i18n/en.json b/i18n/en.json index 7b21a10de..ba4af6a92 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -97,6 +97,12 @@ "discussiontools-topicsubscription-notify-subscribed-title": "You have subscribed!", "discussiontools-topicsubscription-notify-unsubscribed-body": "You will no longer receive notifications about new comments in this topic.", "discussiontools-topicsubscription-notify-unsubscribed-title": "You have unsubscribed.", + "discussiontools-topicsubscription-pager-topic": "Topic", + "discussiontools-topicsubscription-pager-page": "Page", + "discussiontools-topicsubscription-pager-unsubscribe": "Unsubscribe", + "discussiontools-topicsubscription-pager-unsubscribe-button": "Unsubscribe", + "discussiontools-topicsubscription-special-title": "Topic subscriptions", + "discussiontools-topicsubscription-special-intro": "Subscriptions allow you to follow a topic on talk pages. When someone replies to the topic, you will receive a notification.\n\nAdjust how and where you receive these notifications in [[Special:Preferences#mw-prefsection-echo|your preferences]].", "echo-category-title-dt-subscription": "Talk page {{PLURAL:$1|subscription|subscriptions}}", "echo-pref-tooltip-dt-subscription": "Notify me when someone posts a new comment in a topic I am subscribed to.", "tag-discussiontools": "-", diff --git a/i18n/qqq.json b/i18n/qqq.json index 4ebad0609..79df44083 100644 --- a/i18n/qqq.json +++ b/i18n/qqq.json @@ -106,6 +106,12 @@ "discussiontools-topicsubscription-notify-subscribed-title": "Title of notification shown when a user subscribes to a topic.", "discussiontools-topicsubscription-notify-unsubscribed-body": "Body of notification shown when a user unsubscribes from a topic.", "discussiontools-topicsubscription-notify-unsubscribed-title": "Title of notification shown when a user unsubscribes from a topic.", + "discussiontools-topicsubscription-pager-topic": "Used on [[mw:Special:TopicSubscriptions|Special:TopicSubscriptions]] as a table heading.", + "discussiontools-topicsubscription-pager-page": "Used on [[mw:Special:TopicSubscriptions|Special:TopicSubscriptions]] as a table heading.", + "discussiontools-topicsubscription-pager-unsubscribe": "Used on [[mw:Special:TopicSubscriptions|Special:TopicSubscriptions]] as a table heading.", + "discussiontools-topicsubscription-pager-unsubscribe-button": "Used on [[mw:Special:TopicSubscriptions|Special:TopicSubscriptions]] in the table, as a button label.", + "discussiontools-topicsubscription-special-title": "Used on [[mw:Special:TopicSubscriptions|Special:TopicSubscriptions]] as the page title.", + "discussiontools-topicsubscription-special-intro": "Used on [[mw:Special:TopicSubscriptions|Special:TopicSubscriptions]] as an introduction.", "echo-category-title-dt-subscription": "{{doc-echo-category-title|tooltip=Echo-pref-tooltip-dt-subscription}}", "echo-pref-tooltip-dt-subscription": "{{doc-echo-pref-tooltip|title=Echo-category-title-dt-subscription}}", "tag-discussiontools": "{{ignored}}Short description of the discussiontools tag.\n\nShown on lists of changes (history, recentchanges, etc.) for each edit made using DiscussionTools.\n\nSee also:\n* {{msg-mw|Tag-discussiontools-description}}\n{{Related|tag-discussiontools}}", diff --git a/includes/SpecialTopicSubscriptions.php b/includes/SpecialTopicSubscriptions.php new file mode 100644 index 000000000..29150dd69 --- /dev/null +++ b/includes/SpecialTopicSubscriptions.php @@ -0,0 +1,56 @@ +linkRenderer = $linkRenderer; + $this->linkBatchFactory = $linkBatchFactory; + } + + /** + * @inheritDoc + */ + public function execute( $subpage ) { + $this->requireLogin(); + + parent::execute( $subpage ); + + $this->getOutput()->addHtml( $this->msg( 'discussiontools-topicsubscription-special-intro' )->parseAsBlock() ); + + $this->getOutput()->enableOOUI(); + $pager = new TopicSubscriptionsPager( + $this->getContext(), + $this->linkRenderer, + $this->linkBatchFactory + ); + $this->getOutput()->addParserOutputContent( $pager->getFullOutput() ); + } + + /** + * @inheritDoc + */ + public function getDescription() { + return $this->msg( 'discussiontools-topicsubscription-special-title' )->text(); + } + +} diff --git a/includes/TopicSubscriptionsPager.php b/includes/TopicSubscriptionsPager.php new file mode 100644 index 000000000..dbf768e7e --- /dev/null +++ b/includes/TopicSubscriptionsPager.php @@ -0,0 +1,153 @@ + [ 'sub_id' ], + // TODO Add indexes that cover these fields to enable sorting by them + // 'sub_state' => [ 'sub_state', 'sub_item' ], + // 'sub_created' => [ 'sub_created', 'sub_item' ], + // 'sub_notified' => [ 'sub_notified', 'sub_item' ], + ]; + + /** @var LinkBatchFactory */ + private $linkBatchFactory; + + /** + * @param IContextSource $context + * @param LinkRenderer $linkRenderer + * @param LinkBatchFactory $linkBatchFactory + */ + public function __construct( + IContextSource $context, + LinkRenderer $linkRenderer, + LinkBatchFactory $linkBatchFactory + ) { + parent::__construct( $context, $linkRenderer ); + $this->linkBatchFactory = $linkBatchFactory; + } + + /** + * @inheritDoc + */ + public function preprocessResults( $result ) { + $lb = $this->linkBatchFactory->newLinkBatch(); + foreach ( $result as $row ) { + $lb->add( $row->sub_namespace, $row->sub_title ); + } + $lb->execute(); + } + + /** + * @inheritDoc + */ + protected function getFieldNames() { + return [ + '_topic' => $this->msg( 'discussiontools-topicsubscription-pager-topic' )->text(), + '_page' => $this->msg( 'discussiontools-topicsubscription-pager-page' )->text(), + '_unsubscribe' => $this->msg( 'discussiontools-topicsubscription-pager-unsubscribe' )->text(), + ]; + } + + /** + * @inheritDoc + */ + public function formatValue( $field, $value ) { + /** @var stdClass $row */ + $row = $this->mCurrentRow; + $linkRenderer = $this->getLinkRenderer(); + + switch ( $field ) { + case '_topic': + $titleSection = Title::makeTitleSafe( $row->sub_namespace, $row->sub_title, $row->sub_section ); + return $linkRenderer->makeLink( $titleSection, $row->sub_section ); + + case '_page': + $title = Title::makeTitleSafe( $row->sub_namespace, $row->sub_title ); + return $linkRenderer->makeLink( $title, $title->getPrefixedText() ); + + case '_unsubscribe': + $title = Title::makeTitleSafe( $row->sub_namespace, $row->sub_title ); + return (string)new OOUI\ButtonWidget( [ + 'label' => $this->msg( 'discussiontools-topicsubscription-pager-unsubscribe-button' )->text(), + 'flags' => [ 'destructive' ], + 'href' => $title->getLinkURL( [ + 'action' => 'dtunsubscribe', + 'commentname' => $row->sub_item, + ] ), + ] ); + + default: + throw new MWException( "Unknown field '$field'" ); + } + } + + /** + * @inheritDoc + */ + protected function getCellAttrs( $field, $value ) { + $attrs = parent::getCellAttrs( $field, $value ); + if ( $field === '_unsubscribe' ) { + $attrs['style'] = 'text-align: center;'; + } + return $attrs; + } + + /** + * @inheritDoc + */ + public function getQueryInfo() { + return [ + 'tables' => [ + 'discussiontools_subscription', + ], + 'fields' => [ + 'sub_id', + 'sub_item', + 'sub_namespace', + 'sub_title', + 'sub_section', + ], + 'conds' => [ + 'sub_user' => $this->getUser()->getId(), + 'sub_state != ' . SubscriptionStore::STATE_UNSUBSCRIBED, + ], + ]; + } + + /** + * @inheritDoc + */ + public function getDefaultSort() { + return '_topic'; + } + + /** + * @inheritDoc + */ + public function getIndexField() { + return [ self::INDEX_FIELDS[$this->mSort] ]; + } + + /** + * @inheritDoc + */ + protected function isFieldSortable( $field ) { + return isset( self::INDEX_FIELDS[$field] ); + } +}