Use batch requests when syncing friends; this reduces the time for synchronization a lot

This commit is contained in:
Tobias Hößl 2012-04-09 09:42:14 +00:00
parent 8a3643e8db
commit 1df6f59127

73
facebook/facebook.php Executable file → Normal file
View File

@ -197,7 +197,6 @@ function facebook_init(&$a) {
$auth_code = (x($_GET, 'code') ? $_GET['code'] : '');
$error = (x($_GET, 'error_description') ? $_GET['error_description'] : '');
if($error)
logger('facebook_init: Error: ' . $error);
@ -243,7 +242,9 @@ function fb_get_self($uid) {
}
}
function fb_get_friends_sync_new($uid, $access_token, $person) {
function fb_get_friends_sync_new($uid, $access_token, $persons) {
$persons_todo = array();
foreach ($persons as $person) {
$link = 'http://facebook.com/profile.php?id=' . $person->id;
$r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
@ -253,30 +254,25 @@ function fb_get_friends_sync_new($uid, $access_token, $person) {
if (count($r) == 0) {
logger('fb_get_friends: new contact found: ' . $link, LOGGER_DEBUG);
$persons_todo[] = $person;
}
fb_get_friends_sync_full($uid, $access_token, $person);
if (count($persons_todo) > 0) fb_get_friends_sync_full($uid, $access_token, $persons_todo);
}
}
function fb_get_friends_sync_full($uid, $access_token, $person) {
$s = fetch_url('https://graph.facebook.com/' . $person->id . '?access_token=' . $access_token);
if($s) {
$jp = json_decode($s);
logger('fb_get_friends: info: ' . print_r($jp,true), LOGGER_DATA);
// always use numeric link for consistency
$jp->link = 'http://facebook.com/profile.php?id=' . $person->id;
function fb_get_friends_sync_parsecontact($uid, $contact) {
$contact->link = 'http://facebook.com/profile.php?id=' . $contact->id;
// If its a page then set the first name from the username
if (!$jp->first_name and $jp->username)
$jp->first_name = $jp->username;
if (!$contact->first_name and $contact->username)
$contact->first_name = $contact->username;
// check if we already have a contact
$r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
intval($uid),
dbesc($jp->link)
dbesc($contact->link)
);
if(count($r)) {
@ -286,7 +282,7 @@ function fb_get_friends_sync_full($uid, $access_token, $person) {
if((! $r[0]['photo']) || (! $r[0]['thumb']) || (! $r[0]['micro'])) {
require_once("Photo.php");
$photos = import_profile_photo('https://graph.facebook.com/' . $jp->id . '/picture', $uid, $r[0]['id']);
$photos = import_profile_photo('https://graph.facebook.com/' . $contact->id . '/picture', $uid, $r[0]['id']);
$r = q("UPDATE `contact` SET `photo` = '%s',
`thumb` = '%s',
@ -316,15 +312,15 @@ function fb_get_friends_sync_full($uid, $access_token, $person) {
VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ",
intval($uid),
dbesc(datetime_convert()),
dbesc($jp->link),
dbesc(normalise_link($jp->link)),
dbesc($contact->link),
dbesc(normalise_link($contact->link)),
dbesc(''),
dbesc(''),
dbesc($jp->id),
dbesc('facebook ' . $jp->id),
dbesc($jp->name),
dbesc(($jp->nickname) ? $jp->nickname : strtolower($jp->first_name)),
dbesc('https://graph.facebook.com/' . $jp->id . '/picture'),
dbesc($contact->id),
dbesc('facebook ' . $contact->id),
dbesc($contact->name),
dbesc(($contact->nickname) ? $contact->nickname : strtolower($contact->first_name)),
dbesc('https://graph.facebook.com/' . $contact->id . '/picture'),
dbesc(NETWORK_FACEBOOK),
intval(CONTACT_IS_FRIEND),
intval(1),
@ -333,7 +329,7 @@ function fb_get_friends_sync_full($uid, $access_token, $person) {
}
$r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
dbesc($jp->link),
dbesc($contact->link),
intval($uid)
);
@ -364,7 +360,23 @@ function fb_get_friends_sync_full($uid, $access_token, $person) {
dbesc(datetime_convert()),
intval($contact_id)
);
}
function fb_get_friends_sync_full($uid, $access_token, $persons) {
if (count($persons) == 0) return;
$nums = Ceil(count($persons) / 50);
for ($i = 0; $i < $nums; $i++) {
$batch_request = array();
for ($j = $i * 50; $j < ($i+1) * 50 && $j < count($persons); $j++) $batch_request[] = array('method'=>'GET', 'relative_url'=>$persons[$j]->id);
$s = post_url('https://graph.facebook.com/', array('access_token' => $access_token, 'batch' => json_encode($batch_request)));
if($s) {
$results = json_decode($s);
logger('fb_get_friends: info: ' . print_r($results,true), LOGGER_DATA);
foreach ($results as $contact) {
if ($contact->code != 200) logger('fb_get_friends: not found: ' . print_r($contact,true), LOGGER_DEBUG);
else fb_get_friends_sync_parsecontact($uid, json_decode($contact->body));
}
}
}
}
@ -393,11 +405,14 @@ function fb_get_friends($uid, $fullsync = true) {
logger('facebook: fb_get_friends: json: ' . print_r($j,true), LOGGER_DATA);
if(! $j->data)
return;
foreach($j->data as $person)
$persons_todo = array();
foreach($j->data as $person) $persons_todo[] = $person;
if ($fullsync)
fb_get_friends_sync_full($uid, $access_token, $person);
fb_get_friends_sync_full($uid, $access_token, $persons_todo);
else
fb_get_friends_sync_new($uid, $access_token, $person);
fb_get_friends_sync_new($uid, $access_token, $persons_todo);
}
}
@ -477,7 +492,7 @@ function facebook_content(&$a) {
if (get_pconfig(local_user(),'facebook','post')) {
$access_token = get_pconfig(local_user(),'facebook','access_token');
if ($access_token) {
$private_wall = intval(get_pconfig($uid,'facebook','private_wall'));
$private_wall = intval(get_pconfig(local_user(),'facebook','private_wall'));
$s = fetch_url('https://graph.facebook.com/me/feed?access_token=' . $access_token);
if($s) {
$j = json_decode($s);
@ -1179,7 +1194,7 @@ function fb_queue_hook(&$a,&$b) {
}
function fb_get_timeline($access_token, &$since) {
$entries = new stdClass();
$entries->data = array();
$newest = 0;