]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activity.php
Localisation updates from http://translatewiki.net
[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 class Activity
52 {
53     const SPEC   = 'http://activitystrea.ms/spec/1.0/';
54     const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
55     const MEDIA  = 'http://purl.org/syndication/atommedia';
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     const ENCODED = 'encoded';
86
87     public $actor;   // an ActivityObject
88     public $verb;    // a string (the URL)
89     public $objects = array();  // an array of ActivityObjects
90     public $target;  // an ActivityObject
91     public $context; // an ActivityObject
92     public $time;    // Time of the activity
93     public $link;    // an ActivityObject
94     public $entry;   // the source entry
95     public $feed;    // the source feed
96
97     public $summary; // summary of activity
98     public $content; // HTML content of activity
99     public $id;      // ID of the activity
100     public $title;   // title of the activity
101     public $categories = array(); // list of AtomCategory objects
102     public $enclosures = array(); // list of enclosure URL references
103
104     /**
105      * Turns a regular old Atom <entry> into a magical activity
106      *
107      * @param DOMElement $entry Atom entry to poke at
108      * @param DOMElement $feed  Atom feed, for context
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                 // TRANS: Client exception thrown when a feed instance is a DOMDocument.
120                 _('Expecting a root feed element but got a whole XML document.')
121             );
122         }
123
124         $this->entry = $entry;
125         $this->feed  = $feed;
126
127         if ($entry->namespaceURI == Activity::ATOM &&
128             $entry->localName == 'entry') {
129             $this->_fromAtomEntry($entry, $feed);
130         } else if ($entry->namespaceURI == Activity::RSS &&
131                    $entry->localName == 'item') {
132             $this->_fromRssItem($entry, $feed);
133         } else {
134             // Low level exception. No need for i18n.
135             throw new Exception("Unknown DOM element: {$entry->namespaceURI} {$entry->localName}");
136         }
137     }
138
139     function _fromAtomEntry($entry, $feed)
140     {
141         $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
142
143         if (!empty($pubEl)) {
144             $this->time = strtotime($pubEl->textContent);
145         } else {
146             // XXX technically an error; being liberal. Good idea...?
147             $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
148             if (!empty($updateEl)) {
149                 $this->time = strtotime($updateEl->textContent);
150             } else {
151                 $this->time = null;
152             }
153         }
154
155         $this->link = ActivityUtils::getPermalink($entry);
156
157         $verbEl = $this->_child($entry, self::VERB);
158
159         if (!empty($verbEl)) {
160             $this->verb = trim($verbEl->textContent);
161         } else {
162             $this->verb = ActivityVerb::POST;
163             // XXX: do other implied stuff here
164         }
165
166         $objectEls = $entry->getElementsByTagNameNS(self::SPEC, self::OBJECT);
167
168         if ($objectEls->length > 0) {
169             for ($i = 0; $i < $objectEls->length; $i++) {
170                 $objectEl = $objectEls->item($i);
171                 $this->objects[] = new ActivityObject($objectEl);
172             }
173         } else {
174             $this->objects[] = new ActivityObject($entry);
175         }
176
177         $actorEl = $this->_child($entry, self::ACTOR);
178
179         if (!empty($actorEl)) {
180
181             $this->actor = new ActivityObject($actorEl);
182
183             // Cliqset has bad actor IDs (just nickname of user). We
184             // work around it by getting the author data and using its
185             // id instead
186
187             if (!preg_match('/^\w+:/', $this->actor->id)) {
188                 $authorEl = ActivityUtils::child($entry, 'author');
189                 if (!empty($authorEl)) {
190                     $authorObj = new ActivityObject($authorEl);
191                     $this->actor->id = $authorObj->id;
192                 }
193             }
194         } else if (!empty($feed) &&
195                    $subjectEl = $this->_child($feed, self::SUBJECT)) {
196
197             $this->actor = new ActivityObject($subjectEl);
198
199         } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
200
201             $this->actor = new ActivityObject($authorEl);
202
203         } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
204                                                               self::ATOM)) {
205
206             $this->actor = new ActivityObject($authorEl);
207         }
208
209         $contextEl = $this->_child($entry, self::CONTEXT);
210
211         if (!empty($contextEl)) {
212             $this->context = new ActivityContext($contextEl);
213         } else {
214             $this->context = new ActivityContext($entry);
215         }
216
217         $targetEl = $this->_child($entry, self::TARGET);
218
219         if (!empty($targetEl)) {
220             $this->target = new ActivityObject($targetEl);
221         }
222
223         $this->summary = ActivityUtils::childContent($entry, 'summary');
224         $this->id      = ActivityUtils::childContent($entry, 'id');
225         $this->content = ActivityUtils::getContent($entry);
226
227         $catEls = $entry->getElementsByTagNameNS(self::ATOM, 'category');
228         if ($catEls) {
229             for ($i = 0; $i < $catEls->length; $i++) {
230                 $catEl = $catEls->item($i);
231                 $this->categories[] = new AtomCategory($catEl);
232             }
233         }
234
235         foreach (ActivityUtils::getLinks($entry, 'enclosure') as $link) {
236             $this->enclosures[] = $link->getAttribute('href');
237         }
238     }
239
240     function _fromRssItem($item, $channel)
241     {
242         $verbEl = $this->_child($item, self::VERB);
243
244         if (!empty($verbEl)) {
245             $this->verb = trim($verbEl->textContent);
246         } else {
247             $this->verb = ActivityVerb::POST;
248             // XXX: do other implied stuff here
249         }
250
251         $pubDateEl = $this->_child($item, self::PUBDATE, self::RSS);
252
253         if (!empty($pubDateEl)) {
254             $this->time = strtotime($pubDateEl->textContent);
255         }
256
257         if ($authorEl = $this->_child($item, self::AUTHOR, self::RSS)) {
258             $this->actor = ActivityObject::fromRssAuthor($authorEl);
259         } else if ($dcCreatorEl = $this->_child($item, self::CREATOR, self::DC)) {
260             $this->actor = ActivityObject::fromDcCreator($dcCreatorEl);
261         } else if ($posterousEl = $this->_child($item, ActivityObject::AUTHOR, ActivityObject::POSTEROUS)) {
262             // Special case for Posterous.com
263             $this->actor = ActivityObject::fromPosterousAuthor($posterousEl);
264         } else if (!empty($channel)) {
265             $this->actor = ActivityObject::fromRssChannel($channel);
266         } else {
267             // No actor!
268         }
269
270         $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, self::RSS);
271
272         $contentEl = ActivityUtils::child($item, self::ENCODED, self::CONTENTNS);
273
274         if (!empty($contentEl)) {
275             // <content:encoded> XML node's text content is HTML; no further processing needed.
276             $this->content = $contentEl->textContent;
277         } else {
278             $descriptionEl = ActivityUtils::child($item, self::DESCRIPTION, self::RSS);
279             if (!empty($descriptionEl)) {
280                 // Per spec, <description> must be plaintext.
281                 // In practice, often there's HTML... but these days good
282                 // feeds are using <content:encoded> which is explicitly
283                 // real HTML.
284                 // We'll treat this following spec, and do HTML escaping
285                 // to convert from plaintext to HTML.
286                 $this->content = htmlspecialchars($descriptionEl->textContent);
287             }
288         }
289
290         $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, self::RSS);
291
292         // @fixme enclosures
293         // @fixme thumbnails... maybe
294
295         $guidEl = ActivityUtils::child($item, self::GUID, self::RSS);
296
297         if (!empty($guidEl)) {
298             $this->id = $guidEl->textContent;
299
300             if ($guidEl->hasAttribute('isPermaLink') && $guidEl->getAttribute('isPermaLink') != 'false') {
301                 // overwrites <link>
302                 $this->link = $this->id;
303             }
304         }
305
306         $this->objects[] = new ActivityObject($item);
307         $this->context   = new ActivityContext($item);
308     }
309
310     /**
311      * Returns an Atom <entry> based on this activity
312      *
313      * @return DOMElement Atom entry
314      */
315     function toAtomEntry()
316     {
317         return null;
318     }
319
320     function asString($namespace=false, $author=true)
321     {
322         $xs = new XMLStringer(true);
323
324         if ($namespace) {
325             $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
326                            'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
327                            'xmlns:georss' => 'http://www.georss.org/georss',
328                            'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
329                            'xmlns:poco' => 'http://portablecontacts.net/spec/1.0',
330                            'xmlns:media' => 'http://purl.org/syndication/atommedia');
331         } else {
332             $attrs = array();
333         }
334
335         $xs->elementStart('entry', $attrs);
336
337         $xs->element('id', null, $this->id);
338         $xs->element('title', null, $this->title);
339         $xs->element('published', null, self::iso8601Date($this->time));
340         $xs->element('content', array('type' => 'html'), $this->content);
341
342         if (!empty($this->summary)) {
343             $xs->element('summary', null, $this->summary);
344         }
345
346         if (!empty($this->link)) {
347             $xs->element('link', array('rel' => 'alternate',
348                                        'type' => 'text/html'),
349                          $this->link);
350         }
351
352         // XXX: add context
353
354         if ($author) {
355             $xs->elementStart('author');
356             $xs->element('uri', array(), $this->actor->id);
357             if ($this->actor->title) {
358                 $xs->element('name', array(), $this->actor->title);
359             }
360             $xs->elementEnd('author');
361             $xs->raw($this->actor->asString('activity:actor'));
362         }
363
364         $xs->element('activity:verb', null, $this->verb);
365
366         if (!empty($this->objects)) {
367             foreach($this->objects as $object) {
368                 $xs->raw($object->asString());
369             }
370         }
371
372         if ($this->target) {
373             $xs->raw($this->target->asString('activity:target'));
374         }
375
376         foreach ($this->categories as $cat) {
377             $xs->raw($cat->asString());
378         }
379
380         $xs->elementEnd('entry');
381
382         return $xs->getString();
383     }
384
385     private function _child($element, $tag, $namespace=self::SPEC)
386     {
387         return ActivityUtils::child($element, $tag, $namespace);
388     }
389
390     static function iso8601Date($tm)
391     {
392         $dateStr = date('d F Y H:i:s', $tm);
393         $d = new DateTime($dateStr, new DateTimeZone('UTC'));
394         $d->setTimezone(new DateTimeZone(common_timezone()));
395         return $d->format('c');
396     }
397 }