]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activity.php
b1744e68f585c681d76cab8097ee7c0b58d558b9
[quix0rs-gnu-social.git] / 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  Feed
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Zach Copley <zach@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * An activity in the ActivityStrea.ms world
37  *
38  * An activity is kind of like a sentence: someone did something
39  * to something else.
40  *
41  * 'someone' is the 'actor'; 'did something' is the verb;
42  * 'something else' is the object.
43  *
44  * @category  OStatus
45  * @package   StatusNet
46  * @author    Evan Prodromou <evan@status.net>
47  * @copyright 2010 StatusNet, Inc.
48  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
49  * @link      http://status.net/
50  */
51
52 class Activity
53 {
54     const SPEC   = 'http://activitystrea.ms/spec/1.0/';
55     const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
56
57     const VERB       = 'verb';
58     const OBJECT     = 'object';
59     const ACTOR      = 'actor';
60     const SUBJECT    = 'subject';
61     const OBJECTTYPE = 'object-type';
62     const CONTEXT    = 'context';
63     const TARGET     = 'target';
64
65     const ATOM = 'http://www.w3.org/2005/Atom';
66
67     const AUTHOR    = 'author';
68     const PUBLISHED = 'published';
69     const UPDATED   = 'updated';
70
71     const RSS = null; // no namespace!
72
73     const PUBDATE     = 'pubDate';
74     const DESCRIPTION = 'description';
75     const GUID        = 'guid';
76     const SELF        = 'self';
77     const IMAGE       = 'image';
78     const URL         = 'url';
79
80     const DC = 'http://purl.org/dc/elements/1.1/';
81
82     const CREATOR = 'creator';
83
84     const CONTENTNS = 'http://purl.org/rss/1.0/modules/content/';
85
86     public $actor;   // an ActivityObject
87     public $verb;    // a string (the URL)
88     public $object;  // an ActivityObject
89     public $target;  // an ActivityObject
90     public $context; // an ActivityObject
91     public $time;    // Time of the activity
92     public $link;    // an ActivityObject
93     public $entry;   // the source entry
94     public $feed;    // the source feed
95
96     public $summary; // summary of activity
97     public $content; // HTML content of activity
98     public $id;      // ID of the activity
99     public $title;   // title of the activity
100     public $categories = array(); // list of AtomCategory objects
101     public $enclosures = array(); // list of enclosure URL references
102
103     /**
104      * Turns a regular old Atom <entry> into a magical activity
105      *
106      * @param DOMElement $entry Atom entry to poke at
107      * @param DOMElement $feed  Atom feed, for context
108      */
109
110     function __construct($entry = null, $feed = null)
111     {
112         if (is_null($entry)) {
113             return;
114         }
115
116         // Insist on a feed's root DOMElement; don't allow a DOMDocument
117         if ($feed instanceof DOMDocument) {
118             throw new ClientException(
119                 _("Expecting a root feed element but got a whole XML document.")
120             );
121         }
122
123         $this->entry = $entry;
124         $this->feed  = $feed;
125
126         if ($entry->namespaceURI == Activity::ATOM &&
127             $entry->localName == 'entry') {
128             $this->_fromAtomEntry($entry, $feed);
129         } else if ($entry->namespaceURI == Activity::RSS &&
130                    $entry->localName == 'item') {
131             $this->_fromRssItem($entry, $feed);
132         } else {
133             throw new Exception("Unknown DOM element: {$entry->namespaceURI} {$entry->localName}");
134         }
135     }
136
137     function _fromAtomEntry($entry, $feed)
138     {
139         $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
140
141         if (!empty($pubEl)) {
142             $this->time = strtotime($pubEl->textContent);
143         } else {
144             // XXX technically an error; being liberal. Good idea...?
145             $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
146             if (!empty($updateEl)) {
147                 $this->time = strtotime($updateEl->textContent);
148             } else {
149                 $this->time = null;
150             }
151         }
152
153         $this->link = ActivityUtils::getPermalink($entry);
154
155         $verbEl = $this->_child($entry, self::VERB);
156
157         if (!empty($verbEl)) {
158             $this->verb = trim($verbEl->textContent);
159         } else {
160             $this->verb = ActivityVerb::POST;
161             // XXX: do other implied stuff here
162         }
163
164         $objectEl = $this->_child($entry, self::OBJECT);
165
166         if (!empty($objectEl)) {
167             $this->object = new ActivityObject($objectEl);
168         } else {
169             $this->object = new ActivityObject($entry);
170         }
171
172         $actorEl = $this->_child($entry, self::ACTOR);
173
174         if (!empty($actorEl)) {
175
176             $this->actor = new ActivityObject($actorEl);
177
178         } else if (!empty($feed) &&
179                    $subjectEl = $this->_child($feed, self::SUBJECT)) {
180
181             $this->actor = new ActivityObject($subjectEl);
182
183         } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
184
185             $this->actor = new ActivityObject($authorEl);
186
187         } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
188                                                               self::ATOM)) {
189
190             $this->actor = new ActivityObject($authorEl);
191         }
192
193         $contextEl = $this->_child($entry, self::CONTEXT);
194
195         if (!empty($contextEl)) {
196             $this->context = new ActivityContext($contextEl);
197         } else {
198             $this->context = new ActivityContext($entry);
199         }
200
201         $targetEl = $this->_child($entry, self::TARGET);
202
203         if (!empty($targetEl)) {
204             $this->target = new ActivityObject($targetEl);
205         }
206
207         $this->summary = ActivityUtils::childContent($entry, 'summary');
208         $this->id      = ActivityUtils::childContent($entry, 'id');
209         $this->content = ActivityUtils::getContent($entry);
210
211         $catEls = $entry->getElementsByTagNameNS(self::ATOM, 'category');
212         if ($catEls) {
213             for ($i = 0; $i < $catEls->length; $i++) {
214                 $catEl = $catEls->item($i);
215                 $this->categories[] = new AtomCategory($catEl);
216             }
217         }
218
219         foreach (ActivityUtils::getLinks($entry, 'enclosure') as $link) {
220             $this->enclosures[] = $link->getAttribute('href');
221         }
222     }
223
224     function _fromRssItem($item, $channel)
225     {
226         $verbEl = $this->_child($item, self::VERB);
227
228         if (!empty($verbEl)) {
229             $this->verb = trim($verbEl->textContent);
230         } else {
231             $this->verb = ActivityVerb::POST;
232             // XXX: do other implied stuff here
233         }
234
235         $pubDateEl = $this->_child($item, self::PUBDATE, self::RSS);
236
237         if (!empty($pubDateEl)) {
238             $this->time = strtotime($pubDateEl->textContent);
239         }
240
241         $authorEl = $this->_child($item, self::AUTHOR, self::RSS);
242
243         if (!empty($authorEl)) {
244             $this->actor = ActivityObject::fromRssAuthor($authorEl);
245         } else {
246             $dcCreatorEl = $this->_child($item, self::CREATOR, self::DC);
247             if (!empty($dcCreatorEl)) {
248                 $this->actor = ActivityObject::fromDcCreator($dcCreatorEl);
249             } else if (!empty($channel)) {
250                 $this->actor = ActivityObject::fromRssChannel($channel);
251             }
252         }
253
254         $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, self::RSS);
255
256         $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, self::CONTENTNS);
257
258         if (!empty($contentEl)) {
259             $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
260         } else {
261             $descriptionEl = ActivityUtils::child($item, self::DESCRIPTION, self::RSS);
262             if (!empty($descriptionEl)) {
263                 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
264             }
265         }
266
267         $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, self::RSS);
268
269         // @fixme enclosures
270         // @fixme thumbnails... maybe
271
272         $guidEl = ActivityUtils::child($item, self::GUID, self::RSS);
273
274         if (!empty($guidEl)) {
275             $this->id = $guidEl->textContent;
276
277             if ($guidEl->hasAttribute('isPermaLink') && $guidEl->getAttribute('isPermaLink') != 'false') {
278                 // overwrites <link>
279                 $this->link = $this->id;
280             }
281         }
282
283         $this->object  = new ActivityObject($item);
284         $this->context = new ActivityContext($item);
285     }
286
287     /**
288      * Returns an Atom <entry> based on this activity
289      *
290      * @return DOMElement Atom entry
291      */
292
293     function toAtomEntry()
294     {
295         return null;
296     }
297
298     function asString($namespace=false)
299     {
300         $xs = new XMLStringer(true);
301
302         if ($namespace) {
303             $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
304                            'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
305                            'xmlns:georss' => 'http://www.georss.org/georss',
306                            'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
307                            'xmlns:poco' => 'http://portablecontacts.net/spec/1.0',
308                            'xmlns:media' => 'http://purl.org/syndication/atommedia');
309         } else {
310             $attrs = array();
311         }
312
313         $xs->elementStart('entry', $attrs);
314
315         $xs->element('id', null, $this->id);
316         $xs->element('title', null, $this->title);
317         $xs->element('published', null, common_date_iso8601($this->time));
318         $xs->element('content', array('type' => 'html'), $this->content);
319
320         if (!empty($this->summary)) {
321             $xs->element('summary', null, $this->summary);
322         }
323
324         if (!empty($this->link)) {
325             $xs->element('link', array('rel' => 'alternate',
326                                        'type' => 'text/html'),
327                          $this->link);
328         }
329
330         // XXX: add context
331
332         $xs->elementStart('author');
333         $xs->element('uri', array(), $this->actor->id);
334         if ($this->actor->title) {
335             $xs->element('name', array(), $this->actor->title);
336         }
337         $xs->elementEnd('author');
338         $xs->raw($this->actor->asString('activity:actor'));
339
340         $xs->element('activity:verb', null, $this->verb);
341
342         if ($this->object) {
343             $xs->raw($this->object->asString());
344         }
345
346         if ($this->target) {
347             $xs->raw($this->target->asString('activity:target'));
348         }
349
350         foreach ($this->categories as $cat) {
351             $xs->raw($cat->asString());
352         }
353
354         $xs->elementEnd('entry');
355
356         return $xs->getString();
357     }
358
359     private function _child($element, $tag, $namespace=self::SPEC)
360     {
361         return ActivityUtils::child($element, $tag, $namespace);
362     }
363 }
364
365 class AtomCategory
366 {
367     public $term;
368     public $scheme;
369     public $label;
370
371     function __construct($element=null)
372     {
373         if ($element && $element->attributes) {
374             $this->term = $this->extract($element, 'term');
375             $this->scheme = $this->extract($element, 'scheme');
376             $this->label = $this->extract($element, 'label');
377         }
378     }
379
380     protected function extract($element, $attrib)
381     {
382         $node = $element->attributes->getNamedItemNS(Activity::ATOM, $attrib);
383         if ($node) {
384             return trim($node->textContent);
385         }
386         $node = $element->attributes->getNamedItem($attrib);
387         if ($node) {
388             return trim($node->textContent);
389         }
390         return null;
391     }
392
393     function asString()
394     {
395         $attribs = array();
396         if ($this->term !== null) {
397             $attribs['term'] = $this->term;
398         }
399         if ($this->scheme !== null) {
400             $attribs['scheme'] = $this->scheme;
401         }
402         if ($this->label !== null) {
403             $attribs['label'] = $this->label;
404         }
405         $xs = new XMLStringer();
406         $xs->element('category', $attribs);
407         return $xs->asString();
408     }
409 }