r91664 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91663‎ | r91664 | r91665 >
Date:18:30, 7 July 2011
Author:catrope
Status:resolved (Comments)
Tags:
Comment:
ClickTracking: Add support for logging events to files or UDP through $wgClickTrackingLog. Also allow disabling database logging through $wgClickTrackingDatabase.

Smaller tweaks:
* Change $eventId parameter of ClickTrackingHooks::trackEvent() to $eventName, and move the ID->name lookup into the if($wgClickTrackingDatabase) branch. This ensures the clicktracking DB tables are not written to if DB logging is disabled. This function is public, but has no callers outside this extension
* Change the type of the 'namespacenumber' parameter in the API module to 'integer'. It was previously set to 'namespace', which barfs on negative values, which caused nasty errors when an event happened to originate on a special page
Modified paths:
  • /trunk/extensions/ClickTracking/ApiClickTracking.php (modified) (history)
  • /trunk/extensions/ClickTracking/ClickTracking.hooks.php (modified) (history)
  • /trunk/extensions/ClickTracking/ClickTracking.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ClickTracking/ClickTracking.hooks.php
@@ -169,7 +169,7 @@
170170 * @param $sessionId String: unique session id for this editing sesion
171171 * @param $isLoggedIn Boolean: whether or not the user is logged in
172172 * @param $namespace Integer: namespace the user is editing
173 - * @param $eventId Integer: event type
 173+ * @param $eventName String: event type
