r55430 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r55429‎ | r55430 | r55431 >
Date:20:28, 21 August 2009
Author:ashley
Status:deferred
Tags:
Comment:
follow-up to r55425:
*convert spaces to tabs
*tweak doxygen comments
*coding style tweaks
Modified paths:
  • /trunk/extensions/UsabilityInitiative/ClickTracking/ApiClickTracking.php (modified) (history)
  • /trunk/extensions/UsabilityInitiative/ClickTracking/ClickTracking.hooks.php (modified) (history)
  • /trunk/extensions/UsabilityInitiative/ClickTracking/ClickTracking.js (modified) (history)
  • /trunk/extensions/UsabilityInitiative/ClickTracking/ClickTracking.php (modified) (history)
  • /trunk/extensions/UsabilityInitiative/ClickTracking/ClickTracking.sql (modified) (history)
  • /trunk/extensions/UsabilityInitiative/ClickTracking/ClickTrackingEvents.sql (modified) (history)
  • /trunk/extensions/UsabilityInitiative/ClickTracking/UserDailyContribs.sql (modified) (history)

Diff [purge]

Index: trunk/extensions/UsabilityInitiative/ClickTracking/ClickTracking.hooks.php
@@ -10,7 +10,7 @@
1111 class ClickTrackingHooks {
1212
1313 /* Static Functions */
14 -
 14+
1515 /* initializations */
1616
1717 /* 3 tables for click tracking */
@@ -26,7 +26,7 @@
2727 'user_daily_contribs',
2828 dirname( __FILE__ ) . '/UserDailyContribs.sql'
2929 );
30 -
 30+
3131 $wgExtNewTables[] = array(
3232 'click_tracking_events',
3333 dirname( __FILE__ ) . '/ClickTrackingEvents.sql'
@@ -35,146 +35,144 @@
3636 return true;
3737 }
3838
39 -
4039 /**
41 - * adds javascript
 40+ * Adds JavaScript
4241 */
4342 public static function addJS(){
4443 UsabilityInitiativeHooks::initialize();
45 - UsabilityInitiativeHooks::addScript('ClickTracking/ClickTracking.js');
 44+ UsabilityInitiativeHooks::addScript( 'ClickTracking/ClickTracking.js' );
4645 UsabilityInitiativeHooks::addVariables(
4746 array( 'wgTrackingToken' => ClickTrackingHooks::get_session_id() )
4847 );
4948 return true;
5049 }
51 -
52 -
 50+
