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->childNodes;
110 if (empty($els) || $els->length == 0) {
113 for ($i = 0; $i < $els->length; $i++) {
114 $el = $els->item($i);
115 if ($el->localName == $tag && $el->namespaceURI == $namespace) {
123 * Grab the text content of a DOM element child of the current element
125 * @param DOMElement $element Element whose children we examine
126 * @param string $tag Tag to look up
127 * @param string $namespace Namespace to use, defaults to Atom
129 * @return string content of the child
132 static function childContent($element, $tag, $namespace=self::ATOM)
134 $el = self::child($element, $tag, $namespace);
139 return $el->textContent;
145 * A noun-ish thing in the activity universe
147 * The activity streams spec talks about activity objects, while also having
148 * a tag activity:object, which is in fact an activity object. Aaaaaah!
150 * This is just a thing in the activity universe. Can be the subject, object,
151 * or indirect object (target!) of an activity verb. Rotten name, and I'm
152 * propagating it. *sigh*
156 * @author Evan Prodromou <evan@status.net>
157 * @copyright 2010 StatusNet, Inc.
158 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
159 * @link http://status.net/
164 const ARTICLE = 'http://activitystrea.ms/schema/1.0/article';
165 const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
166 const NOTE = 'http://activitystrea.ms/schema/1.0/note';
167 const STATUS = 'http://activitystrea.ms/schema/1.0/status';
168 const FILE = 'http://activitystrea.ms/schema/1.0/file';
169 const PHOTO = 'http://activitystrea.ms/schema/1.0/photo';
170 const ALBUM = 'http://activitystrea.ms/schema/1.0/photo-album';
171 const PLAYLIST = 'http://activitystrea.ms/schema/1.0/playlist';
172 const VIDEO = 'http://activitystrea.ms/schema/1.0/video';
173 const AUDIO = 'http://activitystrea.ms/schema/1.0/audio';
174 const BOOKMARK = 'http://activitystrea.ms/schema/1.0/bookmark';
175 const PERSON = 'http://activitystrea.ms/schema/1.0/person';
176 const GROUP = 'http://activitystrea.ms/schema/1.0/group';
177 const PLACE = 'http://activitystrea.ms/schema/1.0/place';
178 const COMMENT = 'http://activitystrea.ms/schema/1.0/comment';
181 // Atom elements we snarf
183 const TITLE = 'title';
184 const SUMMARY = 'summary';
185 const CONTENT = 'content';
187 const SOURCE = 'source';
191 const EMAIL = 'email';
205 * This probably needs to be refactored
206 * to generate a local class (ActivityPerson, ActivityFile, ...)
207 * based on the object type.
209 * @param DOMElement $element DOM thing to turn into an Activity thing
212 function __construct($element)
214 $this->element = $element;
216 if ($element->tagName == 'author') {
218 $this->type = self::PERSON; // XXX: is this fair?
219 $this->title = $this->_childContent($element, self::NAME);
220 $this->id = $this->_childContent($element, self::URI);
222 if (empty($this->id)) {
223 $email = $this->_childContent($element, self::EMAIL);
224 if (!empty($email)) {
226 $this->id = 'mailto:'.$email;
232 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
235 if (empty($this->type)) {
236 $this->type = ActivityObject::NOTE;
239 $this->id = $this->_childContent($element, self::ID);
240 $this->title = $this->_childContent($element, self::TITLE);
241 $this->summary = $this->_childContent($element, self::SUMMARY);
242 $this->content = $this->_childContent($element, self::CONTENT);
244 $this->source = $this->_getSource($element);
246 $this->link = ActivityUtils::getPermalink($element);
248 // XXX: grab PoCo stuff
251 // Some per-type attributes...
252 if ($this->type == self::PERSON || $this->type == self::GROUP) {
253 $this->displayName = $this->title;
255 // @fixme we may have multiple avatars with different resolutions specified
256 $this->avatar = ActivityUtils::getLink($element, 'avatar');
260 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
262 return ActivityUtils::childContent($element, $tag, $namespace);
265 // Try to get a unique id for the source feed
267 private function _getSource($element)
269 $sourceEl = ActivityUtils::child($element, 'source');
271 if (empty($sourceEl)) {
274 $href = ActivityUtils::getLink($sourceEl, 'self');
278 return ActivityUtils::childContent($sourceEl, 'id');
285 * Utility class to hold a bunch of constant defining default verb types
289 * @author Evan Prodromou <evan@status.net>
290 * @copyright 2010 StatusNet, Inc.
291 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
292 * @link http://status.net/
297 const POST = 'http://activitystrea.ms/schema/1.0/post';
298 const SHARE = 'http://activitystrea.ms/schema/1.0/share';
299 const SAVE = 'http://activitystrea.ms/schema/1.0/save';
300 const FAVORITE = 'http://activitystrea.ms/schema/1.0/favorite';
301 const PLAY = 'http://activitystrea.ms/schema/1.0/play';
302 const FOLLOW = 'http://activitystrea.ms/schema/1.0/follow';
303 const FRIEND = 'http://activitystrea.ms/schema/1.0/make-friend';
304 const JOIN = 'http://activitystrea.ms/schema/1.0/join';
305 const TAG = 'http://activitystrea.ms/schema/1.0/tag';
309 * An activity in the ActivityStrea.ms world
311 * An activity is kind of like a sentence: someone did something
314 * 'someone' is the 'actor'; 'did something' is the verb;
315 * 'something else' is the object.
319 * @author Evan Prodromou <evan@status.net>
320 * @copyright 2010 StatusNet, Inc.
321 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
322 * @link http://status.net/
327 const SPEC = 'http://activitystrea.ms/spec/1.0/';
328 const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
331 const OBJECT = 'object';
332 const ACTOR = 'actor';
333 const SUBJECT = 'subject';
334 const OBJECTTYPE = 'object-type';
335 const CONTEXT = 'context';
336 const TARGET = 'target';
338 const ATOM = 'http://www.w3.org/2005/Atom';
340 const AUTHOR = 'author';
341 const PUBLISHED = 'published';
342 const UPDATED = 'updated';
344 public $actor; // an ActivityObject
345 public $verb; // a string (the URL)
346 public $object; // an ActivityObject
347 public $target; // an ActivityObject
348 public $context; // an ActivityObject
349 public $time; // Time of the activity
350 public $link; // an ActivityObject
351 public $entry; // the source entry
352 public $feed; // the source feed
355 * Turns a regular old Atom <entry> into a magical activity
357 * @param DOMElement $entry Atom entry to poke at
358 * @param DOMElement $feed Atom feed, for context
361 function __construct($entry, $feed = null)
363 $this->entry = $entry;
366 $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
368 if (!empty($pubEl)) {
369 $this->time = strtotime($pubEl->textContent);
371 // XXX technically an error; being liberal. Good idea...?
372 $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
373 if (!empty($updateEl)) {
374 $this->time = strtotime($updateEl->textContent);
380 $this->link = ActivityUtils::getPermalink($entry);
382 $verbEl = $this->_child($entry, self::VERB);
384 if (!empty($verbEl)) {
385 $this->verb = trim($verbEl->textContent);
387 $this->verb = ActivityVerb::POST;
388 // XXX: do other implied stuff here
391 $objectEl = $this->_child($entry, self::OBJECT);
393 if (!empty($objectEl)) {
394 $this->object = new ActivityObject($objectEl);
396 $this->object = new ActivityObject($entry);
399 $actorEl = $this->_child($entry, self::ACTOR);
401 if (!empty($actorEl)) {
403 $this->actor = new ActivityObject($actorEl);
405 } else if (!empty($feed) &&
406 $subjectEl = $this->_child($feed, self::SUBJECT)) {
408 $this->actor = new ActivityObject($subjectEl);
410 } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
412 $this->actor = new ActivityObject($authorEl);
414 } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
417 $this->actor = new ActivityObject($authorEl);
420 $contextEl = $this->_child($entry, self::CONTEXT);
422 if (!empty($contextEl)) {
423 $this->context = new ActivityObject($contextEl);
426 $targetEl = $this->_child($entry, self::TARGET);
428 if (!empty($targetEl)) {
429 $this->target = new ActivityObject($targetEl);
434 * Returns an Atom <entry> based on this activity
436 * @return DOMElement Atom entry
439 function toAtomEntry()
444 private function _child($element, $tag, $namespace=self::SPEC)
446 return ActivityUtils::child($element, $tag, $namespace);