Parse an hcard for hints, if available

This commit is contained in:
Evan Prodromou 2010-02-25 19:58:51 -05:00
parent cc0670791e
commit 894b221e8a

View File

@ -644,7 +644,6 @@ class Ostatus_profile extends Memcached_DataObject
'groups' => array(),
'tags' => array());
// Check for optional attributes...
if (!empty($activity->time)) {
@ -1155,7 +1154,13 @@ class Ostatus_profile extends Memcached_DataObject
$orig = clone($profile);
$profile->nickname = self::getActivityObjectNickname($object, $hints);
if (!empty($object->title)) {
$profile->fullname = $object->title;
} else if (array_key_exists('fullname', $hints)) {
$profile->fullname = $hints['fullname'];
}
if (!empty($object->link)) {
$profile->profileurl = $object->link;
} else if (array_key_exists('profileurl', $hints)) {
@ -1228,14 +1233,18 @@ class Ostatus_profile extends Memcached_DataObject
{
$location = null;
if (!empty($object->poco)) {
if (isset($object->poco->address->formatted)) {
if (!empty($object->poco) &&
isset($object->poco->address->formatted)) {
$location = $object->poco->address->formatted;
} else if (array_key_exists('location', $hints)) {
$location = $hints['location'];
}
if (!empty($location)) {
if (mb_strlen($location) > 255) {
$location = mb_substr($note, 0, 255 - 3) . ' … ';
}
}
}
// @todo Try to find location some othe way? Via goerss point?
@ -1248,15 +1257,18 @@ class Ostatus_profile extends Memcached_DataObject
if (!empty($object->poco)) {
$note = $object->poco->note;
} else if (array_key_exists('bio', $hints)) {
$note = $hints['bio'];
}
if (!empty($note)) {
if (mb_strlen($note) > Profile::maxBio()) {
if (Profile::bioTooLong($note)) {
// XXX: truncate ok?
$bio = mb_substr($note, 0, Profile::maxBio() - 3) . ' … ';
} else {
$bio = $note;
}
}
}
// @todo Try to get bio info some other way?
@ -1270,10 +1282,15 @@ class Ostatus_profile extends Memcached_DataObject
return common_nicknamize($object->poco->preferredUsername);
}
}
if (!empty($object->nickname)) {
return common_nicknamize($object->nickname);
}
if (array_key_exists('nickname', $hints)) {
return $hints['nickname'];
}
// Try the definitive ID
$nickname = self::nicknameFromURI($object->id);
@ -1347,6 +1364,9 @@ class Ostatus_profile extends Memcached_DataObject
case Webfinger::UPDATESFROM:
$feedUrl = $link['href'];
break;
case Webfinger::HCARD:
$hcardUrl = $link['href'];
break;
default:
common_log(LOG_NOTICE, "Don't know what to do with rel = '{$link['rel']}'");
break;
@ -1358,6 +1378,12 @@ class Ostatus_profile extends Memcached_DataObject
'feedurl' => $feedUrl,
'salmon' => $salmonEndpoint);
if (isset($hcardUrl)) {
$hcardHints = self::slurpHcard($hcardUrl);
// Note: Webfinger > hcard
$hints = array_merge($hcardHints, $hints);
}
// If we got a feed URL, try that
if (isset($feedUrl)) {
@ -1464,4 +1490,67 @@ class Ostatus_profile extends Memcached_DataObject
return $file;
}
protected static function slurpHcard($url)
{
set_include_path(get_include_path() . PATH_SEPARATOR . INSTALLDIR . '/plugins/OStatus/extlib/hkit/');
require_once('hkit.class.php');
$h = new hKit;
// Google Buzz hcards need to be tidied. Probably others too.
$h->tidy_mode = 'proxy'; // 'proxy', 'exec', 'php' or 'none'
// Get by URL
$hcards = $h->getByURL('hcard', $url);
if (empty($hcards)) {
return array();
}
// @fixme more intelligent guess on multi-hcard pages
$hcard = $hcards[0];
$hints = array();
$hints['profileurl'] = $url;
if (array_key_exists('nickname', $hcard)) {
$hints['nickname'] = $hcard['nickname'];
}
if (array_key_exists('fn', $hcard)) {
$hints['fullname'] = $hcard['fn'];
} else if (array_key_exists('n', $hcard)) {
$hints['fullname'] = implode(' ', $hcard['n']);
}
if (array_key_exists('photo', $hcard)) {
$hints['avatar'] = $hcard['photo'];
}
if (array_key_exists('note', $hcard)) {
$hints['bio'] = $hcard['note'];
}
if (array_key_exists('adr', $hcard)) {
if (is_string($hcard['adr'])) {
$hints['location'] = $hcard['adr'];
} else if (is_array($hcard['adr'])) {
$hints['location'] = implode(' ', $hcard['adr']);
}
}
if (array_key_exists('url', $hcard)) {
if (is_string($hcard['url'])) {
$hints['homepage'] = $hcard['url'];
} else if (is_array($hcard['adr'])) {
// HACK get the last one; that's how our hcards look
$hints['homepage'] = $hcard['url'][count($hcard['url'])-1];
}
}
return $hints;
}
}