From 42c62cb041a2b2b95c9cda0c20fcf70334fd244c Mon Sep 17 00:00:00 2001 From: deva Date: Tue, 1 Nov 2011 09:32:21 +0000 Subject: Rewl og krat... needs cleaning up - after port to GIT. --- forum/utils/cache.php | 147 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 forum/utils/cache.php (limited to 'forum/utils/cache.php') diff --git a/forum/utils/cache.php b/forum/utils/cache.php new file mode 100644 index 0000000..42e4ca0 --- /dev/null +++ b/forum/utils/cache.php @@ -0,0 +1,147 @@ +id, ENT_QUOTES, "UTF-8") . "\">\n"); + + foreach($this->values as $key => $value) { + fwrite($fp, " "); + fwrite($fp, htmlspecialchars($value, ENT_QUOTES, "UTF-8")); + fwrite($fp, "\n"); + } + + fwrite($fp, " \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, "\n"); + + fwrite($fp, "\n"); + foreach($this->entries as $entry) { + $entry->write($fp); + } + fwrite($fp, "\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(); + } + +} +?> -- cgit v1.2.3