5351 /**
5452 * Gets the session ID...we just want a unique random ID for the page load
5553 * @return session ID
5654 */
5755 public static function get_session_id(){
58 - global $wgUser;
59 - return wfGenerateToken(array($wgUser->getName(), time()));
 56+ global $wgUser;
 57+ return wfGenerateToken( array( $wgUser->getName(), time() ) );
6058 }
61 -
62 - /**
 59+
 60+ /**
6361 * Get the number of revisions a user has made since a given time
6462 * @param $ts beginning timestamp
6563 * @return number of revsions this user has made
6664 */
67 - public static function getEditCountSince($ts){
 65+ public static function getEditCountSince( $ts ){
6866 global $wgUser;
69 -
70 - //convert to just the day
71 - $time = gmdate('Ymd', wfTimestamp(TS_UNIX,$ts));
72 -
 67+
 68+ // convert to just the day
 69+ $time = gmdate( 'Ymd', wfTimestamp( TS_UNIX, $ts ) );
 70+
7371 $dbr = wfGetDB( DB_SLAVE );
74 -
75 - $edits = $dbr->selectField(
76 - 'user_daily_contribs',
77 - 'SUM(contribs)',
78 - array(
79 - 'user_id' => $wgUser->getId(),
80 - "day < '$time'"
81 - ),
82 - __METHOD__
83 - );
84 -
85 - //user hasn't made any edits in whatever amount of time
86 - if( $edits == null ){
87 - $edits = 0;
88 - }
89 -
90 - return $edits;
91 -
 72+
 73+ $edits = $dbr->selectField(
 74+ 'user_daily_contribs',
 75+ 'SUM(contribs)',
 76+ array(
 77+ 'user_id' => $wgUser->getId(),
 78+ "day < '$time'"
 79+ ),
 80+ __METHOD__
 81+ );
 82+
 83+ // user hasn't made any edits in whatever amount of time
 84+ if( $edits == null ){
 85+ $edits = 0;
 86+ }
 87+
 88+ return $edits;
9289 }
93 -
 90+
9491 /**
9592 * Stores a new contribution
96 - * @return unknown_type
 93+ * @return true
9794 */
9895 public static function storeNewContrib(){
9996 global $wgUser;
100 - $today = gmdate('Ymd',time());
 97+ $today = gmdate( 'Ymd', time() );
10198 $dbw = wfGetDB( DB_MASTER );
10299 $sql =
103100 "INSERT INTO user_daily_contribs (user_id,day,contribs) VALUES ({$wgUser->getId()},$today,1) ON DUPLICATE KEY UPDATE contribs=contribs+1;";
104101 $dbw->query($sql, __METHOD__);
105102 return true;
106103 }
107 -
 104+
108105 /**
109 - * get event ID from name
110 - * @param $event_name
111 - * @return $event ID
 106+ * Get event ID from name
 107+ * @param $event_name String: name of the event to get
 108+ * @return integer
112109 */
113 - public static function getEventIDFromName($event_name){
 110+ public static function getEventIDFromName( $event_name ){
114111 $dbr = wfGetDB( DB_SLAVE );
115 -
116 - $id_num = $dbr->selectField(
117 - 'click_tracking_events',
118 - 'id',
119 - array(
120 - 'event_name' => $event_name
121 - ),
122 - __METHOD__
123 - );
124 -
125 - // if this entry doesn't exist...
126 - // this will be incredibly rare as the whole database will only have a few hundred entries in it at most
127 - // and getting DB_MASTER up top would be wasteful
128 -
129 - if($id_num === false){
130 - $dbw = wfGetDB( DB_MASTER );
131 - $dbw->insert('click_tracking_events',
132 - array('event_name' => (string) $event_name),
133 - __METHOD__);
134 -
135 - //should be inserted now...if not, we return zero later
136112
137 - $id_num = $dbr->selectField(
138 - 'click_tracking_events',
139 - 'id',
140 - array(
141 - 'event_name' => $event_name
142 - ),
143 - __METHOD__
144 - );
145 - }
146 -
147 - if($id_num === false){ return 0;}
148 - return $id_num;
 113+ $id_num = $dbr->selectField(
 114+ 'click_tracking_events',
 115+ 'id',
 116+ array(
 117+ 'event_name' => $event_name
 118+ ),
 119+ __METHOD__
 120+ );
 121+
 122+ // if this entry doesn't exist...
 123+ // this will be incredibly rare as the whole database will only have a few hundred entries in it at most
 124+ // and getting DB_MASTER up top would be wasteful
 125+ if( $id_num === false ){
 126+ $dbw = wfGetDB( DB_MASTER );
 127+ $dbw->insert(
 128+ 'click_tracking_events',
 129+ array( 'event_name' => (string) $event_name ),
 130+ __METHOD__
 131+ );
 132+
 133+ // should be inserted now...if not, we return zero later
 134+ $id_num = $dbr->selectField(
 135+ 'click_tracking_events',
 136+ 'id',
 137+ array(
 138+ 'event_name' => $event_name
 139+ ),
 140+ __METHOD__
 141+ );
 142+ }
 143+
 144+ if( $id_num === false ){
 145+ return 0;
 146+ }
 147+
 148+ return $id_num;
149149 }
150 -
 150+
151151 /**
152152 * Track particular event
153 - * @param $session_id unique session id for this editing sesion
154 - * @param $is_logged_in whether or not the user is logged in
155 - * @param $namespace namespace the user is editing
156 - * @param $event_id event type
157 - * @param $contribs contributions the user has made (or NULL if user not logged in)
158 - * @param $contribs_in_timespan number of contributions user has made in a given timespan
 153+ * @param $session_id String: unique session id for this editing sesion
 154+ * @param $is_logged_in Boolean: whether or not the user is logged in
 155+ * @param $namespace Integer: namespace the user is editing
 156+ * @param $event_id Integer: event type
 157+ * @param $contribs Integer: contributions the user has made (or NULL if user not logged in)
 158+ * @param $contribs_in_timespan Integer: number of contributions user has made in a given timespan
159159 * @return true if the event was stored in the DB
160160 */
161 - public static function trackEvent( $session_id, $is_logged_in, $namespace, $event_id, $contribs=0, $contribs_in_timespan=0 ){
162 -
 161+ public static function trackEvent( $session_id, $is_logged_in, $namespace, $event_id, $contribs = 0, $contribs_in_timespan = 0 ){
163162 $dbw = wfGetDB( DB_MASTER );
164163
165164 $dbw->begin();
166 -
 165+
167166 // Builds insert information
168167 $data = array(
169168 'session_id' => (string) $session_id,
170169 'is_logged_in' => (bool) $is_logged_in,
171170 'user_total_contribs' => ( $is_logged_in ? (int) $contribs : null ),
172 - 'user_contribs_span' => ( $is_logged_in ? (int) $contribs_in_timespan : null ),
173 - 'namespace' => (int) $namespace,
 171+ 'user_contribs_span' => ( $is_logged_in ? (int) $contribs_in_timespan : null ),
 172+ 'namespace' => (int) $namespace,
174173 'event_id' => (int) $event_id
175 -
176174 );
177175
178 - $db_status = $dbw->insert('click_tracking', $data, __METHOD__);
 176+ $db_status = $dbw->insert( 'click_tracking', $data, __METHOD__ );
179177 $dbw->commit();
180178 return $db_status;
181179 }
Index: trunk/extensions/UsabilityInitiative/ClickTracking/ClickTracking.js
@@ -1,11 +1,10 @@
2 -
32 (function($) {
43
54 // creates 'track action' function to call the clicktracking API and send the ID
65 $.trackAction = function ( id ){
7 - $j.post( wgScriptPath + '/api.php', { 'action': 'clicktracking', 'eventid': id, 'token': wgTrackingToken } );
 6+ $j.post( wgScriptPath + '/api.php', { 'action': 'clicktracking', 'eventid': id, 'token': wgTrackingToken } );
87 };
98
109 return $(this);
1110
12 -})(jQuery);
 11+})(jQuery);
\ No newline at end of file
Index: trunk/extensions/UsabilityInitiative/ClickTracking/UserDailyContribs.sql
@@ -5,17 +5,15 @@
66 --
77
88 CREATE TABLE IF NOT EXISTS /*_*/user_daily_contribs (
9 -
109 -- user id
1110 user_id integer NOT NULL default 0,
12 -
 11+
1312 -- day
1413 day DATE NOT NULL,
15 -
16 - -- contributions on that day by that user
17 - contribs integer NOT NULL default 0,
1814
 15+ -- contributions on that day by that user
 16+ contribs integer NOT NULL default 0,
 17+
1918 -- a unique entry for a given user_id and day
20 - PRIMARY KEY(user_id, day)
21 -
22 -) /*$wgDBTableOptions*/;
 19+ PRIMARY KEY(user_id, day)
 20+) /*$wgDBTableOptions*/;
