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('GNUSOCIAL')) { exit(1); }
33 require_once(INSTALLDIR.'/lib/activitystreamjsondocument.php');
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';
74 const COLLECTION = 'http://activitystrea.ms/schema/1.0/collection';
75 const APPLICATION = 'http://activitystrea.ms/schema/1.0/application';
77 // Atom elements we snarf
79 const TITLE = 'title';
80 const SUMMARY = 'summary';
82 const SOURCE = 'source';
86 const EMAIL = 'email';
88 const POSTEROUS = 'http://posterous.com/help/rss/1.0';
89 const AUTHOR = 'author';
90 const USERIMAGE = 'userImage';
91 const PROFILEURL = 'profileUrl';
92 const NICKNAME = 'nickName';
93 const DISPLAYNAME = 'displayName';
103 public $selfLink; // think APP (Atom Publishing Protocol)
105 public $avatarLinks = array();
110 // @todo move this stuff to it's own PHOTO activity object
111 const MEDIA_DESCRIPTION = 'description';
116 public $extra = array();
123 * This probably needs to be refactored
124 * to generate a local class (ActivityPerson, ActivityFile, ...)
125 * based on the object type.
127 * @param DOMElement $element DOM thing to turn into an Activity thing
129 function __construct($element = null)
131 if (empty($element)) {
135 $this->element = $element;
137 $this->geopoint = $this->_childContent(
139 ActivityContext::POINT,
140 ActivityContext::GEORSS
143 if ($element->tagName == 'author') {
144 $this->_fromAuthor($element);
145 } else if ($element->tagName == 'item') {
146 $this->_fromRssItem($element);
148 $this->_fromAtomEntry($element);
151 // Some per-type attributes...
152 if ($this->type == self::PERSON || $this->type == self::GROUP) {
153 $this->displayName = $this->title;
155 $photos = ActivityUtils::getLinks($element, 'photo');
156 if (count($photos)) {
157 foreach ($photos as $link) {
158 $this->avatarLinks[] = new AvatarLink($link);
161 $avatars = ActivityUtils::getLinks($element, 'avatar');
162 foreach ($avatars as $link) {
163 $this->avatarLinks[] = new AvatarLink($link);
167 $this->poco = new PoCo($element);
170 if ($this->type == self::PHOTO) {
172 $this->thumbnail = ActivityUtils::getLink($element, 'preview');
173 $this->largerImage = ActivityUtils::getLink($element, 'enclosure');
175 $this->description = ActivityUtils::childContent(
177 ActivityObject::MEDIA_DESCRIPTION,
181 if ($this->type == self::_LIST) {
182 $owner = ActivityUtils::child($this->element, Activity::AUTHOR, Activity::SPEC);
183 $this->owner = new ActivityObject($owner);
187 private function _fromAuthor($element)
189 $this->type = $this->_childContent($element,
190 Activity::OBJECTTYPE,
193 if (empty($this->type)) {
194 $this->type = self::PERSON; // XXX: is this fair?
198 // Start with <poco::displayName>
200 $this->title = ActivityUtils::childContent($element, PoCo::DISPLAYNAME, PoCo::NS);
202 // try falling back to <atom:title>
204 if (empty($this->title)) {
205 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
207 if (!empty($title)) {
208 $this->title = common_strip_html($title);
212 // fall back to <atom:name> as a last resort
214 if (empty($this->title)) {
215 $this->title = $this->_childContent($element, self::NAME);
218 // start with <atom:id>
220 $this->id = $this->_childContent($element, self::ID);
222 // fall back to <atom:uri>
224 if (empty($this->id)) {
225 $this->id = $this->_childContent($element, self::URI);
228 // fall further back to <atom:email>
230 if (empty($this->id)) {
231 $email = $this->_childContent($element, self::EMAIL);
232 if (!empty($email)) {
234 $this->id = 'mailto:'.$email;
238 $this->link = ActivityUtils::getPermalink($element);
240 // fall finally back to <link rel=alternate>
242 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
243 $this->id = $this->link;
247 private function _fromAtomEntry($element)
249 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
252 if (empty($this->type)) {
253 $this->type = ActivityObject::NOTE;
256 $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
257 $this->content = ActivityUtils::getContent($element);
259 // We don't like HTML in our titles, although it's technically allowed
260 $this->title = common_strip_html(ActivityUtils::childHtmlContent($element, self::TITLE));
262 $this->source = $this->_getSource($element);
264 $this->link = ActivityUtils::getPermalink($element);
265 $this->selfLink = ActivityUtils::getSelfLink($element);
267 $this->id = $this->_childContent($element, self::ID);
269 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
270 $this->id = $this->link;
273 $els = $element->childNodes;
276 for ($i = 0; $i < $els->length; $i++) {
277 $link = $els->item($i);
278 if ($link->localName == ActivityUtils::LINK && $link->namespaceURI == ActivityUtils::ATOM) {
280 foreach ($link->attributes as $attrName=>$attrNode) {
281 $attrs[$attrName] = $attrNode->nodeValue;
283 $this->extra[] = [$link->localName,
290 // @todo FIXME: rationalize with Activity::_fromRssItem()
291 private function _fromRssItem($item)
293 if (empty($this->type)) {
294 $this->type = ActivityObject::NOTE;
297 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
299 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
301 if (!empty($contentEl)) {
302 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
304 $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
305 if (!empty($descriptionEl)) {
306 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
310 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
312 $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
314 if (!empty($guidEl)) {
315 $this->id = $guidEl->textContent;
317 if ($guidEl->hasAttribute('isPermaLink') && $guidEl->getAttribute('isPermaLink') != 'false') {
319 $this->link = $this->id;
324 public static function fromRssAuthor($el)
326 $text = $el->textContent;
328 if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
331 } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
334 } else if (preg_match('/.*@.*/', $text)) {
342 // Not really enough info
344 $obj = new ActivityObject();
348 $obj->type = ActivityObject::PERSON;
351 if (!empty($email)) {
352 $obj->id = 'mailto:'.$email;
358 public static function fromDcCreator($el)
360 // Not really enough info
362 $text = $el->textContent;
364 $obj = new ActivityObject();
369 $obj->type = ActivityObject::PERSON;
374 public static function fromRssChannel($el)
376 $obj = new ActivityObject();
380 $obj->type = ActivityObject::PERSON; // @fixme guess better
382 $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
383 $obj->link = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
384 $obj->id = ActivityUtils::getLink($el, Activity::SELF);
386 if (empty($obj->id)) {
387 $obj->id = $obj->link;
390 $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
393 $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
396 $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
398 if (!empty($imageEl)) {
399 $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
400 $al = new AvatarLink();
402 $obj->avatarLinks[] = $al;
408 public static function fromPosterousAuthor($el)
410 $obj = new ActivityObject();
412 $obj->type = ActivityObject::PERSON; // @fixme any others...?
414 $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
416 if (!empty($userImage)) {
417 $al = new AvatarLink();
418 $al->url = $userImage;
419 $obj->avatarLinks[] = $al;
422 $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
423 $obj->id = $obj->link;
425 $obj->poco = new PoCo();
427 $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
428 $obj->poco->displayName = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
430 $obj->title = $obj->poco->displayName;
435 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
437 return ActivityUtils::childContent($element, $tag, $namespace);
440 // Try to get a unique id for the source feed
442 private function _getSource($element)
444 $sourceEl = ActivityUtils::child($element, 'source');
446 if (empty($sourceEl)) {
449 $href = ActivityUtils::getLink($sourceEl, 'self');
453 return ActivityUtils::childContent($sourceEl, 'id');
458 static function fromGroup(User_group $group)
460 $object = new ActivityObject();
462 if (Event::handle('StartActivityObjectFromGroup', array($group, &$object))) {
464 $object->type = ActivityObject::GROUP;
465 $object->id = $group->getUri();
466 $object->title = $group->getBestName();
467 $object->link = $group->getUri();
469 $object->avatarLinks[] = AvatarLink::fromFilename($group->homepage_logo,
470 AVATAR_PROFILE_SIZE);
472 $object->avatarLinks[] = AvatarLink::fromFilename($group->stream_logo,
475 $object->avatarLinks[] = AvatarLink::fromFilename($group->mini_logo,
478 $object->poco = PoCo::fromGroup($group);
479 Event::handle('EndActivityObjectFromGroup', array($group, &$object));
485 static function fromPeopletag($ptag)
487 $object = new ActivityObject();
488 if (Event::handle('StartActivityObjectFromPeopletag', array($ptag, &$object))) {
489 $object->type = ActivityObject::_LIST;
491 $object->id = $ptag->getUri();
492 $object->title = $ptag->tag;
493 $object->summary = $ptag->description;
494 $object->link = $ptag->homeUrl();
495 $object->owner = Profile::getKV('id', $ptag->tagger);
496 $object->poco = PoCo::fromProfile($object->owner);
497 Event::handle('EndActivityObjectFromPeopletag', array($ptag, &$object));
502 static function fromFile(File $file)
504 $object = new ActivityObject();
506 if (Event::handle('StartActivityObjectFromFile', array($file, &$object))) {
508 $object->type = self::mimeTypeToObjectType($file->mimetype);
509 $object->id = TagURI::mint(sprintf("file:%d", $file->id));
510 $object->link = $file->getAttachmentUrl();
513 $object->title = $file->title;
517 $object->date = $file->date;
521 $thumbnail = $file->getThumbnail();
522 $object->thumbnail = $thumbnail;
523 } catch (UseFileAsThumbnailException $e) {
524 $object->thumbnail = null;
525 } catch (UnsupportedMediaException $e) {
526 $object->thumbnail = null;
529 switch (self::canonicalType($object->type)) {
531 $object->largerImage = $file->getUrl();
535 $object->stream = $file->getUrl();
539 Event::handle('EndActivityObjectFromFile', array($file, &$object));
545 static function fromNoticeSource(Notice_source $source)
547 $object = new ActivityObject();
548 $wellKnown = array('web', 'xmpp', 'mail', 'omb', 'system', 'api', 'ostatus',
549 'activity', 'feed', 'mirror', 'twitter', 'facebook');
551 if (Event::handle('StartActivityObjectFromNoticeSource', array($source, &$object))) {
552 $object->type = ActivityObject::APPLICATION;
554 if (in_array($source->code, $wellKnown)) {
555 // We use one ID for all well-known StatusNet sources
556 $object->id = "tag:status.net,2009:notice-source:".$source->code;
557 } else if ($source->url) {
558 // They registered with an URL
559 $object->id = $source->url;
561 // Locally-registered, no URL
562 $object->id = TagURI::mint("notice-source:".$source->code);
566 $object->link = $source->url;
570 $object->title = $source->name;
572 $object->title = $source->code;
575 if ($source->created) {
576 $object->date = $source->created;
579 $object->extra[] = array('status_net', array('source_code' => $source->code));
581 Event::handle('EndActivityObjectFromNoticeSource', array($source, &$object));
587 static function fromMessage(Message $message)
589 $object = new ActivityObject();
591 if (Event::handle('StartActivityObjectFromMessage', array($message, &$object))) {
593 $object->type = ActivityObject::NOTE;
594 $object->id = ($message->uri) ? $message->uri : (($message->url) ? $message->url : TagURI::mint(sprintf("message:%d", $message->id)));
595 $object->content = $message->rendered;
596 $object->date = $message->created;
599 $object->link = $message->url;
601 $object->link = common_local_url('showmessage', array('message' => $message->id));
604 $object->extra[] = array('status_net', array('message_id' => $message->id));
606 Event::handle('EndActivityObjectFromMessage', array($message, &$object));
612 function outputTo($xo, $tag='activity:object')
615 $xo->elementStart($tag);
618 if (Event::handle('StartActivityObjectOutputAtom', array($this, $xo))) {
619 $xo->element('activity:object-type', null, $this->type);
623 if ($tag == 'author') {
624 $xo->element(self::URI, null, $this->id);
626 $xo->element(self::ID, null, $this->id);
629 if (!empty($this->title)) {
630 $name = common_xml_safe_str($this->title);
631 if ($tag == 'author') {
632 // XXX: Backward compatibility hack -- atom:name should contain
633 // full name here, instead of nickname, i.e.: $name. Change
634 // this in the next version.
635 $xo->element(self::NAME, null, $this->poco->preferredUsername);
637 $xo->element(self::TITLE, null, $name);
641 if (!empty($this->summary)) {
645 common_xml_safe_str($this->summary)
649 if (!empty($this->content)) {
650 // XXX: assuming HTML content here
652 ActivityUtils::CONTENT,
653 array('type' => 'html'),
654 common_xml_safe_str($this->content)
658 if (!empty($this->link)) {
662 'rel' => 'alternate',
663 'type' => 'text/html',
664 'href' => $this->link
670 if (!empty($this->selfLink)) {
675 'type' => 'application/atom+xml',
676 'href' => $this->selfLink
682 if(!empty($this->owner)) {
683 $owner = $this->owner->asActivityNoun(self::AUTHOR);
687 if ($this->type == ActivityObject::PERSON
688 || $this->type == ActivityObject::GROUP) {
690 foreach ($this->avatarLinks as $alink) {
694 'type' => $alink->type,
695 'media:width' => $alink->width,
696 'media:height' => $alink->height,
697 'href' => $alink->url,
703 if (!empty($this->geopoint)) {
711 if (!empty($this->poco)) {
712 $this->poco->outputTo($xo);
715 // @fixme there's no way here to make a tree; elements can only contain plaintext
716 // @fixme these may collide with JSON extensions
717 foreach ($this->extra as $el) {
718 list($extraTag, $attrs, $content) = array_pad($el, 3, null);
719 $xo->element($extraTag, $attrs, $content);
722 Event::handle('EndActivityObjectOutputAtom', array($this, $xo));
726 $xo->elementEnd($tag);
732 function asString($tag='activity:object')
734 $xs = new XMLStringer(true);
736 $this->outputTo($xs, $tag);
738 return $xs->getString();
742 * Returns an array based on this Activity Object suitable for
745 * @return array $object the activity object array
752 if (Event::handle('StartActivityObjectOutputJson', array($this, &$object))) {
753 // XXX: attachments are added by Activity
755 // author (Add object for author? Could be useful for repeats.)
757 // content (Add rendered version of the notice?)
759 // downstreamDuplicates
764 $object['id'] = $this->id;
765 } else if ($this->link) {
766 $object['id'] = $this->link;
769 if ($this->type == ActivityObject::PERSON
770 || $this->type == ActivityObject::GROUP) {
773 $object['displayName'] = $this->title;
775 // XXX: Not sure what the best avatar is to use for the
776 // author's "image". For now, I'm using the large size.
779 $avatarMediaLinks = array();
781 foreach ($this->avatarLinks as $a) {
783 // Make a MediaLink for every other Avatar
784 $avatar = new ActivityStreamsMediaLink(
792 // Find the big avatar to use as the "image"
793 if ($a->height == AVATAR_PROFILE_SIZE) {
797 $avatarMediaLinks[] = $avatar->asArray();
800 if (!array_key_exists('status_net', $object)) {
801 $object['status_net'] = array();
804 $object['status_net']['avatarLinks'] = $avatarMediaLinks; // extension
807 if (!empty($imgLink)) {
808 $object['image'] = $imgLink->asArray();
814 // We can probably use the whole schema URL here but probably the
815 // relative simple name is easier to parse
817 $object['objectType'] = self::canonicalType($this->type);
820 $object['summary'] = $this->summary;
822 // content, usually rendered HTML
823 $object['content'] = $this->content;
825 // published (probably don't need. Might be useful for repeats.)
827 // updated (probably don't need this)
829 // TODO: upstreamDuplicates
832 $object['url'] = $this->link;
836 // @fixme these may collide with XML extensions
837 // @fixme multiple tags of same name will overwrite each other
838 // @fixme text content from XML extensions will be lost
840 foreach ($this->extra as $e) {
841 list($objectName, $props, $txt) = array_pad($e, 3, null);
842 if (!empty($objectName)) {
843 $parts = explode(":", $objectName);
844 if (count($parts) == 2 && $parts[0] == "statusnet") {
845 if (!array_key_exists('status_net', $object)) {
846 $object['status_net'] = array();
848 $object['status_net'][$parts[1]] = $props;
850 $object[$objectName] = $props;
855 if (!empty($this->geopoint)) {
857 list($lat, $lon) = explode(' ', $this->geopoint);
859 if (!empty($lat) && !empty($lon)) {
860 $object['location'] = array(
861 'objectType' => 'place',
862 'position' => sprintf("%+02.5F%+03.5F/", $lat, $lon),
867 $loc = Location::fromLatLon((float)$lat, (float)$lon);
870 $name = $loc->getName();
873 $object['location']['displayName'] = $name;
875 $url = $loc->getURL();
878 $object['location']['url'] = $url;
884 if (!empty($this->poco)) {
885 $object['portablecontacts_net'] = array_filter($this->poco->asArray());
888 if (!empty($this->thumbnail)) {
889 if (is_string($this->thumbnail)) {
890 $object['image'] = array('url' => $this->thumbnail);
892 $object['image'] = array('url' => $this->thumbnail->getUrl());
893 if ($this->thumbnail->width) {
894 $object['image']['width'] = $this->thumbnail->width;
896 if ($this->thumbnail->height) {
897 $object['image']['height'] = $this->thumbnail->height;
902 switch (self::canonicalType($this->type)) {
904 if (!empty($this->largerImage)) {
905 $object['fullImage'] = array('url' => $this->largerImage);
910 if (!empty($this->stream)) {
911 $object['stream'] = array('url' => $this->stream);
916 Event::handle('EndActivityObjectOutputJson', array($this, &$object));
918 return array_filter($object);
921 public function getIdentifiers() {
923 foreach(array('id', 'link', 'url') as $id) {
924 if (isset($this->$id)) {
928 return array_unique($ids);
931 static function canonicalType($type) {
932 return ActivityUtils::resolveUri($type, true);
935 static function mimeTypeToObjectType($mimeType) {
940 if (empty($mimeType)) {
944 $parts = explode('/', $mimeType);