Debian Codegraph @

extensions/Echo/includes/SeenTime.php

raw symbols SCIP
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 * A small wrapper around ObjectCache to manage
15 * storing the last time a user has seen notifications
16 */
17 class SeenTime {
18
19 /**
20 * Allowed notification types
21 * @var string[]
22 */
23 private static $allowedTypes = [ 'alert', 'message' ];
24
25 private UserIdentity $user;
26
27 /**
28 * @param UserIdentity $user A logged in user
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 * Hold onto a cache for our operations. Static so it can reuse the same
40 * in-process cache in different instances.
41 *
42 * @return BagOStuff
43 */
44 private static function cache() {
45 static $wrappedCache = null;
46 $services = MediaWikiServices::getInstance();
47
48 // Use a configurable cache backend (T222851) and wrap it with CachedBagOStuff
49 // for an in-process cache (T144534)
50 if ( $wrappedCache === null ) {
51 $cacheConfig = $services->getMainConfig()->get( 'EchoSeenTimeCacheType' );
52 if ( $cacheConfig === null ) {
53 // Hooks::initEchoExtension sets EchoSeenTimeCacheType to $wgMainStash if it's
54 // null, so this can only happen if $wgMainStash is also null
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 * @param string $type Type of seen time to get
67 * @param int $format Format to return time in, defaults to TS_MW
68 * @return string|false Timestamp in specified format, or false if no stored time
69 */
70 public function getTime( $type = 'all', $format = TS_MW ) {
71 $vals = [];
72 if ( $type === 'all' ) {
73 foreach ( self::$allowedTypes as $allowed ) {
74 // Use TS_MW, then convert later, so max works properly for
75 // all formats.
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 // Check if the user still has it set in their preferences
91 $data = $userOptionsLookup->getOption( $this->user, 'echo-seen-time', false );
92 }
93
94 if ( $data === false ) {
95 // We can't remember their real seen time, so reset everything to
96 // unseen.
97 $data = wfTimestamp( TS_MW, 1 );
98 }
99 return wfTimestamp( $format, $data );
100 }
101
102 /**
103 * Sets the seen time
104 *
105 * @param string $time Time, in TS_MW format
106 * @param string $type Type of seen time to set
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 // Write to the in-memory cache immediately, and defer writing to
121 // the real cache
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 * Validate the given type, make sure it is allowed.
132 *
133 * @param string $type Given type
134 * @return bool Type is allowed
135 */
136 private function validateType( $type ) {
137 return in_array( $type, self::$allowedTypes );
138 }
139
140 /**
141 * Build a memcached key.
142 *
143 * @param string $type Given notification type
144 * @return string Memcached key
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 }