\ No newline at end of file
Index: trunk/extensions/UsabilityInitiative/ClickTracking/ClickTracking.sql
@@ -5,23 +5,22 @@
66 CREATE TABLE IF NOT EXISTS /*_*/click_tracking (
77 -- Timestamp
88 action_time timestamp NOT NULL default CURRENT_TIMESTAMP,
9 -
 9+
1010 -- session id
1111 session_id varbinary(255) NOT NULL,
12 -
13 - --true if the user is logged in
14 - is_logged_in boolean NOT NULL,
15 -
 12+
 13+ -- true if the user is logged in
 14+ is_logged_in boolean NOT NULL,
 15+
1616 -- total user contributions
1717 user_total_contribs integer,
18 -
 18+
1919 -- user contributions over a specified timespan
2020 user_contribs_span integer,
21 -
 21+
2222 -- namespace being edited
2323 namespace integer NOT NULL,
2424
25 - -- event ID (not unique)
 25+ -- event ID (not unique)
2626 event_id integer NOT NULL
27 -
28 -) /*$wgDBTableOptions*/;
 27+) /*$wgDBTableOptions*/;
\ No newline at end of file
Index: trunk/extensions/UsabilityInitiative/ClickTracking/ApiClickTracking.php
@@ -1,86 +1,82 @@
22 <?php
3 -
43 /**
54 * Extend the API for click tracking
6 - *
7 - * @author nimishgautam
85 *
 6+ * @file
 7+ * @ingroup API
98 */
109 class ApiClickTracking extends ApiBase {
1110
1211 /**
13 - * runs when the api is called with "clicktracking", takes in "eventid" and an edit token given to the user, "token"
 12+ * runs when the API is called with "clicktracking", takes in "eventid" and an edit token given to the user, "token"
1413 * @see includes/api/ApiBase#execute()
1514 */
1615 public function execute(){
1716 global $wgUser, $wgTitle;
18 -
 17+
1918 $params = $this->extractRequestParams();
2019 $this->validateParams( $params );
2120 $eventid_to_lookup = $params['eventid'];
2221 $session_id = $params['token'];
23 -
24 - //Event ID lookup table
25 - $event_id = ClickTrackingHooks::getEventIDFromName(urldecode($eventid_to_lookup));
26 -
 22+
 23+ // Event ID lookup table
 24+ $event_id = ClickTrackingHooks::getEventIDFromName( urldecode( $eventid_to_lookup ) );
 25+
2726 $is_logged_in = $wgUser->isLoggedIn();
28 -
29 -
 27+
3028 ClickTrackingHooks::trackEvent(
31 - $session_id, //randomly generated session ID
32 - $is_logged_in, //is the user logged in?
33 - $wgTitle->getNamespace(), //what namespace are they editing?
34 - $event_id, //event ID passed in
35 - ( $is_logged_in ? $wgUser->getEditCount() : 0 ), //total edit count or 0 if anonymous
36 - ( $is_logged_in ?
37 - ( ClickTrackingHooks::getEditCountSince( time() - $wgClickTrackContribTimeValue) )
38 - : 0) //contributions since whatever the time value is, or 0 if anonymous
39 - );
 29+ $session_id, // randomly generated session ID
 30+ $is_logged_in, // is the user logged in?
 31+ $wgTitle->getNamespace(), // what namespace are they editing?
 32+ $event_id, // event ID passed in
 33+ ( $is_logged_in ? $wgUser->getEditCount() : 0 ), // total edit count or 0 if anonymous
 34+ ( $is_logged_in ?
 35+ ( ClickTrackingHooks::getEditCountSince( time() - $wgClickTrackContribTimeValue ) )
 36+ : 0
 37+ ) // contributions since whatever the time value is, or 0 if anonymous
 38+ );
4039 }
4140
4241 /**
43 - * required parameter check
 42+ * Required parameter check
4443 * @param $params params extracted from the POST
45 - * @return unknown_type
4644 */
4745 protected function validateParams( $params ) {
48 - $required = array( 'eventid', 'token');
49 - foreach( $required as $arg ) {
50 - if ( !isset( $params[$arg] ) ) {
51 - $this->dieUsageMsg( array( 'missingparam', $arg ) );
52 - }
53 - }
54 - }
55 -
56 -
 46+ $required = array( 'eventid', 'token' );
 47+ foreach( $required as $arg ) {
 48+ if ( !isset( $params[$arg] ) ) {
 49+ $this->dieUsageMsg( array( 'missingparam', $arg ) );
 50+ }
 51+ }
 52+ }
