[Phpfreechat-svn] SF.net SVN: phpfreechat: [888] trunk
Status: Beta
Brought to you by:
kerphi
From: <ke...@us...> - 2006-12-06 15:35:38
|
Revision: 888 http://svn.sourceforge.net/phpfreechat/?rev=888&view=rev Author: kerphi Date: 2006-12-06 07:35:25 -0800 (Wed, 06 Dec 2006) Log Message: ----------- [en] Add a mysql container. See demo55 for an example (thanks to HenkBB) [3h30] [fr] Ajout d'un conteneur mysql. Voyez la demo55 pour un exemple (merci ?\195?\160 HenkBB) [3h30] Modified Paths: -------------- trunk/demo/index.php trunk/testcase/container_generic.php Added Paths: ----------- trunk/demo/demo55_mysql_container.php trunk/src/containers/mysql.class.php trunk/testcase/container_mysql.php Added: trunk/demo/demo55_mysql_container.php =================================================================== --- trunk/demo/demo55_mysql_container.php (rev 0) +++ trunk/demo/demo55_mysql_container.php 2006-12-06 15:35:25 UTC (rev 888) @@ -0,0 +1,41 @@ +<?php + +require_once dirname(__FILE__)."/../src/phpfreechat.class.php"; +$params["serverid"] = md5(__FILE__); // calculate a unique id for this chat +$params["nick"] = "guest".rand(1,1000); +$params["container_type"] = "mysql"; +$params["container_cfg_mysql_host"] = "localhost"; // this is the default value +$params["container_cfg_mysql_port"] = 3306; // this is the default value +$params["container_cfg_mysql_database"] = "phpfreechat"; // this is the default value +$params["container_cfg_mysql_table"] = "phpfreechat"; // this is the default value +$params["container_cfg_mysql_username"] = "root"; // this is the default value +$params["container_cfg_mysql_password"] = ""; // this is the default value +$chat = new phpFreeChat($params); + +?> +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html> + + <head> + <meta http-equiv="content-type" content="text/html; charset=utf-8" /> + <title>phpFreeChat demo</title> + <?php $chat->printJavascript(); ?> + <?php $chat->printStyle(); ?> + </head> + + <body> + <?php $chat->printChat(); ?> + +<?php + // print the current file + echo "<h2>The source code</h2>"; + $filename = __FILE__; + echo "<p><code>".$filename."</code></p>"; + echo "<pre style=\"margin: 0 50px 0 50px; padding: 10px; background-color: #DDD;\">"; + $content = file_get_contents($filename); + echo htmlentities($content); + echo "</pre>"; +?> + </body> + +</html> \ No newline at end of file Modified: trunk/demo/index.php =================================================================== --- trunk/demo/index.php 2006-12-04 17:12:10 UTC (rev 887) +++ trunk/demo/index.php 2006-12-06 15:35:25 UTC (rev 888) @@ -71,10 +71,10 @@ <li><a href="demo31_show_who_is_online-whoisonline.php">demo31 - demo which show how to get the connected users list (whoisonline script)</a></li> <li><a href="demo32_show_last_messages-chat.php">demo32 - demo which show how to get the last posted messages (chat script)</a></li> <li><a href="demo32_show_last_messages-showlastmsg.php">demo32 - demo which show how to get the last posted messages (showlastmsg script)</a></li> - <li><a href="demo35_shared_memory.php">demo35 - demo which show how to use the shared memory container</a> (not yet working)</li> <li><a href="demo43_change_the_nicknames_colors.php">demo43 - demo which show how to change the nicknames automatic colors</a></li> <li><a href="demo50_customized_usermetadata.php">demo50 - demo which shows how to use user metadata : add avatar (images) to each connected users</a></li> + <li><a href="demo55_mysql_container.php">demo55 - demo which show how to use the mysql container</a></li> </ul> Added: trunk/src/containers/mysql.class.php =================================================================== --- trunk/src/containers/mysql.class.php (rev 0) +++ trunk/src/containers/mysql.class.php 2006-12-06 15:35:25 UTC (rev 888) @@ -0,0 +1,274 @@ +<?php +/** + * src/container/mysql.class.php + * + * Copyright © 2006 Stephane Gully <ste...@gm...> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301 USA + */ + +require_once dirname(__FILE__)."/../pfccontainer.class.php"; + +/** + * pfcContainer_Mysql is a concret container which store data into mysql + * + * Because of the new storage functions (setMeta, getMeta, rmMeta) + * everything can be stored in just one single table. + * Using type "HEAP" or "MEMORY" mysql loads this table into memory making it very fast + * There is no routine to create the table if it does not exists so you have to create it by hand + * Replace the database login info at the top of pfcContainer_mysql class with your own + * You also need some config lines in your chat index file: + * $params["container_type"] = "mysql"; + * $params["container_cfg_mysql_host"] = "localhost"; + * $params["container_cfg_mysql_port"] = 3306; + * $params["container_cfg_mysql_database"] = "phpfreechat"; + * $params["container_cfg_mysql_table"] = "phpfreechat"; + * $params["container_cfg_mysql_username"] = "root"; + * $params["container_cfg_mysql_password"] = ""; + * + * @author Stephane Gully <ste...@gm...> + * @author HenkBB + */ +class pfcContainer_Mysql extends pfcContainer +{ + var $_db = null; + var $_sql_create_table = " + CREATE TABLE IF NOT EXISTS `%table%` ( + `server` varchar(256) NOT NULL default '', + `group` varchar(256) NOT NULL default '', + `subgroup` varchar(256) NOT NULL default '', + `leaf` varchar(256) NOT NULL default '', + `leafvalue` varchar(1024) NOT NULL, + `timestamp` int(11) NOT NULL default 0, + PRIMARY KEY (`server`,`group`,`subgroup`,`leaf`) +) ENGINE=MEMORY;"; + + function pfcContainer_Mysql(&$config) + { + pfcContainer::pfcContainer($config); + } + + function getDefaultConfig() + { + $c =& $this->c; + $cfg = pfcContainer::getDefaultConfig(); + $cfg["mysql_host"] = 'localhost'; + $cfg["mysql_port"] = 3306; + $cfg["mysql_database"] = 'phpfreechat'; + $cfg["mysql_table"] = 'phpfreechat'; + $cfg["mysql_username"] = 'root'; + $cfg["mysql_password"] = ''; + return $cfg; + } + + function init() + { + $errors = pfcContainer::init(); + $c =& $this->c; + + // connect to the db + $db = $this->_connect(); + if ($db === FALSE) + { + $errors[] = _pfc("Mysql container: connect error"); + return $errors; + } + + // create the db if it doesn't exists + $db_exists = false; + $db_list = mysql_list_dbs($db); + while (!$db_exists && $row = mysql_fetch_object($db_list)) + $db_exists = ($c->container_cfg_mysql_database == $row->Database); + if (!$db_exists) + { + $query = 'CREATE DATABASE '.$c->container_cfg_mysql_database; + $result = mysql_query($query, $db); + if ($result === FALSE) + { + $errors[] = _pfc("Mysql container: create database error '%s'",mysql_error($db)); + return $errors; + } + mysql_select_db($c->container_cfg_mysql_database, $db); + } + + // create the table if it doesn't exists + $query = str_replace('%table%',$c->container_cfg_mysql_table,$this->_sql_create_table); + $result = mysql_query($query, $db); + if ($result === FALSE) + { + $errors[] = _pfc("Mysql container: create table error '%s'",mysql_error($db)); + return $errors; + } + return $errors; + } + + function _connect() + { + if (!$this->_db) + { + $c =& $this->c; + $this->_db = mysql_pconnect($c->container_cfg_mysql_host.':'.$c->container_cfg_mysql_port, + $c->container_cfg_mysql_username, + $c->container_cfg_mysql_password); + mysql_select_db($c->container_cfg_mysql_database, $this->_db); + } + return $this->_db; + } + + function setMeta($group, $subgroup, $leaf, $leafvalue = NULL) + { + $c =& $this->c; + + if ($c->debug) + file_put_contents("/tmp/debug.txt", "\nsetMeta(".$group.",".$subgroup.",".$leaf.",".$leafvalue.")", FILE_APPEND); + + $server = $c->serverid; + $db = $this->_connect(); + + if ($leafvalue == NULL){$leafvalue="";}; + + $sql_select = "SELECT * FROM ".$c->container_cfg_mysql_table." WHERE `server`='$server' AND `group`='$group' AND `subgroup`='$subgroup' AND `leaf`='$leaf'"; + $sql_insert="REPLACE INTO ".$c->container_cfg_mysql_table." (`server`, `group`, `subgroup`, `leaf`, `leafvalue`, `timestamp`) VALUES('$server', '$group', '$subgroup', '$leaf', '".addslashes($leafvalue)."', '".time()."')"; + $sql_update="UPDATE ".$c->container_cfg_mysql_table." SET `leafvalue`='".addslashes($leafvalue)."', `timestamp`='".time()."' WHERE `server`='$server' AND `group`='$group' AND `subgroup`='$subgroup' AND `leaf`='$leaf'"; + + $res = mysql_query($sql_select, $db); + if( !(mysql_num_rows($res)>0) ) + { + if ($c->debug) + file_put_contents("/tmp/debug.txt", "\nsetSQL(".$sql_insert.")", FILE_APPEND); + + mysql_query($sql_insert, $db); + return 0; // value created + } + else + { + if ($sql_update != "") + { + if ($c->debug) + file_put_contents("/tmp/debug.txt", "\nsetSQL(".$sql_update.")", FILE_APPEND); + + mysql_query($sql_update, $db); + } + return 1; // value overwritten + } + } + + + function getMeta($group, $subgroup = null, $leaf = null, $withleafvalue = false) + { + $c =& $this->c; + if ($c->debug) + file_put_contents("/tmp/debug.txt", "\ngetMeta(".$group.",".$subgroup.",".$leaf.",".$withleafvalue.")", FILE_APPEND); + + $ret = array(); + $ret["timestamp"] = array(); + $ret["value"] = array(); + + $server = $c->serverid; + $db = $this->_connect(); + + $sql_where=""; + $value="leafvalue"; + + if ($group != NULL) + { + $sql_where.=" AND `group`='$group'"; + $value="subgroup"; + } + + if ($subgroup != NULL) + { + $sql_where.=" AND `subgroup`='$subgroup'"; + $value="leaf"; + } + + if ($leaf != NULL) + { + $sql_where.=" AND `leaf`='$leaf'"; + $value="leafvalue"; + } + + $sql_select="SELECT `$value`, `timestamp` FROM ".$c->container_cfg_mysql_table." WHERE `server`='$server' $sql_where GROUP BY `$value` ORDER BY timestamp"; + + if ($c->debug) + file_put_contents("/tmp/debug.txt", "\ngetSQL(".$sql_select.")", FILE_APPEND); + + if ($sql_select != "") + { + $thisresult = mysql_query($sql_select, $db); + if (mysql_num_rows($thisresult)) + { + while ($regel = mysql_fetch_array($thisresult)) + { + $ret["timestamp"][] = $regel["timestamp"]; + if ($value == "leafvalue") + { + if ($withleafvalue) + $ret["value"][] = $regel[$value]; + else + $ret["value"][] = NULL; + } + else + $ret["value"][] = $regel[$value]; + } + + } + else + return $ret; + } + return $ret; + } + + function rmMeta($group, $subgroup = null, $leaf = null) + { + $c =& $this->c; + if ($c->debug) + file_put_contents("/tmp/debug.txt", "\nrmMeta(".$group.",".$subgroup.",".$leaf.")", FILE_APPEND); + + $server = $c->serverid; + $db = $this->_connect(); + + $sql_delete = "DELETE FROM ".$c->container_cfg_mysql_table." WHERE `server`='$server'"; + + if($group != NULL) + $sql_delete .= " AND `group`='$group'"; + + if($subgroup != NULL) + $sql_delete .= " AND `subgroup`='$subgroup'"; + + if ($leaf != NULL) + $sql_delete .= " AND `leaf`='$leaf'"; + + if ($c->debug) + file_put_contents("/tmp/debug.txt", "\nrmSQL(".$sql_delete.")", FILE_APPEND); + + mysql_query($sql_delete, $db); + return true; + } + + function encode($str) + { + return addslashes(urlencode($str)); + } + + function decode($str) + { + return urldecode(stripslashes($str)); + } + +} + +?> \ No newline at end of file Modified: trunk/testcase/container_generic.php =================================================================== --- trunk/testcase/container_generic.php 2006-12-04 17:12:10 UTC (rev 887) +++ trunk/testcase/container_generic.php 2006-12-06 15:35:25 UTC (rev 888) @@ -31,7 +31,7 @@ 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 = new pfcGlobalConfig($params); $this->ct = $this->c->getContainerInstance(); @@ -296,7 +296,7 @@ $ret = $ct->getMeta($group, $subgroup, $leaf, true); $this->assertEquals($ret['value'][0], $leafvalue, "the leaf value is wrong"); } - + function test_getMeta_Generic_1() { $c =& $this->c; @@ -344,7 +344,7 @@ $time = time(); $ret = $ct->getMeta($group, $subgroup); - asort($ret["value"]); + sort($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"); @@ -368,16 +368,15 @@ $ct->setMeta($group, $subgroup2, $leaf1); $ct->setMeta($group, $subgroup2, $leaf2); $time = time(); - + $ret = $ct->getMeta($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"); + sort($ret["value"]); + $this->assertEquals(2, count($ret["timestamp"]), "number of subgroup is wrong"); + $this->assertEquals($time, $ret["timestamp"][0], "the subgroup timestamp is wrong"); + $this->assertEquals($time, $ret["timestamp"][1], "the subgroup timestamp is wrong"); + $this->assertEquals($subgroup1, $ret["value"][0], "the subgroup name is wrong"); + $this->assertEquals($subgroup2, $ret["value"][1], "the subgroup name is wrong"); } - } ?> Added: trunk/testcase/container_mysql.php =================================================================== --- trunk/testcase/container_mysql.php (rev 0) +++ trunk/testcase/container_mysql.php 2006-12-06 15:35:25 UTC (rev 888) @@ -0,0 +1,41 @@ +<?php + +require_once "container_generic.php"; + +class pfcContainerTestcase_Mysql extends pfcContainerTestcase +{ + // constructor of the test suite + function pfcContainerTestcase_Mysql($name) + { + $this->type = "Mysql"; + $this->pfcContainerTestcase($name); + } + + // called before the test functions will be executed + // this function is defined in PHPUnit_TestCase and overwritten + // here + function setUp() + { + pfcContainerTestcase::setUp(); + } + + // called after the test functions are executed + // this function is defined in PHPUnit_TestCase and overwritten + // here + function tearDown() + { + pfcContainerTestcase::tearDown(); + } +} + +// on desactive le timeout car se script peut mettre bcp de temps a s'executer +ini_set('max_execution_time', 0); + +$suite = new PHPUnit_TestSuite(); +$suite->addTestSuite("pfcContainerTestcase_Mysql"); +$result =& PHPUnit::run($suite); +echo "<pre>"; +print_r($result->toString()); +echo "</pre>"; + +?> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |