3 * StatusNet, the distributed open-source microblogging tool
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @author Zach Copley <zach@status.net>
26 * @copyright 2010 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
36 * A noun-ish thing in the activity universe
38 * The activity streams spec talks about activity objects, while also having
39 * a tag activity:object, which is in fact an activity object. Aaaaaah!
41 * This is just a thing in the activity universe. Can be the subject, object,
42 * or indirect object (target!) of an activity verb. Rotten name, and I'm
43 * propagating it. *sigh*
47 * @author Evan Prodromou <evan@status.net>
48 * @copyright 2010 StatusNet, Inc.
49 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
50 * @link http://status.net/
54 const ARTICLE = 'http://activitystrea.ms/schema/1.0/article';
55 const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
56 const NOTE = 'http://activitystrea.ms/schema/1.0/note';
57 const STATUS = 'http://activitystrea.ms/schema/1.0/status';
58 const FILE = 'http://activitystrea.ms/schema/1.0/file';
59 const PHOTO = 'http://activitystrea.ms/schema/1.0/photo';
60 const ALBUM = 'http://activitystrea.ms/schema/1.0/photo-album';
61 const PLAYLIST = 'http://activitystrea.ms/schema/1.0/playlist';
62 const VIDEO = 'http://activitystrea.ms/schema/1.0/video';
63 const AUDIO = 'http://activitystrea.ms/schema/1.0/audio';
64 const BOOKMARK = 'http://activitystrea.ms/schema/1.0/bookmark';
65 const PERSON = 'http://activitystrea.ms/schema/1.0/person';
66 const GROUP = 'http://activitystrea.ms/schema/1.0/group';
67 const _LIST = 'http://activitystrea.ms/schema/1.0/list'; // LIST is reserved
68 const PLACE = 'http://activitystrea.ms/schema/1.0/place';
69 const COMMENT = 'http://activitystrea.ms/schema/1.0/comment';
71 const ACTIVITY = 'http://activitystrea.ms/schema/1.0/activity';
72 const SERVICE = 'http://activitystrea.ms/schema/1.0/service';
74 // Atom elements we snarf
76 const TITLE = 'title';
77 const SUMMARY = 'summary';
79 const SOURCE = 'source';
83 const EMAIL = 'email';
85 const POSTEROUS = 'http://posterous.com/help/rss/1.0';
86 const AUTHOR = 'author';
87 const USERIMAGE = 'userImage';
88 const PROFILEURL = 'profileUrl';
89 const NICKNAME = 'nickName';
90 const DISPLAYNAME = 'displayName';
101 public $avatarLinks = array();
106 // @todo move this stuff to it's own PHOTO activity object
107 const MEDIA_DESCRIPTION = 'description';
112 public $extra = array();
117 * This probably needs to be refactored
118 * to generate a local class (ActivityPerson, ActivityFile, ...)
119 * based on the object type.
121 * @param DOMElement $element DOM thing to turn into an Activity thing
123 function __construct($element = null)
125 if (empty($element)) {
129 $this->element = $element;
131 $this->geopoint = $this->_childContent(
133 ActivityContext::POINT,
134 ActivityContext::GEORSS
137 if ($element->tagName == 'author') {
138 $this->_fromAuthor($element);
139 } else if ($element->tagName == 'item') {
140 $this->_fromRssItem($element);
142 $this->_fromAtomEntry($element);
145 // Some per-type attributes...
146 if ($this->type == self::PERSON || $this->type == self::GROUP) {
147 $this->displayName = $this->title;
149 $photos = ActivityUtils::getLinks($element, 'photo');
150 if (count($photos)) {
151 foreach ($photos as $link) {
152 $this->avatarLinks[] = new AvatarLink($link);
155 $avatars = ActivityUtils::getLinks($element, 'avatar');
156 foreach ($avatars as $link) {
157 $this->avatarLinks[] = new AvatarLink($link);
161 $this->poco = new PoCo($element);
164 if ($this->type == self::PHOTO) {
166 $this->thumbnail = ActivityUtils::getLink($element, 'preview');
167 $this->largerImage = ActivityUtils::getLink($element, 'enclosure');
169 $this->description = ActivityUtils::childContent(
171 ActivityObject::MEDIA_DESCRIPTION,
175 if ($this->type == self::_LIST) {
176 $owner = ActivityUtils::child($this->element, Activity::AUTHOR, Activity::SPEC);
177 $this->owner = new ActivityObject($owner);
181 private function _fromAuthor($element)
183 $this->type = $this->_childContent($element,
184 Activity::OBJECTTYPE,
187 if (empty($this->type)) {
188 $this->type = self::PERSON; // XXX: is this fair?
191 // start with <atom:title>
193 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
195 if (!empty($title)) {
196 $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
199 // fall back to <atom:name>
201 if (empty($this->title)) {
202 $this->title = $this->_childContent($element, self::NAME);
205 // start with <atom:id>
207 $this->id = $this->_childContent($element, self::ID);
209 // fall back to <atom:uri>
211 if (empty($this->id)) {
212 $this->id = $this->_childContent($element, self::URI);
215 // fall further back to <atom:email>
217 if (empty($this->id)) {
218 $email = $this->_childContent($element, self::EMAIL);
219 if (!empty($email)) {
221 $this->id = 'mailto:'.$email;
225 $this->link = ActivityUtils::getPermalink($element);
227 // fall finally back to <link rel=alternate>
229 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
230 $this->id = $this->link;
234 private function _fromAtomEntry($element)
236 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
239 if (empty($this->type)) {
240 $this->type = ActivityObject::NOTE;
243 $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
244 $this->content = ActivityUtils::getContent($element);
246 // We don't like HTML in our titles, although it's technically allowed
248 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
250 $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
252 $this->source = $this->_getSource($element);
254 $this->link = ActivityUtils::getPermalink($element);
256 $this->id = $this->_childContent($element, self::ID);
258 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
259 $this->id = $this->link;
263 // @todo FIXME: rationalize with Activity::_fromRssItem()
264 private function _fromRssItem($item)
266 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
268 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
270 if (!empty($contentEl)) {
271 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
273 $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
274 if (!empty($descriptionEl)) {
275 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
279 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
281 $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
283 if (!empty($guidEl)) {
284 $this->id = $guidEl->textContent;
286 if ($guidEl->hasAttribute('isPermaLink')) {
288 $this->link = $this->id;
293 public static function fromRssAuthor($el)
295 $text = $el->textContent;
297 if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
300 } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
303 } else if (preg_match('/.*@.*/', $text)) {
311 // Not really enough info
313 $obj = new ActivityObject();
317 $obj->type = ActivityObject::PERSON;
320 if (!empty($email)) {
321 $obj->id = 'mailto:'.$email;
327 public static function fromDcCreator($el)
329 // Not really enough info
331 $text = $el->textContent;
333 $obj = new ActivityObject();
338 $obj->type = ActivityObject::PERSON;
343 public static function fromRssChannel($el)
345 $obj = new ActivityObject();
349 $obj->type = ActivityObject::PERSON; // @fixme guess better
351 $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
352 $obj->link = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
353 $obj->id = ActivityUtils::getLink($el, Activity::SELF);
355 if (empty($obj->id)) {
356 $obj->id = $obj->link;
359 $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
362 $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
365 $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
367 if (!empty($imageEl)) {
368 $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
369 $al = new AvatarLink();
371 $obj->avatarLinks[] = $al;
377 public static function fromPosterousAuthor($el)
379 $obj = new ActivityObject();
381 $obj->type = ActivityObject::PERSON; // @fixme any others...?
383 $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
385 if (!empty($userImage)) {
386 $al = new AvatarLink();
387 $al->url = $userImage;
388 $obj->avatarLinks[] = $al;
391 $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
392 $obj->id = $obj->link;
394 $obj->poco = new PoCo();
396 $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
397 $obj->poco->displayName = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
399 $obj->title = $obj->poco->displayName;
404 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
406 return ActivityUtils::childContent($element, $tag, $namespace);
409 // Try to get a unique id for the source feed
411 private function _getSource($element)
413 $sourceEl = ActivityUtils::child($element, 'source');
415 if (empty($sourceEl)) {
418 $href = ActivityUtils::getLink($sourceEl, 'self');
422 return ActivityUtils::childContent($sourceEl, 'id');
427 static function fromNotice(Notice $notice)
429 $object = new ActivityObject();
431 if (Event::handle('StartActivityObjectFromNotice', array($notice, &$object))) {
433 $object->type = (empty($notice->object_type)) ? ActivityObject::NOTE : $notice->object_type;
435 $object->id = $notice->uri;
436 $object->title = $notice->content;
437 $object->content = $notice->rendered;
438 $object->link = $notice->bestUrl();
440 Event::handle('EndActivityObjectFromNotice', array($notice, &$object));
446 static function fromProfile(Profile $profile)
448 $object = new ActivityObject();
450 if (Event::handle('StartActivityObjectFromProfile', array($profile, &$object))) {
451 $object->type = ActivityObject::PERSON;
452 $object->id = $profile->getUri();
453 $object->title = $profile->getBestName();
454 $object->link = $profile->profileurl;
456 $orig = $profile->getOriginalAvatar();
459 $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
468 foreach ($sizes as $size) {
470 $avatar = $profile->getAvatar($size);
472 if (!empty($avatar)) {
473 $alink = AvatarLink::fromAvatar($avatar);
475 $alink = new AvatarLink();
476 $alink->type = 'image/png';
477 $alink->height = $size;
478 $alink->width = $size;
479 $alink->url = Avatar::defaultImage($size);
481 if ($size == AVATAR_PROFILE_SIZE) {
482 // Hack for Twitter import: we don't have a 96x96 image,
483 // but we do have a 73x73 image. For now, fake it with that.
484 $avatar = $profile->getAvatar(73);
486 $alink = AvatarLink::fromAvatar($avatar);
487 $alink->height= $size;
488 $alink->width = $size;
493 $object->avatarLinks[] = $alink;
496 if (isset($profile->lat) && isset($profile->lon)) {
497 $object->geopoint = (float)$profile->lat
498 . ' ' . (float)$profile->lon;
501 $object->poco = PoCo::fromProfile($profile);
503 Event::handle('EndActivityObjectFromProfile', array($profile, &$object));
509 static function fromGroup($group)
511 $object = new ActivityObject();
513 if (Event::handle('StartActivityObjectFromGroup', array($group, &$object))) {
515 $object->type = ActivityObject::GROUP;
516 $object->id = $group->getUri();
517 $object->title = $group->getBestName();
518 $object->link = $group->getUri();
520 $object->avatarLinks[] = AvatarLink::fromFilename($group->homepage_logo,
521 AVATAR_PROFILE_SIZE);
523 $object->avatarLinks[] = AvatarLink::fromFilename($group->stream_logo,
526 $object->avatarLinks[] = AvatarLink::fromFilename($group->mini_logo,
529 $object->poco = PoCo::fromGroup($group);
530 Event::handle('EndActivityObjectFromGroup', array($group, &$object));
536 static function fromPeopletag($ptag)
538 $object = new ActivityObject();
539 if (Event::handle('StartActivityObjectFromPeopletag', array($ptag, &$object))) {
540 $object->type = ActivityObject::_LIST;
542 $object->id = $ptag->getUri();
543 $object->title = $ptag->tag;
544 $object->summary = $ptag->description;
545 $object->link = $ptag->homeUrl();
546 $object->owner = Profile::staticGet('id', $ptag->tagger);
547 $object->poco = PoCo::fromProfile($object->owner);
548 Event::handle('EndActivityObjectFromPeopletag', array($ptag, &$object));
553 function outputTo($xo, $tag='activity:object')
556 $xo->elementStart($tag);
559 if (Event::handle('StartActivityObjectOutputAtom', array($this, $xo))) {
560 $xo->element('activity:object-type', null, $this->type);
564 if ($tag == 'author') {
565 $xo->element(self::URI, null, $this->id);
567 $xo->element(self::ID, null, $this->id);
570 if (!empty($this->title)) {
571 $name = common_xml_safe_str($this->title);
572 if ($tag == 'author') {
573 // XXX: Backward compatibility hack -- atom:name should contain
574 // full name here, instead of nickname, i.e.: $name. Change
575 // this in the next version.
576 $xo->element(self::NAME, null, $this->poco->preferredUsername);
578 $xo->element(self::TITLE, null, $name);
582 if (!empty($this->summary)) {
586 common_xml_safe_str($this->summary)
590 if (!empty($this->content)) {
591 // XXX: assuming HTML content here
593 ActivityUtils::CONTENT,
594 array('type' => 'html'),
595 common_xml_safe_str($this->content)
599 if (!empty($this->link)) {
603 'rel' => 'alternate',
604 'type' => 'text/html',
605 'href' => $this->link
611 if(!empty($this->owner)) {
612 $owner = $this->owner->asActivityNoun(self::AUTHOR);
616 if ($this->type == ActivityObject::PERSON
617 || $this->type == ActivityObject::GROUP) {
619 foreach ($this->avatarLinks as $avatar) {
623 'type' => $avatar->type,
624 'media:width' => $avatar->width,
625 'media:height' => $avatar->height,
626 'href' => $avatar->url
633 if (!empty($this->geopoint)) {
641 if (!empty($this->poco)) {
642 $this->poco->outputTo($xo);
645 // @fixme there's no way here to make a tree; elements can only contain plaintext
646 // @fixme these may collide with JSON extensions
647 foreach ($this->extra as $el) {
648 list($extraTag, $attrs, $content) = $el;
649 $xo->element($extraTag, $attrs, $content);
652 Event::handle('EndActivityObjectOutputAtom', array($this, $xo));
656 $xo->elementEnd($tag);
662 function asString($tag='activity:object')
664 $xs = new XMLStringer(true);
666 $this->outputTo($xs, $tag);
668 return $xs->getString();
672 * Returns an array based on this Activity Object suitable for
675 * @return array $object the activity object array
682 if (Event::handle('StartActivityObjectOutputJson', array($this, &$object))) {
683 // XXX: attachments are added by Activity
685 // author (Add object for author? Could be useful for repeats.)
687 // content (Add rendered version of the notice?)
690 $object['displayName'] = $this->title;
692 // downstreamDuplicates
695 $object['id'] = $this->id;
697 if ($this->type == ActivityObject::PERSON
698 || $this->type == ActivityObject::GROUP) {
700 // XXX: Not sure what the best avatar is to use for the
701 // author's "image". For now, I'm using the large size.
704 $avatarMediaLinks = array();
706 foreach ($this->avatarLinks as $a) {
708 // Make a MediaLink for every other Avatar
709 $avatar = new ActivityStreamsMediaLink(
717 // Find the big avatar to use as the "image"
718 if ($a->height == AVATAR_PROFILE_SIZE) {
722 $avatarMediaLinks[] = $avatar->asArray();
725 if (!array_key_exists('status_net', $object)) {
726 $object['status_net'] = array();
729 $object['status_net']['avatarLinks'] = $avatarMediaLinks; // extension
732 if (!empty($imgLink)) {
733 $object['image'] = $imgLink->asArray();
739 // We can probably use the whole schema URL here but probably the
740 // relative simple name is easier to parse
742 $object['objectType'] = ActivityObject::canonicalType($this->type);
745 $object['summary'] = $this->summary;
748 $object['content'] = $this->content;
750 // published (probably don't need. Might be useful for repeats.)
752 // updated (probably don't need this)
754 // TODO: upstreamDuplicates
756 // url (XXX: need to put the right thing here...)
757 $object['url'] = $this->id;
760 // @fixme these may collide with XML extensions
761 // @fixme multiple tags of same name will overwrite each other
762 // @fixme text content from XML extensions will be lost
764 foreach ($this->extra as $e) {
765 list($objectName, $props, $txt) = $e;
766 if (!empty($objectName)) {
767 $parts = explode(":", $objectName);
768 if (count($parts) == 2 && $parts[0] == "statusnet") {
769 if (!array_key_exists('status_net', $object)) {
770 $object['status_net'] = array();
772 $object['status_net'][$parts[1]] = $props;
774 $object[$objectName] = $props;
779 if (!empty($this->geopoint)) {
781 list($lat, $lon) = explode(' ', $this->geopoint);
783 $object['location'] = array(
784 'objectType' => 'place',
785 'position' => sprintf("%+02.5F%+03.5F/", $lat, $lon),
790 $loc = Location::fromLatLon($lat, $lon);
793 $name = $loc->getName();
796 $object['location']['displayName'] = $name;
798 $url = $loc->getURL();
801 $object['location']['url'] = $url;
806 if (!empty($this->poco)) {
807 $object['portablecontacts_net'] = array_filter($this->poco->asArray());
810 Event::handle('EndActivityObjectOutputJson', array($this, &$object));
812 return array_filter($object);
815 static function canonicalType($type) {
816 $ns = 'http://activitystrea.ms/schema/1.0/';
817 if (substr($type, 0, mb_strlen($ns)) == $ns) {
818 return substr($type, mb_strlen($ns));