5753
58 - public function getParamDescription() {
59 - return array(
60 - 'eventid' => 'string of eventID',
61 - 'token' => 'unique edit ID for this edit session'
62 - );
63 - }
64 -
 54+ public function getParamDescription() {
 55+ return array(
 56+ 'eventid' => 'string of eventID',
 57+ 'token' => 'unique edit ID for this edit session'
 58+ );
 59+ }
 60+
6561 public function getDescription() {
66 - return array(
67 - 'Track user clicks on Javascript items.' );
68 - }
 62+ return array(
 63+ 'Track user clicks on JavaScript items.'
 64+ );
 65+ }
6966
7067 public function getAllowedParams() {
71 - return array(
72 - 'eventid' => array(
73 - ApiBase::PARAM_TYPE => 'string'
74 - ),
75 - 'token' => array(
76 - ApiBase::PARAM_TYPE => 'string'
77 - )
78 - );
79 - }
80 -
81 - //TODO: create a more useful 'version number'
82 - public function getVersion() {
83 - return __CLASS__ . ': $Id: $';
84 - }
85 -
86 -
 68+ return array(
 69+ 'eventid' => array(
 70+ ApiBase::PARAM_TYPE => 'string'
 71+ ),
 72+ 'token' => array(
 73+ ApiBase::PARAM_TYPE => 'string'
 74+ )
 75+ );
 76+ }
 77+
 78+ // TODO: create a more useful 'version number'
 79+ public function getVersion() {
 80+ return __CLASS__ . ': $Id: $';
 81+ }
 82+
