<commit_before><?php

require_once 'XML/RPC2/Client.php';
require_once 'Site/SiteMailingList.php';
require_once 'Site/SiteMailChimpCampaign.php';

/**
 * @package   Site
 * @copyright 2009-2010 silverorange
 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
 */
// TODO: handle addresses somehow magically, perhaps add type checking on merge
// vars, and allow zip to be passed into an address field by filling with
// placeholder data in the other address columns (as suggested by mailchimp).
class SiteMailChimpList extends SiteMailingList
{
	// {{{ class constants

	/**
	 * Error code returned when attempting to subscribe an email address that
	 * has previously unsubscribed. We can't programatically resubscribe them,
	 * MailChimp requires them to resubscribe out of their own volition.
	 */
	const PREVIOUSLY_UNSUBSCRIBED_ERROR_CODE = 212;

	/**
	 * Error code returned when attempting to subscribe an email address that
	 * has bounced in the past, so can't be resubscribed.
	 */
	const BOUNCED_ERROR_CODE = 213;

	/**
	 * Error code returned when attempting to unsubscribe an email address that
	 * is not a current member of the list.
	 */
	const NOT_SUBSCRIBED_ERROR_CODE = 215;

	/**
	 * Error code returned when attempting to unsubscribe an email address that
	 * was never a member of the list.
	 */
	const NOT_FOUND_ERROR_CODE = 232;

	/**
	 * Error code returned when attempting to subscribe an email address that
	 * is not a valid email address.
	 */
	const INVALID_ADDRESS_ERROR_CODE = 502;

	/**
	 * Error code returned when the request timeouts.
	 */
	const TIMEOUT_ERROR_CODE = 28;

	// }}}
	// {{{ protected properties

	protected $client;
	protected $list_merge_array_map = array();
	protected $default_address = array(
		'addr1'   => '',
		'addr2'   => '',
		'city'    => '',
		'state'   => '',
		'zip'     => '',
		'country' => '',
		);

	// }}}
	// {{{ public function __construct()

	public function __construct(SiteApplication $app, $shortname = null)
	{
		parent::__construct($app, $shortname);

		// if the connection takes longer than 5s timeout. This will prevent
		// users from waiting too long when MailChimp is down - requests will
		// just get queued. Without setting this, the timeout is ~90s
		$client_options = array(
			'connectionTimeout' => 5000,
		);

		$this->client = XML_RPC2_Client::create(
			$this->app->config->mail_chimp->api_url, $client_options);

		if ($this->shortname === null)
			$this->shortname = $app->config->mail_chimp->default_list;

		$this->initListMergeArrayMap();
	}

	// }}}
	// {{{ public function isAvailable()

