Index: trunk/extensions/UsabilityInitiative/ClickTracking/ClickTracking.hooks.php |
— | — | @@ -10,7 +10,7 @@ |
11 | 11 | class ClickTrackingHooks { |
12 | 12 | |
13 | 13 | /* Static Functions */ |
14 | | - |
| 14 | + |
15 | 15 | /* initializations */ |
16 | 16 | |
17 | 17 | /* 3 tables for click tracking */ |
— | — | @@ -26,7 +26,7 @@ |
27 | 27 | 'user_daily_contribs', |
28 | 28 | dirname( __FILE__ ) . '/UserDailyContribs.sql' |
29 | 29 | ); |
30 | | - |
| 30 | + |
31 | 31 | $wgExtNewTables[] = array( |
32 | 32 | 'click_tracking_events', |
33 | 33 | dirname( __FILE__ ) . '/ClickTrackingEvents.sql' |
— | — | @@ -35,146 +35,144 @@ |
36 | 36 | return true; |
37 | 37 | } |
38 | 38 | |
39 | | - |
40 | 39 | /** |
41 | | - * adds javascript |
| 40 | + * Adds JavaScript |
42 | 41 | */ |
43 | 42 | public static function addJS(){ |
44 | 43 | UsabilityInitiativeHooks::initialize(); |
45 | | - UsabilityInitiativeHooks::addScript('ClickTracking/ClickTracking.js'); |
| 44 | + UsabilityInitiativeHooks::addScript( 'ClickTracking/ClickTracking.js' ); |
46 | 45 | UsabilityInitiativeHooks::addVariables( |
47 | 46 | array( 'wgTrackingToken' => ClickTrackingHooks::get_session_id() ) |
48 | 47 | ); |
49 | 48 | return true; |
50 | 49 | } |
51 | | - |
52 | | - |
| 50 | + |
53 | 51 | /** |
54 | 52 | * Gets the session ID...we just want a unique random ID for the page load |
55 | 53 | * @return session ID |
56 | 54 | */ |
57 | 55 | 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() ) ); |
60 | 58 | } |
61 | | - |
62 | | - /** |
| 59 | + |
| 60 | + /** |
63 | 61 | * Get the number of revisions a user has made since a given time |
64 | 62 | * @param $ts beginning timestamp |
65 | 63 | * @return number of revsions this user has made |
66 | 64 | */ |
67 | | - public static function getEditCountSince($ts){ |
| 65 | + public static function getEditCountSince( $ts ){ |
68 | 66 | 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 | + |
73 | 71 | $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; |
92 | 89 | } |
93 | | - |
| 90 | + |
94 | 91 | /** |
95 | 92 | * Stores a new contribution |
96 | | - * @return unknown_type |
| 93 | + * @return true |
97 | 94 | */ |
98 | 95 | public static function storeNewContrib(){ |
99 | 96 | global $wgUser; |
100 | | - $today = gmdate('Ymd',time()); |
| 97 | + $today = gmdate( 'Ymd', time() ); |
101 | 98 | $dbw = wfGetDB( DB_MASTER ); |
102 | 99 | $sql = |
103 | 100 | "INSERT INTO user_daily_contribs (user_id,day,contribs) VALUES ({$wgUser->getId()},$today,1) ON DUPLICATE KEY UPDATE contribs=contribs+1;"; |
104 | 101 | $dbw->query($sql, __METHOD__); |
105 | 102 | return true; |
106 | 103 | } |
107 | | - |
| 104 | + |
108 | 105 | /** |
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 |
112 | 109 | */ |
113 | | - public static function getEventIDFromName($event_name){ |
| 110 | + public static function getEventIDFromName( $event_name ){ |
114 | 111 | $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 |
136 | 112 | |
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; |
149 | 149 | } |
150 | | - |
| 150 | + |
151 | 151 | /** |
152 | 152 | * 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 |
159 | 159 | * @return true if the event was stored in the DB |
160 | 160 | */ |
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 ){ |
163 | 162 | $dbw = wfGetDB( DB_MASTER ); |
164 | 163 | |
165 | 164 | $dbw->begin(); |
166 | | - |
| 165 | + |
167 | 166 | // Builds insert information |
168 | 167 | $data = array( |
169 | 168 | 'session_id' => (string) $session_id, |
170 | 169 | 'is_logged_in' => (bool) $is_logged_in, |
171 | 170 | '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, |
174 | 173 | 'event_id' => (int) $event_id |
175 | | - |
176 | 174 | ); |
177 | 175 | |
178 | | - $db_status = $dbw->insert('click_tracking', $data, __METHOD__); |
| 176 | + $db_status = $dbw->insert( 'click_tracking', $data, __METHOD__ ); |
179 | 177 | $dbw->commit(); |
180 | 178 | return $db_status; |
181 | 179 | } |
Index: trunk/extensions/UsabilityInitiative/ClickTracking/ClickTracking.js |
— | — | @@ -1,11 +1,10 @@ |
2 | | - |
3 | 2 | (function($) { |
4 | 3 | |
5 | 4 | // creates 'track action' function to call the clicktracking API and send the ID |
6 | 5 | $.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 } ); |
8 | 7 | }; |
9 | 8 | |
10 | 9 | return $(this); |
11 | 10 | |
12 | | -})(jQuery); |
| 11 | +})(jQuery); |
\ No newline at end of file |
Index: trunk/extensions/UsabilityInitiative/ClickTracking/UserDailyContribs.sql |
— | — | @@ -5,17 +5,15 @@ |
6 | 6 | -- |
7 | 7 | |
8 | 8 | CREATE TABLE IF NOT EXISTS /*_*/user_daily_contribs ( |
9 | | - |
10 | 9 | -- user id |
11 | 10 | user_id integer NOT NULL default 0, |
12 | | - |
| 11 | + |
13 | 12 | -- day |
14 | 13 | day DATE NOT NULL, |
15 | | - |
16 | | - -- contributions on that day by that user |
17 | | - contribs integer NOT NULL default 0, |
18 | 14 | |
| 15 | + -- contributions on that day by that user |
| 16 | + contribs integer NOT NULL default 0, |
| 17 | + |
19 | 18 | -- 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 @@ |
6 | 6 | CREATE TABLE IF NOT EXISTS /*_*/click_tracking ( |
7 | 7 | -- Timestamp |
8 | 8 | action_time timestamp NOT NULL default CURRENT_TIMESTAMP, |
9 | | - |
| 9 | + |
10 | 10 | -- session id |
11 | 11 | 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 | + |
16 | 16 | -- total user contributions |
17 | 17 | user_total_contribs integer, |
18 | | - |
| 18 | + |
19 | 19 | -- user contributions over a specified timespan |
20 | 20 | user_contribs_span integer, |
21 | | - |
| 21 | + |
22 | 22 | -- namespace being edited |
23 | 23 | namespace integer NOT NULL, |
24 | 24 | |
25 | | - -- event ID (not unique) |
| 25 | + -- event ID (not unique) |
26 | 26 | 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 @@ |
2 | 2 | <?php |
3 | | - |
4 | 3 | /** |
5 | 4 | * Extend the API for click tracking |
6 | | - * |
7 | | - * @author nimishgautam |
8 | 5 | * |
| 6 | + * @file |
| 7 | + * @ingroup API |
9 | 8 | */ |
10 | 9 | class ApiClickTracking extends ApiBase { |
11 | 10 | |
12 | 11 | /** |
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" |
14 | 13 | * @see includes/api/ApiBase#execute() |
15 | 14 | */ |
16 | 15 | public function execute(){ |
17 | 16 | global $wgUser, $wgTitle; |
18 | | - |
| 17 | + |
19 | 18 | $params = $this->extractRequestParams(); |
20 | 19 | $this->validateParams( $params ); |
21 | 20 | $eventid_to_lookup = $params['eventid']; |
22 | 21 | $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 | + |
27 | 26 | $is_logged_in = $wgUser->isLoggedIn(); |
28 | | - |
29 | | - |
| 27 | + |
30 | 28 | 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 | + ); |
40 | 39 | } |
41 | 40 | |
42 | 41 | /** |
43 | | - * required parameter check |
| 42 | + * Required parameter check |
44 | 43 | * @param $params params extracted from the POST |
45 | | - * @return unknown_type |
46 | 44 | */ |
47 | 45 | 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 | + } |
57 | 53 | |
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 | + |
65 | 61 | 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 | + } |
69 | 66 | |
70 | 67 | 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 | + |
87 | 83 | } |
\ No newline at end of file |
Index: trunk/extensions/UsabilityInitiative/ClickTracking/ClickTrackingEvents.sql |
— | — | @@ -5,14 +5,12 @@ |
6 | 6 | -- |
7 | 7 | |
8 | 8 | CREATE TABLE IF NOT EXISTS /*_*/click_tracking_events ( |
9 | | - |
10 | 9 | -- event name |
11 | 10 | event_name VARBINARY(255) unique, |
12 | | - |
| 11 | + |
13 | 12 | -- day |
14 | 13 | id INTEGER AUTO_INCREMENT, |
15 | 14 | |
16 | 15 | -- keyed on event, sql makes you use the id as a primary key |
17 | | - PRIMARY KEY(id) |
18 | | - |
| 16 | + PRIMARY KEY(id) |
19 | 17 | ) /*$wgDBTableOptions*/; |
Index: trunk/extensions/UsabilityInitiative/ClickTracking/ClickTracking.php |
— | — | @@ -18,13 +18,12 @@ |
19 | 19 | |
20 | 20 | /* Configuration */ |
21 | 21 | |
22 | | -//functions should check if this is set before logging clicktrack events |
| 22 | +// functions should check if this is set before logging clicktrack events |
23 | 23 | $wgClickTrackEnabled = true; |
24 | 24 | |
| 25 | +// set the time window for what we consider 'recent' contributions, in days |
| 26 | +$wgClickTrackContribTimeValue = 60 * 60 * 24 * 365 / 2; // half a year |
25 | 27 | |
26 | | -//set the time window for what we consider 'recent' contributions, in days |
27 | | -$wgClickTrackContribTimeValue = 60 * 60 * 24 * 365 / 2; //half a year |
28 | | - |
29 | 28 | // Credits |
30 | 29 | $wgExtensionCredits['other'][] = array( |
31 | 30 | 'path' => __FILE__, |
— | — | @@ -39,15 +38,14 @@ |
40 | 39 | require_once( dirname( dirname( __FILE__ ) ) . "/UsabilityInitiative.php" ); |
41 | 40 | |
42 | 41 | // 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'; |
45 | 45 | |
46 | | -$wgAutoloadClasses['ApiClickTracking'] = dirname( __FILE__ ) . '/ApiClickTracking.php'; |
47 | | - |
| 46 | +// Hooked functions |
48 | 47 | $wgHooks['LoadExtensionSchemaUpdates'][] = 'ClickTrackingHooks::schema'; |
49 | | - |
50 | 48 | $wgHooks['ArticleSaveComplete'][] = 'ClickTrackingHooks::storeNewContrib'; |
| 49 | +$wgHooks['EditPage::showEditForm:initial'][] = 'ClickTrackingHooks::addJS'; |
51 | 50 | |
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 |