<commit_before><?php

require_once 'Site/SiteApplicationModule.php';
require_once 'Swat/SwatDate.php';
require_once 'SwatDB/SwatDB.php';
require_once 'SwatDB/SwatDBClassMap.php';
require_once 'Site/dataobjects/SiteAd.php';
require_once 'Site/dataobjects/SiteAdReferrer.php';

/**
 * Web application module for handling site analytics and ad tracking
 *
 * @package   Site
 * @copyright 2007 silverorange
 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
 */
class SiteAnalyticsModule extends SiteApplicationModule
{
	// {{{ protected properties

	/**
	 * The name of the inbound ad tracking URI parameter
	 *
	 * Defaults to 'ad'. Google Analytics users may want to change this to
	 * 'utm_source' or 'utm_campaign'.
	 *
	 * @var string
	 *
	 * @see SiteAnalyticsModule::setInboundTrackingId()
	 * @see SiteAnalyticsModule::cleanInboundTrackingUri()
	 */
	protected $inbound_tracking_id = 'ad';

	/**
	 * Whether or not to automaticlaly clean the inbound tracking id from URIs
	 *
	 * Defaults to false.
	 *
	 * @var boolean
	 *
	 * @see SiteAnalyticsModule::setAutocleanUri()
	 */
	protected $autoclean_uri = false;

	/**
	 * Whether or not to automaticlaly create Ad objects for unknown inbound
	 * tracking ids
	 *
	 * Defaults to false.
	 *
	 * @var boolean
	 *
	 * @see SiteAnalyticsModule::setAutocreateAd()
	 */
	protected $autocreate_ad = false;

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

	/**
	 * Initializes this analytics module
	 *
	 * Stores ad object in the session module and initializes ad database
	 * reference.
	 */
	public function init()
	{
		$this->initAd();

		// set database on ad in session if it exists
		$ad = $this->getAd();
		if ($ad !== null) {
			$db = $this->app->getModule('SiteDatabaseModule')->getConnection();
			$ad->setDatabase($db);
		}
	}

	// }}}
	// {{{ public function getAd()

	/**
	 * Gets the tracked inbound ad for the current session
	 *
	 * @return SiteAd the tracked inbound ad for the current session or null
	 *                 if there is no tracked inbound ad for the current
	 *                 session.
	 */
	public function getAd()
	{
		$session = $this->app->getModule('SiteSessionModule');
		return (isset($session->ad)) ? $session->ad : null;
	}

	// }}}
	// {{{ public function clearAd()

	/**
	 * Clears the tracked inbound ad from the current session
	 */
	public function clearAd()
	{
		$session = $this->app->getModule('SiteSessionModule');
		unset($session->ad);
	}

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

	/**
	 * Gets the module features this module depends on
	 *
	 * The site analytics module depends on the SiteSessionModule and
	 * SiteDatabaseModule features.
	 *
	 * @return array an array of {@link SiteApplicationModuleDependency}
	 *                        objects defining the features this module
	 *                        depends on.
	 */
	public function depends()
	{
		$depends = parent::depends();
		$depends[] = new SiteApplicationModuleDependency('SiteDatabaseModule');
		$depends[] = new SiteApplicationModuleDependency('SiteSessionModule');
		return $depends;
	}

	// }}}
	// {{{ public function cleanInboundTrackingUri()

	/**
	 * Removes inbound ad tracking id from the URI and relocates to a clean
	 * URI
	 *
	 * @param SiteAd $ad the inbound tracking ad to clean from the URI.
	 */
	public function cleanInboundTrackingUri(SiteAd $ad)
	{
		$regexp = sprintf('/&?\??%s=%s/u',
			preg_quote($this->inbound_tracking_id, '/'),
			preg_quote($ad->shortname, '/'));

		// Site URI may not have been parsed yet if autocleaning is used. This
		// must be a full URI as a result.
		$uri = preg_replace($regexp, '', $_SERVER['REQUEST_URI']);
		$this->app->relocate($uri);
	}

	// }}}
	// {{{ public function setInboundTrackingId()

	/**
	 * Sets the name of the URI parameter used for tracking inbound ads
	 *
	 * @param string the name of the URI parameter used for tracking inbound
	 *                ads. Example: 'utm_source'.
	 */
	public function setInboundTrackingId($id)
	{
		$this->inbound_tracking_id = (string)$id;
	}

	// }}}
	// {{{ public function setAutocleanUri()

	/**
	 * Sets whether or not inbound tracking ids should be automatically cleaned
	 * from URIs
	 *
	 * @param boolean $clean optional. True if inbound tracking ids should be
	 *                        automatically cleaned from URIs and false if they<commit_msg>Better regular expression replace for stripping ad token from URI in SiteAnalyticsModule.<commit_after><?php

require_once 'Site/SiteApplicationModule.php';
require_once 'Swat/SwatDate.php';
require_once 'SwatDB/SwatDB.php';
require_once 'SwatDB/SwatDBClassMap.php';
require_once 'Site/dataobjects/SiteAd.php';
require_once 'Site/dataobjects/SiteAdReferrer.php';

/**
 * Web application module for handling site analytics and ad tracking
 *
 * @package   Site
 * @copyright 2007 silverorange
 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
 */
class SiteAnalyticsModule extends SiteApplicationModule
{
	// {{{ protected properties

