<?php

if (!defined('VB_ENTRY'))
	die('Access denied.');
/* ======================================================================*\
  || #################################################################### ||
  || # vBulletin 5.0.0 Beta 10
  || # ---------------------------------------------------------------- # ||
  || # Copyright �2000-2012 vBulletin Solutions Inc. All Rights Reserved. ||
  || # This file may not be redistributed in whole or significant part. # ||
  || # ---------------- VBULLETIN IS NOT FREE SOFTWARE ---------------- # ||
  || # http://www.vbulletin.com | http://www.vbulletin.com/license.html # ||
  || #################################################################### ||
  \*====================================================================== */

/**
 * Db Cache Observer
 * Tracks events using vB::$db.
 *
 * @package vBulletin
 * @author vBulletin Development Team
 * @version $Revision: 29424 $
 * @since $Date: 2009-02-02 14:07:13 +0000 (Mon, 02 Feb 2009) $
 * @copyright vBulletin Solutions Inc.
 */
class vB_Cache_Observer_Db extends vB_Cache_Observer
{
	/* Properties==================================================================== */

	/**
	 *
	 * @var vB_dB_Assertor
	 */
	protected $assertor;

	/* Construction================================================================== */
	public function __construct(vB_Cache $cache)
	{
		parent::__construct($cache);
		$this->assertor = & vB::getDbAssertor();
	}

	/* Cache Events================================================================== */

	/**
	 * Notifies observer that a cache entry has been created with event id's.
	 *
	 * @param string $key						- The key of the cache entry
	 * @param array string $events				- Array of associated events
	 */
	public function written($key, $events)
	{
		$this->registerEvents($key, $events);
	}

	/**
	 * Notifies observer that a cache entry was purged.
	 *
	 * @param string $key						- The key of the cache entry that was purged
	 */
	public function purged($key)
	{
		$this->purgeEvents($key);
	}

	/**
	 * Notifies observer of a crud event.
	 *
	 * @param array | string $event						- The id of the crud event
	 */
	public function event($events)
	{
		if (empty($events))
		{
			return;
		}

		$events = (array) $events;
		foreach ($events as $key=>$event)
		{
			$events[$key] = strval($event);
		}

		$this->triggerEvents($events);
	}

	/**
	 * Notifies observer that a cache entry expired.
	 * The db observer has nothing to do.
	 *
	 * @param string $key						- The key of the cache entry that expired
	 */
	public function expired($key)
	{
		
	}

	/**
	 * Notifies observer that the cache was cleaned.
	 * The observer will remove all event associations.
	 */
	public function clean()
	{
		vB::$db->query_write("
			TRUNCATE TABLE " . TABLE_PREFIX . "cacheevent
		");
	}

	/**
	 * Registers all new cache entry -> event associations.
	 */
	protected function registerEvents($key, $events)
	{
		// Register events
		$events = array_unique($events);

		foreach ($events AS $event)
		{
			if (is_array($event))
			{
				foreach ($event as $event_detail)
				{
					$this->assertor->delete('cacheevent', array('cacheid' => $key, 'event' => $event_detail));
					$this->assertor->insert('cacheevent', array('cacheid' => $key, 'event' => $event_detail));
				}
			}
			else
			{
				$this->assertor->delete('cacheevent', array('cacheid' => $key, 'event' => $event));
				$this->assertor->insert('cacheevent', array('cacheid' => $key, 'event' => $event));
			}
		}
	}

	/**
	 * Expires cache entries associated with triggered events.
	 *
	 * @return bool								- Whether any events were triggered
	 */
	protected function triggerEvents($events)
	{
		if (!sizeof($events))
		{
			return;
		}
		// Get affected cache entries
		$result = $this->assertor->getRows('cacheevent', array( vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_SELECT,
			vB_dB_Query::CONDITIONS_KEY => array(
				array('field' => 'event', 'value' => $events)
			)));

		foreach ($result AS $entry)
		{
			$this->cache->expire($entry['cacheid']);
		}

		return true;
	}

	/**
	 * Purges events that are no longer associated.
	 */
	protected function purgeEvents($key)
	{
		$this->assertor->delete('cacheevent', array(array('field'=>'cacheid', 'value'=>$key)));
	}

}

/* ======================================================================*\
  || ####################################################################
  || # 
  || # SVN: $Revision: 28694 $
  || ####################################################################
  \*====================================================================== */
