Index: trunk/phase3/resources/mediawiki/mediawiki.js |
— | — | @@ -409,6 +409,82 @@ |
410 | 410 | $.cookie( 'mediaWiki.user.id', id, { 'expires': 365, 'path': '/' } ); |
411 | 411 | return id; |
412 | 412 | }; |
| 413 | + |
| 414 | + /** |
| 415 | + * Gets the user's bucket, placing them in one at random based on set odds if needed. |
| 416 | + * |
| 417 | + * @param key String: Name of bucket |
| 418 | + * @param options Object: Bucket configuration options |
| 419 | + * @param options.buckets Object: List of bucket-name/relative-probability pairs (required, |
| 420 | + * must have at least one pair) |
| 421 | + * @param options.version Number: Version of bucket test, changing this forces rebucketing |
| 422 | + * (optional, default: 0) |
| 423 | + * @param options.tracked Boolean: Track the event of bucketing through the API module of |
| 424 | + * the ClickTracking extension (optional, default: false) |
| 425 | + * @param options.expires Number: Length of time (in days) until the user gets rebucketed |
| 426 | + * (optional, default: 30) |
| 427 | + * @return String: Bucket name - the randomly chosen key of the options.buckets object |
| 428 | + * |
| 429 | + * @example |
| 430 | + * mw.user.bucket( 'test', { |
| 431 | + * 'buckets': { 'ignored': 50, 'control': 25, 'test': 25 }, |
| 432 | + * 'version': 1, |
| 433 | + * 'tracked': true, |
| 434 | + * 'expires': 7 |
| 435 | + * } ); |
| 436 | + */ |
| 437 | + this.bucket = function( key, options ) { |
| 438 | + options = $.extend( { |
| 439 | + 'buckets': {}, |
| 440 | + 'version': 0, |
| 441 | + 'tracked': false, |
| 442 | + 'expires': 30 |
| 443 | + }, options || {} ); |
| 444 | + var cookie = $.cookie( 'mw.user.bucket-' + key ); |
| 445 | + var bucket = null; |
| 446 | + var version = 0; |
| 447 | + // Bucket information is stored as 2 integers, together as version:bucket like: "1:2" |
| 448 | + if ( typeof cookie === 'string' && cookie.length > 2 && cookie.indexOf( ':' ) > 0 ) { |
| 449 | + var parts = cookie.split( ':' ); |
| 450 | + if ( parts.length > 1 && parts[1] == options.version ) { |
| 451 | + version = Number( parts[0] ); |
| 452 | + bucket = Number( parts[1] ); |
| 453 | + } |
| 454 | + } |
| 455 | + if ( bucket === null ) { |
| 456 | + if ( !$.isPlainObject( options.buckets ) ) { |
| 457 | + throw 'Invalid buckets error. Object expected for options.buckets .'; |
| 458 | + } |
| 459 | + version = Number( options.version ); |
| 460 | + // Find range |
| 461 | + var range = 0; |
| 462 | + for ( var k in options.buckets ) { |
| 463 | + range += options.buckets[k]; |
| 464 | + } |
| 465 | + // Select random value within range |
| 466 | + var rand = Math.random() * range; |
| 467 | + // Determine which bucket the value landed in |
| 468 | + var total = 0; |
| 469 | + for ( var k in options.buckets ) { |
| 470 | + bucket = k; |
| 471 | + total += options.buckets[k]; |
| 472 | + if ( total >= rand ) { |
| 473 | + break; |
| 474 | + } |
| 475 | + } |
| 476 | + if ( options.tracked ) { |
| 477 | + mw.loader.using( 'jquery.clickTracking', function() { |
| 478 | + $.trackAction( 'mw.user.bucket-' + key + '@' + version + ':' + bucket ); |
| 479 | + } ); |
| 480 | + } |
| 481 | + $.cookie( |
| 482 | + 'mw.userBuckets-' + key, |
| 483 | + version + ':' + bucket, |
| 484 | + { 'path': '/', 'expires': Number( options.expires ) } |
| 485 | + ); |
| 486 | + } |
| 487 | + return bucket; |
| 488 | + }; |
413 | 489 | } |
414 | 490 | |
415 | 491 | /* Public Members */ |