Tadashi Okoshi
slash****@users*****
2005年 6月 26日 (日) 20:46:06 JST
Index: affelio/lib/Affelio/SNS/FriendManager.pm diff -u affelio/lib/Affelio/SNS/FriendManager.pm:1.2 affelio/lib/Affelio/SNS/FriendManager.pm:1.3 --- affelio/lib/Affelio/SNS/FriendManager.pm:1.2 Sat Jun 25 21:14:07 2005 +++ affelio/lib/Affelio/SNS/FriendManager.pm Sun Jun 26 20:46:06 2005 @@ -4,7 +4,7 @@ # http://affelio.jp/ (Japan) # http://affelio.jp/ (USA and other area) # -# $Id: FriendManager.pm,v 1.2 2005/06/25 12:14:07 slash5234 Exp $ +# $Id: FriendManager.pm,v 1.3 2005/06/26 11:46:06 slash5234 Exp $ package Affelio::SNS::FriendManager; { @@ -37,8 +37,147 @@ } ######################################################################## + #add_friend + ######################################################################## + sub add_friend{ #returns uid (int) + my $self = shift; + my $af_id = shift; #arg(1) af_id (string) + my $nickname = shift; #arg(2) nickname (string) + my $timestamp = shift; #arg(3) timestamp (int) + my $password = shift; #arg(4) password (string) + + Affelio::misc::Debug::debug_print("FM::add_friend: ($af_id $nickname $timestamp $password)"); + + my $af = $self->{af}; + my $sth; + my $query; + my @row; + my $maxid; + my $newid; + my $old_uid_in_f2tbl=""; + my $old_f1list_in_f2tbl=","; + + ############################## + #Is ($af_id) already in my F1 table? + #If so, we DO NOT ignore him. + #We have to update his information. + # + $query = "SELECT * FROM AFuser_CORE_friends WHERE af_id = '$af_id'"; + $sth = $af->{db}->prepare($query) or die $af->{db}->errstr; + $sth->execute() or die $af->{db}->errstr; + @row = $sth->fetchrow_array; + if(@row && @row!=() ){ + + Affelio::misc::Debug::debug_print("FM::add_friend: Already my friend!"); + + $query = "update AFuser_CORE_friends set timestamp = '$timestamp', password = '$password' where af_id = '$af_id'"; + $sth = $af->{db}->prepare($query) or die $af->{db}->errstr; + $sth->execute() or die $af->{db}->errstr; + + Affelio::misc::Debug::debug_print("FM::add_friend:\tUpdated an existing friend."); + Affelio::misc::Debug::debug_print("FM::add_friend: end($row[0])."); + ################ + #Return + return $row[0]; #return uid. + } + + ############################## + #Get existing max ID + $query = 'SELECT max(uid) FROM AFuser_CORE_friends'; + $sth = $af->{db}->prepare($query) or die $af->{db}->errstr; + $sth->execute() or die $af->{db}->errstr; + @row = $sth->fetchrow_array; + $maxid = $row[0]; + if(defined($row[0])){ + $maxid = $row[0]; + }else{ + $maxid = 0; + } + $newid = $maxid + 1; + Affelio::misc::Debug::debug_print("FM::add_friend: new_ID= [$newid]"); + + ############################## + #Is $af_id already in F2 table? + #If so, we have to move it from F2 table to F1 table. + $query = "SELECT * FROM AFuser_CORE_friendsfriends WHERE af_id = '$af_id'"; + $sth = $af->{db}->prepare($query) or die $af->{db}->errstr; + $sth->execute() or die $af->{db}->errstr; + @row = $sth->fetchrow_array; + if(@row && @row !=() ){ + ############################################ + #I found the record in F2 table. + #Let's move it to the F1 table. + Affelio::misc::Debug::debug_print("FM::add_friend:\t [@row]"); + Affelio::misc::Debug::debug_print("FM::add_friend:\tHe is my friends' friend."); + Affelio::misc::Debug::debug_print("FM::add_friend:\tLet's move him."); + + #Backup data + $old_uid_in_f2tbl = $row[0]; + $old_f1list_in_f2tbl = $row[4]; + + #Delete the record in the F2 table. + $query = "DELETE FROM AFuser_CORE_friendsfriends WHERE uid = $old_uid_in_f2tbl"; + $sth = $af->{db}->prepare($query) or die $af->{db}->errstr; + $sth->execute() or die $af->{db}->errstr; + Affelio::misc::Debug::debug_print("FM::add_friend:\tRecord($old_uid_in_f2tbl) in F2 tbl deleted."); + + #Update all other F1 and F2 entries' "flist" column + # from $old_uid_in_f2tbl to $newid + # update sample set data=translate(data,'b','z') + db_value_replace($af->{db}, + "AFuser_CORE_friendsfriends", + "uid", + "f1list", + ",$old_uid_in_f2tbl," , + ",$newid," , + ); + + db_value_replace($af->{db}, + "AFuser_CORE_friends", + "uid", + "f2list", + ",$old_uid_in_f2tbl," , + ",$newid," , + ); + + Affelio::misc::Debug::debug_print("FM::add_friend:\tOverwritten id($old_uid_in_f2tbl) -> id($newid)"); + } + + + ############################## + #Insert a new record into my F1 list. + $query = "insert into AFuser_CORE_friends(uid, af_id, nickname, timestamp, password, intro, option_pid,lastupdated,f2list) values ($newid, '$af_id', '$nickname', $timestamp, '$password', ' ', -1, 0, '$old_f1list_in_f2tbl')"; + $sth = $af->{db}->prepare($query) or die $af->{db}->errstr; + $sth->execute() or die $af->{db}->errstr; + + Affelio::misc::Debug::debug_print("FM::add_friend:\tInserted a new record."); + Affelio::misc::Debug::debug_print("FM::add_friend: end($newid)."); + return($newid); + } + + + ######################################################################## + #remove_friend + ######################################################################## + sub remove_friend{ #void + my $self = shift; + my $af_id = shift; #arg(1) uid (int) + + my $af = $self->{af}; + + #$af_id = $af->{db}->quote($af_id); + + #XXX + #Not implemented yet. + } + + + ######################################################################## #save_F2List ######################################################################## + # We retrieve "friend list" from my friends. + # That "friend list" is my "F2 list" (through the friend). + # Then, save_F2list is invoked when the retrieved F2 list gets stored. sub save_F2List{ my $self = shift; my $datain = shift; #arg(1) data input @@ -156,7 +295,8 @@ ############################## #Insert a new record - $query = "insert into AFuser_CORE_friendsfriends (uid, af_id, nickname, timestamp, f1list) values ('$minid-1', '$f2_af_id', '$nickname', $timestamp, ',')"; + my $tmpnewid = $minid-1; + $query = "insert into AFuser_CORE_friendsfriends (uid, af_id, nickname, timestamp, f1list) values ('$tmpnewid', '$f2_af_id', '$nickname', $timestamp, ',')"; $sth = $af->{db}->prepare($query) or die $af->{db}->errstr; # $sth->execute($minid-1, $f2_af_id, $nickname, $timestamp, ",") or die $af->{db}->errstr; $sth->execute() or die $af->{db}->errstr; @@ -217,7 +357,6 @@ } - ######################################################################## #get_updated_friends ######################################################################## @@ -254,7 +393,6 @@ } - ######################################################################## #get_all_friend_list ######################################################################## @@ -273,130 +411,6 @@ ######################################################################## - #add_friend - ######################################################################## - sub add_friend{ #returns uid (int) - my $self = shift; - my $af_id = shift; #arg(1) af_id (string) - my $nickname = shift; #arg(2) nickname (string) - my $timestamp = shift; #arg(3) timestamp (int) - my $password = shift; #arg(4) password (string) - - Affelio::misc::Debug::debug_print("FM::add_friend: ($af_id $nickname $timestamp $password)"); - - my $af = $self->{af}; - my $sth; - my $query; - my @row; - my $maxid; - my $newid; - my $old_uid_in_f2tbl=""; - my $old_f1list_in_f2tbl=","; - - ############################## - #Is $af_id already in F1 table? - #If so, DO NOT IGNORE HIM! We have to update his information. - # - $query = "SELECT * FROM AFuser_CORE_friends WHERE af_id = '$af_id'"; - $sth = $af->{db}->prepare($query) or die $af->{db}->errstr; - $sth->execute() or die $af->{db}->errstr; - @row = $sth->fetchrow_array; - if(@row && @row!=() ){ - - Affelio::misc::Debug::debug_print("FM::add_friend: Already my friend!"); - - $query = "update AFuser_CORE_friends set timestamp = '$timestamp', password = '$password' where af_id = '$af_id'"; - $sth = $af->{db}->prepare($query) or die $af->{db}->errstr; - $sth->execute() or die $af->{db}->errstr; - - Affelio::misc::Debug::debug_print("FM::add_friend:\tUpdated an existing friend."); - Affelio::misc::Debug::debug_print("FM::add_friend: end($row[0])."); - return $row[0]; #return uid. - } - - ############################## - #Get existing max ID - $query = 'SELECT max(uid) FROM AFuser_CORE_friends'; - $sth = $af->{db}->prepare($query) or die $af->{db}->errstr; - $sth->execute() or die $af->{db}->errstr; - @row = $sth->fetchrow_array; - $maxid = $row[0]; - if(defined($row[0])){ - $maxid = $row[0]; - }else{ - $maxid = 0; - } - $newid = $maxid + 1; - Affelio::misc::Debug::debug_print("FM::add_friend: new_ID= [$newid]"); - - ############################## - #Is $af_id already in F2 table? - #If so, we have to move it from F2 table to F1 table. - $query = "SELECT * FROM AFuser_CORE_friendsfriends WHERE af_id = '$af_id'"; - $sth = $af->{db}->prepare($query) or die $af->{db}->errstr; - $sth->execute() or die $af->{db}->errstr; - @row = $sth->fetchrow_array; - if(@row && @row !=() ){ - ############################################ - #I found the record in F2 table. - #Let's move it to the F1 table. - Affelio::misc::Debug::debug_print("FM::add_friend:\t [@row]"); - Affelio::misc::Debug::debug_print("FM::add_friend:\tHe is my friends' friend."); - Affelio::misc::Debug::debug_print("FM::add_friend:\tLet's move him."); - - #Backup data - $old_uid_in_f2tbl = $row[0]; - $old_f1list_in_f2tbl = $row[4]; - - #Delete the record in the F2 table. - $query = "DELETE FROM AFuser_CORE_friendsfriends WHERE uid = $old_uid_in_f2tbl"; - $sth = $af->{db}->prepare($query) or die $af->{db}->errstr; - $sth->execute() or die $af->{db}->errstr; - Affelio::misc::Debug::debug_print("FM::add_friend:\tRecord($old_uid_in_f2tbl) in F2 tbl deleted."); - - #Update all other F1 and F2 entries' "flist" column - # from $old_uid_in_f2tbl to $newid - # update sample set data=translate(data,'b','z') - $query = "UPDATE AFuser_CORE_friendsfriends set f1list = replace(f1list, ',$old_uid_in_f2tbl,', ',$newid,')"; - $sth = $af->{db}->prepare($query) or Affelio::misc::Debug::debug_print("FM::add_friend:\t" . $af->{db}->errstr);die $af->{db}->errstr; - $sth->execute() or Affelio::misc::Debug::debug_print("FM::add_friend:\t" . $af->{db}->errstr);die $af->{db}->errstr; - $query = "UPDATE AFuser_CORE_friends set f2list = replace(f2list, ',$old_uid_in_f2tbl,', ',$newid,')"; - $sth = $af->{db}->prepare($query) or Affelio::misc::Debug::debug_print("FM::add_friend:\t" . $af->{db}->errstr);die $af->{db}->errstr; - $sth->execute() or Affelio::misc::Debug::debug_print("FM::add_friend:\t" . $af->{db}->errstr);die $af->{db}->errstr; - - Affelio::misc::Debug::debug_print("FM::add_friend:\tOverwritten id($old_uid_in_f2tbl) -> id($newid)"); - - } - - ############################## - #Insert a new record - $query = "insert into AFuser_CORE_friends(uid, af_id, nickname, timestamp, password, intro, option_pid,lastupdated,f2list) values ($newid, '$af_id', '$nickname', $timestamp, '$password', ' ', -1, 0, '$old_f1list_in_f2tbl')"; - $sth = $af->{db}->prepare($query) or die $af->{db}->errstr; - $sth->execute() or die $af->{db}->errstr; - - Affelio::misc::Debug::debug_print("FM::add_friend:\tInserted a new record."); - Affelio::misc::Debug::debug_print("FM::add_friend: end($newid)."); - return($newid); - } - - - ######################################################################## - #remove_friend - ######################################################################## - sub remove_friend{ #void - my $self = shift; - my $af_id = shift; #arg(1) uid (int) - - my $af = $self->{af}; - - #$af_id = $af->{db}->quote($af_id); - - #XXX - #Not implemented yet. - } - - - ######################################################################## #get_friend_by_uid # arg(1) uid # returns array(0uid, 1af_id, 2nickname, 3timestamp, @@ -418,6 +432,7 @@ return(@row); } + ######################################################################## #get_friend_by_afid # arg(1) afid @@ -440,6 +455,7 @@ return(@row); } + ######################################################################## #get_attribute_by_uid ######################################################################## @@ -452,16 +468,21 @@ ############################## #retrieve a friend record from DB - my $query = "SELECT $attr FROM AFuser_CORE_friends WHERE uid = $uid"; - my $sth = $af->{db}->prepare($query) or Affelio::misc::Debug::debug_print("FM::add_friend:\t" . $af->{db}->errstr);die $af->{db}->errstr; - - $sth->execute() or Affelio::misc::Debug::debug_print("FM::add_friend:\t" . $af->{db}->errstr);die $af->{db}->errstr; + Affelio::misc::Debug::debug_print("FM::get_attr_by_uid(uid=$uid, attr=$attr)"); + my $query = "SELECT $attr FROM AFuser_CORE_friends WHERE uid = '$uid'"; + my $sth = $af->{db}->prepare($query); + if($@){ + Affelio::misc::Debug::debug_print("FM::add_friend:\t"); + die $af->{db}->errstr; + } + $sth->execute(); my @row = $sth->fetchrow_array; return($row[0]); } + ######################################################################## #F2_get_attribute_by_uid ######################################################################## @@ -481,7 +502,6 @@ return($row[0]); } - ######################################################################## #get_attribute_by_afid ######################################################################## @@ -524,11 +544,75 @@ ############################## #set value into DB - my $query = "update AFuser_CORE_friends set $attr = '$value' where uid = '$uid'"; + my $query = "update AFuser_CORE_friends set $attr = '$value' where uid = $uid"; my $sth = $af->{db}->prepare($query) or die $af->{db}->errstr; $sth->execute or die $af->{db}->errstr; } + sub db_value_replace{ + my $db = shift; + my $dbname = shift; + my $key_col = shift; + my $target_col = shift; + my $old = shift; + my $new = shift; + + Affelio::misc::Debug::debug_print("FM::db_replace: start."); + ###################################### + #Retrieve data + ###################################### + my $q1 = "SELECT $key_col, $target_col FROM $dbname"; + + Affelio::misc::Debug::debug_print("FM::db_replace: [$q1]"); + + my $s1 = $db->prepare($q1); + if($@){ + Affelio::misc::Debug::debug_print("FM::db_replace:".$db->errstr); + die $db->errstr; + } + + $s1->execute(); + if($@){ + Affelio::misc::Debug::debug_print("FM::db_replace:".$db->errstr); + die $db->errstr; + } + + ###################################### + #Distill each line, replace, and store + ###################################### + Affelio::misc::Debug::debug_print("FM::db_replace: distilling..."); + my $key_data; + my $target_data; + my $target_data_new; + while( ($key_data, $target_data) = $s1->fetchrow_array){ + $target_data_new = $target_data; + $old =~ s/\-/\\\-/g; + $target_data_new =~ s/$old/$new/g; + + Affelio::misc::Debug::debug_print("FM::db_replace: [$key_data] [$target_data]->[$target_data_new]"); + + my $q2 = "update $dbname set $target_col = '$target_data_new' where $key_col = $key_data"; + Affelio::misc::Debug::debug_print("FM::db_replace: [$q2]"); + + my $s2 = $db->prepare($q2); + if($@){ + Affelio::misc::Debug::debug_print("FM::db_replace:".$db->errstr); + die $db->errstr; + } + + $s2->execute(); + if($@){ + Affelio::misc::Debug::debug_print("FM::db_replace:".$db->errstr); + die $db->errstr; + } + + } + + Affelio::misc::Debug::debug_print("FM::db_replace: end."); + } + + + ######################################################################## }#package Index: affelio/lib/Affelio/SNS/Handshaker_s.pm diff -u affelio/lib/Affelio/SNS/Handshaker_s.pm:1.2 affelio/lib/Affelio/SNS/Handshaker_s.pm:1.3 --- affelio/lib/Affelio/SNS/Handshaker_s.pm:1.2 Sat Jun 25 21:14:07 2005 +++ affelio/lib/Affelio/SNS/Handshaker_s.pm Sun Jun 26 20:46:06 2005 @@ -4,7 +4,7 @@ # http://affelio.jp/ (Japan) # http://affelio.jp/ (USA and other area) # -# $Id: Handshaker_s.pm,v 1.2 2005/06/25 12:14:07 slash5234 Exp $ +# $Id: Handshaker_s.pm,v 1.3 2005/06/26 11:46:06 slash5234 Exp $ use strict; use XMLRPC::Transport::HTTP; @@ -210,18 +210,18 @@ #Get peer's friends list. ########################################### # "peer's friends" = my F2 friends -# Affelio::misc::Debug::debug_print("server.HandshakeReply: Let's download peer's flist!"); -# my $ret = Affelio::SNS::Handshaker_c::get_F2List(dest_uri => "$peer_af_id/bin/xml-rpc-serv.cgi", proto_ver => 1, timestamp => 0); -# Affelio::misc::Debug::debug_print("server.HandshakeReply: get_F2List finished."); -# Affelio::misc::Debug::debug_print("server.HandshakeReply: List I've got is [$ret]"); + Affelio::misc::Debug::debug_print("server.HandshakeReply: Let's download peer's flist!"); + my $ret = Affelio::SNS::Handshaker_c::get_F2List(dest_uri => "$peer_af_id/bin/xml-rpc-serv.cgi", proto_ver => 1, timestamp => 0); + Affelio::misc::Debug::debug_print("server.HandshakeReply: get_F2List finished."); + Affelio::misc::Debug::debug_print("server.HandshakeReply: List I've got is [$ret]"); ########################################### #Save the F2 list into my DB ########################################### -# $af = Affelio::SNS::Handshaker_s::Util::af_new(); -# Affelio::misc::Debug::debug_print("server.HandshakeReply: Let's save peer's flist!"); -# $af->{fm}->save_F2List($ret, $peer_af_id); -# Affelio::misc::Debug::debug_print("server.HandshakeReply: save_F2List finished."); + $af = Affelio::SNS::Handshaker_s::Util::af_new(); + Affelio::misc::Debug::debug_print("server.HandshakeReply: Let's save peer's flist!"); + $af->{fm}->save_F2List($ret, $peer_af_id); + Affelio::misc::Debug::debug_print("server.HandshakeReply: save_F2List finished."); #Make a new instance of Affelio