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