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';
73 const IMAGE = 'http://activitystrea.ms/schema/1.0/image';
75 // Atom elements we snarf
77 const TITLE = 'title';
78 const SUMMARY = 'summary';
80 const SOURCE = 'source';
84 const EMAIL = 'email';
86 const POSTEROUS = 'http://posterous.com/help/rss/1.0';
87 const AUTHOR = 'author';
88 const USERIMAGE = 'userImage';
89 const PROFILEURL = 'profileUrl';
90 const NICKNAME = 'nickName';
91 const DISPLAYNAME = 'displayName';
102 public $avatarLinks = array();
107 // @todo move this stuff to it's own PHOTO activity object
108 const MEDIA_DESCRIPTION = 'description';
113 public $extra = array();
120 * This probably needs to be refactored
121 * to generate a local class (ActivityPerson, ActivityFile, ...)
122 * based on the object type.
124 * @param DOMElement $element DOM thing to turn into an Activity thing
126 function __construct($element = null)
128 if (empty($element)) {
132 $this->element = $element;
134 $this->geopoint = $this->_childContent(
136 ActivityContext::POINT,
137 ActivityContext::GEORSS
140 if ($element->tagName == 'author') {
141 $this->_fromAuthor($element);
142 } else if ($element->tagName == 'item') {
143 $this->_fromRssItem($element);
145 $this->_fromAtomEntry($element);
148 // Some per-type attributes...
149 if ($this->type == self::PERSON || $this->type == self::GROUP) {
150 $this->displayName = $this->title;
152 $photos = ActivityUtils::getLinks($element, 'photo');
153 if (count($photos)) {
154 foreach ($photos as $link) {
155 $this->avatarLinks[] = new AvatarLink($link);
158 $avatars = ActivityUtils::getLinks($element, 'avatar');
159 foreach ($avatars as $link) {
160 $this->avatarLinks[] = new AvatarLink($link);
164 $this->poco = new PoCo($element);
167 if ($this->type == self::PHOTO) {
169 $this->thumbnail = ActivityUtils::getLink($element, 'preview');
170 $this->largerImage = ActivityUtils::getLink($element, 'enclosure');
172 $this->description = ActivityUtils::childContent(
174 ActivityObject::MEDIA_DESCRIPTION,
178 if ($this->type == self::_LIST) {
179 $owner = ActivityUtils::child($this->element, Activity::AUTHOR, Activity::SPEC);
180 $this->owner = new ActivityObject($owner);
184 private function _fromAuthor($element)
186 $this->type = $this->_childContent($element,
187 Activity::OBJECTTYPE,
190 if (empty($this->type)) {
191 $this->type = self::PERSON; // XXX: is this fair?
194 // start with <atom:title>
196 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
198 if (!empty($title)) {
199 $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
202 // fall back to <atom:name>
204 if (empty($this->title)) {
205 $this->title = $this->_childContent($element, self::NAME);
208 // start with <atom:id>
210 $this->id = $this->_childContent($element, self::ID);
212 // fall back to <atom:uri>
214 if (empty($this->id)) {
215 $this->id = $this->_childContent($element, self::URI);
218 // fall further back to <atom:email>
220 if (empty($this->id)) {
221 $email = $this->_childContent($element, self::EMAIL);
222 if (!empty($email)) {
224 $this->id = 'mailto:'.$email;
228 $this->link = ActivityUtils::getPermalink($element);
230 // fall finally back to <link rel=alternate>
232 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
233 $this->id = $this->link;
237 private function _fromAtomEntry($element)
239 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
242 if (empty($this->type)) {
243 $this->type = ActivityObject::NOTE;
246 $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
247 $this->content = ActivityUtils::getContent($element);
249 // We don't like HTML in our titles, although it's technically allowed
251 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
253 $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
255 $this->source = $this->_getSource($element);
257 $this->link = ActivityUtils::getPermalink($element);
259 $this->id = $this->_childContent($element, self::ID);
261 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
262 $this->id = $this->link;
266 // @todo FIXME: rationalize with Activity::_fromRssItem()
267 private function _fromRssItem($item)
269 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
271 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
273 if (!empty($contentEl)) {
274 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
276 $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
277 if (!empty($descriptionEl)) {
278 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
282 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
284 $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
286 if (!empty($guidEl)) {
287 $this->id = $guidEl->textContent;
289 if ($guidEl->hasAttribute('isPermaLink')) {
291 $this->link = $this->id;
296 public static function fromRssAuthor($el)
298 $text = $el->textContent;
300 if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
303 } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
306 } else if (preg_match('/.*@.*/', $text)) {
314 // Not really enough info
316 $obj = new ActivityObject();
320 $obj->type = ActivityObject::PERSON;
323 if (!empty($email)) {
324 $obj->id = 'mailto:'.$email;
330 public static function fromDcCreator($el)
332 // Not really enough info
334 $text = $el->textContent;
336 $obj = new ActivityObject();
341 $obj->type = ActivityObject::PERSON;
346 public static function fromRssChannel($el)
348 $obj = new ActivityObject();
352 $obj->type = ActivityObject::PERSON; // @fixme guess better
354 $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
355 $obj->link = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
356 $obj->id = ActivityUtils::getLink($el, Activity::SELF);
358 if (empty($obj->id)) {
359 $obj->id = $obj->link;
362 $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
365 $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
368 $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
370 if (!empty($imageEl)) {
371 $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
372 $al = new AvatarLink();
374 $obj->avatarLinks[] = $al;
380 public static function fromPosterousAuthor($el)
382 $obj = new ActivityObject();
384 $obj->type = ActivityObject::PERSON; // @fixme any others...?
386 $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
388 if (!empty($userImage)) {
389 $al = new AvatarLink();
390 $al->url = $userImage;
391 $obj->avatarLinks[] = $al;
394 $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
395 $obj->id = $obj->link;
397 $obj->poco = new PoCo();
399 $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
400 $obj->poco->displayName = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
402 $obj->title = $obj->poco->displayName;
407 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
409 return ActivityUtils::childContent($element, $tag, $namespace);
412 // Try to get a unique id for the source feed
414 private function _getSource($element)
416 $sourceEl = ActivityUtils::child($element, 'source');
418 if (empty($sourceEl)) {
421 $href = ActivityUtils::getLink($sourceEl, 'self');
425 return ActivityUtils::childContent($sourceEl, 'id');
430 static function fromNotice(Notice $notice)
432 $object = new ActivityObject();
434 if (Event::handle('StartActivityObjectFromNotice', array($notice, &$object))) {
436 $object->type = (empty($notice->object_type)) ? ActivityObject::NOTE : $notice->object_type;
438 $object->id = $notice->uri;
439 $object->content = $notice->rendered;
440 $object->link = $notice->bestUrl();
442 Event::handle('EndActivityObjectFromNotice', array($notice, &$object));
448 static function fromProfile(Profile $profile)
450 $object = new ActivityObject();
452 if (Event::handle('StartActivityObjectFromProfile', array($profile, &$object))) {
453 $object->type = ActivityObject::PERSON;
454 $object->id = $profile->getUri();
455 $object->title = $profile->getBestName();
456 $object->link = $profile->profileurl;
458 $orig = $profile->getOriginalAvatar();
461 $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
470 foreach ($sizes as $size) {
472 $avatar = $profile->getAvatar($size);
474 if (!empty($avatar)) {
475 $alink = AvatarLink::fromAvatar($avatar);
477 $alink = new AvatarLink();
478 $alink->type = 'image/png';
479 $alink->height = $size;
480 $alink->width = $size;
481 $alink->url = Avatar::defaultImage($size);
483 if ($size == AVATAR_PROFILE_SIZE) {
484 // Hack for Twitter import: we don't have a 96x96 image,
485 // but we do have a 73x73 image. For now, fake it with that.
486 $avatar = $profile->getAvatar(73);
488 $alink = AvatarLink::fromAvatar($avatar);
489 $alink->height= $size;
490 $alink->width = $size;
495 $object->avatarLinks[] = $alink;
498 if (isset($profile->lat) && isset($profile->lon)) {
499 $object->geopoint = (float)$profile->lat
500 . ' ' . (float)$profile->lon;
503 $object->poco = PoCo::fromProfile($profile);
505 Event::handle('EndActivityObjectFromProfile', array($profile, &$object));
511 static function fromGroup($group)
513 $object = new ActivityObject();
515 if (Event::handle('StartActivityObjectFromGroup', array($group, &$object))) {
517 $object->type = ActivityObject::GROUP;
518 $object->id = $group->getUri();
519 $object->title = $group->getBestName();
520 $object->link = $group->getUri();
522 $object->avatarLinks[] = AvatarLink::fromFilename($group->homepage_logo,
523 AVATAR_PROFILE_SIZE);
525 $object->avatarLinks[] = AvatarLink::fromFilename($group->stream_logo,
528 $object->avatarLinks[] = AvatarLink::fromFilename($group->mini_logo,
531 $object->poco = PoCo::fromGroup($group);
532 Event::handle('EndActivityObjectFromGroup', array($group, &$object));
538 static function fromPeopletag($ptag)
540 $object = new ActivityObject();
541 if (Event::handle('StartActivityObjectFromPeopletag', array($ptag, &$object))) {
542 $object->type = ActivityObject::_LIST;
544 $object->id = $ptag->getUri();
545 $object->title = $ptag->tag;
546 $object->summary = $ptag->description;
547 $object->link = $ptag->homeUrl();
548 $object->owner = Profile::staticGet('id', $ptag->tagger);
549 $object->poco = PoCo::fromProfile($object->owner);
550 Event::handle('EndActivityObjectFromPeopletag', array($ptag, &$object));
555 static function fromFile(File $file)
557 $object = new ActivityObject();
559 if (Event::handle('StartActivityObjectFromFile', array($file, &$object))) {
561 $object->type = self::mimeTypeToObjectType($file->mimetype);
562 $object->id = TagURI::mint(sprintf("file:%d", $file->id));
563 $object->link = common_local_url('attachment', array('attachment' => $file->id));
566 $object->title = $file->title;
570 $object->date = $file->date;
573 $thumbnail = $file->getThumbnail();
575 if (!empty($thumbnail)) {
576 $object->thumbnail = $thumbnail;
579 switch ($object->type) {
580 case ActivityObject::IMAGE:
581 $object->largerImage = $file->url;
583 case ActivityObject::VIDEO:
584 case ActivityObject::AUDIO:
585 $object->stream = $file->url;
589 Event::handle('EndActivityObjectFromFile', array($file, &$object));
595 function outputTo($xo, $tag='activity:object')
598 $xo->elementStart($tag);
601 if (Event::handle('StartActivityObjectOutputAtom', array($this, $xo))) {
602 $xo->element('activity:object-type', null, $this->type);
606 if ($tag == 'author') {
607 $xo->element(self::URI, null, $this->id);
609 $xo->element(self::ID, null, $this->id);
612 if (!empty($this->title)) {
613 $name = common_xml_safe_str($this->title);
614 if ($tag == 'author') {
615 // XXX: Backward compatibility hack -- atom:name should contain
616 // full name here, instead of nickname, i.e.: $name. Change
617 // this in the next version.
618 $xo->element(self::NAME, null, $this->poco->preferredUsername);
620 $xo->element(self::TITLE, null, $name);
624 if (!empty($this->summary)) {
628 common_xml_safe_str($this->summary)
632 if (!empty($this->content)) {
633 // XXX: assuming HTML content here
635 ActivityUtils::CONTENT,
636 array('type' => 'html'),
637 common_xml_safe_str($this->content)
641 if (!empty($this->link)) {
645 'rel' => 'alternate',
646 'type' => 'text/html',
647 'href' => $this->link
653 if(!empty($this->owner)) {
654 $owner = $this->owner->asActivityNoun(self::AUTHOR);
658 if ($this->type == ActivityObject::PERSON
659 || $this->type == ActivityObject::GROUP) {
661 foreach ($this->avatarLinks as $avatar) {
665 'type' => $avatar->type,
666 'media:width' => $avatar->width,
667 'media:height' => $avatar->height,
668 'href' => $avatar->url
675 if (!empty($this->geopoint)) {
683 if (!empty($this->poco)) {
684 $this->poco->outputTo($xo);
687 // @fixme there's no way here to make a tree; elements can only contain plaintext
688 // @fixme these may collide with JSON extensions
689 foreach ($this->extra as $el) {
690 list($extraTag, $attrs, $content) = $el;
691 $xo->element($extraTag, $attrs, $content);
694 Event::handle('EndActivityObjectOutputAtom', array($this, $xo));
698 $xo->elementEnd($tag);
704 function asString($tag='activity:object')
706 $xs = new XMLStringer(true);
708 $this->outputTo($xs, $tag);
710 return $xs->getString();
714 * Returns an array based on this Activity Object suitable for
717 * @return array $object the activity object array
724 if (Event::handle('StartActivityObjectOutputJson', array($this, &$object))) {
725 // XXX: attachments are added by Activity
727 // author (Add object for author? Could be useful for repeats.)
729 // content (Add rendered version of the notice?)
734 $object['displayName'] = $this->title;
737 // downstreamDuplicates
740 $object['id'] = $this->id;
742 if ($this->type == ActivityObject::PERSON
743 || $this->type == ActivityObject::GROUP) {
745 // XXX: Not sure what the best avatar is to use for the
746 // author's "image". For now, I'm using the large size.
749 $avatarMediaLinks = array();
751 foreach ($this->avatarLinks as $a) {
753 // Make a MediaLink for every other Avatar
754 $avatar = new ActivityStreamsMediaLink(
762 // Find the big avatar to use as the "image"
763 if ($a->height == AVATAR_PROFILE_SIZE) {
767 $avatarMediaLinks[] = $avatar->asArray();
770 if (!array_key_exists('status_net', $object)) {
771 $object['status_net'] = array();
774 $object['status_net']['avatarLinks'] = $avatarMediaLinks; // extension
777 if (!empty($imgLink)) {
778 $object['image'] = $imgLink->asArray();
784 // We can probably use the whole schema URL here but probably the
785 // relative simple name is easier to parse
787 $object['objectType'] = ActivityObject::canonicalType($this->type);
790 $object['summary'] = $this->summary;
793 $object['content'] = $this->content;
795 // published (probably don't need. Might be useful for repeats.)
797 // updated (probably don't need this)
799 // TODO: upstreamDuplicates
801 // url (XXX: need to put the right thing here...)
802 $object['url'] = $this->id;
805 // @fixme these may collide with XML extensions
806 // @fixme multiple tags of same name will overwrite each other
807 // @fixme text content from XML extensions will be lost
809 foreach ($this->extra as $e) {
810 list($objectName, $props, $txt) = $e;
811 if (!empty($objectName)) {
812 $parts = explode(":", $objectName);
813 if (count($parts) == 2 && $parts[0] == "statusnet") {
814 if (!array_key_exists('status_net', $object)) {
815 $object['status_net'] = array();
817 $object['status_net'][$parts[1]] = $props;
819 $object[$objectName] = $props;
824 if (!empty($this->geopoint)) {
826 list($lat, $lon) = explode(' ', $this->geopoint);
828 $object['location'] = array(
829 'objectType' => 'place',
830 'position' => sprintf("%+02.5F%+03.5F/", $lat, $lon),
835 $loc = Location::fromLatLon($lat, $lon);
838 $name = $loc->getName();
841 $object['location']['displayName'] = $name;
843 $url = $loc->getURL();
846 $object['location']['url'] = $url;
851 if (!empty($this->poco)) {
852 $object['portablecontacts_net'] = array_filter($this->poco->asArray());
855 if (!empty($this->thumbnail)) {
856 if (is_string($this->thumbnail)) {
857 $object['image'] = array('url' => $this->thumbnail);
859 $object['image'] = array('url' => $this->thumbnail->url);
860 if ($this->thumbnail->width) {
861 $object['image']['width'] = $this->thumbnail->width;
863 if ($this->thumbnail->height) {
864 $object['image']['height'] = $this->thumbnail->height;
869 switch ($object->type) {
871 if (!empty($this->largerImage)) {
872 $object['fullImage'] = array('url' => $this->largerImage);
877 if (!empty($this->stream)) {
878 $object['stream'] = array('url' => $this->stream);
883 Event::handle('EndActivityObjectOutputJson', array($this, &$object));
885 return array_filter($object);
888 static function canonicalType($type) {
889 $ns = 'http://activitystrea.ms/schema/1.0/';
890 if (substr($type, 0, mb_strlen($ns)) == $ns) {
891 return substr($type, mb_strlen($ns));
897 static function mimeTypeToObjectType($mimeType) {
902 if (empty($mimeType)) {
906 $parts = explode('/', $mimeType);