r103135 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103134‎ | r103135 | r103136 >
Date:09:35, 15 November 2011
Author:petrb
Status:deferred
Tags:
Comment:
Performance update
Modified paths:
  • /trunk/extensions/OnlineStatusBar/OnlineStatusBar.body.php (modified) (history)
  • /trunk/extensions/OnlineStatusBar/OnlineStatusBar.php (modified) (history)
  • /trunk/extensions/OnlineStatusBar/OnlineStatusBar.status.php (added) (history)
  • /trunk/extensions/OnlineStatusBar/OnlineStatusBarHooks.php (modified) (history)

Diff [purge]

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 @@
6666 return false;
6767 }
6868
69 - $status = self::getStatus( $user, true );
 69+ $status = OnlineStatusBar_StatusCheck::getStatus( $user );
7070
7171 return array( $status, $user );
7272 }
@@ -91,49 +91,12 @@
9292 return false;
9393 }
9494
95 - $status = self::getStatus( $user, true );
 95+ $status = OnlineStatusBar_StatusCheck::getStatus( $user );
9696
9797 return array( $status, $user );
9898 }
9999
100100 /**
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 - /**
138101 * Purge page
139102 * @return bool
140103 *
@@ -160,80 +123,21 @@
161124 return true;
162125 }
163126
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 - }
183127
184128 /**
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 - /**
217129 * @return timestamp
218130 */
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+
221138 return wfTimestamp( TS_UNIX ) - $wgOnlineStatusBar_LogoutTime;
222139 }
223140
224141 /**
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 - /**
238142 * Checks to see if the user can be tracked
239143 *
240144 * @param User $user
@@ -250,15 +154,4 @@
251155 // do we track them
252156 return $user->getOption( "OnlineStatusBar_active", $wgOnlineStatusBarDefaultEnabled );
253157 }
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 - }
265158 }
Index: trunk/extensions/OnlineStatusBar/OnlineStatusBar.php
@@ -69,6 +69,7 @@
7070 );
7171
7272 $wgAutoloadClasses['OnlineStatusBar'] = "$dir/OnlineStatusBar.body.php";
 73+$wgAutoloadClasses['OnlineStatusBar_StatusCheck'] = "$dir/OnlineStatusBar.status.php";
7374 $wgAutoloadClasses['OnlineStatusBarHooks'] = "$dir/OnlineStatusBarHooks.php";
7475
7576 // Configuration
@@ -83,6 +84,8 @@
8485
8586 // default for anonymous and uknown users
8687 $wgOnlineStatusBarTrackIpUsers = false;
 88+// delay between db updates
 89+$wgOnlineStatusBar_WriteTime = 300;
8790 // default for online
8891 $wgOnlineStatusBarDefaultOnline = "online";
8992 // default for offline
Index: trunk/extensions/OnlineStatusBar/OnlineStatusBarHooks.php
@@ -29,18 +29,19 @@
3030 */
3131 public static function logout( &$user, &$inject_html, $old_name ) {
3232 OnlineStatusBar::purge( $old_name );
33 - OnlineStatusBar::deleteStatus( $old_name );
 33+ OnlineStatusBar_StatusCheck::deleteStatus( $old_name );
3434 return true;
3535 }
3636
3737 /**
38 - * Called everytime on login
 38+ * Called everytime on login
3939 * @return bool
4040 */
4141 public static function updateStatus() {
4242 global $wgUser;
 43+ OnlineStatusBar_StatusCheck::deleteOld();
4344 OnlineStatusBar::purge( $wgUser );
44 - OnlineStatusBar::updateStatus();
 45+ OnlineStatusBar_StatusCheck::updateStatus();
4546 return true;
4647 }
4748
@@ -54,7 +55,7 @@
5556 public static function renderBar( &$article, &$outputDone, &$pcache ) {
5657 $context = $article->getContext();
5758
58 - OnlineStatusBar::updateStatus();
 59+ OnlineStatusBar_StatusCheck::updateStatus();
5960 $result = OnlineStatusBar::getUserInfoFromTitle( $article->getTitle() );
6061 if ( $result === false && User::isIP ( $article->getTitle()->getBaseText() ) ) {
6162 $result = OnlineStatusBar::getAnonFromTitle( $article->getTitle() );

Follow-up revisions

RevisionCommit summaryAuthorDate
r103139fixed sql, it was selecting wrong field, and compared it wrongpetrb10:10, 15 November 2011

Status & tagging log