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')) {
39 const VALUE = 'value';
40 const PRIMARY = 'primary';
46 function __construct($type, $value, $primary = false)
49 $this->value = $value;
50 $this->primary = $primary;
55 $xs = new XMLStringer(true);
56 $xs->elementStart('poco:urls');
57 $xs->element('poco:type', null, $this->type);
58 $xs->element('poco:value', null, $this->value);
59 if (!empty($this->primary)) {
60 $xs->element('poco:primary', null, 'true');
62 $xs->elementEnd('poco:urls');
63 return $xs->getString();
69 const ADDRESS = 'address';
70 const FORMATTED = 'formatted';
74 // @todo Other address fields
78 if (!empty($this->formatted)) {
79 $xs = new XMLStringer(true);
80 $xs->elementStart('poco:address');
81 $xs->element('poco:formatted', null, common_xml_safe_str($this->formatted));
82 $xs->elementEnd('poco:address');
83 return $xs->getString();
92 const NS = 'http://portablecontacts.net/spec/1.0';
94 const USERNAME = 'preferredUsername';
95 const DISPLAYNAME = 'displayName';
98 public $preferredUsername;
102 public $urls = array();
104 function __construct($element = null)
106 if (empty($element)) {
110 $this->preferredUsername = ActivityUtils::childContent(
116 $this->displayName = ActivityUtils::childContent(
122 $this->note = ActivityUtils::childContent(
128 $this->address = $this->_getAddress($element);
129 $this->urls = $this->_getURLs($element);
132 private function _getURLs($element)
134 $urlEls = $element->getElementsByTagnameNS(self::NS, PoCoURL::URLS);
137 foreach ($urlEls as $urlEl) {
139 $type = ActivityUtils::childContent(
145 $value = ActivityUtils::childContent(
151 $primary = ActivityUtils::childContent(
159 if (isset($primary) && $primary == 'true') {
163 // @todo check to make sure a primary hasn't already been added
165 array_push($urls, new PoCoURL($type, $value, $isPrimary));
170 private function _getAddress($element)
172 $addressEl = ActivityUtils::child(
174 PoCoAddress::ADDRESS,
178 if (!empty($addressEl)) {
179 $formatted = ActivityUtils::childContent(
181 PoCoAddress::FORMATTED,
185 if (!empty($formatted)) {
186 $address = new PoCoAddress();
187 $address->formatted = $formatted;
195 function fromProfile($profile)
197 if (empty($profile)) {
203 $poco->preferredUsername = $profile->nickname;
204 $poco->displayName = $profile->getBestName();
206 $poco->note = $profile->bio;
208 $paddy = new PoCoAddress();
209 $paddy->formatted = $profile->location;
210 $poco->address = $paddy;
212 if (!empty($profile->homepage)) {
226 function fromGroup($group)
234 $poco->preferredUsername = $group->nickname;
235 $poco->displayName = $group->getBestName();
237 $poco->note = $group->description;
239 $paddy = new PoCoAddress();
240 $paddy->formatted = $group->location;
241 $poco->address = $paddy;
243 if (!empty($group->homepage)) {
257 function getPrimaryURL()
259 foreach ($this->urls as $url) {
268 $xs = new XMLStringer(true);
270 'poco:preferredUsername',
272 $this->preferredUsername
281 if (!empty($this->note)) {
282 $xs->element('poco:note', null, common_xml_safe_str($this->note));
285 if (!empty($this->address)) {
286 $xs->raw($this->address->asString());
289 foreach ($this->urls as $url) {
290 $xs->raw($url->asString());
293 return $xs->getString();
298 * Utilities for turning DOMish things into Activityish things
300 * Some common functions that I didn't have the bandwidth to try to factor
301 * into some kind of reasonable superclass, so just dumped here. Might
302 * be useful to have an ActivityObject parent class or something.
306 * @author Evan Prodromou <evan@status.net>
307 * @copyright 2010 StatusNet, Inc.
308 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
309 * @link http://status.net/
314 const ATOM = 'http://www.w3.org/2005/Atom';
321 const CONTENT = 'content';
325 * Get the permalink for an Activity object
327 * @param DOMElement $element A DOM element
329 * @return string related link, if any
332 static function getPermalink($element)
334 return self::getLink($element, 'alternate', 'text/html');
338 * Get the permalink for an Activity object
340 * @param DOMElement $element A DOM element
342 * @return string related link, if any
345 static function getLink(DOMNode $element, $rel, $type=null)
347 $els = $element->childNodes;
349 foreach ($els as $link) {
351 if (!($link instanceof DOMElement)) {
355 if ($link->localName == self::LINK && $link->namespaceURI == self::ATOM) {
357 $linkRel = $link->getAttribute(self::REL);
358 $linkType = $link->getAttribute(self::TYPE);
360 if ($linkRel == $rel &&
361 (is_null($type) || $linkType == $type)) {
362 return $link->getAttribute(self::HREF);
370 static function getLinks(DOMNode $element, $rel, $type=null)
372 $els = $element->childNodes;
375 foreach ($els as $link) {
376 if ($link->localName == self::LINK && $link->namespaceURI == self::ATOM) {
378 $linkRel = $link->getAttribute(self::REL);
379 $linkType = $link->getAttribute(self::TYPE);
381 if ($linkRel == $rel &&
382 (is_null($type) || $linkType == $type)) {
392 * Gets the first child element with the given tag
394 * @param DOMElement $element element to pick at
395 * @param string $tag tag to look for
396 * @param string $namespace Namespace to look under
398 * @return DOMElement found element or null
401 static function child(DOMNode $element, $tag, $namespace=self::ATOM)
403 $els = $element->childNodes;
404 if (empty($els) || $els->length == 0) {
407 for ($i = 0; $i < $els->length; $i++) {
408 $el = $els->item($i);
409 if ($el->localName == $tag && $el->namespaceURI == $namespace) {
417 * Grab the text content of a DOM element child of the current element
419 * @param DOMElement $element Element whose children we examine
420 * @param string $tag Tag to look up
421 * @param string $namespace Namespace to use, defaults to Atom
423 * @return string content of the child
426 static function childContent(DOMNode $element, $tag, $namespace=self::ATOM)
428 $el = self::child($element, $tag, $namespace);
433 return $el->textContent;
438 * Get the content of an atom:entry-like object
440 * @param DOMElement $element The element to examine.
442 * @return string unencoded HTML content of the element, like "This -< is <b>HTML</b>."
444 * @todo handle remote content
445 * @todo handle embedded XML mime types
446 * @todo handle base64-encoded non-XML and non-text mime types
449 static function getContent($element)
451 $contentEl = ActivityUtils::child($element, self::CONTENT);
453 if (!empty($contentEl)) {
455 $src = $contentEl->getAttribute(self::SRC);
458 throw new ClientException(_("Can't handle remote content yet."));
461 $type = $contentEl->getAttribute(self::TYPE);
463 // slavishly following http://atompub.org/rfc4287.html#rfc.section.4.1.3.3
465 if (empty($type) || $type == 'text') {
466 return $contentEl->textContent;
467 } else if ($type == 'html') {
468 $text = $contentEl->textContent;
469 return htmlspecialchars_decode($text, ENT_QUOTES);
470 } else if ($type == 'xhtml') {
471 $divEl = ActivityUtils::child($contentEl, 'div', 'http://www.w3.org/1999/xhtml');
475 $doc = $divEl->ownerDocument;
477 $children = $divEl->childNodes;
479 for ($i = 0; $i < $children->length; $i++) {
480 $child = $children->item($i);
481 $text .= $doc->saveXML($child);
484 } else if (in_array($type, array('text/xml', 'application/xml')) ||
485 preg_match('#(+|/)xml$#', $type)) {
486 throw new ClientException(_("Can't handle embedded XML content yet."));
487 } else if (strncasecmp($type, 'text/', 5)) {
488 return $contentEl->textContent;
490 throw new ClientException(_("Can't handle embedded Base64 content yet."));
496 // XXX: Arg! This wouldn't be necessary if we used Avatars conistently
505 function __construct($element=null)
508 // @fixme use correct namespaces
509 $this->url = $element->getAttribute('href');
510 $this->type = $element->getAttribute('type');
511 $width = $element->getAttribute('media:width');
512 if ($width != null) {
513 $this->width = intval($width);
515 $height = $element->getAttribute('media:height');
516 if ($height != null) {
517 $this->height = intval($height);
522 static function fromAvatar($avatar)
524 if (empty($avatar)) {
527 $alink = new AvatarLink();
528 $alink->type = $avatar->mediatype;
529 $alink->height = $avatar->height;
530 $alink->width = $avatar->width;
531 $alink->url = $avatar->displayUrl();
535 static function fromFilename($filename, $size)
537 $alink = new AvatarLink();
538 $alink->url = $filename;
539 $alink->height = $size;
540 if (!empty($filename)) {
541 $alink->width = $size;
542 $alink->type = self::mediatype($filename);
544 $alink->url = User_group::defaultLogo($size);
545 $alink->type = 'image/png';
551 static function mediatype($filename) {
552 $ext = strtolower(end(explode('.', $filename)));
553 if ($ext == 'jpeg') {
556 // hope we don't support any others
557 $types = array('png', 'gif', 'jpg', 'jpeg');
558 if (in_array($ext, $types)) {
559 return 'image/' . $ext;
566 * A noun-ish thing in the activity universe
568 * The activity streams spec talks about activity objects, while also having
569 * a tag activity:object, which is in fact an activity object. Aaaaaah!
571 * This is just a thing in the activity universe. Can be the subject, object,
572 * or indirect object (target!) of an activity verb. Rotten name, and I'm
573 * propagating it. *sigh*
577 * @author Evan Prodromou <evan@status.net>
578 * @copyright 2010 StatusNet, Inc.
579 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
580 * @link http://status.net/
585 const ARTICLE = 'http://activitystrea.ms/schema/1.0/article';
586 const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
587 const NOTE = 'http://activitystrea.ms/schema/1.0/note';
588 const STATUS = 'http://activitystrea.ms/schema/1.0/status';
589 const FILE = 'http://activitystrea.ms/schema/1.0/file';
590 const PHOTO = 'http://activitystrea.ms/schema/1.0/photo';
591 const ALBUM = 'http://activitystrea.ms/schema/1.0/photo-album';
592 const PLAYLIST = 'http://activitystrea.ms/schema/1.0/playlist';
593 const VIDEO = 'http://activitystrea.ms/schema/1.0/video';
594 const AUDIO = 'http://activitystrea.ms/schema/1.0/audio';
595 const BOOKMARK = 'http://activitystrea.ms/schema/1.0/bookmark';
596 const PERSON = 'http://activitystrea.ms/schema/1.0/person';
597 const GROUP = 'http://activitystrea.ms/schema/1.0/group';
598 const PLACE = 'http://activitystrea.ms/schema/1.0/place';
599 const COMMENT = 'http://activitystrea.ms/schema/1.0/comment';
602 // Atom elements we snarf
604 const TITLE = 'title';
605 const SUMMARY = 'summary';
607 const SOURCE = 'source';
611 const EMAIL = 'email';
621 public $avatarLinks = array();
629 * This probably needs to be refactored
630 * to generate a local class (ActivityPerson, ActivityFile, ...)
631 * based on the object type.
633 * @param DOMElement $element DOM thing to turn into an Activity thing
636 function __construct($element = null)
638 if (empty($element)) {
642 $this->element = $element;
644 $this->geopoint = $this->_childContent(
646 ActivityContext::POINT,
647 ActivityContext::GEORSS
650 if ($element->tagName == 'author') {
651 $this->_fromAuthor($element);
652 } else if ($element->tagName == 'item') {
653 $this->_fromRssItem($element);
655 $this->_fromAtomEntry($element);
658 // Some per-type attributes...
659 if ($this->type == self::PERSON || $this->type == self::GROUP) {
660 $this->displayName = $this->title;
662 $photos = ActivityUtils::getLinks($element, 'photo');
663 if (count($photos)) {
664 foreach ($photos as $link) {
665 $this->avatarLinks[] = new AvatarLink($link);
668 $avatars = ActivityUtils::getLinks($element, 'avatar');
669 foreach ($avatars as $link) {
670 $this->avatarLinks[] = new AvatarLink($link);
674 $this->poco = new PoCo($element);
678 private function _fromAuthor($element)
680 $this->type = self::PERSON; // XXX: is this fair?
681 $this->title = $this->_childContent($element, self::NAME);
682 $this->id = $this->_childContent($element, self::URI);
684 if (empty($this->id)) {
685 $email = $this->_childContent($element, self::EMAIL);
686 if (!empty($email)) {
688 $this->id = 'mailto:'.$email;
693 private function _fromAtomEntry($element)
695 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
698 if (empty($this->type)) {
699 $this->type = ActivityObject::NOTE;
702 $this->id = $this->_childContent($element, self::ID);
703 $this->title = $this->_childContent($element, self::TITLE);
704 $this->summary = $this->_childContent($element, self::SUMMARY);
706 $this->source = $this->_getSource($element);
708 $this->content = ActivityUtils::getContent($element);
710 $this->link = ActivityUtils::getPermalink($element);
713 // @fixme rationalize with Activity::_fromRssItem()
715 private function _fromRssItem($item)
717 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
719 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
721 if (!empty($contentEl)) {
722 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
724 $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
725 if (!empty($descriptionEl)) {
726 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
730 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
732 $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
734 if (!empty($guidEl)) {
735 $this->id = $guidEl->textContent;
737 if ($guidEl->hasAttribute('isPermaLink')) {
739 $this->link = $this->id;
744 public static function fromRssAuthor($el)
746 $text = $el->textContent;
748 if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
751 } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
754 } else if (preg_match('/.*@.*/', $text)) {
762 // Not really enough info
764 $obj = new ActivityObject();
768 $obj->type = ActivityObject::PERSON;
771 if (!empty($email)) {
772 $obj->id = 'mailto:'.$email;
778 public static function fromDcCreator($el)
780 // Not really enough info
782 $text = $el->textContent;
784 $obj = new ActivityObject();
789 $obj->type = ActivityObject::PERSON;
794 public static function fromRssChannel($el)
796 $obj = new ActivityObject();
800 $obj->type = ActivityObject::PERSON; // @fixme guess better
802 $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, self::RSS);
803 $obj->link = ActivityUtils::childContent($el, ActivityUtils::LINK, self::RSS);
804 $obj->id = ActivityUtils::getLink($el, self::SELF);
806 $desc = ActivityUtils::childContent($el, self::DESCRIPTION, self::RSS);
809 $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
812 $imageEl = ActivityUtils::child($el, self::IMAGE, self::RSS);
814 if (!empty($imageEl)) {
815 $obj->avatarLinks[] = ActivityUtils::childContent($imageEl, self::URL, self::RSS);
821 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
823 return ActivityUtils::childContent($element, $tag, $namespace);
826 // Try to get a unique id for the source feed
828 private function _getSource($element)
830 $sourceEl = ActivityUtils::child($element, 'source');
832 if (empty($sourceEl)) {
835 $href = ActivityUtils::getLink($sourceEl, 'self');
839 return ActivityUtils::childContent($sourceEl, 'id');
844 static function fromNotice(Notice $notice)
846 $object = new ActivityObject();
848 $object->type = ActivityObject::NOTE;
850 $object->id = $notice->uri;
851 $object->title = $notice->content;
852 $object->content = $notice->rendered;
853 $object->link = $notice->bestUrl();
858 static function fromProfile(Profile $profile)
860 $object = new ActivityObject();
862 $object->type = ActivityObject::PERSON;
863 $object->id = $profile->getUri();
864 $object->title = $profile->getBestName();
865 $object->link = $profile->profileurl;
867 $orig = $profile->getOriginalAvatar();
870 $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
879 foreach ($sizes as $size) {
882 $avatar = $profile->getAvatar($size);
884 if (!empty($avatar)) {
885 $alink = AvatarLink::fromAvatar($avatar);
887 $alink = new AvatarLink();
888 $alink->type = 'image/png';
889 $alink->height = $size;
890 $alink->width = $size;
891 $alink->url = Avatar::defaultImage($size);
894 $object->avatarLinks[] = $alink;
897 if (isset($profile->lat) && isset($profile->lon)) {
898 $object->geopoint = (float)$profile->lat
899 . ' ' . (float)$profile->lon;
902 $object->poco = PoCo::fromProfile($profile);
907 static function fromGroup($group)
909 $object = new ActivityObject();
911 $object->type = ActivityObject::GROUP;
912 $object->id = $group->getUri();
913 $object->title = $group->getBestName();
914 $object->link = $group->getUri();
916 $object->avatarLinks[] = AvatarLink::fromFilename(
917 $group->homepage_logo,
921 $object->avatarLinks[] = AvatarLink::fromFilename(
926 $object->avatarLinks[] = AvatarLink::fromFilename(
931 $object->poco = PoCo::fromGroup($group);
936 function asString($tag='activity:object')
938 $xs = new XMLStringer(true);
940 $xs->elementStart($tag);
942 $xs->element('activity:object-type', null, $this->type);
944 $xs->element(self::ID, null, $this->id);
946 if (!empty($this->title)) {
950 common_xml_safe_str($this->title)
954 if (!empty($this->summary)) {
958 common_xml_safe_str($this->summary)
962 if (!empty($this->content)) {
963 // XXX: assuming HTML content here
965 ActivityUtils::CONTENT,
966 array('type' => 'html'),
967 common_xml_safe_str($this->content)
971 if (!empty($this->link)) {
975 'rel' => 'alternate',
976 'type' => 'text/html',
977 'href' => $this->link
983 if ($this->type == ActivityObject::PERSON
984 || $this->type == ActivityObject::GROUP) {
986 foreach ($this->avatarLinks as $avatar) {
990 'type' => $avatar->type,
991 'media:width' => $avatar->width,
992 'media:height' => $avatar->height,
993 'href' => $avatar->url
1000 if (!empty($this->geopoint)) {
1008 if (!empty($this->poco)) {
1009 $xs->raw($this->poco->asString());
1012 $xs->elementEnd($tag);
1014 return $xs->getString();
1019 * Utility class to hold a bunch of constant defining default verb types
1022 * @package StatusNet
1023 * @author Evan Prodromou <evan@status.net>
1024 * @copyright 2010 StatusNet, Inc.
1025 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
1026 * @link http://status.net/
1031 const POST = 'http://activitystrea.ms/schema/1.0/post';
1032 const SHARE = 'http://activitystrea.ms/schema/1.0/share';
1033 const SAVE = 'http://activitystrea.ms/schema/1.0/save';
1034 const FAVORITE = 'http://activitystrea.ms/schema/1.0/favorite';
1035 const PLAY = 'http://activitystrea.ms/schema/1.0/play';
1036 const FOLLOW = 'http://activitystrea.ms/schema/1.0/follow';
1037 const FRIEND = 'http://activitystrea.ms/schema/1.0/make-friend';
1038 const JOIN = 'http://activitystrea.ms/schema/1.0/join';
1039 const TAG = 'http://activitystrea.ms/schema/1.0/tag';
1041 // Custom OStatus verbs for the flipside until they're standardized
1042 const DELETE = 'http://ostatus.org/schema/1.0/unfollow';
1043 const UNFAVORITE = 'http://ostatus.org/schema/1.0/unfavorite';
1044 const UNFOLLOW = 'http://ostatus.org/schema/1.0/unfollow';
1045 const LEAVE = 'http://ostatus.org/schema/1.0/leave';
1047 // For simple profile-update pings; no content to share.
1048 const UPDATE_PROFILE = 'http://ostatus.org/schema/1.0/update-profile';
1051 class ActivityContext
1056 public $attention = array();
1057 public $conversation;
1059 const THR = 'http://purl.org/syndication/thread/1.0';
1060 const GEORSS = 'http://www.georss.org/georss';
1061 const OSTATUS = 'http://ostatus.org/schema/1.0';
1063 const INREPLYTO = 'in-reply-to';
1065 const HREF = 'href';
1067 const POINT = 'point';
1069 const ATTENTION = 'ostatus:attention';
1070 const CONVERSATION = 'ostatus:conversation';
1072 function __construct($element)
1074 $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
1076 if (!empty($replyToEl)) {
1077 $this->replyToID = $replyToEl->getAttribute(self::REF);
1078 $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
1081 $this->location = $this->getLocation($element);
1083 $this->conversation = ActivityUtils::getLink($element, self::CONVERSATION);
1085 // Multiple attention links allowed
1087 $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
1089 for ($i = 0; $i < $links->length; $i++) {
1091 $link = $links->item($i);
1093 $linkRel = $link->getAttribute(ActivityUtils::REL);
1095 if ($linkRel == self::ATTENTION) {
1096 $this->attention[] = $link->getAttribute(self::HREF);
1102 * Parse location given as a GeoRSS-simple point, if provided.
1103 * http://www.georss.org/simple
1105 * @param feed item $entry
1106 * @return mixed Location or false
1108 function getLocation($dom)
1110 $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
1112 for ($i = 0; $i < $points->length; $i++) {
1113 $point = $points->item($i)->textContent;
1114 return self::locationFromPoint($point);
1120 // XXX: Move to ActivityUtils or Location?
1121 static function locationFromPoint($point)
1123 $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
1124 $point = preg_replace('/\s+/', ' ', $point);
1125 $point = trim($point);
1126 $coords = explode(' ', $point);
1127 if (count($coords) == 2) {
1128 list($lat, $lon) = $coords;
1129 if (is_numeric($lat) && is_numeric($lon)) {
1130 common_log(LOG_INFO, "Looking up location for $lat $lon from georss point");
1131 return Location::fromLatLon($lat, $lon);
1134 common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
1140 * An activity in the ActivityStrea.ms world
1142 * An activity is kind of like a sentence: someone did something
1143 * to something else.
1145 * 'someone' is the 'actor'; 'did something' is the verb;
1146 * 'something else' is the object.
1149 * @package StatusNet
1150 * @author Evan Prodromou <evan@status.net>
1151 * @copyright 2010 StatusNet, Inc.
1152 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
1153 * @link http://status.net/
1158 const SPEC = 'http://activitystrea.ms/spec/1.0/';
1159 const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
1161 const VERB = 'verb';
1162 const OBJECT = 'object';
1163 const ACTOR = 'actor';
1164 const SUBJECT = 'subject';
1165 const OBJECTTYPE = 'object-type';
1166 const CONTEXT = 'context';
1167 const TARGET = 'target';
1169 const ATOM = 'http://www.w3.org/2005/Atom';
1171 const AUTHOR = 'author';
1172 const PUBLISHED = 'published';
1173 const UPDATED = 'updated';
1175 const RSS = null; // no namespace!
1177 const PUBDATE = 'pubDate';
1178 const DESCRIPTION = 'description';
1179 const GUID = 'guid';
1180 const SELF = 'self';
1181 const IMAGE = 'image';
1184 const DC = 'http://purl.org/dc/elements/1.1/';
1186 const CREATOR = 'creator';
1188 const CONTENTNS = 'http://purl.org/rss/1.0/modules/content/';
1190 public $actor; // an ActivityObject
1191 public $verb; // a string (the URL)
1192 public $object; // an ActivityObject
1193 public $target; // an ActivityObject
1194 public $context; // an ActivityObject
1195 public $time; // Time of the activity
1196 public $link; // an ActivityObject
1197 public $entry; // the source entry
1198 public $feed; // the source feed
1200 public $summary; // summary of activity
1201 public $content; // HTML content of activity
1202 public $id; // ID of the activity
1203 public $title; // title of the activity
1204 public $categories = array(); // list of AtomCategory objects
1205 public $enclosures = array(); // list of enclosure URL references
1208 * Turns a regular old Atom <entry> into a magical activity
1210 * @param DOMElement $entry Atom entry to poke at
1211 * @param DOMElement $feed Atom feed, for context
1214 function __construct($entry = null, $feed = null)
1216 if (is_null($entry)) {
1220 // Insist on a feed's root DOMElement; don't allow a DOMDocument
1221 if ($feed instanceof DOMDocument) {
1222 throw new ClientException(
1223 _("Expecting a root feed element but got a whole XML document.")
1227 $this->entry = $entry;
1228 $this->feed = $feed;
1230 if ($entry->namespaceURI == Activity::ATOM &&
1231 $entry->localName == 'entry') {
1232 $this->_fromAtomEntry($entry, $feed);
1233 } else if ($entry->namespaceURI == Activity::RSS &&
1234 $entry->localName == 'item') {
1235 $this->_fromRssItem($entry, $feed);
1237 throw new Exception("Unknown DOM element: {$entry->namespaceURI} {$entry->localName}");
1241 function _fromAtomEntry($entry, $feed)
1243 $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
1245 if (!empty($pubEl)) {
1246 $this->time = strtotime($pubEl->textContent);
1248 // XXX technically an error; being liberal. Good idea...?
1249 $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
1250 if (!empty($updateEl)) {
1251 $this->time = strtotime($updateEl->textContent);
1257 $this->link = ActivityUtils::getPermalink($entry);
1259 $verbEl = $this->_child($entry, self::VERB);
1261 if (!empty($verbEl)) {
1262 $this->verb = trim($verbEl->textContent);
1264 $this->verb = ActivityVerb::POST;
1265 // XXX: do other implied stuff here
1268 $objectEl = $this->_child($entry, self::OBJECT);
1270 if (!empty($objectEl)) {
1271 $this->object = new ActivityObject($objectEl);
1273 $this->object = new ActivityObject($entry);
1276 $actorEl = $this->_child($entry, self::ACTOR);
1278 if (!empty($actorEl)) {
1280 $this->actor = new ActivityObject($actorEl);
1282 } else if (!empty($feed) &&
1283 $subjectEl = $this->_child($feed, self::SUBJECT)) {
1285 $this->actor = new ActivityObject($subjectEl);
1287 } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
1289 $this->actor = new ActivityObject($authorEl);
1291 } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
1294 $this->actor = new ActivityObject($authorEl);
1297 $contextEl = $this->_child($entry, self::CONTEXT);
1299 if (!empty($contextEl)) {
1300 $this->context = new ActivityContext($contextEl);
1302 $this->context = new ActivityContext($entry);
1305 $targetEl = $this->_child($entry, self::TARGET);
1307 if (!empty($targetEl)) {
1308 $this->target = new ActivityObject($targetEl);
1311 $this->summary = ActivityUtils::childContent($entry, 'summary');
1312 $this->id = ActivityUtils::childContent($entry, 'id');
1313 $this->content = ActivityUtils::getContent($entry);
1315 $catEls = $entry->getElementsByTagNameNS(self::ATOM, 'category');
1317 for ($i = 0; $i < $catEls->length; $i++) {
1318 $catEl = $catEls->item($i);
1319 $this->categories[] = new AtomCategory($catEl);
1323 foreach (ActivityUtils::getLinks($entry, 'enclosure') as $link) {
1324 $this->enclosures[] = $link->getAttribute('href');
1328 function _fromRssItem($item, $rss)
1330 $verbEl = $this->_child($item, self::VERB);
1332 if (!empty($verbEl)) {
1333 $this->verb = trim($verbEl->textContent);
1335 $this->verb = ActivityVerb::POST;
1336 // XXX: do other implied stuff here
1339 $pubDateEl = $this->_child($item, self::PUBDATE, self::RSS);
1341 if (!empty($pubDateEl)) {
1342 $this->time = strtotime($pubDateEl->textContent);
1345 $authorEl = $this->_child($item, self::AUTHOR, self::RSS);
1347 if (!empty($authorEl)) {
1348 $this->actor = ActivityObject::fromRssAuthor($authorEl);
1350 $dcCreatorEl = $this->_child($item, self::CREATOR, self::DC);
1351 if (!empty($dcCreatorEl)) {
1352 $this->actor = ActivityObject::fromDcCreator($dcCreatorEl);
1353 } else if (!empty($rss)) {
1354 $this->actor = ActivityObject::fromRssChannel($rss);
1358 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, self::RSS);
1360 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, self::CONTENTNS);
1362 if (!empty($contentEl)) {
1363 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
1365 $descriptionEl = ActivityUtils::child($item, self::DESCRIPTION, self::RSS);
1366 if (!empty($descriptionEl)) {
1367 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
1371 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, self::RSS);
1373 // @fixme enclosures
1374 // @fixme thumbnails... maybe
1376 $guidEl = ActivityUtils::child($item, self::GUID, self::RSS);
1378 if (!empty($guidEl)) {
1379 $this->id = $guidEl->textContent;
1381 if ($guidEl->hasAttribute('isPermaLink') && $guidEl->getAttribute('isPermaLink') != 'false') {
1382 // overwrites <link>
1383 $this->link = $this->id;
1387 $this->object = new ActivityObject($item);
1388 $this->context = new ActivityContext($item);
1392 * Returns an Atom <entry> based on this activity
1394 * @return DOMElement Atom entry
1397 function toAtomEntry()
1402 function asString($namespace=false)
1404 $xs = new XMLStringer(true);
1407 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
1408 'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
1409 'xmlns:georss' => 'http://www.georss.org/georss',
1410 'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
1411 'xmlns:poco' => 'http://portablecontacts.net/spec/1.0',
1412 'xmlns:media' => 'http://purl.org/syndication/atommedia');
1417 $xs->elementStart('entry', $attrs);
1419 $xs->element('id', null, $this->id);
1420 $xs->element('title', null, $this->title);
1421 $xs->element('published', null, common_date_iso8601($this->time));
1422 $xs->element('content', array('type' => 'html'), $this->content);
1424 if (!empty($this->summary)) {
1425 $xs->element('summary', null, $this->summary);
1428 if (!empty($this->link)) {
1429 $xs->element('link', array('rel' => 'alternate',
1430 'type' => 'text/html'),
1436 $xs->elementStart('author');
1437 $xs->element('uri', array(), $this->actor->id);
1438 if ($this->actor->title) {
1439 $xs->element('name', array(), $this->actor->title);
1441 $xs->elementEnd('author');
1442 $xs->raw($this->actor->asString('activity:actor'));
1444 $xs->element('activity:verb', null, $this->verb);
1446 if ($this->object) {
1447 $xs->raw($this->object->asString());
1450 if ($this->target) {
1451 $xs->raw($this->target->asString('activity:target'));
1454 foreach ($this->categories as $cat) {
1455 $xs->raw($cat->asString());
1458 $xs->elementEnd('entry');
1460 return $xs->getString();
1463 private function _child($element, $tag, $namespace=self::SPEC)
1465 return ActivityUtils::child($element, $tag, $namespace);
1475 function __construct($element=null)
1477 if ($element && $element->attributes) {
1478 $this->term = $this->extract($element, 'term');
1479 $this->scheme = $this->extract($element, 'scheme');
1480 $this->label = $this->extract($element, 'label');
1484 protected function extract($element, $attrib)
1486 $node = $element->attributes->getNamedItemNS(Activity::ATOM, $attrib);
1488 return trim($node->textContent);
1490 $node = $element->attributes->getNamedItem($attrib);
1492 return trim($node->textContent);
1500 if ($this->term !== null) {
1501 $attribs['term'] = $this->term;
1503 if ($this->scheme !== null) {
1504 $attribs['scheme'] = $this->scheme;
1506 if ($this->label !== null) {
1507 $attribs['label'] = $this->label;
1509 $xs = new XMLStringer();
1510 $xs->element('category', $attribs);
1511 return $xs->asString();