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';
108 public $extra = array();
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
119 function __construct($element = null)
121 if (empty($element)) {
125 $this->element = $element;
127 $this->geopoint = $this->_childContent(
129 ActivityContext::POINT,
130 ActivityContext::GEORSS
133 if ($element->tagName == 'author') {
134 $this->_fromAuthor($element);
135 } else if ($element->tagName == 'item') {
136 $this->_fromRssItem($element);
138 $this->_fromAtomEntry($element);
141 // Some per-type attributes...
142 if ($this->type == self::PERSON || $this->type == self::GROUP) {
143 $this->displayName = $this->title;
145 $photos = ActivityUtils::getLinks($element, 'photo');
146 if (count($photos)) {
147 foreach ($photos as $link) {
148 $this->avatarLinks[] = new AvatarLink($link);
151 $avatars = ActivityUtils::getLinks($element, 'avatar');
152 foreach ($avatars as $link) {
153 $this->avatarLinks[] = new AvatarLink($link);
157 $this->poco = new PoCo($element);
160 if ($this->type == self::PHOTO) {
162 $this->thumbnail = ActivityUtils::getLink($element, 'preview');
163 $this->largerImage = ActivityUtils::getLink($element, 'enclosure');
165 $this->description = ActivityUtils::childContent(
167 ActivityObject::MEDIA_DESCRIPTION,
173 private function _fromAuthor($element)
175 $this->type = self::PERSON; // XXX: is this fair?
176 $this->title = $this->_childContent($element, self::NAME);
178 $this->id = $this->_childContent($element, self::URI);
180 if (empty($this->id)) {
181 $email = $this->_childContent($element, self::EMAIL);
182 if (!empty($email)) {
184 $this->id = 'mailto:'.$email;
189 private function _fromAtomEntry($element)
191 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
194 if (empty($this->type)) {
195 $this->type = ActivityObject::NOTE;
198 $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
199 $this->content = ActivityUtils::getContent($element);
201 // We don't like HTML in our titles, although it's technically allowed
203 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
205 $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
207 $this->source = $this->_getSource($element);
209 $this->link = ActivityUtils::getPermalink($element);
211 $this->id = $this->_childContent($element, self::ID);
213 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
214 $this->id = $this->link;
218 // @todo FIXME: rationalize with Activity::_fromRssItem()
219 private function _fromRssItem($item)
221 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
223 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
225 if (!empty($contentEl)) {
226 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
228 $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
229 if (!empty($descriptionEl)) {
230 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
234 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
236 $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
238 if (!empty($guidEl)) {
239 $this->id = $guidEl->textContent;
241 if ($guidEl->hasAttribute('isPermaLink')) {
243 $this->link = $this->id;
248 public static function fromRssAuthor($el)
250 $text = $el->textContent;
252 if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
255 } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
258 } else if (preg_match('/.*@.*/', $text)) {
266 // Not really enough info
268 $obj = new ActivityObject();
272 $obj->type = ActivityObject::PERSON;
275 if (!empty($email)) {
276 $obj->id = 'mailto:'.$email;
282 public static function fromDcCreator($el)
284 // Not really enough info
286 $text = $el->textContent;
288 $obj = new ActivityObject();
293 $obj->type = ActivityObject::PERSON;
298 public static function fromRssChannel($el)
300 $obj = new ActivityObject();
304 $obj->type = ActivityObject::PERSON; // @fixme guess better
306 $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
307 $obj->link = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
308 $obj->id = ActivityUtils::getLink($el, Activity::SELF);
310 if (empty($obj->id)) {
311 $obj->id = $obj->link;
314 $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
317 $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
320 $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
322 if (!empty($imageEl)) {
323 $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
324 $al = new AvatarLink();
326 $obj->avatarLinks[] = $al;
332 public static function fromPosterousAuthor($el)
334 $obj = new ActivityObject();
336 $obj->type = ActivityObject::PERSON; // @fixme any others...?
338 $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
340 if (!empty($userImage)) {
341 $al = new AvatarLink();
342 $al->url = $userImage;
343 $obj->avatarLinks[] = $al;
346 $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
347 $obj->id = $obj->link;
349 $obj->poco = new PoCo();
351 $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
352 $obj->poco->displayName = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
354 $obj->title = $obj->poco->displayName;
359 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
361 return ActivityUtils::childContent($element, $tag, $namespace);
364 // Try to get a unique id for the source feed
366 private function _getSource($element)
368 $sourceEl = ActivityUtils::child($element, 'source');
370 if (empty($sourceEl)) {
373 $href = ActivityUtils::getLink($sourceEl, 'self');
377 return ActivityUtils::childContent($sourceEl, 'id');
382 static function fromNotice(Notice $notice)
384 $object = new ActivityObject();
386 if (Event::handle('StartActivityObjectFromNotice', array($notice, &$object))) {
388 $object->type = ActivityObject::NOTE;
390 $object->id = $notice->uri;
391 $object->title = $notice->content;
392 $object->content = $notice->rendered;
393 $object->link = $notice->bestUrl();
395 Event::handle('EndActivityObjectFromNotice', array($notice, &$object));
401 static function fromProfile(Profile $profile)
403 $object = new ActivityObject();
405 if (Event::handle('StartActivityObjectFromProfile', array($profile, &$object))) {
407 $object->type = ActivityObject::PERSON;
408 $object->id = $profile->getUri();
409 $object->title = $profile->getBestName();
410 $object->link = $profile->profileurl;
412 $orig = $profile->getOriginalAvatar();
415 $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
424 foreach ($sizes as $size) {
426 $avatar = $profile->getAvatar($size);
428 if (!empty($avatar)) {
429 $alink = AvatarLink::fromAvatar($avatar);
431 $alink = new AvatarLink();
432 $alink->type = 'image/png';
433 $alink->height = $size;
434 $alink->width = $size;
435 $alink->url = Avatar::defaultImage($size);
437 if ($size == AVATAR_PROFILE_SIZE) {
438 // Hack for Twitter import: we don't have a 96x96 image,
439 // but we do have a 73x73 image. For now, fake it with that.
440 $avatar = $profile->getAvatar(73);
442 $alink = AvatarLink::fromAvatar($avatar);
443 $alink->height= $size;
444 $alink->width = $size;
449 $object->avatarLinks[] = $alink;
452 if (isset($profile->lat) && isset($profile->lon)) {
453 $object->geopoint = (float)$profile->lat
454 . ' ' . (float)$profile->lon;
457 $object->poco = PoCo::fromProfile($profile);
459 Event::handle('EndActivityObjectFromProfile', array($profile, &$object));
465 static function fromGroup($group)
467 $object = new ActivityObject();
469 if (Event::handle('StartActivityObjectFromGroup', array($group, &$object))) {
471 $object->type = ActivityObject::GROUP;
472 $object->id = $group->getUri();
473 $object->title = $group->getBestName();
474 $object->link = $group->getUri();
476 $object->avatarLinks[] = AvatarLink::fromFilename($group->homepage_logo,
477 AVATAR_PROFILE_SIZE);
479 $object->avatarLinks[] = AvatarLink::fromFilename($group->stream_logo,
482 $object->avatarLinks[] = AvatarLink::fromFilename($group->mini_logo,
485 $object->poco = PoCo::fromGroup($group);
487 Event::handle('EndActivityObjectFromGroup', array($group, &$object));
493 function outputTo($xo, $tag='activity:object')
496 $xo->elementStart($tag);
499 $xo->element('activity:object-type', null, $this->type);
503 if ($tag == 'author') {
504 $xo->element(self::URI, null, $this->id);
506 $xo->element(self::ID, null, $this->id);
509 if (!empty($this->title)) {
510 $name = common_xml_safe_str($this->title);
511 if ($tag == 'author') {
512 $xo->element(self::NAME, null, $name);
514 $xo->element(self::TITLE, null, $name);
518 if (!empty($this->summary)) {
522 common_xml_safe_str($this->summary)
526 if (!empty($this->content)) {
527 // XXX: assuming HTML content here
529 ActivityUtils::CONTENT,
530 array('type' => 'html'),
531 common_xml_safe_str($this->content)
535 if (!empty($this->link)) {
539 'rel' => 'alternate',
540 'type' => 'text/html',
541 'href' => $this->link
547 if ($this->type == ActivityObject::PERSON
548 || $this->type == ActivityObject::GROUP) {
550 foreach ($this->avatarLinks as $avatar) {
554 'type' => $avatar->type,
555 'media:width' => $avatar->width,
556 'media:height' => $avatar->height,
557 'href' => $avatar->url
564 if (!empty($this->geopoint)) {
572 if (!empty($this->poco)) {
573 $this->poco->outputTo($xo);
576 foreach ($this->extra as $el) {
577 list($extraTag, $attrs, $content) = $el;
578 $xo->element($extraTag, $attrs, $content);
582 $xo->elementEnd($tag);
588 function asString($tag='activity:object')
590 $xs = new XMLStringer(true);
592 $this->outputTo($xs, $tag);
594 return $xs->getString();