Mooeypoo has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/313148

Change subject: [wip] Store the number of pageviews users have before seeing 
notifs
......................................................................

[wip] Store the number of pageviews users have before seeing notifs

Bug: T114841
Change-Id: Ia16638b3decfb9a01d0359ddc76990b64deac1c8
---
A includes/PageViewsLogger.php
1 file changed, 180 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Echo 
refs/changes/48/313148/1

diff --git a/includes/PageViewsLogger.php b/includes/PageViewsLogger.php
new file mode 100644
index 0000000..05ffffa
--- /dev/null
+++ b/includes/PageViewsLogger.php
@@ -0,0 +1,180 @@
+<?php
+
+/**
+ * A small wrapper around ObjectCache to manage
+ * storing and resetting the number of pageviews
+ * before some action is taken
+ */
+class EchoPageViewsLogger {
+       /**
+        * Allowed notification types
+        * @var array
+        */
+       private static $allowedTypes = array( 'alert', 'message' );
+
+       /**
+        * @var User
+        */
+       private $user;
+
+       /**
+        * @param User $user A logged in user
+        */
+       private function __construct( User $user ) {
+               $this->user = $user;
+       }
+
+       /**
+        * @param User $user
+        * @return EchoPageViews
+        */
+       public static function newFromUser( User $user ) {
+               return new self( $user );
+       }
+
+       /**
+        * Increment the counter for this notification type
+        *
+        * @param string $type Given type
+        */
+       public function increment( $type = 'all', $currSeenTime = null ) {
+               if ( $type === 'all' ) {
+                       foreach ( self::$allowedTypes as $allowed ) {
+                               $this->increment( $allowed );
+                       }
+                       return;
+               }
+
+               if ( !$this->validateType( $type ) ) {
+                       return;
+               }
+
+               // Get the current relevant seen time
+               $currSeenTime = $currSeenTime !== null ?
+                       $currSeenTime :
+                       EchoSeenTime::newFromUser( $this->user )->getTime( 
$type );
+
+               // Store the new value in the cache, if relevant
+               $this->cache()->merge(
+                       $this->getMemcKey( $type ),
+                       function ( BagOStuff $cache, $key, $value ) use ( 
$currSeenTime ) {
+                               $count = 1;
+                               $newSeenTime = $currSeenTime;
+
+                               // Value is an array of [ count => 0, seenTime 
=> seenTimeItCountsFor ]
+                               if (
+                                       $value &&
+                                       isset( $value[ 'count' ] ) &&
+                                       isset( $value[ 'seenTime' ] ) &&
+                                       $currSeenTime === $value[ 'seenTime' ]
+                               ) {
+                                       $newSeenTime = $value[ 'seenTime' ];
+                                       // Increase count
+                                       $count = ( (int)$value[ 'count' ] )++;
+                               }
+
+                               return array( 'count' => $count, 'seenTime' => 
$newSeenTime );
+                       }
+               );
+       }
+
+       public function getStoredCount( $type ) {
+               // It makes no sense to ask for "all" here in
+               // case the count is different.
+               if ( !$this->validateType( $type ) ) {
+                       return false;
+               }
+
+               $data = self::cache()->get( $this->getMemcKey( $type ) );
+
+               if ( $data && isset( $data[ 'count' ] ) ) {
+                       return $data[ 'count' ];
+               } else {
+                       // Nonexisting. Return 0
+                       return 0;
+               }
+       }
+
+       public function getStoredSeenTime( $type ) {
+               // It makes no sense to ask for "all" here in
+               // case the count is different.
+               if ( !$this->validateType( $type ) ) {
+                       return false;
+               }
+
+               $data = self::cache()->get( $this->getMemcKey( $type ) );
+
+               if ( $data && isset( $data[ 'seenTime' ] ) ) {
+                       return $data[ 'seenTime' ];
+               } else {
+                       // Nonexisting. Return 0
+                       return 0;
+               }
+       }
+
+       /**
+        * 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 main stash for persistent storage, and
+               // wrap it with CachedBagOStuff for an in-process
+               // cache. (T144534)
+               if ( $c === null ) {
+                       $c = new CachedBagOStuff(
+                               ObjectCache::getMainStashInstance()
+                       );
+               }
+
+               return $c;
+       }
+
+       /**
+        * Build a memcached key.
+        *
+        * @param string $type Given notification type
+        * @return string Memcached key
+        */
+       protected function getMemcKey( $type = 'all' ) {
+               $localKey = wfMemcKey(
+                       'echo',
+                       'pageview',
+                       $type,
+                       'counter',
+                       $this->user->getId()
+               );
+
+               if ( !$this->user->getOption( 'echo-cross-wiki-notifications' ) 
) {
+                       return $localKey;
+               }
+
+               $lookup = CentralIdLookup::factory();
+               $globalId = $lookup->centralIdFromLocalUser( $this->user, 
CentralIdLookup::AUDIENCE_RAW );
+
+               if ( !$globalId ) {
+                       return $localKey;
+               }
+
+               return wfGlobalCacheKey(
+                       'echo',
+                       'pageview',
+                       $type,
+                       'counter',
+                       $globalId
+               );
+       }
+
+       /**
+        * 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 );
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/313148
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia16638b3decfb9a01d0359ddc76990b64deac1c8
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Echo
Gerrit-Branch: master
Gerrit-Owner: Mooeypoo <mor...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to