<?php /* -*- Mode: php; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */

include_once($UTIL_DIR . "/convert.php");
include_once($UTIL_DIR . "/forums.php");

class CacheEntry {
  public $values = array();
  public $id;

  public function write($fp)
  {
    fwrite($fp, "  <entry id=\"" .
           htmlspecialchars($this->id, ENT_QUOTES, "UTF-8") . "\">\n");

    foreach($this->values as $key => $value) {
      fwrite($fp, "    <value key=\"" .
             htmlspecialchars($key, ENT_QUOTES, "UTF-8") . "\">");
      fwrite($fp, htmlspecialchars($value, ENT_QUOTES, "UTF-8"));
      fwrite($fp, "</value>\n");
    }

    fwrite($fp, "  </entry>\n");
  }

  public function value($key)
  {
    if(!isset($this->values[$key])) return "";
    return $this->values[$key];
  }

  public function setValue($key, $value)
  {
    $this->values[$key] = $value;
  }

	public function CacheEntry($id)
	{
    $this->id = $id;
	}
}

class Cache {
  private $file;
	public $entries = array();

  public function rebuild($what = "all")
  {
    global $FORUMS_DIR, $users;


    if($what == "all") $this->entries = array();


    if($what == "forum_numberofthreads" || $what == "all") {
      $entry = new CacheEntry("forum_numberofthreads");
      
      $forums = new Forums($FORUMS_DIR . "/forums.xml");
      foreach($forums->forums as $forum) {
        $threads = new Threads($FORUMS_DIR . "/" . $forum->fid);
        $entry->setValue($forum->fid, sizeof($threads->threads));
      }
      $this->add($entry);
    }


    if($what == "forum_lastpost" || $what == "all") {
      $entry = new CacheEntry("forum_lastpost");

      $forums = new Forums($FORUMS_DIR . "/forums.xml");
      foreach($forums->forums as $forum) {
        $threads = new Threads($FORUMS_DIR . "/" . $forum->fid);

        foreach($users->users as $user) {

        $unread = false;
        foreach($threads->threads as $thread) {
          if($thread->lastseen[$user->uid] < $thread->lastpost) {
            $unread = true;
            break;
          }
        }
        
        $entry->setValue($forum->fid . "-" . $user->uid, $unread);
        }

      }
      $this->add($entry);
    }


    $this->write();
  }

	public function add($entry) {
    $key = $entry->id;
		$this->entries[$key] = $entry;
	}
	
	public function write()
	{
 		$fp = fopen($this->file, "w");

		$block = TRUE;
		flock($fp, LOCK_EX, $block); // do an exclusive lock

		fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

		fwrite($fp, "<cache>\n");
		foreach($this->entries as $entry) {
      $entry->write($fp);
		}
		fwrite($fp, "</cache>\n");

		fclose($fp);
	}

	public function get($id)
	{
    return $this->entries[$id];
	}

	private function read()
	{
    $dom = new DomDocument;
    $dom->preserveWhiteSpace = FALSE;
    $dom->load($this->file);
    $ces = $dom->getElementsByTagName('entry');
			
    foreach($ces as $c) {
      $entry = new CacheEntry($c->getAttribute('id'));
      foreach($c->childNodes as $v) {
        if($v->tagName != "value") continue;
        $entry->setValue($v->getAttribute('key'), $v->textContent);
      }

      $this->add($entry);
    }
	}

	public function Cache($file)
	{
		$this->file = $file;
    if(file_exists($this->file)) $this->read();
	}

}
?>