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