]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/activity.php
Merge branch 'testing' of git@gitorious.org:statusnet/mainline into testing
[quix0rs-gnu-social.git] / plugins / OStatus / lib / activity.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * An activity
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  OStatus
23  * @package   StatusNet
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/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * Utilities for turning DOMish things into Activityish things
36  *
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.
40  *
41  * @category  OStatus
42  * @package   StatusNet
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/
47  */
48
49 class ActivityUtils
50 {
51     const ATOM = 'http://www.w3.org/2005/Atom';
52
53     const LINK = 'link';
54     const REL  = 'rel';
55     const TYPE = 'type';
56     const HREF = 'href';
57
58     /**
59      * Get the permalink for an Activity object
60      *
61      * @param DOMElement $element A DOM element
62      *
63      * @return string related link, if any
64      */
65
66     static function getLink($element)
67     {
68         $links = $element->getElementsByTagnameNS(self::ATOM, self::LINK);
69
70         foreach ($links as $link) {
71
72             $rel = $link->getAttribute(self::REL);
73             $type = $link->getAttribute(self::TYPE);
74
75             if ($rel == 'alternate' && $type == 'text/html') {
76                 return $link->getAttribute(self::HREF);
77             }
78         }
79
80         return null;
81     }
82 }
83
84 /**
85  * A noun-ish thing in the activity universe
86  *
87  * The activity streams spec talks about activity objects, while also having
88  * a tag activity:object, which is in fact an activity object. Aaaaaah!
89  *
90  * This is just a thing in the activity universe. Can be the subject, object,
91  * or indirect object (target!) of an activity verb. Rotten name, and I'm
92  * propagating it. *sigh*
93  *
94  * @category  OStatus
95  * @package   StatusNet
96  * @author    Evan Prodromou <evan@status.net>
97  * @copyright 2010 StatusNet, Inc.
98  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
99  * @link      http://status.net/
100  */
101
102 class ActivityObject
103 {
104     const ARTICLE   = 'http://activitystrea.ms/schema/1.0/article';
105     const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
106     const NOTE      = 'http://activitystrea.ms/schema/1.0/note';
107     const STATUS    = 'http://activitystrea.ms/schema/1.0/status';
108     const FILE      = 'http://activitystrea.ms/schema/1.0/file';
109     const PHOTO     = 'http://activitystrea.ms/schema/1.0/photo';
110     const ALBUM     = 'http://activitystrea.ms/schema/1.0/photo-album';
111     const PLAYLIST  = 'http://activitystrea.ms/schema/1.0/playlist';
112     const VIDEO     = 'http://activitystrea.ms/schema/1.0/video';
113     const AUDIO     = 'http://activitystrea.ms/schema/1.0/audio';
114     const BOOKMARK  = 'http://activitystrea.ms/schema/1.0/bookmark';
115     const PERSON    = 'http://activitystrea.ms/schema/1.0/person';
116     const GROUP     = 'http://activitystrea.ms/schema/1.0/group';
117     const PLACE     = 'http://activitystrea.ms/schema/1.0/place';
118     const COMMENT   = 'http://activitystrea.ms/schema/1.0/comment';
119     // ^^^^^^^^^^ tea!
120
121     // Atom elements we snarf
122
123     const TITLE   = 'title';
124     const SUMMARY = 'summary';
125     const CONTENT = 'content';
126     const ID      = 'id';
127     const SOURCE  = 'source';
128
129     const NAME  = 'name';
130     const URI   = 'uri';
131     const EMAIL = 'email';
132
133     public $type;
134     public $id;
135     public $title;
136     public $summary;
137     public $content;
138     public $link;
139     public $source;
140
141     /**
142      * Constructor
143      *
144      * This probably needs to be refactored
145      * to generate a local class (ActivityPerson, ActivityFile, ...)
146      * based on the object type.
147      *
148      * @param DOMElement $element DOM thing to turn into an Activity thing
149      */
150
151     function __construct($element)
152     {
153         $this->source = $element;
154
155         if ($element->tagName == 'author') {
156
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);
160
161             if (empty($this->id)) {
162                 $email = $this->_childContent($element, self::EMAIL);
163                 if (!empty($email)) {
164                     // XXX: acct: ?
165                     $this->id = 'mailto:'.$email;
166                 }
167             }
168
169         } else {
170
171             $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
172                                                Activity::SPEC);
173
174             if (empty($this->type)) {
175                 $this->type = ActivityObject::NOTE;
176             }
177
178             $this->id      = $this->_childContent($element, self::ID);
179             $this->title   = $this->_childContent($element, self::TITLE);
180             $this->summary = $this->_childContent($element, self::SUMMARY);
181             $this->content = $this->_childContent($element, self::CONTENT);
182             $this->source  = $this->_childContent($element, self::SOURCE);
183
184             $this->link = ActivityUtils::getLink($element);
185
186             // XXX: grab PoCo stuff
187         }
188     }
189
190     /**
191      * Grab the text content of a DOM element child of the current element
192      *
193      * @param DOMElement $element   Element whose children we examine
194      * @param string     $tag       Tag to look up
195      * @param string     $namespace Namespace to use, defaults to Atom
196      *
197      * @return string content of the child
198      */
199
200     private function _childContent($element, $tag, $namespace=Activity::ATOM)
201     {
202         $els = $element->getElementsByTagnameNS($namespace, $tag);
203
204         if (empty($els) || $els->length == 0) {
205             return null;
206         } else {
207             $el = $els->item(0);
208             return $el->textContent;
209         }
210     }
211 }
212
213 /**
214  * Utility class to hold a bunch of constant defining default verb types
215  *
216  * @category  OStatus
217  * @package   StatusNet
218  * @author    Evan Prodromou <evan@status.net>
219  * @copyright 2010 StatusNet, Inc.
220  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
221  * @link      http://status.net/
222  */
223
224 class ActivityVerb
225 {
226     const POST     = 'http://activitystrea.ms/schema/1.0/post';
227     const SHARE    = 'http://activitystrea.ms/schema/1.0/share';
228     const SAVE     = 'http://activitystrea.ms/schema/1.0/save';
229     const FAVORITE = 'http://activitystrea.ms/schema/1.0/favorite';
230     const PLAY     = 'http://activitystrea.ms/schema/1.0/play';
231     const FOLLOW   = 'http://activitystrea.ms/schema/1.0/follow';
232     const FRIEND   = 'http://activitystrea.ms/schema/1.0/make-friend';
233     const JOIN     = 'http://activitystrea.ms/schema/1.0/join';
234     const TAG      = 'http://activitystrea.ms/schema/1.0/tag';
235 }
236
237 /**
238  * An activity in the ActivityStrea.ms world
239  *
240  * An activity is kind of like a sentence: someone did something
241  * to something else.
242  *
243  * 'someone' is the 'actor'; 'did something' is the verb;
244  * 'something else' is the object.
245  *
246  * @category  OStatus
247  * @package   StatusNet
248  * @author    Evan Prodromou <evan@status.net>
249  * @copyright 2010 StatusNet, Inc.
250  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
251  * @link      http://status.net/
252  */
253
254 class Activity
255 {
256     const SPEC   = 'http://activitystrea.ms/spec/1.0/';
257     const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
258
259     const VERB       = 'verb';
260     const OBJECT     = 'object';
261     const ACTOR      = 'actor';
262     const SUBJECT    = 'subject';
263     const OBJECTTYPE = 'object-type';
264     const CONTEXT    = 'context';
265     const TARGET     = 'target';
266
267     const ATOM = 'http://www.w3.org/2005/Atom';
268
269     const AUTHOR    = 'author';
270     const PUBLISHED = 'published';
271     const UPDATED   = 'updated';
272
273     public $actor;   // an ActivityObject
274     public $verb;    // a string (the URL)
275     public $object;  // an ActivityObject
276     public $target;  // an ActivityObject
277     public $context; // an ActivityObject
278     public $time;    // Time of the activity
279     public $link;    // an ActivityObject
280     public $entry;   // the source entry
281     public $feed;    // the source feed
282
283     /**
284      * Turns a regular old Atom <entry> into a magical activity
285      *
286      * @param DOMElement $entry Atom entry to poke at
287      * @param DOMElement $feed  Atom feed, for context
288      */
289
290     function __construct($entry, $feed = null)
291     {
292         $this->entry = $entry;
293         $this->feed  = $feed;
294
295         $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
296
297         if (!empty($pubEl)) {
298             $this->time = strtotime($pubEl->textContent);
299         } else {
300             // XXX technically an error; being liberal. Good idea...?
301             $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
302             if (!empty($updateEl)) {
303                 $this->time = strtotime($updateEl->textContent);
304             } else {
305                 $this->time = null;
306             }
307         }
308
309         $this->link = ActivityUtils::getLink($entry);
310
311         $verbEl = $this->_child($entry, self::VERB);
312
313         if (!empty($verbEl)) {
314             $this->verb = trim($verbEl->textContent);
315         } else {
316             $this->verb = ActivityVerb::POST;
317             // XXX: do other implied stuff here
318         }
319
320         $objectEl = $this->_child($entry, self::OBJECT);
321
322         if (!empty($objectEl)) {
323             $this->object = new ActivityObject($objectEl);
324         } else {
325             $this->object = new ActivityObject($entry);
326         }
327
328         $actorEl = $this->_child($entry, self::ACTOR);
329
330         if (!empty($actorEl)) {
331
332             $this->actor = new ActivityObject($actorEl);
333
334         } else if (!empty($feed) &&
335                    $subjectEl = $this->_child($feed, self::SUBJECT)) {
336
337             $this->actor = new ActivityObject($subjectEl);
338
339         } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
340
341             $this->actor = new ActivityObject($authorEl);
342
343         } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
344                                                               self::ATOM)) {
345
346             $this->actor = new ActivityObject($authorEl);
347         }
348
349         $contextEl = $this->_child($entry, self::CONTEXT);
350
351         if (!empty($contextEl)) {
352             $this->context = new ActivityObject($contextEl);
353         }
354
355         $targetEl = $this->_child($entry, self::TARGET);
356
357         if (!empty($targetEl)) {
358             $this->target = new ActivityObject($targetEl);
359         }
360     }
361
362     /**
363      * Returns an Atom <entry> based on this activity
364      *
365      * @return DOMElement Atom entry
366      */
367
368     function toAtomEntry()
369     {
370         return null;
371     }
372
373     /**
374      * Gets the first child element with the given tag
375      *
376      * @param DOMElement $element   element to pick at
377      * @param string     $tag       tag to look for
378      * @param string     $namespace Namespace to look under
379      *
380      * @return DOMElement found element or null
381      */
382
383     private function _child($element, $tag, $namespace=self::SPEC)
384     {
385         $els = $element->getElementsByTagnameNS($namespace, $tag);
386
387         if (empty($els) || $els->length == 0) {
388             return null;
389         } else {
390             return $els->item(0);
391         }
392     }
393 }