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/
54 const ARTICLE = 'http://activitystrea.ms/schema/1.0/article';
55 const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
56 const NOTE = 'http://activitystrea.ms/schema/1.0/note';
57 const STATUS = 'http://activitystrea.ms/schema/1.0/status';
58 const FILE = 'http://activitystrea.ms/schema/1.0/file';
59 const PHOTO = 'http://activitystrea.ms/schema/1.0/photo';
60 const ALBUM = 'http://activitystrea.ms/schema/1.0/photo-album';
61 const PLAYLIST = 'http://activitystrea.ms/schema/1.0/playlist';
62 const VIDEO = 'http://activitystrea.ms/schema/1.0/video';
63 const AUDIO = 'http://activitystrea.ms/schema/1.0/audio';
64 const BOOKMARK = 'http://activitystrea.ms/schema/1.0/bookmark';
65 const PERSON = 'http://activitystrea.ms/schema/1.0/person';
66 const GROUP = 'http://activitystrea.ms/schema/1.0/group';
67 const PLACE = 'http://activitystrea.ms/schema/1.0/place';
68 const COMMENT = 'http://activitystrea.ms/schema/1.0/comment';
71 // Atom elements we snarf
73 const TITLE = 'title';
74 const SUMMARY = 'summary';
76 const SOURCE = 'source';
80 const EMAIL = 'email';
82 const POSTEROUS = 'http://posterous.com/help/rss/1.0';
83 const AUTHOR = 'author';
84 const USERIMAGE = 'userImage';
85 const PROFILEURL = 'profileUrl';
86 const NICKNAME = 'nickName';
87 const DISPLAYNAME = 'displayName';
97 public $avatarLinks = array();
102 // @todo move this stuff to it's own PHOTO activity object
103 const MEDIA_DESCRIPTION = 'description';
109 public $extra; // For extra stuff
114 * This probably needs to be refactored
115 * to generate a local class (ActivityPerson, ActivityFile, ...)
116 * based on the object type.
118 * @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,
174 private function _fromAuthor($element)
176 $this->type = self::PERSON; // XXX: is this fair?
177 $this->title = $this->_childContent($element, self::NAME);
179 $this->id = $this->_childContent($element, self::URI);
181 if (empty($this->id)) {
182 $email = $this->_childContent($element, self::EMAIL);
183 if (!empty($email)) {
185 $this->id = 'mailto:'.$email;
190 private function _fromAtomEntry($element)
192 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
195 if (empty($this->type)) {
196 $this->type = ActivityObject::NOTE;
199 $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
200 $this->content = ActivityUtils::getContent($element);
202 // We don't like HTML in our titles, although it's technically allowed
204 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
206 $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
208 $this->source = $this->_getSource($element);
210 $this->link = ActivityUtils::getPermalink($element);
212 $this->id = $this->_childContent($element, self::ID);
214 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
215 $this->id = $this->link;
219 // @todo FIXME: rationalize with Activity::_fromRssItem()
220 private function _fromRssItem($item)
222 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
224 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
226 if (!empty($contentEl)) {
227 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
229 $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
230 if (!empty($descriptionEl)) {
231 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
235 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
237 $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
239 if (!empty($guidEl)) {
240 $this->id = $guidEl->textContent;
242 if ($guidEl->hasAttribute('isPermaLink')) {
244 $this->link = $this->id;
249 public static function fromRssAuthor($el)
251 $text = $el->textContent;
253 if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
256 } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
259 } else if (preg_match('/.*@.*/', $text)) {
267 // Not really enough info
269 $obj = new ActivityObject();
273 $obj->type = ActivityObject::PERSON;
276 if (!empty($email)) {
277 $obj->id = 'mailto:'.$email;
283 public static function fromDcCreator($el)
285 // Not really enough info
287 $text = $el->textContent;
289 $obj = new ActivityObject();
294 $obj->type = ActivityObject::PERSON;
299 public static function fromRssChannel($el)
301 $obj = new ActivityObject();
305 $obj->type = ActivityObject::PERSON; // @fixme guess better
307 $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
308 $obj->link = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
309 $obj->id = ActivityUtils::getLink($el, Activity::SELF);
311 if (empty($obj->id)) {
312 $obj->id = $obj->link;
315 $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
318 $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
321 $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
323 if (!empty($imageEl)) {
324 $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
325 $al = new AvatarLink();
327 $obj->avatarLinks[] = $al;
333 public static function fromPosterousAuthor($el)
335 $obj = new ActivityObject();
337 $obj->type = ActivityObject::PERSON; // @fixme any others...?
339 $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
341 if (!empty($userImage)) {
342 $al = new AvatarLink();
343 $al->url = $userImage;
344 $obj->avatarLinks[] = $al;
347 $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
348 $obj->id = $obj->link;
350 $obj->poco = new PoCo();
352 $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
353 $obj->poco->displayName = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
355 $obj->title = $obj->poco->displayName;
360 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
362 return ActivityUtils::childContent($element, $tag, $namespace);
365 // Try to get a unique id for the source feed
367 private function _getSource($element)
369 $sourceEl = ActivityUtils::child($element, 'source');
371 if (empty($sourceEl)) {
374 $href = ActivityUtils::getLink($sourceEl, 'self');
378 return ActivityUtils::childContent($sourceEl, 'id');
383 static function fromNotice(Notice $notice)
385 $object = new ActivityObject();
387 if (Event::handle('StartActivityObjectFromNotice', array($notice, &$object))) {
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();
396 Event::handle('EndActivityObjectFromNotice', array($notice, &$object));
402 static function fromProfile(Profile $profile)
404 $object = new ActivityObject();
406 if (Event::handle('StartActivityObjectFromProfile', array($profile, &$object))) {
408 $object->type = ActivityObject::PERSON;
409 $object->id = $profile->getUri();
410 $object->title = $profile->getBestName();
411 $object->link = $profile->profileurl;
413 $orig = $profile->getOriginalAvatar();
416 $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
425 foreach ($sizes as $size) {
427 $avatar = $profile->getAvatar($size);
429 if (!empty($avatar)) {
430 $alink = AvatarLink::fromAvatar($avatar);
432 $alink = new AvatarLink();
433 $alink->type = 'image/png';
434 $alink->height = $size;
435 $alink->width = $size;
436 $alink->url = Avatar::defaultImage($size);
438 if ($size == AVATAR_PROFILE_SIZE) {
439 // Hack for Twitter import: we don't have a 96x96 image,
440 // but we do have a 73x73 image. For now, fake it with that.
441 $avatar = $profile->getAvatar(73);
443 $alink = AvatarLink::fromAvatar($avatar);
444 $alink->height= $size;
445 $alink->width = $size;
450 $object->avatarLinks[] = $alink;
453 if (isset($profile->lat) && isset($profile->lon)) {
454 $object->geopoint = (float)$profile->lat
455 . ' ' . (float)$profile->lon;
458 $object->poco = PoCo::fromProfile($profile);
460 Event::handle('EndActivityObjectFromProfile', array($profile, &$object));
466 static function fromGroup($group)
468 $object = new ActivityObject();
470 if (Event::handle('StartActivityObjectFromGroup', array($group, &$object))) {
472 $object->type = ActivityObject::GROUP;
473 $object->id = $group->getUri();
474 $object->title = $group->getBestName();
475 $object->link = $group->getUri();
477 $object->avatarLinks[] = AvatarLink::fromFilename($group->homepage_logo,
478 AVATAR_PROFILE_SIZE);
480 $object->avatarLinks[] = AvatarLink::fromFilename($group->stream_logo,
483 $object->avatarLinks[] = AvatarLink::fromFilename($group->mini_logo,
486 $object->poco = PoCo::fromGroup($group);
488 Event::handle('EndActivityObjectFromGroup', array($group, &$object));
494 function asString($tag='activity:object')
496 $xs = new XMLStringer(true);
498 $xs->elementStart($tag);
500 $xs->element('activity:object-type', null, $this->type);
502 $xs->element(self::ID, null, $this->id);
504 if (!empty($this->title)) {
508 common_xml_safe_str($this->title)
512 if (!empty($this->summary)) {
516 common_xml_safe_str($this->summary)
520 if (!empty($this->content)) {
521 // XXX: assuming HTML content here
523 ActivityUtils::CONTENT,
524 array('type' => 'html'),
525 common_xml_safe_str($this->content)
529 if (!empty($this->link)) {
533 'rel' => 'alternate',
534 'type' => 'text/html',
535 'href' => $this->link
541 if ($this->type == ActivityObject::PERSON
542 || $this->type == ActivityObject::GROUP) {
544 foreach ($this->avatarLinks as $avatar) {
548 'type' => $avatar->type,
549 'media:width' => $avatar->width,
550 'media:height' => $avatar->height,
551 'href' => $avatar->url
558 if (!empty($this->geopoint)) {
566 if (!empty($this->poco)) {
567 $xs->raw($this->poco->asString());
570 foreach ($this->extra as $el) {
571 list($tag, $attrs, $content) = $el;
572 $xs->element($tag, $attrs, $content);
575 $xs->elementEnd($tag);
577 return $xs->getString();