| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications; |
| 4 |
|
| 5 |
use MediaWiki\Deferred\DeferredUpdates; |
| 6 |
use MediaWiki\MediaWikiServices; |
| 7 |
use MediaWiki\User\CentralId\CentralIdLookup; |
| 8 |
use MediaWiki\User\UserIdentity; |
| 9 |
use UnexpectedValueException; |
| 10 |
use Wikimedia\ObjectCache\BagOStuff; |
| 11 |
use Wikimedia\ObjectCache\CachedBagOStuff; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class SeenTime { |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
private static $allowedTypes = [ 'alert', 'message' ]; |
| 24 |
|
| 25 |
private UserIdentity $user; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
private function __construct( UserIdentity $user ) { |
| 31 |
$this->user = $user; |
| 32 |
} |
| 33 |
|
| 34 |
public static function newFromUser( UserIdentity $user ): self { |
| 35 |
return new self( $user ); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
private static function cache() { |
| 45 |
static $wrappedCache = null; |
| 46 |
$services = MediaWikiServices::getInstance(); |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
if ( $wrappedCache === null ) { |
| 51 |
$cacheConfig = $services->getMainConfig()->get( 'EchoSeenTimeCacheType' ); |
| 52 |
if ( $cacheConfig === null ) { |
| 53 |
|
| 54 |
|
| 55 |
throw new UnexpectedValueException( |
| 56 |
'Either $wgEchoSeenTimeCacheType or $wgMainStash must be set' |
| 57 |
); |
| 58 |
} |
| 59 |
return new CachedBagOStuff( $services->getObjectCacheFactory()->getInstance( $cacheConfig ) ); |
| 60 |
} |
| 61 |
|
| 62 |
return $wrappedCache; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public function getTime( $type = 'all', $format = TS_MW ) { |
| 71 |
$vals = []; |
| 72 |
if ( $type === 'all' ) { |
| 73 |
foreach ( self::$allowedTypes as $allowed ) { |
| 74 |
|
| 75 |
|
| 76 |
$vals[] = $this->getTime( $allowed, TS_MW ); |
| 77 |
} |
| 78 |
|
| 79 |
return wfTimestamp( $format, min( $vals ) ); |
| 80 |
} |
| 81 |
|
| 82 |
if ( !$this->validateType( $type ) ) { |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
$data = self::cache()->get( $this->getMemcKey( $type ) ); |
| 87 |
|
| 88 |
if ( $data === false ) { |
| 89 |
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup(); |
| 90 |
|
| 91 |
$data = $userOptionsLookup->getOption( $this->user, 'echo-seen-time', false ); |
| 92 |
} |
| 93 |
|
| 94 |
if ( $data === false ) { |
| 95 |
|
| 96 |
|
| 97 |
$data = wfTimestamp( TS_MW, 1 ); |
| 98 |
} |
| 99 |
return wfTimestamp( $format, $data ); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public function setTime( $time, $type = 'all' ) { |
| 109 |
if ( $type === 'all' ) { |
| 110 |
foreach ( self::$allowedTypes as $allowed ) { |
| 111 |
$this->setTime( $time, $allowed ); |
| 112 |
} |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
if ( !$this->validateType( $type ) ) { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
$key = $this->getMemcKey( $type ); |
| 123 |
$cache = self::cache(); |
| 124 |
$cache->set( $key, $time, $cache::TTL_YEAR, BagOStuff::WRITE_CACHE_ONLY ); |
| 125 |
DeferredUpdates::addCallableUpdate( static function () use ( $key, $time, $cache ) { |
| 126 |
$cache->set( $key, $time, $cache::TTL_YEAR ); |
| 127 |
} ); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
private function validateType( $type ) { |
| 137 |
return in_array( $type, self::$allowedTypes ); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
protected function getMemcKey( $type = 'all' ) { |
| 147 |
$localKey = self::cache()->makeKey( |
| 148 |
'echo', 'seen', $type, 'time', $this->user->getId() |
| 149 |
); |
| 150 |
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup(); |
| 151 |
|
| 152 |
if ( !$userOptionsLookup->getOption( $this->user, 'echo-cross-wiki-notifications' ) ) { |
| 153 |
return $localKey; |
| 154 |
} |
| 155 |
|
| 156 |
$globalId = MediaWikiServices::getInstance() |
| 157 |
->getCentralIdLookup() |
| 158 |
->centralIdFromLocalUser( $this->user, CentralIdLookup::AUDIENCE_RAW ); |
| 159 |
|
| 160 |
if ( !$globalId ) { |
| 161 |
return $localKey; |
| 162 |
} |
| 163 |
|
| 164 |
return self::cache()->makeGlobalKey( |
| 165 |
'echo', 'seen', $type, 'time', $globalId |
| 166 |
); |
| 167 |
} |
| 168 |
} |