Merge "Fetch user's topic subscriptions on the page in a single query"

This commit is contained in:
jenkins-bot 2021-05-03 14:06:41 +00:00 committed by Gerrit Code Review
commit 9822a60757
3 changed files with 26 additions and 16 deletions

View file

@ -213,15 +213,25 @@ class CommentFormatter {
string $text, Language $lang, SubscriptionStore $subscriptionStore, UserIdentity $user
) : string {
$doc = new DOMDocument();
$matches = [];
preg_match_all( '/<!--__DTSUBSCRIBE__(.*?)-->/', $text, $matches );
$itemNames = $matches[1];
$items = $subscriptionStore->getSubscriptionItemsForUser(
$user,
$itemNames
);
$itemsByName = [];
foreach ( $items as $item ) {
$itemsByName[ $item->getItemName() ] = $item;
}
$text = preg_replace_callback(
'/<!--__DTSUBSCRIBE__(.*?)-->/',
function ( $matches ) use ( $doc, $subscriptionStore, $user ) {
function ( $matches ) use ( $doc, $itemsByName ) {
$itemName = $matches[1];
$items = $subscriptionStore->getSubscriptionItemsForUser(
$user,
$itemName
);
$isSubscribed = count( $items ) && !$items[0]->isMuted();
$isSubscribed = isset( $itemsByName[ $itemName ] ) && !$itemsByName[ $itemName ]->isMuted();
$subscribe = $doc->createElement( 'span' );
$subscribe->setAttribute(
'class',

View file

@ -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
);

View file

@ -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 {