	/**
	 * Tests to make sure the service is available.
	 *
	 * Returns false if MailChimp returns an unexpected value or the
	 * XML_RPC2_Client throws an exception. Unexpected values from MailChimp
	 * get thrown in exceptions as well. Any exceptions thrown are not exited
	 * on, so that we can queue requests based on service availability.
	 *
	 * @return boolean whether or not the service is available.
	 */
	public function isAvailable()
	{
		$available = false;

		try {
			$result = $this->client->ping(
				$this->app->config->mail_chimp->api_key);

			// Endearing? Yes. But also annoying to have to check for a string.
			if ($result === "Everything's Chimpy!") {
				$available = true;
			} else {
				// throw whatever the chimp has given us back
				$e = new SiteException($result);
				$e->log();
			}
		} catch (XML_RPC2_CurlException $e) {
			// ignore timeout exceptions.
			if ($e->getCode() !== self::TIMEOUT_ERROR_CODE) {
				$e = new SiteException($e);
				$e->log();
			}
		} catch (Exception $e) {
			$e = new SiteException($e);
			$e->log();
		}<commit_msg>make the constant name slightly more accurate.<commit_after><?php

require_once 'XML/RPC2/Client.php';
require_once 'Site/SiteMailingList.php';
require_once 'Site/SiteMailChimpCampaign.php';

/**
 * @package   Site
 * @copyright 2009-2010 silverorange
 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
 */
// TODO: handle addresses somehow magically, perhaps add type checking on merge
// vars, and allow zip to be passed into an address field by filling with
// placeholder data in the other address columns (as suggested by mailchimp).
class SiteMailChimpList extends SiteMailingList
{
	// {{{ class constants

	/**
	 * Error code returned when attempting to subscribe an email address that
	 * has previously unsubscribed. We can't programatically resubscribe them,
	 * MailChimp requires them to resubscribe out of their own volition.
	 */
	const PREVIOUSLY_UNSUBSCRIBED_ERROR_CODE = 212;

	/**
	 * Error code returned when attempting to subscribe an email address that
	 * has bounced in the past, so can't be resubscribed.
	 */
	const BOUNCED_ERROR_CODE = 213;

	/**
	 * Error code returned when attempting to unsubscribe an email address that
	 * is not a current member of the list.
	 */
	const NOT_SUBSCRIBED_ERROR_CODE = 215;

	/**
	 * Error code returned when attempting to unsubscribe an email address that
	 * was never a member of the list.
	 */
	const NOT_FOUND_ERROR_CODE = 232;

	/**
	 * Error code returned when attempting to subscribe an email address that
	 * is not a valid email address.
	 */
	const INVALID_ADDRESS_ERROR_CODE = 502;

	/**
	 * Error code returned when the request timeouts.
	 */
	const CURL_TIMEOUT_ERROR_CODE = 28;

	// }}}
	// {{{ protected properties

	protected $client;
	protected $list_merge_array_map = array();
	protected $default_address = array(
		'addr1'   => '',
		'addr2'   => '',
		'city'    => '',
		'state'   => '',
		'zip'     => '',
		'country' => '',
		);

	// }}}
	// {{{ public function __construct()

	public function __construct(SiteApplication $app, $shortname = null)
	{
		parent::__construct($app, $shortname);

		// if the connection takes longer than 5s timeout. This will prevent
		// users from waiting too long when MailChimp is down - requests will
		// just get queued. Without setting this, the timeout is ~90s
		$client_options = array(
			'connectionTimeout' => 5000,
		);

		$this->client = XML_RPC2_Client::create(
			$this->app->config->mail_chimp->api_url, $client_options);

		if ($this->shortname === null)
			$this->shortname = $app->config->mail_chimp->default_list;

		$this->initListMergeArrayMap();
	}

	// }}}
	// {{{ public function isAvailable()

	/**
	 * Tests to make sure the service is available.
	 *
	 * Returns false if MailChimp returns an unexpected value or the
	 * XML_RPC2_Client throws an exception. Unexpected values from MailChimp
	 * get thrown in exceptions as well. Any exceptions thrown are not exited
	 * on, so that we can queue requests based on service availability.
	 *
	 * @return boolean whether or not the service is available.
	 */
	public function isAvailable()
	{
		$available = false;

		try {
			$result = $this->client->ping(
				$this->app->config->mail_chimp->api_key);

			// Endearing? Yes. But also annoying to have to check for a string.
			if ($result === "Everything's Chimpy!") {
				$available = true;
			} else {
				// throw whatever the chimp has given us back
				$e = new SiteException($result);
				$e->log();
			}
		} catch (XML_RPC2_CurlException $e) {
			// ignore timeout exceptions.
			if ($e->getCode() !== self::CURL_TIMEOUT_ERROR_CODE) {
				$e = new SiteException($e);
				$e->log();
			}
		} catch (Exception $e) {
			$e = new SiteException($e);
			$e->log();
		}
{'fraction_non_alphanumeric': 0.11605337646583097, 'fraction_numerical': 0.010783124410297884, 'mean_word_length': 3.44311377245509, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 59, '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': '485755', 'n_tokens_mistral': 2506, 'n_tokens_neox': 2198, 'n_words': 768}