From 12b0883686d35ddafffc801b0744cc355fa8998d Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Tue, 22 Nov 2011 21:57:25 +0000 Subject: [PATCH] Split chatsitevisitor table into chatsitevisitor and visitedpage --- src/messenger/webim/install/dbinfo.php | 12 +++++- src/messenger/webim/install/dbperform.php | 5 +++ src/messenger/webim/libs/track.php | 52 ++++++++++++----------- src/messenger/webim/operator/tracked.php | 3 +- 4 files changed, 45 insertions(+), 27 deletions(-) diff --git a/src/messenger/webim/install/dbinfo.php b/src/messenger/webim/install/dbinfo.php index 0e928373..1ea596ab 100644 --- a/src/messenger/webim/install/dbinfo.php +++ b/src/messenger/webim/install/dbinfo.php @@ -119,7 +119,6 @@ $dbtables = array( "firsttime" => "datetime NOT NULL DEFAULT 0", "lasttime" => "datetime NOT NULL DEFAULT 0", "entry" => "text NOT NULL", - "path" => "text NOT NULL", "details" => "text NOT NULL", "invited" => "tinyint(1) NOT NULL DEFAULT 0", "invitationtime" => "datetime", @@ -128,6 +127,13 @@ $dbtables = array( "chats" => "INT NOT NULL DEFAULT 0", "threadid" => "INT references ${mysqlprefix}chatthread(threadid) on delete set null" ), + + "${mysqlprefix}visitedpage" => array( + "pageid" => "INT NOT NULL auto_increment PRIMARY KEY", + "address" => "varchar(1024)", + "visittime" => "datetime NOT NULL DEFAULT 0", + "visitorid" => "INT", + ), ); $dbtables_indexes = array( @@ -136,6 +142,9 @@ $dbtables_indexes = array( ), "${mysqlprefix}chatsitevisitor" => array( "threadid" => "threadid" + ), + "${mysqlprefix}visitedpage" => array( + "visitorid" => "visitorid" ) ); @@ -150,6 +159,7 @@ $dbtables_can_update = array( "${mysqlprefix}chatgroupoperator" => array(), "${mysqlprefix}chatresponses" => array(), "${mysqlprefix}chatsitevisitor" => array(), + "${mysqlprefix}visitedpage" => array(), ); function show_install_err($text) diff --git a/src/messenger/webim/install/dbperform.php b/src/messenger/webim/install/dbperform.php index b0013e8e..c51470e0 100644 --- a/src/messenger/webim/install/dbperform.php +++ b/src/messenger/webim/install/dbperform.php @@ -168,6 +168,11 @@ if ($act == "silentcreateall") { if ($res && mysql_num_rows($res) == 0) { runsql("ALTER TABLE ${mysqlprefix}chatsitevisitor ADD INDEX (threadid)", $link); } + + $res = mysql_query("select null from information_schema.statistics where table_schema = '$mysqldb' and table_name = '${mysqlprefix}visitedpage' and index_name = 'visitorid'", $link); + if ($res && mysql_num_rows($res) == 0) { + runsql("ALTER TABLE ${mysqlprefix}visitedpage ADD INDEX (visitorid)", $link); + } } } diff --git a/src/messenger/webim/libs/track.php b/src/messenger/webim/libs/track.php index bd3753c1..aab49b0c 100644 --- a/src/messenger/webim/libs/track.php +++ b/src/messenger/webim/libs/track.php @@ -32,8 +32,8 @@ function track_visitor($visitorid, $entry, $referer, $link) return $visitor; } else { - perform_query(sprintf("update ${mysqlprefix}chatsitevisitor set lasttime = CURRENT_TIMESTAMP, path = '%s' where visitorid=" . $visitor['visitorid'], - db_escape_string(track_build_path($referer, $visitor['path']))), $link); + perform_query("update ${mysqlprefix}chatsitevisitor set lasttime = CURRENT_TIMESTAMP where visitorid=" . $visitor['visitorid'], $link); + track_visit_page($visitor['visitorid'], $referer, $link); return $visitor['visitorid']; } } @@ -44,13 +44,16 @@ function track_visitor_start($entry, $referer, $link) $visitor = visitor_from_request(); - perform_query(sprintf("insert into ${mysqlprefix}chatsitevisitor (userid, username, firsttime, lasttime, entry, path, details) values ('%s', '%s', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, '%s', '%s', '%s')", + perform_query(sprintf("insert into ${mysqlprefix}chatsitevisitor (userid, username, firsttime, lasttime, entry, details) values ('%s', '%s', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, '%s', '%s')", db_escape_string($visitor['id']), db_escape_string($visitor['name']), db_escape_string($entry), - db_escape_string(track_build_path($referer, '')), db_escape_string(track_build_details())), $link); + if ($id) { + track_visit_page($id, $referer, $link); + } + $id = db_insert_id($link); return $id ? $id : 0; } @@ -75,33 +78,34 @@ function track_get_visitor_by_threadid($threadid, $link) return $visitor; } - -function track_build_path($referer, $path) +function track_visit_page($visitorid, $page, $link) { - if ($path !== '') { - $path = unserialize($path); - krsort($path); - - list($lasttime, $lastpage) = each($path); - - if ($referer != $lastpage) { - $path[time()] = $referer; + global $mysqlprefix; + + if (empty($page)) { + return; + } + $lastpage = select_one_row(sprintf("select address from ${mysqlprefix}visitedpage where visitorid = '%s' order by visittime desc limit 1", + db_escape_string($visitorid)), $link); + if ( $lastpage['address'] != $page ) { + perform_query(sprintf("insert into ${mysqlprefix}visitedpage (visitorid, address, visittime) values ('%s', '%s', CURRENT_TIMESTAMP)", + db_escape_string($visitorid), + db_escape_string($page)), $link); } - } - else { - $path[time()] = $referer; - } - - $path = serialize($path); - return $path; } -function track_retrieve_path($visitor) +function track_get_path($visitor, $link) { - return unserialize($visitor['path']); + global $mysqlprefix; + $query_result = perform_query(sprintf("select address, UNIX_TIMESTAMP(visittime) as visittime from ${mysqlprefix}visitedpage where visitorid = '%s'", + db_escape_string($visitor['visitorid'])), $link); + $result = array(); + while( $page = db_fetch_assoc($query_result) ){ + $result[$page['visittime']] = $page['address']; + } + return $result; } - function track_build_details() { $result = array( diff --git a/src/messenger/webim/operator/tracked.php b/src/messenger/webim/operator/tracked.php index 9b6f8565..9d5f3e64 100644 --- a/src/messenger/webim/operator/tracked.php +++ b/src/messenger/webim/operator/tracked.php @@ -53,10 +53,9 @@ else { die("Wrong visitor!"); } } +$path = track_get_path($visitor, $link); close_connection($link); -$path = track_retrieve_path($visitor); - $page['entry'] = htmlspecialchars($visitor['entry']); $page['history'] = array(); ksort($path);