2020-05-23 08:43:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @author DannyS712
|
|
|
|
|
*/
|
|
|
|
|
|
2021-03-26 22:24:43 +00:00
|
|
|
namespace MediaWiki\Watchlist;
|
2020-05-23 08:43:34 +00:00
|
|
|
|
|
|
|
|
use MediaWiki\HookContainer\HookContainer;
|
|
|
|
|
use MediaWiki\HookContainer\HookRunner;
|
|
|
|
|
use MediaWiki\Linker\LinkTarget;
|
2021-03-26 22:24:43 +00:00
|
|
|
use MediaWiki\Page\PageIdentity;
|
2021-04-08 19:34:40 +00:00
|
|
|
use MediaWiki\Page\PageReference;
|
2021-04-13 03:28:23 +00:00
|
|
|
use MediaWiki\Page\WikiPageFactory;
|
2021-03-26 22:24:43 +00:00
|
|
|
use MediaWiki\Permissions\Authority;
|
2020-05-23 08:43:34 +00:00
|
|
|
use MediaWiki\Revision\RevisionLookup;
|
2022-03-18 03:41:27 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2023-08-25 12:29:41 +00:00
|
|
|
use MediaWiki\Status\Status;
|
2023-09-18 14:17:28 +00:00
|
|
|
use MediaWiki\Title\NamespaceInfo;
|
2021-03-26 22:24:43 +00:00
|
|
|
use MediaWiki\User\TalkPageNotificationManager;
|
2023-09-19 12:13:45 +00:00
|
|
|
use MediaWiki\User\User;
|
2021-03-26 22:24:43 +00:00
|
|
|
use MediaWiki\User\UserFactory;
|
2021-03-26 22:24:43 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2021-04-13 03:28:23 +00:00
|
|
|
use StatusValue;
|
|
|
|
|
use Wikimedia\ParamValidator\TypeDef\ExpiryDef;
|
2023-05-04 21:41:21 +00:00
|
|
|
use Wikimedia\Rdbms\ReadOnlyMode;
|
2020-05-23 08:43:34 +00:00
|
|
|
|
|
|
|
|
/**
|
2021-03-26 22:24:43 +00:00
|
|
|
* WatchlistManager service
|
2020-05-23 08:43:34 +00:00
|
|
|
*
|
|
|
|
|
* @since 1.35
|
|
|
|
|
*/
|
2021-03-26 22:24:43 +00:00
|
|
|
class WatchlistManager {
|
2020-05-23 08:43:34 +00:00
|
|
|
|
2019-10-25 08:07:22 +00:00
|
|
|
/**
|
|
|
|
|
* @internal For use by ServiceWiring
|
|
|
|
|
*/
|
2022-08-21 15:07:07 +00:00
|
|
|
public const OPTION_ENOTIF = 'isEnotifEnabled';
|
2020-05-23 08:43:34 +00:00
|
|
|
|
2022-08-21 15:07:07 +00:00
|
|
|
/** @var bool */
|
|
|
|
|
private $isEnotifEnabled;
|
2020-05-23 08:43:34 +00:00
|
|
|
|
|
|
|
|
/** @var HookRunner */
|
|
|
|
|
private $hookRunner;
|
|
|
|
|
|
|
|
|
|
/** @var ReadOnlyMode */
|
|
|
|
|
private $readOnlyMode;
|
|
|
|
|
|
|
|
|
|
/** @var RevisionLookup */
|
|
|
|
|
private $revisionLookup;
|
|
|
|
|
|
|
|
|
|
/** @var TalkPageNotificationManager */
|
|
|
|
|
private $talkPageNotificationManager;
|
|
|
|
|
|
|
|
|
|
/** @var WatchedItemStoreInterface */
|
|
|
|
|
private $watchedItemStore;
|
|
|
|
|
|
2021-03-26 22:24:43 +00:00
|
|
|
/** @var UserFactory */
|
|
|
|
|
private $userFactory;
|
|
|
|
|
|
2021-04-08 19:34:40 +00:00
|
|
|
/** @var NamespaceInfo */
|
|
|
|
|
private $nsInfo;
|
|
|
|
|
|
2021-04-13 03:28:23 +00:00
|
|
|
/** @var WikiPageFactory */
|
|
|
|
|
private $wikiPageFactory;
|
|
|
|
|
|
2020-05-23 08:43:34 +00:00
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*
|
|
|
|
|
* Cache for getTitleNotificationTimestamp
|
|
|
|
|
*
|
|
|
|
|
* Keys need to reflect both the specific user and the title:
|
|
|
|
|
*
|
|
|
|
|
* Since only users have watchlists, the user is represented with `u⧼user id⧽`
|
|
|
|
|
*
|
|
|
|
|
* Since the method accepts LinkTarget objects, cannot rely on the object's toString,
|
|
|
|
|
* since it is different for TitleValue and Title. Implement a simplified string
|
|
|
|
|
* representation of the string that TitleValue uses: `⧼namespace number⧽:⧼db key⧽`
|
|
|
|
|
*
|
|
|
|
|
* Entries are in the form of
|
|
|
|
|
* u⧼user id⧽-⧼namespace number⧽:⧼db key⧽ => ⧼timestamp or false⧽
|
|
|
|
|
*/
|
|
|
|
|
private $notificationTimestampCache = [];
|
|
|
|
|
|
|
|
|
|
/**
|
2022-08-21 15:07:07 +00:00
|
|
|
* @param array{isEnotifEnabled:bool} $options
|
2020-05-23 08:43:34 +00:00
|
|
|
* @param HookContainer $hookContainer
|
|
|
|
|
* @param ReadOnlyMode $readOnlyMode
|
|
|
|
|
* @param RevisionLookup $revisionLookup
|
|
|
|
|
* @param TalkPageNotificationManager $talkPageNotificationManager
|
|
|
|
|
* @param WatchedItemStoreInterface $watchedItemStore
|
2021-03-26 22:24:43 +00:00
|
|
|
* @param UserFactory $userFactory
|
2021-04-08 19:34:40 +00:00
|
|
|
* @param NamespaceInfo $nsInfo
|
2021-04-13 03:28:23 +00:00
|
|
|
* @param WikiPageFactory $wikiPageFactory
|
2020-05-23 08:43:34 +00:00
|
|
|
*/
|
|
|
|
|
public function __construct(
|
2022-08-21 15:07:07 +00:00
|
|
|
array $options,
|
2020-05-23 08:43:34 +00:00
|
|
|
HookContainer $hookContainer,
|
|
|
|
|
ReadOnlyMode $readOnlyMode,
|
|
|
|
|
RevisionLookup $revisionLookup,
|
|
|
|
|
TalkPageNotificationManager $talkPageNotificationManager,
|
2021-03-26 22:24:43 +00:00
|
|
|
WatchedItemStoreInterface $watchedItemStore,
|
2021-04-08 19:34:40 +00:00
|
|
|
UserFactory $userFactory,
|
2021-04-13 03:28:23 +00:00
|
|
|
NamespaceInfo $nsInfo,
|
|
|
|
|
WikiPageFactory $wikiPageFactory
|
2020-05-23 08:43:34 +00:00
|
|
|
) {
|
2022-08-21 15:07:07 +00:00
|
|
|
$this->isEnotifEnabled = $options[ self::OPTION_ENOTIF ];
|
2020-05-23 08:43:34 +00:00
|
|
|
$this->hookRunner = new HookRunner( $hookContainer );
|
|
|
|
|
$this->readOnlyMode = $readOnlyMode;
|
|
|
|
|
$this->revisionLookup = $revisionLookup;
|
|
|
|
|
$this->talkPageNotificationManager = $talkPageNotificationManager;
|
|
|
|
|
$this->watchedItemStore = $watchedItemStore;
|
2021-03-26 22:24:43 +00:00
|
|
|
$this->userFactory = $userFactory;
|
2021-04-08 19:34:40 +00:00
|
|
|
$this->nsInfo = $nsInfo;
|
2021-04-13 03:28:23 +00:00
|
|
|
$this->wikiPageFactory = $wikiPageFactory;
|
2020-05-23 08:43:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resets all of the given user's page-change notification timestamps.
|
|
|
|
|
* If e-notif e-mails are on, they will receive notification mails on
|
|
|
|
|
* the next change of any watched page.
|
|
|
|
|
*
|
|
|
|
|
* @note If the user doesn't have 'editmywatchlist', this will do nothing.
|
|
|
|
|
*
|
2021-03-26 22:24:43 +00:00
|
|
|
* @param Authority|UserIdentity $performer deprecated passing UserIdentity since 1.37
|
2020-05-23 08:43:34 +00:00
|
|
|
*/
|
2021-03-26 22:24:43 +00:00
|
|
|
public function clearAllUserNotifications( $performer ) {
|
2020-05-23 08:43:34 +00:00
|
|
|
if ( $this->readOnlyMode->isReadOnly() ) {
|
|
|
|
|
// Cannot change anything in read only
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-26 22:24:43 +00:00
|
|
|
if ( !$performer instanceof Authority ) {
|
|
|
|
|
$performer = $this->userFactory->newFromUserIdentity( $performer );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$user = $performer->getUser();
|
|
|
|
|
|
2023-08-31 14:59:34 +00:00
|
|
|
// NOTE: Has to be before `editmywatchlist` user right check, to ensure
|
|
|
|
|
// anonymous / temporary users have their talk page notifications cleared (T345031).
|
2022-08-21 15:07:07 +00:00
|
|
|
if ( !$this->isEnotifEnabled ) {
|
2020-05-23 08:43:34 +00:00
|
|
|
$this->talkPageNotificationManager->removeUserHasNewMessages( $user );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-31 14:59:34 +00:00
|
|
|
if ( !$performer->isAllowed( 'editmywatchlist' ) ) {
|
|
|
|
|
// User isn't allowed to edit the watchlist
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 18:32:20 +00:00
|
|
|
if ( !$user->isRegistered() ) {
|
2020-05-23 08:43:34 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->watchedItemStore->resetAllNotificationTimestampsForUser( $user );
|
|
|
|
|
|
|
|
|
|
// We also need to clear here the "you have new message" notification for the own
|
|
|
|
|
// user_talk page; it's cleared one page view later in WikiPage::doViewUpdates().
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clear the user's notification timestamp for the given title.
|
|
|
|
|
* If e-notif e-mails are on, they will receive notification mails on
|
|
|
|
|
* the next change of the page if it's watched etc.
|
|
|
|
|
*
|
|
|
|
|
* @note If the user doesn't have 'editmywatchlist', this will do nothing.
|
|
|
|
|
*
|
2021-03-26 22:24:43 +00:00
|
|
|
* @param Authority|UserIdentity $performer deprecated passing UserIdentity since 1.37
|
|
|
|
|
* @param LinkTarget|PageIdentity $title deprecated passing LinkTarget since 1.37
|
2020-05-23 08:43:34 +00:00
|
|
|
* @param int $oldid The revision id being viewed. If not given or 0, latest revision is assumed.
|
2022-03-18 03:41:27 +00:00
|
|
|
* @param RevisionRecord|null $oldRev The revision record associated with $oldid, or null if
|
|
|
|
|
* the latest revision is used
|
2020-05-23 08:43:34 +00:00
|
|
|
*/
|
|
|
|
|
public function clearTitleUserNotifications(
|
2021-03-26 22:24:43 +00:00
|
|
|
$performer,
|
|
|
|
|
$title,
|
2022-03-18 03:41:27 +00:00
|
|
|
int $oldid = 0,
|
2024-10-16 18:58:33 +00:00
|
|
|
?RevisionRecord $oldRev = null
|
2020-05-23 08:43:34 +00:00
|
|
|
) {
|
|
|
|
|
if ( $this->readOnlyMode->isReadOnly() ) {
|
|
|
|
|
// Cannot change anything in read only
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-26 22:24:43 +00:00
|
|
|
if ( !$performer instanceof Authority ) {
|
|
|
|
|
$performer = $this->userFactory->newFromUserIdentity( $performer );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$userIdentity = $performer->getUser();
|
2020-05-23 08:43:34 +00:00
|
|
|
$userTalkPage = (
|
|
|
|
|
$title->getNamespace() === NS_USER_TALK &&
|
2021-03-26 22:24:43 +00:00
|
|
|
$title->getDBkey() === strtr( $userIdentity->getName(), ' ', '_' )
|
2020-05-23 08:43:34 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ( $userTalkPage ) {
|
2022-03-18 03:41:27 +00:00
|
|
|
if ( !$oldid ) {
|
|
|
|
|
$oldRev = null;
|
|
|
|
|
} elseif ( !$oldRev ) {
|
|
|
|
|
$oldRev = $this->revisionLookup->getRevisionById( $oldid );
|
|
|
|
|
}
|
2023-08-31 14:59:34 +00:00
|
|
|
// NOTE: Has to be called before isAllowed() check, to ensure users with no watchlist
|
|
|
|
|
// access (by default, temporary and anonymous users) can clear their talk page
|
|
|
|
|
// notification (T345031).
|
2022-03-18 00:51:34 +00:00
|
|
|
$this->talkPageNotificationManager->clearForPageView( $userIdentity, $oldRev );
|
2020-05-23 08:43:34 +00:00
|
|
|
}
|
|
|
|
|
|
2022-08-21 15:07:07 +00:00
|
|
|
if ( !$this->isEnotifEnabled ) {
|
2020-05-23 08:43:34 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-26 22:24:43 +00:00
|
|
|
if ( !$userIdentity->isRegistered() ) {
|
2020-05-23 08:43:34 +00:00
|
|
|
// Nothing else to do
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-31 14:59:34 +00:00
|
|
|
// NOTE: Has to be checked after the TalkPageNotificationManager::clearForPageView call, to
|
|
|
|
|
// ensure users with no watchlist access (by default, temporary and anonymous users) can
|
|
|
|
|
// clear their talk page notification (T345031).
|
|
|
|
|
if ( !$performer->isAllowed( 'editmywatchlist' ) ) {
|
|
|
|
|
// User isn't allowed to edit the watchlist
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-23 08:43:34 +00:00
|
|
|
// Only update the timestamp if the page is being watched.
|
|
|
|
|
// The query to find out if it is watched is cached both in memcached and per-invocation,
|
|
|
|
|
// and when it does have to be executed, it can be on a replica DB
|
|
|
|
|
// If this is the user's newtalk page, we always update the timestamp
|
|
|
|
|
$force = $userTalkPage ? 'force' : '';
|
2021-03-26 22:24:43 +00:00
|
|
|
$this->watchedItemStore->resetNotificationTimestamp( $userIdentity, $title, $force, $oldid );
|
2020-05-23 08:43:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the timestamp when this page was updated since the user last saw it.
|
|
|
|
|
*
|
|
|
|
|
* @param UserIdentity $user
|
2021-03-26 22:24:43 +00:00
|
|
|
* @param LinkTarget|PageIdentity $title deprecated passing LinkTarget since 1.37
|
2020-05-23 08:43:34 +00:00
|
|
|
* @return string|bool|null String timestamp, false if not watched, null if nothing is unseen
|
|
|
|
|
*/
|
2021-03-26 22:24:43 +00:00
|
|
|
public function getTitleNotificationTimestamp( UserIdentity $user, $title ) {
|
2022-04-29 18:32:20 +00:00
|
|
|
if ( !$user->isRegistered() ) {
|
2020-05-23 08:43:34 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 01:14:02 +00:00
|
|
|
$cacheKey = 'u' . $user->getId() . '-' .
|
|
|
|
|
$title->getNamespace() . ':' . $title->getDBkey();
|
2020-05-23 08:43:34 +00:00
|
|
|
|
|
|
|
|
// avoid isset here, as it'll return false for null entries
|
|
|
|
|
if ( array_key_exists( $cacheKey, $this->notificationTimestampCache ) ) {
|
|
|
|
|
return $this->notificationTimestampCache[ $cacheKey ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$watchedItem = $this->watchedItemStore->getWatchedItem( $user, $title );
|
|
|
|
|
if ( $watchedItem ) {
|
|
|
|
|
$timestamp = $watchedItem->getNotificationTimestamp();
|
|
|
|
|
} else {
|
|
|
|
|
$timestamp = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->notificationTimestampCache[ $cacheKey ] = $timestamp;
|
|
|
|
|
return $timestamp;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-08 19:34:40 +00:00
|
|
|
/**
|
|
|
|
|
* @since 1.37
|
|
|
|
|
* @param PageReference $target
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isWatchable( PageReference $target ): bool {
|
|
|
|
|
if ( !$this->nsInfo->isWatchable( $target->getNamespace() ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $target instanceof PageIdentity && !$target->canExist() ) {
|
|
|
|
|
// Catch "improper" Title instances
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-26 22:24:43 +00:00
|
|
|
/**
|
|
|
|
|
* Check if the page is watched by the user.
|
|
|
|
|
* @since 1.37
|
|
|
|
|
* @param UserIdentity $userIdentity
|
|
|
|
|
* @param PageIdentity $target
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
public function isWatchedIgnoringRights( UserIdentity $userIdentity, PageIdentity $target ): bool {
|
2021-03-26 22:24:43 +00:00
|
|
|
if ( $this->isWatchable( $target ) ) {
|
|
|
|
|
return $this->watchedItemStore->isWatched( $userIdentity, $target );
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if the page is watched by the user and the user has permission to view their
|
|
|
|
|
* watchlist.
|
|
|
|
|
* @since 1.37
|
|
|
|
|
* @param Authority $performer
|
|
|
|
|
* @param PageIdentity $target
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
public function isWatched( Authority $performer, PageIdentity $target ): bool {
|
2021-03-26 22:24:43 +00:00
|
|
|
if ( $performer->isAllowed( 'viewmywatchlist' ) ) {
|
|
|
|
|
return $this->isWatchedIgnoringRights( $performer->getUser(), $target );
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if the article is temporarily watched by the user.
|
|
|
|
|
* @since 1.37
|
|
|
|
|
* @param UserIdentity $userIdentity
|
|
|
|
|
* @param PageIdentity $target
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
public function isTempWatchedIgnoringRights( UserIdentity $userIdentity, PageIdentity $target ): bool {
|
2021-03-26 22:24:43 +00:00
|
|
|
if ( $this->isWatchable( $target ) ) {
|
|
|
|
|
return $this->watchedItemStore->isTempWatched( $userIdentity, $target );
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if the page is temporarily watched by the user and the user has permission to view
|
|
|
|
|
* their watchlist.
|
|
|
|
|
* @since 1.37
|
|
|
|
|
* @param Authority $performer
|
|
|
|
|
* @param PageIdentity $target
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
public function isTempWatched( Authority $performer, PageIdentity $target ): bool {
|
2021-03-26 22:24:43 +00:00
|
|
|
if ( $performer->isAllowed( 'viewmywatchlist' ) ) {
|
|
|
|
|
return $this->isTempWatchedIgnoringRights( $performer->getUser(), $target );
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-04-19 17:35:35 +00:00
|
|
|
* Watch a page. Calls the WatchArticle and WatchArticleComplete hooks.
|
2021-03-26 22:24:43 +00:00
|
|
|
* @since 1.37
|
2021-04-19 17:35:35 +00:00
|
|
|
* @param UserIdentity $userIdentity
|
2021-03-26 22:24:43 +00:00
|
|
|
* @param PageIdentity $target
|
|
|
|
|
* @param string|null $expiry Optional expiry timestamp in any format acceptable to wfTimestamp(),
|
|
|
|
|
* null will not create expiries, or leave them unchanged should they already exist.
|
2021-04-19 17:35:35 +00:00
|
|
|
* @return StatusValue
|
2021-03-26 22:24:43 +00:00
|
|
|
*/
|
|
|
|
|
public function addWatchIgnoringRights(
|
2021-04-19 17:35:35 +00:00
|
|
|
UserIdentity $userIdentity,
|
2021-03-26 22:24:43 +00:00
|
|
|
PageIdentity $target,
|
|
|
|
|
?string $expiry = null
|
2021-07-22 03:11:47 +00:00
|
|
|
): StatusValue {
|
2021-03-26 22:24:43 +00:00
|
|
|
if ( !$this->isWatchable( $target ) ) {
|
2021-04-19 17:35:35 +00:00
|
|
|
return StatusValue::newFatal( 'watchlistnotwatchable' );
|
2021-03-26 22:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-19 17:35:35 +00:00
|
|
|
$wikiPage = $this->wikiPageFactory->newFromTitle( $target );
|
|
|
|
|
$title = $wikiPage->getTitle();
|
2021-03-26 22:24:43 +00:00
|
|
|
|
2021-04-19 17:35:35 +00:00
|
|
|
// TODO: update hooks to take Authority
|
|
|
|
|
$status = Status::newFatal( 'hookaborted' );
|
|
|
|
|
$user = $this->userFactory->newFromUserIdentity( $userIdentity );
|
|
|
|
|
if ( $this->hookRunner->onWatchArticle( $user, $wikiPage, $status, $expiry ) ) {
|
|
|
|
|
$status = StatusValue::newGood();
|
|
|
|
|
$this->watchedItemStore->addWatch( $userIdentity, $this->nsInfo->getSubjectPage( $title ), $expiry );
|
|
|
|
|
if ( $this->nsInfo->canHaveTalkPage( $title ) ) {
|
|
|
|
|
$this->watchedItemStore->addWatch( $userIdentity, $this->nsInfo->getTalkPage( $title ), $expiry );
|
|
|
|
|
}
|
|
|
|
|
$this->hookRunner->onWatchArticleComplete( $user, $wikiPage );
|
2021-03-26 22:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// eventually user_touched should be factored out of User and this should be replaced
|
2021-04-19 17:35:35 +00:00
|
|
|
$user->invalidateCache();
|
|
|
|
|
|
|
|
|
|
return $status;
|
2021-03-26 22:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Watch a page if the user has permission to edit their watchlist.
|
2021-04-19 17:35:35 +00:00
|
|
|
* Calls the WatchArticle and WatchArticleComplete hooks.
|
2021-03-26 22:24:43 +00:00
|
|
|
* @since 1.37
|
|
|
|
|
* @param Authority $performer
|
|
|
|
|
* @param PageIdentity $target
|
|
|
|
|
* @param string|null $expiry Optional expiry timestamp in any format acceptable to wfTimestamp(),
|
|
|
|
|
* null will not create expiries, or leave them unchanged should they already exist.
|
2021-04-19 17:35:35 +00:00
|
|
|
* @return StatusValue
|
2021-03-26 22:24:43 +00:00
|
|
|
*/
|
|
|
|
|
public function addWatch(
|
|
|
|
|
Authority $performer,
|
|
|
|
|
PageIdentity $target,
|
|
|
|
|
?string $expiry = null
|
2021-07-22 03:11:47 +00:00
|
|
|
): StatusValue {
|
2021-04-19 17:35:35 +00:00
|
|
|
if ( !$performer->isAllowed( 'editmywatchlist' ) ) {
|
|
|
|
|
// TODO: this function should be moved out of User
|
|
|
|
|
return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
|
2021-03-26 22:24:43 +00:00
|
|
|
}
|
2021-04-19 17:35:35 +00:00
|
|
|
|
2021-04-23 19:42:45 +00:00
|
|
|
return $this->addWatchIgnoringRights( $performer->getUser(), $target, $expiry );
|
2021-03-26 22:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-04-19 17:35:35 +00:00
|
|
|
* Stop watching a page. Calls the UnwatchArticle and UnwatchArticleComplete hooks.
|
2021-03-26 22:24:43 +00:00
|
|
|
* @since 1.37
|
2021-04-19 17:35:35 +00:00
|
|
|
* @param UserIdentity $userIdentity
|
2021-03-26 22:24:43 +00:00
|
|
|
* @param PageIdentity $target
|
2021-04-19 17:35:35 +00:00
|
|
|
* @return StatusValue
|
2021-03-26 22:24:43 +00:00
|
|
|
*/
|
|
|
|
|
public function removeWatchIgnoringRights(
|
2021-04-19 17:35:35 +00:00
|
|
|
UserIdentity $userIdentity,
|
2021-03-26 22:24:43 +00:00
|
|
|
PageIdentity $target
|
2021-07-22 03:11:47 +00:00
|
|
|
): StatusValue {
|
2021-03-26 22:24:43 +00:00
|
|
|
if ( !$this->isWatchable( $target ) ) {
|
2021-04-19 17:35:35 +00:00
|
|
|
return StatusValue::newFatal( 'watchlistnotwatchable' );
|
2021-03-26 22:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
2021-04-19 17:35:35 +00:00
|
|
|
$wikiPage = $this->wikiPageFactory->newFromTitle( $target );
|
|
|
|
|
$title = $wikiPage->getTitle();
|
2021-03-26 22:24:43 +00:00
|
|
|
|
2021-04-19 17:35:35 +00:00
|
|
|
// TODO: update hooks to take Authority
|
|
|
|
|
$status = Status::newFatal( 'hookaborted' );
|
|
|
|
|
$user = $this->userFactory->newFromUserIdentity( $userIdentity );
|
|
|
|
|
if ( $this->hookRunner->onUnwatchArticle( $user, $wikiPage, $status ) ) {
|
|
|
|
|
$status = StatusValue::newGood();
|
2021-04-23 20:42:09 +00:00
|
|
|
$this->watchedItemStore->removeWatch( $userIdentity, $this->nsInfo->getSubjectPage( $title ) );
|
2021-04-19 17:35:35 +00:00
|
|
|
if ( $this->nsInfo->canHaveTalkPage( $title ) ) {
|
2021-04-23 20:42:09 +00:00
|
|
|
$this->watchedItemStore->removeWatch( $userIdentity, $this->nsInfo->getTalkPage( $title ) );
|
2021-04-19 17:35:35 +00:00
|
|
|
}
|
|
|
|
|
$this->hookRunner->onUnwatchArticleComplete( $user, $wikiPage );
|
2021-03-26 22:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// eventually user_touched should be factored out of User and this should be replaced
|
2021-04-19 17:35:35 +00:00
|
|
|
$user->invalidateCache();
|
|
|
|
|
|
|
|
|
|
return $status;
|
2021-03-26 22:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stop watching a page if the user has permission to edit their watchlist.
|
2021-04-19 17:35:35 +00:00
|
|
|
* Calls the UnwatchArticle and UnwatchArticleComplete hooks.
|
2021-03-26 22:24:43 +00:00
|
|
|
* @since 1.37
|
|
|
|
|
* @param Authority $performer
|
|
|
|
|
* @param PageIdentity $target
|
2021-04-19 17:35:35 +00:00
|
|
|
* @return StatusValue
|
2021-03-26 22:24:43 +00:00
|
|
|
*/
|
|
|
|
|
public function removeWatch(
|
|
|
|
|
Authority $performer,
|
|
|
|
|
PageIdentity $target
|
2021-07-22 03:11:47 +00:00
|
|
|
): StatusValue {
|
2021-04-19 17:35:35 +00:00
|
|
|
if ( !$performer->isAllowed( 'editmywatchlist' ) ) {
|
|
|
|
|
// TODO: this function should be moved out of User
|
|
|
|
|
return User::newFatalPermissionDeniedStatus( 'editmywatchlist' );
|
2021-03-26 22:24:43 +00:00
|
|
|
}
|
2021-04-19 17:35:35 +00:00
|
|
|
|
2021-04-23 19:42:45 +00:00
|
|
|
return $this->removeWatchIgnoringRights( $performer->getUser(), $target );
|
2021-03-26 22:24:43 +00:00
|
|
|
}
|
2021-04-13 03:28:23 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Watch or unwatch a page, calling watch/unwatch hooks as appropriate.
|
|
|
|
|
* Checks before watching or unwatching to see if the page is already in the requested watch
|
|
|
|
|
* state and if the expiry is the same so it does not act unnecessarily.
|
|
|
|
|
*
|
|
|
|
|
* @param bool $watch Whether to watch or unwatch the page
|
|
|
|
|
* @param Authority $performer who is watching/unwatching
|
2021-04-21 04:25:46 +00:00
|
|
|
* @param PageIdentity $target Page to watch/unwatch
|
2021-04-13 03:28:23 +00:00
|
|
|
* @param string|null $expiry Optional expiry timestamp in any format acceptable to wfTimestamp(),
|
|
|
|
|
* null will not create expiries, or leave them unchanged should they already exist.
|
|
|
|
|
* @return StatusValue
|
|
|
|
|
* @since 1.37
|
|
|
|
|
*/
|
|
|
|
|
public function setWatch(
|
|
|
|
|
bool $watch,
|
|
|
|
|
Authority $performer,
|
2021-04-21 04:25:46 +00:00
|
|
|
PageIdentity $target,
|
2024-10-16 18:58:33 +00:00
|
|
|
?string $expiry = null
|
2021-07-22 03:11:47 +00:00
|
|
|
): StatusValue {
|
2024-07-26 18:14:40 +00:00
|
|
|
// User must be registered, and (T371091) not a temp user
|
|
|
|
|
if ( !$performer->getUser()->isRegistered() || $performer->isTemp() ) {
|
2021-04-13 03:28:23 +00:00
|
|
|
return StatusValue::newGood();
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-26 18:14:40 +00:00
|
|
|
// User must be either changing the watch state or at least the expiry.
|
|
|
|
|
|
2022-05-09 09:09:00 +00:00
|
|
|
// Only call addWatchIgnoringRights() or removeWatch() if there's been a change in the watched status.
|
2022-07-31 11:43:50 +00:00
|
|
|
$oldWatchedItem = $this->watchedItemStore->getWatchedItem( $performer->getUser(), $target );
|
2021-04-13 03:28:23 +00:00
|
|
|
$changingWatchStatus = (bool)$oldWatchedItem !== $watch;
|
|
|
|
|
if ( $oldWatchedItem && $expiry !== null ) {
|
|
|
|
|
// If there's an old watched item, a non-null change to the expiry requires an UPDATE.
|
2021-06-04 04:12:21 +00:00
|
|
|
$oldWatchPeriod = $oldWatchedItem->getExpiry() ?? 'infinity';
|
2021-04-13 03:28:23 +00:00
|
|
|
$changingWatchStatus = $changingWatchStatus ||
|
|
|
|
|
$oldWatchPeriod !== ExpiryDef::normalizeExpiry( $expiry, TS_MW );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $changingWatchStatus ) {
|
|
|
|
|
// If the user doesn't have 'editmywatchlist', we still want to
|
|
|
|
|
// allow them to add but not remove items via edits and such.
|
|
|
|
|
if ( $watch ) {
|
2021-04-21 04:25:46 +00:00
|
|
|
return $this->addWatchIgnoringRights( $performer->getUser(), $target, $expiry );
|
2021-04-13 03:28:23 +00:00
|
|
|
} else {
|
2021-04-21 04:25:46 +00:00
|
|
|
return $this->removeWatch( $performer, $target );
|
2021-04-13 03:28:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return StatusValue::newGood();
|
|
|
|
|
}
|
2020-05-23 08:43:34 +00:00
|
|
|
}
|