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';
105 public $selfLink; // think APP (Atom Publishing Protocol)
107 public $avatarLinks = array();
112 // @todo move this stuff to it's own PHOTO activity object
113 const MEDIA_DESCRIPTION = 'description';
118 public $extra = array();
125 * This probably needs to be refactored
126 * to generate a local class (ActivityPerson, ActivityFile, ...)
127 * based on the object type.
129 * @param DOMElement $element DOM thing to turn into an Activity thing
131 function __construct($element = null)
133 if (empty($element)) {
137 $this->element = $element;
139 $this->geopoint = $this->_childContent(
141 ActivityContext::POINT,
142 ActivityContext::GEORSS
145 if ($element->tagName == 'author') {
146 $this->_fromAuthor($element);
147 } else if ($element->tagName == 'item') {
148 $this->_fromRssItem($element);
150 $this->_fromAtomEntry($element);
153 // Some per-type attributes...
154 if ($this->type == self::PERSON || $this->type == self::GROUP) {
155 $this->displayName = $this->title;
157 $photos = ActivityUtils::getLinks($element, 'photo');
158 if (count($photos)) {
159 foreach ($photos as $link) {
160 $this->avatarLinks[] = new AvatarLink($link);
163 $avatars = ActivityUtils::getLinks($element, 'avatar');
164 foreach ($avatars as $link) {
165 $this->avatarLinks[] = new AvatarLink($link);
169 $this->poco = new PoCo($element);
172 if ($this->type == self::PHOTO) {
174 $this->thumbnail = ActivityUtils::getLink($element, 'preview');
175 $this->largerImage = ActivityUtils::getLink($element, 'enclosure');
177 $this->description = ActivityUtils::childContent(
179 ActivityObject::MEDIA_DESCRIPTION,
183 if ($this->type == self::_LIST) {
184 $owner = ActivityUtils::child($this->element, Activity::AUTHOR, Activity::SPEC);
185 $this->owner = new ActivityObject($owner);
189 private function _fromAuthor($element)
191 $this->type = $this->_childContent($element,
192 Activity::OBJECTTYPE,
195 if (empty($this->type)) {
196 $this->type = self::PERSON; // XXX: is this fair?
200 // Start with <poco::displayName>
202 $this->title = ActivityUtils::childContent($element, PoCo::DISPLAYNAME, PoCo::NS);
204 // try falling back to <atom:title>
206 if (empty($this->title)) {
207 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
209 if (!empty($title)) {
210 $this->title = common_strip_html($title);
214 // fall back to <atom:name> as a last resort
216 if (empty($this->title)) {
217 $this->title = $this->_childContent($element, self::NAME);
220 // start with <atom:id>
222 $this->id = $this->_childContent($element, self::ID);
224 // fall back to <atom:uri>
226 if (empty($this->id)) {
227 $this->id = $this->_childContent($element, self::URI);
230 // fall further back to <atom:email>
232 if (empty($this->id)) {
233 $email = $this->_childContent($element, self::EMAIL);
234 if (!empty($email)) {
236 $this->id = 'mailto:'.$email;
240 $this->link = ActivityUtils::getPermalink($element);
242 // fall finally back to <link rel=alternate>
244 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
245 $this->id = $this->link;
249 private function _fromAtomEntry($element)
251 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
254 if (empty($this->type)) {
255 $this->type = ActivityObject::NOTE;
258 $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
259 $this->content = ActivityUtils::getContent($element);
261 // We don't like HTML in our titles, although it's technically allowed
262 $this->title = common_strip_html(ActivityUtils::childHtmlContent($element, self::TITLE));
264 $this->source = $this->_getSource($element);
266 $this->link = ActivityUtils::getPermalink($element);
267 $this->selfLink = ActivityUtils::getSelfLink($element);
269 $this->id = $this->_childContent($element, self::ID);
271 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
272 $this->id = $this->link;
276 // @todo FIXME: rationalize with Activity::_fromRssItem()
277 private function _fromRssItem($item)
279 if (empty($this->type)) {
280 $this->type = ActivityObject::NOTE;
283 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
285 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
287 if (!empty($contentEl)) {
288 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
290 $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
291 if (!empty($descriptionEl)) {
292 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
296 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
298 $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
300 if (!empty($guidEl)) {
301 $this->id = $guidEl->textContent;
303 if ($guidEl->hasAttribute('isPermaLink') && $guidEl->getAttribute('isPermaLink') != 'false') {
305 $this->link = $this->id;
310 public static function fromRssAuthor($el)
312 $text = $el->textContent;
314 if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
317 } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
320 } else if (preg_match('/.*@.*/', $text)) {
328 // Not really enough info
330 $obj = new ActivityObject();
334 $obj->type = ActivityObject::PERSON;
337 if (!empty($email)) {
338 $obj->id = 'mailto:'.$email;
344 public static function fromDcCreator($el)
346 // Not really enough info
348 $text = $el->textContent;
350 $obj = new ActivityObject();
355 $obj->type = ActivityObject::PERSON;
360 public static function fromRssChannel($el)
362 $obj = new ActivityObject();
366 $obj->type = ActivityObject::PERSON; // @fixme guess better
368 $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
369 $obj->link = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
370 $obj->id = ActivityUtils::getLink($el, Activity::SELF);
372 if (empty($obj->id)) {
373 $obj->id = $obj->link;
376 $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
379 $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
382 $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
384 if (!empty($imageEl)) {
385 $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
386 $al = new AvatarLink();
388 $obj->avatarLinks[] = $al;
394 public static function fromPosterousAuthor($el)
396 $obj = new ActivityObject();
398 $obj->type = ActivityObject::PERSON; // @fixme any others...?
400 $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
402 if (!empty($userImage)) {
403 $al = new AvatarLink();
404 $al->url = $userImage;
405 $obj->avatarLinks[] = $al;
408 $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
409 $obj->id = $obj->link;
411 $obj->poco = new PoCo();
413 $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
414 $obj->poco->displayName = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
416 $obj->title = $obj->poco->displayName;
421 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
423 return ActivityUtils::childContent($element, $tag, $namespace);
426 // Try to get a unique id for the source feed
428 private function _getSource($element)
430 $sourceEl = ActivityUtils::child($element, 'source');
432 if (empty($sourceEl)) {
435 $href = ActivityUtils::getLink($sourceEl, 'self');
439 return ActivityUtils::childContent($sourceEl, 'id');
444 static function fromGroup(User_group $group)
446 $object = new ActivityObject();
448 if (Event::handle('StartActivityObjectFromGroup', array($group, &$object))) {
450 $object->type = ActivityObject::GROUP;
451 $object->id = $group->getUri();
452 $object->title = $group->getBestName();
453 $object->link = $group->getUri();
455 $object->avatarLinks[] = AvatarLink::fromFilename($group->homepage_logo,
456 AVATAR_PROFILE_SIZE);
458 $object->avatarLinks[] = AvatarLink::fromFilename($group->stream_logo,
461 $object->avatarLinks[] = AvatarLink::fromFilename($group->mini_logo,
464 $object->poco = PoCo::fromGroup($group);
465 Event::handle('EndActivityObjectFromGroup', array($group, &$object));
471 static function fromPeopletag($ptag)
473 $object = new ActivityObject();
474 if (Event::handle('StartActivityObjectFromPeopletag', array($ptag, &$object))) {
475 $object->type = ActivityObject::_LIST;
477 $object->id = $ptag->getUri();
478 $object->title = $ptag->tag;
479 $object->summary = $ptag->description;
480 $object->link = $ptag->homeUrl();
481 $object->owner = Profile::getKV('id', $ptag->tagger);
482 $object->poco = PoCo::fromProfile($object->owner);
483 Event::handle('EndActivityObjectFromPeopletag', array($ptag, &$object));
488 static function fromFile(File $file)
490 $object = new ActivityObject();
492 if (Event::handle('StartActivityObjectFromFile', array($file, &$object))) {
494 $object->type = self::mimeTypeToObjectType($file->mimetype);
495 $object->id = TagURI::mint(sprintf("file:%d", $file->id));
496 $object->link = common_local_url('attachment', array('attachment' => $file->id));
499 $object->title = $file->title;
503 $object->date = $file->date;
507 $thumbnail = $file->getThumbnail();
508 $object->thumbnail = $thumbnail;
509 } catch (UseFileAsThumbnailException $e) {
510 $object->thumbnail = null;
511 } catch (UnsupportedMediaException $e) {
512 $object->thumbnail = null;
515 switch (self::canonicalType($object->type)) {
517 $object->largerImage = $file->getUrl();
521 $object->stream = $file->getUrl();
525 Event::handle('EndActivityObjectFromFile', array($file, &$object));
531 static function fromNoticeSource(Notice_source $source)
533 $object = new ActivityObject();
534 $wellKnown = array('web', 'xmpp', 'mail', 'omb', 'system', 'api', 'ostatus',
535 'activity', 'feed', 'mirror', 'twitter', 'facebook');
537 if (Event::handle('StartActivityObjectFromNoticeSource', array($source, &$object))) {
538 $object->type = ActivityObject::APPLICATION;
540 if (in_array($source->code, $wellKnown)) {
541 // We use one ID for all well-known StatusNet sources
542 $object->id = "tag:status.net,2009:notice-source:".$source->code;
543 } else if ($source->url) {
544 // They registered with an URL
545 $object->id = $source->url;
547 // Locally-registered, no URL
548 $object->id = TagURI::mint("notice-source:".$source->code);
552 $object->link = $source->url;
556 $object->title = $source->name;
558 $object->title = $source->code;
561 if ($source->created) {
562 $object->date = $source->created;
565 $object->extra[] = array('status_net', array('source_code' => $source->code));
567 Event::handle('EndActivityObjectFromNoticeSource', array($source, &$object));
573 static function fromMessage(Message $message)
575 $object = new ActivityObject();
577 if (Event::handle('StartActivityObjectFromMessage', array($message, &$object))) {
579 $object->type = ActivityObject::NOTE;
580 $object->id = ($message->uri) ? $message->uri : (($message->url) ? $message->url : TagURI::mint(sprintf("message:%d", $message->id)));
581 $object->content = $message->rendered;
582 $object->date = $message->created;
585 $object->link = $message->url;
587 $object->link = common_local_url('showmessage', array('message' => $message->id));
590 $object->extra[] = array('status_net', array('message_id' => $message->id));
592 Event::handle('EndActivityObjectFromMessage', array($message, &$object));
598 function outputTo($xo, $tag='activity:object')
601 $xo->elementStart($tag);
604 if (Event::handle('StartActivityObjectOutputAtom', array($this, $xo))) {
605 $xo->element('activity:object-type', null, $this->type);
609 if ($tag == 'author') {
610 $xo->element(self::URI, null, $this->id);
612 $xo->element(self::ID, null, $this->id);
615 if (!empty($this->title)) {
616 $name = common_xml_safe_str($this->title);
617 if ($tag == 'author') {
618 // XXX: Backward compatibility hack -- atom:name should contain
619 // full name here, instead of nickname, i.e.: $name. Change
620 // this in the next version.
621 $xo->element(self::NAME, null, $this->poco->preferredUsername);
623 $xo->element(self::TITLE, null, $name);
627 if (!empty($this->summary)) {
631 common_xml_safe_str($this->summary)
635 if (!empty($this->content)) {
636 // XXX: assuming HTML content here
638 ActivityUtils::CONTENT,
639 array('type' => 'html'),
640 common_xml_safe_str($this->content)
644 if (!empty($this->link)) {
648 'rel' => 'alternate',
649 'type' => 'text/html',
650 'href' => $this->link
656 if (!empty($this->selfLink)) {
661 'type' => 'application/atom+xml',
662 'href' => $this->selfLink
668 if(!empty($this->owner)) {
669 $owner = $this->owner->asActivityNoun(self::AUTHOR);
673 if ($this->type == ActivityObject::PERSON
674 || $this->type == ActivityObject::GROUP) {
676 foreach ($this->avatarLinks as $alink) {
680 'type' => $alink->type,
681 'media:width' => $alink->width,
682 'media:height' => $alink->height,
683 'href' => $alink->url,
689 if (!empty($this->geopoint)) {
697 if (!empty($this->poco)) {
698 $this->poco->outputTo($xo);
701 // @fixme there's no way here to make a tree; elements can only contain plaintext
702 // @fixme these may collide with JSON extensions
703 foreach ($this->extra as $el) {
704 list($extraTag, $attrs, $content) = array_pad($el, 3, null);
705 $xo->element($extraTag, $attrs, $content);
708 Event::handle('EndActivityObjectOutputAtom', array($this, $xo));
712 $xo->elementEnd($tag);
718 function asString($tag='activity:object')
720 $xs = new XMLStringer(true);
722 $this->outputTo($xs, $tag);
724 return $xs->getString();
728 * Returns an array based on this Activity Object suitable for
731 * @return array $object the activity object array
738 if (Event::handle('StartActivityObjectOutputJson', array($this, &$object))) {
739 // XXX: attachments are added by Activity
741 // author (Add object for author? Could be useful for repeats.)
743 // content (Add rendered version of the notice?)
745 // downstreamDuplicates
750 $object['id'] = $this->id;
751 } else if ($this->link) {
752 $object['id'] = $this->link;
755 if ($this->type == ActivityObject::PERSON
756 || $this->type == ActivityObject::GROUP) {
759 $object['displayName'] = $this->title;
761 // XXX: Not sure what the best avatar is to use for the
762 // author's "image". For now, I'm using the large size.
765 $avatarMediaLinks = array();
767 foreach ($this->avatarLinks as $a) {
769 // Make a MediaLink for every other Avatar
770 $avatar = new ActivityStreamsMediaLink(
778 // Find the big avatar to use as the "image"
779 if ($a->height == AVATAR_PROFILE_SIZE) {
783 $avatarMediaLinks[] = $avatar->asArray();
786 if (!array_key_exists('status_net', $object)) {
787 $object['status_net'] = array();
790 $object['status_net']['avatarLinks'] = $avatarMediaLinks; // extension
793 if (!empty($imgLink)) {
794 $object['image'] = $imgLink->asArray();
800 // We can probably use the whole schema URL here but probably the
801 // relative simple name is easier to parse
803 $object['objectType'] = self::canonicalType($this->type);
806 $object['summary'] = $this->summary;
808 // content, usually rendered HTML
809 $object['content'] = $this->content;
811 // published (probably don't need. Might be useful for repeats.)
813 // updated (probably don't need this)
815 // TODO: upstreamDuplicates
818 $object['url'] = $this->link;
822 // @fixme these may collide with XML extensions
823 // @fixme multiple tags of same name will overwrite each other
824 // @fixme text content from XML extensions will be lost
826 foreach ($this->extra as $e) {
827 list($objectName, $props, $txt) = array_pad($e, 3, null);
828 if (!empty($objectName)) {
829 $parts = explode(":", $objectName);
830 if (count($parts) == 2 && $parts[0] == "statusnet") {
831 if (!array_key_exists('status_net', $object)) {
832 $object['status_net'] = array();
834 $object['status_net'][$parts[1]] = $props;
836 $object[$objectName] = $props;
841 if (!empty($this->geopoint)) {
843 list($lat, $lon) = explode(' ', $this->geopoint);
845 if (!empty($lat) && !empty($lon)) {
846 $object['location'] = array(
847 'objectType' => 'place',
848 'position' => sprintf("%+02.5F%+03.5F/", $lat, $lon),
853 $loc = Location::fromLatLon((float)$lat, (float)$lon);
856 $name = $loc->getName();
859 $object['location']['displayName'] = $name;
861 $url = $loc->getURL();
864 $object['location']['url'] = $url;
870 if (!empty($this->poco)) {
871 $object['portablecontacts_net'] = array_filter($this->poco->asArray());
874 if (!empty($this->thumbnail)) {
875 if (is_string($this->thumbnail)) {
876 $object['image'] = array('url' => $this->thumbnail);
878 $object['image'] = array('url' => $this->thumbnail->getUrl());
879 if ($this->thumbnail->width) {
880 $object['image']['width'] = $this->thumbnail->width;
882 if ($this->thumbnail->height) {
883 $object['image']['height'] = $this->thumbnail->height;
888 switch (self::canonicalType($this->type)) {
890 if (!empty($this->largerImage)) {
891 $object['fullImage'] = array('url' => $this->largerImage);
896 if (!empty($this->stream)) {
897 $object['stream'] = array('url' => $this->stream);
902 Event::handle('EndActivityObjectOutputJson', array($this, &$object));
904 return array_filter($object);
907 public function getIdentifiers() {
909 foreach(array('id', 'link', 'url') as $id) {
910 if (isset($this->$id)) {
914 return array_unique($ids);
917 static function canonicalType($type) {
918 return ActivityUtils::resolveUri($type, true);
921 static function mimeTypeToObjectType($mimeType) {
926 if (empty($mimeType)) {
930 $parts = explode('/', $mimeType);