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 * An activity in the ActivityStrea.ms world
38 * An activity is kind of like a sentence: someone did something
41 * 'someone' is the 'actor'; 'did something' is the verb;
42 * 'something else' is the object.
46 * @author Evan Prodromou <evan@status.net>
47 * @copyright 2010 StatusNet, Inc.
48 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
49 * @link http://status.net/
53 const SPEC = 'http://activitystrea.ms/spec/1.0/';
54 const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
55 const MEDIA = 'http://purl.org/syndication/atommedia';
58 const OBJECT = 'object';
59 const ACTOR = 'actor';
60 const SUBJECT = 'subject';
61 const OBJECTTYPE = 'object-type';
62 const CONTEXT = 'context';
63 const TARGET = 'target';
65 const ATOM = 'http://www.w3.org/2005/Atom';
67 const AUTHOR = 'author';
68 const PUBLISHED = 'published';
69 const UPDATED = 'updated';
71 const RSS = null; // no namespace!
73 const PUBDATE = 'pubDate';
74 const DESCRIPTION = 'description';
77 const IMAGE = 'image';
80 const DC = 'http://purl.org/dc/elements/1.1/';
82 const CREATOR = 'creator';
84 const CONTENTNS = 'http://purl.org/rss/1.0/modules/content/';
85 const ENCODED = 'encoded';
87 public $actor; // an ActivityObject
88 public $verb; // a string (the URL)
89 public $objects = array(); // an array of ActivityObjects
90 public $target; // an ActivityObject
91 public $context; // an ActivityObject
92 public $time; // Time of the activity
93 public $link; // an ActivityObject
94 public $entry; // the source entry
95 public $feed; // the source feed
97 public $summary; // summary of activity
98 public $content; // HTML content of activity
99 public $id; // ID of the activity
100 public $title; // title of the activity
101 public $categories = array(); // list of AtomCategory objects
102 public $enclosures = array(); // list of enclosure URL references
104 public $extra = array(); // extra elements as array(tag, attrs, content)
105 public $source; // ActivitySource object representing 'home feed'
106 public $selfLink; // <link rel='self' type='application/atom+xml'>
107 public $editLink; // <link rel='edit' type='application/atom+xml'>
110 * Turns a regular old Atom <entry> into a magical activity
112 * @param DOMElement $entry Atom entry to poke at
113 * @param DOMElement $feed Atom feed, for context
115 function __construct($entry = null, $feed = null)
117 if (is_null($entry)) {
121 // Insist on a feed's root DOMElement; don't allow a DOMDocument
122 if ($feed instanceof DOMDocument) {
123 throw new ClientException(
124 // TRANS: Client exception thrown when a feed instance is a DOMDocument.
125 _('Expecting a root feed element but got a whole XML document.')
129 $this->entry = $entry;
132 if ($entry->namespaceURI == Activity::ATOM &&
133 $entry->localName == 'entry') {
134 $this->_fromAtomEntry($entry, $feed);
135 } else if ($entry->namespaceURI == Activity::RSS &&
136 $entry->localName == 'item') {
137 $this->_fromRssItem($entry, $feed);
139 // Low level exception. No need for i18n.
140 throw new Exception("Unknown DOM element: {$entry->namespaceURI} {$entry->localName}");
144 function _fromAtomEntry($entry, $feed)
146 $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
148 if (!empty($pubEl)) {
149 $this->time = strtotime($pubEl->textContent);
151 // XXX technically an error; being liberal. Good idea...?
152 $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
153 if (!empty($updateEl)) {
154 $this->time = strtotime($updateEl->textContent);
160 $this->link = ActivityUtils::getPermalink($entry);
162 $verbEl = $this->_child($entry, self::VERB);
164 if (!empty($verbEl)) {
165 $this->verb = trim($verbEl->textContent);
167 $this->verb = ActivityVerb::POST;
168 // XXX: do other implied stuff here
171 $objectEls = $entry->getElementsByTagNameNS(self::SPEC, self::OBJECT);
173 if ($objectEls->length > 0) {
174 for ($i = 0; $i < $objectEls->length; $i++) {
175 $objectEl = $objectEls->item($i);
176 $this->objects[] = new ActivityObject($objectEl);
179 $this->objects[] = new ActivityObject($entry);
182 $actorEl = $this->_child($entry, self::ACTOR);
184 if (!empty($actorEl)) {
185 // Standalone <activity:actor> elements are a holdover from older
186 // versions of ActivityStreams. Newer feeds should have this data
187 // integrated straight into <atom:author>.
189 $this->actor = new ActivityObject($actorEl);
191 // Cliqset has bad actor IDs (just nickname of user). We
192 // work around it by getting the author data and using its
195 if (!preg_match('/^\w+:/', $this->actor->id)) {
196 $authorEl = ActivityUtils::child($entry, 'author');
197 if (!empty($authorEl)) {
198 $authorObj = new ActivityObject($authorEl);
199 $this->actor->id = $authorObj->id;
202 } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
204 // An <atom:author> in the entry overrides any author info on
205 // the surrounding feed.
206 $this->actor = new ActivityObject($authorEl);
208 } else if (!empty($feed) &&
209 $subjectEl = $this->_child($feed, self::SUBJECT)) {
211 // Feed subject is used for things like groups.
212 // Should actually possibly not be interpreted as an actor...?
213 $this->actor = new ActivityObject($subjectEl);
215 } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
218 // If there's no <atom:author> on the entry, it's safe to assume
219 // the containing feed's authorship info applies.
220 $this->actor = new ActivityObject($authorEl);
223 $contextEl = $this->_child($entry, self::CONTEXT);
225 if (!empty($contextEl)) {
226 $this->context = new ActivityContext($contextEl);
228 $this->context = new ActivityContext($entry);
231 $targetEl = $this->_child($entry, self::TARGET);
233 if (!empty($targetEl)) {
234 $this->target = new ActivityObject($targetEl);
237 $this->summary = ActivityUtils::childContent($entry, 'summary');
238 $this->id = ActivityUtils::childContent($entry, 'id');
239 $this->content = ActivityUtils::getContent($entry);
241 $catEls = $entry->getElementsByTagNameNS(self::ATOM, 'category');
243 for ($i = 0; $i < $catEls->length; $i++) {
244 $catEl = $catEls->item($i);
245 $this->categories[] = new AtomCategory($catEl);
249 foreach (ActivityUtils::getLinks($entry, 'enclosure') as $link) {
250 $this->enclosures[] = $link->getAttribute('href');
253 // From APP. Might be useful.
255 $this->selfLink = ActivityUtils::getLink($entry, 'self', 'application/atom+xml');
256 $this->editLink = ActivityUtils::getLink($entry, 'edit', 'application/atom+xml');
259 function _fromRssItem($item, $channel)
261 $verbEl = $this->_child($item, self::VERB);
263 if (!empty($verbEl)) {
264 $this->verb = trim($verbEl->textContent);
266 $this->verb = ActivityVerb::POST;
267 // XXX: do other implied stuff here
270 $pubDateEl = $this->_child($item, self::PUBDATE, self::RSS);
272 if (!empty($pubDateEl)) {
273 $this->time = strtotime($pubDateEl->textContent);
276 if ($authorEl = $this->_child($item, self::AUTHOR, self::RSS)) {
277 $this->actor = ActivityObject::fromRssAuthor($authorEl);
278 } else if ($dcCreatorEl = $this->_child($item, self::CREATOR, self::DC)) {
279 $this->actor = ActivityObject::fromDcCreator($dcCreatorEl);
280 } else if ($posterousEl = $this->_child($item, ActivityObject::AUTHOR, ActivityObject::POSTEROUS)) {
281 // Special case for Posterous.com
282 $this->actor = ActivityObject::fromPosterousAuthor($posterousEl);
283 } else if (!empty($channel)) {
284 $this->actor = ActivityObject::fromRssChannel($channel);
289 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, self::RSS);
291 $contentEl = ActivityUtils::child($item, self::ENCODED, self::CONTENTNS);
293 if (!empty($contentEl)) {
294 // <content:encoded> XML node's text content is HTML; no further processing needed.
295 $this->content = $contentEl->textContent;
297 $descriptionEl = ActivityUtils::child($item, self::DESCRIPTION, self::RSS);
298 if (!empty($descriptionEl)) {
299 // Per spec, <description> must be plaintext.
300 // In practice, often there's HTML... but these days good
301 // feeds are using <content:encoded> which is explicitly
303 // We'll treat this following spec, and do HTML escaping
304 // to convert from plaintext to HTML.
305 $this->content = htmlspecialchars($descriptionEl->textContent);
309 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, self::RSS);
312 // @fixme thumbnails... maybe
314 $guidEl = ActivityUtils::child($item, self::GUID, self::RSS);
316 if (!empty($guidEl)) {
317 $this->id = $guidEl->textContent;
319 if ($guidEl->hasAttribute('isPermaLink') && $guidEl->getAttribute('isPermaLink') != 'false') {
321 $this->link = $this->id;
325 $this->objects[] = new ActivityObject($item);
326 $this->context = new ActivityContext($item);
330 * Returns an Atom <entry> based on this activity
332 * @return DOMElement Atom entry
335 function toAtomEntry()
341 * Returns an array based on this activity suitable
342 * for encoding as a JSON object
344 * @return array $activity
352 $activity['actor'] = $this->actor->asArray();
355 $activity['body'] = $this->content;
357 // generator <-- We could use this when we know a notice is created
358 // locally. Or if we know the upstream Generator.
360 // icon <-- I've decided to use the posting user's stream avatar here
361 // for now (also included in the avatarLinks extension)
365 if ($this->verb == ActivityVerb::POST && count($this->objects) == 1) {
366 $activity['object'] = $this->objects[0]->asArray();
368 // Context stuff. For now I'm just sticking most of it
369 // in a property called "context"
371 if (!empty($this->context)) {
373 if (!empty($this->context->location)) {
374 $loc = $this->context->location;
378 $activity['geopoint'] = array(
380 'coordinates' => array($loc->lat, $loc->lon)
385 $activity['to'] = $this->context->getToArray();
386 $activity['context'] = $this->context->asArray();
389 // Instead of adding enclosures as an extension to JSON
390 // Activities, it seems like we should be using the
391 // attachedObjects property of ActivityObject
393 $attachedObjects = array();
395 // XXX: OK, this is kinda cheating. We should probably figure out
396 // what kind of objects these are based on mime-type and then
397 // create specific object types. Right now this rely on
398 // duck-typing. Also, we should include an embed code for
399 // video attachments.
401 foreach ($this->enclosures as $enclosure) {
403 if (is_string($enclosure)) {
405 $attachedObjects[]['id'] = $enclosure;
409 $attachedObjects[]['id'] = $enclosure->url;
411 $mediaLink = new ActivityStreamsMediaLink(
416 // XXX: Add 'size' as an extension to MediaLink?
419 $attachedObjects[]['mediaLink'] = $mediaLink->asArray(); // extension
421 if ($enclosure->title) {
422 $attachedObjects[]['displayName'] = $enclosure->title;
427 if (!empty($attachedObjects)) {
428 $activity['object']['attachedObjects'] = $attachedObjects;
432 $activity['object'] = array();
433 foreach($this->objects as $object) {
434 $activity['object'][] = $object->asArray();
438 $activity['postedTime'] = self::iso8601Date($this->time); // Change to exactly be RFC3339?
442 'objectType' => 'service',
443 'displayName' => common_config('site', 'name'),
444 'url' => common_root_url()
447 $activity['provider'] = $provider;
450 if (!empty($this->target)) {
451 $activity['target'] = $this->target->asArray();
455 $activity['title'] = $this->title;
457 // updatedTime <-- Should we use this to indicate the time we received
458 // a remote notice? Probably not.
462 // We can probably use the whole schema URL here but probably the
463 // relative simple name is easier to parse
464 $activity['verb'] = substr($this->verb, strrpos($this->verb, '/') + 1);
466 /* Purely extensions hereafter */
470 // Use an Activity Object for term? Which object? Note?
471 foreach ($this->categories as $cat) {
472 $tags[] = $cat->term;
475 $activity['tags'] = $tags;
477 // XXX: a bit of a hack... Since JSON isn't namespaced we probably
478 // shouldn't be using 'statusnet:notice_info', but this will work
481 foreach ($this->extra as $e) {
482 list($objectName, $props, $txt) = $e;
483 if (!empty($objectName)) {
484 $activity[$objectName] = $props;
488 return array_filter($activity);
491 function asString($namespace=false, $author=true, $source=false)
493 $xs = new XMLStringer(true);
494 $this->outputTo($xs, $namespace, $author, $source);
495 return $xs->getString();
498 function outputTo($xs, $namespace=false, $author=true, $source=false)
501 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
502 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0',
503 'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
504 'xmlns:georss' => 'http://www.georss.org/georss',
505 'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
506 'xmlns:poco' => 'http://portablecontacts.net/spec/1.0',
507 'xmlns:media' => 'http://purl.org/syndication/atommedia',
508 'xmlns:statusnet' => 'http://status.net/schema/api/1/');
513 $xs->elementStart('entry', $attrs);
515 if ($this->verb == ActivityVerb::POST && count($this->objects) == 1) {
517 $obj = $this->objects[0];
518 $obj->outputTo($xs, null);
521 $xs->element('id', null, $this->id);
522 $xs->element('title', null, $this->title);
524 $xs->element('content', array('type' => 'html'), $this->content);
526 if (!empty($this->summary)) {
527 $xs->element('summary', null, $this->summary);
530 if (!empty($this->link)) {
531 $xs->element('link', array('rel' => 'alternate',
532 'type' => 'text/html'),
538 $xs->element('activity:verb', null, $this->verb);
540 $published = self::iso8601Date($this->time);
542 $xs->element('published', null, $published);
543 $xs->element('updated', null, $published);
546 $this->actor->outputTo($xs, 'author');
548 // XXX: Remove <activity:actor> ASAP! Author information
549 // has been moved to the author element in the Activity
550 // Streams spec. We're outputting actor only for backward
551 // compatibility with clients that can only parse
552 // activities based on older versions of the spec.
554 $depMsg = 'Deprecation warning: activity:actor is present '
555 . 'only for backward compatibility. It will be '
556 . 'removed in the next version of StatusNet.';
557 $xs->comment($depMsg);
558 $this->actor->outputTo($xs, 'activity:actor');
561 if ($this->verb != ActivityVerb::POST || count($this->objects) != 1) {
562 foreach($this->objects as $object) {
563 $object->outputTo($xs, 'activity:object');
567 if (!empty($this->context)) {
569 if (!empty($this->context->replyToID)) {
570 if (!empty($this->context->replyToUrl)) {
571 $xs->element('thr:in-reply-to',
572 array('ref' => $this->context->replyToID,
573 'href' => $this->context->replyToUrl));
575 $xs->element('thr:in-reply-to',
576 array('ref' => $this->context->replyToID));
580 if (!empty($this->context->replyToUrl)) {
581 $xs->element('link', array('rel' => 'related',
582 'href' => $this->context->replyToUrl));
585 if (!empty($this->context->conversation)) {
586 $xs->element('link', array('rel' => 'ostatus:conversation',
587 'href' => $this->context->conversation));
590 foreach ($this->context->attention as $attnURI) {
591 $xs->element('link', array('rel' => 'ostatus:attention',
592 'href' => $attnURI));
593 $xs->element('link', array('rel' => 'mentioned',
594 'href' => $attnURI));
597 // XXX: shoulda used ActivityVerb::SHARE
599 if (!empty($this->context->forwardID)) {
600 if (!empty($this->context->forwardUrl)) {
601 $xs->element('ostatus:forward',
602 array('ref' => $this->context->forwardID,
603 'href' => $this->context->forwardUrl));
605 $xs->element('ostatus:forward',
606 array('ref' => $this->context->forwardID));
610 if (!empty($this->context->location)) {
611 $loc = $this->context->location;
612 $xs->element('georss:point', null, $loc->lat . ' ' . $loc->lon);
617 $this->target->outputTo($xs, 'activity:target');
620 foreach ($this->categories as $cat) {
624 // can be either URLs or enclosure objects
626 foreach ($this->enclosures as $enclosure) {
627 if (is_string($enclosure)) {
628 $xs->element('link', array('rel' => 'enclosure',
629 'href' => $enclosure));
631 $attributes = array('rel' => 'enclosure',
632 'href' => $enclosure->url,
633 'type' => $enclosure->mimetype,
634 'length' => $enclosure->size);
635 if ($enclosure->title) {
636 $attributes['title'] = $enclosure->title;
638 $xs->element('link', $attributes);
642 // Info on the source feed
644 if ($source && !empty($this->source)) {
645 $xs->elementStart('source');
647 $xs->element('id', null, $this->source->id);
648 $xs->element('title', null, $this->source->title);
650 if (array_key_exists('alternate', $this->source->links)) {
651 $xs->element('link', array('rel' => 'alternate',
652 'type' => 'text/html',
653 'href' => $this->source->links['alternate']));
656 if (array_key_exists('self', $this->source->links)) {
657 $xs->element('link', array('rel' => 'self',
658 'type' => 'application/atom+xml',
659 'href' => $this->source->links['self']));
662 if (array_key_exists('license', $this->source->links)) {
663 $xs->element('link', array('rel' => 'license',
664 'href' => $this->source->links['license']));
667 if (!empty($this->source->icon)) {
668 $xs->element('icon', null, $this->source->icon);
671 if (!empty($this->source->updated)) {
672 $xs->element('updated', null, $this->source->updated);
675 $xs->elementEnd('source');
678 if (!empty($this->selfLink)) {
679 $xs->element('link', array('rel' => 'self',
680 'type' => 'application/atom+xml',
681 'href' => $this->selfLink));
684 if (!empty($this->editLink)) {
685 $xs->element('link', array('rel' => 'edit',
686 'type' => 'application/atom+xml',
687 'href' => $this->editLink));
690 // For throwing in extra elements; used for statusnet:notice_info
692 foreach ($this->extra as $el) {
693 list($tag, $attrs, $content) = $el;
694 $xs->element($tag, $attrs, $content);
697 $xs->elementEnd('entry');
702 private function _child($element, $tag, $namespace=self::SPEC)
704 return ActivityUtils::child($element, $tag, $namespace);
707 static function iso8601Date($tm)
709 $dateStr = date('d F Y H:i:s', $tm);
710 $d = new DateTime($dateStr, new DateTimeZone('UTC'));
711 $d->setTimezone(new DateTimeZone(common_timezone()));
712 return $d->format('c');