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';
73 // Atom elements we snarf
75 const TITLE = 'title';
76 const SUMMARY = 'summary';
78 const SOURCE = 'source';
82 const EMAIL = 'email';
84 const POSTEROUS = 'http://posterous.com/help/rss/1.0';
85 const AUTHOR = 'author';
86 const USERIMAGE = 'userImage';
87 const PROFILEURL = 'profileUrl';
88 const NICKNAME = 'nickName';
89 const DISPLAYNAME = 'displayName';
100 public $avatarLinks = array();
105 // @todo move this stuff to it's own PHOTO activity object
106 const MEDIA_DESCRIPTION = 'description';
111 public $extra = array();
116 * This probably needs to be refactored
117 * to generate a local class (ActivityPerson, ActivityFile, ...)
118 * based on the object type.
120 * @param DOMElement $element DOM thing to turn into an Activity thing
122 function __construct($element = null)
124 if (empty($element)) {
128 $this->element = $element;
130 $this->geopoint = $this->_childContent(
132 ActivityContext::POINT,
133 ActivityContext::GEORSS
136 if ($element->tagName == 'author') {
137 $this->_fromAuthor($element);
138 } else if ($element->tagName == 'item') {
139 $this->_fromRssItem($element);
141 $this->_fromAtomEntry($element);
144 // Some per-type attributes...
145 if ($this->type == self::PERSON || $this->type == self::GROUP) {
146 $this->displayName = $this->title;
148 $photos = ActivityUtils::getLinks($element, 'photo');
149 if (count($photos)) {
150 foreach ($photos as $link) {
151 $this->avatarLinks[] = new AvatarLink($link);
154 $avatars = ActivityUtils::getLinks($element, 'avatar');
155 foreach ($avatars as $link) {
156 $this->avatarLinks[] = new AvatarLink($link);
160 $this->poco = new PoCo($element);
163 if ($this->type == self::PHOTO) {
165 $this->thumbnail = ActivityUtils::getLink($element, 'preview');
166 $this->largerImage = ActivityUtils::getLink($element, 'enclosure');
168 $this->description = ActivityUtils::childContent(
170 ActivityObject::MEDIA_DESCRIPTION,
174 if ($this->type == self::_LIST) {
175 $owner = ActivityUtils::child($this->element, Activity::AUTHOR, Activity::SPEC);
176 $this->owner = new ActivityObject($owner);
180 private function _fromAuthor($element)
182 $this->type = $this->_childContent($element,
183 Activity::OBJECTTYPE,
186 if (empty($this->type)) {
187 $this->type = self::PERSON; // XXX: is this fair?
190 // start with <atom:title>
192 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
194 if (!empty($title)) {
195 $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
198 // fall back to <atom:name>
200 if (empty($this->title)) {
201 $this->title = $this->_childContent($element, self::NAME);
204 // start with <atom:id>
206 $this->id = $this->_childContent($element, self::ID);
208 // fall back to <atom:uri>
210 if (empty($this->id)) {
211 $this->id = $this->_childContent($element, self::URI);
214 // fall further back to <atom:email>
216 if (empty($this->id)) {
217 $email = $this->_childContent($element, self::EMAIL);
218 if (!empty($email)) {
220 $this->id = 'mailto:'.$email;
224 $this->link = ActivityUtils::getPermalink($element);
226 // fall finally back to <link rel=alternate>
228 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
229 $this->id = $this->link;
233 private function _fromAtomEntry($element)
235 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
238 if (empty($this->type)) {
239 $this->type = ActivityObject::NOTE;
242 $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
243 $this->content = ActivityUtils::getContent($element);
245 // We don't like HTML in our titles, although it's technically allowed
247 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
249 $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
251 $this->source = $this->_getSource($element);
253 $this->link = ActivityUtils::getPermalink($element);
255 $this->id = $this->_childContent($element, self::ID);
257 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
258 $this->id = $this->link;
262 // @todo FIXME: rationalize with Activity::_fromRssItem()
263 private function _fromRssItem($item)
265 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
267 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
269 if (!empty($contentEl)) {
270 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
272 $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
273 if (!empty($descriptionEl)) {
274 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
278 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
280 $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
282 if (!empty($guidEl)) {
283 $this->id = $guidEl->textContent;
285 if ($guidEl->hasAttribute('isPermaLink')) {
287 $this->link = $this->id;
292 public static function fromRssAuthor($el)
294 $text = $el->textContent;
296 if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
299 } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
302 } else if (preg_match('/.*@.*/', $text)) {
310 // Not really enough info
312 $obj = new ActivityObject();
316 $obj->type = ActivityObject::PERSON;
319 if (!empty($email)) {
320 $obj->id = 'mailto:'.$email;
326 public static function fromDcCreator($el)
328 // Not really enough info
330 $text = $el->textContent;
332 $obj = new ActivityObject();
337 $obj->type = ActivityObject::PERSON;
342 public static function fromRssChannel($el)
344 $obj = new ActivityObject();
348 $obj->type = ActivityObject::PERSON; // @fixme guess better
350 $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
351 $obj->link = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
352 $obj->id = ActivityUtils::getLink($el, Activity::SELF);
354 if (empty($obj->id)) {
355 $obj->id = $obj->link;
358 $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
361 $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
364 $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
366 if (!empty($imageEl)) {
367 $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
368 $al = new AvatarLink();
370 $obj->avatarLinks[] = $al;
376 public static function fromPosterousAuthor($el)
378 $obj = new ActivityObject();
380 $obj->type = ActivityObject::PERSON; // @fixme any others...?
382 $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
384 if (!empty($userImage)) {
385 $al = new AvatarLink();
386 $al->url = $userImage;
387 $obj->avatarLinks[] = $al;
390 $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
391 $obj->id = $obj->link;
393 $obj->poco = new PoCo();
395 $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
396 $obj->poco->displayName = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
398 $obj->title = $obj->poco->displayName;
403 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
405 return ActivityUtils::childContent($element, $tag, $namespace);
408 // Try to get a unique id for the source feed
410 private function _getSource($element)
412 $sourceEl = ActivityUtils::child($element, 'source');
414 if (empty($sourceEl)) {
417 $href = ActivityUtils::getLink($sourceEl, 'self');
421 return ActivityUtils::childContent($sourceEl, 'id');
426 static function fromNotice(Notice $notice)
428 $object = new ActivityObject();
430 if (Event::handle('StartActivityObjectFromNotice', array($notice, &$object))) {
432 $object->type = (empty($notice->object_type)) ? ActivityObject::NOTE : $notice->object_type;
434 $object->id = $notice->uri;
435 $object->title = $notice->content;
436 $object->content = $notice->rendered;
437 $object->link = $notice->bestUrl();
439 Event::handle('EndActivityObjectFromNotice', array($notice, &$object));
445 static function fromProfile(Profile $profile)
447 $object = new ActivityObject();
449 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
632 if (!empty($this->geopoint)) {
640 if (!empty($this->poco)) {
641 $this->poco->outputTo($xo);
644 // @fixme there's no way here to make a tree; elements can only contain plaintext
645 // @fixme these may collide with JSON extensions
646 foreach ($this->extra as $el) {
647 list($extraTag, $attrs, $content) = $el;
648 $xo->element($extraTag, $attrs, $content);
651 Event::handle('EndActivityObjectOutputAtom', array($this, $xo));
655 $xo->elementEnd($tag);
661 function asString($tag='activity:object')
663 $xs = new XMLStringer(true);
665 $this->outputTo($xs, $tag);
667 return $xs->getString();
671 * Returns an array based on this Activity Object suitable for
674 * @return array $object the activity object array
681 if (Event::handle('StartActivityObjectOutputJson', array($this, &$object))) {
682 // XXX: attachments are added by Activity
684 // author (Add object for author? Could be useful for repeats.)
686 // content (Add rendered version of the notice?)
689 $object['displayName'] = $this->title;
691 // downstreamDuplicates
694 $object['id'] = $this->id;
696 // XXX: Should we use URL here? or a crazy tag URI?
697 $object['id'] = $this->id;
699 if ($this->type == ActivityObject::PERSON
700 || $this->type == ActivityObject::GROUP) {
702 // XXX: Not sure what the best avatar is to use for the
703 // author's "image". For now, I'm using the large size.
706 $avatarMediaLinks = array();
708 foreach ($this->avatarLinks as $a) {
710 // Make a MediaLink for every other Avatar
711 $avatar = new ActivityStreamsMediaLink(
719 // Find the big avatar to use as the "image"
720 if ($a->height == AVATAR_PROFILE_SIZE) {
724 $avatarMediaLinks[] = $avatar->asArray();
727 $object['avatarLinks'] = $avatarMediaLinks; // extension
730 if (!empty($imgLink)) {
731 $object['image'] = $imgLink->asArray();
737 // We can probably use the whole schema URL here but probably the
738 // relative simple name is easier to parse
739 // @fixme this breaks extension URIs
740 $object['type'] = substr($this->type, strrpos($this->type, '/') + 1);
742 // published (probably don't need. Might be useful for repeats.)
745 $object['summary'] = $this->summary;
747 // udpated (probably don't need this)
749 // TODO: upstreamDuplicates
751 // url (XXX: need to put the right thing here...)
752 $object['url'] = $this->id;
755 // @fixme these may collide with XML extensions
756 // @fixme multiple tags of same name will overwrite each other
757 // @fixme text content from XML extensions will be lost
758 foreach ($this->extra as $e) {
759 list($objectName, $props, $txt) = $e;
760 $object[$objectName] = $props;
763 if (!empty($this->geopoint)) {
765 list($lat, $long) = explode(' ', $this->geopoint);
767 $object['geopoint'] = array(
769 'coordinates' => array($lat, $long)
773 if (!empty($this->poco)) {
774 $object['contact'] = array_filter($this->poco->asArray());
776 Event::handle('EndActivityObjectOutputJson', array($this, &$object));
778 return array_filter($object);