]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activityobject.php
Localisation updates from http://translatewiki.net
[quix0rs-gnu-social.git] / lib / activityobject.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  * A noun-ish thing in the activity universe
37  *
38  * The activity streams spec talks about activity objects, while also having
39  * a tag activity:object, which is in fact an activity object. Aaaaaah!
40  *
41  * This is just a thing in the activity universe. Can be the subject, object,
42  * or indirect object (target!) of an activity verb. Rotten name, and I'm
43  * propagating it. *sigh*
44  *
45  * @category  OStatus
46  * @package   StatusNet
47  * @author    Evan Prodromou <evan@status.net>
48  * @copyright 2010 StatusNet, Inc.
49  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
50  * @link      http://status.net/
51  */
52 class ActivityObject
53 {
54     const ARTICLE   = 'http://activitystrea.ms/schema/1.0/article';
55     const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
56     const NOTE      = 'http://activitystrea.ms/schema/1.0/note';
57     const STATUS    = 'http://activitystrea.ms/schema/1.0/status';
58     const FILE      = 'http://activitystrea.ms/schema/1.0/file';
59     const PHOTO     = 'http://activitystrea.ms/schema/1.0/photo';
60     const ALBUM     = 'http://activitystrea.ms/schema/1.0/photo-album';
61     const PLAYLIST  = 'http://activitystrea.ms/schema/1.0/playlist';
62     const VIDEO     = 'http://activitystrea.ms/schema/1.0/video';
63     const AUDIO     = 'http://activitystrea.ms/schema/1.0/audio';
64     const BOOKMARK  = 'http://activitystrea.ms/schema/1.0/bookmark';
65     const PERSON    = 'http://activitystrea.ms/schema/1.0/person';
66     const GROUP     = 'http://activitystrea.ms/schema/1.0/group';
67     const PLACE     = 'http://activitystrea.ms/schema/1.0/place';
68     const COMMENT   = 'http://activitystrea.ms/schema/1.0/comment';
69     // ^^^^^^^^^^ tea!
70
71     // Atom elements we snarf
72
73     const TITLE   = 'title';
74     const SUMMARY = 'summary';
75     const ID      = 'id';
76     const SOURCE  = 'source';
77
78     const NAME  = 'name';
79     const URI   = 'uri';
80     const EMAIL = 'email';
81
82     const POSTEROUS   = 'http://posterous.com/help/rss/1.0';
83     const AUTHOR      = 'author';
84     const USERIMAGE   = 'userImage';
85     const PROFILEURL  = 'profileUrl';
86     const NICKNAME    = 'nickName';
87     const DISPLAYNAME = 'displayName';
88
89     public $element;
90     public $type;
91     public $id;
92     public $title;
93     public $summary;
94     public $content;
95     public $link;
96     public $source;
97     public $avatarLinks = array();
98     public $geopoint;
99     public $poco;
100     public $displayName;
101
102     // @todo move this stuff to it's own PHOTO activity object
103     const MEDIA_DESCRIPTION = 'description';
104
105     public $thumbnail;
106     public $largerImage;
107     public $description;
108
109     /**
110      * Constructor
111      *
112      * This probably needs to be refactored
113      * to generate a local class (ActivityPerson, ActivityFile, ...)
114      * based on the object type.
115      *
116      * @param DOMElement $element DOM thing to turn into an Activity thing
117      */
118     function __construct($element = null)
119     {
120         if (empty($element)) {
121             return;
122         }
123
124         $this->element = $element;
125
126         $this->geopoint = $this->_childContent(
127             $element,
128             ActivityContext::POINT,
129             ActivityContext::GEORSS
130         );
131
132         if ($element->tagName == 'author') {
133             $this->_fromAuthor($element);
134         } else if ($element->tagName == 'item') {
135             $this->_fromRssItem($element);
136         } else {
137             $this->_fromAtomEntry($element);
138         }
139
140         // Some per-type attributes...
141         if ($this->type == self::PERSON || $this->type == self::GROUP) {
142             $this->displayName = $this->title;
143
144             $photos = ActivityUtils::getLinks($element, 'photo');
145             if (count($photos)) {
146                 foreach ($photos as $link) {
147                     $this->avatarLinks[] = new AvatarLink($link);
148                 }
149             } else {
150                 $avatars = ActivityUtils::getLinks($element, 'avatar');
151                 foreach ($avatars as $link) {
152                     $this->avatarLinks[] = new AvatarLink($link);
153                 }
154             }
155
156             $this->poco = new PoCo($element);
157         }
158
159         if ($this->type == self::PHOTO) {
160
161             $this->thumbnail   = ActivityUtils::getLink($element, 'preview');
162             $this->largerImage = ActivityUtils::getLink($element, 'enclosure');
163
164             $this->description = ActivityUtils::childContent(
165                 $element,
166                 ActivityObject::MEDIA_DESCRIPTION,
167                 Activity::MEDIA
168             );
169         }
170     }
171
172     private function _fromAuthor($element)
173     {
174         $this->type  = self::PERSON; // XXX: is this fair?
175         $this->title = $this->_childContent($element, self::NAME);
176
177         $this->id = $this->_childContent($element, self::URI);
178
179         if (empty($this->id)) {
180             $email = $this->_childContent($element, self::EMAIL);
181             if (!empty($email)) {
182                 // XXX: acct: ?
183                 $this->id = 'mailto:'.$email;
184             }
185         }
186     }
187
188     private function _fromAtomEntry($element)
189     {
190         $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
191                                            Activity::SPEC);
192
193         if (empty($this->type)) {
194             $this->type = ActivityObject::NOTE;
195         }
196
197         $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
198         $this->content = ActivityUtils::getContent($element);
199
200         // We don't like HTML in our titles, although it's technically allowed
201
202         $title = ActivityUtils::childHtmlContent($element, self::TITLE);
203
204         $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
205
206         $this->source  = $this->_getSource($element);
207
208         $this->link = ActivityUtils::getPermalink($element);
209
210         $this->id = $this->_childContent($element, self::ID);
211
212         if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
213             $this->id = $this->link;
214         }
215     }
216
217     // @todo FIXME: rationalize with Activity::_fromRssItem()
218     private function _fromRssItem($item)
219     {
220         $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
221
222         $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
223
224         if (!empty($contentEl)) {
225             $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
226         } else {
227             $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
228             if (!empty($descriptionEl)) {
229                 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
230             }
231         }
232
233         $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
234
235         $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
236
237         if (!empty($guidEl)) {
238             $this->id = $guidEl->textContent;
239
240             if ($guidEl->hasAttribute('isPermaLink')) {
241                 // overwrites <link>
242                 $this->link = $this->id;
243             }
244         }
245     }
246
247     public static function fromRssAuthor($el)
248     {
249         $text = $el->textContent;
250
251         if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
252             $email = $match[1];
253             $name = $match[2];
254         } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
255             $name = $match[1];
256             $email = $match[2];
257         } else if (preg_match('/.*@.*/', $text)) {
258             $email = $text;
259             $name = null;
260         } else {
261             $name = $text;
262             $email = null;
263         }
264
265         // Not really enough info
266
267         $obj = new ActivityObject();
268
269         $obj->element = $el;
270
271         $obj->type  = ActivityObject::PERSON;
272         $obj->title = $name;
273
274         if (!empty($email)) {
275             $obj->id = 'mailto:'.$email;
276         }
277
278         return $obj;
279     }
280
281     public static function fromDcCreator($el)
282     {
283         // Not really enough info
284
285         $text = $el->textContent;
286
287         $obj = new ActivityObject();
288
289         $obj->element = $el;
290
291         $obj->title = $text;
292         $obj->type  = ActivityObject::PERSON;
293
294         return $obj;
295     }
296
297     public static function fromRssChannel($el)
298     {
299         $obj = new ActivityObject();
300
301         $obj->element = $el;
302
303         $obj->type = ActivityObject::PERSON; // @fixme guess better
304
305         $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
306         $obj->link  = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
307         $obj->id    = ActivityUtils::getLink($el, Activity::SELF);
308
309         if (empty($obj->id)) {
310             $obj->id = $obj->link;
311         }
312
313         $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
314
315         if (!empty($desc)) {
316             $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
317         }
318
319         $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
320
321         if (!empty($imageEl)) {
322             $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
323             $al = new AvatarLink();
324             $al->url = $url;
325             $obj->avatarLinks[] = $al;
326         }
327
328         return $obj;
329     }
330
331     public static function fromPosterousAuthor($el)
332     {
333         $obj = new ActivityObject();
334
335         $obj->type = ActivityObject::PERSON; // @fixme any others...?
336
337         $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
338
339         if (!empty($userImage)) {
340             $al = new AvatarLink();
341             $al->url = $userImage;
342             $obj->avatarLinks[] = $al;
343         }
344
345         $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
346         $obj->id   = $obj->link;
347
348         $obj->poco = new PoCo();
349
350         $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
351         $obj->poco->displayName       = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
352
353         $obj->title = $obj->poco->displayName;
354
355         return $obj;
356     }
357
358     private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
359     {
360         return ActivityUtils::childContent($element, $tag, $namespace);
361     }
362
363     // Try to get a unique id for the source feed
364
365     private function _getSource($element)
366     {
367         $sourceEl = ActivityUtils::child($element, 'source');
368
369         if (empty($sourceEl)) {
370             return null;
371         } else {
372             $href = ActivityUtils::getLink($sourceEl, 'self');
373             if (!empty($href)) {
374                 return $href;
375             } else {
376                 return ActivityUtils::childContent($sourceEl, 'id');
377             }
378         }
379     }
380
381     static function fromNotice(Notice $notice)
382     {
383         $object = new ActivityObject();
384
385         $object->type    = ActivityObject::NOTE;
386
387         $object->id      = $notice->uri;
388         $object->title   = $notice->content;
389         $object->content = $notice->rendered;
390         $object->link    = $notice->bestUrl();
391
392         return $object;
393     }
394
395     static function fromProfile(Profile $profile)
396     {
397         $object = new ActivityObject();
398
399         $object->type   = ActivityObject::PERSON;
400         $object->id     = $profile->getUri();
401         $object->title  = $profile->getBestName();
402         $object->link   = $profile->profileurl;
403
404         $orig = $profile->getOriginalAvatar();
405
406         if (!empty($orig)) {
407             $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
408         }
409
410         $sizes = array(
411             AVATAR_PROFILE_SIZE,
412             AVATAR_STREAM_SIZE,
413             AVATAR_MINI_SIZE
414         );
415
416         foreach ($sizes as $size) {
417             $alink  = null;
418             $avatar = $profile->getAvatar($size);
419
420             if (!empty($avatar)) {
421                 $alink = AvatarLink::fromAvatar($avatar);
422             } else {
423                 $alink = new AvatarLink();
424                 $alink->type   = 'image/png';
425                 $alink->height = $size;
426                 $alink->width  = $size;
427                 $alink->url    = Avatar::defaultImage($size);
428
429                 if ($size == AVATAR_PROFILE_SIZE) {
430                     // Hack for Twitter import: we don't have a 96x96 image,
431                     // but we do have a 73x73 image. For now, fake it with that.
432                     $avatar = $profile->getAvatar(73);
433                     if ($avatar) {
434                         $alink = AvatarLink::fromAvatar($avatar);
435                         $alink->height= $size;
436                         $alink->width = $size;
437                     }
438                 }
439             }
440
441             $object->avatarLinks[] = $alink;
442         }
443
444         if (isset($profile->lat) && isset($profile->lon)) {
445             $object->geopoint = (float)$profile->lat
446                 . ' ' . (float)$profile->lon;
447         }
448
449         $object->poco = PoCo::fromProfile($profile);
450
451         return $object;
452     }
453
454     static function fromGroup($group)
455     {
456         $object = new ActivityObject();
457
458         $object->type   = ActivityObject::GROUP;
459         $object->id     = $group->getUri();
460         $object->title  = $group->getBestName();
461         $object->link   = $group->getUri();
462
463         $object->avatarLinks[] = AvatarLink::fromFilename(
464             $group->homepage_logo,
465             AVATAR_PROFILE_SIZE
466         );
467
468         $object->avatarLinks[] = AvatarLink::fromFilename(
469             $group->stream_logo,
470             AVATAR_STREAM_SIZE
471         );
472
473         $object->avatarLinks[] = AvatarLink::fromFilename(
474             $group->mini_logo,
475             AVATAR_MINI_SIZE
476         );
477
478         $object->poco = PoCo::fromGroup($group);
479
480         return $object;
481     }
482
483     function asString($tag='activity:object')
484     {
485         $xs = new XMLStringer(true);
486
487         $xs->elementStart($tag);
488
489         $xs->element('activity:object-type', null, $this->type);
490
491         $xs->element(self::ID, null, $this->id);
492
493         if (!empty($this->title)) {
494             $xs->element(
495                 self::TITLE,
496                 null,
497                 common_xml_safe_str($this->title)
498             );
499         }
500
501         if (!empty($this->summary)) {
502             $xs->element(
503                 self::SUMMARY,
504                 null,
505                 common_xml_safe_str($this->summary)
506             );
507         }
508
509         if (!empty($this->content)) {
510             // XXX: assuming HTML content here
511             $xs->element(
512                 ActivityUtils::CONTENT,
513                 array('type' => 'html'),
514                 common_xml_safe_str($this->content)
515             );
516         }
517
518         if (!empty($this->link)) {
519             $xs->element(
520                 'link',
521                 array(
522                     'rel' => 'alternate',
523                     'type' => 'text/html',
524                     'href' => $this->link
525                 ),
526                 null
527             );
528         }
529
530         if ($this->type == ActivityObject::PERSON
531             || $this->type == ActivityObject::GROUP) {
532
533             foreach ($this->avatarLinks as $avatar) {
534                 $xs->element(
535                     'link', array(
536                         'rel'  => 'avatar',
537                         'type'         => $avatar->type,
538                         'media:width'  => $avatar->width,
539                         'media:height' => $avatar->height,
540                         'href' => $avatar->url
541                     ),
542                     null
543                 );
544             }
545         }
546
547         if (!empty($this->geopoint)) {
548             $xs->element(
549                 'georss:point',
550                 null,
551                 $this->geopoint
552             );
553         }
554
555         if (!empty($this->poco)) {
556             $xs->raw($this->poco->asString());
557         }
558
559         $xs->elementEnd($tag);
560
561         return $xs->getString();
562     }
563 }