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 * @copyright 2010 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
27 * @link http://status.net/
30 if (!defined('STATUSNET')) {
35 * Utilities for turning DOMish things into Activityish things
37 * Some common functions that I didn't have the bandwidth to try to factor
38 * into some kind of reasonable superclass, so just dumped here. Might
39 * be useful to have an ActivityObject parent class or something.
43 * @author Evan Prodromou <evan@status.net>
44 * @copyright 2010 StatusNet, Inc.
45 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
46 * @link http://status.net/
51 const ATOM = 'http://www.w3.org/2005/Atom';
59 * Get the permalink for an Activity object
61 * @param DOMElement $element A DOM element
63 * @return string related link, if any
66 static function getPermalink($element)
68 return self::getLink($element, 'alternate', 'text/html');
72 * Get the permalink for an Activity object
74 * @param DOMElement $element A DOM element
76 * @return string related link, if any
79 static function getLink($element, $rel, $type=null)
81 $links = $element->getElementsByTagnameNS(self::ATOM, self::LINK);
83 foreach ($links as $link) {
85 $linkRel = $link->getAttribute(self::REL);
86 $linkType = $link->getAttribute(self::TYPE);
88 if ($linkRel == $rel &&
89 (is_null($type) || $linkType == $type)) {
90 return $link->getAttribute(self::HREF);
98 * Gets the first child element with the given tag
100 * @param DOMElement $element element to pick at
101 * @param string $tag tag to look for
102 * @param string $namespace Namespace to look under
104 * @return DOMElement found element or null
107 static function child($element, $tag, $namespace=self::ATOM)
109 $els = $element->getElementsByTagnameNS($namespace, $tag);
111 if (empty($els) || $els->length == 0) {
114 return $els->item(0);
119 * Grab the text content of a DOM element child of the current element
121 * @param DOMElement $element Element whose children we examine
122 * @param string $tag Tag to look up
123 * @param string $namespace Namespace to use, defaults to Atom
125 * @return string content of the child
128 static function childContent($element, $tag, $namespace=self::ATOM)
130 $el = self::child($element, $tag, $namespace);
135 return $el->textContent;
141 * A noun-ish thing in the activity universe
143 * The activity streams spec talks about activity objects, while also having
144 * a tag activity:object, which is in fact an activity object. Aaaaaah!
146 * This is just a thing in the activity universe. Can be the subject, object,
147 * or indirect object (target!) of an activity verb. Rotten name, and I'm
148 * propagating it. *sigh*
152 * @author Evan Prodromou <evan@status.net>
153 * @copyright 2010 StatusNet, Inc.
154 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
155 * @link http://status.net/
160 const ARTICLE = 'http://activitystrea.ms/schema/1.0/article';
161 const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
162 const NOTE = 'http://activitystrea.ms/schema/1.0/note';
163 const STATUS = 'http://activitystrea.ms/schema/1.0/status';
164 const FILE = 'http://activitystrea.ms/schema/1.0/file';
165 const PHOTO = 'http://activitystrea.ms/schema/1.0/photo';
166 const ALBUM = 'http://activitystrea.ms/schema/1.0/photo-album';
167 const PLAYLIST = 'http://activitystrea.ms/schema/1.0/playlist';
168 const VIDEO = 'http://activitystrea.ms/schema/1.0/video';
169 const AUDIO = 'http://activitystrea.ms/schema/1.0/audio';
170 const BOOKMARK = 'http://activitystrea.ms/schema/1.0/bookmark';
171 const PERSON = 'http://activitystrea.ms/schema/1.0/person';
172 const GROUP = 'http://activitystrea.ms/schema/1.0/group';
173 const PLACE = 'http://activitystrea.ms/schema/1.0/place';
174 const COMMENT = 'http://activitystrea.ms/schema/1.0/comment';
177 // Atom elements we snarf
179 const TITLE = 'title';
180 const SUMMARY = 'summary';
181 const CONTENT = 'content';
183 const SOURCE = 'source';
187 const EMAIL = 'email';
201 * This probably needs to be refactored
202 * to generate a local class (ActivityPerson, ActivityFile, ...)
203 * based on the object type.
205 * @param DOMElement $element DOM thing to turn into an Activity thing
208 function __construct($element)
210 $this->element = $element;
212 if ($element->tagName == 'author') {
214 $this->type = self::PERSON; // XXX: is this fair?
215 $this->title = $this->_childContent($element, self::NAME);
216 $this->id = $this->_childContent($element, self::URI);
218 if (empty($this->id)) {
219 $email = $this->_childContent($element, self::EMAIL);
220 if (!empty($email)) {
222 $this->id = 'mailto:'.$email;
228 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
231 if (empty($this->type)) {
232 $this->type = ActivityObject::NOTE;
235 $this->id = $this->_childContent($element, self::ID);
236 $this->title = $this->_childContent($element, self::TITLE);
237 $this->summary = $this->_childContent($element, self::SUMMARY);
238 $this->content = $this->_childContent($element, self::CONTENT);
240 $this->source = $this->_getSource($element);
242 $this->link = ActivityUtils::getPermalink($element);
244 // XXX: grab PoCo stuff
248 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
250 return ActivityUtils::childContent($element, $tag, $namespace);
253 // Try to get a unique id for the source feed
255 private function _getSource($element)
257 $sourceEl = ActivityUtils::child($element, 'source');
259 if (empty($sourceEl)) {
262 $href = ActivityUtils::getLink($sourceEl, 'self');
266 return ActivityUtils::childContent($sourceEl, 'id');
273 * Utility class to hold a bunch of constant defining default verb types
277 * @author Evan Prodromou <evan@status.net>
278 * @copyright 2010 StatusNet, Inc.
279 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
280 * @link http://status.net/
285 const POST = 'http://activitystrea.ms/schema/1.0/post';
286 const SHARE = 'http://activitystrea.ms/schema/1.0/share';
287 const SAVE = 'http://activitystrea.ms/schema/1.0/save';
288 const FAVORITE = 'http://activitystrea.ms/schema/1.0/favorite';
289 const PLAY = 'http://activitystrea.ms/schema/1.0/play';
290 const FOLLOW = 'http://activitystrea.ms/schema/1.0/follow';
291 const FRIEND = 'http://activitystrea.ms/schema/1.0/make-friend';
292 const JOIN = 'http://activitystrea.ms/schema/1.0/join';
293 const TAG = 'http://activitystrea.ms/schema/1.0/tag';
297 * An activity in the ActivityStrea.ms world
299 * An activity is kind of like a sentence: someone did something
302 * 'someone' is the 'actor'; 'did something' is the verb;
303 * 'something else' is the object.
307 * @author Evan Prodromou <evan@status.net>
308 * @copyright 2010 StatusNet, Inc.
309 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
310 * @link http://status.net/
315 const SPEC = 'http://activitystrea.ms/spec/1.0/';
316 const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
319 const OBJECT = 'object';
320 const ACTOR = 'actor';
321 const SUBJECT = 'subject';
322 const OBJECTTYPE = 'object-type';
323 const CONTEXT = 'context';
324 const TARGET = 'target';
326 const ATOM = 'http://www.w3.org/2005/Atom';
328 const AUTHOR = 'author';
329 const PUBLISHED = 'published';
330 const UPDATED = 'updated';
332 public $actor; // an ActivityObject
333 public $verb; // a string (the URL)
334 public $object; // an ActivityObject
335 public $target; // an ActivityObject
336 public $context; // an ActivityObject
337 public $time; // Time of the activity
338 public $link; // an ActivityObject
339 public $entry; // the source entry
340 public $feed; // the source feed
343 * Turns a regular old Atom <entry> into a magical activity
345 * @param DOMElement $entry Atom entry to poke at
346 * @param DOMElement $feed Atom feed, for context
349 function __construct($entry, $feed = null)
351 $this->entry = $entry;
354 $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
356 if (!empty($pubEl)) {
357 $this->time = strtotime($pubEl->textContent);
359 // XXX technically an error; being liberal. Good idea...?
360 $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
361 if (!empty($updateEl)) {
362 $this->time = strtotime($updateEl->textContent);
368 $this->link = ActivityUtils::getPermalink($entry);
370 $verbEl = $this->_child($entry, self::VERB);
372 if (!empty($verbEl)) {
373 $this->verb = trim($verbEl->textContent);
375 $this->verb = ActivityVerb::POST;
376 // XXX: do other implied stuff here
379 $objectEl = $this->_child($entry, self::OBJECT);
381 if (!empty($objectEl)) {
382 $this->object = new ActivityObject($objectEl);
384 $this->object = new ActivityObject($entry);
387 $actorEl = $this->_child($entry, self::ACTOR);
389 if (!empty($actorEl)) {
391 $this->actor = new ActivityObject($actorEl);
393 } else if (!empty($feed) &&
394 $subjectEl = $this->_child($feed, self::SUBJECT)) {
396 $this->actor = new ActivityObject($subjectEl);
398 } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
400 $this->actor = new ActivityObject($authorEl);
402 } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
405 $this->actor = new ActivityObject($authorEl);
408 $contextEl = $this->_child($entry, self::CONTEXT);
410 if (!empty($contextEl)) {
411 $this->context = new ActivityObject($contextEl);
414 $targetEl = $this->_child($entry, self::TARGET);
416 if (!empty($targetEl)) {
417 $this->target = new ActivityObject($targetEl);
422 * Returns an Atom <entry> based on this activity
424 * @return DOMElement Atom entry
427 function toAtomEntry()
432 private function _child($element, $tag, $namespace=self::SPEC)
434 return ActivityUtils::child($element, $tag, $namespace);