2021-02-16 07:51:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
|
|
|
use MediaWiki\Linker\LinkTarget;
|
2021-04-27 20:21:50 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2021-02-16 07:51:49 +00:00
|
|
|
use MediaWiki\User\UserFactory;
|
|
|
|
use MediaWiki\User\UserIdentity;
|
|
|
|
use ReadOnlyMode;
|
|
|
|
use stdClass;
|
|
|
|
use TitleValue;
|
2021-05-05 18:16:54 +00:00
|
|
|
use Wikimedia\Rdbms\FakeResultWrapper;
|
2021-02-16 07:51:49 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
|
|
|
use Wikimedia\Rdbms\ILBFactory;
|
|
|
|
use Wikimedia\Rdbms\ILoadBalancer;
|
|
|
|
use Wikimedia\Rdbms\IResultWrapper;
|
|
|
|
|
|
|
|
class SubscriptionStore {
|
2021-04-27 20:21:50 +00:00
|
|
|
/**
|
|
|
|
* Maximum number of subscriptions that we can store for each user.
|
|
|
|
*/
|
|
|
|
private const USER_SUBSCRIPTION_LIMIT = 5000;
|
|
|
|
|
2021-08-16 16:13:12 +00:00
|
|
|
/**
|
|
|
|
* Constants for the values of the sub_state field.
|
|
|
|
*/
|
|
|
|
public const STATE_UNSUBSCRIBED = 0;
|
|
|
|
public const STATE_SUBSCRIBED = 1;
|
|
|
|
|
2021-02-16 07:51:49 +00:00
|
|
|
/** @var ILBFactory */
|
|
|
|
private $lbFactory;
|
|
|
|
|
|
|
|
/** @var ILoadBalancer */
|
|
|
|
private $loadBalancer;
|
|
|
|
|
|
|
|
/** @var ReadOnlyMode */
|
|
|
|
private $readOnlyMode;
|
|
|
|
|
|
|
|
/** @var UserFactory */
|
|
|
|
private $userFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ILBFactory $lbFactory
|
|
|
|
* @param ReadOnlyMode $readOnlyMode
|
|
|
|
* @param UserFactory $userFactory
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
ILBFactory $lbFactory,
|
|
|
|
ReadOnlyMode $readOnlyMode,
|
|
|
|
UserFactory $userFactory
|
|
|
|
) {
|
|
|
|
$this->lbFactory = $lbFactory;
|
|
|
|
$this->loadBalancer = $lbFactory->getMainLB();
|
|
|
|
|
|
|
|
$this->userFactory = $userFactory;
|
|
|
|
$this->readOnlyMode = $readOnlyMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-05-13 00:11:45 +00:00
|
|
|
* @param int $dbIndex DB_PRIMARY or DB_REPLICA
|
2021-02-16 07:51:49 +00:00
|
|
|
*
|
|
|
|
* @return IDatabase
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
private function getConnectionRef( $dbIndex ): IDatabase {
|
2021-02-16 07:51:49 +00:00
|
|
|
return $this->loadBalancer->getConnectionRef( $dbIndex, [ 'watchlist' ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IDatabase $db
|
|
|
|
* @param UserIdentity|null $user
|
2021-04-23 18:54:49 +00:00
|
|
|
* @param array|null $itemNames
|
2021-08-16 16:13:12 +00:00
|
|
|
* @param int|null $state One of SubscriptionStore::STATE_* constants
|
2021-02-16 07:51:49 +00:00
|
|
|
* @return IResultWrapper|false
|
|
|
|
*/
|
|
|
|
private function fetchSubscriptions(
|
|
|
|
IDatabase $db,
|
|
|
|
?UserIdentity $user = null,
|
2021-04-23 18:54:49 +00:00
|
|
|
?array $itemNames = null,
|
2021-02-16 07:51:49 +00:00
|
|
|
?int $state = null
|
|
|
|
) {
|
|
|
|
$conditions = [];
|
|
|
|
|
|
|
|
if ( $user ) {
|
|
|
|
$conditions[ 'sub_user' ] = $user->getId();
|
|
|
|
}
|
|
|
|
|
2021-04-23 18:54:49 +00:00
|
|
|
if ( $itemNames !== null ) {
|
2021-05-05 18:16:54 +00:00
|
|
|
if ( !count( $itemNames ) ) {
|
|
|
|
// We are not allowed to construct a filter with an empty array.
|
|
|
|
// Any empty array should result in no items being returned.
|
|
|
|
return new FakeResultWrapper( [] );
|
|
|
|
}
|
2021-04-23 18:54:49 +00:00
|
|
|
$conditions[ 'sub_item' ] = $itemNames;
|
2021-02-16 07:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $state !== null ) {
|
|
|
|
$conditions[ 'sub_state' ] = $state;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $db->select(
|
|
|
|
'discussiontools_subscription',
|
|
|
|
[
|
|
|
|
'sub_user', 'sub_item', 'sub_namespace', 'sub_title', 'sub_section', 'sub_state',
|
|
|
|
'sub_created', 'sub_notified'
|
|
|
|
],
|
|
|
|
$conditions,
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param UserIdentity $user
|
2021-04-23 18:54:49 +00:00
|
|
|
* @param array|null $itemNames
|
2021-08-16 16:13:12 +00:00
|
|
|
* @param int|null $state One of SubscriptionStore::STATE_* constants
|
2021-02-16 07:51:49 +00:00
|
|
|
* @param array $options
|
|
|
|
* @return SubscriptionItem[]
|
|
|
|
*/
|
|
|
|
public function getSubscriptionItemsForUser(
|
|
|
|
UserIdentity $user,
|
2021-04-23 18:54:49 +00:00
|
|
|
?array $itemNames = null,
|
2021-02-16 07:51:49 +00:00
|
|
|
?int $state = null,
|
|
|
|
array $options = []
|
2021-07-22 07:25:13 +00:00
|
|
|
): array {
|
2021-02-16 07:51:49 +00:00
|
|
|
// Only a registered user can be subscribed
|
|
|
|
if ( !$user->isRegistered() ) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$options += [ 'forWrite' => false ];
|
2021-05-13 00:11:45 +00:00
|
|
|
$db = $this->getConnectionRef( $options['forWrite'] ? DB_PRIMARY : DB_REPLICA );
|
2021-02-16 07:51:49 +00:00
|
|
|
|
|
|
|
$rows = $this->fetchSubscriptions(
|
|
|
|
$db,
|
|
|
|
$user,
|
2021-04-23 18:54:49 +00:00
|
|
|
$itemNames,
|
2021-02-16 07:51:49 +00:00
|
|
|
$state
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( !$rows ) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$items = [];
|
|
|
|
foreach ( $rows as $row ) {
|
|
|
|
$target = new TitleValue( (int)$row->sub_namespace, $row->sub_title, $row->sub_section );
|
|
|
|
$items[] = $this->getSubscriptionItemFromRow( $user, $target, $row );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $items;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $itemName
|
2021-08-16 16:13:12 +00:00
|
|
|
* @param int|null $state One of SubscriptionStore::STATE_* constants
|
2021-02-16 07:51:49 +00:00
|
|
|
* @param array $options
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getSubscriptionItemsForTopic(
|
|
|
|
string $itemName,
|
|
|
|
?int $state = null,
|
|
|
|
array $options = []
|
2021-07-22 07:25:13 +00:00
|
|
|
): array {
|
2021-02-16 07:51:49 +00:00
|
|
|
$options += [ 'forWrite' => false ];
|
2021-05-13 00:11:45 +00:00
|
|
|
$db = $this->getConnectionRef( $options['forWrite'] ? DB_PRIMARY : DB_REPLICA );
|
2021-02-16 07:51:49 +00:00
|
|
|
|
|
|
|
$rows = $this->fetchSubscriptions(
|
|
|
|
$db,
|
|
|
|
null,
|
2021-04-23 18:54:49 +00:00
|
|
|
[ $itemName ],
|
2021-02-16 07:51:49 +00:00
|
|
|
$state
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( !$rows ) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$items = [];
|
|
|
|
foreach ( $rows as $row ) {
|
|
|
|
$target = new TitleValue( (int)$row->sub_namespace, $row->sub_title, $row->sub_section );
|
|
|
|
$user = $this->userFactory->newFromId( $row->sub_user );
|
|
|
|
$items[] = $this->getSubscriptionItemFromRow( $user, $target, $row );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $items;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param UserIdentity $user
|
|
|
|
* @param LinkTarget $target
|
|
|
|
* @param stdClass $row
|
|
|
|
* @return SubscriptionItem
|
|
|
|
*/
|
|
|
|
private function getSubscriptionItemFromRow(
|
|
|
|
UserIdentity $user,
|
|
|
|
LinkTarget $target,
|
|
|
|
stdClass $row
|
2021-07-22 07:25:13 +00:00
|
|
|
): SubscriptionItem {
|
2021-02-16 07:51:49 +00:00
|
|
|
return new SubscriptionItem(
|
|
|
|
$user,
|
|
|
|
$row->sub_item,
|
|
|
|
$target,
|
|
|
|
$row->sub_state,
|
|
|
|
$row->sub_created,
|
|
|
|
$row->sub_notified
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-27 20:21:50 +00:00
|
|
|
/**
|
|
|
|
* @param UserIdentity $user
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-07-22 07:25:13 +00:00
|
|
|
private function userExceedsSubscriptionLimit( UserIdentity $user ): bool {
|
2021-04-27 20:21:50 +00:00
|
|
|
$logger = LoggerFactory::getInstance( 'DiscussionTools' );
|
|
|
|
// This is always queried before updating
|
|
|
|
$db = $this->getConnectionRef( DB_PRIMARY );
|
|
|
|
|
|
|
|
$rowCount = $db->selectRowCount(
|
|
|
|
'discussiontools_subscription',
|
|
|
|
'*',
|
|
|
|
[ 'sub_user' => $user->getId() ],
|
|
|
|
__METHOD__,
|
|
|
|
[ 'LIMIT' => self::USER_SUBSCRIPTION_LIMIT ]
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( $rowCount >= self::USER_SUBSCRIPTION_LIMIT / 2 ) {
|
|
|
|
$logger->warning(
|
|
|
|
"User {user} has {rowCount} subscriptions, approaching the limit",
|
|
|
|
[
|
|
|
|
'user' => $user->getId(),
|
|
|
|
'rowCount' => $rowCount,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rowCount >= self::USER_SUBSCRIPTION_LIMIT;
|
|
|
|
}
|
|
|
|
|
2021-02-16 07:51:49 +00:00
|
|
|
/**
|
|
|
|
* @param UserIdentity $user
|
|
|
|
* @param LinkTarget $target
|
|
|
|
* @param string $itemName
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function addSubscriptionForUser(
|
|
|
|
UserIdentity $user,
|
|
|
|
LinkTarget $target,
|
|
|
|
string $itemName
|
2021-07-22 07:25:13 +00:00
|
|
|
): bool {
|
2021-02-16 07:51:49 +00:00
|
|
|
if ( $this->readOnlyMode->isReadOnly() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Only a registered user can subscribe
|
|
|
|
if ( !$user->isRegistered() ) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-27 20:21:50 +00:00
|
|
|
if ( $this->userExceedsSubscriptionLimit( $user ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-13 00:11:45 +00:00
|
|
|
$dbw = $this->getConnectionRef( DB_PRIMARY );
|
2021-02-16 07:51:49 +00:00
|
|
|
$dbw->upsert(
|
|
|
|
'discussiontools_subscription',
|
|
|
|
[
|
|
|
|
'sub_user' => $user->getId(),
|
|
|
|
'sub_namespace' => $target->getNamespace(),
|
|
|
|
'sub_title' => $target->getDBkey(),
|
|
|
|
'sub_section' => $target->getFragment(),
|
|
|
|
'sub_item' => $itemName,
|
2021-08-16 16:13:12 +00:00
|
|
|
'sub_state' => self::STATE_SUBSCRIBED,
|
2021-02-16 07:51:49 +00:00
|
|
|
'sub_created' => $dbw->timestamp(),
|
|
|
|
],
|
|
|
|
[ [ 'sub_user', 'sub_item' ] ],
|
|
|
|
[
|
2021-08-16 16:13:12 +00:00
|
|
|
'sub_state' => self::STATE_SUBSCRIBED,
|
2021-02-16 07:51:49 +00:00
|
|
|
],
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
return (bool)$dbw->affectedRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param UserIdentity $user
|
|
|
|
* @param string $itemName
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function removeSubscriptionForUser(
|
|
|
|
UserIdentity $user,
|
|
|
|
string $itemName
|
2021-07-22 07:25:13 +00:00
|
|
|
): bool {
|
2021-02-16 07:51:49 +00:00
|
|
|
if ( $this->readOnlyMode->isReadOnly() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Only a registered user can subscribe
|
|
|
|
if ( !$user->isRegistered() ) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-13 00:11:45 +00:00
|
|
|
$dbw = $this->getConnectionRef( DB_PRIMARY );
|
2021-02-16 07:51:49 +00:00
|
|
|
$dbw->update(
|
|
|
|
'discussiontools_subscription',
|
2021-08-16 16:13:12 +00:00
|
|
|
[ 'sub_state' => self::STATE_UNSUBSCRIBED ],
|
2021-02-16 07:51:49 +00:00
|
|
|
[
|
|
|
|
'sub_user' => $user->getId(),
|
|
|
|
'sub_item' => $itemName,
|
|
|
|
],
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
return (bool)$dbw->affectedRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $field Timestamp field name
|
|
|
|
* @param UserIdentity|null $user
|
|
|
|
* @param string $itemName
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function updateSubscriptionTimestamp(
|
|
|
|
string $field,
|
|
|
|
?UserIdentity $user,
|
|
|
|
string $itemName
|
2021-07-22 07:25:13 +00:00
|
|
|
): bool {
|
2021-02-16 07:51:49 +00:00
|
|
|
if ( $this->readOnlyMode->isReadOnly() ) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-13 00:11:45 +00:00
|
|
|
$dbw = $this->getConnectionRef( DB_PRIMARY );
|
2021-02-16 07:51:49 +00:00
|
|
|
|
|
|
|
$conditions = [
|
|
|
|
'sub_item' => $itemName,
|
|
|
|
];
|
|
|
|
|
|
|
|
if ( $user ) {
|
|
|
|
$conditions[ 'sub_user' ] = $user->getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
$dbw->update(
|
|
|
|
'discussiontools_subscription',
|
|
|
|
[ $field => $dbw->timestamp() ],
|
|
|
|
$conditions,
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
return (bool)$dbw->affectedRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the notified timestamp on a subscription
|
|
|
|
*
|
|
|
|
* This field could be used in future to cleanup notifications
|
|
|
|
* that are no longer needed (e.g. because the conversation has
|
|
|
|
* been archived), so should be set for muted notifications too.
|
|
|
|
*
|
|
|
|
* @param UserIdentity|null $user
|
|
|
|
* @param string $itemName
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function updateSubscriptionNotifiedTimestamp(
|
|
|
|
?UserIdentity $user,
|
|
|
|
string $itemName
|
2021-07-22 07:25:13 +00:00
|
|
|
): bool {
|
2021-02-16 07:51:49 +00:00
|
|
|
return $this->updateSubscriptionTimestamp(
|
|
|
|
'sub_notified',
|
|
|
|
$user,
|
|
|
|
$itemName
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|