<commit_before><?php

require_once 'Site/SiteApplicationModule.php';

/**
 * Web application module for testing alternatives.
 *
 * @package   Site
 * @copyright 2008-2010 silverorange
 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
 * @link      http://code.google.com/apis/analytics/docs/tracking/gaTrackingCustomVariables.html
 */
class SiteAlternativeTestModule extends SiteApplicationModule
{
	// {{{ private properties

	private $tests = array();
	private $slots = array();

	// }}}
	// {{{ public function init()

	public function init()
	{
		foreach ($this->tests as $name => $value) {
			$this->initTest($name);
		}
	}

	// }}}
	// {{{ public function registerTest()

	public function registerTest($name, $default_value, $slot = null)
	{
		$this->tests[$name] = $default_value;

		// Slots are used by GA custom variables.
		if ($slot == null) {
			$slot = (count($this->slots) + 1);
		}

		if ($slot > 5) {
			throw new SiteException('Concurrent test limit of 5 exceeded.');
		}

		$this->slots[$name] = $slot;
	}

	// }}}
	// {{{ public function depends()

	/**
	 * Get the modules this module depends on
	 *
	 * The site alternative test module depends on the SiteCookieModule.
	 *
	 * @return array an array of {@link SiteApplicationModuleDependency}
	 *                        objects defining the features this module
	 *                        depends on.
	 */
	public function depends()
	{
		$depends = parent::depends();
		$depends[] = new SiteApplicationModuleDependency('SiteCookieModule');
		return $depends;
	}

	// }}}
	// {{{ public function __get()

	public function __get($name)
	{
		if (!array_key_exists($name, $this->tests)) {
			throw new SiteException(sprintf(
				"Test '%s' does not exist.", $name));
		}

		// value is stored as string 1 or 0.
		return ($this->tests[$name] == '1' ? true : false);
	}

	// }}}
	// {{{ public function getAll()

	public function getAll()
	{
		return $this->tests;
	}

	// }}}
	// {{{ protected function initTest()

	protected function initTest($name)
	{
		$value       = null;
		$set_cookie  = false;
		$cookie_name = 'test_'.$name;

		if (isset($_GET['alternatetest']) && $_GET['alternatetest'] == $name
			&& isset($_GET['alternatetestversion'])) {
			// Cast as boolean so it matches expected values. This should change
			// if we ever do more than a/b testing.
			$value = (bool) $_GET['alternatetestversion'];
			$set_cookie = true;
		} else {
			if (isset($this->app->cookie->$cookie_name)) {
				$value = $this->app->cookie->$cookie_name;
			} else {
				$value = (mt_rand(0, 1) === 1);
				$set_cookie = true;
			}
		}

		// store value as string since a boolean false would delete the cookie.
		$value = ($value === true) ? '1' : '0';

		if ($set_cookie === true) {
			// Set the cookie for 2 years, just like the google analytics
			// cookie. This is overkill, but matches how we're tracking it in
			// Google Analytics
			$this->app->cookie->setCookie($cookie_name, $value,
				strtotime('+2 Years'));
		}

		$this->tests[$name] = $value;

		if ($this->app->hasModule('SiteAnalyticsModule')) {<commit_msg>Cast value as a boolean from the cookie because it's stored as a string. Otherwise all test value cookies would end up being 'false' due to the strict comparison for setting its value.<commit_after><?php

require_once 'Site/SiteApplicationModule.php';

/**
 * Web application module for testing alternatives.
 *
 * @package   Site
 * @copyright 2008-2010 silverorange
 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
 * @link      http://code.google.com/apis/analytics/docs/tracking/gaTrackingCustomVariables.html
 */
class SiteAlternativeTestModule extends SiteApplicationModule
{
	// {{{ private properties

	private $tests = array();
	private $slots = array();

	// }}}
	// {{{ public function init()

	public function init()
	{
		foreach ($this->tests as $name => $value) {
			$this->initTest($name);
		}
	}

	// }}}
	// {{{ public function registerTest()

	public function registerTest($name, $default_value, $slot = null)
	{
		$this->tests[$name] = $default_value;

		// Slots are used by GA custom variables.
		if ($slot == null) {
			$slot = (count($this->slots) + 1);
		}

		if ($slot > 5) {
			throw new SiteException('Concurrent test limit of 5 exceeded.');
		}

		$this->slots[$name] = $slot;
	}

	// }}}
	// {{{ public function depends()

	/**
	 * Get the modules this module depends on
	 *
	 * The site alternative test module depends on the SiteCookieModule.
	 *
	 * @return array an array of {@link SiteApplicationModuleDependency}
	 *                        objects defining the features this module
	 *                        depends on.
	 */
	public function depends()
	{
		$depends = parent::depends();
		$depends[] = new SiteApplicationModuleDependency('SiteCookieModule');
		return $depends;
	}

	// }}}
	// {{{ public function __get()

	public function __get($name)
	{
		if (!array_key_exists($name, $this->tests)) {
			throw new SiteException(sprintf(
				"Test '%s' does not exist.", $name));
		}

		// value is stored as string 1 or 0.
		return ($this->tests[$name] == '1' ? true : false);
	}

	// }}}
	// {{{ public function getAll()

	public function getAll()
	{
		return $this->tests;
	}

	// }}}
	// {{{ protected function initTest()

	protected function initTest($name)
	{
		$value       = null;
		$set_cookie  = false;
		$cookie_name = 'test_'.$name;

		if (isset($_GET['alternatetest']) && $_GET['alternatetest'] == $name
			&& isset($_GET['alternatetestversion'])) {
			// Cast as boolean so it matches expected values. This should change
			// if we ever do more than a/b testing.
			$value = (bool) $_GET['alternatetestversion'];
			$set_cookie = true;
		} else {
			if (isset($this->app->cookie->$cookie_name)) {
				$value = (bool) $this->app->cookie->$cookie_name;
			} else {
				$value = (mt_rand(0, 1) === 1);
				$set_cookie = true;
			}
		}

		// store value as string since a boolean false would delete the cookie.
		$value = ($value === true) ? '1' : '0';

		if ($set_cookie === true) {
			// Set the cookie for 2 years, just like the google analytics
			// cookie. This is overkill, but matches how we're tracking it in
			// Google Analytics
			$this->app->cookie->setCookie($cookie_name, $value,
				strtotime('+2 Years'));
		}

		$this->tests[$name] = $value;

		if ($this->app->hasModule('SiteAnalyticsModule')) {
{'fraction_non_alphanumeric': 0.1574913981857992, 'fraction_numerical': 0.007194244604316547, 'mean_word_length': 3.1525974025974026, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3084159', 'n_tokens_mistral': 2251, 'n_tokens_neox': 1999, 'n_words': 573}