Index: trunk/extensions/ArticleCreationWorkflow/includes/PHPBucket.php |
— | — | @@ -0,0 +1,57 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +abstract class PHPBucket { |
| 5 | + /** |
| 6 | + * Buckets a user into one of a number of buckets at random. |
| 7 | + * A user is maintained in a bucket using a cookie. |
| 8 | + * |
| 9 | + * @param $key String: A unique identifier for this set of buckets. |
| 10 | + * @param $options Map: An array of options. Valid keys: |
| 11 | + * buckets: Associative array of bucket name to weight. |
| 12 | + * version: Version ID, increment to invalidate all buckets. |
| 13 | + * expires: Expiry, in days, of the bucket cookie. |
| 14 | + * |
| 15 | + * @todo Implement click tracking |
| 16 | + * @return type description |
| 17 | + */ |
| 18 | + static function getBucket( $key, $options ) { |
| 19 | + global $wgUser, $wgRequest; |
| 20 | + $defaults = array( |
| 21 | + 'buckets' => array(), |
| 22 | + 'version' => 0, |
| 23 | + 'tracked' => false, |
| 24 | + 'expires' => 30, |
| 25 | + ); |
| 26 | + |
| 27 | + $options = $options + $defaults; |
| 28 | + |
| 29 | + $cookieName = 'phpbucket:'.$key.':'.$options['version']; |
| 30 | + |
| 31 | + $selectedBucket = $wgRequest->getCookie( $cookieName ); |
| 32 | + |
| 33 | + if ( ! $selectedBucket ) { |
| 34 | + $range = 0; |
| 35 | + foreach( $options['buckets'] as $bucket => $weight ) { |
| 36 | + $range += $weight; |
| 37 | + } |
| 38 | + |
| 39 | + $rand = rand(0, $range); |
| 40 | + $upTo = 0; |
| 41 | + foreach( $options['buckets'] as $bucket => $weight ) { |
| 42 | + $selectedBucket = $bucket; |
| 43 | + $upTo += $weight; |
| 44 | + |
| 45 | + if ( $upTo >= $rand ) { |
| 46 | + break; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Set our cookie |
| 51 | + $exp = time() + ($options['expires'] * 3600 * 24); |
| 52 | + $wgRequest->response() |
| 53 | + ->setCookie( $cookieName, $selectedBucket, $exp); |
| 54 | + } |
| 55 | + |
| 56 | + return $selectedBucket; |
| 57 | + } |
| 58 | +} |
\ No newline at end of file |
Property changes on: trunk/extensions/ArticleCreationWorkflow/includes/PHPBucket.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 59 | + native |
Index: trunk/extensions/ArticleCreationWorkflow/ArticleCreationWorkflow.php |
— | — | @@ -17,6 +17,7 @@ |
18 | 18 | /* Object model */ |
19 | 19 | $wgAutoloadClasses['ArticleCreationTemplates'] = $articleCreationDir . 'includes/ArticleCreationTemplates.php'; |
20 | 20 | $wgAutoloadClasses['ArticleCreationUtil'] = $articleCreationDir . 'includes/ArticleCreationUtil.php'; |
| 21 | +$wgAutoloadClasses['PHPBucket'] = $articleCreationDir . 'includes/PHPBucket.php'; |
21 | 22 | |
22 | 23 | /* Special Pages */ |
23 | 24 | $wgAutoloadClasses['SpecialArticleCreationLanding'] = $articleCreationDir . 'SpecialArticleCreationLanding.php'; |