r85049 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r85048‎ | r85049 | r85050 >
Date:21:40, 30 March 2011
Author:nimishg
Status:resolved (Comments)
Tags:
Comment:
Simplified buckets, created a possible framework for user bucket campaigns
Modified paths:
  • /trunk/extensions/ClickTracking/ClickTracking.hooks.php (modified) (history)
  • /trunk/extensions/ClickTracking/modules/ext.UserBuckets.js (added) (history)
  • /trunk/extensions/ClickTracking/modules/sampleCampaign.js (added) (history)

Diff [purge]

Index: trunk/extensions/ClickTracking/ClickTracking.hooks.php
@@ -120,43 +120,7 @@
121121 }
122122
123123
124 - /*
125 - * Convenience function to unpackbuckets
126 - *
127 - */
128 - public static function unpackBuckets(){
129 - $bucketInfo = self::unpackBucketInfo();
130 -
131 - if( $bucketInfo == NULL ) return NULL;
132 -
133 - $retval = array();
134 - foreach($bucketInfo as $bucket){
135 - $friendly_named_bucket = array();
136 - $friendly_named_bucket["name"] = $bucket[0];
137 - $friendly_named_bucket["value"] = $bucket[1];
138 - $friendly_named_bucket["version"] = $bucket[2];
139 - $retval[] = $friendly_named_bucket;
140 - }
141 - return $retval;
142 - }
143124
144 - /*
145 - * Convenience function to pack buckets
146 - *
147 - */
148 - public static function packBuckets( $buckets ){
149 - if( $buckets == NULL ) return; //nothing to do
150 - $packedBucketInfo = array();
151 -
152 - foreach($buckets as $bucket){
153 - $bucketInfo = array();
154 - $bucketInfo[0] = $bucket["name"];
155 - $bucketInfo[1] = $bucket["value"];
156 - $bucketInfo[2] = $bucket["version"];
157 - $packedBucketInfo[] = $bucketInfo;
158 - }
159 - self::packBucketInfo($packedBucketInfo);
160 - }
161125
162126 /**
163127 * Returns bucket information
@@ -221,19 +185,18 @@
222186 );
223187 $db_status_buckets = true;
224188 $db_status = $dbw->insert( 'click_tracking', $data, __METHOD__ );
225 - $bucket_id = $dbw->insertId();
226189
227190 if( $recordBucketInfo && $db_status ){
228191 $buckets = self::unpackBucketInfo();
229192 if( $buckets ){
230 - foreach( $buckets as $bucket ){
 193+ foreach( $buckets as $bucketName => $bucketValue ){
231194 $db_status_buckets = $db_status_buckets &&
232195 $dbw->insert( 'click_tracking_user_properties',
233196 array(
234197 'session_id' => (string) $sessionId,
235 - 'property_id' => (string) $bucket[0],
236 - 'property_value' => (string) $bucket[1],
237 - 'property_version' => (int) $bucket[2]
 198+ 'property_id' => (string) $bucketName,
 199+ 'property_value' => (string) $bucketValue[0],
 200+ 'property_version' => (int) $bucketValue[1]
238201 ),
239202 __METHOD__);
240203 }
Index: trunk/extensions/ClickTracking/modules/ext.UserBuckets.js
@@ -0,0 +1,62 @@
 2+
 3+
 4+if( !JSON || !JSON.stringify ){
 5+ //include OpenSource JSON stringify from json.org
 6+}
 7+
 8+
 9+//lazy-load
 10+$.getBuckets = function (force){
 11+ if (typeof(this.userBuckets) == 'undefined' || force ){
 12+ this.userBuckets = $.parseJSON( $.cookie('userbuckets') );
 13+ }
 14+ return this.userBuckets;
 15+};
 16+
 17+$.setBucket = function ( bucketName, bucketValue, bucketVersion ){
 18+ var bucketCookies = $.getBuckets();
 19+ bucketCookies[ bucketName ] = [ bucketValue, bucketVersion ];
 20+ $.cookie('userbuckets', JSON.stringify( bucketCookies ) );
 21+ bucketCookies = $.getBuckets(true); //force it to rerun and update
 22+};
 23+
 24+$.setupActiveBuckets = function(){
 25+ var buckets = $.getBuckets();
 26+ for(iter in MW.activeCampaigns){
 27+ var campaign = MW.activeCampaigns[iter];
 28+
 29+ // if bucket has been set, or bucket version is out of date,
 30+ // set up a user bucket
 31+ if( !buckets[campaign.name] || buckets[campaign.name][1] < campaign.version){
 32+ //add up all rates
 33+ var bucketTotal = 0;
 34+ for ( var rate in campaign.rates ){
 35+ bucketTotal += campaign.rates[rate];
 36+ }
 37+
 38+ //give the user a random number in those rates
 39+ var currentUser = Math.floor(Math.random() * (bucketTotal+1));
 40+
 41+ // recurse through the rates until we get into the range the user falls in,
 42+ // assign them to that range
 43+ var runningTotal = 0;
 44+ for( rate in campaign.rates ){
 45+ runningTotal += campaign.rates[rate];
 46+ if(currentUser <= runningTotal){
 47+ $.setBucket(campaign.name, rate, campaign.version);
 48+ }
 49+ break;
 50+ }
 51+ }
 52+
 53+ // do the actual code in the campaign based on the bucket
 54+ if($.getBuckets()[campaign.name][0] != "none"){
 55+ campaign[$.getBuckets()[campaign.name][0]](); //function to execute
 56+ if(campaign.all){
 57+ campaign.all();
 58+ }
 59+ }
 60+
 61+ }
 62+
 63+};
\ No newline at end of file
Property changes on: trunk/extensions/ClickTracking/modules/ext.UserBuckets.js
___________________________________________________________________
Added: svn:eol-style
164 + native
Index: trunk/extensions/ClickTracking/modules/sampleCampaign.js
@@ -0,0 +1,50 @@
 2+
 3+//checks
 4+if(!MW){ MW={};}
 5+if(!MW.activeCampaigns){ MW.activeCampaigns ={}; }
 6+
 7+//define new active campaign
 8+MW.activeCampaigns[MW.activeCampaigns.length] =
 9+
 10+{
 11+ //Treatment name
 12+ "name": "ArticleSave",
 13+
 14+ //Treatment version. Increment this when altering rates
 15+ "version": 2,
 16+
 17+ // Rates are calculated out of the total sum, so
 18+ // rates of x:10000, y:3, and z:1 mean users have a
 19+ // chance of being in bucket x at 10000/10004,
 20+ // y at 3/10004 and z at 1/10004
 21+ // The algorithm is faster if these are ordered in descending order,
 22+ // particularly if there are orders of magnitude differences in the
 23+ // bucket sizes
 24+ // "none" is reserved for control
 25+ "rates": {"none": 10000, "Bold": 3, "Italics": 1 },
 26+
 27+ // individual changes, function names corresponding
 28+ // to what is in "rates" object
 29+ // (note: "none" function not needed or used)
 30+
 31+ "Bold": function(){
 32+ //change edit button to bold
 33+ $j("#wpSave").css("font-weight", "bolder");
 34+
 35+ },
 36+ "Italics": function(){
 37+ //change edit button to italics
 38+ $j("#wpSave").css("font-weight", "normal")
 39+ .css("font-style", "italic");
 40+
 41+ },
 42+
 43+ // "allActive" is reserved.
 44+ // If this function exists, it will be apply to every user not in the "none" bucket
 45+ "allActive": function(){
 46+ //add click tracking to save
 47+ $j("#wpSave").click(function(){ $j.trackAction('save'); });
 48+
 49+ }
 50+
 51+};
\ No newline at end of file
Property changes on: trunk/extensions/ClickTracking/modules/sampleCampaign.js
___________________________________________________________________
Added: svn:eol-style
152 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r86961cross-browser funnimishg18:10, 26 April 2011
r86976* Replace MW (inexistant) with mw (global alias to the mediaWiki object)...krinkle19:35, 26 April 2011

Comments

#Comment by Krinkle (talk | contribs)   18:39, 26 April 2011

Some random points:

  • Check code conventions (whitespace, new line end of file, single quotes etc. ): Manual:Coding conventions
  • The global mediaWiki is always available, including its alias "mw". MW (capital) however does not exist, please remove the check and use the lowercase alias.
  • Don't depend on $. And $j is a legacy alias. Instead, use jQuery and locally alias it to $ by using an Immediately-Invoked Function Expression as shown here.

Status & tagging log