r57781 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r57780‎ | r57781 | r57782 >
Date:18:13, 15 October 2009
Author:catrope
Status:ok
Tags:
Comment:
ClickTracking: Code style cleanup
Modified paths:
  • /trunk/extensions/UsabilityInitiative/ClickTracking/ApiClickTracking.php (modified) (history)
  • /trunk/extensions/UsabilityInitiative/ClickTracking/ApiSpecialClickTracking.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)

Diff [purge]

Index: trunk/extensions/UsabilityInitiative/ClickTracking/ClickTracking.hooks.php
@@ -36,42 +36,34 @@
3737 * @return unknown_type
3838 */
3939 public static function parserTestTables( &$tables ) {
40 - $tables[] = 'click_tracking';
41 - $tables[] = 'click_tracking_events';
42 - return true;
 40+ $tables[] = 'click_tracking';
 41+ $tables[] = 'click_tracking_events';
 42+ return true;
4343 }
4444
4545 /*
4646 * check to see if user is throttled
4747 */
48 - public static function isUserThrottled(){
 48+ public static function isUserThrottled() {
4949 global $wgClickTrackThrottle;
50 - if( ( $wgClickTrackThrottle >= 0 ) && ( rand() % $wgClickTrackThrottle == 0 ) ){
51 - return 'false';
52 - }
53 - else {
54 - return 'true';
55 - }
 50+ return !( $wgClickTrackThrottle >= 0 && rand() % $wgClickTrackThrottle == 0 );
5651 }
5752
5853
5954 /**
6055 * Adds JavaScript
6156 */
62 - public static function addJS(){
 57+ public static function addJS() {
6358 global $wgOut;
6459
6560 UsabilityInitiativeHooks::initialize();
6661 UsabilityInitiativeHooks::addScript( 'ClickTracking/ClickTracking.js' );
6762 UsabilityInitiativeHooks::addVariables(
68 - array(
 63+ array(
6964 'wgTrackingToken' => ClickTrackingHooks::get_session_id()
 65+ 'wgClickTrackingIsThrottled' => ClickTrackingHooks::isUserThrottled()
7066 )
7167 );
72 - //need a literal false, not "false" to be output, to work prperly
73 - $userThrottle = ClickTrackingHooks::isUserThrottled();
74 - $wgOut->addScript("<script> var wgClickTrackingIsThrottled = $userThrottle; </script>");
75 -
7668 return true;
7769
7870 }
@@ -80,7 +72,7 @@
8173 * Gets the session ID...we just want a unique random ID for the page load
8274 * @return session ID
8375 */
84 - public static function get_session_id(){
 76+ public static function get_session_id() {
8577 global $wgUser;
8678 return wfGenerateToken( array( $wgUser->getName(), time() ) );
8779 }
@@ -90,7 +82,7 @@
9183 * @param $ts beginning timestamp
9284 * @return number of revsions this user has made
9385 */
94 - public static function getEditCountSince( $ts ){
 86+ public static function getEditCountSince( $ts ) {
9587 global $wgUser;
9688
9789 // convert to just the day
@@ -109,7 +101,7 @@
110102 );
111103
112104 // user hasn't made any edits in whatever amount of time
113 - if( $edits == null ){
 105+ if ( $edits == null ) {
114106 $edits = 0;
115107 }
116108
@@ -122,22 +114,23 @@
123115 * @param $event_name String: name of the event to get
124116 * @return integer
125117 */
126 - public static function getEventIDFromName( $event_name ){
 118+ public static function getEventIDFromName( $event_name ) {
127119 $dbw = wfGetDB( DB_MASTER ); //replication lag means sometimes a new event will not exist in the table yet
128120
129121 $id_num = $dbw->selectField(
130122 'click_tracking_events',
131123 'id',
132 - array(
 124+ array(
133125 'event_name' => $event_name
134 - ),
 126+ ),
135127 __METHOD__
136128 );
137129
138130 // if this entry doesn't exist...
139131 // this will be incredibly rare as the whole database will only have a few hundred entries in it at most
140132 // and getting DB_MASTER up top would be wasteful
141 - if( $id_num === false ){
 133+ // FIXME: Use replace() instead of this selectField --> insert or update logic
 134+ if( $id_num === false ) {
142135 $dbw->insert(
143136 'click_tracking_events',
144137 array( 'event_name' => (string) $event_name ),
@@ -165,8 +158,8 @@
166159 * @param $contribs_in_timespan3 Integer: number of contributions user has made in timespan of granularity 3 (defined by ClickTracking/$wgClickTrackContribGranularity3)
167160 * @return true if the event was stored in the DB
168161 */
169 - public static function trackEvent( $session_id, $is_logged_in, $namespace, $event_id, $contribs = 0,
170 - $contribs_in_timespan1 = 0, $contribs_in_timespan2 = 0, $contribs_in_timespan3 = 0 ){
 162+ public static function trackEvent( $session_id, $is_logged_in, $namespace, $event_id, $contribs = 0,
 163+ $contribs_in_timespan1 = 0, $contribs_in_timespan2 = 0, $contribs_in_timespan3 = 0 ) {
171164 $dbw = wfGetDB( DB_MASTER );
172165
173166 $dbw->begin();
Index: trunk/extensions/UsabilityInitiative/ClickTracking/ClickTracking.js
@@ -2,7 +2,7 @@
33
44 if(!wgClickTrackingIsThrottled){
55 // creates 'track action' function to call the clicktracking API and send the ID
6 - $.trackAction = function ( id ){
 6+ $.trackAction = function ( id ) {
77 $j.post( wgScriptPath + '/api.php', { 'action': 'clicktracking', 'eventid': id, 'token': wgTrackingToken } );
88 };
99 }
Index: trunk/extensions/UsabilityInitiative/ClickTracking/ApiClickTracking.php
@@ -12,7 +12,7 @@
1313 * runs when the API is called with "clicktracking", takes in "eventid" and an edit token given to the user, "token"
1414 * @see includes/api/ApiBase#execute()
1515 */
16 - public function execute(){
 16+ public function execute() {
1717 global $wgUser, $wgTitle, $wgClickTrackContribGranularity1, $wgClickTrackContribGranularity2, $wgClickTrackContribGranularity3;
1818
1919 $params = $this->extractRequestParams();
@@ -25,17 +25,14 @@
2626
2727 $is_logged_in = $wgUser->isLoggedIn();
2828 $now = time();
29 - $granularity1 = $is_logged_in?
30 - ClickTrackingHooks::getEditCountSince( $now - $wgClickTrackContribGranularity1 )
31 - : 0;
 29+ $granularity1 = $is_logged_in ?
 30+ ClickTrackingHooks::getEditCountSince( $now - $wgClickTrackContribGranularity1 ) : 0;
3231
33 - $granularity2 = $is_logged_in?
34 - ClickTrackingHooks::getEditCountSince( $now - $wgClickTrackContribGranularity2 )
35 - : 0;
 32+ $granularity2 = $is_logged_in ?
 33+ ClickTrackingHooks::getEditCountSince( $now - $wgClickTrackContribGranularity2 ) : 0;
3634
37 - $granularity3 = $is_logged_in?
38 - ClickTrackingHooks::getEditCountSince( $now - $wgClickTrackContribGranularity3 )
39 - : 0;
 35+ $granularity3 = $is_logged_in ?
 36+ ClickTrackingHooks::getEditCountSince( $now - $wgClickTrackContribGranularity3 ) : 0;
4037
4138 ClickTrackingHooks::trackEvent(
4239 $session_id, // randomly generated session ID
@@ -88,7 +85,7 @@
8986
9087 // TODO: create a more useful 'version number'
9188 public function getVersion() {
92 - return __CLASS__ . ': $Id: $';
 89+ return __CLASS__ . ': $Id$';
9390 }
9491
9592 }
\ No newline at end of file
Property changes on: trunk/extensions/UsabilityInitiative/ClickTracking/ApiClickTracking.php
___________________________________________________________________
Added: svn:keywords
9693 + Id
Index: trunk/extensions/UsabilityInitiative/ClickTracking/ApiSpecialClickTracking.php
@@ -13,9 +13,7 @@
1414 * and "increment" as how many days to increment
1515 * @see includes/api/ApiBase#execute()
1616 */
17 - public function execute(){
18 -
19 -
 17+ public function execute() {
2018 $params = $this->extractRequestParams();
2119 $this->validateParams( $params );
2220 $event_id = $params['eventid'];
@@ -24,15 +22,10 @@
2523 $increment = $params['increment'];
2624 $userDefString = $params['userdefs'];
2725
28 - $click_data = array();
29 - try{
30 - $click_data = SpecialClickTracking::getChartData($event_id, $startdate, $enddate, $increment, $userDefString);
31 - $this->getResult()->addValue(array('datapoints'), 'expert', $click_data['expert']);
32 - $this->getResult()->addValue(array('datapoints'), 'basic', $click_data['basic']);
33 - $this->getResult()->addValue(array('datapoints'), 'intermediate', $click_data['intermediate']);
34 - }
35 - catch(Exception $e){ /* no result */ }
36 -
 26+ try {
 27+ $click_data = SpecialClickTracking::getChartData( $event_id, $startdate, $enddate, $increment, $userDefString );
 28+ $this->getResult()->addValue( null, 'datapoints', $click_data );
 29+ } catch ( Exception $e ) { /* no result */ }
3730 }
3831
3932 /**
@@ -47,19 +40,6 @@
4841 }
4942 }
5043
51 - //check if event id parses to an int greater than zero
52 - if( (int) $params['eventid'] <= 0){
53 - $this->dieUsage("Invalid event ID", "badeventid");
54 - }
55 -
56 - //check start and end date are of proper format
57 - if($params['startdate'] != 0 && strptime( $this->space_out_date($params['startdate']), "%Y %m %d") === false){
58 - $this->dieUsage("startdate not in YYYYMMDD format: <<{$params['startdate']}>>", "badstartdate");
59 - }
60 - if($params['enddate'] != 0 && strptime( $this->space_out_date($params['enddate']), "%Y %m %d") === false){
61 - $this->dieUsage("enddate not in YYYYMMDD format:<<{$params['enddate']}>>", "badenddate");
62 - }
63 -
6444 //check if increment is a positive int
6545 if( (int) $params['increment'] <= 0){
6646 $this->dieUsage("Invalid increment", "badincrement");
@@ -102,10 +82,10 @@
10383 ApiBase::PARAM_MIN => 1
10484 ),
10585 'startdate' => array(
106 - ApiBase::PARAM_TYPE => 'integer'
 86+ ApiBase::PARAM_TYPE => 'timestamp'
10787 ),
10888 'enddate' => array(
109 - ApiBase::PARAM_TYPE => 'integer'
 89+ ApiBase::PARAM_TYPE => 'timestamp'
11090 ),
11191 'increment' => array(
11292 ApiBase::PARAM_TYPE => 'integer',
@@ -113,7 +93,8 @@
11494 ApiBase::PARAM_MAX => 365 //1 year
11595 ),
11696 'userdefs' => array (
117 - ApiBase::PARAM_TYPE => 'string')
 97+ ApiBase::PARAM_TYPE => 'string'
 98+ )
11899 );
119100 }
120101
Index: trunk/extensions/UsabilityInitiative/ClickTracking/ClickTracking.php
@@ -25,7 +25,7 @@
2626
2727 // set the time window for what we consider 'recent' contributions, in days
2828 $wgClickTrackContribGranularity1 = 60 * 60 * 24 * 365 / 2; // half a year
29 -$wgClickTrackContribGranularity2 =60 * 60 * 24 * 365 / 4; // 1/4 a year (3 months approx)
 29+$wgClickTrackContribGranularity2 = 60 * 60 * 24 * 365 / 4; // 1/4 a year (3 months approx)
3030 $wgClickTrackContribGranularity3 = 60 * 60 * 24 * 30; //30 days (1 month approx)
3131
3232 // Credits

Status & tagging log