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 = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
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
255 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
257 $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
259 $this->source = $this->_getSource($element);
261 $this->link = ActivityUtils::getPermalink($element);
263 $this->id = $this->_childContent($element, self::ID);
265 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
266 $this->id = $this->link;
270 // @todo FIXME: rationalize with Activity::_fromRssItem()
271 private function _fromRssItem($item)
273 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
275 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
277 if (!empty($contentEl)) {
278 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
280 $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
281 if (!empty($descriptionEl)) {
282 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
286 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
288 $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
290 if (!empty($guidEl)) {
291 $this->id = $guidEl->textContent;
293 if ($guidEl->hasAttribute('isPermaLink')) {
295 $this->link = $this->id;
300 public static function fromRssAuthor($el)
302 $text = $el->textContent;
304 if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
307 } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
310 } else if (preg_match('/.*@.*/', $text)) {
318 // Not really enough info
320 $obj = new ActivityObject();
324 $obj->type = ActivityObject::PERSON;
327 if (!empty($email)) {
328 $obj->id = 'mailto:'.$email;
334 public static function fromDcCreator($el)
336 // Not really enough info
338 $text = $el->textContent;
340 $obj = new ActivityObject();
345 $obj->type = ActivityObject::PERSON;
350 public static function fromRssChannel($el)
352 $obj = new ActivityObject();
356 $obj->type = ActivityObject::PERSON; // @fixme guess better
358 $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
359 $obj->link = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
360 $obj->id = ActivityUtils::getLink($el, Activity::SELF);
362 if (empty($obj->id)) {
363 $obj->id = $obj->link;
366 $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
369 $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
372 $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
374 if (!empty($imageEl)) {
375 $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
376 $al = new AvatarLink();
378 $obj->avatarLinks[] = $al;
384 public static function fromPosterousAuthor($el)
386 $obj = new ActivityObject();
388 $obj->type = ActivityObject::PERSON; // @fixme any others...?
390 $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
392 if (!empty($userImage)) {
393 $al = new AvatarLink();
394 $al->url = $userImage;
395 $obj->avatarLinks[] = $al;
398 $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
399 $obj->id = $obj->link;
401 $obj->poco = new PoCo();
403 $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
404 $obj->poco->displayName = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
406 $obj->title = $obj->poco->displayName;
411 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
413 return ActivityUtils::childContent($element, $tag, $namespace);
416 // Try to get a unique id for the source feed
418 private function _getSource($element)
420 $sourceEl = ActivityUtils::child($element, 'source');
422 if (empty($sourceEl)) {
425 $href = ActivityUtils::getLink($sourceEl, 'self');
429 return ActivityUtils::childContent($sourceEl, 'id');
434 static function fromNotice(Notice $notice)
436 $object = new ActivityObject();
438 if (Event::handle('StartActivityObjectFromNotice', array($notice, &$object))) {
440 $object->type = (empty($notice->object_type)) ? ActivityObject::NOTE : $notice->object_type;
442 $object->id = $notice->uri;
443 $object->title = 'New ' . self::canonicalType($object->type) . ' by ';
445 $object->title .= $notice->getProfile()->getAcctUri();
446 } catch (ProfileNoAcctUriException $e) {
447 $object->title .= $e->profile->nickname;
449 $object->content = $notice->rendered;
450 $object->link = $notice->getUrl();
452 $object->extra[] = array('status_net', array('notice_id' => $notice->id));
454 Event::handle('EndActivityObjectFromNotice', array($notice, &$object));
460 static function fromGroup(User_group $group)
462 $object = new ActivityObject();
464 if (Event::handle('StartActivityObjectFromGroup', array($group, &$object))) {
466 $object->type = ActivityObject::GROUP;
467 $object->id = $group->getUri();
468 $object->title = $group->getBestName();
469 $object->link = $group->getUri();
471 $object->avatarLinks[] = AvatarLink::fromFilename($group->homepage_logo,
472 AVATAR_PROFILE_SIZE);
474 $object->avatarLinks[] = AvatarLink::fromFilename($group->stream_logo,
477 $object->avatarLinks[] = AvatarLink::fromFilename($group->mini_logo,
480 $object->poco = PoCo::fromGroup($group);
481 Event::handle('EndActivityObjectFromGroup', array($group, &$object));
487 static function fromPeopletag($ptag)
489 $object = new ActivityObject();
490 if (Event::handle('StartActivityObjectFromPeopletag', array($ptag, &$object))) {
491 $object->type = ActivityObject::_LIST;
493 $object->id = $ptag->getUri();
494 $object->title = $ptag->tag;
495 $object->summary = $ptag->description;
496 $object->link = $ptag->homeUrl();
497 $object->owner = Profile::getKV('id', $ptag->tagger);
498 $object->poco = PoCo::fromProfile($object->owner);
499 Event::handle('EndActivityObjectFromPeopletag', array($ptag, &$object));
504 static function fromFile(File $file)
506 $object = new ActivityObject();
508 if (Event::handle('StartActivityObjectFromFile', array($file, &$object))) {
510 $object->type = self::mimeTypeToObjectType($file->mimetype);
511 $object->id = TagURI::mint(sprintf("file:%d", $file->id));
512 $object->link = common_local_url('attachment', array('attachment' => $file->id));
515 $object->title = $file->title;
519 $object->date = $file->date;
523 $thumbnail = $file->getThumbnail();
524 $object->thumbnail = $thumbnail;
525 } catch (UseFileAsThumbnailException $e) {
526 $object->thumbnail = null;
527 } catch (UnsupportedMediaException $e) {
528 $object->thumbnail = null;
531 switch (self::canonicalType($object->type)) {
533 $object->largerImage = $file->url;
537 $object->stream = $file->url;
541 Event::handle('EndActivityObjectFromFile', array($file, &$object));
547 static function fromNoticeSource(Notice_source $source)
549 $object = new ActivityObject();
550 $wellKnown = array('web', 'xmpp', 'mail', 'omb', 'system', 'api', 'ostatus',
551 'activity', 'feed', 'mirror', 'twitter', 'facebook');
553 if (Event::handle('StartActivityObjectFromNoticeSource', array($source, &$object))) {
554 $object->type = ActivityObject::APPLICATION;
556 if (in_array($source->code, $wellKnown)) {
557 // We use one ID for all well-known StatusNet sources
558 $object->id = "tag:status.net,2009:notice-source:".$source->code;
559 } else if ($source->url) {
560 // They registered with an URL
561 $object->id = $source->url;
563 // Locally-registered, no URL
564 $object->id = TagURI::mint("notice-source:".$source->code);
568 $object->link = $source->url;
572 $object->title = $source->name;
574 $object->title = $source->code;
577 if ($source->created) {
578 $object->date = $source->created;
581 $object->extra[] = array('status_net', array('source_code' => $source->code));
583 Event::handle('EndActivityObjectFromNoticeSource', array($source, &$object));
589 static function fromMessage(Message $message)
591 $object = new ActivityObject();
593 if (Event::handle('StartActivityObjectFromMessage', array($message, &$object))) {
595 $object->type = ActivityObject::NOTE;
596 $object->id = ($message->uri) ? $message->uri : (($message->url) ? $message->url : TagURI::mint(sprintf("message:%d", $message->id)));
597 $object->content = $message->rendered;
598 $object->date = $message->created;
601 $object->link = $message->url;
603 $object->link = common_local_url('showmessage', array('message' => $message->id));
606 $object->extra[] = array('status_net', array('message_id' => $message->id));
608 Event::handle('EndActivityObjectFromMessage', array($message, &$object));
614 function outputTo($xo, $tag='activity:object')
617 $xo->elementStart($tag);
620 if (Event::handle('StartActivityObjectOutputAtom', array($this, $xo))) {
621 $xo->element('activity:object-type', null, $this->type);
625 if ($tag == 'author') {
626 $xo->element(self::URI, null, $this->id);
628 $xo->element(self::ID, null, $this->id);
631 if (!empty($this->title)) {
632 $name = common_xml_safe_str($this->title);
633 if ($tag == 'author') {
634 // XXX: Backward compatibility hack -- atom:name should contain
635 // full name here, instead of nickname, i.e.: $name. Change
636 // this in the next version.
637 $xo->element(self::NAME, null, $this->poco->preferredUsername);
639 $xo->element(self::TITLE, null, $name);
643 if (!empty($this->summary)) {
647 common_xml_safe_str($this->summary)
651 if (!empty($this->content)) {
652 // XXX: assuming HTML content here
654 ActivityUtils::CONTENT,
655 array('type' => 'html'),
656 common_xml_safe_str($this->content)
660 if (!empty($this->link)) {
664 'rel' => 'alternate',
665 'type' => 'text/html',
666 'href' => $this->link
672 if(!empty($this->owner)) {
673 $owner = $this->owner->asActivityNoun(self::AUTHOR);
677 if ($this->type == ActivityObject::PERSON
678 || $this->type == ActivityObject::GROUP) {
680 foreach ($this->avatarLinks as $alink) {
684 'type' => $alink->type,
685 'media:width' => $alink->width,
686 'media:height' => $alink->height,
687 'href' => $alink->url,
693 if (!empty($this->geopoint)) {
701 if (!empty($this->poco)) {
702 $this->poco->outputTo($xo);
705 // @fixme there's no way here to make a tree; elements can only contain plaintext
706 // @fixme these may collide with JSON extensions
707 foreach ($this->extra as $el) {
708 list($extraTag, $attrs, $content) = array_pad($el, 3, null);
709 $xo->element($extraTag, $attrs, $content);
712 Event::handle('EndActivityObjectOutputAtom', array($this, $xo));
716 $xo->elementEnd($tag);
722 function asString($tag='activity:object')
724 $xs = new XMLStringer(true);
726 $this->outputTo($xs, $tag);
728 return $xs->getString();
732 * Returns an array based on this Activity Object suitable for
735 * @return array $object the activity object array
742 if (Event::handle('StartActivityObjectOutputJson', array($this, &$object))) {
743 // XXX: attachments are added by Activity
745 // author (Add object for author? Could be useful for repeats.)
747 // content (Add rendered version of the notice?)
749 // downstreamDuplicates
754 $object['id'] = $this->id;
755 } else if ($this->link) {
756 $object['id'] = $this->link;
759 if ($this->type == ActivityObject::PERSON
760 || $this->type == ActivityObject::GROUP) {
763 $object['displayName'] = $this->title;
765 // XXX: Not sure what the best avatar is to use for the
766 // author's "image". For now, I'm using the large size.
769 $avatarMediaLinks = array();
771 foreach ($this->avatarLinks as $a) {
773 // Make a MediaLink for every other Avatar
774 $avatar = new ActivityStreamsMediaLink(
782 // Find the big avatar to use as the "image"
783 if ($a->height == AVATAR_PROFILE_SIZE) {
787 $avatarMediaLinks[] = $avatar->asArray();
790 if (!array_key_exists('status_net', $object)) {
791 $object['status_net'] = array();
794 $object['status_net']['avatarLinks'] = $avatarMediaLinks; // extension
797 if (!empty($imgLink)) {
798 $object['image'] = $imgLink->asArray();
804 // We can probably use the whole schema URL here but probably the
805 // relative simple name is easier to parse
807 $object['objectType'] = self::canonicalType($this->type);
810 $object['summary'] = $this->summary;
812 // content, usually rendered HTML
813 $object['content'] = $this->content;
815 // published (probably don't need. Might be useful for repeats.)
817 // updated (probably don't need this)
819 // TODO: upstreamDuplicates
822 $object['url'] = $this->link;
826 // @fixme these may collide with XML extensions
827 // @fixme multiple tags of same name will overwrite each other
828 // @fixme text content from XML extensions will be lost
830 foreach ($this->extra as $e) {
831 list($objectName, $props, $txt) = array_pad($e, 3, null);
832 if (!empty($objectName)) {
833 $parts = explode(":", $objectName);
834 if (count($parts) == 2 && $parts[0] == "statusnet") {
835 if (!array_key_exists('status_net', $object)) {
836 $object['status_net'] = array();
838 $object['status_net'][$parts[1]] = $props;
840 $object[$objectName] = $props;
845 if (!empty($this->geopoint)) {
847 list($lat, $lon) = explode(' ', $this->geopoint);
849 if (!empty($lat) && !empty($lon)) {
850 $object['location'] = array(
851 'objectType' => 'place',
852 'position' => sprintf("%+02.5F%+03.5F/", $lat, $lon),
857 $loc = Location::fromLatLon((float)$lat, (float)$lon);
860 $name = $loc->getName();
863 $object['location']['displayName'] = $name;
865 $url = $loc->getURL();
868 $object['location']['url'] = $url;
874 if (!empty($this->poco)) {
875 $object['portablecontacts_net'] = array_filter($this->poco->asArray());
878 if (!empty($this->thumbnail)) {
879 if (is_string($this->thumbnail)) {
880 $object['image'] = array('url' => $this->thumbnail);
882 $object['image'] = array('url' => $this->thumbnail->url);
883 if ($this->thumbnail->width) {
884 $object['image']['width'] = $this->thumbnail->width;
886 if ($this->thumbnail->height) {
887 $object['image']['height'] = $this->thumbnail->height;
892 switch (self::canonicalType($this->type)) {
894 if (!empty($this->largerImage)) {
895 $object['fullImage'] = array('url' => $this->largerImage);
900 if (!empty($this->stream)) {
901 $object['stream'] = array('url' => $this->stream);
906 Event::handle('EndActivityObjectOutputJson', array($this, &$object));
908 return array_filter($object);
911 public function getIdentifiers() {
913 foreach(array('id', 'link', 'url') as $id) {
914 if (isset($this->$id)) {
918 return array_unique($ids);
921 static function canonicalType($type) {
922 return ActivityUtils::resolveUri($type, true);
925 static function mimeTypeToObjectType($mimeType) {
930 if (empty($mimeType)) {
934 $parts = explode('/', $mimeType);