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';
72 // Atom elements we snarf
74 const TITLE = 'title';
75 const SUMMARY = 'summary';
77 const SOURCE = 'source';
81 const EMAIL = 'email';
83 const POSTEROUS = 'http://posterous.com/help/rss/1.0';
84 const AUTHOR = 'author';
85 const USERIMAGE = 'userImage';
86 const PROFILEURL = 'profileUrl';
87 const NICKNAME = 'nickName';
88 const DISPLAYNAME = 'displayName';
99 public $avatarLinks = array();
104 // @todo move this stuff to it's own PHOTO activity object
105 const MEDIA_DESCRIPTION = 'description';
110 public $extra = array();
115 * This probably needs to be refactored
116 * to generate a local class (ActivityPerson, ActivityFile, ...)
117 * based on the object type.
119 * @param DOMElement $element DOM thing to turn into an Activity thing
121 function __construct($element = null)
123 if (empty($element)) {
127 $this->element = $element;
129 $this->geopoint = $this->_childContent(
131 ActivityContext::POINT,
132 ActivityContext::GEORSS
135 if ($element->tagName == 'author') {
136 $this->_fromAuthor($element);
137 } else if ($element->tagName == 'item') {
138 $this->_fromRssItem($element);
140 $this->_fromAtomEntry($element);
143 // Some per-type attributes...
144 if ($this->type == self::PERSON || $this->type == self::GROUP) {
145 $this->displayName = $this->title;
147 $photos = ActivityUtils::getLinks($element, 'photo');
148 if (count($photos)) {
149 foreach ($photos as $link) {
150 $this->avatarLinks[] = new AvatarLink($link);
153 $avatars = ActivityUtils::getLinks($element, 'avatar');
154 foreach ($avatars as $link) {
155 $this->avatarLinks[] = new AvatarLink($link);
159 $this->poco = new PoCo($element);
162 if ($this->type == self::PHOTO) {
164 $this->thumbnail = ActivityUtils::getLink($element, 'preview');
165 $this->largerImage = ActivityUtils::getLink($element, 'enclosure');
167 $this->description = ActivityUtils::childContent(
169 ActivityObject::MEDIA_DESCRIPTION,
173 if ($this->type == self::_LIST) {
174 $owner = ActivityUtils::child($this->element, Activity::AUTHOR, Activity::SPEC);
175 $this->owner = new ActivityObject($owner);
179 private function _fromAuthor($element)
181 $this->type = $this->_childContent($element,
182 Activity::OBJECTTYPE,
185 if (empty($this->type)) {
186 $this->type = self::PERSON; // XXX: is this fair?
189 // start with <atom:title>
191 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
193 if (!empty($title)) {
194 $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
197 // fall back to <atom:name>
199 if (empty($this->title)) {
200 $this->title = $this->_childContent($element, self::NAME);
203 // start with <atom:id>
205 $this->id = $this->_childContent($element, self::ID);
207 // fall back to <atom:uri>
209 if (empty($this->id)) {
210 $this->id = $this->_childContent($element, self::URI);
213 // fall further back to <atom:email>
215 if (empty($this->id)) {
216 $email = $this->_childContent($element, self::EMAIL);
217 if (!empty($email)) {
219 $this->id = 'mailto:'.$email;
223 $this->link = ActivityUtils::getPermalink($element);
225 // fall finally back to <link rel=alternate>
227 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
228 $this->id = $this->link;
232 private function _fromAtomEntry($element)
234 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
237 if (empty($this->type)) {
238 $this->type = ActivityObject::NOTE;
241 $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
242 $this->content = ActivityUtils::getContent($element);
244 // We don't like HTML in our titles, although it's technically allowed
246 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
248 $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
250 $this->source = $this->_getSource($element);
252 $this->link = ActivityUtils::getPermalink($element);
254 $this->id = $this->_childContent($element, self::ID);
256 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
257 $this->id = $this->link;
261 // @todo FIXME: rationalize with Activity::_fromRssItem()
262 private function _fromRssItem($item)
264 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
266 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
268 if (!empty($contentEl)) {
269 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
271 $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
272 if (!empty($descriptionEl)) {
273 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
277 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
279 $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
281 if (!empty($guidEl)) {
282 $this->id = $guidEl->textContent;
284 if ($guidEl->hasAttribute('isPermaLink')) {
286 $this->link = $this->id;
291 public static function fromRssAuthor($el)
293 $text = $el->textContent;
295 if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
298 } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
301 } else if (preg_match('/.*@.*/', $text)) {
309 // Not really enough info
311 $obj = new ActivityObject();
315 $obj->type = ActivityObject::PERSON;
318 if (!empty($email)) {
319 $obj->id = 'mailto:'.$email;
325 public static function fromDcCreator($el)
327 // Not really enough info
329 $text = $el->textContent;
331 $obj = new ActivityObject();
336 $obj->type = ActivityObject::PERSON;
341 public static function fromRssChannel($el)
343 $obj = new ActivityObject();
347 $obj->type = ActivityObject::PERSON; // @fixme guess better
349 $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
350 $obj->link = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
351 $obj->id = ActivityUtils::getLink($el, Activity::SELF);
353 if (empty($obj->id)) {
354 $obj->id = $obj->link;
357 $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
360 $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
363 $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
365 if (!empty($imageEl)) {
366 $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
367 $al = new AvatarLink();
369 $obj->avatarLinks[] = $al;
375 public static function fromPosterousAuthor($el)
377 $obj = new ActivityObject();
379 $obj->type = ActivityObject::PERSON; // @fixme any others...?
381 $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
383 if (!empty($userImage)) {
384 $al = new AvatarLink();
385 $al->url = $userImage;
386 $obj->avatarLinks[] = $al;
389 $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
390 $obj->id = $obj->link;
392 $obj->poco = new PoCo();
394 $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
395 $obj->poco->displayName = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
397 $obj->title = $obj->poco->displayName;
402 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
404 return ActivityUtils::childContent($element, $tag, $namespace);
407 // Try to get a unique id for the source feed
409 private function _getSource($element)
411 $sourceEl = ActivityUtils::child($element, 'source');
413 if (empty($sourceEl)) {
416 $href = ActivityUtils::getLink($sourceEl, 'self');
420 return ActivityUtils::childContent($sourceEl, 'id');
425 static function fromNotice(Notice $notice)
427 $object = new ActivityObject();
429 if (Event::handle('StartActivityObjectFromNotice', array($notice, &$object))) {
431 $object->type = (empty($notice->object_type)) ? ActivityObject::NOTE : $notice->object_type;
433 $object->id = $notice->uri;
434 $object->title = $notice->content;
435 $object->content = $notice->rendered;
436 $object->link = $notice->bestUrl();
438 Event::handle('EndActivityObjectFromNotice', array($notice, &$object));
444 static function fromProfile(Profile $profile)
446 $object = new ActivityObject();
448 if (Event::handle('StartActivityObjectFromProfile', array($profile, &$object))) {
450 $object->type = ActivityObject::PERSON;
451 $object->id = $profile->getUri();
452 $object->title = $profile->getBestName();
453 $object->link = $profile->profileurl;
455 $orig = $profile->getOriginalAvatar();
458 $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
467 foreach ($sizes as $size) {
469 $avatar = $profile->getAvatar($size);
471 if (!empty($avatar)) {
472 $alink = AvatarLink::fromAvatar($avatar);
474 $alink = new AvatarLink();
475 $alink->type = 'image/png';
476 $alink->height = $size;
477 $alink->width = $size;
478 $alink->url = Avatar::defaultImage($size);
480 if ($size == AVATAR_PROFILE_SIZE) {
481 // Hack for Twitter import: we don't have a 96x96 image,
482 // but we do have a 73x73 image. For now, fake it with that.
483 $avatar = $profile->getAvatar(73);
485 $alink = AvatarLink::fromAvatar($avatar);
486 $alink->height= $size;
487 $alink->width = $size;
492 $object->avatarLinks[] = $alink;
495 if (isset($profile->lat) && isset($profile->lon)) {
496 $object->geopoint = (float)$profile->lat
497 . ' ' . (float)$profile->lon;
500 $object->poco = PoCo::fromProfile($profile);
502 Event::handle('EndActivityObjectFromProfile', array($profile, &$object));
508 static function fromGroup($group)
510 $object = new ActivityObject();
512 if (Event::handle('StartActivityObjectFromGroup', array($group, &$object))) {
514 $object->type = ActivityObject::GROUP;
515 $object->id = $group->getUri();
516 $object->title = $group->getBestName();
517 $object->link = $group->getUri();
519 $object->avatarLinks[] = AvatarLink::fromFilename($group->homepage_logo,
520 AVATAR_PROFILE_SIZE);
522 $object->avatarLinks[] = AvatarLink::fromFilename($group->stream_logo,
525 $object->avatarLinks[] = AvatarLink::fromFilename($group->mini_logo,
528 $object->poco = PoCo::fromGroup($group);
529 Event::handle('EndActivityObjectFromGroup', array($group, &$object));
535 static function fromPeopletag($ptag)
537 $object = new ActivityObject();
538 if (Event::handle('StartActivityObjectFromPeopletag', array($ptag, &$object))) {
539 $object->type = ActivityObject::_LIST;
541 $object->id = $ptag->getUri();
542 $object->title = $ptag->tag;
543 $object->summary = $ptag->description;
544 $object->link = $ptag->homeUrl();
545 $object->owner = Profile::staticGet('id', $ptag->tagger);
546 $object->poco = PoCo::fromProfile($object->owner);
547 Event::handle('EndActivityObjectFromPeopletag', array($ptag, &$object));
552 function outputTo($xo, $tag='activity:object')
555 $xo->elementStart($tag);
558 if (Event::handle('StartActivityObjectOutputAtom', array($this, $xo))) {
559 $xo->element('activity:object-type', null, $this->type);
563 if ($tag == 'author') {
564 $xo->element(self::URI, null, $this->id);
566 $xo->element(self::ID, null, $this->id);
569 if (!empty($this->title)) {
570 $name = common_xml_safe_str($this->title);
571 if ($tag == 'author') {
572 // XXX: Backward compatibility hack -- atom:name should contain
573 // full name here, instead of nickname, i.e.: $name. Change
574 // this in the next version.
575 $xo->element(self::NAME, null, $this->poco->preferredUsername);
577 $xo->element(self::TITLE, null, $name);
581 if (!empty($this->summary)) {
585 common_xml_safe_str($this->summary)
589 if (!empty($this->content)) {
590 // XXX: assuming HTML content here
592 ActivityUtils::CONTENT,
593 array('type' => 'html'),
594 common_xml_safe_str($this->content)
598 if (!empty($this->link)) {
602 'rel' => 'alternate',
603 'type' => 'text/html',
604 'href' => $this->link
610 if(!empty($this->owner)) {
611 $owner = $this->owner->asActivityNoun(self::AUTHOR);
615 if ($this->type == ActivityObject::PERSON
616 || $this->type == ActivityObject::GROUP) {
618 foreach ($this->avatarLinks as $avatar) {
622 'type' => $avatar->type,
623 'media:width' => $avatar->width,
624 'media:height' => $avatar->height,
625 '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: attachedObjects are added by Activity
686 $object['displayName'] = $this->title;
688 // TODO: downstreamDuplicates
690 // embedCode (used for video)
694 // XXX: Should we use URL here? or a crazy tag URI?
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 $object['avatarLinks'] = $avatarMediaLinks; // extension
728 $object['image'] = $imgLink->asArray();
733 // We can probably use the whole schema URL here but probably the
734 // relative simple name is easier to parse
735 // @fixme this breaks extension URIs
736 $object['type'] = substr($this->type, strrpos($this->type, '/') + 1);
739 $object['summary'] = $this->summary;
741 // TODO: upstreamDuplicates
743 // url (XXX: need to put the right thing here...)
744 $object['url'] = $this->id;
747 // @fixme these may collide with XML extensions
748 // @fixme multiple tags of same name will overwrite each other
749 // @fixme text content from XML extensions will be lost
750 foreach ($this->extra as $e) {
751 list($objectName, $props, $txt) = $e;
752 $object[$objectName] = $props;
757 if (!empty($this->geopoint)) {
759 list($lat, $long) = explode(' ', $this->geopoint);
761 $object['geopoint'] = array(
763 'coordinates' => array($lat, $long)
767 if (!empty($this->poco)) {
768 $object['contact'] = $this->poco->asArray();
770 Event::handle('EndActivityObjectOutputJson', array($this, &$object));
772 return array_filter($object);