| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\DiscussionTools; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Cache\LinkBatchFactory; |
| 7 |
use MediaWiki\Context\IContextSource; |
| 8 |
use MediaWiki\Extension\DiscussionTools\ThreadItem\DatabaseThreadItem; |
| 9 |
use MediaWiki\Html\Html; |
| 10 |
use MediaWiki\Linker\Linker; |
| 11 |
use MediaWiki\Linker\LinkRenderer; |
| 12 |
use MediaWiki\Pager\TablePager; |
| 13 |
use MediaWiki\Title\Title; |
| 14 |
use OOUI; |
| 15 |
|
| 16 |
class TopicSubscriptionsPager extends TablePager { |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
private const INDEX_FIELDS = [ |
| 24 |
|
| 25 |
|
| 26 |
'_topic' => [ 'sub_id' ], |
| 27 |
'sub_created' => [ 'sub_id' ], |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
]; |
| 33 |
|
| 34 |
private LinkBatchFactory $linkBatchFactory; |
| 35 |
private ThreadItemStore $threadItemStore; |
| 36 |
private ThreadItemFormatter $threadItemFormatter; |
| 37 |
|
| 38 |
|
| 39 |
private array $threadItemsByName = []; |
| 40 |
|
| 41 |
public function __construct( |
| 42 |
IContextSource $context, |
| 43 |
LinkRenderer $linkRenderer, |
| 44 |
LinkBatchFactory $linkBatchFactory, |
| 45 |
ThreadItemStore $threadItemStore, |
| 46 |
ThreadItemFormatter $threadItemFormatter |
| 47 |
) { |
| 48 |
parent::__construct( $context, $linkRenderer ); |
| 49 |
$this->linkBatchFactory = $linkBatchFactory; |
| 50 |
$this->threadItemStore = $threadItemStore; |
| 51 |
$this->threadItemFormatter = $threadItemFormatter; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function preprocessResults( $result ) { |
| 58 |
if ( !$result->numRows() ) { |
| 59 |
return; |
| 60 |
} |
| 61 |
$lb = $this->linkBatchFactory->newLinkBatch(); |
| 62 |
$itemNames = []; |
| 63 |
foreach ( $result as $row ) { |
| 64 |
$lb->add( $row->sub_namespace, $row->sub_title ); |
| 65 |
$itemNames[] = $row->sub_item; |
| 66 |
} |
| 67 |
$lb->execute(); |
| 68 |
|
| 69 |
|
| 70 |
$threadItems = $this->threadItemStore->findNewestRevisionsByName( $itemNames, $this->mLimit * 5 ); |
| 71 |
foreach ( $threadItems as $threadItem ) { |
| 72 |
$this->threadItemsByName[ $threadItem->getName() ][] = $threadItem; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
protected function getFieldNames() { |
| 80 |
return [ |
| 81 |
'_topic' => $this->msg( 'discussiontools-topicsubscription-pager-topic' )->text(), |
| 82 |
'_page' => $this->msg( 'discussiontools-topicsubscription-pager-page' )->text(), |
| 83 |
'sub_created' => $this->msg( 'discussiontools-topicsubscription-pager-created' )->text(), |
| 84 |
'sub_notified' => $this->msg( 'discussiontools-topicsubscription-pager-notified' )->text(), |
| 85 |
'_unsubscribe' => $this->msg( 'discussiontools-topicsubscription-pager-actions' )->text(), |
| 86 |
]; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public function formatValue( $field, $value ) { |
| 93 |
|
| 94 |
$row = $this->mCurrentRow; |
| 95 |
|
| 96 |
switch ( $field ) { |
| 97 |
case '_topic': |
| 98 |
return $this->formatValueTopic( $row ); |
| 99 |
|
| 100 |
case '_page': |
| 101 |
return $this->formatValuePage( $row ); |
| 102 |
|
| 103 |
case 'sub_created': |
| 104 |
return htmlspecialchars( $this->getLanguage()->userTimeAndDate( $value, $this->getUser() ) ); |
| 105 |
|
| 106 |
case 'sub_notified': |
| 107 |
return $value ? |
| 108 |
htmlspecialchars( $this->getLanguage()->userTimeAndDate( $value, $this->getUser() ) ) : |
| 109 |
$this->msg( 'discussiontools-topicsubscription-pager-notified-never' )->escaped(); |
| 110 |
|
| 111 |
case '_unsubscribe': |
| 112 |
$title = Title::makeTitleSafe( $row->sub_namespace, $row->sub_title ); |
| 113 |
if ( !$title ) { |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
$title = Title::newMainPage(); |
| 118 |
} |
| 119 |
return (string)new OOUI\ButtonWidget( [ |
| 120 |
'label' => $this->msg( 'discussiontools-topicsubscription-pager-unsubscribe-button' )->text(), |
| 121 |
'classes' => [ 'ext-discussiontools-special-unsubscribe-button' ], |
| 122 |
'framed' => false, |
| 123 |
'flags' => [ 'destructive' ], |
| 124 |
'data' => [ |
| 125 |
'item' => $row->sub_item, |
| 126 |
'title' => $title->getPrefixedText(), |
| 127 |
], |
| 128 |
'href' => $title->getLinkURL( [ |
| 129 |
'action' => 'dtunsubscribe', |
| 130 |
'commentname' => $row->sub_item, |
| 131 |
] ), |
| 132 |
'infusable' => true, |
| 133 |
] ); |
| 134 |
|
| 135 |
default: |
| 136 |
throw new InvalidArgumentException( "Unknown field '$field'" ); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
private function maybeFormatAsList( array $list ): string { |
| 146 |
if ( count( $list ) === 1 ) { |
| 147 |
return $list[0]; |
| 148 |
} else { |
| 149 |
foreach ( $list as &$item ) { |
| 150 |
$item = Html::rawElement( 'li', [], $item ); |
| 151 |
} |
| 152 |
return Html::rawElement( 'ul', [], implode( '', $list ) ); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
private function formatValuePage( \stdClass $row ): string { |
| 157 |
$linkRenderer = $this->getLinkRenderer(); |
| 158 |
|
| 159 |
if ( isset( $this->threadItemsByName[ $row->sub_item ] ) ) { |
| 160 |
$items = []; |
| 161 |
foreach ( $this->threadItemsByName[ $row->sub_item ] as $threadItem ) { |
| 162 |
if ( $threadItem->isCanonicalPermalink() ) { |
| 163 |
$items[] = $this->threadItemFormatter->formatLine( $threadItem, $this ); |
| 164 |
} |
| 165 |
} |
| 166 |
if ( $items ) { |
| 167 |
return $this->maybeFormatAsList( $items ); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
$title = Title::makeTitleSafe( $row->sub_namespace, $row->sub_title ); |
| 177 |
if ( !$title ) { |
| 178 |
|
| 179 |
return Html::element( 'span', [ 'class' => 'mw-invalidtitle' ], |
| 180 |
Linker::getInvalidTitleDescription( |
| 181 |
$this->getContext(), $row->sub_namespace, $row->sub_title ) |
| 182 |
); |
| 183 |
} |
| 184 |
return $linkRenderer->makeLink( $title ); |
| 185 |
} |
| 186 |
|
| 187 |
private function formatValueTopic( \stdClass $row ): string { |
| 188 |
$linkRenderer = $this->getLinkRenderer(); |
| 189 |
|
| 190 |
$sectionText = $row->sub_section; |
| 191 |
$sectionLink = $row->sub_section; |
| 192 |
|
| 193 |
|
| 194 |
$last = mb_substr( $sectionText, -1 ); |
| 195 |
if ( $last !== '' && ( $last === "\x1f" || mb_ord( $last ) === false ) ) { |
| 196 |
$sectionText = substr( $sectionText, 0, -strlen( $last ) ) . $this->msg( 'ellipsis' )->text(); |
| 197 |
$sectionLink = null; |
| 198 |
} |
| 199 |
|
| 200 |
if ( str_starts_with( $row->sub_item, 'p-topics-' ) ) { |
| 201 |
return '<em>' . |
| 202 |
$this->msg( 'discussiontools-topicsubscription-pager-newtopics-label' )->escaped() . |
| 203 |
'</em>'; |
| 204 |
} |
| 205 |
|
| 206 |
if ( isset( $this->threadItemsByName[ $row->sub_item ] ) ) { |
| 207 |
$items = []; |
| 208 |
foreach ( $this->threadItemsByName[ $row->sub_item ] as $threadItem ) { |
| 209 |
if ( $threadItem->isCanonicalPermalink() ) { |
| 210 |
|
| 211 |
|
| 212 |
$items[] = $this->threadItemFormatter->makeLink( $threadItem, $sectionText ); |
| 213 |
} |
| 214 |
} |
| 215 |
if ( $items ) { |
| 216 |
return $this->maybeFormatAsList( $items ); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
if ( !$sectionLink ) { |
| 226 |
|
| 227 |
return htmlspecialchars( $sectionText ); |
| 228 |
} |
| 229 |
$titleSection = Title::makeTitleSafe( $row->sub_namespace, $row->sub_title, $sectionLink ); |
| 230 |
if ( !$titleSection ) { |
| 231 |
|
| 232 |
return htmlspecialchars( $sectionText ); |
| 233 |
} |
| 234 |
return $linkRenderer->makeLink( $titleSection, $sectionText ); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
protected function getCellAttrs( $field, $value ) { |
| 241 |
$attrs = parent::getCellAttrs( $field, $value ); |
| 242 |
if ( $field === '_unsubscribe' ) { |
| 243 |
$attrs['style'] = 'text-align: center;'; |
| 244 |
} |
| 245 |
return $attrs; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public function getQueryInfo() { |
| 252 |
return [ |
| 253 |
'tables' => [ |
| 254 |
'discussiontools_subscription', |
| 255 |
], |
| 256 |
'fields' => [ |
| 257 |
'sub_id', |
| 258 |
'sub_item', |
| 259 |
'sub_namespace', |
| 260 |
'sub_title', |
| 261 |
'sub_section', |
| 262 |
'sub_created', |
| 263 |
'sub_notified', |
| 264 |
], |
| 265 |
'conds' => [ |
| 266 |
'sub_user' => $this->getUser()->getId(), |
| 267 |
$this->getDatabase()->expr( 'sub_state', '!=', SubscriptionStore::STATE_UNSUBSCRIBED ), |
| 268 |
], |
| 269 |
]; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public function getDefaultSort() { |
| 276 |
return 'sub_created'; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
public function getDefaultDirections() { |
| 283 |
return static::DIR_DESCENDING; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
public function getIndexField() { |
| 290 |
return [ static::INDEX_FIELDS[$this->mSort] ]; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
protected function isFieldSortable( $field ) { |
| 297 |
|
| 298 |
return isset( static::INDEX_FIELDS[$field] ) && $field !== '_topic'; |
| 299 |
} |
| 300 |
} |