mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/DiscussionTools
synced 2024-11-28 02:00:57 +00:00
Merge "Fetch user's topic subscriptions on the page in a single query"
This commit is contained in:
commit
9822a60757
|
@ -213,15 +213,25 @@ class CommentFormatter {
|
|||
string $text, Language $lang, SubscriptionStore $subscriptionStore, UserIdentity $user
|
||||
) : string {
|
||||
$doc = new DOMDocument();
|
||||
$text = preg_replace_callback(
|
||||
'/<!--__DTSUBSCRIBE__(.*?)-->/',
|
||||
function ( $matches ) use ( $doc, $subscriptionStore, $user ) {
|
||||
$itemName = $matches[1];
|
||||
|
||||
$matches = [];
|
||||
preg_match_all( '/<!--__DTSUBSCRIBE__(.*?)-->/', $text, $matches );
|
||||
$itemNames = $matches[1];
|
||||
|
||||
$items = $subscriptionStore->getSubscriptionItemsForUser(
|
||||
$user,
|
||||
$itemName
|
||||
$itemNames
|
||||
);
|
||||
$isSubscribed = count( $items ) && !$items[0]->isMuted();
|
||||
$itemsByName = [];
|
||||
foreach ( $items as $item ) {
|
||||
$itemsByName[ $item->getItemName() ] = $item;
|
||||
}
|
||||
|
||||
$text = preg_replace_callback(
|
||||
'/<!--__DTSUBSCRIBE__(.*?)-->/',
|
||||
function ( $matches ) use ( $doc, $itemsByName ) {
|
||||
$itemName = $matches[1];
|
||||
$isSubscribed = isset( $itemsByName[ $itemName ] ) && !$itemsByName[ $itemName ]->isMuted();
|
||||
$subscribe = $doc->createElement( 'span' );
|
||||
$subscribe->setAttribute(
|
||||
'class',
|
||||
|
|
|
@ -58,14 +58,14 @@ class SubscriptionStore {
|
|||
/**
|
||||
* @param IDatabase $db
|
||||
* @param UserIdentity|null $user
|
||||
* @param string|null $itemName
|
||||
* @param array|null $itemNames
|
||||
* @param int|null $state
|
||||
* @return IResultWrapper|false
|
||||
*/
|
||||
private function fetchSubscriptions(
|
||||
IDatabase $db,
|
||||
?UserIdentity $user = null,
|
||||
?string $itemName = null,
|
||||
?array $itemNames = null,
|
||||
?int $state = null
|
||||
) {
|
||||
$conditions = [];
|
||||
|
@ -74,8 +74,8 @@ class SubscriptionStore {
|
|||
$conditions[ 'sub_user' ] = $user->getId();
|
||||
}
|
||||
|
||||
if ( $itemName ) {
|
||||
$conditions[ 'sub_item' ] = $itemName;
|
||||
if ( $itemNames !== null ) {
|
||||
$conditions[ 'sub_item' ] = $itemNames;
|
||||
}
|
||||
|
||||
if ( $state !== null ) {
|
||||
|
@ -95,14 +95,14 @@ class SubscriptionStore {
|
|||
|
||||
/**
|
||||
* @param UserIdentity $user
|
||||
* @param string|null $itemName
|
||||
* @param array|null $itemNames
|
||||
* @param int|null $state
|
||||
* @param array $options
|
||||
* @return SubscriptionItem[]
|
||||
*/
|
||||
public function getSubscriptionItemsForUser(
|
||||
UserIdentity $user,
|
||||
?string $itemName = null,
|
||||
?array $itemNames = null,
|
||||
?int $state = null,
|
||||
array $options = []
|
||||
) : array {
|
||||
|
@ -117,7 +117,7 @@ class SubscriptionStore {
|
|||
$rows = $this->fetchSubscriptions(
|
||||
$db,
|
||||
$user,
|
||||
$itemName,
|
||||
$itemNames,
|
||||
$state
|
||||
);
|
||||
|
||||
|
@ -151,7 +151,7 @@ class SubscriptionStore {
|
|||
$rows = $this->fetchSubscriptions(
|
||||
$db,
|
||||
null,
|
||||
$itemName,
|
||||
[ $itemName ],
|
||||
$state
|
||||
);
|
||||
|
||||
|
|
|
@ -15,14 +15,14 @@ class MockSubscriptionStore extends SubscriptionStore {
|
|||
|
||||
/**
|
||||
* @param UserIdentity $user Unused, required for inheritance
|
||||
* @param string|null $itemName Unused, required for inheritance
|
||||
* @param array|null $itemNames Unused, required for inheritance
|
||||
* @param int|null $state Unused, required for inheritance
|
||||
* @param array $options Unused, required for inheritance
|
||||
* @return array
|
||||
*/
|
||||
public function getSubscriptionItemsForUser(
|
||||
UserIdentity $user,
|
||||
?string $itemName = null,
|
||||
?array $itemNames = null,
|
||||
?int $state = null,
|
||||
array $options = []
|
||||
) : array {
|
||||
|
|
Loading…
Reference in a new issue