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/
54 const SPEC = 'http://activitystrea.ms/spec/1.0/';
55 const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
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/';
86 public $actor; // an ActivityObject
87 public $verb; // a string (the URL)
88 public $object; // an ActivityObject
89 public $target; // an ActivityObject
90 public $context; // an ActivityObject
91 public $time; // Time of the activity
92 public $link; // an ActivityObject
93 public $entry; // the source entry
94 public $feed; // the source feed
96 public $summary; // summary of activity
97 public $content; // HTML content of activity
98 public $id; // ID of the activity
99 public $title; // title of the activity
100 public $categories = array(); // list of AtomCategory objects
101 public $enclosures = array(); // list of enclosure URL references
104 * Turns a regular old Atom <entry> into a magical activity
106 * @param DOMElement $entry Atom entry to poke at
107 * @param DOMElement $feed Atom feed, for context
110 function __construct($entry = null, $feed = null)
112 if (is_null($entry)) {
116 // Insist on a feed's root DOMElement; don't allow a DOMDocument
117 if ($feed instanceof DOMDocument) {
118 throw new ClientException(
119 _("Expecting a root feed element but got a whole XML document.")
123 $this->entry = $entry;
126 if ($entry->namespaceURI == Activity::ATOM &&
127 $entry->localName == 'entry') {
128 $this->_fromAtomEntry($entry, $feed);
129 } else if ($entry->namespaceURI == Activity::RSS &&
130 $entry->localName == 'item') {
131 $this->_fromRssItem($entry, $feed);
133 throw new Exception("Unknown DOM element: {$entry->namespaceURI} {$entry->localName}");
137 function _fromAtomEntry($entry, $feed)
139 $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
141 if (!empty($pubEl)) {
142 $this->time = strtotime($pubEl->textContent);
144 // XXX technically an error; being liberal. Good idea...?
145 $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
146 if (!empty($updateEl)) {
147 $this->time = strtotime($updateEl->textContent);
153 $this->link = ActivityUtils::getPermalink($entry);
155 $verbEl = $this->_child($entry, self::VERB);
157 if (!empty($verbEl)) {
158 $this->verb = trim($verbEl->textContent);
160 $this->verb = ActivityVerb::POST;
161 // XXX: do other implied stuff here
164 $objectEl = $this->_child($entry, self::OBJECT);
166 if (!empty($objectEl)) {
167 $this->object = new ActivityObject($objectEl);
169 $this->object = new ActivityObject($entry);
172 $actorEl = $this->_child($entry, self::ACTOR);
174 if (!empty($actorEl)) {
176 $this->actor = new ActivityObject($actorEl);
178 } else if (!empty($feed) &&
179 $subjectEl = $this->_child($feed, self::SUBJECT)) {
181 $this->actor = new ActivityObject($subjectEl);
183 } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
185 $this->actor = new ActivityObject($authorEl);
187 } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
190 $this->actor = new ActivityObject($authorEl);
193 $contextEl = $this->_child($entry, self::CONTEXT);
195 if (!empty($contextEl)) {
196 $this->context = new ActivityContext($contextEl);
198 $this->context = new ActivityContext($entry);
201 $targetEl = $this->_child($entry, self::TARGET);
203 if (!empty($targetEl)) {
204 $this->target = new ActivityObject($targetEl);
207 $this->summary = ActivityUtils::childContent($entry, 'summary');
208 $this->id = ActivityUtils::childContent($entry, 'id');
209 $this->content = ActivityUtils::getContent($entry);
211 $catEls = $entry->getElementsByTagNameNS(self::ATOM, 'category');
213 for ($i = 0; $i < $catEls->length; $i++) {
214 $catEl = $catEls->item($i);
215 $this->categories[] = new AtomCategory($catEl);
219 foreach (ActivityUtils::getLinks($entry, 'enclosure') as $link) {
220 $this->enclosures[] = $link->getAttribute('href');
224 function _fromRssItem($item, $channel)
226 $verbEl = $this->_child($item, self::VERB);
228 if (!empty($verbEl)) {
229 $this->verb = trim($verbEl->textContent);
231 $this->verb = ActivityVerb::POST;
232 // XXX: do other implied stuff here
235 $pubDateEl = $this->_child($item, self::PUBDATE, self::RSS);
237 if (!empty($pubDateEl)) {
238 $this->time = strtotime($pubDateEl->textContent);
241 if ($authorEl = $this->_child($item, self::AUTHOR, self::RSS)) {
242 $this->actor = ActivityObject::fromRssAuthor($authorEl);
243 } else if ($dcCreatorEl = $this->_child($item, self::CREATOR, self::DC)) {
244 $this->actor = ActivityObject::fromDcCreator($dcCreatorEl);
245 } else if ($posterousEl = $this->_child($item, ActivityObject::AUTHOR, ActivityObject::POSTEROUS)) {
246 // Special case for Posterous.com
247 $this->actor = ActivityObject::fromPosterousAuthor($posterousEl);
248 } else if (!empty($channel)) {
249 $this->actor = ActivityObject::fromRssChannel($channel);
254 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, self::RSS);
256 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, self::CONTENTNS);
258 if (!empty($contentEl)) {
259 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
261 $descriptionEl = ActivityUtils::child($item, self::DESCRIPTION, self::RSS);
262 if (!empty($descriptionEl)) {
263 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
267 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, self::RSS);
270 // @fixme thumbnails... maybe
272 $guidEl = ActivityUtils::child($item, self::GUID, self::RSS);
274 if (!empty($guidEl)) {
275 $this->id = $guidEl->textContent;
277 if ($guidEl->hasAttribute('isPermaLink') && $guidEl->getAttribute('isPermaLink') != 'false') {
279 $this->link = $this->id;
283 $this->object = new ActivityObject($item);
284 $this->context = new ActivityContext($item);
288 * Returns an Atom <entry> based on this activity
290 * @return DOMElement Atom entry
293 function toAtomEntry()
298 function asString($namespace=false)
300 $xs = new XMLStringer(true);
303 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
304 'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
305 'xmlns:georss' => 'http://www.georss.org/georss',
306 'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
307 'xmlns:poco' => 'http://portablecontacts.net/spec/1.0',
308 'xmlns:media' => 'http://purl.org/syndication/atommedia');
313 $xs->elementStart('entry', $attrs);
315 $xs->element('id', null, $this->id);
316 $xs->element('title', null, $this->title);
317 $xs->element('published', null, common_date_iso8601($this->time));
318 $xs->element('content', array('type' => 'html'), $this->content);
320 if (!empty($this->summary)) {
321 $xs->element('summary', null, $this->summary);
324 if (!empty($this->link)) {
325 $xs->element('link', array('rel' => 'alternate',
326 'type' => 'text/html'),
332 $xs->elementStart('author');
333 $xs->element('uri', array(), $this->actor->id);
334 if ($this->actor->title) {
335 $xs->element('name', array(), $this->actor->title);
337 $xs->elementEnd('author');
338 $xs->raw($this->actor->asString('activity:actor'));
340 $xs->element('activity:verb', null, $this->verb);
343 $xs->raw($this->object->asString());
347 $xs->raw($this->target->asString('activity:target'));
350 foreach ($this->categories as $cat) {
351 $xs->raw($cat->asString());
354 $xs->elementEnd('entry');
356 return $xs->getString();
359 private function _child($element, $tag, $namespace=self::SPEC)
361 return ActivityUtils::child($element, $tag, $namespace);
371 function __construct($element=null)
373 if ($element && $element->attributes) {
374 $this->term = $this->extract($element, 'term');
375 $this->scheme = $this->extract($element, 'scheme');
376 $this->label = $this->extract($element, 'label');
380 protected function extract($element, $attrib)
382 $node = $element->attributes->getNamedItemNS(Activity::ATOM, $attrib);
384 return trim($node->textContent);
386 $node = $element->attributes->getNamedItem($attrib);
388 return trim($node->textContent);
396 if ($this->term !== null) {
397 $attribs['term'] = $this->term;
399 if ($this->scheme !== null) {
400 $attribs['scheme'] = $this->scheme;
402 if ($this->label !== null) {
403 $attribs['label'] = $this->label;
405 $xs = new XMLStringer();
406 $xs->element('category', $attribs);
407 return $xs->asString();