Index: trunk/extensions/FlaggedRevs/dataclasses/FRUserActivity.php |
— | — | @@ -3,6 +3,9 @@ |
4 | 4 | * Class of utility functions for getting/tracking user activity |
5 | 5 | */ |
6 | 6 | class FRUserActivity { |
| 7 | + const PAGE_REVIEW_MS = 1200; // 20*60 |
| 8 | + const CHANGE_REVIEW_MS = 360; // 6*60 |
| 9 | + |
7 | 10 | /** |
8 | 11 | * Get number of active users watching a page |
9 | 12 | * @param Title $title |
— | — | @@ -48,14 +51,13 @@ |
49 | 52 | global $wgMemc; |
50 | 53 | $key = wfMemcKey( 'flaggedrevs', 'userReviewingPage', $pageId ); |
51 | 54 | $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 ); |
56 | 58 | } |
57 | 59 | |
58 | 60 | /* |
59 | | - * Check is someone is currently reviewing a page |
| 61 | + * Check if someone is currently reviewing a page |
60 | 62 | * @param int $pageId |
61 | 63 | * @return bool |
62 | 64 | */ |
— | — | @@ -65,50 +67,28 @@ |
66 | 68 | } |
67 | 69 | |
68 | 70 | /* |
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 | + * |
70 | 75 | * @param User $user |
71 | 76 | * @param int $pageId |
72 | 77 | * @return bool flag set |
73 | 78 | */ |
74 | | - public static function setUserReviewingPage( $user, $pageId ) { |
75 | | - global $wgMemc; |
| 79 | + public static function setUserReviewingPage( User $user, $pageId ) { |
76 | 80 | $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 ); |
88 | 82 | } |
89 | 83 | |
90 | 84 | /* |
91 | | - * Clear the flag for who is reviewing a page |
| 85 | + * Decrement/clear the flag for who is reviewing a page |
92 | 86 | * @param User $user |
93 | 87 | * @param int $pageId |
94 | 88 | * @return bool flag unset |
95 | 89 | */ |
96 | | - public static function clearUserReviewingPage( $user, $pageId ) { |
97 | | - global $wgMemc; |
| 90 | + public static function clearUserReviewingPage( User $user, $pageId ) { |
98 | 91 | $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 ); |
113 | 93 | } |
114 | 94 | |
115 | 95 | /* |
— | — | @@ -121,14 +101,13 @@ |
122 | 102 | global $wgMemc; |
123 | 103 | $key = wfMemcKey( 'flaggedrevs', 'userReviewingDiff', $oldId, $newId ); |
124 | 104 | $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 ); |
129 | 108 | } |
130 | 109 | |
131 | 110 | /* |
132 | | - * Check is someone is currently reviewing a diff |
| 111 | + * Check if someone is currently reviewing a diff |
133 | 112 | * @param int $oldId |
134 | 113 | * @param int $newId |
135 | 114 | * @return bool |
— | — | @@ -139,20 +118,46 @@ |
140 | 119 | } |
141 | 120 | |
142 | 121 | /* |
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. |
144 | 125 | * @param User $user |
145 | 126 | * @param int $pageId |
146 | 127 | * @return bool flag set |
147 | 128 | */ |
148 | | - public static function setUserReviewingDiff( $user, $oldId, $newId ) { |
149 | | - global $wgMemc; |
| 129 | + public static function setUserReviewingDiff( User $user, $oldId, $newId ) { |
150 | 130 | $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 | + } |
153 | 133 | |
| 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 | + |
154 | 150 | $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 ); |
157 | 162 | $wasSet = true; |
158 | 163 | } |
159 | 164 | $wgMemc->unlock( $key ); |
— | — | @@ -160,29 +165,26 @@ |
161 | 166 | return $wasSet; |
162 | 167 | } |
163 | 168 | |
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 ) { |
172 | 170 | global $wgMemc; |
173 | | - $key = wfMemcKey( 'flaggedrevs', 'userReviewingDiff', $oldId, $newId ); |
| 171 | + $wasSet = false; // was changed? |
| 172 | + |
174 | 173 | $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; |
180 | 177 | 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 | + } |
182 | 184 | $wasSet = true; |
183 | 185 | } |
184 | 186 | } |
185 | 187 | $wgMemc->unlock( $key ); |
186 | | - |
| 188 | + |
187 | 189 | return $wasSet; |
188 | 190 | } |
189 | 191 | } |
Index: trunk/extensions/FlaggedRevs/presentation/modules/review.js |
— | — | @@ -356,6 +356,4 @@ |
357 | 357 | FlaggedRevsReview.enableAjaxReview(); |
358 | 358 | |
359 | 359 | // 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... |
362 | 360 | window.onbeforeunload = FlaggedRevsReview.deadvertiseReviewing; |