174174 * @param $contribs Integer: contributions the user has made (or NULL if user not logged in)
175175 * @param $contribs_in_timespan1 Integer: number of contributions user has made in timespan of granularity 1
176176 * (defined by ClickTracking/$wgClickTrackContribGranularity1)
@@ -181,49 +181,74 @@
182182 * @param $relevantBucket String: name/index of the particular bucket we're concerned with for this event
183183 * @return Boolean: true if the event was stored in the DB
184184 */
185 - public static function trackEvent( $sessionId, $isLoggedIn, $namespace, $eventId, $contribs = 0,
 185+ public static function trackEvent( $sessionId, $isLoggedIn, $namespace, $eventName, $contribs = 0,
186186 $contribs_in_timespan1 = 0, $contribs_in_timespan2 = 0, $contribs_in_timespan3 = 0, $additional = null, $recordBucketInfo = true ) {
187 -
188 - $dbw = wfGetDB( DB_MASTER );
189 - $dbw->begin();
190 - // Builds insert information
191 - $data = array(
192 - 'action_time' => $dbw->timestamp(),
193 - 'session_id' => (string) $sessionId,
194 - 'is_logged_in' => (bool) $isLoggedIn,
195 - 'user_total_contribs' => ( $isLoggedIn ? (int) $contribs : null ),
196 - 'user_contribs_span1' => ( $isLoggedIn ? (int) $contribs_in_timespan1 : null ),
197 - 'user_contribs_span2' => ( $isLoggedIn ? (int) $contribs_in_timespan2 : null ),
198 - 'user_contribs_span3' => ( $isLoggedIn ? (int) $contribs_in_timespan3 : null ),
199 - 'namespace' => (int) $namespace,
200 - 'event_id' => (int) $eventId,
201 - 'additional_info' => ( isset( $additional ) ? (string) $additional : null )
202 - );
203 - $db_status_buckets = true;
204 - $db_status = $dbw->insert( 'click_tracking', $data, __METHOD__ );
205 - $dbw->commit();
 187+
 188+ global $wgClickTrackingDatabase, $wgClickTrackingLog;
 189+ $retval = true;
 190+ if ( $wgClickTrackingDatabase ) {
 191+ $eventId = self::getEventIDFromName( $eventName );
 192+ $dbw = wfGetDB( DB_MASTER );
 193+ $dbw->begin();
 194+ // Builds insert information
 195+ $data = array(
 196+ 'action_time' => $dbw->timestamp(),
 197+ 'session_id' => (string) $sessionId,
 198+ 'is_logged_in' => (bool) $isLoggedIn,
 199+ 'user_total_contribs' => ( $isLoggedIn ? (int) $contribs : null ),
 200+ 'user_contribs_span1' => ( $isLoggedIn ? (int) $contribs_in_timespan1 : null ),
 201+ 'user_contribs_span2' => ( $isLoggedIn ? (int) $contribs_in_timespan2 : null ),
 202+ 'user_contribs_span3' => ( $isLoggedIn ? (int) $contribs_in_timespan3 : null ),
 203+ 'namespace' => (int) $namespace,
 204+ 'event_id' => (int) $eventId,
 205+ 'additional_info' => ( isset( $additional ) ? (string) $additional : null )
 206+ );
 207+ $db_status_buckets = true;
 208+ $db_status = $dbw->insert( 'click_tracking', $data, __METHOD__ );
 209+ $dbw->commit();
206210
207 - if( $recordBucketInfo && $db_status ){
208 - $buckets = self::unpackBucketInfo();
209 - if( $buckets ){
210 - foreach( $buckets as $bucketName => $bucketValue ){
211 - $db_current_bucket_insert = $dbw->insert( 'click_tracking_user_properties',
212 - array(
213 - 'session_id' => (string) $sessionId,
214 - 'property_name' => (string) $bucketName,
215 - 'property_value' => (string) $bucketValue[0],
216 - 'property_version' => (int) $bucketValue[1]
217 - ),
218 - __METHOD__,
219 - array( 'IGNORE' )
220 - );
221 - $db_status_buckets = $db_status_buckets && $db_current_bucket_insert;
222 - }
223 - }//ifbuckets
224 - }//ifrecord
225 -
226 - $dbw->commit();
227 - return ($db_status && $db_status_buckets);
 211+ if( $recordBucketInfo && $db_status ){
 212+ $buckets = self::unpackBucketInfo();
 213+ if( $buckets ){
 214+ foreach( $buckets as $bucketName => $bucketValue ){
 215+ $db_current_bucket_insert = $dbw->insert( 'click_tracking_user_properties',
 216+ array(
 217+ 'session_id' => (string) $sessionId,
 218+ 'property_name' => (string) $bucketName,
 219+ 'property_value' => (string) $bucketValue[0],
 220+ 'property_version' => (int) $bucketValue[1]
 221+ ),
 222+ __METHOD__,
 223+ array( 'IGNORE' )
 224+ );
 225+ $db_status_buckets = $db_status_buckets && $db_current_bucket_insert;
 226+ }
 227+ }//ifbuckets
 228+ }//ifrecord
 229+
 230+ $dbw->commit();
 231+ $retval = $db_status && $db_status_buckets;
 232+ }
 233+ if ( $wgClickTrackingLog ) {
 234+ $msg = implode( "\t", array(
 235+ // Replace tabs with spaces in all strings
 236+ str_replace( "\t", ' ', $eventName ),
 237+ wfTimestampNow(),
 238+ (bool)$isLoggedIn,
 239+ str_replace( "\t", ' ', $sessionId ),
 240+ (int)$namespace,
 241+ (int)$contribs,
 242+ (int)$contribs_in_timespan1,
 243+ (int)$contribs_in_timespan2,
 244+ (int)$contribs_in_timespan3,
 245+ str_replace( "\t", ' ', $additional ),
 246+ ) );
 247+ wfErrorLog( $msg, $wgClickTrackingLog );
 248+
 249+ // No need to mess with $retval here, doing
 250+ // $retval == $retval && true is useless
 251+ }
 252+ return $retval;
228253 }
229254
230255 public static function editPageShowEditFormFields( $editPage, $output ) {
Index: trunk/extensions/ClickTracking/ApiClickTracking.php
@@ -32,9 +32,8 @@
3333 $additional = $params['additional'];
3434 }
3535
36 - // Event ID lookup table
3736 // FIXME: API should already have urldecode()d
38 - $eventId = ClickTrackingHooks::getEventIDFromName( urldecode( $eventid_to_lookup ) );
 37+ $eventName = urldecode( $eventid_to_lookup );
3938
4039 $isLoggedIn = $wgUser->isLoggedIn();
4140 $now = time();
@@ -51,7 +50,7 @@
5251 $sessionId, // randomly generated session ID
5352 $isLoggedIn, // is the user logged in?
5453 (int)$namespace, // what namespace are they editing?
55 - $eventId, // event ID passed in
 54+ $eventName, // event ID passed in
5655 ( $isLoggedIn ? $wgUser->getEditCount() : 0 ), // total edit count or 0 if anonymous
5756 $granularity1, // contributions made in granularity 1 time frame
5857 $granularity2, // contributions made in granularity 2 time frame
@@ -103,7 +102,7 @@
104103 ApiBase::PARAM_REQUIRED => true,
105104 ),
106105 'namespacenumber' => array(
107 - ApiBase::PARAM_TYPE => 'namespace',
 106+ ApiBase::PARAM_TYPE => 'integer', // not 'namespace', we need to allow negative numbers
108107 ApiBase::PARAM_REQUIRED => true,
109108 ),
110109 'token' => array(
Index: trunk/extensions/ClickTracking/ClickTracking.php
@@ -26,6 +26,11 @@
2727 $wgClickTrackContribGranularity2 = 60 * 60 * 24 * 365 / 4; // 3 months
2828 $wgClickTrackContribGranularity3 = 60 * 60 * 24 * 30; // 1 month
2929
 30+// If not false, log events to this file. May also be a UDP socket, denoted as udp://host:port/prefix
 31+$wgClickTrackingLog = false;
 32+// Whether to log clicks to the database. If this is enabled and a log file is configured, events will be logged to both
 33+$wgClickTrackingDatabase = true;
 34+
3035 /* Setup */
3136
3237 $wgExtensionCredits['other'][] = array(

Follow-up revisions

RevisionCommit summaryAuthorDate
r91682Add seemingly missing transaction $db-begin() noticed in r91664reedy20:50, 7 July 2011
r918991.17wmf1: MFT r91664 (ClickTracking UDP), adapted a bit to applycatrope18:47, 11 July 2011

Comments

#Comment by Reedy (talk | contribs)   20:32, 7 July 2011

Don't you need another $dbw->begin() before you start doing the second set of queries? As you've already called commit..

#Comment by Catrope (talk | contribs)   20:33, 7 July 2011

It probably does need that, yes, but that code wasn't introduced in this rev, only indented and wrapped in an if statement.

Status & tagging log