r90143 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90142‎ | r90143 | r90144 >
Date:20:39, 15 June 2011
Author:aaron
Status:ok
Tags:
Comment:
FRUserActivity (r90033) improvements:
* Keep track of instance counter for people with multiple (same) diff windows open
* Reduced cache setting code duplication
* Use class constants and tweaked function return style
* Fixed doc typos
Modified paths:
  • /trunk/extensions/FlaggedRevs/dataclasses/FRUserActivity.php (modified) (history)
  • /trunk/extensions/FlaggedRevs/presentation/modules/review.js (modified) (history)

Diff [purge]

Index: trunk/extensions/FlaggedRevs/dataclasses/FRUserActivity.php
@@ -3,6 +3,9 @@
44 * Class of utility functions for getting/tracking user activity
55 */
66 class FRUserActivity {
 7+ const PAGE_REVIEW_MS = 1200; // 20*60
 8+ const CHANGE_REVIEW_MS = 360; // 6*60
 9+
710 /**
811 * Get number of active users watching a page
912 * @param Title $title
@@ -48,14 +51,13 @@
4952 global $wgMemc;
5053 $key = wfMemcKey( 'flaggedrevs', 'userReviewingPage', $pageId );
5154 $val = $wgMemc->get( $key );
52 - if ( is_array( $val ) && count( $val ) == 2 ) {
53 - return $val;
54 - }
55 - return array( null, null );
 55+ return ( count( $val ) == 3 )
 56+ ? array( $val[0], $val[1] )
 57+ : array( null, null );
5658 }
5759
5860 /*
59 - * Check is someone is currently reviewing a page
 61+ * Check if someone is currently reviewing a page
6062 * @param int $pageId
6163 * @return bool
6264 */
@@ -65,50 +67,28 @@
6668 }
6769
6870 /*
69 - * Set the flag for who is reviewing a page if not already set by someone
 71+ * Set the flag for who is reviewing a page if not already set by someone.
 72+ * If already set, then increment the instance counter (multiple windows)
 73+ * and add on time to the expiry.
 74+ *
7075 * @param User $user
7176 * @param int $pageId
7277 * @return bool flag set
7378 */
74 - public static function setUserReviewingPage( $user, $pageId ) {
75 - global $wgMemc;
 79+ public static function setUserReviewingPage( User $user, $pageId ) {
7680 $key = wfMemcKey( 'flaggedrevs', 'userReviewingPage', $pageId );
77 - $val = array( $user->getName(), wfTimestampNow() );
78 - $wasSet = false;
79 -
80 - $wgMemc->lock( $key, 4 ); // 4 sec timeout
81 - if ( !$wgMemc->get( $key ) ) { // no flag set
82 - $wgMemc->set( $key, $val, 20*60 ); // 20 min
83 - $wasSet = true;
84 - }
85 - $wgMemc->unlock( $key );
86 -
87 - return $wasSet;
 81+ return self::incUserReviewingItem( $key, $user, self::PAGE_REVIEW_MS );
8882 }
8983
9084 /*
91 - * Clear the flag for who is reviewing a page
 85+ * Decrement/clear the flag for who is reviewing a page
9286 * @param User $user
9387 * @param int $pageId
9488 * @return bool flag unset
9589 */
96 - public static function clearUserReviewingPage( $user, $pageId ) {
97 - global $wgMemc;
 90+ public static function clearUserReviewingPage( User $user, $pageId ) {
9891 $key = wfMemcKey( 'flaggedrevs', 'userReviewingPage', $pageId );
99 - $wgMemc->lock( $key, 4 ); // 4 sec timeout
100 - $val = $wgMemc->get( $key );
101 - $wasSet = false;
102 -
103 - if ( is_array( $val ) && count( $val ) == 2 ) { // flag set
104 - list( $u, $ts ) = $val;
105 - if ( $u === $user->getName() ) {
106 - $wgMemc->delete( $key );
107 - $wasSet = true;
108 - }
109 - }
110 - $wgMemc->unlock( $key );
111 -
112 - return $wasSet;
 92+ return self::decUserReviewingItem( $key, $user, self::PAGE_REVIEW_MS );
11393 }
11494
11595 /*
@@ -121,14 +101,13 @@
122102 global $wgMemc;
123103 $key = wfMemcKey( 'flaggedrevs', 'userReviewingDiff', $oldId, $newId );
124104 $val = $wgMemc->get( $key );
125 - if ( is_array( $val ) && count( $val ) == 2 ) {
126 - return $val;
127 - }
128 - return array( null, null );
 105+ return ( count( $val ) == 3 )
 106+ ? array( $val[0], $val[1] )
 107+ : array( null, null );
129108 }
130109
131110 /*
132 - * Check is someone is currently reviewing a diff
 111+ * Check if someone is currently reviewing a diff
133112 * @param int $oldId
134113 * @param int $newId
135114 * @return bool
@@ -139,20 +118,46 @@
140119 }
141120
142121 /*
143 - * Set the flag for who is reviewing a diff if not already set by someone
 122+ * Set the flag for who is reviewing a diff if not already set by someone.
 123+ * If already set, then increment the instance counter (multiple windows)
 124+ * and add on time to the expiry.
144125 * @param User $user
145126 * @param int $pageId
146127 * @return bool flag set
147128 */
148 - public static function setUserReviewingDiff( $user, $oldId, $newId ) {
149 - global $wgMemc;
 129+ public static function setUserReviewingDiff( User $user, $oldId, $newId ) {
150130 $key = wfMemcKey( 'flaggedrevs', 'userReviewingDiff', $oldId, $newId );
151 - $val = array( $user->getName(), wfTimestampNow() );
152 - $wasSet = false;
 131+ return self::incUserReviewingItem( $key, $user, self::CHANGE_REVIEW_MS );
 132+ }
153133
 134+ /*
 135+ * Decrement/clear the flag for who is reviewing a diff
 136+ * @param User $user
 137+ * @param int $oldId
 138+ * @param int $newId
 139+ * @return bool flag unset
 140+ */
 141+ public static function clearUserReviewingDiff( User $user, $oldId, $newId ) {
 142+ $key = wfMemcKey( 'flaggedrevs', 'userReviewingDiff', $oldId, $newId );
 143+ return self::decUserReviewingItem( $key, $user, self::CHANGE_REVIEW_MS );
 144+ }
 145+
 146+ protected static function incUserReviewingItem( $key, User $user, $ttlMs ) {
 147+ global $wgMemc;
 148+ $wasSet = false; // was changed?
 149+
154150 $wgMemc->lock( $key, 4 ); // 4 sec timeout
155 - if ( !$wgMemc->get( $key ) ) { // no flag set
156 - $wgMemc->set( $key, $val, 6*20 ); // 6 min
 151+ $oldVal = $wgMemc->get( $key );
 152+ if ( count( $oldVal ) == 3 ) { // flag set
 153+ list( $u, $ts, $cnt ) = $oldVal;
 154+ if ( $u === $user->getName() ) { // by this user
 155+ $newVal = array( $u, $ts, $cnt+1 ); // inc counter
 156+ $wgMemc->set( $key, $newVal, $ttlMs );
 157+ $wasSet = true;
 158+ }
 159+ } else { // no flag set
 160+ $newVal = array( $user->getName(), wfTimestampNow(), 1 );
 161+ $wgMemc->set( $key, $newVal, $ttlMs );
157162 $wasSet = true;
158163 }
159164 $wgMemc->unlock( $key );
@@ -160,29 +165,26 @@
161166 return $wasSet;
162167 }
163168
164 - /*
165 - * Clear the flag for who is reviewing a diff
166 - * @param User $user
167 - * @param int $oldId
168 - * @param int $newId
169 - * @return bool flag unset
170 - */
171 - public static function clearUserReviewingDiff( $user, $oldId, $newId ) {
 169+ protected static function decUserReviewingItem( $key, User $user, $ttlMs ) {
172170 global $wgMemc;
173 - $key = wfMemcKey( 'flaggedrevs', 'userReviewingDiff', $oldId, $newId );
 171+ $wasSet = false; // was changed?
 172+
174173 $wgMemc->lock( $key, 4 ); // 4 sec timeout
175 - $val = $wgMemc->get( $key );
176 - $wasSet = false;
177 -
178 - if ( is_array( $val ) && count( $val ) == 2 ) { // flag set
179 - list( $u, $ts ) = $val;
 174+ $oldVal = $wgMemc->get( $key );
 175+ if ( count( $oldVal ) == 3 ) { // flag set
 176+ list( $u, $ts, $cnt ) = $oldVal;
180177 if ( $u === $user->getName() ) {
181 - $wgMemc->delete( $key );
 178+ if ( $cnt <= 1 ) {
 179+ $wgMemc->delete( $key );
 180+ } else {
 181+ $newVal = array( $u, $ts, $cnt-1 ); // dec counter
 182+ $wgMemc->set( $key, $newVal );
 183+ }
182184 $wasSet = true;
183185 }
184186 }
185187 $wgMemc->unlock( $key );
186 -
 188+
187189 return $wasSet;
188190 }
189191 }
Index: trunk/extensions/FlaggedRevs/presentation/modules/review.js
@@ -356,6 +356,4 @@
357357 FlaggedRevsReview.enableAjaxReview();
358358
359359 // Flag users as "no longer reviewing" on navigate-away
360 -// @TODO: This doesn't handle a user having the same diff open twice,
361 -// closing one, but reviewing in the other very well...
362360 window.onbeforeunload = FlaggedRevsReview.deadvertiseReviewing;

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r90033*Added API to set "currently reviewing" flag for pages and diffs...aaron03:41, 14 June 2011

Status & tagging log