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 return $contentEl->textContent;
462 } else if ($type == 'html') {
463 $text = $contentEl->textContent;
464 return htmlspecialchars_decode($text, ENT_QUOTES);
465 } else if ($type == 'xhtml') {
466 $divEl = ActivityUtils::child($contentEl, 'div', 'http://www.w3.org/1999/xhtml');
470 $doc = $divEl->ownerDocument;
472 $children = $divEl->childNodes;
474 for ($i = 0; $i < $children->length; $i++) {
475 $child = $children->item($i);
476 $text .= $doc->saveXML($child);
479 } else if (in_array($type, array('text/xml', 'application/xml')) ||
480 preg_match('#(+|/)xml$#', $type)) {
481 throw new ClientException(_("Can't handle embedded XML content yet."));
482 } else if (strncasecmp($type, 'text/', 5)) {
483 return $contentEl->textContent;
485 throw new ClientException(_("Can't handle embedded Base64 content yet."));
491 // XXX: Arg! This wouldn't be necessary if we used Avatars conistently
500 function __construct($element=null)
503 // @fixme use correct namespaces
504 $this->url = $element->getAttribute('href');
505 $this->type = $element->getAttribute('type');
506 $width = $element->getAttribute('media:width');
507 if ($width != null) {
508 $this->width = intval($width);
510 $height = $element->getAttribute('media:height');
511 if ($height != null) {
512 $this->height = intval($height);
517 static function fromAvatar($avatar)
519 if (empty($avatar)) {
522 $alink = new AvatarLink();
523 $alink->type = $avatar->mediatype;
524 $alink->height = $avatar->height;
525 $alink->width = $avatar->width;
526 $alink->url = $avatar->displayUrl();
530 static function fromFilename($filename, $size)
532 $alink = new AvatarLink();
533 $alink->url = $filename;
534 $alink->height = $size;
535 if (!empty($filename)) {
536 $alink->width = $size;
537 $alink->type = self::mediatype($filename);
539 $alink->url = User_group::defaultLogo($size);
540 $alink->type = 'image/png';
546 static function mediatype($filename) {
547 $ext = strtolower(end(explode('.', $filename)));
548 if ($ext == 'jpeg') {
551 // hope we don't support any others
552 $types = array('png', 'gif', 'jpg', 'jpeg');
553 if (in_array($ext, $types)) {
554 return 'image/' . $ext;
561 * A noun-ish thing in the activity universe
563 * The activity streams spec talks about activity objects, while also having
564 * a tag activity:object, which is in fact an activity object. Aaaaaah!
566 * This is just a thing in the activity universe. Can be the subject, object,
567 * or indirect object (target!) of an activity verb. Rotten name, and I'm
568 * propagating it. *sigh*
572 * @author Evan Prodromou <evan@status.net>
573 * @copyright 2010 StatusNet, Inc.
574 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
575 * @link http://status.net/
580 const ARTICLE = 'http://activitystrea.ms/schema/1.0/article';
581 const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
582 const NOTE = 'http://activitystrea.ms/schema/1.0/note';
583 const STATUS = 'http://activitystrea.ms/schema/1.0/status';
584 const FILE = 'http://activitystrea.ms/schema/1.0/file';
585 const PHOTO = 'http://activitystrea.ms/schema/1.0/photo';
586 const ALBUM = 'http://activitystrea.ms/schema/1.0/photo-album';
587 const PLAYLIST = 'http://activitystrea.ms/schema/1.0/playlist';
588 const VIDEO = 'http://activitystrea.ms/schema/1.0/video';
589 const AUDIO = 'http://activitystrea.ms/schema/1.0/audio';
590 const BOOKMARK = 'http://activitystrea.ms/schema/1.0/bookmark';
591 const PERSON = 'http://activitystrea.ms/schema/1.0/person';
592 const GROUP = 'http://activitystrea.ms/schema/1.0/group';
593 const PLACE = 'http://activitystrea.ms/schema/1.0/place';
594 const COMMENT = 'http://activitystrea.ms/schema/1.0/comment';
597 // Atom elements we snarf
599 const TITLE = 'title';
600 const SUMMARY = 'summary';
602 const SOURCE = 'source';
606 const EMAIL = 'email';
616 public $avatarLinks = array();
624 * This probably needs to be refactored
625 * to generate a local class (ActivityPerson, ActivityFile, ...)
626 * based on the object type.
628 * @param DOMElement $element DOM thing to turn into an Activity thing
631 function __construct($element = null)
633 if (empty($element)) {
637 $this->element = $element;
639 $this->geopoint = $this->_childContent(
641 ActivityContext::POINT,
642 ActivityContext::GEORSS
645 if ($element->tagName == 'author') {
646 $this->_fromAuthor($element);
647 } else if ($element->tagName == 'item') {
648 $this->_fromRssItem($element);
650 $this->_fromAtomEntry($element);
653 // Some per-type attributes...
654 if ($this->type == self::PERSON || $this->type == self::GROUP) {
655 $this->displayName = $this->title;
657 $photos = ActivityUtils::getLinks($element, 'photo');
658 if (count($photos)) {
659 foreach ($photos as $link) {
660 $this->avatarLinks[] = new AvatarLink($link);
663 $avatars = ActivityUtils::getLinks($element, 'avatar');
664 foreach ($avatars as $link) {
665 $this->avatarLinks[] = new AvatarLink($link);
669 $this->poco = new PoCo($element);
673 private function _fromAuthor($element)
675 $this->type = self::PERSON; // XXX: is this fair?
676 $this->title = $this->_childContent($element, self::NAME);
677 $this->id = $this->_childContent($element, self::URI);
679 if (empty($this->id)) {
680 $email = $this->_childContent($element, self::EMAIL);
681 if (!empty($email)) {
683 $this->id = 'mailto:'.$email;
688 private function _fromAtomEntry($element)
690 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
693 if (empty($this->type)) {
694 $this->type = ActivityObject::NOTE;
697 $this->id = $this->_childContent($element, self::ID);
698 $this->title = $this->_childContent($element, self::TITLE);
699 $this->summary = $this->_childContent($element, self::SUMMARY);
701 $this->source = $this->_getSource($element);
703 $this->content = ActivityUtils::getContent($element);
705 $this->link = ActivityUtils::getPermalink($element);
708 // @fixme rationalize with Activity::_fromRssItem()
710 private function _fromRssItem($item)
712 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
714 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
716 if (!empty($contentEl)) {
717 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
719 $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
720 if (!empty($descriptionEl)) {
721 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
725 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
727 $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
729 if (!empty($guidEl)) {
730 $this->id = $guidEl->textContent;
732 if ($guidEl->hasAttribute('isPermaLink')) {
734 $this->link = $this->id;
739 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
741 return ActivityUtils::childContent($element, $tag, $namespace);
744 // Try to get a unique id for the source feed
746 private function _getSource($element)
748 $sourceEl = ActivityUtils::child($element, 'source');
750 if (empty($sourceEl)) {
753 $href = ActivityUtils::getLink($sourceEl, 'self');
757 return ActivityUtils::childContent($sourceEl, 'id');
762 static function fromNotice(Notice $notice)
764 $object = new ActivityObject();
766 $object->type = ActivityObject::NOTE;
768 $object->id = $notice->uri;
769 $object->title = $notice->content;
770 $object->content = $notice->rendered;
771 $object->link = $notice->bestUrl();
776 static function fromProfile(Profile $profile)
778 $object = new ActivityObject();
780 $object->type = ActivityObject::PERSON;
781 $object->id = $profile->getUri();
782 $object->title = $profile->getBestName();
783 $object->link = $profile->profileurl;
785 $orig = $profile->getOriginalAvatar();
788 $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
797 foreach ($sizes as $size) {
800 $avatar = $profile->getAvatar($size);
802 if (!empty($avatar)) {
803 $alink = AvatarLink::fromAvatar($avatar);
805 $alink = new AvatarLink();
806 $alink->type = 'image/png';
807 $alink->height = $size;
808 $alink->width = $size;
809 $alink->url = Avatar::defaultImage($size);
812 $object->avatarLinks[] = $alink;
815 if (isset($profile->lat) && isset($profile->lon)) {
816 $object->geopoint = (float)$profile->lat
817 . ' ' . (float)$profile->lon;
820 $object->poco = PoCo::fromProfile($profile);
825 static function fromGroup($group)
827 $object = new ActivityObject();
829 $object->type = ActivityObject::GROUP;
830 $object->id = $group->getUri();
831 $object->title = $group->getBestName();
832 $object->link = $group->getUri();
834 $object->avatarLinks[] = AvatarLink::fromFilename(
835 $group->homepage_logo,
839 $object->avatarLinks[] = AvatarLink::fromFilename(
844 $object->avatarLinks[] = AvatarLink::fromFilename(
849 $object->poco = PoCo::fromGroup($group);
854 function asString($tag='activity:object')
856 $xs = new XMLStringer(true);
858 $xs->elementStart($tag);
860 $xs->element('activity:object-type', null, $this->type);
862 $xs->element(self::ID, null, $this->id);
864 if (!empty($this->title)) {
868 common_xml_safe_str($this->title)
872 if (!empty($this->summary)) {
876 common_xml_safe_str($this->summary)
880 if (!empty($this->content)) {
881 // XXX: assuming HTML content here
883 ActivityUtils::CONTENT,
884 array('type' => 'html'),
885 common_xml_safe_str($this->content)
889 if (!empty($this->link)) {
893 'rel' => 'alternate',
894 'type' => 'text/html',
895 'href' => $this->link
901 if ($this->type == ActivityObject::PERSON
902 || $this->type == ActivityObject::GROUP) {
904 foreach ($this->avatarLinks as $avatar) {
908 'type' => $avatar->type,
909 'media:width' => $avatar->width,
910 'media:height' => $avatar->height,
911 'href' => $avatar->url
918 if (!empty($this->geopoint)) {
926 if (!empty($this->poco)) {
927 $xs->raw($this->poco->asString());
930 $xs->elementEnd($tag);
932 return $xs->getString();
937 * Utility class to hold a bunch of constant defining default verb types
941 * @author Evan Prodromou <evan@status.net>
942 * @copyright 2010 StatusNet, Inc.
943 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
944 * @link http://status.net/
949 const POST = 'http://activitystrea.ms/schema/1.0/post';
950 const SHARE = 'http://activitystrea.ms/schema/1.0/share';
951 const SAVE = 'http://activitystrea.ms/schema/1.0/save';
952 const FAVORITE = 'http://activitystrea.ms/schema/1.0/favorite';
953 const PLAY = 'http://activitystrea.ms/schema/1.0/play';
954 const FOLLOW = 'http://activitystrea.ms/schema/1.0/follow';
955 const FRIEND = 'http://activitystrea.ms/schema/1.0/make-friend';
956 const JOIN = 'http://activitystrea.ms/schema/1.0/join';
957 const TAG = 'http://activitystrea.ms/schema/1.0/tag';
959 // Custom OStatus verbs for the flipside until they're standardized
960 const DELETE = 'http://ostatus.org/schema/1.0/unfollow';
961 const UNFAVORITE = 'http://ostatus.org/schema/1.0/unfavorite';
962 const UNFOLLOW = 'http://ostatus.org/schema/1.0/unfollow';
963 const LEAVE = 'http://ostatus.org/schema/1.0/leave';
965 // For simple profile-update pings; no content to share.
966 const UPDATE_PROFILE = 'http://ostatus.org/schema/1.0/update-profile';
969 class ActivityContext
974 public $attention = array();
975 public $conversation;
977 const THR = 'http://purl.org/syndication/thread/1.0';
978 const GEORSS = 'http://www.georss.org/georss';
979 const OSTATUS = 'http://ostatus.org/schema/1.0';
981 const INREPLYTO = 'in-reply-to';
985 const POINT = 'point';
987 const ATTENTION = 'ostatus:attention';
988 const CONVERSATION = 'ostatus:conversation';
990 function __construct($element)
992 $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
994 if (!empty($replyToEl)) {
995 $this->replyToID = $replyToEl->getAttribute(self::REF);
996 $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
999 $this->location = $this->getLocation($element);
1001 $this->conversation = ActivityUtils::getLink($element, self::CONVERSATION);
1003 // Multiple attention links allowed
1005 $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
1007 for ($i = 0; $i < $links->length; $i++) {
1009 $link = $links->item($i);
1011 $linkRel = $link->getAttribute(ActivityUtils::REL);
1013 if ($linkRel == self::ATTENTION) {
1014 $this->attention[] = $link->getAttribute(self::HREF);
1020 * Parse location given as a GeoRSS-simple point, if provided.
1021 * http://www.georss.org/simple
1023 * @param feed item $entry
1024 * @return mixed Location or false
1026 function getLocation($dom)
1028 $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
1030 for ($i = 0; $i < $points->length; $i++) {
1031 $point = $points->item($i)->textContent;
1032 return self::locationFromPoint($point);
1038 // XXX: Move to ActivityUtils or Location?
1039 static function locationFromPoint($point)
1041 $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
1042 $point = preg_replace('/\s+/', ' ', $point);
1043 $point = trim($point);
1044 $coords = explode(' ', $point);
1045 if (count($coords) == 2) {
1046 list($lat, $lon) = $coords;
1047 if (is_numeric($lat) && is_numeric($lon)) {
1048 common_log(LOG_INFO, "Looking up location for $lat $lon from georss point");
1049 return Location::fromLatLon($lat, $lon);
1052 common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
1058 * An activity in the ActivityStrea.ms world
1060 * An activity is kind of like a sentence: someone did something
1061 * to something else.
1063 * 'someone' is the 'actor'; 'did something' is the verb;
1064 * 'something else' is the object.
1067 * @package StatusNet
1068 * @author Evan Prodromou <evan@status.net>
1069 * @copyright 2010 StatusNet, Inc.
1070 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
1071 * @link http://status.net/
1076 const SPEC = 'http://activitystrea.ms/spec/1.0/';
1077 const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
1079 const VERB = 'verb';
1080 const OBJECT = 'object';
1081 const ACTOR = 'actor';
1082 const SUBJECT = 'subject';
1083 const OBJECTTYPE = 'object-type';
1084 const CONTEXT = 'context';
1085 const TARGET = 'target';
1087 const ATOM = 'http://www.w3.org/2005/Atom';
1089 const AUTHOR = 'author';
1090 const PUBLISHED = 'published';
1091 const UPDATED = 'updated';
1093 const RSS = null; // no namespace!
1095 const PUBDATE = 'pubDate';
1096 const DESCRIPTION = 'description';
1097 const GUID = 'guid';
1098 const SELF = 'self';
1099 const IMAGE = 'image';
1102 const DC = 'http://purl.org/dc/elements/1.1/';
1104 const CREATOR = 'creator';
1106 const CONTENTNS = 'http://purl.org/rss/1.0/modules/content/';
1108 public $actor; // an ActivityObject
1109 public $verb; // a string (the URL)
1110 public $object; // an ActivityObject
1111 public $target; // an ActivityObject
1112 public $context; // an ActivityObject
1113 public $time; // Time of the activity
1114 public $link; // an ActivityObject
1115 public $entry; // the source entry
1116 public $feed; // the source feed
1118 public $summary; // summary of activity
1119 public $content; // HTML content of activity
1120 public $id; // ID of the activity
1121 public $title; // title of the activity
1122 public $categories = array(); // list of AtomCategory objects
1123 public $enclosures = array(); // list of enclosure URL references
1126 * Turns a regular old Atom <entry> into a magical activity
1128 * @param DOMElement $entry Atom entry to poke at
1129 * @param DOMElement $feed Atom feed, for context
1132 function __construct($entry = null, $feed = null)
1134 if (is_null($entry)) {
1138 // Insist on a feed's root DOMElement; don't allow a DOMDocument
1139 if ($feed instanceof DOMDocument) {
1140 throw new ClientException(
1141 _("Expecting a root feed element but got a whole XML document.")
1145 $this->entry = $entry;
1146 $this->feed = $feed;
1148 if ($entry->namespaceURI == Activity::ATOM &&
1149 $entry->localName == 'entry') {
1150 $this->_fromAtomEntry($entry, $feed);
1151 } else if ($entry->namespaceURI == Activity::RSS &&
1152 $entry->localName == 'item') {
1153 $this->_fromRssItem($entry, $feed);
1155 throw new Exception("Unknown DOM element: {$entry->namespaceURI} {$entry->localName}");
1159 function _fromAtomEntry($entry, $feed)
1161 $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
1163 if (!empty($pubEl)) {
1164 $this->time = strtotime($pubEl->textContent);
1166 // XXX technically an error; being liberal. Good idea...?
1167 $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
1168 if (!empty($updateEl)) {
1169 $this->time = strtotime($updateEl->textContent);
1175 $this->link = ActivityUtils::getPermalink($entry);
1177 $verbEl = $this->_child($entry, self::VERB);
1179 if (!empty($verbEl)) {
1180 $this->verb = trim($verbEl->textContent);
1182 $this->verb = ActivityVerb::POST;
1183 // XXX: do other implied stuff here
1186 $objectEl = $this->_child($entry, self::OBJECT);
1188 if (!empty($objectEl)) {
1189 $this->object = new ActivityObject($objectEl);
1191 $this->object = new ActivityObject($entry);
1194 $actorEl = $this->_child($entry, self::ACTOR);
1196 if (!empty($actorEl)) {
1198 $this->actor = new ActivityObject($actorEl);
1200 } else if (!empty($feed) &&
1201 $subjectEl = $this->_child($feed, self::SUBJECT)) {
1203 $this->actor = new ActivityObject($subjectEl);
1205 } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
1207 $this->actor = new ActivityObject($authorEl);
1209 } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
1212 $this->actor = new ActivityObject($authorEl);
1215 $contextEl = $this->_child($entry, self::CONTEXT);
1217 if (!empty($contextEl)) {
1218 $this->context = new ActivityContext($contextEl);
1220 $this->context = new ActivityContext($entry);
1223 $targetEl = $this->_child($entry, self::TARGET);
1225 if (!empty($targetEl)) {
1226 $this->target = new ActivityObject($targetEl);
1229 $this->summary = ActivityUtils::childContent($entry, 'summary');
1230 $this->id = ActivityUtils::childContent($entry, 'id');
1231 $this->content = ActivityUtils::getContent($entry);
1233 $catEls = $entry->getElementsByTagNameNS(self::ATOM, 'category');
1235 for ($i = 0; $i < $catEls->length; $i++) {
1236 $catEl = $catEls->item($i);
1237 $this->categories[] = new AtomCategory($catEl);
1241 foreach (ActivityUtils::getLinks($entry, 'enclosure') as $link) {
1242 $this->enclosures[] = $link->getAttribute('href');
1246 function _fromRssItem($item, $rss)
1248 $verbEl = $this->_child($item, self::VERB);
1250 if (!empty($verbEl)) {
1251 $this->verb = trim($verbEl->textContent);
1253 $this->verb = ActivityVerb::POST;
1254 // XXX: do other implied stuff here
1257 $pubDateEl = $this->_child($item, self::PUBDATE, self::RSS);
1259 if (!empty($pubDateEl)) {
1260 $this->time = strtotime($pubDateEl->textContent);
1263 $authorEl = $this->_child($item, self::AUTHOR, self::RSS);
1265 if (!empty($authorEl)) {
1266 $this->actor = $this->_fromRssAuthor($authorEl);
1268 $dcCreatorEl = $this->_child($item, self::CREATOR, self::DC);
1269 if (!empty($dcCreatorEl)) {
1270 $this->actor = $this->_fromDcCreator($dcCreatorEl);
1271 } else if (!empty($rss)) {
1272 $this->actor = $this->_fromRss($rss);
1276 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, self::RSS);
1278 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, self::CONTENTNS);
1280 if (!empty($contentEl)) {
1281 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
1283 $descriptionEl = ActivityUtils::child($item, self::DESCRIPTION, self::RSS);
1284 if (!empty($descriptionEl)) {
1285 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
1289 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, self::RSS);
1291 // @fixme enclosures
1292 // @fixme thumbnails... maybe
1294 $guidEl = ActivityUtils::child($item, self::GUID, self::RSS);
1296 if (!empty($guidEl)) {
1297 $this->id = $guidEl->textContent;
1299 if ($guidEl->hasAttribute('isPermaLink') && $guidEl->getAttribute('isPermaLink') != 'false') {
1300 // overwrites <link>
1301 $this->link = $this->id;
1305 $this->object = new ActivityObject($item);
1306 $this->context = new ActivityContext($item);
1310 * Returns an Atom <entry> based on this activity
1312 * @return DOMElement Atom entry
1315 function toAtomEntry()
1320 function asString($namespace=false)
1322 $xs = new XMLStringer(true);
1325 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
1326 'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
1327 'xmlns:georss' => 'http://www.georss.org/georss',
1328 'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
1329 'xmlns:poco' => 'http://portablecontacts.net/spec/1.0',
1330 'xmlns:media' => 'http://purl.org/syndication/atommedia');
1335 $xs->elementStart('entry', $attrs);
1337 $xs->element('id', null, $this->id);
1338 $xs->element('title', null, $this->title);
1339 $xs->element('published', null, common_date_iso8601($this->time));
1340 $xs->element('content', array('type' => 'html'), $this->content);
1342 if (!empty($this->summary)) {
1343 $xs->element('summary', null, $this->summary);
1346 if (!empty($this->link)) {
1347 $xs->element('link', array('rel' => 'alternate',
1348 'type' => 'text/html'),
1354 $xs->elementStart('author');
1355 $xs->element('uri', array(), $this->actor->id);
1356 if ($this->actor->title) {
1357 $xs->element('name', array(), $this->actor->title);
1359 $xs->elementEnd('author');
1360 $xs->raw($this->actor->asString('activity:actor'));
1362 $xs->element('activity:verb', null, $this->verb);
1364 if ($this->object) {
1365 $xs->raw($this->object->asString());
1368 if ($this->target) {
1369 $xs->raw($this->target->asString('activity:target'));
1372 foreach ($this->categories as $cat) {
1373 $xs->raw($cat->asString());
1376 $xs->elementEnd('entry');
1378 return $xs->getString();
1381 function _fromRssAuthor($el)
1383 $text = $el->textContent;
1385 if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
1388 } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
1391 } else if (preg_match('/.*@.*/', $text)) {
1399 // Not really enough info
1401 $actor = new ActivityObject();
1403 $actor->element = $el;
1405 $actor->type = ActivityObject::PERSON;
1406 $actor->title = $name;
1408 if (!empty($email)) {
1409 $actor->id = 'mailto:'.$email;
1415 function _fromDcCreator($el)
1417 // Not really enough info
1419 $text = $el->textContent;
1421 $actor = new ActivityObject();
1423 $actor->element = $el;
1425 $actor->title = $text;
1426 $actor->type = ActivityObject::PERSON;
1431 function _fromRss($el)
1433 $actor = new ActivityObject();
1435 $actor->element = $el;
1437 $actor->type = ActivityObject::PERSON; // @fixme guess better
1439 $actor->title = ActivityUtils::childContent($el, ActivityObject::TITLE, self::RSS);
1440 $actor->link = ActivityUtils::childContent($el, ActivityUtils::LINK, self::RSS);
1441 $actor->id = ActivityUtils::getLink($el, self::SELF);
1443 $desc = ActivityUtils::childContent($el, self::DESCRIPTION, self::RSS);
1445 if (!empty($desc)) {
1446 $actor->content = htmlspecialchars_decode($desc, ENT_QUOTES);
1449 $imageEl = ActivityUtils::child($el, self::IMAGE, self::RSS);
1451 if (!empty($imageEl)) {
1452 $actor->avatarLinks[] = ActivityUtils::childContent($imageEl, self::URL, self::RSS);
1458 private function _child($element, $tag, $namespace=self::SPEC)
1460 return ActivityUtils::child($element, $tag, $namespace);
1470 function __construct($element=null)
1472 if ($element && $element->attributes) {
1473 $this->term = $this->extract($element, 'term');
1474 $this->scheme = $this->extract($element, 'scheme');
1475 $this->label = $this->extract($element, 'label');
1479 protected function extract($element, $attrib)
1481 $node = $element->attributes->getNamedItemNS(Activity::ATOM, $attrib);
1483 return trim($node->textContent);
1485 $node = $element->attributes->getNamedItem($attrib);
1487 return trim($node->textContent);
1495 if ($this->term !== null) {
1496 $attribs['term'] = $this->term;
1498 if ($this->scheme !== null) {
1499 $attribs['scheme'] = $this->scheme;
1501 if ($this->label !== null) {
1502 $attribs['label'] = $this->label;
1504 $xs = new XMLStringer();
1505 $xs->element('category', $attribs);
1506 return $xs->asString();