[Phpfreechat-svn] SF.net SVN: phpfreechat: [770] trunk
Status: Beta
Brought to you by:
kerphi
From: <ke...@us...> - 2006-09-19 15:26:05
|
Revision: 770 http://svn.sourceforge.net/phpfreechat/?rev=770&view=rev Author: kerphi Date: 2006-09-19 08:25:50 -0700 (Tue, 19 Sep 2006) Log Message: ----------- Work in progress: datamodel refactoring (don't checkout this revision it doesn't work) Modified Paths: -------------- trunk/src/commands/leave.class.php trunk/src/containers/file.class.php trunk/testcase/container_file.php trunk/testcase/container_generic.php Modified: trunk/src/commands/leave.class.php =================================================================== --- trunk/src/commands/leave.class.php 2006-09-19 06:51:13 UTC (rev 769) +++ trunk/src/commands/leave.class.php 2006-09-19 15:25:50 UTC (rev 770) @@ -72,7 +72,7 @@ // remove the nickname from the channel/pv $container =& $c->getContainerInstance(); - $container->removeNick($leave_recip, $u->nick); + $container->removeNick($chan, $u->nickid); // return ok to the client // then the client will remove the channel' tab Modified: trunk/src/containers/file.class.php =================================================================== --- trunk/src/containers/file.class.php 2006-09-19 06:51:13 UTC (rev 769) +++ trunk/src/containers/file.class.php 2006-09-19 15:25:50 UTC (rev 770) @@ -92,61 +92,17 @@ { $c =& $this->c; - // store nickid -> nickname and nickname -> nickid correspondance - $this->setMeta($nick, "nickname", "fromnickid", $nickid); - $this->setMeta($nickid, "nickid", "fromnickname", $nick); + if ($chan == NULL) $chan = 'SERVER'; - $this->_registerUserMeta($nickid, $chan); + $this->setMeta2("nickid-to-metadata", $nickid, 'nick', $nick); + $this->setMeta2("metadata-to-nickid", 'nick', $this->_encode($nick), $nickid); - if ($c->debug) pxlog("createNick - nickname metadata created: chan=".($chan==NULL?"SERVER":$chan)." nickid=".$nickid, "chat", $c->getId()); + $this->setMeta2("nickid-to-channelid", $nickid, $this->_encode($chan)); + $this->setMeta2("channelid-to-nickid", $this->_encode($chan), $nickid); - /* - // increment the nick references (used to know when the nick is really disconnected) - $nick_ref = $this->getMeta("references", $nickid); - if ($nick_ref == NULL || !is_numeric($nick_ref)) $nick_ref = 0; - $nick_ref++; - $this->setMeta($nick_ref, "references", $nickid); - */ - + // update the SERVER channel + $this->updateNick($nickid); - $c =& $this->c; - $nick_dir = ($chan != NULL) ? - $c->container_cfg_channel_dir."/".$this->_encode($chan)."/nicknames" : - $c->container_cfg_server_dir."/nicknames"; - - // check the nickname directory exists - $errors = @test_writable_dir($nick_dir, $chan."/nicknames/".$nick); - if ($c->debug) - { - if (count($errors)>0) - pxlog("createNick(".$nick.", ".$nickid.") - Error: ".var_export($errors,true), "chat", $c->getId()); - } - - $nickid_filename = $nick_dir."/".$nickid; //$this->_encode($nick); - - // check the if the file exists only in debug mode! - if ($c->debug) - { - /* - if (file_exists($nickid_filename)) - pxlog("createNick(".$nick.", ".$nickid.") - Error: another nickname data file exists, we are overwriting it (nickname takeover)!: ".$nickid_filename, "chat", $c->getId()); - else - pxlog("createNick - nickname file created: chan=".($chan==NULL?"SERVER":$chan)." nickid=".$nickid, "chat", $c->getId()); - */ - } - - // trust the caller : this nick is not used - touch($nickid_filename); - - // append the nickname to the cached nickname list - $id = $this->isNickOnline($chan, $nick); - $_chan = ($chan == NULL) ? "SERVER" : $chan; - if ($id<0) - { - $this->_users[$_chan]["nickid"][] = $nickid; - $this->_users[$_chan]["timestamp"][] = filemtime($nickid_filename); - } - return true; } @@ -157,68 +113,49 @@ * @param $nick the nickname to remove * @return true if the nickname was correctly removed */ - function removeNick($chan, $nick) + function removeNick($chan, $nickid) { - // retrive the nickid to remove - $nickid = $this->getNickId($nick); - if ($nickid == "undefined") return false; + if ($chan == NULL) $chan = 'SERVER'; - $c =& $this->c; - $nick_dir = ($chan != NULL) ? - $c->container_cfg_channel_dir."/".$this->_encode($chan)."/nicknames" : - $c->container_cfg_server_dir."/nicknames"; - $nickid_filename = $nick_dir."/".$nickid; //$this->_encode($nick); + $ret = $this->getMeta2("channelid-to-nickid", $this->_encode('SERVER'), $nickid); + $timestamp = $ret["timestamp"][0]; + + $deleted_user = array(); + $deleted_user["nick"][] = $this->getNickname($nickid); + $deleted_user["nickid"][] = $nickid; + $deleted_user["timestamp"][] = $timestamp; - if ($c->debug) - { - // @todo: check if the removed nick is mine in debug mode! - - // check the nickname file really exists - if (!file_exists($nickid_filename)) - pxlog("removeNick(".$nick.") - Error: the nickname data file to remove doesn't exists: ".$nickid_filename, "chat", $c->getId()); - else - pxlog("removeNick - nickname file removed: chan=".($chan==NULL?"SERVER":$chan)." nickid=".$nickid, "chat", $c->getId()); - } - $ok = @unlink($nickid_filename); + // @todo ne supprimer l'utilisateur que du channel donne en parametres + // car la commande /leave va simplement supprimer l'utilisateur du channel courant + // il faut par contre faire un test sur les channels de l'utilisateur et dans le cas ou l'utilisateur + // est deconnecte du dernier channel (il se peut que ce soit SERVER) alors on supprime ses metadata. - // remove the user metadata if he is disconnected from the server + // il faudrait egalement adapter removeObsoleteNick pour qu'elle appel N fois removeNick + // N etant le nombre de channel de l'utilisateur. Ainsi l'utilisateur dera vraiment deconnecte - $this->_unregisterUserMeta($nickid, $chan); - /* - // decrement the nick references and kill the metadata if not more references is found - // (used to know when the nick is really disconnected) - $nick_ref = $this->getMeta("references", $nickid); - if ($nick_ref == NULL || !is_numeric($nick_ref)) $nick_ref = 0; - $nick_ref--; - if ($nick_ref <= 0) - { - $this->rmMeta("nickid", "fromnickname", $nick); - $this->rmMeta("nickname", "fromnickid", $nickid); - $this->rmMeta("references", $nickid); // destroy also the reference counter (by default its value is 0) - } - else - $this->setMeta($nick_ref, "references", $nickid); - */ - if ($c->debug) - { - // check the nickname file is correctly deleted - if (file_exists($nickid_filename)) - pxlog("removeNick(".$nick.") - Error: the nickname data file yet exists", "chat", $c->getId()); - } + // get the user's disconnected channels + $channels = array(); + $ret2 = $this->getMeta2("nickid-to-channelid",$nickid); + foreach($ret2["value"] as $v) + $channels[] = $this->_decode($v); + $deleted_user["channels"][] = $channels; - // remove the nickname from the cache list - $id = $this->isNickOnline($chan, $nick); - $_chan = ($chan == NULL) ? "SERVER" : $chan; - if ($id >= 0) + // get the user nickname + $nick = $this->getNickname($nickid); + // loop on user channels + foreach($channels as $ch) { - unset($this->_users[$_chan]["nickid"][$id]); - unset($this->_users[$_chan]["timestamp"][$id]); + // remove the nickname to nickid correspondance + $this->rmMeta2("metadata-to-nickid", 'nick', $this->_encode($nick)); + // remove disconnected nickname metadata + $this->rmMeta2("nickid-to-metadata", $nickid); + // remove the nickid from the channel list + $this->rmMeta2("channelid-to-nickid", $this->_encode($ch), $nickid); } - - return $ok; + return $deleted_user; } /** @@ -227,46 +164,15 @@ * @param $chan where to update the nick, if null then update the server nick * @param $nick nickname to update (raw nickname) */ - function updateNick($chan, $nick) + function updateNick($nickid) { - // retrive the nickid to update - $nickid = $this->getNickId($nick); - if ($nickid == "undefined") return false; - - // update the user metadata - $this->_registerUserMeta($nickid, $chan); - $c =& $this->c; - $there = false; - - $nick_dir = ($chan != NULL) ? - $c->container_cfg_channel_dir."/".$this->_encode($chan)."/nicknames" : - $c->container_cfg_server_dir."/nicknames"; - if (!is_dir($nick_dir)) mkdir_r($nick_dir); - - // update my online status file - $nickid_filename = $nick_dir."/".$nickid; //$this->_encode($nick); - if (file_exists($nickid_filename)) $there = true; - @touch($nickid_filename); - @chmod($nickid_filename, 0700); - if ($c->debug) pxlog("updateNick - nickname file updated: chan=".($chan==NULL?"SERVER":$chan)." nickid=".$nickid, "chat", $c->getId()); - - // append the nickname to the cache list - $_chan = ($chan == NULL) ? "SERVER" : $chan; - $id = $this->isNickOnline($chan, $nick); - if ($id < 0) - { - $this->_users[$_chan]["nickid"][] = $nickid; - $this->_users[$_chan]["timestamp"][] = filemtime($nickid_filename); - } - else - { - // just update the timestamp if the nickname is allready present in the cached list - $this->_users[$_chan]["timestamp"][$id] = filemtime($nickid_filename); - } - - return $there; + $chan = 'SERVER'; + + $this->setMeta2("nickid-to-channelid", $nickid, $this->_encode($chan)); + $this->setMeta2("channelid-to-nickid", $this->_encode($chan), $nickid); + return true; } /** @@ -278,41 +184,19 @@ */ function changeNick($newnick, $oldnick) { - $oldnickid = $this->getNickId($oldnick); - $newnickid = $this->getNickId($newnick); - if ($oldnickid == "undefined") return false; // the oldnick must be connected - if ($newnickid != "undefined") return false; // the newnick must not be inuse - - $this->rmMeta("nickid", "fromnickname", $oldnick); // remove the oldnickname -> oldnickid association - $this->setMeta($newnick, "nickname", "fromnickid", $oldnickid); - $this->setMeta($oldnickid, "nickid", "fromnickname", $newnick); - - /* $c =& $this->c; - $nick_dir = ($chan != NULL) ? - $c->container_cfg_channel_dir."/".$this->_encode($chan)."/nicknames" : - $c->container_cfg_server_dir."/nicknames"; - // $newnickid_filename = $nick_dir."/".$this->_encode($newnick); - $oldnickid_filename = $nick_dir."/".$oldnickid; //$this->_encode($oldnick); - - $ok = @rename($oldnick_filename, $newnick_filename); - */ - // update the nick cache list + $oldnickid = $this->getNickId($oldnick); + $newnickid = $this->getNickId($newnick); + if ($oldnickid == "") return false; // the oldnick must be connected + if ($newnickid != "") return false; // the newnick must not be inuse - //if($ok) - /* - { - $_chan = ($chan == NULL) ? "SERVER" : $chan; - $id = $this->isNickOnline($chan, $oldnick); - if ($id >= 0) - { - $this->_users[$_chan][$id]["nick"] = $newnick; - $this->_users[$_chan][$id]["timestamp"] = filemtime($newnick_filename); - } - } - */ + // remove the oldnick to oldnickid correspondance + $this->rmMeta2("metadata-to-nickid", 'nick', $this->_encode($oldnick)); + // update the nickname + $this->setMeta2("nickid-to-metadata", $oldnickid, 'nick', $newnick); + $this->setMeta2("metadata-to-nickid", 'nick', $this->_encode($newnick), $oldnickid); return true; } @@ -324,8 +208,8 @@ */ function getNickId($nick) { - $nickid = $this->getMeta("nickid", "fromnickname", $nick); - if ($nickid == NULL) $nickid = "undefined"; + $nickid = $this->getMeta2("metadata-to-nickid", 'nick', $this->_encode($nick), true); + $nickid = isset($nickid["value"][0]) ? $nickid["value"][0] : ""; return $nickid; } @@ -336,8 +220,8 @@ */ function getNickname($nickid) { - $nick = $this->getMeta("nickname", "fromnickid", $nickid); - if ($nick == NULL) $nick = ""; + $nick = $this->getMeta2("nickid-to-metadata", $nickid, 'nick', true); + $nick = isset($nick["value"][0]) ? $nick["value"][0] : ""; return $nick; } @@ -348,70 +232,31 @@ * @param $timeout * @return array("nickid"=>array("nickid1", ...),"timestamp"=>array(timestamp1, ...)) contains all disconnected nickids and there timestamp */ - function removeObsoleteNick($chan, $timeout) + function removeObsoleteNick($timeout) { $c =& $this->c; - $nick_dir = ($chan != NULL) ? - $c->container_cfg_channel_dir."/".$this->_encode($chan)."/nicknames" : - $c->container_cfg_server_dir."/nicknames"; - // check the nickname directory exists - $errors = @test_writable_dir($nick_dir, $chan."/nicknames"); - - $deleted_user = array(); - $online_user = array(); - $dir_handle = opendir($nick_dir); - while (false !== ($file = readdir($dir_handle))) - { - if ($file == "." || $file == "..") continue; // skip . and .. generic files - $f_time = filemtime($nick_dir."/".$file); - if (time() > ($f_time+$timeout/1000) ) // user will be disconnected after 'timeout' secondes of inactivity - { - $deleted_user["nick"][] = $this->getNickname($file); - $deleted_user["nickid"][] = $file; - $deleted_user["timestamp"][] = $f_time; - @unlink($nick_dir."/".$file); // disconnect expired user - } - else - { - // optimisation: cache user list for next getOnlineNick call - $online_user["nickid"][] = $file; - $online_user["timestamp"][] = $f_time; - } - } + $chan = 'SERVER'; - // remove the user metadata if he is disconnected from the server - if (isset($deleted_user["nickid"]) && count($deleted_user["nickid"])>0) + $deleted_user = array('nick'=>array(), + 'nickid'=>array(), + 'timestamp'=>array(), + 'channels'=>array()); + $ret = $this->getMeta2("channelid-to-nickid", $this->_encode($chan)); + for($i = 0; $i<count($ret['timestamp']); $i++) { - foreach($deleted_user["nickid"] as $du_nid) + $timestamp = $ret['timestamp'][$i]; + $nickid = $ret['value'][$i]; + if (time() > ($timestamp+$timeout/1000) ) // user will be disconnected after 'timeout' secondes of inactivity { - $du_nickid = $du_nid; - $du_nickname = $this->getNickname($du_nid); - - $this->_unregisterUserMeta($du_nickid, $chan); - - /* - // decrement the nick references and kill the metadata if not more references is found - // (used to know when the nick is really disconnected) - $nick_ref = $this->getMeta("references", $du_nickid); - if ($nick_ref == NULL || !is_numeric($nick_ref)) $nick_ref = 0; - $nick_ref--; - if ($nick_ref <= 0) - { - $this->rmMeta("nickid", "fromnickname", $du_nickname); - $this->rmMeta("nickname", "fromnickid", $du_nickid); - $this->rmMeta("references", $du_nickid); // destroy also the reference counter (by default its value is 0) - } - else - $this->setMeta($nick_ref, "references", $du_nickid); - */ + $du = $this->removeNick($nickid); + $deleted_user["nick"] = array_merge($deleted_user["nick"], $du["nick"]); + $deleted_user["nickid"] = array_merge($deleted_user["nickid"], $du["nickid"]); + $deleted_user["timestamp"] = array_merge($deleted_user["timestamp"], $du["timestamp"]); + $deleted_user["channels"] = array_merge($deleted_user["channels"], $du["channels"]); } } - - // cache the updated user list - $_chan = ($chan == NULL) ? "SERVER" : $chan; - $this->_users[$_chan] =& $online_user; - + return $deleted_user; } @@ -422,31 +267,25 @@ */ function getOnlineNick($chan) { - // return the cached user list if it exists - $_chan = ($chan == NULL) ? "SERVER" : $chan; - if (isset($this->_users[$_chan]) && is_array($this->_users[$_chan])) - return $this->_users[$_chan]; - $c =& $this->c; - - $nick_dir = ($chan != NULL) ? - $c->container_cfg_channel_dir."/".$this->_encode($chan)."/nicknames" : - $c->container_cfg_server_dir."/nicknames"; - if (!is_dir($nick_dir)) mkdir_r($nick_dir); + if ($chan == NULL) $chan = 'SERVER'; + $online_user = array(); - $dir_handle = opendir($nick_dir); - while (false !== ($file = readdir($dir_handle))) + $ret = $this->getMeta2("channelid-to-nickid", $this->_encode($chan)); + for($i = 0; $i<count($ret['timestamp']); $i++) { - if ($file == "." || $file == "..") continue; // skip . and .. generic files - $online_user["nickid"][] = $file; - $online_user["timestamp"][] = filemtime($nick_dir."/".$file); - } + $nickid = $ret['value'][$i]; - // cache the user list - $this->_users[$_chan] =& $online_user; + // get timestamp from the SERVER channel + $timestamp = $this->getMeta2("channelid-to-nickid", $this->_encode('SERVER'), $nickid); + $timestamp = $timestamp['timestamp'][0]; - return $this->_users[$_chan]; + $online_user["nick"][] = $this->getNickname($nickid); + $online_user["nickid"][] = $nickid; + $online_user["timestamp"][] = $timestamp; + } + return $online_user; } /** @@ -454,41 +293,16 @@ * @param $chan if NULL then check if the user is online on the server, otherwise check if the user has joined the channel * @return -1 if the user is off line, a positive (>=0) if the user is online */ - function isNickOnline($chan, $nick) + function isNickOnline($chan, $nickid) { - // @todo optimise with this piece of code - /* - $nickid = $this->getNickId($nick); - if ($nickid == "undefined") return false; + if ($chan == NULL) $chan = 'SERVER'; - $nick_dir = ($chan != NULL) ? - $c->container_cfg_channel_dir."/".$this->_encode($chan)."/nicknames" : - $c->container_cfg_server_dir."/nicknames"; - if (!is_dir($nick_dir)) mkdir_r($nick_dir); - - return file_exists($nick_dir."/".$nickid); - */ - - $nickid = $this->getNickId($nick); - - // get the nickname list - $_chan = ($chan == NULL) ? "SERVER" : $chan; - $online_user = isset($this->_users[$_chan]) ? $this->_users[$_chan] : $this->getOnlineNick($chan); - - $uid = 0; - $isonline = false; - if (!isset($online_user["nickid"])) return -1; - while($uid < count($online_user["nickid"]) && !$isonline) + $ret = $this->getMeta2("channelid-to-nickid", $this->_encode($chan)); + for($i = 0; $i<count($ret['timestamp']); $i++) { - if ($online_user["nickid"][$uid] == $nickid) - $isonline = true; - else - $uid++; + if ($ret['value'][$i] == $nickid) return $i; } - if ($isonline) - return $uid; - else - return -1; + return -1; } /** @@ -501,24 +315,15 @@ * @return $msg_id the created message identifier */ function write($chan, $nick, $cmd, $param) - { + { $c =& $this->c; + if ($chan == NULL) $chan = 'SERVER'; + + $msgid = $this->_requestMsgId($chan); - $msg_dir = ($chan != NULL) ? - $c->container_cfg_channel_dir."/".$this->_encode($chan)."/messages" : - $c->container_cfg_server_dir."/messages"; - // check the messages directory exists - $errors = @test_writable_dir($msg_dir, $chan."/messages"); - if (count($errors) > 0) return $errors; // an error occurs ? - - // request a unique id for this new message - $msg_id = $this->_requestMsgId($chan); - if (is_array($msg_id)) return $msg_id; // an error occurs ? - $msg_filename = $msg_dir."/".$msg_id; - // format message $data = "\n"; - $data .= $msg_id."\t"; + $data .= $msgid."\t"; $data .= date("d/m/Y")."\t"; $data .= date("H:i:s")."\t"; $data .= $nick."\t"; @@ -526,14 +331,14 @@ $data .= $param; // write message - file_put_contents($msg_filename, $data); + $this->setMeta2("channelid-to-msg", $this->_encode($chan), $msgid, $data); // delete the obsolete message - $old_msg_id = $msg_id - $c->max_msg - 20; - if ($old_msg_id > 0 && file_exists($msg_dir."/".$old_msg_id)) - @unlink($msg_dir."/".$old_msg_id); - - return $msg_id; + $old_msgid = $msgid - $c->max_msg - 20; + if ($old_msgid > 0) + $this->rmMeta2("channelid-to-msg", $this->_encode($chan), $old_msgid); + + return $msgid; } /** @@ -546,36 +351,29 @@ function read($chan, $from_id) { $c =& $this->c; - - $msg_dir = ($chan != NULL) ? - $c->container_cfg_channel_dir."/".$this->_encode($chan)."/messages" : - $c->container_cfg_server_dir."/messages"; - // check the messages directory exists - $errors = @test_writable_dir($msg_dir, $chan."/messages"); + if ($chan == NULL) $chan = 'SERVER'; - - // read the files into the directory - // sort it by filename order (id order) - // then take only the > $from_id messages - $newmsg = array(); + // read new messages id + $new_msgid_list = array(); $new_from_id = $from_id; - $dir_handle = opendir($msg_dir); - while (false !== ($file = readdir($dir_handle))) + $msgid_list = $this->getMeta2("channelid-to-msg", $this->_encode($chan)); + for($i = 0; $i<count($msgid_list["value"]); $i++) { - if ($file == "." || $file == "..") continue; // skip . and .. generic files - if ($file>$from_id) + $msgidtmp = $msgid_list["value"][$i]; + + if ($msgidtmp > $from_id) { - if ($file > $new_from_id) - $new_from_id = $file; - $newmsg[] = $file; + if ($msgidtmp > $new_from_id) $new_from_id = $msgidtmp; + $new_msgid_list[] = $msgidtmp; } } - - // format content + + // read messages content and parse content $datalist = array(); - foreach ( $newmsg as $m ) + foreach ( $new_msgid_list as $mid ) { - $line = file_get_contents($msg_dir."/".$m); + $line = $this->getMeta2("channelid-to-msg", $this->_encode($chan), $mid, true); + $line = $line["value"][0]; if ($line != "" && $line != "\n") { $formated_line = explode( "\t", $line ); @@ -603,131 +401,17 @@ */ function getLastId($chan) { - $c =& $this->c; + if ($chan == NULL) $chan = 'SERVER'; - // calculate the messages.index location - $chan_dir = ($chan != NULL) ? - $c->container_cfg_channel_dir."/".$this->_encode($chan) : - $c->container_cfg_server_dir; - $index_filename = $chan_dir . "/messages.index"; - - // read last message id - $lastid = trim(@file_get_contents($index_filename)); - if (!is_numeric($lastid)) $lastid = 0; - - return $lastid; - } - - - /** - * Read meta data identified by a key - * As an example the default file container store metadata into metadata/type/subtype/hash(key) - * @param $key is the index which identify a metadata - * @param $type is used to "group" some metadata - * @param $subtype is used to "group" precisely some metadata, use NULL to ignore it - * @return mixed the value assigned to the key, NULL if not found - */ - function getMeta($key, $type, $subtype = NULL) - { - // encode parameters - $enc_key = $this->_encode($key); - $enc_type = $this->_encode($type); - $enc_subtype = ($subtype == NULL) ? "NULL" : $this->_encode($subtype); - if (isset($this->_meta[$enc_type][$enc_subtype][$enc_key])) - return $this->_meta[$enc_type][$enc_subtype][$enc_key]; - - // read data from metadata file - $c =& $this->c; - $dir_base = $c->container_cfg_meta_dir; - $dir = $dir_base."/".$enc_type.($enc_subtype == "NULL" ? "" : "/".$enc_subtype); - $filename = $dir."/".$enc_key; - $ret = @file_get_contents($filename); - if ($ret == false) $ret = NULL; - - // store the result in the cache - $this->_meta[$enc_type][$enc_subtype][$enc_key] = $ret; - - return $ret; - } - - /** - * Write a meta data value identified by a key - * As an example the default file container store metadata into metadata/type/subtype/hash(key) - * @param $key is the index which identify a metadata - * @param $value is the value associated to the key - * @param $type is used to "group" some metadata - * @param $subtype is used to "group" precisely some metadata, use NULL to ignore it - * @return true on success, false on error - */ - function setMeta($value, $key, $type, $subtype = NULL) - { - // encode parameters - $enc_key = $this->_encode($key); - $enc_type = $this->_encode($type); - $enc_subtype = ($subtype == NULL) ? "NULL" : $this->_encode($subtype); - - // create directories - $c =& $this->c; - $dir_base = $c->container_cfg_meta_dir; - $dir = $dir_base."/".$enc_type.($enc_subtype == "NULL" ? "" : "/".$enc_subtype); - if (!is_dir($dir)) mkdir_r($dir); - - // create or replace metadata file - $filename = $dir."/".$enc_key; - $ret = @file_put_contents($filename, $value); - - // store the value in the cache - if ($ret) $this->_meta[$enc_type][$enc_subtype][$enc_key] = $value; - - if ($ret == false) - return false; + $lastmsgid = $this->getMeta2("channelid-to-msgid", $this->_encode($chan), 'lastmsgid', true); + if (count($lastmsgid["value"]) == 0) + $lastmsgid = 0; else - return true; + $lastmsgid = $lastmsgid["value"][0]; + return $lastmsgid; } - /** - * Remove a meta data key/value couple - * Notice: if key is NULL then all the meta data must be removed - * @param $key is the key to delete, use NULL to delete all the metadata - * @param $type is used to "group" some metadata - * @param $subtype is used to "group" precisely some metadata, use NULL to ignore it - * @return true on success, false on error - */ - function rmMeta($key, $type, $subtype = NULL) - { - $c =& $this->c; - - // encode parameters - $enc_key = ($key == NULL) ? "NULL" : $this->_encode($key); - $enc_type = $this->_encode($type); - $enc_subtype = ($subtype == NULL) ? "NULL" : $this->_encode($subtype); - // rm data from metadata file - $dir_base = $c->container_cfg_meta_dir; - $dir = $dir_base."/".$enc_type.($enc_subtype == "NULL" ? "" : "/".$enc_subtype); - $ret = true; - if ($enc_key == "NULL") - { - // remove all keys (the complete directory) - @rm_r($dir); - - // remove the cached data - unset($this->_meta[$enc_type][$enc_subtype]); - } - else - { - // just remove one key - $filename = $dir."/".$enc_key; - $ret = @unlink($filename); - - // remove the cached data - if (isset($this->_meta[$enc_type][$enc_subtype][$enc_key])) - unset($this->_meta[$enc_type][$enc_subtype][$enc_key]); - } - - return $ret; - } - /** * Remove all created data for this server (identified by serverid) * Notice: for the default File container, it's just a recursive directory remove @@ -739,56 +423,11 @@ $dir = $c->container_cfg_server_dir; @rm_r($dir); // empty the cache - $this->_meta = array(); - $this->_users = array("nickid" => array(), - "timestamp" => array()); + // $this->_meta = array(); + // $this->_users = array("nickid" => array(), + // "timestamp" => array()); } - function _registerUserMeta($nickid, $chan) - { - $c =& $this->c; - // create or update the nickname references (used to know when the nick is really disconnected) - if ($chan == NULL) $chan = "SERVER"; - $ref = $this->getMeta("references", $nickid); - if ($ref == NULL) - $ref = array(); - else - $ref = explode(';',$ref); - if ($c->debug) pxlog("registerUserMeta -> ref=".implode(';',$ref), "chat", $c->getId()); - if (in_array($chan,$ref)) - return; - else - $ref[] = $chan; - $ref = implode(';',$ref); - $this->setMeta($ref, "references", $nickid); - } - - function _unregisterUserMeta($nickid, $chan) - { - $c =& $this->c; - // decrement the nick references and kill the metadata if not more references is found - // (used to know when the nick is really disconnected) - if ($chan == NULL) $chan = "SERVER"; - $nickname = $this->getNickname($nickid); - $ref = $this->getMeta("references", $nickid); - if ($ref == NULL) $ref = ''; - $ref = explode(';',$ref); - $ref = array_diff($ref, array($chan)); - if (count($ref) == 0) - { - $this->rmMeta("nickid", "fromnickname", $nickname); - $this->rmMeta("nickname", "fromnickid", $nickid); - $this->rmMeta("references", $nickid); // destroy also the reference counter - if ($c->debug) pxlog("_unregisterUserMeta -> destroy!", "chat", $c->getId()); - } - else - { - $ref = implode(';',$ref); - $this->setMeta($ref, "references", $nickid); - if ($c->debug) pxlog("_unregisterUserMeta -> ref=".$ref, "chat", $c->getId()); - } - } - /** * Return a unique id. Each time this function is called, the last id is incremented. @@ -797,38 +436,13 @@ */ function _requestMsgId($chan) { - $c =& $this->c; - - // calculate the messages.index location - $chan_dir = ($chan != NULL) ? - $c->container_cfg_channel_dir."/".$this->_encode($chan) : - $c->container_cfg_server_dir; - // check the directory exists - $errors = @test_writable_dir($chan_dir, $chan_dir); - if (count($errors) > 0) return $errors; + if ($chan == NULL) $chan = 'SERVER'; - $index_filename = $chan_dir . "/messages.index"; + $lastmsgid = $this->getLastId($chan); + $lastmsgid++; + $this->setMeta2("channelid-to-msgid", $this->_encode($chan), 'lastmsgid', $lastmsgid); - // read last message id - $msg_id = 0; - if (!file_exists($index_filename)) - file_put_contents($index_filename, "0"); - $fp = fopen($index_filename, "rw+"); - if (is_resource($fp)) - { - flock ($fp, LOCK_EX); - $msg_id = fread($fp, filesize($index_filename)); - if (!is_numeric($msg_id)) $msg_id = 0; - // increment message id and save it - $msg_id++; - ftruncate($fp, 0); - fseek($fp, 0); - fwrite($fp, $msg_id); - flock ($fp, LOCK_UN); - fclose($fp); - } - - return $msg_id; + return $lastmsgid; } /** @@ -837,6 +451,7 @@ */ function _encode($str) { + return urlencode($str); return base64_encode(urlencode($str)); } @@ -846,6 +461,7 @@ */ function _decode($str) { + return urldecode($str); return urldecode(base64_decode($str)); } @@ -873,17 +489,19 @@ $dir_base = $c->container_cfg_meta_dir; $dir = $dir_base.'/'.$group.'/'.$subgroup; if (!is_dir($dir)) mkdir_r($dir); - + // create or replace metadata file $leaffilename = $dir."/".$leaf; $leafexists = file_exists($leaffilename); if ($leafvalue == NULL) { - @touch($leaffilename); + if (file_exists($leaffilename) && + filesize($leaffilename)>0) unlink($leaffilename); + touch($leaffilename); } else { - @file_put_contents($leaffilename, $leafvalue); + file_put_contents($leaffilename, $leafvalue); } // store the value in the memory cache @@ -919,12 +537,15 @@ if ($subgroup == NULL) { - $dh = opendir($dir); - while (false !== ($file = readdir($dh))) + if (is_dir($dir)) { - if ($file == "." || $file == "..") continue; // skip . and .. generic files - $ret["timestamp"][] = @filemtime($dir.'/'.$file); - $ret["value"][] = $file; + $dh = opendir($dir); + while (false !== ($file = readdir($dh))) + { + if ($file == "." || $file == "..") continue; // skip . and .. generic files + $ret["timestamp"][] = filemtime($dir.'/'.$file); + $ret["value"][] = $file; + } } return $ret; } @@ -933,13 +554,16 @@ if ($leaf == NULL) { - $dh = opendir($dir); - $ret = array(); - while (false !== ($file = readdir($dh))) + if (is_dir($dir)) { - if ($file == "." || $file == "..") continue; // skip . and .. generic files - $ret["timestamp"][] = @filemtime($dir.'/'.$file); - $ret["value"][] = $file; + $dh = opendir($dir); + $ret = array(); + while (false !== ($file = readdir($dh))) + { + if ($file == "." || $file == "..") continue; // skip . and .. generic files + $ret["timestamp"][] = filemtime($dir.'/'.$file); + $ret["value"][] = $file; + } } return $ret; } @@ -949,12 +573,9 @@ if (!file_exists($leaffilename)) return $ret; if ($withleafvalue) { - $ret["value"][] = @file_get_contents($leaffilename); + $ret["value"][] = file_get_contents($leaffilename); } - else - { - $ret["timestamp"][] = @filemtime($leaffilename); - } + $ret["timestamp"][] = filemtime($leaffilename); // @todo // store the result in the memory cache @@ -983,7 +604,7 @@ if ($subgroup == NULL) { - @rm_r($dir); + rm_r($dir); return true; } @@ -991,14 +612,14 @@ if ($leaf == NULL) { - @rm_r($dir); + rm_r($dir); return true; } $leaffilename = $dir."/".$leaf; if (!file_exists($leaffilename)) return false; - @unlink($leaffilename); + unlink($leaffilename); return true; } Modified: trunk/testcase/container_file.php =================================================================== --- trunk/testcase/container_file.php 2006-09-19 06:51:13 UTC (rev 769) +++ trunk/testcase/container_file.php 2006-09-19 15:25:50 UTC (rev 770) @@ -25,7 +25,257 @@ function tearDown() { pfcContainerTestcase::tearDown(); - } + } + + function test_setMeta_File_1() + { + $c =& $this->c; + $ct =& $this->ct; + + $prefix = __FUNCTION__; + $group = $prefix."_nickid-to-channelid"; + $subgroup = $prefix."_nickid1"; + $leaf = $prefix."_channelid1"; + $ret = $ct->setMeta2($group, $subgroup, $leaf); + $this->assertEquals($ret, 0, "the leaf should be first time created"); + + $f = $c->container_cfg_meta_dir.'/'.$group.'/'.$subgroup.'/'.$leaf; + $ret = file_exists($f); + $this->assertEquals($ret, true, "the leaf file should exists"); + + $ret = file_get_contents($f); + $this->assertEquals($ret, '', "the leaf file should contain nothing"); + } + + function test_setMeta_File_2() + { + $c =& $this->c; + $ct =& $this->ct; + + $prefix = __FUNCTION__; + $group = $prefix."_nickid-to-channelid"; + $subgroup = $prefix."_nickid1"; + $leaf = $prefix."_channelid1"; + $leafvalue = $prefix."_leafvalue1"; + $ret = $ct->setMeta2($group, $subgroup, $leaf, $leafvalue); + $this->assertEquals($ret, 0, "the leaf should be first time created"); + + $f = $c->container_cfg_meta_dir.'/'.$group.'/'.$subgroup.'/'.$leaf; + $ret = file_exists($f); + $this->assertEquals($ret, true, "the leaf file should exists"); + + $ret = file_get_contents($f); + $this->assertEquals($ret, $leafvalue, "the leaf file should contain the value"); + } + + function test_setMeta_File_3() + { + $c =& $this->c; + $ct =& $this->ct; + + $prefix = __FUNCTION__; + $group = $prefix."_nickid-to-channelid"; + $subgroup = $prefix."_nickid1"; + $leaf = $prefix."_channelid1"; + $leafvalue = $prefix."_leafvalue1"; + + $ret = $ct->setMeta2($group, $subgroup, $leaf, $leafvalue); + $this->assertEquals($ret, 0, "the leaf should be first time created"); + + $leafvalue = null; + $ret = $ct->setMeta2($group, $subgroup, $leaf, $leafvalue); + $this->assertEquals($ret, 1, "the leaf should be overwritten"); + + $f = $c->container_cfg_meta_dir.'/'.$group.'/'.$subgroup.'/'.$leaf; + $ret = file_exists($f); + $this->assertEquals($ret, true, "the leaf file should exists"); + + $ret = file_get_contents($f); + $this->assertEquals($ret, '', "the leaf file should contain nothing"); + } + + + function test_getMeta_File_1() + { + $c =& $this->c; + $ct =& $this->ct; + + $prefix = __FUNCTION__; + $group = $prefix."_nickid-to-channelid"; + $subgroup = $prefix."_nickid1"; + $leaf = $prefix."_channelid1"; + $ct->setMeta2($group, $subgroup, $leaf); + $time = time(); + + $ret = $ct->getMeta2($group, $subgroup, $leaf); + $this->assertEquals(count($ret["timestamp"]), 1, "number of leaf is wrong"); + $this->assertEquals($ret["timestamp"][0], $time, "the leaf timestamp is wrong"); + $this->assertEquals($ret["value"][0], null, "the leaf value is wrong"); + + $ret = $ct->getMeta2($group, $subgroup); + $this->assertEquals(count($ret["timestamp"]), 1, "number of leaf is wrong"); + $this->assertEquals($ret["timestamp"][0], $time, "the leaf timestamp is wrong"); + $this->assertEquals($ret["value"][0], $leaf, "the leaf name is wrong"); + + $leafvalue = $prefix."_leafvalue"; + $ct->setMeta2($group, $subgroup, $leaf, $leafvalue); + $time = time(); + + $ret = $ct->getMeta2($group, $subgroup, $leaf, true); + $this->assertEquals(count($ret["timestamp"]), 1, "number of leaf is wrong"); + $this->assertEquals($ret["timestamp"][0], $time, "the leaf timestamp is wrong"); + $this->assertEquals($ret["value"][0], $leafvalue, "the leaf value is wrong"); + } + + function test_getMeta_File_2() + { + $c =& $this->c; + $ct =& $this->ct; + + $prefix = __FUNCTION__; + $group = $prefix."_nickid-to-channelid"; + $subgroup = $prefix."_nickid1"; + $leaf1 = $prefix."_channelid1"; + $leaf2 = $prefix."_channelid2"; + $ct->setMeta2($group, $subgroup, $leaf1); + $ct->setMeta2($group, $subgroup, $leaf2); + $time = time(); + + $ret = $ct->getMeta2($group, $subgroup); + asort($ret["value"]); + $this->assertEquals(count($ret["timestamp"]), 2, "number of leaf is wrong"); + $this->assertEquals($ret["timestamp"][0], $time, "the leaf timestamp is wrong"); + $this->assertEquals($ret["timestamp"][1], $time, "the leaf timestamp is wrong"); + $this->assertEquals($ret["value"][0], $leaf1, "the leaf name is wrong"); + $this->assertEquals($ret["value"][1], $leaf2, "the leaf name is wrong"); + } + + function test_getMeta_File_3() + { + $c =& $this->c; + $ct =& $this->ct; + + $prefix = __FUNCTION__; + $group = $prefix."_nickid-to-channelid"; + $subgroup1 = $prefix."_nickid1"; + $subgroup2 = $prefix."_nickid2"; + $leaf1 = $prefix."_channelid1"; + $leaf2 = $prefix."_channelid2"; + $ct->setMeta2($group, $subgroup1, $leaf1); + $ct->setMeta2($group, $subgroup1, $leaf2); + $ct->setMeta2($group, $subgroup2, $leaf1); + $ct->setMeta2($group, $subgroup2, $leaf2); + $time = time(); + + $ret = $ct->getMeta2($group); + asort($ret["value"]); + $this->assertEquals(count($ret["timestamp"]), 2, "number of subgroup is wrong"); + $this->assertEquals($ret["timestamp"][0], $time, "the subgroup timestamp is wrong"); + $this->assertEquals($ret["timestamp"][1], $time, "the subgroup timestamp is wrong"); + $this->assertEquals($ret["value"][0], $subgroup1, "the subgroup name is wrong"); + $this->assertEquals($ret["value"][1], $subgroup2, "the subgroup name is wrong"); + } + + function test_rmMeta_File_1() + { + $c =& $this->c; + $ct =& $this->ct; + + $prefix = __FUNCTION__; + $group = $prefix."_nickid-to-channelid"; + $subgroup = $prefix."_nickid1"; + $leaf = $prefix."_channelid1"; + $ret = $ct->setMeta2($group, $subgroup, $leaf); + + $ret = $ct->rmMeta2($group, $subgroup, $leaf); + $this->assertEquals($ret, true, "the returned value should be true (rm success)"); + + $f = $c->container_cfg_meta_dir.'/'.$group.'/'.$subgroup.'/'.$leaf; + $ret = file_exists($f); + $this->assertEquals($ret, false, "the leaf file should not exists anymore"); + } + + function test_rmMeta_File_2() + { + $c =& $this->c; + $ct =& $this->ct; + + $prefix = __FUNCTION__; + $group = $prefix."_nickid-to-channelid"; + $subgroup = $prefix."_nickid1"; + $leaf1 = $prefix."_channelid1"; + $leaf2 = $prefix."_channelid2"; + $ret = $ct->setMeta2($group, $subgroup, $leaf1); + $ret = $ct->setMeta2($group, $subgroup, $leaf2); + + $ret = $ct->rmMeta2($group, $subgroup); + $this->assertEquals($ret, true, "the returned value should be true (rm success)"); + + $f = $c->container_cfg_meta_dir.'/'.$group.'/'.$subgroup.'/'.$leaf1; + $ret = file_exists($f); + $this->assertEquals($ret, false, "the leaf file should not exists anymore"); + $f = $c->container_cfg_meta_dir.'/'.$group.'/'.$subgroup.'/'.$leaf2; + $ret = file_exists($f); + $this->assertEquals($ret, false, "the leaf file should not exists anymore"); + + $d = $c->container_cfg_meta_dir.'/'.$group.'/'.$subgroup; + $ret = file_exists($f); + $this->assertEquals($ret, false, "the subgroup directory should not exists anymore"); + } + + function test_rmMeta_File_3() + { + $c =& $this->c; + $ct =& $this->ct; + + $prefix = __FUNCTION__; + $group = $prefix."_nickid-to-channelid"; + $subgroup = $prefix."_nickid1"; + $leaf1 = $prefix."_channelid1"; + $leaf2 = $prefix."_channelid2"; + $ret = $ct->setMeta2($group, $subgroup, $leaf1); + $ret = $ct->setMeta2($group, $subgroup, $leaf2); + + $ret = $ct->rmMeta2($group); + $this->assertEquals($ret, true, "the returned value should be true (rm success)"); + + $f = $c->container_cfg_meta_dir.'/'.$group.'/'.$subgroup.'/'.$leaf1; + $ret = file_exists($f); + $this->assertEquals($ret, false, "the leaf file should not exists anymore"); + $f = $c->container_cfg_meta_dir.'/'.$group.'/'.$subgroup.'/'.$leaf2; + $ret = file_exists($f); + $this->assertEquals($ret, false, "the leaf file should not exists anymore"); + + $d = $c->container_cfg_meta_dir.'/'.$group.'/'.$subgroup; + $ret = file_exists($d); + $this->assertEquals($ret, false, "the subgroup directory should not exists anymore"); + + $d = $c->container_cfg_meta_dir.'/'.$group; + $ret = file_exists($d); + $this->assertEquals($ret, false, "the group directory should not exists anymore"); + } + + + function test_encodedecode_File_1() + { + $c =& $this->c; + $ct =& $this->ct; + + $string = "il était une fois C;h:!?§+ toto=}at是"; + + $prefix = __FUNCTION__; + $group = $prefix."_nickid-to-channelid"; + $subgroup = $prefix."_nickid1"; + $leaf = $prefix."_".$ct->_encode($string); + $leafvalue = $string; + $ct->setMeta2($group, $subgroup, $leaf, $leafvalue); + + $ret = $ct->getMeta2($group, $subgroup); + $this->assertEquals($ret['value'][0], $leaf, "the leaf name is wrong"); + $ret = $ct->getMeta2($group, $subgroup, $leaf, true); + $this->assertEquals($ret['value'][0], $leafvalue, "the leaf value is wrong"); + } + } // on desactive le timeout car se script peut mettre bcp de temps a s'executer Modified: trunk/testcase/container_generic.php =================================================================== --- trunk/testcase/container_generic.php 2006-09-19 06:51:13 UTC (rev 769) +++ trunk/testcase/container_generic.php 2006-09-19 15:25:50 UTC (rev 770) @@ -30,12 +30,12 @@ require_once dirname(__FILE__)."/../src/pfcglobalconfig.class.php"; $params = array(); $params["title"] = "testcase -> pfccontainer_".$this->type; - $params["serverid"] = md5(__FILE__ . time()); + $params["serverid"] = md5(__FILE__/* . time()*/); $params["container_type"] = $this->type; $this->c =& pfcGlobalConfig::Instance($params); $this->ct =& $this->c->getContainerInstance(); } - + // called after the test functions are executed // this function is defined in PHPUnit_TestCase and overwritten // here @@ -45,45 +45,48 @@ $this->c->destroyCache(); } + function testCreateNick_Generic() { $c =& $this->c; $ct =& $this->ct; - $nick = $this->nick; - $nickid = $this->nickid; - $chan = $this->chan; + $prefix = __FUNCTION__; + $nick = $prefix . '_' . $this->nick; + $nickid = $prefix . '_' . $this->nickid; + $chan = $prefix . '_' . $this->chan; // create on the channel $this->ct->createNick($chan, $nick, $nickid); - $isonline = ($this->ct->isNickOnline($chan, $nick) >= 0); + $isonline = ($this->ct->isNickOnline($chan, $nickid) >= 0); $this->assertTrue($isonline, "nickname should be online on the channel"); // create on the server $chan = NULL; $this->ct->createNick($chan, $nick, $nickid); - $isonline = ($this->ct->isNickOnline($chan, $nick) >= 0); + $isonline = ($this->ct->isNickOnline($chan, $nickid) >= 0); $this->assertTrue($isonline, "nickname should be online on the server"); } - + function testRemoveNick_Generic() { $c =& $this->c; $ct =& $this->ct; - $nick = $this->nick; - $nickid = $this->nickid; - $chan = $this->chan; + $prefix = __FUNCTION__; + $nick = $prefix . '_' . $this->nick; + $nickid = $prefix . '_' . $this->nickid; + $chan = $prefix . '_' . $this->chan; // on the channel $this->ct->createNick($chan, $nick, $nickid); - $this->ct->removeNick($chan, $nick); - $isonline = ($this->ct->isNickOnline($chan, $nick) >= 0); + $this->ct->removeNick($nickid); + $isonline = ($this->ct->isNickOnline($chan, $nickid) >= 0); $this->assertFalse($isonline, "nickname shouldn't be online on the channel"); // on the server $chan = NULL; $this->ct->createNick($chan, $nick, $nickid); - $this->ct->removeNick($chan, $nick); - $isonline = ($this->ct->isNickOnline($chan, $nick) >= 0); + $this->ct->removeNick($nickid); + $isonline = ($this->ct->isNickOnline($chan, $nickid) >= 0); $this->assertFalse($isonline, "nickname shouldn't be online on the server"); } @@ -91,159 +94,165 @@ { $c =& $this->c; $ct =& $this->ct; - $nick = $this->nick; - $nickid = $this->nickid; - $chan = $this->chan; + $prefix = __FUNCTION__; + $nick = $prefix . '_' . $this->nick; + $nickid = $prefix . '_' . $this->nickid; + $chan = $prefix . '_' . $this->chan; $this->ct->createNick(NULL, $nick, $nickid); $ret = $this->ct->getNickId($nick); $this->assertEquals($nickid, $ret, "created nickname doesn't have a correct nickid"); } - - function testRemoveObsoleteNick_Generic() + + + function testGetNickname_Generic() { $c =& $this->c; $ct =& $this->ct; - $nick = $this->nick; - $nickid = $this->nickid; - $chan = $this->chan; + $prefix = __FUNCTION__; + $nick = $prefix . '_' . $this->nick; + $nickid = $prefix . '_' . $this->nickid; + $chan = $prefix . '_' . $this->chan; // on the channel $this->ct->createNick($chan, $nick, $nickid); - sleep(2); - $ret = $this->ct->removeObsoleteNick($chan, 1000); - $this->assertEquals(count($ret["nickid"]), 1, "1 nickname should be obsolete"); - $isonline = ($this->ct->isNickOnline($chan, $nick) >= 0); - $this->assertFalse($isonline, "nickname shouldn't be online anymore"); - - // on the server - $chan = NULL; + + $ret = $this->ct->getNickname($nickid); + $this->assertEquals($nick, $ret, "nickname value is wrong"); + } + + function testGetOnlineNick_Generic() + { + $c =& $this->c; + $ct =& $this->ct; + $prefix = __FUNCTION__; + $nick = $prefix . '_' . $this->nick; + $nickid = $prefix . '_' . $this->nickid; + $chan = $prefix . '_' . $this->chan; + + // on the channel $this->ct->createNick($chan, $nick, $nickid); - sleep(2); - $ret = $this->ct->removeObsoleteNick($chan, 1000); - $this->assertEquals(count($ret["nickid"]), 1, "1 nickname should be obsolete"); - $isonline = ($this->ct->isNickOnline($chan, $nick) >= 0); - $this->assertFalse($isonline, "nickname shouldn't be online anymore"); + $time = time(); + $ret = $this->ct->getOnlineNick($chan); + $this->assertEquals(1, count($ret["nickid"]), "1 nickname should be online"); + $this->assertEquals(1, count($ret["nick"]), "1 nickname should be online"); + $this->assertEquals(1, count($ret["timestamp"]), "1 nickname should be online"); + + $this->assertEquals($time, $ret["timestamp"][0], "nickname timestamp is wrong"); + $this->assertEquals($nick, $ret["nick"][0], "nickname value is wrong"); + $this->assertEquals($nickid, $ret["nickid"][0], "nickname id is wrong"); } + - function testSetGetRmMeta_Generic() + function testRemoveObsoleteNick_Generic() { $c =& $this->c; $ct =& $this->ct; - $nick = $this->nick; - $nickid = $this->nickid; - $chan = $this->chan; + $prefix = __FUNCTION__; + $nick = $prefix . '_' . $this->nick; + $nickid = $prefix . '_' . $this->nickid; + $chan = $prefix . '_' . $this->chan; - // set / get - $this->ct->setMeta($nickid, "key1", "nickname", $nick); - $metadata = $this->ct->getMeta("key1", "nickname", $nick); - $this->assertEquals($nickid, $metadata, "metadata value is not correct"); - - // set / rm / get - $this->ct->setMeta($nickid, "key2", "nickname", $nick); - $metadata = $this->ct->getMeta("key2", "nickname", $nick); - $this->assertEquals($nickid, $metadata, "metadata value is not correct"); - $this->ct->rmMeta("key2", "nickname", $nick); - $metadata = $this->ct->getMeta("key2", "nickname", $nick); - $this->assertNull($metadata, "metadata should not exists anymore"); - - // set / rm (all) / get - $this->ct->setMeta($nickid, "key2", "nickname", $nick); - $metadata = $this->ct->getMeta("key2", "nickname", $nick); - $this->assertEquals($nickid, $metadata, "metadata value is not correct"); - $this->ct->rmMeta(NULL, "nickname", $nick); - $metadata = $this->ct->getMeta("key2", "nickname", $nick); - $this->assertNull($metadata, "metadata should not exists anymore"); - $metadata = $this->ct->getMeta("key1", "nickname", $nick); - $this->assertNull($metadata, "metadata should not exists anymore"); + $this->ct->createNick($chan, $nick, $nickid); + sleep(2); + $ret = $this->ct->removeObsoleteNick(1000); + $this->assertEquals(count($ret["nickid"]), 1, "1 nickname should be obsolete"); + $isonline = ($this->ct->isNickOnline($chan, $nickid) >= 0); + $this->assertFalse($isonline, "nickname shouldn't be online anymore"); } - + function testupdateNick_Generic() { $c =& $this->c; $ct =& $this->ct; - $nick = $this->nick; - $nickid = $this->nickid; - $chan = $this->chan; + $prefix = __FUNCTION__; + $nick = $prefix . '_' . $this->nick; + $nickid = $prefix . '_' . $this->nickid; + $chan = $prefix . '_' . $this->chan; - // on the channel $this->ct->createNick($chan, $nick, $nickid); sleep(2); - $ret = $this->ct->updateNick($chan, $nick); + $ret = $this->ct->updateNick($nickid); $this->assertTrue($ret, "nickname should be correctly updated"); - $ret = $this->ct->removeObsoleteNick($chan, 1000); - $this->assertFalse(in_array($nick, $ret), "nickname shouldn't be removed because it has been updated"); - $isonline = ($this->ct->isNickOnline($chan, $nick) >= 0); + + $ret = $this->ct->removeObsoleteNick(1000); + $this->assertFalse(in_array($nick, $ret['nick']), "nickname shouldn't be removed because it has been updated"); + $isonline = ($this->ct->isNickOnline($chan, $nickid) >= 0); $this->assertTrue($isonline, "nickname should be online"); - - // on the server - $chan = NULL; - $this->ct->createNick($chan, $nick, $nickid); - sleep(2); - $ret = $this->ct->updateNick($chan, $nick); - $this->assertTrue($ret, "nickname should be correctly updated"); - $ret = $this->ct->removeObsoleteNick($chan, 1000); - $this->assertFalse(in_array($nick, $ret), "nickname shouldn't be removed because it has been updated"); - $isonline = ($this->ct->isNickOnline($chan, $nick) >= 0); - $this->assertTrue($isonline, "nickname should be online"); } + function testchangeNick_Generic() { $c =& $this->c; $ct =& $this->ct; - $nick1 = $this->nick; - $nick2 = $this->nick."2"; - $nickid = $this->nickid; - $chan = $this->chan; + $prefix = __FUNCTION__; + $nick1 = $prefix . '_' . $this->nick; + $nick2 = $prefix . '_' . $this->nick.'2'; + $nickid = $prefix . '_' . $this->nickid; + $chan = $prefix . '_' . $this->chan; // create a nick on a channel and change it $this->ct->createNick($chan, $nick1, $nickid); $ret = $this->ct->changeNick($nick2, $nick1); $this->assertTrue($ret, "nickname change function should returns true (success)"); - $isonline1 = ($this->ct->isNickOnline($chan, $nick1) >= 0); - $isonline2 = ($this->ct->isNickOnline($chan, $nick2) >= 0); + $isonline1 = ($this->ct->isNickOnline($chan, $this->ct->getNickId($nick1)) >= 0); + $isonline2 = ($this->ct->isNickOnline($chan, $this->ct->getNickId($nick2)) >= 0); $this->assertFalse($isonline1, "nickname shouldn't be online"); $this->assertTrue($isonline2, "nickname shouldn't be online"); } - + + function testgetLastId_Generic() + { + $c =& $this->c; + $ct =& $this->ct; + $prefix = __FUNCTION__; + $nick = $prefix . '_' . $this->nick; + $nickid = $prefix . '_' . $this->nickid; + $chan = $prefix . '_' . $this->chan; + $cmd = "send"; + $msg = "my test message"; + + // on the channel + $this->ct->createNick($chan, $nick, $nickid); + for($i = 0; $i < 10; $i++) + { + $msgid = $this->ct->write($chan, $nick, $cmd ,$msg . $i); + $this->assertEquals($msgid, $i+1,"generated msg_id is not correct"); + } + $msgid = $this->ct->getLastId($chan); + $this->assertEquals(10, $msgid, "last msgid is not correct"); + } + function testwrite_Generic() { $c =& $this->c; $ct =& $this->ct; - $nick = $this->nick; - $nickid = $this->nickid; - $chan = $this->chan; + $prefix = __FUNCTION__; + $nick = $prefix . '_' . $this->nick; + $nickid = $prefix . '_' . $this->nickid; + $chan = $prefix . '_' . $this->chan; $cmd = "send"; $msg = "my test message"; // create message on the channel $this->ct->createNick($chan, $nick, $nickid); $msgid = $this->ct->write($chan, $nick, $cmd, $msg); - $this->assertEquals($msgid, 1,"generated msg_id is not correct"); + $this->assertEquals(1, $msgid,"generated msg_id is not correct"); $res = $this->ct->read($chan, 0); $this->assertEquals(1, count($res["data"]), "1 messages should be read"); $this->assertEquals($msg, $res["data"][1]["param"] ,"messages data is not the same as the sent one"); - $this->assertEquals($res["new_from_id"], 1 ,"new_from_id is not correct"); - - // create message on the server - $chan = NULL; - $this->ct->createNick($chan, $nick, $nickid); - $msgid = $this->ct->write($chan, $nick, $cmd, $msg); - $this->assertEquals($msgid, 1,"generated msg_id is not correct"); - $res = $this->ct->read($chan, 0); - $this->assertEquals(1, count($res["data"]), "1 messages should be read"); - $this->assertEquals($msg, $res["data"][1]["param"] ,"messages data is not the same as the sent one"); - $this->assertEquals($res["new_from_id"], 1 ,"new_from_id is not correct"); + $this->assertEquals(1, $res["new_from_id"],"new_from_id is not correct"); } function testread_Generic() { $c =& $this->c; $ct =& $this->ct; - $nick = $this->nick; - $nickid = $this->nickid; - $chan = $this->chan; + $prefix = __FUNCTION__; + $nick = $prefix . '_' . $this->nick; + $nickid = $prefix . '_' . $this->nickid; + $chan = $prefix . '_' . $this->chan; $cmd = "send"; $msg = "my test message"; @@ -268,37 +277,6 @@ $this->assertEquals($res["new_from_id"], 10 ,"new_from_id is not correct"); } - function testgetLastId_Generic() - { - $c =& $this->c; - $ct =& $this->ct; - $nick = $this->nick; - $nickid = $this->nickid; - $chan = $this->chan; - $cmd = "send"; - $msg = "my test message"; - - // on the channel - $this->ct->createNick($chan, $nick, $nickid); - for($i = 0; $i < 10; $i++) - { - $msgid = $this->ct->write($chan, $nick, $cmd ,$msg . $i); - $this->assertEquals($msgid, $i+1,"generated msg_id is not correct"); - } - $msgid = $this->ct->getLastId($chan); - $this->assertEquals(10, $msgid, "last msgid is not correct"); - - // on the server - $chan = NULL; - $this->ct->createNick($chan, $nick, $nickid); - for($i = 0; $i < 10; $i++) - { - $msgid = $this->ct->write($chan, $nick, $cmd ,$msg . $i); - $this->assertEquals($msgid, $i+1,"generated msg_id is not correct"); - } - $msgid = $this->ct->getLastId($chan); - $this->assertEquals(10, $msgid, "last msgid is not correct"); - } } ?> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |