start building social graph

This commit is contained in:
friendica 2013-01-01 23:56:27 -08:00
parent 4d7fe15d3b
commit 50d1cb2e3a
6 changed files with 91 additions and 99 deletions

View file

@ -16,7 +16,7 @@ require_once('include/features.php');
define ( 'FRIENDICA_PLATFORM', 'Friendica Red'); define ( 'FRIENDICA_PLATFORM', 'Friendica Red');
define ( 'FRIENDICA_VERSION', trim(file_get_contents('version.inc')) . 'R'); define ( 'FRIENDICA_VERSION', trim(file_get_contents('version.inc')) . 'R');
define ( 'ZOT_REVISION', 1 ); define ( 'ZOT_REVISION', 1 );
define ( 'DB_UPDATE_VERSION', 1013 ); define ( 'DB_UPDATE_VERSION', 1014 );
define ( 'EOL', '<br />' . "\r\n" ); define ( 'EOL', '<br />' . "\r\n" );
define ( 'ATOM_TIME', 'Y-m-d\TH:i:s\Z' ); define ( 'ATOM_TIME', 'Y-m-d\TH:i:s\Z' );

View file

@ -94,19 +94,17 @@ function onepoll_run($argv, $argc){
// set last updated timestamp // set last updated timestamp
/* $r = null;
if($contact['poco']) {
$r = q("SELECT count(*) as total from glink if($contact['xchan_connurl']) {
where `cid` = %d and updated > UTC_TIMESTAMP() - INTERVAL 1 DAY", $r = q("SELECT xlink_id from xlink
intval($contact['id']) where xlink_xchan = '%s'and updated > UTC_TIMESTAMP() - INTERVAL 1 DAY",
intval($contact['xchan_hash'])
); );
} }
if(count($r)) { if($r) {
if(! $r[0]['total']) { poco_load($contact['xchan_hash'],$contact['xchan_connurl']);
poco_load($contact['id'],$importer_uid,0,$contact['poco']);
}
} }
*/
return; return;
} }

View file

@ -20,151 +20,120 @@ require_once('include/datetime.php');
function poco_load($cid, $uid = 0,$zcid = 0,$url = null) { function poco_load($xchan = null,$url = null) {
$a = get_app(); $a = get_app();
if($cid) { if($xchan && ! $url) {
if((! $url) || (! $uid)) { $r = q("select xchan_connurl from xchan where xchan_hash = %d limit 1",
$r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1", intval($xchan)
intval($cid) );
); if($r) {
if(count($r)) { $url = $r[0]['xchan_connurl'];
$url = $r[0]['poco']; $uid = $r[0]['abook_channel'];
$uid = $r[0]['uid'];
}
} }
if(! $uid)
return;
} }
if(! $url) if(! $url)
return; return;
$url = $url . (($uid) ? '/@me/@all?fields=displayName,urls,photos' : '?fields=displayName,urls,photos') ;
$url = $url . '?fields=displayName,hash,urls,photos' ;
logger('poco_load: ' . $url, LOGGER_DEBUG); logger('poco_load: ' . $url, LOGGER_DEBUG);
$s = fetch_url($url); $s = z_fetch_url($url);
logger('poco_load: returns ' . $s, LOGGER_DATA); logger('poco_load: returns ' . print_r($s,true), LOGGER_DATA);
logger('poco_load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG); if(! $s['success'])
if(($a->get_curl_code() > 299) || (! $s))
return; return;
$j = json_decode($s); $j = json_decode($s['body'],true);
logger('poco_load: json: ' . print_r($j,true),LOGGER_DATA); logger('poco_load: ' . print_r($j,true),LOGGER_DATA);
if(! isset($j->entry)) if(! x($j,'entry') && is_array($j['entry']))
return; return;
$total = 0; $total = 0;
foreach($j->entry as $entry) { foreach($j['entry'] as $entry) {
$total ++; $total ++;
$profile_url = ''; $profile_url = '';
$profile_photo = ''; $profile_photo = '';
$connect_url = ''; $address = '';
$name = ''; $name = '';
$hash = '';
$name = $entry->displayName; $name = $entry['displayName'];
$hash = $entry['hash'];
if(isset($entry->urls)) { if(x($entry,'urls') && is_array($entry['urls'])) {
foreach($entry->urls as $url) { foreach($entry['urls'] as $url) {
if($url->type == 'profile') { if($url['type'] == 'profile') {
$profile_url = $url->value; $profile_url = $url['value'];
continue; continue;
} }
if($url->type == 'webfinger') { if($url['type'] == 'zot') {
$connect_url = str_replace('acct:' , '', $url->value); $address = str_replace('acct:' , '', $url['value']);
continue; continue;
} }
} }
} }
if(isset($entry->photos)) { if(x($entry,'photos') && is_array($entry['photos'])) {
foreach($entry->photos as $photo) { foreach($entry['photos'] as $photo) {
if($photo->type == 'profile') { if($photo['type'] == 'profile') {
$profile_photo = $photo->value; $profile_photo = $photo['value'];
continue; continue;
} }
} }
} }
if((! $name) || (! $profile_url) || (! $profile_photo)) if((! $name) || (! $profile_url) || (! $profile_photo) || (! $hash) || (! $address))
continue; continue;
$x = q("select * from `gcontact` where `nurl` = '%s' limit 1", $x = q("select xchan_hash from xchan where xchan_hash = '%s' limit 1",
dbesc(normalise_link($profile_url)) dbesc($hash)
); );
if(count($x)) { // We've never seen this person before. Import them.
$gcid = $x[0]['id'];
if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo) { if(($x !== false) && (! count($x))) {
q("update gcontact set `name` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s' if($address) {
where `nurl` = '%s' limit 1", $z = zot_finger($address,null);
dbesc($name), if($z['success']) {
dbesc($profile_photo), $j = json_decode($z['body'],true);
dbesc($connect_url), if($j)
dbesc($profile_url), import_xchan($j);
dbesc(normalise_link($profile_url)) }
);
} }
} }
else {
q("insert into `gcontact` (`name`,`url`,`nurl`,`photo`,`connect`)
values ( '%s', '%s', '%s', '%s','%s') ",
dbesc($name),
dbesc($profile_url),
dbesc(normalise_link($profile_url)),
dbesc($profile_photo),
dbesc($connect_url)
);
$x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
dbesc(normalise_link($profile_url))
);
if(count($x))
$gcid = $x[0]['id'];
}
if(! $gcid)
return;
$r = q("select * from glink where `cid` = %d and `uid` = %d and `gcid` = %d and `zcid` = %d limit 1", $r = q("select * from xlink where xlink_xchan = '%s' and xlink_link = '%s' limit 1",
intval($cid), dbesc($xchan),
intval($uid), dbesc($hash)
intval($gcid),
intval($zcid)
); );
if(! count($r)) { if(! $r) {
q("insert into glink ( `cid`,`uid`,`gcid`,`zcid`, `updated`) values (%d,%d,%d,%d, '%s') ", q("insert into xlink ( xlink_xchan, xlink_link, xlink_updated ) values ( '%s', '%s', '%s' ) ",
intval($cid), dbesc($xchan),
intval($uid), dbesc($hash),
intval($gcid),
intval($zcid),
dbesc(datetime_convert()) dbesc(datetime_convert())
); );
} }
else { else {
q("update glink set updated = '%s' where `cid` = %d and `uid` = %d and `gcid` = %d and zcid = %d limit 1", q("update xlink set xlink_updated = '%s' where xlink_id = %d limit 1",
dbesc(datetime_convert()), dbesc(datetime_convert()),
intval($cid), intval($r[0]['xlink_id'])
intval($uid),
intval($gcid),
intval($zcid)
); );
} }
} }
logger("poco_load: loaded $total entries",LOGGER_DEBUG); logger("poco_load: loaded $total entries",LOGGER_DEBUG);
q("delete from glink where `cid` = %d and `uid` = %d and `zcid` = %d and `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY", q("delete from xlink where xlink_xchan = '%s' and xlink_updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
intval($cid), dbesc($xchan)
intval($uid),
intval($zcid)
); );
} }

View file

@ -846,6 +846,17 @@ CREATE TABLE IF NOT EXISTS `xchan` (
KEY `xchan_connurl` (`xchan_connurl`) KEY `xchan_connurl` (`xchan_connurl`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8; ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `xlink` (
`xlink_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`xlink_xchan` char(255) NOT NULL DEFAULT '',
`xlink_link` char(255) NOT NULL DEFAULT '',
`xlink_updated` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`xlink_id`),
KEY `xlink_xchan` ( `xlink_xchan` ),
KEY `xlink_link` ( `xlink_link` ),
KEY `xlink_updated` ( `xlink_updated` )
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `xprof` ( CREATE TABLE IF NOT EXISTS `xprof` (
`xprof_hash` char(255) NOT NULL, `xprof_hash` char(255) NOT NULL,
`xprof_desc` char(255) NOT NULL DEFAULT '', `xprof_desc` char(255) NOT NULL DEFAULT '',

View file

@ -1,6 +1,6 @@
<?php <?php
define( 'UPDATE_VERSION' , 1013 ); define( 'UPDATE_VERSION' , 1014 );
/** /**
* *
@ -197,3 +197,17 @@ ADD INDEX ( `xchan_connurl` )");
return UPDATE_SUCCESS; return UPDATE_SUCCESS;
return UPDATE_FAILED; return UPDATE_FAILED;
} }
function update_r1013() {
$r = q"CREATE TABLE if not exists `xlink` (
`xlink_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`xlink_xchan` CHAR( 255 ) NOT NULL DEFAULT '',
`xlink_link` CHAR( 255 ) NOT NULL DEFAULT '',
`xlink_updated` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE = MYISAM ");
$r2 = q("alter table xlink add index ( xlink_xchan ), add index ( xlink link ), add index ( xlink_updated ) ");
if($r && $r2)
return UPDATE_SUCCESS;
return UPDATE_FAILED;
}

View file

@ -1 +1 @@
2012-12-31.186 2013-01-01.187