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';
112 * This probably needs to be refactored
113 * to generate a local class (ActivityPerson, ActivityFile, ...)
114 * based on the object type.
116 * @param DOMElement $element DOM thing to turn into an Activity thing
118 function __construct($element = null)
120 if (empty($element)) {
124 $this->element = $element;
126 $this->geopoint = $this->_childContent(
128 ActivityContext::POINT,
129 ActivityContext::GEORSS
132 if ($element->tagName == 'author') {
133 $this->_fromAuthor($element);
134 } else if ($element->tagName == 'item') {
135 $this->_fromRssItem($element);
137 $this->_fromAtomEntry($element);
140 // Some per-type attributes...
141 if ($this->type == self::PERSON || $this->type == self::GROUP) {
142 $this->displayName = $this->title;
144 $photos = ActivityUtils::getLinks($element, 'photo');
145 if (count($photos)) {
146 foreach ($photos as $link) {
147 $this->avatarLinks[] = new AvatarLink($link);
150 $avatars = ActivityUtils::getLinks($element, 'avatar');
151 foreach ($avatars as $link) {
152 $this->avatarLinks[] = new AvatarLink($link);
156 $this->poco = new PoCo($element);
159 if ($this->type == self::PHOTO) {
161 $this->thumbnail = ActivityUtils::getLink($element, 'preview');
162 $this->largerImage = ActivityUtils::getLink($element, 'enclosure');
164 $this->description = ActivityUtils::childContent(
166 ActivityObject::MEDIA_DESCRIPTION,
172 private function _fromAuthor($element)
174 $this->type = self::PERSON; // XXX: is this fair?
175 $this->title = $this->_childContent($element, self::NAME);
177 $this->id = $this->_childContent($element, self::URI);
179 if (empty($this->id)) {
180 $email = $this->_childContent($element, self::EMAIL);
181 if (!empty($email)) {
183 $this->id = 'mailto:'.$email;
188 private function _fromAtomEntry($element)
190 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
193 if (empty($this->type)) {
194 $this->type = ActivityObject::NOTE;
197 $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
198 $this->content = ActivityUtils::getContent($element);
200 // We don't like HTML in our titles, although it's technically allowed
202 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
204 $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
206 $this->source = $this->_getSource($element);
208 $this->link = ActivityUtils::getPermalink($element);
210 $this->id = $this->_childContent($element, self::ID);
212 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
213 $this->id = $this->link;
217 // @todo FIXME: rationalize with Activity::_fromRssItem()
218 private function _fromRssItem($item)
220 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
222 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
224 if (!empty($contentEl)) {
225 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
227 $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
228 if (!empty($descriptionEl)) {
229 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
233 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
235 $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
237 if (!empty($guidEl)) {
238 $this->id = $guidEl->textContent;
240 if ($guidEl->hasAttribute('isPermaLink')) {
242 $this->link = $this->id;
247 public static function fromRssAuthor($el)
249 $text = $el->textContent;
251 if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
254 } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
257 } else if (preg_match('/.*@.*/', $text)) {
265 // Not really enough info
267 $obj = new ActivityObject();
271 $obj->type = ActivityObject::PERSON;
274 if (!empty($email)) {
275 $obj->id = 'mailto:'.$email;
281 public static function fromDcCreator($el)
283 // Not really enough info
285 $text = $el->textContent;
287 $obj = new ActivityObject();
292 $obj->type = ActivityObject::PERSON;
297 public static function fromRssChannel($el)
299 $obj = new ActivityObject();
303 $obj->type = ActivityObject::PERSON; // @fixme guess better
305 $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
306 $obj->link = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
307 $obj->id = ActivityUtils::getLink($el, Activity::SELF);
309 if (empty($obj->id)) {
310 $obj->id = $obj->link;
313 $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
316 $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
319 $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
321 if (!empty($imageEl)) {
322 $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
323 $al = new AvatarLink();
325 $obj->avatarLinks[] = $al;
331 public static function fromPosterousAuthor($el)
333 $obj = new ActivityObject();
335 $obj->type = ActivityObject::PERSON; // @fixme any others...?
337 $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
339 if (!empty($userImage)) {
340 $al = new AvatarLink();
341 $al->url = $userImage;
342 $obj->avatarLinks[] = $al;
345 $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
346 $obj->id = $obj->link;
348 $obj->poco = new PoCo();
350 $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
351 $obj->poco->displayName = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
353 $obj->title = $obj->poco->displayName;
358 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
360 return ActivityUtils::childContent($element, $tag, $namespace);
363 // Try to get a unique id for the source feed
365 private function _getSource($element)
367 $sourceEl = ActivityUtils::child($element, 'source');
369 if (empty($sourceEl)) {
372 $href = ActivityUtils::getLink($sourceEl, 'self');
376 return ActivityUtils::childContent($sourceEl, 'id');
381 static function fromNotice(Notice $notice)
383 $object = new ActivityObject();
385 $object->type = ActivityObject::NOTE;
387 $object->id = $notice->uri;
388 $object->title = $notice->content;
389 $object->content = $notice->rendered;
390 $object->link = $notice->bestUrl();
395 static function fromProfile(Profile $profile)
397 $object = new ActivityObject();
399 $object->type = ActivityObject::PERSON;
400 $object->id = $profile->getUri();
401 $object->title = $profile->getBestName();
402 $object->link = $profile->profileurl;
404 $orig = $profile->getOriginalAvatar();
407 $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
416 foreach ($sizes as $size) {
418 $avatar = $profile->getAvatar($size);
420 if (!empty($avatar)) {
421 $alink = AvatarLink::fromAvatar($avatar);
423 $alink = new AvatarLink();
424 $alink->type = 'image/png';
425 $alink->height = $size;
426 $alink->width = $size;
427 $alink->url = Avatar::defaultImage($size);
429 if ($size == AVATAR_PROFILE_SIZE) {
430 // Hack for Twitter import: we don't have a 96x96 image,
431 // but we do have a 73x73 image. For now, fake it with that.
432 $avatar = $profile->getAvatar(73);
434 $alink = AvatarLink::fromAvatar($avatar);
435 $alink->height= $size;
436 $alink->width = $size;
441 $object->avatarLinks[] = $alink;
444 if (isset($profile->lat) && isset($profile->lon)) {
445 $object->geopoint = (float)$profile->lat
446 . ' ' . (float)$profile->lon;
449 $object->poco = PoCo::fromProfile($profile);
454 static function fromGroup($group)
456 $object = new ActivityObject();
458 $object->type = ActivityObject::GROUP;
459 $object->id = $group->getUri();
460 $object->title = $group->getBestName();
461 $object->link = $group->getUri();
463 $object->avatarLinks[] = AvatarLink::fromFilename(
464 $group->homepage_logo,
468 $object->avatarLinks[] = AvatarLink::fromFilename(
473 $object->avatarLinks[] = AvatarLink::fromFilename(
478 $object->poco = PoCo::fromGroup($group);
483 function asString($tag='activity:object')
485 $xs = new XMLStringer(true);
487 $xs->elementStart($tag);
489 $xs->element('activity:object-type', null, $this->type);
491 $xs->element(self::ID, null, $this->id);
493 if (!empty($this->title)) {
497 common_xml_safe_str($this->title)
501 if (!empty($this->summary)) {
505 common_xml_safe_str($this->summary)
509 if (!empty($this->content)) {
510 // XXX: assuming HTML content here
512 ActivityUtils::CONTENT,
513 array('type' => 'html'),
514 common_xml_safe_str($this->content)
518 if (!empty($this->link)) {
522 'rel' => 'alternate',
523 'type' => 'text/html',
524 'href' => $this->link
530 if ($this->type == ActivityObject::PERSON
531 || $this->type == ActivityObject::GROUP) {
533 foreach ($this->avatarLinks as $avatar) {
537 'type' => $avatar->type,
538 'media:width' => $avatar->width,
539 'media:height' => $avatar->height,
540 'href' => $avatar->url
547 if (!empty($this->geopoint)) {
555 if (!empty($this->poco)) {
556 $xs->raw($this->poco->asString());
559 $xs->elementEnd($tag);
561 return $xs->getString();