	/**
	 * The name of the inbound ad tracking URI parameter
	 *
	 * Defaults to 'ad'. Google Analytics users may want to change this to
	 * 'utm_source' or 'utm_campaign'.
	 *
	 * @var string
	 *
	 * @see SiteAnalyticsModule::setInboundTrackingId()
	 * @see SiteAnalyticsModule::cleanInboundTrackingUri()
	 */
	protected $inbound_tracking_id = 'ad';

	/**
	 * Whether or not to automaticlaly clean the inbound tracking id from URIs
	 *
	 * Defaults to false.
	 *
	 * @var boolean
	 *
	 * @see SiteAnalyticsModule::setAutocleanUri()
	 */
	protected $autoclean_uri = false;

	/**
	 * Whether or not to automaticlaly create Ad objects for unknown inbound
	 * tracking ids
	 *
	 * Defaults to false.
	 *
	 * @var boolean
	 *
	 * @see SiteAnalyticsModule::setAutocreateAd()
	 */
	protected $autocreate_ad = false;

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

	/**
	 * Initializes this analytics module
	 *
	 * Stores ad object in the session module and initializes ad database
	 * reference.
	 */
	public function init()
	{
		$this->initAd();

		// set database on ad in session if it exists
		$ad = $this->getAd();
		if ($ad !== null) {
			$db = $this->app->getModule('SiteDatabaseModule')->getConnection();
			$ad->setDatabase($db);
		}
	}

	// }}}
	// {{{ public function getAd()

	/**
	 * Gets the tracked inbound ad for the current session
	 *
	 * @return SiteAd the tracked inbound ad for the current session or null
	 *                 if there is no tracked inbound ad for the current
	 *                 session.
	 */
	public function getAd()
	{
		$session = $this->app->getModule('SiteSessionModule');
		return (isset($session->ad)) ? $session->ad : null;
	}

	// }}}
	// {{{ public function clearAd()

	/**
	 * Clears the tracked inbound ad from the current session
	 */
	public function clearAd()
	{
		$session = $this->app->getModule('SiteSessionModule');
		unset($session->ad);
	}

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

	/**
	 * Gets the module features this module depends on
	 *
	 * The site analytics module depends on the SiteSessionModule and
	 * SiteDatabaseModule features.
	 *
	 * @return array an array of {@link SiteApplicationModuleDependency}
	 *                        objects defining the features this module
	 *                        depends on.
	 */
	public function depends()
	{
		$depends = parent::depends();
		$depends[] = new SiteApplicationModuleDependency('SiteDatabaseModule');
		$depends[] = new SiteApplicationModuleDependency('SiteSessionModule');
		return $depends;
	}

	// }}}
	// {{{ public function cleanInboundTrackingUri()

	/**
	 * Removes inbound ad tracking id from the URI and relocates to a clean
	 * URI
	 *
	 * @param SiteAd $ad the inbound tracking ad to clean from the URI.
	 */
	public function cleanInboundTrackingUri(SiteAd $ad)
	{
		$regexp = sprintf(
			'/
			(?:
				(\?)%1$s=%2$s& # ad starts query string
				|
				\?%1$s=%2$s$   # ad is entire query string
				|
				&%1$s=%2$s&    # ad is embedded in query string
				|
				&%1$s=%2$s$    # ad ends query string
			)
			/xu',
			preg_quote($this->inbound_tracking_id, '/'),
			preg_quote($ad->shortname, '/'));

		// Site URI may not have been parsed yet if autocleaning is used. This
		// must be a full URI as a result.
		$uri = preg_replace($regexp, '\1', $_SERVER['REQUEST_URI']);

		// relocate to cleaned URI
		$this->app->relocate($uri);
	}

	// }}}
	// {{{ public function setInboundTrackingId()

	/**
	 * Sets the name of the URI parameter used for tracking inbound ads
	 *
	 * @param string the name of the URI parameter used for tracking inbound
	 *                ads. Example: 'utm_source'.
	 */
	public function setInboundTrackingId($id)
	{
		$this->inbound_tracking_id = (string)$id;
	}

	// }}}
	// {{{ public function setAutocleanUri()

	/**
	 * Sets whether or not inbound tracking ids should be automatically cleaned
	 * from URIs
	 *
	 * @param boolean $clean optional. True if inbound tracking ids should be
	 *                        automatically cleaned from URIs and false if they
{'fraction_non_alphanumeric': 0.12289450354609929, 'fraction_numerical': 0.002327127659574468, 'mean_word_length': 3.1456132292145154, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, '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': '2984618', 'n_tokens_mistral': 2962, 'n_tokens_neox': 2668, 'n_words': 841}