]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/lib/activity.php
restructure Activity classes to push more DOM stuff to ActivityUtils
[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 getPermalink($element)
67     {
68         return self::getLink($element, 'alternate', 'text/html');
69     }
70
71     /**
72      * Get the permalink for an Activity object
73      *
74      * @param DOMElement $element A DOM element
75      *
76      * @return string related link, if any
77      */
78
79     static function getLink($element, $rel, $type=null)
80     {
81         $links = $element->getElementsByTagnameNS(self::ATOM, self::LINK);
82
83         foreach ($links as $link) {
84
85             $linkRel = $link->getAttribute(self::REL);
86             $linkType = $link->getAttribute(self::TYPE);
87
88             if ($linkRel == $rel &&
89                 (is_null($type) || $linkType == $type)) {
90                 return $link->getAttribute(self::HREF);
91             }
92         }
93
94         return null;
95     }
96
97     /**
98      * Gets the first child element with the given tag
99      *
100      * @param DOMElement $element   element to pick at
101      * @param string     $tag       tag to look for
102      * @param string     $namespace Namespace to look under
103      *
104      * @return DOMElement found element or null
105      */
106
107     static function child($element, $tag, $namespace=self::ATOM)
108     {
109         $els = $element->getElementsByTagnameNS($namespace, $tag);
110
111         if (empty($els) || $els->length == 0) {
112             return null;
113         } else {
114             return $els->item(0);
115         }
116     }
117
118     /**
119      * Grab the text content of a DOM element child of the current element
120      *
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
124      *
125      * @return string content of the child
126      */
127
128     static function childContent($element, $tag, $namespace=self::ATOM)
129     {
130         $el = self::child($element, $tag, $namespace);
131
132         if (empty($el)) {
133             return null;
134         } else {
135             return $el->textContent;
136         }
137     }
138 }
139
140 /**
141  * A noun-ish thing in the activity universe
142  *
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!
145  *
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*
149  *
150  * @category  OStatus
151  * @package   StatusNet
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/
156  */
157
158 class ActivityObject
159 {
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';
175     // ^^^^^^^^^^ tea!
176
177     // Atom elements we snarf
178
179     const TITLE   = 'title';
180     const SUMMARY = 'summary';
181     const CONTENT = 'content';
182     const ID      = 'id';
183     const SOURCE  = 'source';
184
185     const NAME  = 'name';
186     const URI   = 'uri';
187     const EMAIL = 'email';
188
189     public $element;
190     public $type;
191     public $id;
192     public $title;
193     public $summary;
194     public $content;
195     public $link;
196     public $source;
197
198     /**
199      * Constructor
200      *
201      * This probably needs to be refactored
202      * to generate a local class (ActivityPerson, ActivityFile, ...)
203      * based on the object type.
204      *
205      * @param DOMElement $element DOM thing to turn into an Activity thing
206      */
207
208     function __construct($element)
209     {
210         $this->element = $element;
211
212         if ($element->tagName == 'author') {
213
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);
217
218             if (empty($this->id)) {
219                 $email = $this->_childContent($element, self::EMAIL);
220                 if (!empty($email)) {
221                     // XXX: acct: ?
222                     $this->id = 'mailto:'.$email;
223                 }
224             }
225
226         } else {
227
228             $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
229                                                Activity::SPEC);
230
231             if (empty($this->type)) {
232                 $this->type = ActivityObject::NOTE;
233             }
234
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);
239
240             $this->source  = $this->_getSource($element);
241
242             $this->link = ActivityUtils::getPermalink($element);
243
244             // XXX: grab PoCo stuff
245         }
246     }
247
248     private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
249     {
250         return ActivityUtils::childContent($element, $tag, $namespace);
251     }
252
253     // Try to get a unique id for the source feed
254
255     private function _getSource($element)
256     {
257         $sourceEl = ActivityUtils::child($element, 'source');
258
259         if (empty($sourceEl)) {
260             return null;
261         } else {
262             $href = ActivityUtils::getLink($sourceEl, 'self');
263             if (!empty($href)) {
264                 return $href;
265             } else {
266                 return ActivityUtils::childContent($sourceEl, 'id');
267             }
268         }
269     }
270 }
271
272 /**
273  * Utility class to hold a bunch of constant defining default verb types
274  *
275  * @category  OStatus
276  * @package   StatusNet
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/
281  */
282
283 class ActivityVerb
284 {
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';
294 }
295
296 /**
297  * An activity in the ActivityStrea.ms world
298  *
299  * An activity is kind of like a sentence: someone did something
300  * to something else.
301  *
302  * 'someone' is the 'actor'; 'did something' is the verb;
303  * 'something else' is the object.
304  *
305  * @category  OStatus
306  * @package   StatusNet
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/
311  */
312
313 class Activity
314 {
315     const SPEC   = 'http://activitystrea.ms/spec/1.0/';
316     const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
317
318     const VERB       = 'verb';
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';
325
326     const ATOM = 'http://www.w3.org/2005/Atom';
327
328     const AUTHOR    = 'author';
329     const PUBLISHED = 'published';
330     const UPDATED   = 'updated';
331
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
341
342     /**
343      * Turns a regular old Atom <entry> into a magical activity
344      *
345      * @param DOMElement $entry Atom entry to poke at
346      * @param DOMElement $feed  Atom feed, for context
347      */
348
349     function __construct($entry, $feed = null)
350     {
351         $this->entry = $entry;
352         $this->feed  = $feed;
353
354         $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
355
356         if (!empty($pubEl)) {
357             $this->time = strtotime($pubEl->textContent);
358         } else {
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);
363             } else {
364                 $this->time = null;
365             }
366         }
367
368         $this->link = ActivityUtils::getPermalink($entry);
369
370         $verbEl = $this->_child($entry, self::VERB);
371
372         if (!empty($verbEl)) {
373             $this->verb = trim($verbEl->textContent);
374         } else {
375             $this->verb = ActivityVerb::POST;
376             // XXX: do other implied stuff here
377         }
378
379         $objectEl = $this->_child($entry, self::OBJECT);
380
381         if (!empty($objectEl)) {
382             $this->object = new ActivityObject($objectEl);
383         } else {
384             $this->object = new ActivityObject($entry);
385         }
386
387         $actorEl = $this->_child($entry, self::ACTOR);
388
389         if (!empty($actorEl)) {
390
391             $this->actor = new ActivityObject($actorEl);
392
393         } else if (!empty($feed) &&
394                    $subjectEl = $this->_child($feed, self::SUBJECT)) {
395
396             $this->actor = new ActivityObject($subjectEl);
397
398         } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
399
400             $this->actor = new ActivityObject($authorEl);
401
402         } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
403                                                               self::ATOM)) {
404
405             $this->actor = new ActivityObject($authorEl);
406         }
407
408         $contextEl = $this->_child($entry, self::CONTEXT);
409
410         if (!empty($contextEl)) {
411             $this->context = new ActivityObject($contextEl);
412         }
413
414         $targetEl = $this->_child($entry, self::TARGET);
415
416         if (!empty($targetEl)) {
417             $this->target = new ActivityObject($targetEl);
418         }
419     }
420
421     /**
422      * Returns an Atom <entry> based on this activity
423      *
424      * @return DOMElement Atom entry
425      */
426
427     function toAtomEntry()
428     {
429         return null;
430     }
431
432     private function _child($element, $tag, $namespace=self::SPEC)
433     {
434         return ActivityUtils::child($element, $tag, $namespace);
435     }
436 }