2015-08-24 23:43:38 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-13 06:43:40 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications;
|
|
|
|
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\Deferred\DeferredUpdates;
|
2019-04-17 11:59:14 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\User\CentralId\CentralIdLookup;
|
2024-03-15 08:27:36 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2022-11-13 06:43:40 +00:00
|
|
|
use UnexpectedValueException;
|
2024-10-19 22:55:03 +00:00
|
|
|
use Wikimedia\ObjectCache\BagOStuff;
|
|
|
|
use Wikimedia\ObjectCache\CachedBagOStuff;
|
2019-04-17 11:59:14 +00:00
|
|
|
|
2015-08-24 23:43:38 +00:00
|
|
|
/**
|
|
|
|
* A small wrapper around ObjectCache to manage
|
|
|
|
* storing the last time a user has seen notifications
|
|
|
|
*/
|
2022-11-13 06:43:40 +00:00
|
|
|
class SeenTime {
|
2015-08-24 23:43:38 +00:00
|
|
|
|
|
|
|
/**
|
2015-09-03 01:11:10 +00:00
|
|
|
* Allowed notification types
|
2018-08-13 07:25:22 +00:00
|
|
|
* @var string[]
|
2015-08-24 23:43:38 +00:00
|
|
|
*/
|
2016-12-05 18:51:07 +00:00
|
|
|
private static $allowedTypes = [ 'alert', 'message' ];
|
2015-08-24 23:43:38 +00:00
|
|
|
|
2024-03-15 08:27:36 +00:00
|
|
|
private UserIdentity $user;
|
2015-08-24 23:43:38 +00:00
|
|
|
|
|
|
|
/**
|
2024-03-15 08:27:36 +00:00
|
|
|
* @param UserIdentity $user A logged in user
|
2015-08-24 23:43:38 +00:00
|
|
|
*/
|
2024-03-15 08:27:36 +00:00
|
|
|
private function __construct( UserIdentity $user ) {
|
2015-08-24 23:43:38 +00:00
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
2024-03-15 08:27:36 +00:00
|
|
|
public static function newFromUser( UserIdentity $user ): self {
|
2015-08-24 23:43:38 +00:00
|
|
|
return new self( $user );
|
|
|
|
}
|
|
|
|
|
2016-09-08 19:22:24 +00:00
|
|
|
/**
|
|
|
|
* Hold onto a cache for our operations. Static so it can reuse the same
|
|
|
|
* in-process cache in different instances.
|
|
|
|
*
|
|
|
|
* @return BagOStuff
|
|
|
|
*/
|
|
|
|
private static function cache() {
|
2019-10-04 00:01:00 +00:00
|
|
|
static $wrappedCache = null;
|
2024-05-14 10:15:24 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
2016-09-08 19:22:24 +00:00
|
|
|
|
2019-10-04 00:01:00 +00:00
|
|
|
// Use a configurable cache backend (T222851) and wrap it with CachedBagOStuff
|
|
|
|
// for an in-process cache (T144534)
|
|
|
|
if ( $wrappedCache === null ) {
|
2024-05-14 10:15:24 +00:00
|
|
|
$cacheConfig = $services->getMainConfig()->get( 'EchoSeenTimeCacheType' );
|
2019-10-04 00:01:00 +00:00
|
|
|
if ( $cacheConfig === null ) {
|
2022-11-13 06:43:40 +00:00
|
|
|
// Hooks::initEchoExtension sets EchoSeenTimeCacheType to $wgMainStash if it's
|
2019-10-04 00:01:00 +00:00
|
|
|
// null, so this can only happen if $wgMainStash is also null
|
|
|
|
throw new UnexpectedValueException(
|
|
|
|
'Either $wgEchoSeenTimeCacheType or $wgMainStash must be set'
|
|
|
|
);
|
|
|
|
}
|
2024-05-14 10:15:24 +00:00
|
|
|
return new CachedBagOStuff( $services->getObjectCacheFactory()->getInstance( $cacheConfig ) );
|
2016-09-08 19:22:24 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 00:01:00 +00:00
|
|
|
return $wrappedCache;
|
2016-09-08 19:22:24 +00:00
|
|
|
}
|
|
|
|
|
2015-08-24 23:43:38 +00:00
|
|
|
/**
|
2016-07-20 03:02:33 +00:00
|
|
|
* @param string $type Type of seen time to get
|
|
|
|
* @param int $format Format to return time in, defaults to TS_MW
|
2018-08-13 07:32:22 +00:00
|
|
|
* @return string|false Timestamp in specified format, or false if no stored time
|
2015-08-24 23:43:38 +00:00
|
|
|
*/
|
2016-09-23 18:46:44 +00:00
|
|
|
public function getTime( $type = 'all', $format = TS_MW ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$vals = [];
|
2015-09-03 01:11:10 +00:00
|
|
|
if ( $type === 'all' ) {
|
|
|
|
foreach ( self::$allowedTypes as $allowed ) {
|
2016-07-29 00:03:56 +00:00
|
|
|
// Use TS_MW, then convert later, so max works properly for
|
|
|
|
// all formats.
|
2016-09-23 18:46:44 +00:00
|
|
|
$vals[] = $this->getTime( $allowed, TS_MW );
|
2015-09-03 01:11:10 +00:00
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2016-08-01 19:11:34 +00:00
|
|
|
return wfTimestamp( $format, min( $vals ) );
|
2015-09-03 01:11:10 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 20:00:35 +00:00
|
|
|
if ( !$this->validateType( $type ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-23 18:46:44 +00:00
|
|
|
$data = self::cache()->get( $this->getMemcKey( $type ) );
|
2016-09-13 20:00:35 +00:00
|
|
|
|
|
|
|
if ( $data === false ) {
|
2021-12-01 18:29:00 +00:00
|
|
|
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
|
2016-09-13 20:00:35 +00:00
|
|
|
// Check if the user still has it set in their preferences
|
2021-12-01 18:29:00 +00:00
|
|
|
$data = $userOptionsLookup->getOption( $this->user, 'echo-seen-time', false );
|
2015-08-24 23:43:38 +00:00
|
|
|
}
|
2016-09-06 17:38:01 +00:00
|
|
|
|
|
|
|
if ( $data === false ) {
|
2016-09-09 21:46:11 +00:00
|
|
|
// We can't remember their real seen time, so reset everything to
|
|
|
|
// unseen.
|
|
|
|
$data = wfTimestamp( TS_MW, 1 );
|
2016-07-20 03:02:33 +00:00
|
|
|
}
|
2016-09-06 17:38:01 +00:00
|
|
|
return wfTimestamp( $format, $data );
|
2015-08-24 23:43:38 +00:00
|
|
|
}
|
|
|
|
|
2016-07-20 22:20:59 +00:00
|
|
|
/**
|
|
|
|
* Sets the seen time
|
|
|
|
*
|
|
|
|
* @param string $time Time, in TS_MW format
|
|
|
|
* @param string $type Type of seen time to set
|
|
|
|
*/
|
2016-09-13 20:00:35 +00:00
|
|
|
public function setTime( $time, $type = 'all' ) {
|
2015-09-03 01:11:10 +00:00
|
|
|
if ( $type === 'all' ) {
|
|
|
|
foreach ( self::$allowedTypes as $allowed ) {
|
|
|
|
$this->setTime( $time, $allowed );
|
|
|
|
}
|
2016-09-23 19:11:37 +00:00
|
|
|
return;
|
2015-09-03 01:11:10 +00:00
|
|
|
}
|
2016-09-23 19:11:37 +00:00
|
|
|
|
|
|
|
if ( !$this->validateType( $type ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write to the in-memory cache immediately, and defer writing to
|
|
|
|
// the real cache
|
|
|
|
$key = $this->getMemcKey( $type );
|
|
|
|
$cache = self::cache();
|
2019-10-04 21:54:07 +00:00
|
|
|
$cache->set( $key, $time, $cache::TTL_YEAR, BagOStuff::WRITE_CACHE_ONLY );
|
2021-05-04 16:06:42 +00:00
|
|
|
DeferredUpdates::addCallableUpdate( static function () use ( $key, $time, $cache ) {
|
2019-10-04 21:54:07 +00:00
|
|
|
$cache->set( $key, $time, $cache::TTL_YEAR );
|
2016-09-23 19:11:37 +00:00
|
|
|
} );
|
2015-09-03 01:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the given type, make sure it is allowed.
|
|
|
|
*
|
|
|
|
* @param string $type Given type
|
|
|
|
* @return bool Type is allowed
|
|
|
|
*/
|
|
|
|
private function validateType( $type ) {
|
|
|
|
return in_array( $type, self::$allowedTypes );
|
2015-08-24 23:43:38 +00:00
|
|
|
}
|
2016-09-13 23:18:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a memcached key.
|
|
|
|
*
|
|
|
|
* @param string $type Given notification type
|
|
|
|
* @return string Memcached key
|
|
|
|
*/
|
|
|
|
protected function getMemcKey( $type = 'all' ) {
|
2019-04-17 11:59:14 +00:00
|
|
|
$localKey = self::cache()->makeKey(
|
|
|
|
'echo', 'seen', $type, 'time', $this->user->getId()
|
|
|
|
);
|
2021-12-01 18:29:00 +00:00
|
|
|
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
|
2016-09-13 23:18:29 +00:00
|
|
|
|
2021-12-01 18:29:00 +00:00
|
|
|
if ( !$userOptionsLookup->getOption( $this->user, 'echo-cross-wiki-notifications' ) ) {
|
2016-09-13 23:18:29 +00:00
|
|
|
return $localKey;
|
|
|
|
}
|
|
|
|
|
2021-06-24 19:21:49 +00:00
|
|
|
$globalId = MediaWikiServices::getInstance()
|
|
|
|
->getCentralIdLookup()
|
|
|
|
->centralIdFromLocalUser( $this->user, CentralIdLookup::AUDIENCE_RAW );
|
2016-09-13 23:18:29 +00:00
|
|
|
|
|
|
|
if ( !$globalId ) {
|
|
|
|
return $localKey;
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:59:14 +00:00
|
|
|
return self::cache()->makeGlobalKey(
|
|
|
|
'echo', 'seen', $type, 'time', $globalId
|
|
|
|
);
|
2016-09-13 23:18:29 +00:00
|
|
|
}
|
2015-08-24 23:43:38 +00:00
|
|
|
}
|