Index: trunk/extensions/OnlineStatusBar/OnlineStatusBar.status.php |
— | — | @@ -0,0 +1,135 @@ |
| 2 | +<? |
| 3 | +if ( !defined( 'MEDIAWIKI' ) ) { |
| 4 | + echo "This is a part of mediawiki and can't be started separately"; |
| 5 | + die(); |
| 6 | +} |
| 7 | + |
| 8 | +/** |
| 9 | + * File which contains status check for Online status bar extension. |
| 10 | + * |
| 11 | + * @file |
| 12 | + * @ingroup Extensions |
| 13 | + * @author Petr Bena <benapetr@gmail.com> |
| 14 | + * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later |
| 15 | + * @link http://www.mediawiki.org/wiki/Extension:OnlineStatusBar Documentation |
| 16 | + */ |
| 17 | + |
| 18 | +class OnlineStatusBar_StatusCheck { |
| 19 | + |
| 20 | + /** |
| 21 | + * @param $user User |
| 22 | + * @return String |
| 23 | + */ |
| 24 | + public static function getStatus( $user, $delayed_check = false ) { |
| 25 | + global $wgOnlineStatusBarDefaultOffline, $wgOnlineStatusBarDefaultOnline; |
| 26 | + |
| 27 | + // instead of delete every time just select the records which are not that old |
| 28 | + $t_time = OnlineStatusBar::getTimeoutDate(); |
| 29 | + $dbr = wfGetDB( DB_SLAVE ); |
| 30 | + $w_time = OnlineStatusBar::getTimeoutDate( true ); |
| 31 | + $result = $dbr->selectField( 'online_status', 'username', array( 'username' => $user->getName(), |
| 32 | + "timestamp > " . $dbr->addQuotes( $dbr->timestamp( $t_time ) ) ), |
| 33 | + __METHOD__, array( 'LIMIT 1', 'ORDER BY timestamp DESC' ) ); |
| 34 | + |
| 35 | + |
| 36 | + if ( $result === false ) { |
| 37 | + $status = $wgOnlineStatusBarDefaultOffline; |
| 38 | + } else { |
| 39 | + // let's check if it isn't anon |
| 40 | + if ( $user->isLoggedIn() ) { |
| 41 | + $status = $user->getOption( 'OnlineStatusBar_status', $wgOnlineStatusBarDefaultOnline ); |
| 42 | + if ( $delayed_check ) { |
| 43 | + if ( $result[0]->timestamp > w_time ) { |
| 44 | + $status = 'write'; |
| 45 | + } |
| 46 | + } |
| 47 | + } else { |
| 48 | + $status = $wgOnlineStatusBarDefaultOnline; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if ( $status == 'hidden' && !($delayed_check) ) { |
| 53 | + $status = $wgOnlineStatusBarDefaultOffline; |
| 54 | + } |
| 55 | + |
| 56 | + return $status; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Insert to the database |
| 61 | + * @return bool |
| 62 | + */ |
| 63 | + public static function updateDb() { |
| 64 | + global $wgUser; |
| 65 | + // Skip users we don't track |
| 66 | + if ( OnlineStatusBar::isValid ( $wgUser ) != true ) { |
| 67 | + return false; |
| 68 | + } |
| 69 | + // If we track them, let's insert it to the table |
| 70 | + $dbw = wfGetDB( DB_MASTER ); |
| 71 | + $row = array( |
| 72 | + 'username' => $wgUser->getName(), |
| 73 | + 'timestamp' => $dbw->timestamp(), |
| 74 | + ); |
| 75 | + $dbw->insert( 'online_status', $row, __METHOD__, 'DELAYED' ); |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Delete user who logged out |
| 81 | + * @param $userName string |
| 82 | + * @return bool |
| 83 | + */ |
| 84 | + static function deleteStatus( $userName ) { |
| 85 | + $dbw = wfGetDB( DB_MASTER ); |
| 86 | + $dbw->delete( 'online_status', array( 'username' => $userName ), __METHOD__ ); // delete user |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Update status of user |
| 92 | + * @return bool |
| 93 | + */ |
| 94 | + public static function updateStatus() { |
| 95 | + global $wgUser, $wgOnlineStatusBarDefaultOffline, $wgOnlineStatusBarTrackIpUsers, $wgOnlineStatusBarDefaultEnabled; |
| 96 | + // if anon users are not tracked and user is anon leave it |
| 97 | + if ( !$wgOnlineStatusBarTrackIpUsers ) { |
| 98 | + if ( !$wgUser->isLoggedIn() ) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + } |
| 102 | + // if user doesn't want to be tracked leave it as well for privacy reasons |
| 103 | + if ( $wgUser->isLoggedIn() && !$wgUser->getOption ( "OnlineStatusBar_active", $wgOnlineStatusBarDefaultEnabled ) ) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + $user_status = self::getStatus( $wgUser, true ); |
| 107 | + if ( $user_status == $wgOnlineStatusBarDefaultOffline ) { |
| 108 | + self::updateDb(); |
| 109 | + return true; |
| 110 | + } |
| 111 | + |
| 112 | + if ( $user_status == 'write' ) { |
| 113 | + $dbw = wfGetDB( DB_MASTER ); |
| 114 | + $dbw->update( |
| 115 | + 'online_status', |
| 116 | + array( 'timestamp' => $dbw->timestamp() ), |
| 117 | + array( 'username' => $wgUser->getName() ), |
| 118 | + __METHOD__ |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + return true; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Delete old records from the table, this function is called frequently too keep it as small as possible |
| 127 | + * @return int |
| 128 | + */ |
| 129 | + public static function deleteOld() { |
| 130 | + $dbw = wfGetDB( DB_MASTER ); |
| 131 | + // calculate time and convert it back to mediawiki format |
| 132 | + $time = OnlineStatusBar::getTimeoutDate(); |
| 133 | + $dbw->delete( 'online_status', array( "timestamp < " . $dbw->addQuotes( $dbw->timestamp( $time ) ) ), __METHOD__ ); |
| 134 | + return 0; |
| 135 | + } |
| 136 | +} |
Index: trunk/extensions/OnlineStatusBar/OnlineStatusBar.body.php |
— | — | @@ -65,7 +65,7 @@ |
66 | 66 | return false; |
67 | 67 | } |
68 | 68 | |
69 | | - $status = self::getStatus( $user, true ); |
| 69 | + $status = OnlineStatusBar_StatusCheck::getStatus( $user ); |
70 | 70 | |
71 | 71 | return array( $status, $user ); |
72 | 72 | } |
— | — | @@ -91,49 +91,12 @@ |
92 | 92 | return false; |
93 | 93 | } |
94 | 94 | |
95 | | - $status = self::getStatus( $user, true ); |
| 95 | + $status = OnlineStatusBar_StatusCheck::getStatus( $user ); |
96 | 96 | |
97 | 97 | return array( $status, $user ); |
98 | 98 | } |
99 | 99 | |
100 | 100 | /** |
101 | | - * @param $user User |
102 | | - * @return String |
103 | | - */ |
104 | | - public static function getStatus( $user, $update = false ) { |
105 | | - global $wgOnlineStatusBarDefaultOffline, $wgOnlineStatusBarDefaultOnline; |
106 | | - // remove old entries |
107 | | - if ( $update ) |
108 | | - { |
109 | | - self::deleteOld(); |
110 | | - } |
111 | | - |
112 | | - // instead of delete every time just select the records which are not that old |
113 | | - $t_time = self::getTimeoutDate(); |
114 | | - $dbr = wfGetDB( DB_SLAVE ); |
115 | | - $result = $dbr->selectField( 'online_status', 'username', array( 'username' => $user->getName(), |
116 | | - "timestamp > " . $dbr->addQuotes( $dbr->timestamp( $t_time ) ) ), |
117 | | - __METHOD__, array( 'LIMIT 1', 'ORDER BY timestamp DESC' ) ); |
118 | | - |
119 | | - if ( $result === false ) { |
120 | | - $status = $wgOnlineStatusBarDefaultOffline; |
121 | | - } else { |
122 | | - // let's check if it isn't anon |
123 | | - if ( $user->isLoggedIn() ) { |
124 | | - $status = $user->getOption( 'OnlineStatusBar_status', $wgOnlineStatusBarDefaultOnline ); |
125 | | - } else { |
126 | | - $status = $wgOnlineStatusBarDefaultOnline; |
127 | | - } |
128 | | - } |
129 | | - |
130 | | - if ( $status == 'hidden' ) { |
131 | | - $status = $wgOnlineStatusBarDefaultOffline; |
132 | | - } |
133 | | - |
134 | | - return $status; |
135 | | - } |
136 | | - |
137 | | - /** |
138 | 101 | * Purge page |
139 | 102 | * @return bool |
140 | 103 | * |
— | — | @@ -160,80 +123,21 @@ |
161 | 124 | return true; |
162 | 125 | } |
163 | 126 | |
164 | | - /** |
165 | | - * Insert to the database |
166 | | - * @return bool |
167 | | - */ |
168 | | - public static function updateDb() { |
169 | | - global $wgUser; |
170 | | - // Skip users we don't track |
171 | | - if ( self::isValid ( $wgUser ) != true ) { |
172 | | - return false; |
173 | | - } |
174 | | - // If we track them, let's insert it to the table |
175 | | - $dbw = wfGetDB( DB_MASTER ); |
176 | | - $row = array( |
177 | | - 'username' => $wgUser->getName(), |
178 | | - 'timestamp' => $dbw->timestamp(), |
179 | | - ); |
180 | | - $dbw->insert( 'online_status', $row, __METHOD__, 'DELAYED' ); |
181 | | - return false; |
182 | | - } |
183 | 127 | |
184 | 128 | /** |
185 | | - * Update status of user |
186 | | - * @return bool |
187 | | - */ |
188 | | - public static function updateStatus() { |
189 | | - global $wgUser, $wgOnlineStatusBarDefaultOffline, $wgOnlineStatusBarTrackIpUsers, $wgOnlineStatusBarDefaultEnabled; |
190 | | - // if anon users are not tracked and user is anon leave it |
191 | | - if ( !$wgOnlineStatusBarTrackIpUsers ) { |
192 | | - if ( !$wgUser->isLoggedIn() ) { |
193 | | - return false; |
194 | | - } |
195 | | - } |
196 | | - // if user doesn't want to be tracked leave it as well for privacy reasons |
197 | | - if ( $wgUser->isLoggedIn() && !$wgUser->getOption ( "OnlineStatusBar_active", $wgOnlineStatusBarDefaultEnabled ) ) { |
198 | | - return false; |
199 | | - } |
200 | | - if ( OnlineStatusBar::getStatus( $wgUser ) == $wgOnlineStatusBarDefaultOffline ) { |
201 | | - OnlineStatusBar::updateDb(); |
202 | | - return true; |
203 | | - } |
204 | | - |
205 | | - $dbw = wfGetDB( DB_MASTER ); |
206 | | - $dbw->update( |
207 | | - 'online_status', |
208 | | - array( 'timestamp' => $dbw->timestamp() ), |
209 | | - array( 'username' => $wgUser->getName() ), |
210 | | - __METHOD__ |
211 | | - ); |
212 | | - |
213 | | - return true; |
214 | | - } |
215 | | - |
216 | | - /** |
217 | 129 | * @return timestamp |
218 | 130 | */ |
219 | | - private static function getTimeoutDate() { |
220 | | - global $wgOnlineStatusBar_LogoutTime; |
| 131 | + public static function getTimeoutDate( $delayed = false ) { |
| 132 | + global $wgOnlineStatusBar_WriteTime, $wgOnlineStatusBar_LogoutTime; |
| 133 | + |
| 134 | + if ($delayed) { |
| 135 | + return wfTimestamp( TS_UNIX ) - $wgOnlineStatusBar_WriteTime; |
| 136 | + } |
| 137 | + |
221 | 138 | return wfTimestamp( TS_UNIX ) - $wgOnlineStatusBar_LogoutTime; |
222 | 139 | } |
223 | 140 | |
224 | 141 | /** |
225 | | - * Delete old records from the table, this function is called frequently too keep it as small as possible |
226 | | - * @return int |
227 | | - */ |
228 | | - public static function deleteOld() { |
229 | | - $dbw = wfGetDB( DB_MASTER ); |
230 | | - // calculate time and convert it back to mediawiki format |
231 | | - $time = self::getTimeoutDate(); |
232 | | - $dbw->delete( 'online_status', array( "timestamp < " . $dbw->addQuotes( $dbw->timestamp( $time ) ) ), __METHOD__ ); |
233 | | - return 0; |
234 | | - } |
235 | | - |
236 | | - |
237 | | - /** |
238 | 142 | * Checks to see if the user can be tracked |
239 | 143 | * |
240 | 144 | * @param User $user |
— | — | @@ -250,15 +154,4 @@ |
251 | 155 | // do we track them |
252 | 156 | return $user->getOption( "OnlineStatusBar_active", $wgOnlineStatusBarDefaultEnabled ); |
253 | 157 | } |
254 | | - |
255 | | - /** |
256 | | - * Delete user who logged out |
257 | | - * @param $userName string |
258 | | - * @return bool |
259 | | - */ |
260 | | - static function deleteStatus( $userName ) { |
261 | | - $dbw = wfGetDB( DB_MASTER ); |
262 | | - $dbw->delete( 'online_status', array( 'username' => $userName ), __METHOD__ ); // delete user |
263 | | - return true; |
264 | | - } |
265 | 158 | } |
Index: trunk/extensions/OnlineStatusBar/OnlineStatusBar.php |
— | — | @@ -69,6 +69,7 @@ |
70 | 70 | ); |
71 | 71 | |
72 | 72 | $wgAutoloadClasses['OnlineStatusBar'] = "$dir/OnlineStatusBar.body.php"; |
| 73 | +$wgAutoloadClasses['OnlineStatusBar_StatusCheck'] = "$dir/OnlineStatusBar.status.php"; |
73 | 74 | $wgAutoloadClasses['OnlineStatusBarHooks'] = "$dir/OnlineStatusBarHooks.php"; |
74 | 75 | |
75 | 76 | // Configuration |
— | — | @@ -83,6 +84,8 @@ |
84 | 85 | |
85 | 86 | // default for anonymous and uknown users |
86 | 87 | $wgOnlineStatusBarTrackIpUsers = false; |
| 88 | +// delay between db updates |
| 89 | +$wgOnlineStatusBar_WriteTime = 300; |
87 | 90 | // default for online |
88 | 91 | $wgOnlineStatusBarDefaultOnline = "online"; |
89 | 92 | // default for offline |
Index: trunk/extensions/OnlineStatusBar/OnlineStatusBarHooks.php |
— | — | @@ -29,18 +29,19 @@ |
30 | 30 | */ |
31 | 31 | public static function logout( &$user, &$inject_html, $old_name ) { |
32 | 32 | OnlineStatusBar::purge( $old_name ); |
33 | | - OnlineStatusBar::deleteStatus( $old_name ); |
| 33 | + OnlineStatusBar_StatusCheck::deleteStatus( $old_name ); |
34 | 34 | return true; |
35 | 35 | } |
36 | 36 | |
37 | 37 | /** |
38 | | - * Called everytime on login |
| 38 | + * Called everytime on login |
39 | 39 | * @return bool |
40 | 40 | */ |
41 | 41 | public static function updateStatus() { |
42 | 42 | global $wgUser; |
| 43 | + OnlineStatusBar_StatusCheck::deleteOld(); |
43 | 44 | OnlineStatusBar::purge( $wgUser ); |
44 | | - OnlineStatusBar::updateStatus(); |
| 45 | + OnlineStatusBar_StatusCheck::updateStatus(); |
45 | 46 | return true; |
46 | 47 | } |
47 | 48 | |
— | — | @@ -54,7 +55,7 @@ |
55 | 56 | public static function renderBar( &$article, &$outputDone, &$pcache ) { |
56 | 57 | $context = $article->getContext(); |
57 | 58 | |
58 | | - OnlineStatusBar::updateStatus(); |
| 59 | + OnlineStatusBar_StatusCheck::updateStatus(); |
59 | 60 | $result = OnlineStatusBar::getUserInfoFromTitle( $article->getTitle() ); |
60 | 61 | if ( $result === false && User::isIP ( $article->getTitle()->getBaseText() ) ) { |
61 | 62 | $result = OnlineStatusBar::getAnonFromTitle( $article->getTitle() ); |