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')) {
36 * A noun-ish thing in the activity universe
38 * The activity streams spec talks about activity objects, while also having
39 * a tag activity:object, which is in fact an activity object. Aaaaaah!
41 * This is just a thing in the activity universe. Can be the subject, object,
42 * or indirect object (target!) of an activity verb. Rotten name, and I'm
43 * propagating it. *sigh*
47 * @author Evan Prodromou <evan@status.net>
48 * @copyright 2010 StatusNet, Inc.
49 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
50 * @link http://status.net/
55 const ARTICLE = 'http://activitystrea.ms/schema/1.0/article';
56 const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
57 const NOTE = 'http://activitystrea.ms/schema/1.0/note';
58 const STATUS = 'http://activitystrea.ms/schema/1.0/status';
59 const FILE = 'http://activitystrea.ms/schema/1.0/file';
60 const PHOTO = 'http://activitystrea.ms/schema/1.0/photo';
61 const ALBUM = 'http://activitystrea.ms/schema/1.0/photo-album';
62 const PLAYLIST = 'http://activitystrea.ms/schema/1.0/playlist';
63 const VIDEO = 'http://activitystrea.ms/schema/1.0/video';
64 const AUDIO = 'http://activitystrea.ms/schema/1.0/audio';
65 const BOOKMARK = 'http://activitystrea.ms/schema/1.0/bookmark';
66 const PERSON = 'http://activitystrea.ms/schema/1.0/person';
67 const GROUP = 'http://activitystrea.ms/schema/1.0/group';
68 const PLACE = 'http://activitystrea.ms/schema/1.0/place';
69 const COMMENT = 'http://activitystrea.ms/schema/1.0/comment';
72 // Atom elements we snarf
74 const TITLE = 'title';
75 const SUMMARY = 'summary';
77 const SOURCE = 'source';
81 const EMAIL = 'email';
83 const POSTEROUS = 'http://posterous.com/help/rss/1.0';
84 const AUTHOR = 'author';
85 const USERIMAGE = 'userImage';
86 const PROFILEURL = 'profileUrl';
87 const NICKNAME = 'nickName';
88 const DISPLAYNAME = 'displayName';
98 public $avatarLinks = array();
103 // @todo move this stuff to it's own PHOTO activity object
104 const MEDIA_DESCRIPTION = 'description';
113 * This probably needs to be refactored
114 * to generate a local class (ActivityPerson, ActivityFile, ...)
115 * based on the object type.
117 * @param DOMElement $element DOM thing to turn into an Activity thing
120 function __construct($element = null)
122 if (empty($element)) {
126 $this->element = $element;
128 $this->geopoint = $this->_childContent(
130 ActivityContext::POINT,
131 ActivityContext::GEORSS
134 if ($element->tagName == 'author') {
135 $this->_fromAuthor($element);
136 } else if ($element->tagName == 'item') {
137 $this->_fromRssItem($element);
139 $this->_fromAtomEntry($element);
142 // Some per-type attributes...
143 if ($this->type == self::PERSON || $this->type == self::GROUP) {
144 $this->displayName = $this->title;
146 $photos = ActivityUtils::getLinks($element, 'photo');
147 if (count($photos)) {
148 foreach ($photos as $link) {
149 $this->avatarLinks[] = new AvatarLink($link);
152 $avatars = ActivityUtils::getLinks($element, 'avatar');
153 foreach ($avatars as $link) {
154 $this->avatarLinks[] = new AvatarLink($link);
158 $this->poco = new PoCo($element);
161 if ($this->type == self::PHOTO) {
163 $this->thumbnail = ActivityUtils::getLink($element, 'preview');
164 $this->largerImage = ActivityUtils::getLink($element, 'enclosure');
166 $this->description = ActivityUtils::childContent(
168 ActivityObject::MEDIA_DESCRIPTION,
175 private function _fromAuthor($element)
177 $this->type = self::PERSON; // XXX: is this fair?
178 $this->title = $this->_childContent($element, self::NAME);
180 $this->id = $this->_childContent($element, self::URI);
182 if (empty($this->id)) {
183 $email = $this->_childContent($element, self::EMAIL);
184 if (!empty($email)) {
186 $this->id = 'mailto:'.$email;
191 private function _fromAtomEntry($element)
193 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
196 if (empty($this->type)) {
197 $this->type = ActivityObject::NOTE;
200 $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
201 $this->content = ActivityUtils::getContent($element);
203 // We don't like HTML in our titles, although it's technically allowed
205 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
207 $this->title = html_entity_decode(strip_tags($title));
209 $this->source = $this->_getSource($element);
211 $this->link = ActivityUtils::getPermalink($element);
213 $this->id = $this->_childContent($element, self::ID);
215 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
216 $this->id = $this->link;
220 // @fixme rationalize with Activity::_fromRssItem()
222 private function _fromRssItem($item)
224 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
226 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
228 if (!empty($contentEl)) {
229 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
231 $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
232 if (!empty($descriptionEl)) {
233 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
237 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
239 $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
241 if (!empty($guidEl)) {
242 $this->id = $guidEl->textContent;
244 if ($guidEl->hasAttribute('isPermaLink')) {
246 $this->link = $this->id;
251 public static function fromRssAuthor($el)
253 $text = $el->textContent;
255 if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
258 } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
261 } else if (preg_match('/.*@.*/', $text)) {
269 // Not really enough info
271 $obj = new ActivityObject();
275 $obj->type = ActivityObject::PERSON;
278 if (!empty($email)) {
279 $obj->id = 'mailto:'.$email;
285 public static function fromDcCreator($el)
287 // Not really enough info
289 $text = $el->textContent;
291 $obj = new ActivityObject();
296 $obj->type = ActivityObject::PERSON;
301 public static function fromRssChannel($el)
303 $obj = new ActivityObject();
307 $obj->type = ActivityObject::PERSON; // @fixme guess better
309 $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
310 $obj->link = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
311 $obj->id = ActivityUtils::getLink($el, Activity::SELF);
313 if (empty($obj->id)) {
314 $obj->id = $obj->link;
317 $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
320 $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
323 $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
325 if (!empty($imageEl)) {
326 $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
327 $al = new AvatarLink();
329 $obj->avatarLinks[] = $al;
335 public static function fromPosterousAuthor($el)
337 $obj = new ActivityObject();
339 $obj->type = ActivityObject::PERSON; // @fixme any others...?
341 $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
343 if (!empty($userImage)) {
344 $al = new AvatarLink();
345 $al->url = $userImage;
346 $obj->avatarLinks[] = $al;
349 $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
350 $obj->id = $obj->link;
352 $obj->poco = new PoCo();
354 $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
355 $obj->poco->displayName = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
357 $obj->title = $obj->poco->displayName;
362 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
364 return ActivityUtils::childContent($element, $tag, $namespace);
367 // Try to get a unique id for the source feed
369 private function _getSource($element)
371 $sourceEl = ActivityUtils::child($element, 'source');
373 if (empty($sourceEl)) {
376 $href = ActivityUtils::getLink($sourceEl, 'self');
380 return ActivityUtils::childContent($sourceEl, 'id');
385 static function fromNotice(Notice $notice)
387 $object = new ActivityObject();
389 $object->type = ActivityObject::NOTE;
391 $object->id = $notice->uri;
392 $object->title = $notice->content;
393 $object->content = $notice->rendered;
394 $object->link = $notice->bestUrl();
399 static function fromProfile(Profile $profile)
401 $object = new ActivityObject();
403 $object->type = ActivityObject::PERSON;
404 $object->id = $profile->getUri();
405 $object->title = $profile->getBestName();
406 $object->link = $profile->profileurl;
408 $orig = $profile->getOriginalAvatar();
411 $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
420 foreach ($sizes as $size) {
423 $avatar = $profile->getAvatar($size);
425 if (!empty($avatar)) {
426 $alink = AvatarLink::fromAvatar($avatar);
428 $alink = new AvatarLink();
429 $alink->type = 'image/png';
430 $alink->height = $size;
431 $alink->width = $size;
432 $alink->url = Avatar::defaultImage($size);
434 if ($size == AVATAR_PROFILE_SIZE) {
435 // Hack for Twitter import: we don't have a 96x96 image,
436 // but we do have a 73x73 image. For now, fake it with that.
437 $avatar = $profile->getAvatar(73);
439 $alink = AvatarLink::fromAvatar($avatar);
440 $alink->height= $size;
441 $alink->width = $size;
446 $object->avatarLinks[] = $alink;
449 if (isset($profile->lat) && isset($profile->lon)) {
450 $object->geopoint = (float)$profile->lat
451 . ' ' . (float)$profile->lon;
454 $object->poco = PoCo::fromProfile($profile);
459 static function fromGroup($group)
461 $object = new ActivityObject();
463 $object->type = ActivityObject::GROUP;
464 $object->id = $group->getUri();
465 $object->title = $group->getBestName();
466 $object->link = $group->getUri();
468 $object->avatarLinks[] = AvatarLink::fromFilename(
469 $group->homepage_logo,
473 $object->avatarLinks[] = AvatarLink::fromFilename(
478 $object->avatarLinks[] = AvatarLink::fromFilename(
483 $object->poco = PoCo::fromGroup($group);
488 function asString($tag='activity:object')
490 $xs = new XMLStringer(true);
492 $xs->elementStart($tag);
494 $xs->element('activity:object-type', null, $this->type);
496 $xs->element(self::ID, null, $this->id);
498 if (!empty($this->title)) {
502 common_xml_safe_str($this->title)
506 if (!empty($this->summary)) {
510 common_xml_safe_str($this->summary)
514 if (!empty($this->content)) {
515 // XXX: assuming HTML content here
517 ActivityUtils::CONTENT,
518 array('type' => 'html'),
519 common_xml_safe_str($this->content)
523 if (!empty($this->link)) {
527 'rel' => 'alternate',
528 'type' => 'text/html',
529 'href' => $this->link
535 if ($this->type == ActivityObject::PERSON
536 || $this->type == ActivityObject::GROUP) {
538 foreach ($this->avatarLinks as $avatar) {
542 'type' => $avatar->type,
543 'media:width' => $avatar->width,
544 'media:height' => $avatar->height,
545 'href' => $avatar->url
552 if (!empty($this->geopoint)) {
560 if (!empty($this->poco)) {
561 $xs->raw($this->poco->asString());
564 $xs->elementEnd($tag);
566 return $xs->getString();