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 PLACE = 'http://activitystrea.ms/schema/1.0/place';
68 const COMMENT = 'http://activitystrea.ms/schema/1.0/comment';
71 // Atom elements we snarf
73 const TITLE = 'title';
74 const SUMMARY = 'summary';
76 const SOURCE = 'source';
80 const EMAIL = 'email';
82 const POSTEROUS = 'http://posterous.com/help/rss/1.0';
83 const AUTHOR = 'author';
84 const USERIMAGE = 'userImage';
85 const PROFILEURL = 'profileUrl';
86 const NICKNAME = 'nickName';
87 const DISPLAYNAME = 'displayName';
97 public $avatarLinks = array();
102 // @todo move this stuff to it's own PHOTO activity object
103 const MEDIA_DESCRIPTION = 'description';
108 public $extra = array();
113 * This probably needs to be refactored
114 * to generate a local class (ActivityPerson, ActivityFile, ...)
115 * based on the object type.
117 * @param DOMElement $element DOM thing to turn into an Activity thing
119 function __construct($element = null)
121 if (empty($element)) {
125 $this->element = $element;
127 $this->geopoint = $this->_childContent(
129 ActivityContext::POINT,
130 ActivityContext::GEORSS
133 if ($element->tagName == 'author') {
134 $this->_fromAuthor($element);
135 } else if ($element->tagName == 'item') {
136 $this->_fromRssItem($element);
138 $this->_fromAtomEntry($element);
141 // Some per-type attributes...
142 if ($this->type == self::PERSON || $this->type == self::GROUP) {
143 $this->displayName = $this->title;
145 $photos = ActivityUtils::getLinks($element, 'photo');
146 if (count($photos)) {
147 foreach ($photos as $link) {
148 $this->avatarLinks[] = new AvatarLink($link);
151 $avatars = ActivityUtils::getLinks($element, 'avatar');
152 foreach ($avatars as $link) {
153 $this->avatarLinks[] = new AvatarLink($link);
157 $this->poco = new PoCo($element);
160 if ($this->type == self::PHOTO) {
162 $this->thumbnail = ActivityUtils::getLink($element, 'preview');
163 $this->largerImage = ActivityUtils::getLink($element, 'enclosure');
165 $this->description = ActivityUtils::childContent(
167 ActivityObject::MEDIA_DESCRIPTION,
173 private function _fromAuthor($element)
175 $this->type = $this->_childContent($element,
176 Activity::OBJECTTYPE,
179 if (empty($this->type)) {
180 $this->type = self::PERSON; // XXX: is this fair?
183 // start with <atom:title>
185 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
187 if (!empty($title)) {
188 $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
191 // fall back to <atom:name>
193 if (empty($this->title)) {
194 $this->title = $this->_childContent($element, self::NAME);
197 // start with <atom:id>
199 $this->id = $this->_childContent($element, self::ID);
201 // fall back to <atom:uri>
203 if (empty($this->id)) {
204 $this->id = $this->_childContent($element, self::URI);
207 // fall further back to <atom:email>
209 if (empty($this->id)) {
210 $email = $this->_childContent($element, self::EMAIL);
211 if (!empty($email)) {
213 $this->id = 'mailto:'.$email;
217 $this->link = ActivityUtils::getPermalink($element);
219 // fall finally back to <link rel=alternate>
221 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
222 $this->id = $this->link;
226 private function _fromAtomEntry($element)
228 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
231 if (empty($this->type)) {
232 $this->type = ActivityObject::NOTE;
235 $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
236 $this->content = ActivityUtils::getContent($element);
238 // We don't like HTML in our titles, although it's technically allowed
240 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
242 $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
244 $this->source = $this->_getSource($element);
246 $this->link = ActivityUtils::getPermalink($element);
248 $this->id = $this->_childContent($element, self::ID);
250 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
251 $this->id = $this->link;
255 // @todo FIXME: rationalize with Activity::_fromRssItem()
256 private function _fromRssItem($item)
258 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
260 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
262 if (!empty($contentEl)) {
263 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
265 $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
266 if (!empty($descriptionEl)) {
267 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
271 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
273 $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
275 if (!empty($guidEl)) {
276 $this->id = $guidEl->textContent;
278 if ($guidEl->hasAttribute('isPermaLink')) {
280 $this->link = $this->id;
285 public static function fromRssAuthor($el)
287 $text = $el->textContent;
289 if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
292 } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
295 } else if (preg_match('/.*@.*/', $text)) {
303 // Not really enough info
305 $obj = new ActivityObject();
309 $obj->type = ActivityObject::PERSON;
312 if (!empty($email)) {
313 $obj->id = 'mailto:'.$email;
319 public static function fromDcCreator($el)
321 // Not really enough info
323 $text = $el->textContent;
325 $obj = new ActivityObject();
330 $obj->type = ActivityObject::PERSON;
335 public static function fromRssChannel($el)
337 $obj = new ActivityObject();
341 $obj->type = ActivityObject::PERSON; // @fixme guess better
343 $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
344 $obj->link = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
345 $obj->id = ActivityUtils::getLink($el, Activity::SELF);
347 if (empty($obj->id)) {
348 $obj->id = $obj->link;
351 $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
354 $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
357 $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
359 if (!empty($imageEl)) {
360 $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
361 $al = new AvatarLink();
363 $obj->avatarLinks[] = $al;
369 public static function fromPosterousAuthor($el)
371 $obj = new ActivityObject();
373 $obj->type = ActivityObject::PERSON; // @fixme any others...?
375 $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
377 if (!empty($userImage)) {
378 $al = new AvatarLink();
379 $al->url = $userImage;
380 $obj->avatarLinks[] = $al;
383 $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
384 $obj->id = $obj->link;
386 $obj->poco = new PoCo();
388 $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
389 $obj->poco->displayName = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
391 $obj->title = $obj->poco->displayName;
396 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
398 return ActivityUtils::childContent($element, $tag, $namespace);
401 // Try to get a unique id for the source feed
403 private function _getSource($element)
405 $sourceEl = ActivityUtils::child($element, 'source');
407 if (empty($sourceEl)) {
410 $href = ActivityUtils::getLink($sourceEl, 'self');
414 return ActivityUtils::childContent($sourceEl, 'id');
419 static function fromNotice(Notice $notice)
421 $object = new ActivityObject();
423 if (Event::handle('StartActivityObjectFromNotice', array($notice, &$object))) {
425 $object->type = ActivityObject::NOTE;
427 $object->id = $notice->uri;
428 $object->title = $notice->content;
429 $object->content = $notice->rendered;
430 $object->link = $notice->bestUrl();
432 Event::handle('EndActivityObjectFromNotice', array($notice, &$object));
438 static function fromProfile(Profile $profile)
440 $object = new ActivityObject();
442 if (Event::handle('StartActivityObjectFromProfile', array($profile, &$object))) {
444 $object->type = ActivityObject::PERSON;
445 $object->id = $profile->getUri();
446 $object->title = $profile->getBestName();
447 $object->link = $profile->profileurl;
449 $orig = $profile->getOriginalAvatar();
452 $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
461 foreach ($sizes as $size) {
463 $avatar = $profile->getAvatar($size);
465 if (!empty($avatar)) {
466 $alink = AvatarLink::fromAvatar($avatar);
468 $alink = new AvatarLink();
469 $alink->type = 'image/png';
470 $alink->height = $size;
471 $alink->width = $size;
472 $alink->url = Avatar::defaultImage($size);
474 if ($size == AVATAR_PROFILE_SIZE) {
475 // Hack for Twitter import: we don't have a 96x96 image,
476 // but we do have a 73x73 image. For now, fake it with that.
477 $avatar = $profile->getAvatar(73);
479 $alink = AvatarLink::fromAvatar($avatar);
480 $alink->height= $size;
481 $alink->width = $size;
486 $object->avatarLinks[] = $alink;
489 if (isset($profile->lat) && isset($profile->lon)) {
490 $object->geopoint = (float)$profile->lat
491 . ' ' . (float)$profile->lon;
494 $object->poco = PoCo::fromProfile($profile);
496 Event::handle('EndActivityObjectFromProfile', array($profile, &$object));
502 static function fromGroup($group)
504 $object = new ActivityObject();
506 if (Event::handle('StartActivityObjectFromGroup', array($group, &$object))) {
508 $object->type = ActivityObject::GROUP;
509 $object->id = $group->getUri();
510 $object->title = $group->getBestName();
511 $object->link = $group->getUri();
513 $object->avatarLinks[] = AvatarLink::fromFilename($group->homepage_logo,
514 AVATAR_PROFILE_SIZE);
516 $object->avatarLinks[] = AvatarLink::fromFilename($group->stream_logo,
519 $object->avatarLinks[] = AvatarLink::fromFilename($group->mini_logo,
522 $object->poco = PoCo::fromGroup($group);
524 Event::handle('EndActivityObjectFromGroup', array($group, &$object));
530 function outputTo($xo, $tag='activity:object')
533 $xo->elementStart($tag);
536 $xo->element('activity:object-type', null, $this->type);
540 if ($tag == 'author') {
541 $xo->element(self::URI, null, $this->id);
543 $xo->element(self::ID, null, $this->id);
546 if (!empty($this->title)) {
547 $name = common_xml_safe_str($this->title);
548 if ($tag == 'author') {
549 // XXX: Backward compatibility hack -- atom:name should contain
550 // full name here, instead of nickname, i.e.: $name. Change
551 // this in the next version.
552 $xo->element(self::NAME, null, $this->poco->preferredUsername);
554 $xo->element(self::TITLE, null, $name);
558 if (!empty($this->summary)) {
562 common_xml_safe_str($this->summary)
566 if (!empty($this->content)) {
567 // XXX: assuming HTML content here
569 ActivityUtils::CONTENT,
570 array('type' => 'html'),
571 common_xml_safe_str($this->content)
575 if (!empty($this->link)) {
579 'rel' => 'alternate',
580 'type' => 'text/html',
581 'href' => $this->link
587 if ($this->type == ActivityObject::PERSON
588 || $this->type == ActivityObject::GROUP) {
590 foreach ($this->avatarLinks as $avatar) {
594 'type' => $avatar->type,
595 'media:width' => $avatar->width,
596 'media:height' => $avatar->height,
597 'href' => $avatar->url
604 if (!empty($this->geopoint)) {
612 if (!empty($this->poco)) {
613 $this->poco->outputTo($xo);
616 foreach ($this->extra as $el) {
617 list($extraTag, $attrs, $content) = $el;
618 $xo->element($extraTag, $attrs, $content);
622 $xo->elementEnd($tag);
628 function asString($tag='activity:object')
630 $xs = new XMLStringer(true);
632 $this->outputTo($xs, $tag);
634 return $xs->getString();
638 * Returns an array based on this Activity Object suitable for
641 * @return array $object the activity object array
648 // XXX: attachedObjects are added by Activity
651 $object['displayName'] = $this->title;
653 // TODO: downstreamDuplicates
655 // embedCode (used for video)
659 // XXX: Should we use URL here? or a crazy tag URI?
660 $object['id'] = $this->id;
662 if ($this->type == ActivityObject::PERSON
663 || $this->type == ActivityObject::GROUP) {
665 // XXX: Not sure what the best avatar is to use for the
666 // author's "image". For now, I'm using the stream size
667 // one, but possibly it should be large
670 foreach ($this->avatarLinks as $a) {
671 if ($a->height == AVATAR_STREAM_SIZE) {
677 $imgLink = new ActivityStreamsMediaLink(
684 $object['image'] = $imgLink->asArray();
689 // We can probably use the whole schema URL here but probably the
690 // relative simple name is easier to parse
691 $object['type'] = substr($this->type, strrpos($this->type, '/') + 1);
694 $object['summary'] = $this->summary;
696 // TODO: upstreamDuplicates
698 // url (XXX: need to put the right thing here...)
699 $object['url'] = $this->id;
701 // TODO: extensions (OStatus stuff, etc.)
703 return array_filter($object);