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')) {
35 require_once(INSTALLDIR.'/lib/activitystreamjsondocument.php');
38 * A noun-ish thing in the activity universe
40 * The activity streams spec talks about activity objects, while also having
41 * a tag activity:object, which is in fact an activity object. Aaaaaah!
43 * This is just a thing in the activity universe. Can be the subject, object,
44 * or indirect object (target!) of an activity verb. Rotten name, and I'm
45 * propagating it. *sigh*
49 * @author Evan Prodromou <evan@status.net>
50 * @copyright 2010 StatusNet, Inc.
51 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
52 * @link http://status.net/
56 const ARTICLE = 'http://activitystrea.ms/schema/1.0/article';
57 const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
58 const NOTE = 'http://activitystrea.ms/schema/1.0/note';
59 const STATUS = 'http://activitystrea.ms/schema/1.0/status';
60 const FILE = 'http://activitystrea.ms/schema/1.0/file';
61 const PHOTO = 'http://activitystrea.ms/schema/1.0/photo';
62 const ALBUM = 'http://activitystrea.ms/schema/1.0/photo-album';
63 const PLAYLIST = 'http://activitystrea.ms/schema/1.0/playlist';
64 const VIDEO = 'http://activitystrea.ms/schema/1.0/video';
65 const AUDIO = 'http://activitystrea.ms/schema/1.0/audio';
66 const BOOKMARK = 'http://activitystrea.ms/schema/1.0/bookmark';
67 const PERSON = 'http://activitystrea.ms/schema/1.0/person';
68 const GROUP = 'http://activitystrea.ms/schema/1.0/group';
69 const _LIST = 'http://activitystrea.ms/schema/1.0/list'; // LIST is reserved
70 const PLACE = 'http://activitystrea.ms/schema/1.0/place';
71 const COMMENT = 'http://activitystrea.ms/schema/1.0/comment';
73 const ACTIVITY = 'http://activitystrea.ms/schema/1.0/activity';
74 const SERVICE = 'http://activitystrea.ms/schema/1.0/service';
75 const IMAGE = 'http://activitystrea.ms/schema/1.0/image';
76 const COLLECTION = 'http://activitystrea.ms/schema/1.0/collection';
77 const APPLICATION = 'http://activitystrea.ms/schema/1.0/application';
79 // Atom elements we snarf
81 const TITLE = 'title';
82 const SUMMARY = 'summary';
84 const SOURCE = 'source';
88 const EMAIL = 'email';
90 const POSTEROUS = 'http://posterous.com/help/rss/1.0';
91 const AUTHOR = 'author';
92 const USERIMAGE = 'userImage';
93 const PROFILEURL = 'profileUrl';
94 const NICKNAME = 'nickName';
95 const DISPLAYNAME = 'displayName';
106 public $avatarLinks = array();
111 // @todo move this stuff to it's own PHOTO activity object
112 const MEDIA_DESCRIPTION = 'description';
117 public $extra = array();
124 * This probably needs to be refactored
125 * to generate a local class (ActivityPerson, ActivityFile, ...)
126 * based on the object type.
128 * @param DOMElement $element DOM thing to turn into an Activity thing
130 function __construct($element = null)
132 if (empty($element)) {
136 $this->element = $element;
138 $this->geopoint = $this->_childContent(
140 ActivityContext::POINT,
141 ActivityContext::GEORSS
144 if ($element->tagName == 'author') {
145 $this->_fromAuthor($element);
146 } else if ($element->tagName == 'item') {
147 $this->_fromRssItem($element);
149 $this->_fromAtomEntry($element);
152 // Some per-type attributes...
153 if ($this->type == self::PERSON || $this->type == self::GROUP) {
154 $this->displayName = $this->title;
156 $photos = ActivityUtils::getLinks($element, 'photo');
157 if (count($photos)) {
158 foreach ($photos as $link) {
159 $this->avatarLinks[] = new AvatarLink($link);
162 $avatars = ActivityUtils::getLinks($element, 'avatar');
163 foreach ($avatars as $link) {
164 $this->avatarLinks[] = new AvatarLink($link);
168 $this->poco = new PoCo($element);
171 if ($this->type == self::PHOTO) {
173 $this->thumbnail = ActivityUtils::getLink($element, 'preview');
174 $this->largerImage = ActivityUtils::getLink($element, 'enclosure');
176 $this->description = ActivityUtils::childContent(
178 ActivityObject::MEDIA_DESCRIPTION,
182 if ($this->type == self::_LIST) {
183 $owner = ActivityUtils::child($this->element, Activity::AUTHOR, Activity::SPEC);
184 $this->owner = new ActivityObject($owner);
188 private function _fromAuthor($element)
190 $this->type = $this->_childContent($element,
191 Activity::OBJECTTYPE,
194 if (empty($this->type)) {
195 $this->type = self::PERSON; // XXX: is this fair?
198 // start with <atom:title>
200 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
202 if (!empty($title)) {
203 $this->title = common_strip_html($title);
206 // fall back to <atom:name>
208 if (empty($this->title)) {
209 $this->title = $this->_childContent($element, self::NAME);
212 // start with <atom:id>
214 $this->id = $this->_childContent($element, self::ID);
216 // fall back to <atom:uri>
218 if (empty($this->id)) {
219 $this->id = $this->_childContent($element, self::URI);
222 // fall further back to <atom:email>
224 if (empty($this->id)) {
225 $email = $this->_childContent($element, self::EMAIL);
226 if (!empty($email)) {
228 $this->id = 'mailto:'.$email;
232 $this->link = ActivityUtils::getPermalink($element);
234 // fall finally back to <link rel=alternate>
236 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
237 $this->id = $this->link;
241 private function _fromAtomEntry($element)
243 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
246 if (empty($this->type)) {
247 $this->type = ActivityObject::NOTE;
250 $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
251 $this->content = ActivityUtils::getContent($element);
253 // We don't like HTML in our titles, although it's technically allowed
254 $this->title = common_strip_html(ActivityUtils::childHtmlContent($element, self::TITLE));
256 $this->source = $this->_getSource($element);
258 $this->link = ActivityUtils::getPermalink($element);
260 $this->id = $this->_childContent($element, self::ID);
262 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
263 $this->id = $this->link;
267 // @todo FIXME: rationalize with Activity::_fromRssItem()
268 private function _fromRssItem($item)
270 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
272 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
274 if (!empty($contentEl)) {
275 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
277 $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
278 if (!empty($descriptionEl)) {
279 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
283 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
285 $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
287 if (!empty($guidEl)) {
288 $this->id = $guidEl->textContent;
290 if ($guidEl->hasAttribute('isPermaLink')) {
292 $this->link = $this->id;
297 public static function fromRssAuthor($el)
299 $text = $el->textContent;
301 if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
304 } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
307 } else if (preg_match('/.*@.*/', $text)) {
315 // Not really enough info
317 $obj = new ActivityObject();
321 $obj->type = ActivityObject::PERSON;
324 if (!empty($email)) {
325 $obj->id = 'mailto:'.$email;
331 public static function fromDcCreator($el)
333 // Not really enough info
335 $text = $el->textContent;
337 $obj = new ActivityObject();
342 $obj->type = ActivityObject::PERSON;
347 public static function fromRssChannel($el)
349 $obj = new ActivityObject();
353 $obj->type = ActivityObject::PERSON; // @fixme guess better
355 $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
356 $obj->link = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
357 $obj->id = ActivityUtils::getLink($el, Activity::SELF);
359 if (empty($obj->id)) {
360 $obj->id = $obj->link;
363 $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
366 $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
369 $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
371 if (!empty($imageEl)) {
372 $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
373 $al = new AvatarLink();
375 $obj->avatarLinks[] = $al;
381 public static function fromPosterousAuthor($el)
383 $obj = new ActivityObject();
385 $obj->type = ActivityObject::PERSON; // @fixme any others...?
387 $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
389 if (!empty($userImage)) {
390 $al = new AvatarLink();
391 $al->url = $userImage;
392 $obj->avatarLinks[] = $al;
395 $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
396 $obj->id = $obj->link;
398 $obj->poco = new PoCo();
400 $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
401 $obj->poco->displayName = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
403 $obj->title = $obj->poco->displayName;
408 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
410 return ActivityUtils::childContent($element, $tag, $namespace);
413 // Try to get a unique id for the source feed
415 private function _getSource($element)
417 $sourceEl = ActivityUtils::child($element, 'source');
419 if (empty($sourceEl)) {
422 $href = ActivityUtils::getLink($sourceEl, 'self');
426 return ActivityUtils::childContent($sourceEl, 'id');
431 static function fromGroup(User_group $group)
433 $object = new ActivityObject();
435 if (Event::handle('StartActivityObjectFromGroup', array($group, &$object))) {
437 $object->type = ActivityObject::GROUP;
438 $object->id = $group->getUri();
439 $object->title = $group->getBestName();
440 $object->link = $group->getUri();
442 $object->avatarLinks[] = AvatarLink::fromFilename($group->homepage_logo,
443 AVATAR_PROFILE_SIZE);
445 $object->avatarLinks[] = AvatarLink::fromFilename($group->stream_logo,
448 $object->avatarLinks[] = AvatarLink::fromFilename($group->mini_logo,
451 $object->poco = PoCo::fromGroup($group);
452 Event::handle('EndActivityObjectFromGroup', array($group, &$object));
458 static function fromPeopletag($ptag)
460 $object = new ActivityObject();
461 if (Event::handle('StartActivityObjectFromPeopletag', array($ptag, &$object))) {
462 $object->type = ActivityObject::_LIST;
464 $object->id = $ptag->getUri();
465 $object->title = $ptag->tag;
466 $object->summary = $ptag->description;
467 $object->link = $ptag->homeUrl();
468 $object->owner = Profile::getKV('id', $ptag->tagger);
469 $object->poco = PoCo::fromProfile($object->owner);
470 Event::handle('EndActivityObjectFromPeopletag', array($ptag, &$object));
475 static function fromFile(File $file)
477 $object = new ActivityObject();
479 if (Event::handle('StartActivityObjectFromFile', array($file, &$object))) {
481 $object->type = self::mimeTypeToObjectType($file->mimetype);
482 $object->id = TagURI::mint(sprintf("file:%d", $file->id));
483 $object->link = common_local_url('attachment', array('attachment' => $file->id));
486 $object->title = $file->title;
490 $object->date = $file->date;
494 $thumbnail = $file->getThumbnail();
495 $object->thumbnail = $thumbnail;
496 } catch (UseFileAsThumbnailException $e) {
497 $object->thumbnail = null;
498 } catch (UnsupportedMediaException $e) {
499 $object->thumbnail = null;
502 switch (self::canonicalType($object->type)) {
504 $object->largerImage = $file->url;
508 $object->stream = $file->url;
512 Event::handle('EndActivityObjectFromFile', array($file, &$object));
518 static function fromNoticeSource(Notice_source $source)
520 $object = new ActivityObject();
521 $wellKnown = array('web', 'xmpp', 'mail', 'omb', 'system', 'api', 'ostatus',
522 'activity', 'feed', 'mirror', 'twitter', 'facebook');
524 if (Event::handle('StartActivityObjectFromNoticeSource', array($source, &$object))) {
525 $object->type = ActivityObject::APPLICATION;
527 if (in_array($source->code, $wellKnown)) {
528 // We use one ID for all well-known StatusNet sources
529 $object->id = "tag:status.net,2009:notice-source:".$source->code;
530 } else if ($source->url) {
531 // They registered with an URL
532 $object->id = $source->url;
534 // Locally-registered, no URL
535 $object->id = TagURI::mint("notice-source:".$source->code);
539 $object->link = $source->url;
543 $object->title = $source->name;
545 $object->title = $source->code;
548 if ($source->created) {
549 $object->date = $source->created;
552 $object->extra[] = array('status_net', array('source_code' => $source->code));
554 Event::handle('EndActivityObjectFromNoticeSource', array($source, &$object));
560 static function fromMessage(Message $message)
562 $object = new ActivityObject();
564 if (Event::handle('StartActivityObjectFromMessage', array($message, &$object))) {
566 $object->type = ActivityObject::NOTE;
567 $object->id = ($message->uri) ? $message->uri : (($message->url) ? $message->url : TagURI::mint(sprintf("message:%d", $message->id)));
568 $object->content = $message->rendered;
569 $object->date = $message->created;
572 $object->link = $message->url;
574 $object->link = common_local_url('showmessage', array('message' => $message->id));
577 $object->extra[] = array('status_net', array('message_id' => $message->id));
579 Event::handle('EndActivityObjectFromMessage', array($message, &$object));
585 function outputTo($xo, $tag='activity:object')
588 $xo->elementStart($tag);
591 if (Event::handle('StartActivityObjectOutputAtom', array($this, $xo))) {
592 $xo->element('activity:object-type', null, $this->type);
596 if ($tag == 'author') {
597 $xo->element(self::URI, null, $this->id);
599 $xo->element(self::ID, null, $this->id);
602 if (!empty($this->title)) {
603 $name = common_xml_safe_str($this->title);
604 if ($tag == 'author') {
605 // XXX: Backward compatibility hack -- atom:name should contain
606 // full name here, instead of nickname, i.e.: $name. Change
607 // this in the next version.
608 $xo->element(self::NAME, null, $this->poco->preferredUsername);
610 $xo->element(self::TITLE, null, $name);
614 if (!empty($this->summary)) {
618 common_xml_safe_str($this->summary)
622 if (!empty($this->content)) {
623 // XXX: assuming HTML content here
625 ActivityUtils::CONTENT,
626 array('type' => 'html'),
627 common_xml_safe_str($this->content)
631 if (!empty($this->link)) {
635 'rel' => 'alternate',
636 'type' => 'text/html',
637 'href' => $this->link
643 if(!empty($this->owner)) {
644 $owner = $this->owner->asActivityNoun(self::AUTHOR);
648 if ($this->type == ActivityObject::PERSON
649 || $this->type == ActivityObject::GROUP) {
651 foreach ($this->avatarLinks as $alink) {
655 'type' => $alink->type,
656 'media:width' => $alink->width,
657 'media:height' => $alink->height,
658 'href' => $alink->url,
664 if (!empty($this->geopoint)) {
672 if (!empty($this->poco)) {
673 $this->poco->outputTo($xo);
676 // @fixme there's no way here to make a tree; elements can only contain plaintext
677 // @fixme these may collide with JSON extensions
678 foreach ($this->extra as $el) {
679 list($extraTag, $attrs, $content) = array_pad($el, 3, null);
680 $xo->element($extraTag, $attrs, $content);
683 Event::handle('EndActivityObjectOutputAtom', array($this, $xo));
687 $xo->elementEnd($tag);
693 function asString($tag='activity:object')
695 $xs = new XMLStringer(true);
697 $this->outputTo($xs, $tag);
699 return $xs->getString();
703 * Returns an array based on this Activity Object suitable for
706 * @return array $object the activity object array
713 if (Event::handle('StartActivityObjectOutputJson', array($this, &$object))) {
714 // XXX: attachments are added by Activity
716 // author (Add object for author? Could be useful for repeats.)
718 // content (Add rendered version of the notice?)
720 // downstreamDuplicates
725 $object['id'] = $this->id;
726 } else if ($this->link) {
727 $object['id'] = $this->link;
730 if ($this->type == ActivityObject::PERSON
731 || $this->type == ActivityObject::GROUP) {
734 $object['displayName'] = $this->title;
736 // XXX: Not sure what the best avatar is to use for the
737 // author's "image". For now, I'm using the large size.
740 $avatarMediaLinks = array();
742 foreach ($this->avatarLinks as $a) {
744 // Make a MediaLink for every other Avatar
745 $avatar = new ActivityStreamsMediaLink(
753 // Find the big avatar to use as the "image"
754 if ($a->height == AVATAR_PROFILE_SIZE) {
758 $avatarMediaLinks[] = $avatar->asArray();
761 if (!array_key_exists('status_net', $object)) {
762 $object['status_net'] = array();
765 $object['status_net']['avatarLinks'] = $avatarMediaLinks; // extension
768 if (!empty($imgLink)) {
769 $object['image'] = $imgLink->asArray();
775 // We can probably use the whole schema URL here but probably the
776 // relative simple name is easier to parse
778 $object['objectType'] = self::canonicalType($this->type);
781 $object['summary'] = $this->summary;
783 // content, usually rendered HTML
784 $object['content'] = $this->content;
786 // published (probably don't need. Might be useful for repeats.)
788 // updated (probably don't need this)
790 // TODO: upstreamDuplicates
793 $object['url'] = $this->link;
797 // @fixme these may collide with XML extensions
798 // @fixme multiple tags of same name will overwrite each other
799 // @fixme text content from XML extensions will be lost
801 foreach ($this->extra as $e) {
802 list($objectName, $props, $txt) = array_pad($e, 3, null);
803 if (!empty($objectName)) {
804 $parts = explode(":", $objectName);
805 if (count($parts) == 2 && $parts[0] == "statusnet") {
806 if (!array_key_exists('status_net', $object)) {
807 $object['status_net'] = array();
809 $object['status_net'][$parts[1]] = $props;
811 $object[$objectName] = $props;
816 if (!empty($this->geopoint)) {
818 list($lat, $lon) = explode(' ', $this->geopoint);
820 if (!empty($lat) && !empty($lon)) {
821 $object['location'] = array(
822 'objectType' => 'place',
823 'position' => sprintf("%+02.5F%+03.5F/", $lat, $lon),
828 $loc = Location::fromLatLon((float)$lat, (float)$lon);
831 $name = $loc->getName();
834 $object['location']['displayName'] = $name;
836 $url = $loc->getURL();
839 $object['location']['url'] = $url;
845 if (!empty($this->poco)) {
846 $object['portablecontacts_net'] = array_filter($this->poco->asArray());
849 if (!empty($this->thumbnail)) {
850 if (is_string($this->thumbnail)) {
851 $object['image'] = array('url' => $this->thumbnail);
853 $object['image'] = array('url' => $this->thumbnail->url);
854 if ($this->thumbnail->width) {
855 $object['image']['width'] = $this->thumbnail->width;
857 if ($this->thumbnail->height) {
858 $object['image']['height'] = $this->thumbnail->height;
863 switch (self::canonicalType($this->type)) {
865 if (!empty($this->largerImage)) {
866 $object['fullImage'] = array('url' => $this->largerImage);
871 if (!empty($this->stream)) {
872 $object['stream'] = array('url' => $this->stream);
877 Event::handle('EndActivityObjectOutputJson', array($this, &$object));
879 return array_filter($object);
882 public function getIdentifiers() {
884 foreach(array('id', 'link', 'url') as $id) {
885 if (isset($this->$id)) {
889 return array_unique($ids);
892 static function canonicalType($type) {
893 return ActivityUtils::resolveUri($type, true);
896 static function mimeTypeToObjectType($mimeType) {
901 if (empty($mimeType)) {
905 $parts = explode('/', $mimeType);