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