Add PoCo parsing and some other fixes.

This commit is contained in:
Zach Copley 2010-02-23 22:35:48 -08:00
parent 868ae8e62b
commit 3a3af6782a

View File

@ -34,6 +34,7 @@ if (!defined('STATUSNET')) {
class PoCoURL class PoCoURL
{ {
const URLS = 'urls';
const TYPE = 'type'; const TYPE = 'type';
const VALUE = 'value'; const VALUE = 'value';
const PRIMARY = 'primary'; const PRIMARY = 'primary';
@ -55,7 +56,7 @@ class PoCoURL
$xs->elementStart('poco:urls'); $xs->elementStart('poco:urls');
$xs->element('poco:type', null, $this->type); $xs->element('poco:type', null, $this->type);
$xs->element('poco:value', null, $this->value); $xs->element('poco:value', null, $this->value);
if ($this->primary) { if (!empty($this->primary)) {
$xs->element('poco:primary', null, 'true'); $xs->element('poco:primary', null, 'true');
} }
$xs->elementEnd('poco:urls'); $xs->elementEnd('poco:urls');
@ -70,21 +71,19 @@ class PoCoAddress
public $formatted; public $formatted;
function __construct($formatted) // @todo Other address fields
{
if (empty($formatted)) {
return null;
}
$this->formatted = $formatted;
}
function asString() function asString()
{ {
$xs = new XMLStringer(true); if (!empty($this->formatted)) {
$xs->elementStart('poco:address'); $xs = new XMLStringer(true);
$xs->element('poco:formatted', null, $this->formatted); $xs->elementStart('poco:address');
$xs->elementEnd('poco:address'); $xs->element('poco:formatted', null, $this->formatted);
return $xs->getString(); $xs->elementEnd('poco:address');
return $xs->getString();
}
return null;
} }
} }
@ -92,26 +91,117 @@ class PoCo
{ {
const NS = 'http://portablecontacts.net/spec/1.0'; const NS = 'http://portablecontacts.net/spec/1.0';
const USERNAME = 'preferredUsername'; const USERNAME = 'preferredUsername';
const NOTE = 'note'; const DISPLAYNAME = 'displayName';
const URLS = 'urls'; const NOTE = 'note';
public $preferredUsername; public $preferredUsername;
public $displayName;
public $note; public $note;
public $address; public $address;
public $urls = array(); public $urls = array();
function __construct($profile) function __construct($element = null)
{ {
$this->preferredUsername = $profile->nickname; if (empty($element)) {
$this->displayName = $profile->getBestName(); return;
}
$this->note = $profile->bio; $this->preferredUsername = ActivityUtils::childContent(
$this->address = new PoCoAddress($profile->location); $element,
self::USERNAME,
self::NS
);
$this->displayName = ActivityUtils::childContent(
$element,
self::DISPLAYNAME,
self::NS
);
$this->note = ActivityUtils::childContent(
$element,
self::NOTE,
self::NS
);
$this->address = $this->_getAddress($element);
$this->urls = $this->_getURLs($element);
}
private function _getURLs($element)
{
$urlEls = $element->getElementsByTagnameNS(self::NS, PoCoURL::URLS);
$urls = array();
foreach ($urlEls as $urlEl) {
$type = ActivityUtils::childContent(
$urlEl,
PoCoURL::TYPE,
PoCo::NS
);
$value = ActivityUtils::childContent(
$urlEl,
PoCoURL::VALUE,
PoCo::NS
);
$primary = ActivityUtils::childContent(
$urlEl,
PoCoURL::PRIMARY,
PoCo::NS
);
array_push($urls, new PoCoURL($type, $value, $primary));
}
return $urls;
}
private function _getAddress($element)
{
$addressEl = ActivityUtils::child(
$element,
PoCoAddress::ADDRESS,
PoCo::NS
);
$formatted = ActivityUtils::childContent(
$addressEl,
PoCoAddress::FORMATTED,
self::NS
);
if (!empty($formatted)) {
$address = new PoCoAddress();
$address->formatted = $formatted;
return $address;
}
return null;
}
function fromProfile($profile)
{
if (empty($profile)) {
return null;
}
$poco = new PoCo();
$poco->preferredUsername = $profile->nickname;
$poco->displayName = $profile->getBestName();
$poco->note = $profile->bio;
$paddy = new PoCoAddress();
$paddy->formatted = $profile->location;
$poco->address = $paddy;
if (!empty($profile->homepage)) { if (!empty($profile->homepage)) {
array_push( array_push(
$this->urls, $poco->urls,
new PoCoURL( new PoCoURL(
'homepage', 'homepage',
$profile->homepage, $profile->homepage,
@ -119,6 +209,8 @@ class PoCo
) )
); );
} }
return $poco;
} }
function asString() function asString()
@ -381,6 +473,8 @@ class ActivityObject
public $source; public $source;
public $avatar; public $avatar;
public $geopoint; public $geopoint;
public $poco;
public $displayName;
/** /**
* Constructor * Constructor
@ -433,7 +527,6 @@ class ActivityObject
$this->link = ActivityUtils::getPermalink($element); $this->link = ActivityUtils::getPermalink($element);
// XXX: grab PoCo stuff
} }
// Some per-type attributes... // Some per-type attributes...
@ -441,8 +534,9 @@ class ActivityObject
$this->displayName = $this->title; $this->displayName = $this->title;
// @fixme we may have multiple avatars with different resolutions specified // @fixme we may have multiple avatars with different resolutions specified
$this->avatar = ActivityUtils::getLink($element, 'avatar'); $this->avatar = ActivityUtils::getLink($element, 'avatar');
$this->nickname = ActivityUtils::childContent($element, PoCo::USERNAME, PoCo::NS);
$this->poco = new PoCo($element);
} }
} }
@ -497,7 +591,7 @@ class ActivityObject
$object->geopoint = (float)$profile->lat . ' ' . (float)$profile->lon; $object->geopoint = (float)$profile->lat . ' ' . (float)$profile->lon;
} }
$object->poco = new PoCo($profile); $object->poco = PoCo::fromProfile($profile);
return $object; return $object;
} }
@ -526,11 +620,19 @@ class ActivityObject
} }
if (!empty($this->link)) { if (!empty($this->link)) {
$xs->element('link', array('rel' => 'alternate', 'type' => 'text/html'), $xs->element(
$this->link); 'link',
array(
'rel' => 'alternate',
'type' => 'text/html',
'href' => $this->link
),
null
);
} }
if ($this->type == ActivityObject::PERSON) { if ($this->type == ActivityObject::PERSON
|| $this->type == ActivityObject::GROUP) {
$xs->element( $xs->element(
'link', array( 'link', array(
'type' => empty($this->avatar) ? 'image/png' : $this->avatar->mediatype, 'type' => empty($this->avatar) ? 'image/png' : $this->avatar->mediatype,
@ -539,7 +641,7 @@ class ActivityObject
? Avatar::defaultImage(AVATAR_PROFILE_SIZE) ? Avatar::defaultImage(AVATAR_PROFILE_SIZE)
: $this->avatar->displayUrl() : $this->avatar->displayUrl()
), ),
'' null
); );
} }