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';
91 public $avatarLinks = array();
99 * This probably needs to be refactored
100 * to generate a local class (ActivityPerson, ActivityFile, ...)
101 * based on the object type.
103 * @param DOMElement $element DOM thing to turn into an Activity thing
106 function __construct($element = null)
108 if (empty($element)) {
112 $this->element = $element;
114 $this->geopoint = $this->_childContent(
116 ActivityContext::POINT,
117 ActivityContext::GEORSS
120 if ($element->tagName == 'author') {
121 $this->_fromAuthor($element);
122 } else if ($element->tagName == 'item') {
123 $this->_fromRssItem($element);
125 $this->_fromAtomEntry($element);
128 // Some per-type attributes...
129 if ($this->type == self::PERSON || $this->type == self::GROUP) {
130 $this->displayName = $this->title;
132 $photos = ActivityUtils::getLinks($element, 'photo');
133 if (count($photos)) {
134 foreach ($photos as $link) {
135 $this->avatarLinks[] = new AvatarLink($link);
138 $avatars = ActivityUtils::getLinks($element, 'avatar');
139 foreach ($avatars as $link) {
140 $this->avatarLinks[] = new AvatarLink($link);
144 $this->poco = new PoCo($element);
148 private function _fromAuthor($element)
150 $this->type = self::PERSON; // XXX: is this fair?
151 $this->title = $this->_childContent($element, self::NAME);
152 $this->id = $this->_childContent($element, self::URI);
154 if (empty($this->id)) {
155 $email = $this->_childContent($element, self::EMAIL);
156 if (!empty($email)) {
158 $this->id = 'mailto:'.$email;
163 private function _fromAtomEntry($element)
165 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
168 if (empty($this->type)) {
169 $this->type = ActivityObject::NOTE;
172 $this->id = $this->_childContent($element, self::ID);
173 $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
174 $this->content = ActivityUtils::getContent($element);
176 // We don't like HTML in our titles, although it's technically allowed
178 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
180 $this->title = html_entity_decode(strip_tags($title));
182 $this->source = $this->_getSource($element);
184 $this->link = ActivityUtils::getPermalink($element);
187 // @fixme rationalize with Activity::_fromRssItem()
189 private function _fromRssItem($item)
191 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
193 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
195 if (!empty($contentEl)) {
196 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
198 $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
199 if (!empty($descriptionEl)) {
200 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
204 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
206 $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
208 if (!empty($guidEl)) {
209 $this->id = $guidEl->textContent;
211 if ($guidEl->hasAttribute('isPermaLink')) {
213 $this->link = $this->id;
218 public static function fromRssAuthor($el)
220 $text = $el->textContent;
222 if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
225 } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
228 } else if (preg_match('/.*@.*/', $text)) {
236 // Not really enough info
238 $obj = new ActivityObject();
242 $obj->type = ActivityObject::PERSON;
245 if (!empty($email)) {
246 $obj->id = 'mailto:'.$email;
252 public static function fromDcCreator($el)
254 // Not really enough info
256 $text = $el->textContent;
258 $obj = new ActivityObject();
263 $obj->type = ActivityObject::PERSON;
268 public static function fromRssChannel($el)
270 $obj = new ActivityObject();
274 $obj->type = ActivityObject::PERSON; // @fixme guess better
276 $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
277 $obj->link = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
278 $obj->id = ActivityUtils::getLink($el, Activity::SELF);
280 if (empty($obj->id)) {
281 $obj->id = $obj->link;
284 $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
287 $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
290 $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
292 if (!empty($imageEl)) {
293 $obj->avatarLinks[] = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
299 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
301 return ActivityUtils::childContent($element, $tag, $namespace);
304 // Try to get a unique id for the source feed
306 private function _getSource($element)
308 $sourceEl = ActivityUtils::child($element, 'source');
310 if (empty($sourceEl)) {
313 $href = ActivityUtils::getLink($sourceEl, 'self');
317 return ActivityUtils::childContent($sourceEl, 'id');
322 static function fromNotice(Notice $notice)
324 $object = new ActivityObject();
326 $object->type = ActivityObject::NOTE;
328 $object->id = $notice->uri;
329 $object->title = $notice->content;
330 $object->content = $notice->rendered;
331 $object->link = $notice->bestUrl();
336 static function fromProfile(Profile $profile)
338 $object = new ActivityObject();
340 $object->type = ActivityObject::PERSON;
341 $object->id = $profile->getUri();
342 $object->title = $profile->getBestName();
343 $object->link = $profile->profileurl;
345 $orig = $profile->getOriginalAvatar();
348 $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
357 foreach ($sizes as $size) {
360 $avatar = $profile->getAvatar($size);
362 if (!empty($avatar)) {
363 $alink = AvatarLink::fromAvatar($avatar);
365 $alink = new AvatarLink();
366 $alink->type = 'image/png';
367 $alink->height = $size;
368 $alink->width = $size;
369 $alink->url = Avatar::defaultImage($size);
372 $object->avatarLinks[] = $alink;
375 if (isset($profile->lat) && isset($profile->lon)) {
376 $object->geopoint = (float)$profile->lat
377 . ' ' . (float)$profile->lon;
380 $object->poco = PoCo::fromProfile($profile);
385 static function fromGroup($group)
387 $object = new ActivityObject();
389 $object->type = ActivityObject::GROUP;
390 $object->id = $group->getUri();
391 $object->title = $group->getBestName();
392 $object->link = $group->getUri();
394 $object->avatarLinks[] = AvatarLink::fromFilename(
395 $group->homepage_logo,
399 $object->avatarLinks[] = AvatarLink::fromFilename(
404 $object->avatarLinks[] = AvatarLink::fromFilename(
409 $object->poco = PoCo::fromGroup($group);
414 function asString($tag='activity:object')
416 $xs = new XMLStringer(true);
418 $xs->elementStart($tag);
420 $xs->element('activity:object-type', null, $this->type);
422 $xs->element(self::ID, null, $this->id);
424 if (!empty($this->title)) {
428 common_xml_safe_str($this->title)
432 if (!empty($this->summary)) {
436 common_xml_safe_str($this->summary)
440 if (!empty($this->content)) {
441 // XXX: assuming HTML content here
443 ActivityUtils::CONTENT,
444 array('type' => 'html'),
445 common_xml_safe_str($this->content)
449 if (!empty($this->link)) {
453 'rel' => 'alternate',
454 'type' => 'text/html',
455 'href' => $this->link
461 if ($this->type == ActivityObject::PERSON
462 || $this->type == ActivityObject::GROUP) {
464 foreach ($this->avatarLinks as $avatar) {
468 'type' => $avatar->type,
469 'media:width' => $avatar->width,
470 'media:height' => $avatar->height,
471 'href' => $avatar->url
478 if (!empty($this->geopoint)) {
486 if (!empty($this->poco)) {
487 $xs->raw($this->poco->asString());
490 $xs->elementEnd($tag);
492 return $xs->getString();