Add Special:TopicSubscriptions

Bug: T273342
Change-Id: If96a0df1efbf5cadfb6bf2bf8f7ad5c9c90ea142
This commit is contained in:
Bartosz Dziewoński 2021-03-05 21:43:49 +01:00
parent b0ce65736e
commit a0dc12ab56
6 changed files with 247 additions and 0 deletions

14
DiscussionTools.alias.php Normal file
View file

@ -0,0 +1,14 @@
<?php
/**
* Aliases for special pages
*
* @file
* @ingroup Extensions
*/
$specialPageAliases = [];
/** English (English) */
$specialPageAliases['en'] = [
'TopicSubscriptions' => [ 'TopicSubscriptions' ],
];

View file

@ -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",

View file

@ -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": "-",

View file

@ -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}}",

View file

@ -0,0 +1,56 @@
<?php
namespace MediaWiki\Extension\DiscussionTools;
use MediaWiki\Cache\LinkBatchFactory;
use MediaWiki\Linker\LinkRenderer;
use SpecialPage;
class SpecialTopicSubscriptions extends SpecialPage {
/** @var LinkRenderer */
private $linkRenderer;
/** @var LinkBatchFactory */
private $linkBatchFactory;
/**
* @param LinkRenderer $linkRenderer
* @param LinkBatchFactory $linkBatchFactory
*/
public function __construct(
LinkRenderer $linkRenderer,
LinkBatchFactory $linkBatchFactory
) {
parent::__construct( 'TopicSubscriptions' );
$this->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();
}
}

View file

@ -0,0 +1,153 @@
<?php
namespace MediaWiki\Extension\DiscussionTools;
use IContextSource;
use MediaWiki\Cache\LinkBatchFactory;
use MediaWiki\Linker\LinkRenderer;
use MWException;
use OOUI;
use TablePager;
use Title;
class TopicSubscriptionsPager extends TablePager {
/**
* Map of our field names (see ::getFieldNames()) to the column names actually used for
* pagination. This is needed to ensure that the values are unique, and that pagination
* won't get "stuck" when e.g. 50 subscriptions are all created within a second.
*/
private const INDEX_FIELDS = [
'_topic' => [ '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] );
}
}