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();
106 * This probably needs to be refactored
107 * to generate a local class (ActivityPerson, ActivityFile, ...)
108 * based on the object type.
110 * @param DOMElement $element DOM thing to turn into an Activity thing
113 function __construct($element = null)
115 if (empty($element)) {
119 $this->element = $element;
121 $this->geopoint = $this->_childContent(
123 ActivityContext::POINT,
124 ActivityContext::GEORSS
127 if ($element->tagName == 'author') {
128 $this->_fromAuthor($element);
129 } else if ($element->tagName == 'item') {
130 $this->_fromRssItem($element);
132 $this->_fromAtomEntry($element);
135 // Some per-type attributes...
136 if ($this->type == self::PERSON || $this->type == self::GROUP) {
137 $this->displayName = $this->title;
139 $photos = ActivityUtils::getLinks($element, 'photo');
140 if (count($photos)) {
141 foreach ($photos as $link) {
142 $this->avatarLinks[] = new AvatarLink($link);
145 $avatars = ActivityUtils::getLinks($element, 'avatar');
146 foreach ($avatars as $link) {
147 $this->avatarLinks[] = new AvatarLink($link);
151 $this->poco = new PoCo($element);
155 private function _fromAuthor($element)
157 $this->type = self::PERSON; // XXX: is this fair?
158 $this->title = $this->_childContent($element, self::NAME);
160 $id = $this->_childContent($element, self::URI);
161 if (ActivityUtils::validateUri($id)) {
165 if (empty($this->id)) {
166 $email = $this->_childContent($element, self::EMAIL);
167 if (!empty($email)) {
169 $this->id = 'mailto:'.$email;
174 private function _fromAtomEntry($element)
176 if ($element->localName == 'actor') {
177 // Old-fashioned <activity:actor>...
178 // First pull anything from <author>, then we'll add on top.
179 $author = ActivityUtils::child($element->parentNode, 'author');
181 $this->_fromAuthor($author);
185 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
188 if (empty($this->type)) {
189 $this->type = ActivityObject::NOTE;
192 $id = $this->_childContent($element, self::ID);
193 if (ActivityUtils::validateUri($id)) {
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));
206 $this->source = $this->_getSource($element);
208 $this->link = ActivityUtils::getPermalink($element);
211 // @fixme rationalize with Activity::_fromRssItem()
213 private function _fromRssItem($item)
215 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
217 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
219 if (!empty($contentEl)) {
220 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
222 $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
223 if (!empty($descriptionEl)) {
224 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
228 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
230 $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
232 if (!empty($guidEl)) {
233 $this->id = $guidEl->textContent;
235 if ($guidEl->hasAttribute('isPermaLink')) {
237 $this->link = $this->id;
242 public static function fromRssAuthor($el)
244 $text = $el->textContent;
246 if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
249 } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
252 } else if (preg_match('/.*@.*/', $text)) {
260 // Not really enough info
262 $obj = new ActivityObject();
266 $obj->type = ActivityObject::PERSON;
269 if (!empty($email)) {
270 $obj->id = 'mailto:'.$email;
276 public static function fromDcCreator($el)
278 // Not really enough info
280 $text = $el->textContent;
282 $obj = new ActivityObject();
287 $obj->type = ActivityObject::PERSON;
292 public static function fromRssChannel($el)
294 $obj = new ActivityObject();
298 $obj->type = ActivityObject::PERSON; // @fixme guess better
300 $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
301 $obj->link = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
302 $obj->id = ActivityUtils::getLink($el, Activity::SELF);
304 if (empty($obj->id)) {
305 $obj->id = $obj->link;
308 $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
311 $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
314 $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
316 if (!empty($imageEl)) {
317 $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
318 $al = new AvatarLink();
320 $obj->avatarLinks[] = $al;
326 public static function fromPosterousAuthor($el)
328 $obj = new ActivityObject();
330 $obj->type = ActivityObject::PERSON; // @fixme any others...?
332 $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
334 if (!empty($userImage)) {
335 $al = new AvatarLink();
336 $al->url = $userImage;
337 $obj->avatarLinks[] = $al;
340 $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
341 $obj->id = $obj->link;
343 $obj->poco = new PoCo();
345 $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
346 $obj->poco->displayName = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
348 $obj->title = $obj->poco->displayName;
353 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
355 return ActivityUtils::childContent($element, $tag, $namespace);
358 // Try to get a unique id for the source feed
360 private function _getSource($element)
362 $sourceEl = ActivityUtils::child($element, 'source');
364 if (empty($sourceEl)) {
367 $href = ActivityUtils::getLink($sourceEl, 'self');
371 return ActivityUtils::childContent($sourceEl, 'id');
376 static function fromNotice(Notice $notice)
378 $object = new ActivityObject();
380 $object->type = ActivityObject::NOTE;
382 $object->id = $notice->uri;
383 $object->title = $notice->content;
384 $object->content = $notice->rendered;
385 $object->link = $notice->bestUrl();
390 static function fromProfile(Profile $profile)
392 $object = new ActivityObject();
394 $object->type = ActivityObject::PERSON;
395 $object->id = $profile->getUri();
396 $object->title = $profile->getBestName();
397 $object->link = $profile->profileurl;
399 $orig = $profile->getOriginalAvatar();
402 $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
411 foreach ($sizes as $size) {
414 $avatar = $profile->getAvatar($size);
416 if (!empty($avatar)) {
417 $alink = AvatarLink::fromAvatar($avatar);
419 $alink = new AvatarLink();
420 $alink->type = 'image/png';
421 $alink->height = $size;
422 $alink->width = $size;
423 $alink->url = Avatar::defaultImage($size);
426 $object->avatarLinks[] = $alink;
429 if (isset($profile->lat) && isset($profile->lon)) {
430 $object->geopoint = (float)$profile->lat
431 . ' ' . (float)$profile->lon;
434 $object->poco = PoCo::fromProfile($profile);
439 static function fromGroup($group)
441 $object = new ActivityObject();
443 $object->type = ActivityObject::GROUP;
444 $object->id = $group->getUri();
445 $object->title = $group->getBestName();
446 $object->link = $group->getUri();
448 $object->avatarLinks[] = AvatarLink::fromFilename(
449 $group->homepage_logo,
453 $object->avatarLinks[] = AvatarLink::fromFilename(
458 $object->avatarLinks[] = AvatarLink::fromFilename(
463 $object->poco = PoCo::fromGroup($group);
468 function asString($tag='activity:object')
470 $xs = new XMLStringer(true);
472 $xs->elementStart($tag);
474 $xs->element('activity:object-type', null, $this->type);
476 $xs->element(self::ID, null, $this->id);
478 if (!empty($this->title)) {
482 common_xml_safe_str($this->title)
486 if (!empty($this->summary)) {
490 common_xml_safe_str($this->summary)
494 if (!empty($this->content)) {
495 // XXX: assuming HTML content here
497 ActivityUtils::CONTENT,
498 array('type' => 'html'),
499 common_xml_safe_str($this->content)
503 if (!empty($this->link)) {
507 'rel' => 'alternate',
508 'type' => 'text/html',
509 'href' => $this->link
515 if ($this->type == ActivityObject::PERSON
516 || $this->type == ActivityObject::GROUP) {
518 foreach ($this->avatarLinks as $avatar) {
522 'type' => $avatar->type,
523 'media:width' => $avatar->width,
524 'media:height' => $avatar->height,
525 'href' => $avatar->url
532 if (!empty($this->geopoint)) {
540 if (!empty($this->poco)) {
541 $xs->raw($this->poco->asString());
544 $xs->elementEnd($tag);
546 return $xs->getString();