2021-03-05 20:43:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
2022-10-28 18:24:02 +00:00
|
|
|
use InvalidArgumentException;
|
2021-03-05 20:43:49 +00:00
|
|
|
use MediaWiki\Cache\LinkBatchFactory;
|
2024-06-08 22:02:35 +00:00
|
|
|
use MediaWiki\Context\IContextSource;
|
2024-03-29 00:35:32 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\ThreadItem\DatabaseThreadItem;
|
2023-09-05 23:24:21 +00:00
|
|
|
use MediaWiki\Html\Html;
|
|
|
|
use MediaWiki\Linker\Linker;
|
2021-03-05 20:43:49 +00:00
|
|
|
use MediaWiki\Linker\LinkRenderer;
|
2023-12-11 15:38:02 +00:00
|
|
|
use MediaWiki\Pager\TablePager;
|
2023-08-19 18:16:15 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2021-03-05 20:43:49 +00:00
|
|
|
use OOUI;
|
|
|
|
|
|
|
|
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;
|
2024-03-29 00:35:32 +00:00
|
|
|
private ThreadItemStore $threadItemStore;
|
|
|
|
private ThreadItemFormatter $threadItemFormatter;
|
|
|
|
|
|
|
|
/** @var array<string,DatabaseThreadItem[]> */
|
2024-04-15 19:03:55 +00:00
|
|
|
private array $threadItemsByName = [];
|
2021-03-05 20:43:49 +00:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
IContextSource $context,
|
|
|
|
LinkRenderer $linkRenderer,
|
2024-03-29 00:35:32 +00:00
|
|
|
LinkBatchFactory $linkBatchFactory,
|
|
|
|
ThreadItemStore $threadItemStore,
|
|
|
|
ThreadItemFormatter $threadItemFormatter
|
2021-03-05 20:43:49 +00:00
|
|
|
) {
|
|
|
|
parent::__construct( $context, $linkRenderer );
|
|
|
|
$this->linkBatchFactory = $linkBatchFactory;
|
2024-03-29 00:35:32 +00:00
|
|
|
$this->threadItemStore = $threadItemStore;
|
|
|
|
$this->threadItemFormatter = $threadItemFormatter;
|
2021-03-05 20:43:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function preprocessResults( $result ) {
|
2024-04-15 19:03:55 +00:00
|
|
|
if ( !$result->numRows() ) {
|
|
|
|
return;
|
|
|
|
}
|
2021-03-05 20:43:49 +00:00
|
|
|
$lb = $this->linkBatchFactory->newLinkBatch();
|
2024-03-29 00:35:32 +00:00
|
|
|
$itemNames = [];
|
2021-03-05 20:43:49 +00:00
|
|
|
foreach ( $result as $row ) {
|
|
|
|
$lb->add( $row->sub_namespace, $row->sub_title );
|
2024-03-29 00:35:32 +00:00
|
|
|
$itemNames[] = $row->sub_item;
|
2021-03-05 20:43:49 +00:00
|
|
|
}
|
|
|
|
$lb->execute();
|
2024-03-29 00:35:32 +00:00
|
|
|
|
|
|
|
// Increased limit to allow finding and skipping over some bad permalinks
|
|
|
|
$threadItems = $this->threadItemStore->findNewestRevisionsByName( $itemNames, $this->mLimit * 5 );
|
|
|
|
foreach ( $threadItems as $threadItem ) {
|
|
|
|
$this->threadItemsByName[ $threadItem->getName() ][] = $threadItem;
|
|
|
|
}
|
2021-03-05 20:43:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
|
|
|
|
switch ( $field ) {
|
|
|
|
case '_topic':
|
2024-03-29 00:35:32 +00:00
|
|
|
return $this->formatValueTopic( $row );
|
2021-03-05 20:43:49 +00:00
|
|
|
|
|
|
|
case '_page':
|
2024-03-29 00:35:32 +00:00
|
|
|
return $this->formatValuePage( $row );
|
2021-03-05 20:43:49 +00:00
|
|
|
|
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 );
|
2023-09-05 23:24:21 +00:00
|
|
|
if ( !$title ) {
|
|
|
|
// Handle invalid titles (T345648)
|
|
|
|
// The title isn't checked when unsubscribing, as long as it's a valid title,
|
|
|
|
// so specify something to make it possible to unsubscribe from the buggy entries.
|
|
|
|
$title = Title::newMainPage();
|
|
|
|
}
|
2021-03-05 20:43:49 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-29 00:35:32 +00:00
|
|
|
/**
|
|
|
|
* Format items as a HTML list, unless there's just one item, in which case return it unwrapped.
|
|
|
|
* @param string[] $list HTML
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
|
|
|
private function maybeFormatAsList( array $list ): string {
|
|
|
|
if ( count( $list ) === 1 ) {
|
|
|
|
return $list[0];
|
|
|
|
} else {
|
|
|
|
foreach ( $list as &$item ) {
|
|
|
|
$item = Html::rawElement( 'li', [], $item );
|
|
|
|
}
|
|
|
|
return Html::rawElement( 'ul', [], implode( '', $list ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function formatValuePage( \stdClass $row ): string {
|
|
|
|
$linkRenderer = $this->getLinkRenderer();
|
|
|
|
|
|
|
|
if ( isset( $this->threadItemsByName[ $row->sub_item ] ) ) {
|
|
|
|
$items = [];
|
|
|
|
foreach ( $this->threadItemsByName[ $row->sub_item ] as $threadItem ) {
|
|
|
|
if ( $threadItem->isCanonicalPermalink() ) {
|
|
|
|
$items[] = $this->threadItemFormatter->formatLine( $threadItem, $this );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( $items ) {
|
|
|
|
return $this->maybeFormatAsList( $items );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found items in the permalink database, but they're not good permalinks.
|
|
|
|
// TODO: We could link to the full list on Special:FindComment here
|
|
|
|
// (but we don't link it from the mw.notify message either, at the moment).
|
|
|
|
}
|
|
|
|
|
|
|
|
// Permalink not available - display a plain link to the page title at the time of subscription
|
|
|
|
$title = Title::makeTitleSafe( $row->sub_namespace, $row->sub_title );
|
|
|
|
if ( !$title ) {
|
|
|
|
// Handle invalid titles (T345648)
|
|
|
|
return Html::element( 'span', [ 'class' => 'mw-invalidtitle' ],
|
|
|
|
Linker::getInvalidTitleDescription(
|
|
|
|
$this->getContext(), $row->sub_namespace, $row->sub_title )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return $linkRenderer->makeLink( $title );
|
|
|
|
}
|
|
|
|
|
|
|
|
private function formatValueTopic( \stdClass $row ): string {
|
|
|
|
$linkRenderer = $this->getLinkRenderer();
|
|
|
|
|
|
|
|
$sectionText = $row->sub_section;
|
|
|
|
$sectionLink = $row->sub_section;
|
|
|
|
// Detect truncated section titles: either intentionally truncated by SubscriptionStore,
|
|
|
|
// or incorrect multibyte truncation of old entries (T345648).
|
|
|
|
$last = mb_substr( $sectionText, -1 );
|
|
|
|
if ( $last !== '' && ( $last === "\x1f" || mb_ord( $last ) === false ) ) {
|
|
|
|
$sectionText = substr( $sectionText, 0, -strlen( $last ) ) . $this->msg( 'ellipsis' )->text();
|
|
|
|
$sectionLink = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( str_starts_with( $row->sub_item, 'p-topics-' ) ) {
|
|
|
|
return '<em>' .
|
|
|
|
$this->msg( 'discussiontools-topicsubscription-pager-newtopics-label' )->escaped() .
|
|
|
|
'</em>';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $this->threadItemsByName[ $row->sub_item ] ) ) {
|
|
|
|
$items = [];
|
|
|
|
foreach ( $this->threadItemsByName[ $row->sub_item ] as $threadItem ) {
|
|
|
|
if ( $threadItem->isCanonicalPermalink() ) {
|
|
|
|
// TODO: Can we extract the current topic title out of $threadItem->getId() sometimes,
|
|
|
|
// instead of always using the topic title at the time of subscription? (T295431)
|
|
|
|
$items[] = $this->threadItemFormatter->makeLink( $threadItem, $sectionText );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( $items ) {
|
|
|
|
return $this->maybeFormatAsList( $items );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Found items in the permalink database, but they're not good permalinks.
|
|
|
|
// TODO: We could link to the full list on Special:FindComment here
|
|
|
|
// (but we don't link it from the mw.notify message either, at the moment).
|
|
|
|
}
|
|
|
|
|
|
|
|
// Permalink not available - display a plain link to the section at the time of subscription
|
|
|
|
if ( !$sectionLink ) {
|
|
|
|
// We can't link to the section correctly, since the only link we have is truncated
|
|
|
|
return htmlspecialchars( $sectionText );
|
|
|
|
}
|
|
|
|
$titleSection = Title::makeTitleSafe( $row->sub_namespace, $row->sub_title, $sectionLink );
|
|
|
|
if ( !$titleSection ) {
|
|
|
|
// Handle invalid titles of any other kind, just in case
|
|
|
|
return htmlspecialchars( $sectionText );
|
|
|
|
}
|
|
|
|
return $linkRenderer->makeLink( $titleSection, $sectionText );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|