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);
159 $this->id = $this->_childContent($element, self::URI);
161 if (empty($this->id)) {
162 $email = $this->_childContent($element, self::EMAIL);
163 if (!empty($email)) {
165 $this->id = 'mailto:'.$email;
170 private function _fromAtomEntry($element)
172 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
175 if (empty($this->type)) {
176 $this->type = ActivityObject::NOTE;
179 $this->id = $this->_childContent($element, self::ID);
180 $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
181 $this->content = ActivityUtils::getContent($element);
183 // We don't like HTML in our titles, although it's technically allowed
185 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
187 $this->title = html_entity_decode(strip_tags($title));
189 $this->source = $this->_getSource($element);
191 $this->link = ActivityUtils::getPermalink($element);
194 // @fixme rationalize with Activity::_fromRssItem()
196 private function _fromRssItem($item)
198 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
200 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
202 if (!empty($contentEl)) {
203 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
205 $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
206 if (!empty($descriptionEl)) {
207 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
211 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
213 $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
215 if (!empty($guidEl)) {
216 $this->id = $guidEl->textContent;
218 if ($guidEl->hasAttribute('isPermaLink')) {
220 $this->link = $this->id;
225 public static function fromRssAuthor($el)
227 $text = $el->textContent;
229 if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
232 } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
235 } else if (preg_match('/.*@.*/', $text)) {
243 // Not really enough info
245 $obj = new ActivityObject();
249 $obj->type = ActivityObject::PERSON;
252 if (!empty($email)) {
253 $obj->id = 'mailto:'.$email;
259 public static function fromDcCreator($el)
261 // Not really enough info
263 $text = $el->textContent;
265 $obj = new ActivityObject();
270 $obj->type = ActivityObject::PERSON;
275 public static function fromRssChannel($el)
277 $obj = new ActivityObject();
281 $obj->type = ActivityObject::PERSON; // @fixme guess better
283 $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
284 $obj->link = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
285 $obj->id = ActivityUtils::getLink($el, Activity::SELF);
287 if (empty($obj->id)) {
288 $obj->id = $obj->link;
291 $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
294 $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
297 $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
299 if (!empty($imageEl)) {
300 $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
301 $al = new AvatarLink();
303 $obj->avatarLinks[] = $al;
309 public static function fromPosterousAuthor($el)
311 $obj = new ActivityObject();
313 $obj->type = ActivityObject::PERSON; // @fixme any others...?
315 $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
317 if (!empty($userImage)) {
318 $al = new AvatarLink();
319 $al->url = $userImage;
320 $obj->avatarLinks[] = $al;
323 $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
324 $obj->id = $obj->link;
326 $obj->poco = new PoCo();
328 $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
329 $obj->poco->displayName = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
331 $obj->title = $obj->poco->displayName;
336 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
338 return ActivityUtils::childContent($element, $tag, $namespace);
341 // Try to get a unique id for the source feed
343 private function _getSource($element)
345 $sourceEl = ActivityUtils::child($element, 'source');
347 if (empty($sourceEl)) {
350 $href = ActivityUtils::getLink($sourceEl, 'self');
354 return ActivityUtils::childContent($sourceEl, 'id');
359 static function fromNotice(Notice $notice)
361 $object = new ActivityObject();
363 $object->type = ActivityObject::NOTE;
365 $object->id = $notice->uri;
366 $object->title = $notice->content;
367 $object->content = $notice->rendered;
368 $object->link = $notice->bestUrl();
373 static function fromProfile(Profile $profile)
375 $object = new ActivityObject();
377 $object->type = ActivityObject::PERSON;
378 $object->id = $profile->getUri();
379 $object->title = $profile->getBestName();
380 $object->link = $profile->profileurl;
382 $orig = $profile->getOriginalAvatar();
385 $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
394 foreach ($sizes as $size) {
397 $avatar = $profile->getAvatar($size);
399 if (!empty($avatar)) {
400 $alink = AvatarLink::fromAvatar($avatar);
402 $alink = new AvatarLink();
403 $alink->type = 'image/png';
404 $alink->height = $size;
405 $alink->width = $size;
406 $alink->url = Avatar::defaultImage($size);
409 $object->avatarLinks[] = $alink;
412 if (isset($profile->lat) && isset($profile->lon)) {
413 $object->geopoint = (float)$profile->lat
414 . ' ' . (float)$profile->lon;
417 $object->poco = PoCo::fromProfile($profile);
422 static function fromGroup($group)
424 $object = new ActivityObject();
426 $object->type = ActivityObject::GROUP;
427 $object->id = $group->getUri();
428 $object->title = $group->getBestName();
429 $object->link = $group->getUri();
431 $object->avatarLinks[] = AvatarLink::fromFilename(
432 $group->homepage_logo,
436 $object->avatarLinks[] = AvatarLink::fromFilename(
441 $object->avatarLinks[] = AvatarLink::fromFilename(
446 $object->poco = PoCo::fromGroup($group);
451 function asString($tag='activity:object')
453 $xs = new XMLStringer(true);
455 $xs->elementStart($tag);
457 $xs->element('activity:object-type', null, $this->type);
459 $xs->element(self::ID, null, $this->id);
461 if (!empty($this->title)) {
465 common_xml_safe_str($this->title)
469 if (!empty($this->summary)) {
473 common_xml_safe_str($this->summary)
477 if (!empty($this->content)) {
478 // XXX: assuming HTML content here
480 ActivityUtils::CONTENT,
481 array('type' => 'html'),
482 common_xml_safe_str($this->content)
486 if (!empty($this->link)) {
490 'rel' => 'alternate',
491 'type' => 'text/html',
492 'href' => $this->link
498 if ($this->type == ActivityObject::PERSON
499 || $this->type == ActivityObject::GROUP) {
501 foreach ($this->avatarLinks as $avatar) {
505 'type' => $avatar->type,
506 'media:width' => $avatar->width,
507 'media:height' => $avatar->height,
508 'href' => $avatar->url
515 if (!empty($this->geopoint)) {
523 if (!empty($this->poco)) {
524 $xs->raw($this->poco->asString());
527 $xs->elementEnd($tag);
529 return $xs->getString();