]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activityobject.php
Better handling of null values in ActivityObject::mimeTypeToObjectType
[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 _LIST     = 'http://activitystrea.ms/schema/1.0/list'; // LIST is reserved
68     const PLACE     = 'http://activitystrea.ms/schema/1.0/place';
69     const COMMENT   = 'http://activitystrea.ms/schema/1.0/comment';
70     // ^^^^^^^^^^ tea!
71     const ACTIVITY = 'http://activitystrea.ms/schema/1.0/activity';
72     const SERVICE   = 'http://activitystrea.ms/schema/1.0/service';
73     const IMAGE     = 'http://activitystrea.ms/schema/1.0/image';
74
75     // Atom elements we snarf
76
77     const TITLE   = 'title';
78     const SUMMARY = 'summary';
79     const ID      = 'id';
80     const SOURCE  = 'source';
81
82     const NAME  = 'name';
83     const URI   = 'uri';
84     const EMAIL = 'email';
85
86     const POSTEROUS   = 'http://posterous.com/help/rss/1.0';
87     const AUTHOR      = 'author';
88     const USERIMAGE   = 'userImage';
89     const PROFILEURL  = 'profileUrl';
90     const NICKNAME    = 'nickName';
91     const DISPLAYNAME = 'displayName';
92
93     public $element;
94     public $type;
95     public $id;
96     public $title;
97     public $summary;
98     public $content;
99     public $owner;
100     public $link;
101     public $source;
102     public $avatarLinks = array();
103     public $geopoint;
104     public $poco;
105     public $displayName;
106
107     // @todo move this stuff to it's own PHOTO activity object
108     const MEDIA_DESCRIPTION = 'description';
109
110     public $thumbnail;
111     public $largerImage;
112     public $description;
113     public $extra = array();
114
115     public $stream;
116
117     /**
118      * Constructor
119      *
120      * This probably needs to be refactored
121      * to generate a local class (ActivityPerson, ActivityFile, ...)
122      * based on the object type.
123      *
124      * @param DOMElement $element DOM thing to turn into an Activity thing
125      */
126     function __construct($element = null)
127     {
128         if (empty($element)) {
129             return;
130         }
131
132         $this->element = $element;
133
134         $this->geopoint = $this->_childContent(
135             $element,
136             ActivityContext::POINT,
137             ActivityContext::GEORSS
138         );
139
140         if ($element->tagName == 'author') {
141             $this->_fromAuthor($element);
142         } else if ($element->tagName == 'item') {
143             $this->_fromRssItem($element);
144         } else {
145             $this->_fromAtomEntry($element);
146         }
147
148         // Some per-type attributes...
149         if ($this->type == self::PERSON || $this->type == self::GROUP) {
150             $this->displayName = $this->title;
151
152             $photos = ActivityUtils::getLinks($element, 'photo');
153             if (count($photos)) {
154                 foreach ($photos as $link) {
155                     $this->avatarLinks[] = new AvatarLink($link);
156                 }
157             } else {
158                 $avatars = ActivityUtils::getLinks($element, 'avatar');
159                 foreach ($avatars as $link) {
160                     $this->avatarLinks[] = new AvatarLink($link);
161                 }
162             }
163
164             $this->poco = new PoCo($element);
165         }
166
167         if ($this->type == self::PHOTO) {
168
169             $this->thumbnail   = ActivityUtils::getLink($element, 'preview');
170             $this->largerImage = ActivityUtils::getLink($element, 'enclosure');
171
172             $this->description = ActivityUtils::childContent(
173                 $element,
174                 ActivityObject::MEDIA_DESCRIPTION,
175                 Activity::MEDIA
176             );
177         }
178         if ($this->type == self::_LIST) {
179             $owner = ActivityUtils::child($this->element, Activity::AUTHOR, Activity::SPEC);
180             $this->owner = new ActivityObject($owner);
181         }
182     }
183
184     private function _fromAuthor($element)
185     {
186         $this->type = $this->_childContent($element,
187                                            Activity::OBJECTTYPE,
188                                            Activity::SPEC);
189
190         if (empty($this->type)) {
191             $this->type = self::PERSON; // XXX: is this fair?
192         }
193
194         // start with <atom:title>
195
196         $title = ActivityUtils::childHtmlContent($element, self::TITLE);
197
198         if (!empty($title)) {
199             $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
200         }
201
202         // fall back to <atom:name>
203
204         if (empty($this->title)) {
205             $this->title = $this->_childContent($element, self::NAME);
206         }
207
208         // start with <atom:id>
209
210         $this->id = $this->_childContent($element, self::ID);
211
212         // fall back to <atom:uri>
213
214         if (empty($this->id)) {
215             $this->id = $this->_childContent($element, self::URI);
216         }
217
218         // fall further back to <atom:email>
219
220         if (empty($this->id)) {
221             $email = $this->_childContent($element, self::EMAIL);
222             if (!empty($email)) {
223                 // XXX: acct: ?
224                 $this->id = 'mailto:'.$email;
225             }
226         }
227
228         $this->link = ActivityUtils::getPermalink($element);
229
230         // fall finally back to <link rel=alternate>
231
232         if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
233             $this->id = $this->link;
234         }
235     }
236
237     private function _fromAtomEntry($element)
238     {
239         $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
240                                            Activity::SPEC);
241
242         if (empty($this->type)) {
243             $this->type = ActivityObject::NOTE;
244         }
245
246         $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
247         $this->content = ActivityUtils::getContent($element);
248
249         // We don't like HTML in our titles, although it's technically allowed
250
251         $title = ActivityUtils::childHtmlContent($element, self::TITLE);
252
253         $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
254
255         $this->source  = $this->_getSource($element);
256
257         $this->link = ActivityUtils::getPermalink($element);
258
259         $this->id = $this->_childContent($element, self::ID);
260
261         if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
262             $this->id = $this->link;
263         }
264     }
265
266     // @todo FIXME: rationalize with Activity::_fromRssItem()
267     private function _fromRssItem($item)
268     {
269         $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
270
271         $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
272
273         if (!empty($contentEl)) {
274             $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
275         } else {
276             $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
277             if (!empty($descriptionEl)) {
278                 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
279             }
280         }
281
282         $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
283
284         $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
285
286         if (!empty($guidEl)) {
287             $this->id = $guidEl->textContent;
288
289             if ($guidEl->hasAttribute('isPermaLink')) {
290                 // overwrites <link>
291                 $this->link = $this->id;
292             }
293         }
294     }
295
296     public static function fromRssAuthor($el)
297     {
298         $text = $el->textContent;
299
300         if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
301             $email = $match[1];
302             $name = $match[2];
303         } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
304             $name = $match[1];
305             $email = $match[2];
306         } else if (preg_match('/.*@.*/', $text)) {
307             $email = $text;
308             $name = null;
309         } else {
310             $name = $text;
311             $email = null;
312         }
313
314         // Not really enough info
315
316         $obj = new ActivityObject();
317
318         $obj->element = $el;
319
320         $obj->type  = ActivityObject::PERSON;
321         $obj->title = $name;
322
323         if (!empty($email)) {
324             $obj->id = 'mailto:'.$email;
325         }
326
327         return $obj;
328     }
329
330     public static function fromDcCreator($el)
331     {
332         // Not really enough info
333
334         $text = $el->textContent;
335
336         $obj = new ActivityObject();
337
338         $obj->element = $el;
339
340         $obj->title = $text;
341         $obj->type  = ActivityObject::PERSON;
342
343         return $obj;
344     }
345
346     public static function fromRssChannel($el)
347     {
348         $obj = new ActivityObject();
349
350         $obj->element = $el;
351
352         $obj->type = ActivityObject::PERSON; // @fixme guess better
353
354         $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
355         $obj->link  = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
356         $obj->id    = ActivityUtils::getLink($el, Activity::SELF);
357
358         if (empty($obj->id)) {
359             $obj->id = $obj->link;
360         }
361
362         $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
363
364         if (!empty($desc)) {
365             $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
366         }
367
368         $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
369
370         if (!empty($imageEl)) {
371             $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
372             $al = new AvatarLink();
373             $al->url = $url;
374             $obj->avatarLinks[] = $al;
375         }
376
377         return $obj;
378     }
379
380     public static function fromPosterousAuthor($el)
381     {
382         $obj = new ActivityObject();
383
384         $obj->type = ActivityObject::PERSON; // @fixme any others...?
385
386         $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
387
388         if (!empty($userImage)) {
389             $al = new AvatarLink();
390             $al->url = $userImage;
391             $obj->avatarLinks[] = $al;
392         }
393
394         $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
395         $obj->id   = $obj->link;
396
397         $obj->poco = new PoCo();
398
399         $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
400         $obj->poco->displayName       = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
401
402         $obj->title = $obj->poco->displayName;
403
404         return $obj;
405     }
406
407     private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
408     {
409         return ActivityUtils::childContent($element, $tag, $namespace);
410     }
411
412     // Try to get a unique id for the source feed
413
414     private function _getSource($element)
415     {
416         $sourceEl = ActivityUtils::child($element, 'source');
417
418         if (empty($sourceEl)) {
419             return null;
420         } else {
421             $href = ActivityUtils::getLink($sourceEl, 'self');
422             if (!empty($href)) {
423                 return $href;
424             } else {
425                 return ActivityUtils::childContent($sourceEl, 'id');
426             }
427         }
428     }
429
430     static function fromNotice(Notice $notice)
431     {
432         $object = new ActivityObject();
433
434         if (Event::handle('StartActivityObjectFromNotice', array($notice, &$object))) {
435
436             $object->type    = (empty($notice->object_type)) ? ActivityObject::NOTE : $notice->object_type;
437
438             $object->id      = $notice->uri;
439             $object->content = $notice->rendered;
440             $object->link    = $notice->bestUrl();
441
442             Event::handle('EndActivityObjectFromNotice', array($notice, &$object));
443         }
444
445         return $object;
446     }
447
448     static function fromProfile(Profile $profile)
449     {
450         $object = new ActivityObject();
451
452         if (Event::handle('StartActivityObjectFromProfile', array($profile, &$object))) {
453             $object->type   = ActivityObject::PERSON;
454             $object->id     = $profile->getUri();
455             $object->title  = $profile->getBestName();
456             $object->link   = $profile->profileurl;
457
458             $orig = $profile->getOriginalAvatar();
459
460             if (!empty($orig)) {
461                 $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
462             }
463
464             $sizes = array(
465                 AVATAR_PROFILE_SIZE,
466                 AVATAR_STREAM_SIZE,
467                 AVATAR_MINI_SIZE
468             );
469
470             foreach ($sizes as $size) {
471                 $alink  = null;
472                 $avatar = $profile->getAvatar($size);
473
474                 if (!empty($avatar)) {
475                     $alink = AvatarLink::fromAvatar($avatar);
476                 } else {
477                     $alink = new AvatarLink();
478                     $alink->type   = 'image/png';
479                     $alink->height = $size;
480                     $alink->width  = $size;
481                     $alink->url    = Avatar::defaultImage($size);
482
483                     if ($size == AVATAR_PROFILE_SIZE) {
484                         // Hack for Twitter import: we don't have a 96x96 image,
485                         // but we do have a 73x73 image. For now, fake it with that.
486                         $avatar = $profile->getAvatar(73);
487                         if ($avatar) {
488                             $alink = AvatarLink::fromAvatar($avatar);
489                             $alink->height= $size;
490                             $alink->width = $size;
491                         }
492                     }
493                 }
494
495                 $object->avatarLinks[] = $alink;
496             }
497
498             if (isset($profile->lat) && isset($profile->lon)) {
499                 $object->geopoint = (float)$profile->lat
500                     . ' ' . (float)$profile->lon;
501             }
502
503             $object->poco = PoCo::fromProfile($profile);
504
505             Event::handle('EndActivityObjectFromProfile', array($profile, &$object));
506         }
507
508         return $object;
509     }
510
511     static function fromGroup($group)
512     {
513         $object = new ActivityObject();
514
515         if (Event::handle('StartActivityObjectFromGroup', array($group, &$object))) {
516
517             $object->type   = ActivityObject::GROUP;
518             $object->id     = $group->getUri();
519             $object->title  = $group->getBestName();
520             $object->link   = $group->getUri();
521
522             $object->avatarLinks[] = AvatarLink::fromFilename($group->homepage_logo,
523                                                               AVATAR_PROFILE_SIZE);
524
525             $object->avatarLinks[] = AvatarLink::fromFilename($group->stream_logo,
526                                                               AVATAR_STREAM_SIZE);
527
528             $object->avatarLinks[] = AvatarLink::fromFilename($group->mini_logo,
529                                                               AVATAR_MINI_SIZE);
530
531             $object->poco = PoCo::fromGroup($group);
532             Event::handle('EndActivityObjectFromGroup', array($group, &$object));
533         }
534
535         return $object;
536     }
537
538     static function fromPeopletag($ptag)
539     {
540         $object = new ActivityObject();
541         if (Event::handle('StartActivityObjectFromPeopletag', array($ptag, &$object))) {
542             $object->type    = ActivityObject::_LIST;
543
544             $object->id      = $ptag->getUri();
545             $object->title   = $ptag->tag;
546             $object->summary = $ptag->description;
547             $object->link    = $ptag->homeUrl();
548             $object->owner   = Profile::staticGet('id', $ptag->tagger);
549             $object->poco    = PoCo::fromProfile($object->owner);
550             Event::handle('EndActivityObjectFromPeopletag', array($ptag, &$object));
551         }
552         return $object;
553     }
554
555     static function fromFile(File $file)
556     {
557         $object = new ActivityObject();
558
559         if (Event::handle('StartActivityObjectFromFile', array($file, &$object))) {
560
561             $object->type = self::mimeTypeToObjectType($file->mimetype);
562             $object->id   = TagURI::mint(sprintf("file:%d", $file->id));
563             $object->link = common_local_url('attachment', array('id' => $file->id));
564
565             if ($file->title) {
566                 $object->title = $file->title;
567             }
568
569             if ($file->date) {
570                 $object->date = $file->date;
571             }
572
573             $thumbnail = $file->getThumbnail();
574
575             if (!empty($thumbnail)) {
576                 $object->thumbnail = $thumbnail;
577             }
578
579             switch ($object->type) {
580             case ActivityObject::IMAGE:
581                 $object->largerImage = $file->url;
582                 break;
583             case ActivityObject::VIDEO:
584             case ActivityObject::AUDIO:
585                 $object->stream = $file->url;
586                 break;
587             }
588
589             Event::handle('EndActivityObjectFromFile', array($file, &$object));
590         }
591
592         return $object;
593     }
594
595     function outputTo($xo, $tag='activity:object')
596     {
597         if (!empty($tag)) {
598             $xo->elementStart($tag);
599         }
600
601         if (Event::handle('StartActivityObjectOutputAtom', array($this, $xo))) {
602             $xo->element('activity:object-type', null, $this->type);
603
604             // <author> uses URI
605
606             if ($tag == 'author') {
607                 $xo->element(self::URI, null, $this->id);
608             } else {
609                 $xo->element(self::ID, null, $this->id);
610             }
611
612             if (!empty($this->title)) {
613                 $name = common_xml_safe_str($this->title);
614                 if ($tag == 'author') {
615                     // XXX: Backward compatibility hack -- atom:name should contain
616                     // full name here, instead of nickname, i.e.: $name. Change
617                     // this in the next version.
618                     $xo->element(self::NAME, null, $this->poco->preferredUsername);
619                 } else {
620                     $xo->element(self::TITLE, null, $name);
621                 }
622             }
623
624             if (!empty($this->summary)) {
625                 $xo->element(
626                     self::SUMMARY,
627                     null,
628                     common_xml_safe_str($this->summary)
629                 );
630             }
631
632             if (!empty($this->content)) {
633                 // XXX: assuming HTML content here
634                 $xo->element(
635                     ActivityUtils::CONTENT,
636                     array('type' => 'html'),
637                     common_xml_safe_str($this->content)
638                 );
639             }
640
641             if (!empty($this->link)) {
642                 $xo->element(
643                     'link',
644                     array(
645                         'rel' => 'alternate',
646                         'type' => 'text/html',
647                         'href' => $this->link
648                     ),
649                     null
650                 );
651             }
652
653             if(!empty($this->owner)) {
654                 $owner = $this->owner->asActivityNoun(self::AUTHOR);
655                 $xo->raw($owner);
656             }
657
658             if ($this->type == ActivityObject::PERSON
659                 || $this->type == ActivityObject::GROUP) {
660
661                 foreach ($this->avatarLinks as $avatar) {
662                     $xo->element(
663                         'link', array(
664                             'rel'  => 'avatar',
665                             'type'         => $avatar->type,
666                             'media:width'  => $avatar->width,
667                             'media:height' => $avatar->height,
668                             'href' => $avatar->url
669                         ),
670                         null
671                     );
672                 }
673             }
674
675             if (!empty($this->geopoint)) {
676                 $xo->element(
677                     'georss:point',
678                     null,
679                     $this->geopoint
680                 );
681             }
682
683             if (!empty($this->poco)) {
684                 $this->poco->outputTo($xo);
685             }
686
687             // @fixme there's no way here to make a tree; elements can only contain plaintext
688             // @fixme these may collide with JSON extensions
689             foreach ($this->extra as $el) {
690                 list($extraTag, $attrs, $content) = $el;
691                 $xo->element($extraTag, $attrs, $content);
692             }
693
694             Event::handle('EndActivityObjectOutputAtom', array($this, $xo));
695         }
696
697         if (!empty($tag)) {
698             $xo->elementEnd($tag);
699         }
700
701         return;
702     }
703
704     function asString($tag='activity:object')
705     {
706         $xs = new XMLStringer(true);
707
708         $this->outputTo($xs, $tag);
709
710         return $xs->getString();
711     }
712
713     /*
714      * Returns an array based on this Activity Object suitable for
715      * encoding as JSON.
716      *
717      * @return array $object the activity object array
718      */
719
720     function asArray()
721     {
722         $object = array();
723
724         if (Event::handle('StartActivityObjectOutputJson', array($this, &$object))) {
725             // XXX: attachments are added by Activity
726
727             // author (Add object for author? Could be useful for repeats.)
728
729             // content (Add rendered version of the notice?)
730
731             // displayName
732
733             if ($this->title) {
734                 $object['displayName'] = $this->title;
735             }
736
737             // downstreamDuplicates
738
739             // id
740             $object['id'] = $this->id;
741
742             if ($this->type == ActivityObject::PERSON
743                 || $this->type == ActivityObject::GROUP) {
744
745                 // XXX: Not sure what the best avatar is to use for the
746                 // author's "image". For now, I'm using the large size.
747
748                 $imgLink          = null;
749                 $avatarMediaLinks = array();
750
751                 foreach ($this->avatarLinks as $a) {
752
753                     // Make a MediaLink for every other Avatar
754                     $avatar = new ActivityStreamsMediaLink(
755                         $a->url,
756                         $a->width,
757                         $a->height,
758                         $a->type,
759                         'avatar'
760                     );
761
762                     // Find the big avatar to use as the "image"
763                     if ($a->height == AVATAR_PROFILE_SIZE) {
764                         $imgLink = $avatar;
765                     }
766
767                     $avatarMediaLinks[] = $avatar->asArray();
768                 }
769
770                 if (!array_key_exists('status_net', $object)) {
771                     $object['status_net'] = array();
772                 }
773
774                 $object['status_net']['avatarLinks'] = $avatarMediaLinks; // extension
775
776                 // image
777                 if (!empty($imgLink)) {
778                     $object['image']  = $imgLink->asArray();
779                 }
780             }
781
782             // objectType
783             //
784             // We can probably use the whole schema URL here but probably the
785             // relative simple name is easier to parse
786
787             $object['objectType'] = ActivityObject::canonicalType($this->type);
788
789             // summary
790             $object['summary'] = $this->summary;
791
792             // summary
793             $object['content'] = $this->content;
794
795             // published (probably don't need. Might be useful for repeats.)
796
797             // updated (probably don't need this)
798
799             // TODO: upstreamDuplicates
800
801             // url (XXX: need to put the right thing here...)
802             $object['url'] = $this->id;
803
804             /* Extensions */
805             // @fixme these may collide with XML extensions
806             // @fixme multiple tags of same name will overwrite each other
807             // @fixme text content from XML extensions will be lost
808
809             foreach ($this->extra as $e) {
810                 list($objectName, $props, $txt) = $e;
811                 if (!empty($objectName)) {
812                     $parts = explode(":", $objectName);
813                     if (count($parts) == 2 && $parts[0] == "statusnet") {
814                         if (!array_key_exists('status_net', $object)) {
815                             $object['status_net'] = array();
816                         }
817                         $object['status_net'][$parts[1]] = $props;
818                     } else {
819                         $object[$objectName] = $props;
820                     }
821                 }
822             }
823
824             if (!empty($this->geopoint)) {
825
826                 list($lat, $lon) = explode(' ', $this->geopoint);
827
828                 $object['location'] = array(
829                     'objectType' => 'place',
830                     'position' => sprintf("%+02.5F%+03.5F/", $lat, $lon),
831                     'lat' => $lat,
832                     'lon' => $lon
833                 );
834
835                 $loc = Location::fromLatLon($lat, $lon);
836
837                 if ($loc) {
838                     $name = $loc->getName();
839
840                     if ($name) {
841                         $object['location']['displayName'] = $name;
842                     }
843                     $url = $loc->getURL();
844
845                     if ($url) {
846                         $object['location']['url'] = $url;
847                     }
848                 }
849             }
850
851             if (!empty($this->poco)) {
852                 $object['portablecontacts_net'] = array_filter($this->poco->asArray());
853             }
854
855             if (!empty($this->thumbnail)) {
856                 if (is_string($this->thumbnail)) {
857                     $object['image'] = array('url' => $this->thumbnail);
858                 } else {
859                     $object['image'] = array('url' => $this->thumbnail->url);
860                     if ($this->thumbnail->width) {
861                         $object['image']['width'] = $this->thumbnail->width;
862                     }
863                     if ($this->thumbnail->height) {
864                         $object['image']['height'] = $this->thumbnail->height;
865                     }
866                 }
867             }
868
869             switch ($object->type) {
870             case self::IMAGE:
871                 if (!empty($this->largerImage)) {
872                     $object['fullImage'] = array('url' => $this->largerImage);
873                 }
874                 break;
875             case self::AUDIO:
876             case self::VIDEO:
877                 if (!empty($this->stream)) {
878                     $object['stream'] = array('url' => $this->stream);
879                 }
880                 break;
881             }
882
883             Event::handle('EndActivityObjectOutputJson', array($this, &$object));
884         }
885         return array_filter($object);
886     }
887
888     static function canonicalType($type) {
889         $ns = 'http://activitystrea.ms/schema/1.0/';
890         if (substr($type, 0, mb_strlen($ns)) == $ns) {
891             return substr($type, mb_strlen($ns));
892         } else {
893             return $type;
894         }
895     }
896
897     static function mimeTypeToObjectType($mimeType) {
898         $ot = null;
899
900         // Default
901
902         if (empty($mimeType)) {
903             return self::FILE;
904         }
905
906         $parts = explode('/', $mimeType);
907
908         switch ($parts[0]) {
909         case 'image':
910             $ot = self::IMAGE;
911             break;
912         case 'audio':
913             $ot = self::AUDIO;
914             break;
915         case 'video':
916             $ot = self::VIDEO;
917             break;
918         default:
919             $ot = self::FILE;
920         }
921
922         return $ot;
923     }
924 }