2021-03-05 20:43:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
|
|
|
use IContextSource;
|
2022-10-28 18:24:02 +00:00
|
|
|
use InvalidArgumentException;
|
2021-03-05 20:43:49 +00:00
|
|
|
use MediaWiki\Cache\LinkBatchFactory;
|
|
|
|
use MediaWiki\Linker\LinkRenderer;
|
|
|
|
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 = [
|
2021-10-07 16:34:24 +00:00
|
|
|
// The auto-increment ID will almost always have the same order as sub_created
|
|
|
|
// and the field already has an index.
|
2021-03-05 20:43:49 +00:00
|
|
|
'_topic' => [ 'sub_id' ],
|
2021-10-07 16:34:24 +00:00
|
|
|
'sub_created' => [ 'sub_id' ],
|
2021-03-05 20:43:49 +00:00
|
|
|
// 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' ],
|
|
|
|
];
|
|
|
|
|
2022-10-21 19:34:18 +00:00
|
|
|
private LinkBatchFactory $linkBatchFactory;
|
2021-03-05 20:43:49 +00:00
|
|
|
|
|
|
|
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(),
|
2021-10-07 16:34:24 +00:00
|
|
|
'sub_created' => $this->msg( 'discussiontools-topicsubscription-pager-created' )->text(),
|
|
|
|
'sub_notified' => $this->msg( 'discussiontools-topicsubscription-pager-notified' )->text(),
|
2021-10-12 13:06:57 +00:00
|
|
|
'_unsubscribe' => $this->msg( 'discussiontools-topicsubscription-pager-actions' )->text(),
|
2021-03-05 20:43:49 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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() );
|
|
|
|
|
2021-10-07 16:34:24 +00:00
|
|
|
case 'sub_created':
|
|
|
|
return htmlspecialchars( $this->getLanguage()->userTimeAndDate( $value, $this->getUser() ) );
|
|
|
|
|
|
|
|
case 'sub_notified':
|
|
|
|
return $value ?
|
|
|
|
htmlspecialchars( $this->getLanguage()->userTimeAndDate( $value, $this->getUser() ) ) :
|
|
|
|
$this->msg( 'discussiontools-topicsubscription-pager-notified-never' )->escaped();
|
|
|
|
|
2021-03-05 20:43:49 +00:00
|
|
|
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(),
|
2023-02-04 14:30:14 +00:00
|
|
|
'classes' => [ 'ext-discussiontools-special-unsubscribe-button' ],
|
2021-10-12 13:06:57 +00:00
|
|
|
'framed' => false,
|
2021-03-05 20:43:49 +00:00
|
|
|
'flags' => [ 'destructive' ],
|
2023-02-04 14:30:14 +00:00
|
|
|
'data' => [
|
|
|
|
'item' => $row->sub_item,
|
|
|
|
'title' => $title->getPrefixedText(),
|
|
|
|
],
|
2021-03-05 20:43:49 +00:00
|
|
|
'href' => $title->getLinkURL( [
|
|
|
|
'action' => 'dtunsubscribe',
|
|
|
|
'commentname' => $row->sub_item,
|
|
|
|
] ),
|
2023-02-04 14:30:14 +00:00
|
|
|
'infusable' => true,
|
2021-03-05 20:43:49 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
default:
|
2022-10-28 18:24:02 +00:00
|
|
|
throw new InvalidArgumentException( "Unknown field '$field'" );
|
2021-03-05 20:43:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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',
|
2021-10-07 16:34:24 +00:00
|
|
|
'sub_created',
|
|
|
|
'sub_notified',
|
2021-03-05 20:43:49 +00:00
|
|
|
],
|
|
|
|
'conds' => [
|
|
|
|
'sub_user' => $this->getUser()->getId(),
|
|
|
|
'sub_state != ' . SubscriptionStore::STATE_UNSUBSCRIBED,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getDefaultSort() {
|
2021-10-07 16:34:24 +00:00
|
|
|
return 'sub_created';
|
2021-03-05 20:43:49 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 21:17:26 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getDefaultDirections() {
|
|
|
|
return static::DIR_DESCENDING;
|
|
|
|
}
|
|
|
|
|
2021-03-05 20:43:49 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getIndexField() {
|
2022-06-09 13:51:33 +00:00
|
|
|
return [ static::INDEX_FIELDS[$this->mSort] ];
|
2021-03-05 20:43:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
protected function isFieldSortable( $field ) {
|
2021-10-07 16:34:24 +00:00
|
|
|
// Hide the sort button for "Topic" as it is more accurately shown as "Created"
|
2022-06-09 13:51:33 +00:00
|
|
|
return isset( static::INDEX_FIELDS[$field] ) && $field !== '_topic';
|
2021-03-05 20:43:49 +00:00
|
|
|
}
|
|
|
|
}
|