2015-08-24 23:43:38 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A small wrapper around ObjectCache to manage
|
|
|
|
* storing the last time a user has seen notifications
|
|
|
|
*/
|
|
|
|
class EchoSeenTime {
|
|
|
|
|
|
|
|
/**
|
2015-09-03 01:11:10 +00:00
|
|
|
* Allowed notification types
|
|
|
|
* @var array
|
2015-08-24 23:43:38 +00:00
|
|
|
*/
|
2015-09-03 01:11:10 +00:00
|
|
|
private static $allowedTypes = array( 'alert', 'message' );
|
2015-08-24 23:43:38 +00:00
|
|
|
|
|
|
|
/**
|
2015-09-03 01:11:10 +00:00
|
|
|
* @var User
|
2015-08-24 23:43:38 +00:00
|
|
|
*/
|
2015-09-03 01:11:10 +00:00
|
|
|
private $user;
|
2015-08-24 23:43:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user A logged in user
|
|
|
|
*/
|
|
|
|
private function __construct( User $user ) {
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
* @return EchoSeenTime
|
|
|
|
*/
|
|
|
|
public static function newFromUser( User $user ) {
|
|
|
|
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() {
|
|
|
|
static $c = null;
|
|
|
|
|
|
|
|
// Use db-replicated for persistent storage, and
|
|
|
|
// wrap it with CachedBagOStuff for an in-process
|
|
|
|
// cache. (T144534)
|
|
|
|
if ( $c === null ) {
|
|
|
|
$c = new CachedBagOStuff(
|
|
|
|
ObjectCache::getInstance( 'db-replicated' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $c;
|
|
|
|
}
|
|
|
|
|
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
|
2015-08-24 23:43:38 +00:00
|
|
|
* @param int $flags BagOStuff::READ_LATEST to use the master
|
2016-07-20 03:02:33 +00:00
|
|
|
* @param int $format Format to return time in, defaults to TS_MW
|
|
|
|
* @return string|bool Timestamp in specified format, or false if no stored time
|
2015-08-24 23:43:38 +00:00
|
|
|
*/
|
2016-07-20 03:02:33 +00:00
|
|
|
public function getTime( $type = 'all', $flags = 0, $format = TS_MW ) {
|
2015-09-03 01:11:10 +00:00
|
|
|
$vals = array();
|
|
|
|
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.
|
|
|
|
$vals[] = $this->getTime( $allowed, $flags, 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
|
|
|
}
|
|
|
|
|
|
|
|
if ( $this->validateType( $type ) ) {
|
|
|
|
$key = wfMemcKey( 'echo', 'seen', $type, 'time', $this->user->getId() );
|
|
|
|
$cas = 0; // Unused, but we have to pass something by reference
|
2016-09-08 19:22:24 +00:00
|
|
|
$data = self::cache()->get( $key, $cas, $flags );
|
2015-09-03 01:11:10 +00:00
|
|
|
if ( $data === false ) {
|
|
|
|
// Check if the user still has it set in their preferences
|
|
|
|
$data = $this->user->getOption( 'echo-seen-time', false );
|
|
|
|
}
|
2015-08-24 23:43:38 +00:00
|
|
|
}
|
2016-07-20 03:02:33 +00:00
|
|
|
if ( $data !== false ) {
|
|
|
|
$formattedData = wfTimestamp( $format, $data );
|
|
|
|
} else {
|
|
|
|
$formattedData = $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $formattedData;
|
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
|
|
|
|
*/
|
2015-09-03 01:11:10 +00:00
|
|
|
public function setTime( $time, $type = 'all' ) {
|
|
|
|
if ( $type === 'all' ) {
|
|
|
|
foreach ( self::$allowedTypes as $allowed ) {
|
|
|
|
$this->setTime( $time, $allowed );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( $this->validateType( $type ) ) {
|
|
|
|
$key = wfMemcKey( 'echo', 'seen', $type, 'time', $this->user->getId() );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2016-09-08 19:22:24 +00:00
|
|
|
return self::cache()->set( $key, $time );
|
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
|
|
|
}
|
|
|
|
}
|