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) {
350 if ($link->localName == self::LINK && $link->namespaceURI == self::ATOM) {
352 $linkRel = $link->getAttribute(self::REL);
353 $linkType = $link->getAttribute(self::TYPE);
355 if ($linkRel == $rel &&
356 (is_null($type) || $linkType == $type)) {
357 return $link->getAttribute(self::HREF);
365 static function getLinks(DOMNode $element, $rel, $type=null)
367 $els = $element->childNodes;
370 foreach ($els as $link) {
371 if ($link->localName == self::LINK && $link->namespaceURI == self::ATOM) {
373 $linkRel = $link->getAttribute(self::REL);
374 $linkType = $link->getAttribute(self::TYPE);
376 if ($linkRel == $rel &&
377 (is_null($type) || $linkType == $type)) {
387 * Gets the first child element with the given tag
389 * @param DOMElement $element element to pick at
390 * @param string $tag tag to look for
391 * @param string $namespace Namespace to look under
393 * @return DOMElement found element or null
396 static function child(DOMNode $element, $tag, $namespace=self::ATOM)
398 $els = $element->childNodes;
399 if (empty($els) || $els->length == 0) {
402 for ($i = 0; $i < $els->length; $i++) {
403 $el = $els->item($i);
404 if ($el->localName == $tag && $el->namespaceURI == $namespace) {
412 * Grab the text content of a DOM element child of the current element
414 * @param DOMElement $element Element whose children we examine
415 * @param string $tag Tag to look up
416 * @param string $namespace Namespace to use, defaults to Atom
418 * @return string content of the child
421 static function childContent(DOMNode $element, $tag, $namespace=self::ATOM)
423 $el = self::child($element, $tag, $namespace);
428 return $el->textContent;
433 * Get the content of an atom:entry-like object
435 * @param DOMElement $element The element to examine.
437 * @return string unencoded HTML content of the element, like "This -< is <b>HTML</b>."
439 * @todo handle remote content
440 * @todo handle embedded XML mime types
441 * @todo handle base64-encoded non-XML and non-text mime types
444 static function getContent($element)
446 $contentEl = ActivityUtils::child($element, self::CONTENT);
448 if (!empty($contentEl)) {
450 $src = $contentEl->getAttribute(self::SRC);
453 throw new ClientException(_("Can't handle remote content yet."));
456 $type = $contentEl->getAttribute(self::TYPE);
458 // slavishly following http://atompub.org/rfc4287.html#rfc.section.4.1.3.3
460 if (empty($type) || $type == 'text') {
461 // Plain text source -- let's turn it into HTML!
462 return htmlspecialchars($contentEl->textContent);
463 } else if ($type == 'html') {
464 // The XML text decoding gives us an HTML string ready to roll.
465 return $contentEl->textContent, ENT_QUOTES;
466 } else if ($type == 'xhtml') {
467 // Embedded XHTML; we have to pull it out of the document tree,
468 // then serialize it back out to an HTML fragment string.
469 $divEl = ActivityUtils::child($contentEl, 'div', 'http://www.w3.org/1999/xhtml');
473 $doc = $divEl->ownerDocument;
475 $children = $divEl->childNodes;
477 for ($i = 0; $i < $children->length; $i++) {
478 $child = $children->item($i);
479 $text .= $doc->saveXML($child);
482 } else if (in_array($type, array('text/xml', 'application/xml')) ||
483 preg_match('#(+|/)xml$#', $type)) {
484 throw new ClientException(_("Can't handle embedded XML content yet."));
485 } else if (strncasecmp($type, 'text/', 5)) {
486 return $contentEl->textContent;
488 throw new ClientException(_("Can't handle embedded Base64 content yet."));
494 // XXX: Arg! This wouldn't be necessary if we used Avatars conistently
503 function __construct($element=null)
506 // @fixme use correct namespaces
507 $this->url = $element->getAttribute('href');
508 $this->type = $element->getAttribute('type');
509 $width = $element->getAttribute('media:width');
510 if ($width != null) {
511 $this->width = intval($width);
513 $height = $element->getAttribute('media:height');
514 if ($height != null) {
515 $this->height = intval($height);
520 static function fromAvatar($avatar)
522 if (empty($avatar)) {
525 $alink = new AvatarLink();
526 $alink->type = $avatar->mediatype;
527 $alink->height = $avatar->height;
528 $alink->width = $avatar->width;
529 $alink->url = $avatar->displayUrl();
533 static function fromFilename($filename, $size)
535 $alink = new AvatarLink();
536 $alink->url = $filename;
537 $alink->height = $size;
538 if (!empty($filename)) {
539 $alink->width = $size;
540 $alink->type = self::mediatype($filename);
542 $alink->url = User_group::defaultLogo($size);
543 $alink->type = 'image/png';
549 static function mediatype($filename) {
550 $ext = strtolower(end(explode('.', $filename)));
551 if ($ext == 'jpeg') {
554 // hope we don't support any others
555 $types = array('png', 'gif', 'jpg', 'jpeg');
556 if (in_array($ext, $types)) {
557 return 'image/' . $ext;
564 * A noun-ish thing in the activity universe
566 * The activity streams spec talks about activity objects, while also having
567 * a tag activity:object, which is in fact an activity object. Aaaaaah!
569 * This is just a thing in the activity universe. Can be the subject, object,
570 * or indirect object (target!) of an activity verb. Rotten name, and I'm
571 * propagating it. *sigh*
575 * @author Evan Prodromou <evan@status.net>
576 * @copyright 2010 StatusNet, Inc.
577 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
578 * @link http://status.net/
583 const ARTICLE = 'http://activitystrea.ms/schema/1.0/article';
584 const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
585 const NOTE = 'http://activitystrea.ms/schema/1.0/note';
586 const STATUS = 'http://activitystrea.ms/schema/1.0/status';
587 const FILE = 'http://activitystrea.ms/schema/1.0/file';
588 const PHOTO = 'http://activitystrea.ms/schema/1.0/photo';
589 const ALBUM = 'http://activitystrea.ms/schema/1.0/photo-album';
590 const PLAYLIST = 'http://activitystrea.ms/schema/1.0/playlist';
591 const VIDEO = 'http://activitystrea.ms/schema/1.0/video';
592 const AUDIO = 'http://activitystrea.ms/schema/1.0/audio';
593 const BOOKMARK = 'http://activitystrea.ms/schema/1.0/bookmark';
594 const PERSON = 'http://activitystrea.ms/schema/1.0/person';
595 const GROUP = 'http://activitystrea.ms/schema/1.0/group';
596 const PLACE = 'http://activitystrea.ms/schema/1.0/place';
597 const COMMENT = 'http://activitystrea.ms/schema/1.0/comment';
600 // Atom elements we snarf
602 const TITLE = 'title';
603 const SUMMARY = 'summary';
605 const SOURCE = 'source';
609 const EMAIL = 'email';
619 public $avatarLinks = array();
627 * This probably needs to be refactored
628 * to generate a local class (ActivityPerson, ActivityFile, ...)
629 * based on the object type.
631 * @param DOMElement $element DOM thing to turn into an Activity thing
634 function __construct($element = null)
636 if (empty($element)) {
640 $this->element = $element;
642 $this->geopoint = $this->_childContent(
644 ActivityContext::POINT,
645 ActivityContext::GEORSS
648 if ($element->tagName == 'author') {
650 $this->type = self::PERSON; // XXX: is this fair?
651 $this->title = $this->_childContent($element, self::NAME);
652 $this->id = $this->_childContent($element, self::URI);
654 if (empty($this->id)) {
655 $email = $this->_childContent($element, self::EMAIL);
656 if (!empty($email)) {
658 $this->id = 'mailto:'.$email;
664 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
667 if (empty($this->type)) {
668 $this->type = ActivityObject::NOTE;
671 $this->id = $this->_childContent($element, self::ID);
672 $this->title = $this->_childContent($element, self::TITLE);
673 $this->summary = $this->_childContent($element, self::SUMMARY);
675 $this->source = $this->_getSource($element);
677 $this->content = ActivityUtils::getContent($element);
679 $this->link = ActivityUtils::getPermalink($element);
683 // Some per-type attributes...
684 if ($this->type == self::PERSON || $this->type == self::GROUP) {
685 $this->displayName = $this->title;
687 $photos = ActivityUtils::getLinks($element, 'photo');
688 if (count($photos)) {
689 foreach ($photos as $link) {
690 $this->avatarLinks[] = new AvatarLink($link);
693 $avatars = ActivityUtils::getLinks($element, 'avatar');
694 foreach ($avatars as $link) {
695 $this->avatarLinks[] = new AvatarLink($link);
699 $this->poco = new PoCo($element);
703 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
705 return ActivityUtils::childContent($element, $tag, $namespace);
708 // Try to get a unique id for the source feed
710 private function _getSource($element)
712 $sourceEl = ActivityUtils::child($element, 'source');
714 if (empty($sourceEl)) {
717 $href = ActivityUtils::getLink($sourceEl, 'self');
721 return ActivityUtils::childContent($sourceEl, 'id');
726 static function fromNotice($notice)
728 $object = new ActivityObject();
730 $object->type = ActivityObject::NOTE;
732 $object->id = $notice->uri;
733 $object->title = $notice->content;
734 $object->content = $notice->rendered;
735 $object->link = $notice->bestUrl();
740 static function fromProfile($profile)
742 $object = new ActivityObject();
744 $object->type = ActivityObject::PERSON;
745 $object->id = $profile->getUri();
746 $object->title = $profile->getBestName();
747 $object->link = $profile->profileurl;
749 $orig = $profile->getOriginalAvatar();
752 $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
761 foreach ($sizes as $size) {
764 $avatar = $profile->getAvatar($size);
766 if (!empty($avatar)) {
767 $alink = AvatarLink::fromAvatar($avatar);
769 $alink = new AvatarLink();
770 $alink->type = 'image/png';
771 $alink->height = $size;
772 $alink->width = $size;
773 $alink->url = Avatar::defaultImage($size);
776 $object->avatarLinks[] = $alink;
779 if (isset($profile->lat) && isset($profile->lon)) {
780 $object->geopoint = (float)$profile->lat
781 . ' ' . (float)$profile->lon;
784 $object->poco = PoCo::fromProfile($profile);
789 static function fromGroup($group)
791 $object = new ActivityObject();
793 $object->type = ActivityObject::GROUP;
794 $object->id = $group->getUri();
795 $object->title = $group->getBestName();
796 $object->link = $group->getUri();
798 $object->avatarLinks[] = AvatarLink::fromFilename(
799 $group->homepage_logo,
803 $object->avatarLinks[] = AvatarLink::fromFilename(
808 $object->avatarLinks[] = AvatarLink::fromFilename(
813 $object->poco = PoCo::fromGroup($group);
818 function asString($tag='activity:object')
820 $xs = new XMLStringer(true);
822 $xs->elementStart($tag);
824 $xs->element('activity:object-type', null, $this->type);
826 $xs->element(self::ID, null, $this->id);
828 if (!empty($this->title)) {
832 common_xml_safe_str($this->title)
836 if (!empty($this->summary)) {
840 common_xml_safe_str($this->summary)
844 if (!empty($this->content)) {
845 // XXX: assuming HTML content here
847 ActivityUtils::CONTENT,
848 array('type' => 'html'),
849 common_xml_safe_str($this->content)
853 if (!empty($this->link)) {
857 'rel' => 'alternate',
858 'type' => 'text/html',
859 'href' => $this->link
865 if ($this->type == ActivityObject::PERSON
866 || $this->type == ActivityObject::GROUP) {
868 foreach ($this->avatarLinks as $avatar) {
872 'type' => $avatar->type,
873 'media:width' => $avatar->width,
874 'media:height' => $avatar->height,
875 'href' => $avatar->url
882 if (!empty($this->geopoint)) {
890 if (!empty($this->poco)) {
891 $xs->raw($this->poco->asString());
894 $xs->elementEnd($tag);
896 return $xs->getString();
901 * Utility class to hold a bunch of constant defining default verb types
905 * @author Evan Prodromou <evan@status.net>
906 * @copyright 2010 StatusNet, Inc.
907 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
908 * @link http://status.net/
913 const POST = 'http://activitystrea.ms/schema/1.0/post';
914 const SHARE = 'http://activitystrea.ms/schema/1.0/share';
915 const SAVE = 'http://activitystrea.ms/schema/1.0/save';
916 const FAVORITE = 'http://activitystrea.ms/schema/1.0/favorite';
917 const PLAY = 'http://activitystrea.ms/schema/1.0/play';
918 const FOLLOW = 'http://activitystrea.ms/schema/1.0/follow';
919 const FRIEND = 'http://activitystrea.ms/schema/1.0/make-friend';
920 const JOIN = 'http://activitystrea.ms/schema/1.0/join';
921 const TAG = 'http://activitystrea.ms/schema/1.0/tag';
923 // Custom OStatus verbs for the flipside until they're standardized
924 const DELETE = 'http://ostatus.org/schema/1.0/unfollow';
925 const UNFAVORITE = 'http://ostatus.org/schema/1.0/unfavorite';
926 const UNFOLLOW = 'http://ostatus.org/schema/1.0/unfollow';
927 const LEAVE = 'http://ostatus.org/schema/1.0/leave';
929 // For simple profile-update pings; no content to share.
930 const UPDATE_PROFILE = 'http://ostatus.org/schema/1.0/update-profile';
933 class ActivityContext
938 public $attention = array();
939 public $conversation;
941 const THR = 'http://purl.org/syndication/thread/1.0';
942 const GEORSS = 'http://www.georss.org/georss';
943 const OSTATUS = 'http://ostatus.org/schema/1.0';
945 const INREPLYTO = 'in-reply-to';
949 const POINT = 'point';
951 const ATTENTION = 'ostatus:attention';
952 const CONVERSATION = 'ostatus:conversation';
954 function __construct($element)
956 $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
958 if (!empty($replyToEl)) {
959 $this->replyToID = $replyToEl->getAttribute(self::REF);
960 $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
963 $this->location = $this->getLocation($element);
965 $this->conversation = ActivityUtils::getLink($element, self::CONVERSATION);
967 // Multiple attention links allowed
969 $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
971 for ($i = 0; $i < $links->length; $i++) {
973 $link = $links->item($i);
975 $linkRel = $link->getAttribute(ActivityUtils::REL);
977 if ($linkRel == self::ATTENTION) {
978 $this->attention[] = $link->getAttribute(self::HREF);
984 * Parse location given as a GeoRSS-simple point, if provided.
985 * http://www.georss.org/simple
987 * @param feed item $entry
988 * @return mixed Location or false
990 function getLocation($dom)
992 $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
994 for ($i = 0; $i < $points->length; $i++) {
995 $point = $points->item($i)->textContent;
996 return self::locationFromPoint($point);
1002 // XXX: Move to ActivityUtils or Location?
1003 static function locationFromPoint($point)
1005 $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
1006 $point = preg_replace('/\s+/', ' ', $point);
1007 $point = trim($point);
1008 $coords = explode(' ', $point);
1009 if (count($coords) == 2) {
1010 list($lat, $lon) = $coords;
1011 if (is_numeric($lat) && is_numeric($lon)) {
1012 common_log(LOG_INFO, "Looking up location for $lat $lon from georss point");
1013 return Location::fromLatLon($lat, $lon);
1016 common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
1022 * An activity in the ActivityStrea.ms world
1024 * An activity is kind of like a sentence: someone did something
1025 * to something else.
1027 * 'someone' is the 'actor'; 'did something' is the verb;
1028 * 'something else' is the object.
1031 * @package StatusNet
1032 * @author Evan Prodromou <evan@status.net>
1033 * @copyright 2010 StatusNet, Inc.
1034 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
1035 * @link http://status.net/
1040 const SPEC = 'http://activitystrea.ms/spec/1.0/';
1041 const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
1043 const VERB = 'verb';
1044 const OBJECT = 'object';
1045 const ACTOR = 'actor';
1046 const SUBJECT = 'subject';
1047 const OBJECTTYPE = 'object-type';
1048 const CONTEXT = 'context';
1049 const TARGET = 'target';
1051 const ATOM = 'http://www.w3.org/2005/Atom';
1053 const AUTHOR = 'author';
1054 const PUBLISHED = 'published';
1055 const UPDATED = 'updated';
1057 public $actor; // an ActivityObject
1058 public $verb; // a string (the URL)
1059 public $object; // an ActivityObject
1060 public $target; // an ActivityObject
1061 public $context; // an ActivityObject
1062 public $time; // Time of the activity
1063 public $link; // an ActivityObject
1064 public $entry; // the source entry
1065 public $feed; // the source feed
1067 public $summary; // summary of activity
1068 public $content; // HTML content of activity
1069 public $id; // ID of the activity
1070 public $title; // title of the activity
1071 public $categories = array(); // list of AtomCategory objects
1072 public $enclosures = array(); // list of enclosure URL references
1075 * Turns a regular old Atom <entry> into a magical activity
1077 * @param DOMElement $entry Atom entry to poke at
1078 * @param DOMElement $feed Atom feed, for context
1081 function __construct($entry = null, $feed = null)
1083 if (is_null($entry)) {
1087 $this->entry = $entry;
1089 // Insist on a feed's root DOMElement; don't allow a DOMDocument
1090 if ($feed instanceof DOMDocument) {
1091 throw new ClientException(
1092 _("Expecting a root feed element but got a whole XML document.")
1096 $this->feed = $feed;
1098 $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
1100 if (!empty($pubEl)) {
1101 $this->time = strtotime($pubEl->textContent);
1103 // XXX technically an error; being liberal. Good idea...?
1104 $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
1105 if (!empty($updateEl)) {
1106 $this->time = strtotime($updateEl->textContent);
1112 $this->link = ActivityUtils::getPermalink($entry);
1114 $verbEl = $this->_child($entry, self::VERB);
1116 if (!empty($verbEl)) {
1117 $this->verb = trim($verbEl->textContent);
1119 $this->verb = ActivityVerb::POST;
1120 // XXX: do other implied stuff here
1123 $objectEl = $this->_child($entry, self::OBJECT);
1125 if (!empty($objectEl)) {
1126 $this->object = new ActivityObject($objectEl);
1128 $this->object = new ActivityObject($entry);
1131 $actorEl = $this->_child($entry, self::ACTOR);
1133 if (!empty($actorEl)) {
1135 $this->actor = new ActivityObject($actorEl);
1137 } else if (!empty($feed) &&
1138 $subjectEl = $this->_child($feed, self::SUBJECT)) {
1140 $this->actor = new ActivityObject($subjectEl);
1142 } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
1144 $this->actor = new ActivityObject($authorEl);
1146 } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
1149 $this->actor = new ActivityObject($authorEl);
1152 $contextEl = $this->_child($entry, self::CONTEXT);
1154 if (!empty($contextEl)) {
1155 $this->context = new ActivityContext($contextEl);
1157 $this->context = new ActivityContext($entry);
1160 $targetEl = $this->_child($entry, self::TARGET);
1162 if (!empty($targetEl)) {
1163 $this->target = new ActivityObject($targetEl);
1166 $this->summary = ActivityUtils::childContent($entry, 'summary');
1167 $this->id = ActivityUtils::childContent($entry, 'id');
1168 $this->content = ActivityUtils::getContent($entry);
1170 $catEls = $entry->getElementsByTagNameNS(self::ATOM, 'category');
1172 for ($i = 0; $i < $catEls->length; $i++) {
1173 $catEl = $catEls->item($i);
1174 $this->categories[] = new AtomCategory($catEl);
1178 foreach (ActivityUtils::getLinks($entry, 'enclosure') as $link) {
1179 $this->enclosures[] = $link->getAttribute('href');
1184 * Returns an Atom <entry> based on this activity
1186 * @return DOMElement Atom entry
1189 function toAtomEntry()
1194 function asString($namespace=false)
1196 $xs = new XMLStringer(true);
1199 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
1200 'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
1201 'xmlns:georss' => 'http://www.georss.org/georss',
1202 'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
1203 'xmlns:poco' => 'http://portablecontacts.net/spec/1.0',
1204 'xmlns:media' => 'http://purl.org/syndication/atommedia');
1209 $xs->elementStart('entry', $attrs);
1211 $xs->element('id', null, $this->id);
1212 $xs->element('title', null, $this->title);
1213 $xs->element('published', null, common_date_iso8601($this->time));
1214 $xs->element('content', array('type' => 'html'), $this->content);
1216 if (!empty($this->summary)) {
1217 $xs->element('summary', null, $this->summary);
1220 if (!empty($this->link)) {
1221 $xs->element('link', array('rel' => 'alternate',
1222 'type' => 'text/html'),
1228 $xs->elementStart('author');
1229 $xs->element('uri', array(), $this->actor->id);
1230 if ($this->actor->title) {
1231 $xs->element('name', array(), $this->actor->title);
1233 $xs->elementEnd('author');
1234 $xs->raw($this->actor->asString('activity:actor'));
1236 $xs->element('activity:verb', null, $this->verb);
1238 if ($this->object) {
1239 $xs->raw($this->object->asString());
1242 if ($this->target) {
1243 $xs->raw($this->target->asString('activity:target'));
1246 foreach ($this->categories as $cat) {
1247 $xs->raw($cat->asString());
1250 $xs->elementEnd('entry');
1252 return $xs->getString();
1255 private function _child($element, $tag, $namespace=self::SPEC)
1257 return ActivityUtils::child($element, $tag, $namespace);
1267 function __construct($element=null)
1269 if ($element && $element->attributes) {
1270 $this->term = $this->extract($element, 'term');
1271 $this->scheme = $this->extract($element, 'scheme');
1272 $this->label = $this->extract($element, 'label');
1276 protected function extract($element, $attrib)
1278 $node = $element->attributes->getNamedItemNS(Activity::ATOM, $attrib);
1280 return trim($node->textContent);
1282 $node = $element->attributes->getNamedItem($attrib);
1284 return trim($node->textContent);
1292 if ($this->term !== null) {
1293 $attribs['term'] = $this->term;
1295 if ($this->scheme !== null) {
1296 $attribs['scheme'] = $this->scheme;
1298 if ($this->label !== null) {
1299 $attribs['label'] = $this->label;
1301 $xs = new XMLStringer();
1302 $xs->element('category', $attribs);
1303 return $xs->asString();