r111111 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111110‎ | r111111 | r111112 >
Date:22:16, 9 February 2012
Author:werdna
Status:ok (Comments)
Tags:
Comment:
Add new PHPBucket class, pure PHP implementation of mediawiki.user.bucket
Modified paths:
  • /trunk/extensions/ArticleCreationWorkflow/ArticleCreationWorkflow.php (modified) (history)
  • /trunk/extensions/ArticleCreationWorkflow/includes/PHPBucket.php (added) (history)

Diff [purge]

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
159 + native
Index: trunk/extensions/ArticleCreationWorkflow/ArticleCreationWorkflow.php
@@ -17,6 +17,7 @@
1818 /* Object model */
1919 $wgAutoloadClasses['ArticleCreationTemplates'] = $articleCreationDir . 'includes/ArticleCreationTemplates.php';
2020 $wgAutoloadClasses['ArticleCreationUtil'] = $articleCreationDir . 'includes/ArticleCreationUtil.php';
 21+$wgAutoloadClasses['PHPBucket'] = $articleCreationDir . 'includes/PHPBucket.php';
2122
2223 /* Special Pages */
2324 $wgAutoloadClasses['SpecialArticleCreationLanding'] = $articleCreationDir . 'SpecialArticleCreationLanding.php';

Comments

#Comment by Jeroen De Dauw (talk | contribs)   01:22, 10 February 2012

111111! o_O

#Comment by Raindrift (talk | contribs)   01:57, 17 February 2012

This seems like it should move to core for 1.20.

Status & tagging log