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