8783 }
\ No newline at end of file
Index: trunk/extensions/UsabilityInitiative/ClickTracking/ClickTrackingEvents.sql
@@ -5,14 +5,12 @@
66 --
77
88 CREATE TABLE IF NOT EXISTS /*_*/click_tracking_events (
9 -
109 -- event name
1110 event_name VARBINARY(255) unique,
12 -
 11+
1312 -- day
1413 id INTEGER AUTO_INCREMENT,
1514
1615 -- keyed on event, sql makes you use the id as a primary key
17 - PRIMARY KEY(id)
18 -
 16+ PRIMARY KEY(id)
1917 ) /*$wgDBTableOptions*/;
Index: trunk/extensions/UsabilityInitiative/ClickTracking/ClickTracking.php
@@ -18,13 +18,12 @@
1919
2020 /* Configuration */
2121
22 -//functions should check if this is set before logging clicktrack events
 22+// functions should check if this is set before logging clicktrack events
2323 $wgClickTrackEnabled = true;
2424
 25+// set the time window for what we consider 'recent' contributions, in days
 26+$wgClickTrackContribTimeValue = 60 * 60 * 24 * 365 / 2; // half a year
2527
26 -//set the time window for what we consider 'recent' contributions, in days
27 -$wgClickTrackContribTimeValue = 60 * 60 * 24 * 365 / 2; //half a year
28 -
2928 // Credits
3029 $wgExtensionCredits['other'][] = array(
3130 'path' => __FILE__,
@@ -39,15 +38,14 @@
4039 require_once( dirname( dirname( __FILE__ ) ) . "/UsabilityInitiative.php" );
4140
4241 // Adds Autoload Classes
43 -$wgAutoloadClasses['ClickTrackingHooks'] =
44 - dirname( __FILE__ ) . '/ClickTracking.hooks.php';
 42+$dir = dirname( __FILE__ ) . '/';
 43+$wgAutoloadClasses['ClickTrackingHooks'] = $dir . 'ClickTracking.hooks.php';
 44+$wgAutoloadClasses['ApiClickTracking'] = $dir . 'ApiClickTracking.php';
4545
46 -$wgAutoloadClasses['ApiClickTracking'] = dirname( __FILE__ ) . '/ApiClickTracking.php';
47 -
 46+// Hooked functions
4847 $wgHooks['LoadExtensionSchemaUpdates'][] = 'ClickTrackingHooks::schema';
49 -
5048 $wgHooks['ArticleSaveComplete'][] = 'ClickTrackingHooks::storeNewContrib';
 49+$wgHooks['EditPage::showEditForm:initial'][] = 'ClickTrackingHooks::addJS';
5150
52 -$wgAPIModules['clicktracking'] = 'ApiClickTracking';
53 -
54 -$wgHooks['EditPage::showEditForm:initial'] [] = 'ClickTrackingHooks::addJS';
 51+// Set up the new API module
 52+$wgAPIModules['clicktracking'] = 'ApiClickTracking';
\ No newline at end of file

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r55425JS click tracking!...nimishg19:50, 21 August 2009

Status & tagging log