]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activity.php
Merge branch 'rationalize-activity' into testing
[quix0rs-gnu-social.git] / lib / activity.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 class PoCoURL
36 {
37     const TYPE      = 'type';
38     const VALUE     = 'value';
39     const PRIMARY   = 'primary';
40
41     public $type;
42     public $value;
43     public $primary;
44
45     function __construct($type, $value, $primary = false)
46     {
47         $this->type    = $type;
48         $this->value   = $value;
49         $this->primary = $primary;
50     }
51
52     function asString()
53     {
54         $xs = new XMLStringer(true);
55         $xs->elementStart('poco:urls');
56         $xs->element('poco:type', null, $this->type);
57         $xs->element('poco:value', null, $this->value);
58         if ($this->primary) {
59             $xs->element('poco:primary', null, 'true');
60         }
61         $xs->elementEnd('poco:urls');
62         return $xs->getString();
63     }
64 }
65
66 class PoCoAddress
67 {
68     const ADDRESS   = 'address';
69     const FORMATTED = 'formatted';
70
71     public $formatted;
72
73     function __construct($formatted)
74     {
75         if (empty($formatted)) {
76             return null;
77         }
78         $this->formatted = $formatted;
79     }
80
81     function asString()
82     {
83         $xs = new XMLStringer(true);
84         $xs->elementStart('poco:address');
85         $xs->element('poco:formatted', null, $this->formatted);
86         $xs->elementEnd('poco:address');
87         return $xs->getString();
88     }
89 }
90
91 class PoCo
92 {
93     const NS = 'http://portablecontacts.net/spec/1.0';
94
95     const USERNAME  = 'preferredUsername';
96     const NOTE      = 'note';
97     const URLS      = 'urls';
98
99     public $preferredUsername;
100     public $note;
101     public $address;
102     public $urls = array();
103
104     function __construct($profile)
105     {
106         $this->preferredUsername = $profile->nickname;
107
108         $this->note    = $profile->bio;
109         $this->address = new PoCoAddress($profile->location);
110
111         if (!empty($profile->homepage)) {
112             array_push(
113                 $this->urls,
114                 new PoCoURL(
115                     'homepage',
116                     $profile->homepage,
117                     true
118                 )
119             );
120         }
121     }
122
123     function asString()
124     {
125         $xs = new XMLStringer(true);
126         $xs->element(
127             'poco:preferredUsername',
128             null,
129             $this->preferredUsername
130         );
131
132         if (!empty($this->note)) {
133             $xs->element('poco:note', null, $this->note);
134         }
135
136         if (!empty($this->address)) {
137             $xs->raw($this->address->asString());
138         }
139
140         foreach ($this->urls as $url) {
141             $xs->raw($url->asString());
142         }
143
144         return $xs->getString();
145     }
146 }
147
148 /**
149  * Utilities for turning DOMish things into Activityish things
150  *
151  * Some common functions that I didn't have the bandwidth to try to factor
152  * into some kind of reasonable superclass, so just dumped here. Might
153  * be useful to have an ActivityObject parent class or something.
154  *
155  * @category  OStatus
156  * @package   StatusNet
157  * @author    Evan Prodromou <evan@status.net>
158  * @copyright 2010 StatusNet, Inc.
159  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
160  * @link      http://status.net/
161  */
162
163 class ActivityUtils
164 {
165     const ATOM = 'http://www.w3.org/2005/Atom';
166
167     const LINK = 'link';
168     const REL  = 'rel';
169     const TYPE = 'type';
170     const HREF = 'href';
171
172     const CONTENT = 'content';
173     const SRC     = 'src';
174
175     /**
176      * Get the permalink for an Activity object
177      *
178      * @param DOMElement $element A DOM element
179      *
180      * @return string related link, if any
181      */
182
183     static function getPermalink($element)
184     {
185         return self::getLink($element, 'alternate', 'text/html');
186     }
187
188     /**
189      * Get the permalink for an Activity object
190      *
191      * @param DOMElement $element A DOM element
192      *
193      * @return string related link, if any
194      */
195
196     static function getLink($element, $rel, $type=null)
197     {
198         $links = $element->getElementsByTagnameNS(self::ATOM, self::LINK);
199
200         foreach ($links as $link) {
201
202             $linkRel = $link->getAttribute(self::REL);
203             $linkType = $link->getAttribute(self::TYPE);
204
205             if ($linkRel == $rel &&
206                 (is_null($type) || $linkType == $type)) {
207                 return $link->getAttribute(self::HREF);
208             }
209         }
210
211         return null;
212     }
213
214     /**
215      * Gets the first child element with the given tag
216      *
217      * @param DOMElement $element   element to pick at
218      * @param string     $tag       tag to look for
219      * @param string     $namespace Namespace to look under
220      *
221      * @return DOMElement found element or null
222      */
223
224     static function child($element, $tag, $namespace=self::ATOM)
225     {
226         $els = $element->childNodes;
227         if (empty($els) || $els->length == 0) {
228             return null;
229         } else {
230             for ($i = 0; $i < $els->length; $i++) {
231                 $el = $els->item($i);
232                 if ($el->localName == $tag && $el->namespaceURI == $namespace) {
233                     return $el;
234                 }
235             }
236         }
237     }
238
239     /**
240      * Grab the text content of a DOM element child of the current element
241      *
242      * @param DOMElement $element   Element whose children we examine
243      * @param string     $tag       Tag to look up
244      * @param string     $namespace Namespace to use, defaults to Atom
245      *
246      * @return string content of the child
247      */
248
249     static function childContent($element, $tag, $namespace=self::ATOM)
250     {
251         $el = self::child($element, $tag, $namespace);
252
253         if (empty($el)) {
254             return null;
255         } else {
256             return $el->textContent;
257         }
258     }
259
260     /**
261      * Get the content of an atom:entry-like object
262      *
263      * @param DOMElement $element The element to examine.
264      *
265      * @return string unencoded HTML content of the element, like "This -&lt; is <b>HTML</b>."
266      *
267      * @todo handle remote content
268      * @todo handle embedded XML mime types
269      * @todo handle base64-encoded non-XML and non-text mime types
270      */
271
272     static function getContent($element)
273     {
274         $contentEl = ActivityUtils::child($element, self::CONTENT);
275
276         if (!empty($contentEl)) {
277
278             $src  = $contentEl->getAttribute(self::SRC);
279
280             if (!empty($src)) {
281                 throw new ClientException(_("Can't handle remote content yet."));
282             }
283
284             $type = $contentEl->getAttribute(self::TYPE);
285
286             // slavishly following http://atompub.org/rfc4287.html#rfc.section.4.1.3.3
287
288             if ($type == 'text') {
289                 return $contentEl->textContent;
290             } else if ($type == 'html') {
291                 $text = $contentEl->textContent;
292                 return htmlspecialchars_decode($text, ENT_QUOTES);
293             } else if ($type == 'xhtml') {
294                 $divEl = ActivityUtils::child($contentEl, 'div');
295                 if (empty($divEl)) {
296                     return null;
297                 }
298                 $doc = $divEl->ownerDocument;
299                 $text = '';
300                 $children = $divEl->childNodes;
301
302                 for ($i = 0; $i < $children->length; $i++) {
303                     $child = $children->item($i);
304                     $text .= $doc->saveXML($child);
305                 }
306                 return trim($text);
307             } else if (in_array(array('text/xml', 'application/xml'), $type) ||
308                        preg_match('#(+|/)xml$#', $type)) {
309                 throw new ClientException(_("Can't handle embedded XML content yet."));
310             } else if (strncasecmp($type, 'text/', 5)) {
311                 return $contentEl->textContent;
312             } else {
313                 throw new ClientException(_("Can't handle embedded Base64 content yet."));
314             }
315         }
316     }
317 }
318
319 /**
320  * A noun-ish thing in the activity universe
321  *
322  * The activity streams spec talks about activity objects, while also having
323  * a tag activity:object, which is in fact an activity object. Aaaaaah!
324  *
325  * This is just a thing in the activity universe. Can be the subject, object,
326  * or indirect object (target!) of an activity verb. Rotten name, and I'm
327  * propagating it. *sigh*
328  *
329  * @category  OStatus
330  * @package   StatusNet
331  * @author    Evan Prodromou <evan@status.net>
332  * @copyright 2010 StatusNet, Inc.
333  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
334  * @link      http://status.net/
335  */
336
337 class ActivityObject
338 {
339     const ARTICLE   = 'http://activitystrea.ms/schema/1.0/article';
340     const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
341     const NOTE      = 'http://activitystrea.ms/schema/1.0/note';
342     const STATUS    = 'http://activitystrea.ms/schema/1.0/status';
343     const FILE      = 'http://activitystrea.ms/schema/1.0/file';
344     const PHOTO     = 'http://activitystrea.ms/schema/1.0/photo';
345     const ALBUM     = 'http://activitystrea.ms/schema/1.0/photo-album';
346     const PLAYLIST  = 'http://activitystrea.ms/schema/1.0/playlist';
347     const VIDEO     = 'http://activitystrea.ms/schema/1.0/video';
348     const AUDIO     = 'http://activitystrea.ms/schema/1.0/audio';
349     const BOOKMARK  = 'http://activitystrea.ms/schema/1.0/bookmark';
350     const PERSON    = 'http://activitystrea.ms/schema/1.0/person';
351     const GROUP     = 'http://activitystrea.ms/schema/1.0/group';
352     const PLACE     = 'http://activitystrea.ms/schema/1.0/place';
353     const COMMENT   = 'http://activitystrea.ms/schema/1.0/comment';
354     // ^^^^^^^^^^ tea!
355
356     // Atom elements we snarf
357
358     const TITLE   = 'title';
359     const SUMMARY = 'summary';
360     const ID      = 'id';
361     const SOURCE  = 'source';
362
363     const NAME  = 'name';
364     const URI   = 'uri';
365     const EMAIL = 'email';
366
367     public $element;
368     public $type;
369     public $id;
370     public $title;
371     public $summary;
372     public $content;
373     public $link;
374     public $source;
375     public $avatar;
376     public $geopoint;
377
378     /**
379      * Constructor
380      *
381      * This probably needs to be refactored
382      * to generate a local class (ActivityPerson, ActivityFile, ...)
383      * based on the object type.
384      *
385      * @param DOMElement $element DOM thing to turn into an Activity thing
386      */
387
388     function __construct($element = null)
389     {
390         if (empty($element)) {
391             return;
392         }
393
394         $this->element = $element;
395
396         if ($element->tagName == 'author') {
397
398             $this->type  = self::PERSON; // XXX: is this fair?
399             $this->title = $this->_childContent($element, self::NAME);
400             $this->id    = $this->_childContent($element, self::URI);
401
402             if (empty($this->id)) {
403                 $email = $this->_childContent($element, self::EMAIL);
404                 if (!empty($email)) {
405                     // XXX: acct: ?
406                     $this->id = 'mailto:'.$email;
407                 }
408             }
409
410         } else {
411
412             $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
413                                                Activity::SPEC);
414
415             if (empty($this->type)) {
416                 $this->type = ActivityObject::NOTE;
417             }
418
419             $this->id      = $this->_childContent($element, self::ID);
420             $this->title   = $this->_childContent($element, self::TITLE);
421             $this->summary = $this->_childContent($element, self::SUMMARY);
422
423             $this->source  = $this->_getSource($element);
424
425             $this->content = ActivityUtils::getContent($element);
426
427             $this->link = ActivityUtils::getPermalink($element);
428
429             // XXX: grab PoCo stuff
430         }
431
432         // Some per-type attributes...
433         if ($this->type == self::PERSON || $this->type == self::GROUP) {
434             $this->displayName = $this->title;
435
436             // @fixme we may have multiple avatars with different resolutions specified
437             $this->avatar   = ActivityUtils::getLink($element, 'avatar');
438             $this->nickname = ActivityUtils::childContent($element, PoCo::USERNAME, PoCo::NS);
439         }
440     }
441
442     private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
443     {
444         return ActivityUtils::childContent($element, $tag, $namespace);
445     }
446
447     // Try to get a unique id for the source feed
448
449     private function _getSource($element)
450     {
451         $sourceEl = ActivityUtils::child($element, 'source');
452
453         if (empty($sourceEl)) {
454             return null;
455         } else {
456             $href = ActivityUtils::getLink($sourceEl, 'self');
457             if (!empty($href)) {
458                 return $href;
459             } else {
460                 return ActivityUtils::childContent($sourceEl, 'id');
461             }
462         }
463     }
464
465     static function fromNotice($notice)
466     {
467         $object = new ActivityObject();
468
469         $object->type    = ActivityObject::NOTE;
470
471         $object->id      = $notice->uri;
472         $object->title   = $notice->content;
473         $object->content = $notice->rendered;
474         $object->link    = $notice->bestUrl();
475
476         return $object;
477     }
478
479     /**
480      * @fixme missing avatar, bio info, etc
481      */
482     static function fromProfile($profile)
483     {
484         $object = new ActivityObject();
485
486         $object->type   = ActivityObject::PERSON;
487         $object->id     = $profile->getUri();
488         $object->title  = $profile->getBestName();
489         $object->link   = $profile->profileurl;
490         $object->avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
491
492         if (isset($profile->lat) && isset($profile->lon)) {
493             $object->geopoint = (float)$profile->lat . ' ' . (float)$profile->lon;
494         }
495
496         $object->poco = new PoCo($profile);
497
498         return $object;
499     }
500
501     /**
502      * @fixme missing avatar, bio info, etc
503      */
504     function asString($tag='activity:object')
505     {
506         $xs = new XMLStringer(true);
507
508         $xs->elementStart($tag);
509
510         $xs->element('activity:object-type', null, $this->type);
511
512         $xs->element(self::ID, null, $this->id);
513
514         if (!empty($this->title)) {
515             $xs->element(self::TITLE, null, $this->title);
516         }
517
518         if (!empty($this->summary)) {
519             $xs->element(self::SUMMARY, null, $this->summary);
520         }
521
522         if (!empty($this->content)) {
523             // XXX: assuming HTML content here
524             $xs->element(ActivityUtils::CONTENT, array('type' => 'html'), $this->content);
525         }
526
527         if (!empty($this->link)) {
528             $xs->element('link', array('rel' => 'alternate', 'type' => 'text/html'),
529                          $this->link);
530         }
531
532         if ($this->type == ActivityObject::PERSON) {
533             $xs->element(
534                 'link', array(
535                     'type' => empty($this->avatar) ? 'image/png' : $this->avatar->mediatype,
536                     'rel'  => 'avatar',
537                     'href' => empty($this->avatar)
538                     ? Avatar::defaultImage(AVATAR_PROFILE_SIZE)
539                     : $this->avatar->displayUrl()
540                 ),
541                 ''
542             );
543         }
544
545         if (!empty($this->geopoint)) {
546             $xs->element(
547                 'georss:point',
548                 null,
549                 $this->geopoint
550             );
551         }
552
553         if (!empty($this->poco)) {
554             $xs->raw($this->poco->asString());
555         }
556
557         $xs->elementEnd($tag);
558
559         return $xs->getString();
560     }
561 }
562
563 /**
564  * Utility class to hold a bunch of constant defining default verb types
565  *
566  * @category  OStatus
567  * @package   StatusNet
568  * @author    Evan Prodromou <evan@status.net>
569  * @copyright 2010 StatusNet, Inc.
570  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
571  * @link      http://status.net/
572  */
573
574 class ActivityVerb
575 {
576     const POST     = 'http://activitystrea.ms/schema/1.0/post';
577     const SHARE    = 'http://activitystrea.ms/schema/1.0/share';
578     const SAVE     = 'http://activitystrea.ms/schema/1.0/save';
579     const FAVORITE = 'http://activitystrea.ms/schema/1.0/favorite';
580     const PLAY     = 'http://activitystrea.ms/schema/1.0/play';
581     const FOLLOW   = 'http://activitystrea.ms/schema/1.0/follow';
582     const FRIEND   = 'http://activitystrea.ms/schema/1.0/make-friend';
583     const JOIN     = 'http://activitystrea.ms/schema/1.0/join';
584     const TAG      = 'http://activitystrea.ms/schema/1.0/tag';
585
586     // Custom OStatus verbs for the flipside until they're standardized
587     const DELETE     = 'http://ostatus.org/schema/1.0/unfollow';
588     const UNFAVORITE = 'http://ostatus.org/schema/1.0/unfavorite';
589     const UNFOLLOW   = 'http://ostatus.org/schema/1.0/unfollow';
590     const LEAVE      = 'http://ostatus.org/schema/1.0/leave';
591 }
592
593 class ActivityContext
594 {
595     public $replyToID;
596     public $replyToUrl;
597     public $location;
598     public $attention = array();
599     public $conversation;
600
601     const THR     = 'http://purl.org/syndication/thread/1.0';
602     const GEORSS  = 'http://www.georss.org/georss';
603     const OSTATUS = 'http://ostatus.org/schema/1.0';
604
605     const INREPLYTO = 'in-reply-to';
606     const REF       = 'ref';
607     const HREF      = 'href';
608
609     const POINT     = 'point';
610
611     const ATTENTION    = 'ostatus:attention';
612     const CONVERSATION = 'ostatus:conversation';
613
614     function __construct($element)
615     {
616         $replyToEl = ActivityUtils::child($element, self::INREPLYTO, self::THR);
617
618         if (!empty($replyToEl)) {
619             $this->replyToID  = $replyToEl->getAttribute(self::REF);
620             $this->replyToUrl = $replyToEl->getAttribute(self::HREF);
621         }
622
623         $this->location = $this->getLocation($element);
624
625         $this->conversation = ActivityUtils::getLink($element, self::CONVERSATION);
626
627         // Multiple attention links allowed
628
629         $links = $element->getElementsByTagNameNS(ActivityUtils::ATOM, ActivityUtils::LINK);
630
631         for ($i = 0; $i < $links->length; $i++) {
632
633             $link = $links->item($i);
634
635             $linkRel = $link->getAttribute(ActivityUtils::REL);
636
637             if ($linkRel == self::ATTENTION) {
638                 $this->attention[] = $link->getAttribute(self::HREF);
639             }
640         }
641     }
642
643     /**
644      * Parse location given as a GeoRSS-simple point, if provided.
645      * http://www.georss.org/simple
646      *
647      * @param feed item $entry
648      * @return mixed Location or false
649      */
650     function getLocation($dom)
651     {
652         $points = $dom->getElementsByTagNameNS(self::GEORSS, self::POINT);
653
654         for ($i = 0; $i < $points->length; $i++) {
655             $point = $points->item($i)->textContent;
656             $point = str_replace(',', ' ', $point); // per spec "treat commas as whitespace"
657             $point = preg_replace('/\s+/', ' ', $point);
658             $point = trim($point);
659             $coords = explode(' ', $point);
660             if (count($coords) == 2) {
661                 list($lat, $lon) = $coords;
662                 if (is_numeric($lat) && is_numeric($lon)) {
663                     common_log(LOG_INFO, "Looking up location for $lat $lon from georss");
664                     return Location::fromLatLon($lat, $lon);
665                 }
666             }
667             common_log(LOG_ERR, "Ignoring bogus georss:point value $point");
668         }
669
670         return null;
671     }
672 }
673
674 /**
675  * An activity in the ActivityStrea.ms world
676  *
677  * An activity is kind of like a sentence: someone did something
678  * to something else.
679  *
680  * 'someone' is the 'actor'; 'did something' is the verb;
681  * 'something else' is the object.
682  *
683  * @category  OStatus
684  * @package   StatusNet
685  * @author    Evan Prodromou <evan@status.net>
686  * @copyright 2010 StatusNet, Inc.
687  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
688  * @link      http://status.net/
689  */
690
691 class Activity
692 {
693     const SPEC   = 'http://activitystrea.ms/spec/1.0/';
694     const SCHEMA = 'http://activitystrea.ms/schema/1.0/';
695
696     const VERB       = 'verb';
697     const OBJECT     = 'object';
698     const ACTOR      = 'actor';
699     const SUBJECT    = 'subject';
700     const OBJECTTYPE = 'object-type';
701     const CONTEXT    = 'context';
702     const TARGET     = 'target';
703
704     const ATOM = 'http://www.w3.org/2005/Atom';
705
706     const AUTHOR    = 'author';
707     const PUBLISHED = 'published';
708     const UPDATED   = 'updated';
709
710     public $actor;   // an ActivityObject
711     public $verb;    // a string (the URL)
712     public $object;  // an ActivityObject
713     public $target;  // an ActivityObject
714     public $context; // an ActivityObject
715     public $time;    // Time of the activity
716     public $link;    // an ActivityObject
717     public $entry;   // the source entry
718     public $feed;    // the source feed
719
720     public $summary; // summary of activity
721     public $content; // HTML content of activity
722     public $id;      // ID of the activity
723     public $title;   // title of the activity
724
725     /**
726      * Turns a regular old Atom <entry> into a magical activity
727      *
728      * @param DOMElement $entry Atom entry to poke at
729      * @param DOMElement $feed  Atom feed, for context
730      */
731
732     function __construct($entry = null, $feed = null)
733     {
734         if (is_null($entry)) {
735             return;
736         }
737
738         $this->entry = $entry;
739         $this->feed  = $feed;
740
741         $pubEl = $this->_child($entry, self::PUBLISHED, self::ATOM);
742
743         if (!empty($pubEl)) {
744             $this->time = strtotime($pubEl->textContent);
745         } else {
746             // XXX technically an error; being liberal. Good idea...?
747             $updateEl = $this->_child($entry, self::UPDATED, self::ATOM);
748             if (!empty($updateEl)) {
749                 $this->time = strtotime($updateEl->textContent);
750             } else {
751                 $this->time = null;
752             }
753         }
754
755         $this->link = ActivityUtils::getPermalink($entry);
756
757         $verbEl = $this->_child($entry, self::VERB);
758
759         if (!empty($verbEl)) {
760             $this->verb = trim($verbEl->textContent);
761         } else {
762             $this->verb = ActivityVerb::POST;
763             // XXX: do other implied stuff here
764         }
765
766         $objectEl = $this->_child($entry, self::OBJECT);
767
768         if (!empty($objectEl)) {
769             $this->object = new ActivityObject($objectEl);
770         } else {
771             $this->object = new ActivityObject($entry);
772         }
773
774         $actorEl = $this->_child($entry, self::ACTOR);
775
776         if (!empty($actorEl)) {
777
778             $this->actor = new ActivityObject($actorEl);
779
780         } else if (!empty($feed) &&
781                    $subjectEl = $this->_child($feed, self::SUBJECT)) {
782
783             $this->actor = new ActivityObject($subjectEl);
784
785         } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
786
787             $this->actor = new ActivityObject($authorEl);
788
789         } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
790                                                               self::ATOM)) {
791
792             $this->actor = new ActivityObject($authorEl);
793         }
794
795         $contextEl = $this->_child($entry, self::CONTEXT);
796
797         if (!empty($contextEl)) {
798             $this->context = new ActivityContext($contextEl);
799         } else {
800             $this->context = new ActivityContext($entry);
801         }
802
803         $targetEl = $this->_child($entry, self::TARGET);
804
805         if (!empty($targetEl)) {
806             $this->target = new ActivityObject($targetEl);
807         }
808
809         $this->summary = ActivityUtils::childContent($entry, 'summary');
810         $this->id      = ActivityUtils::childContent($entry, 'id');
811         $this->content = ActivityUtils::getContent($entry);
812     }
813
814     /**
815      * Returns an Atom <entry> based on this activity
816      *
817      * @return DOMElement Atom entry
818      */
819
820     function toAtomEntry()
821     {
822         return null;
823     }
824
825     function asString($namespace=false)
826     {
827         $xs = new XMLStringer(true);
828
829         if ($namespace) {
830             $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
831                            'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
832                            'xmlns:ostatus' => 'http://ostatus.org/schema/1.0');
833         } else {
834             $attrs = array();
835         }
836
837         $xs->elementStart('entry', $attrs);
838
839         $xs->element('id', null, $this->id);
840         $xs->element('title', null, $this->title);
841         $xs->element('published', null, common_date_iso8601($this->time));
842         $xs->element('content', array('type' => 'html'), $this->content);
843
844         if (!empty($this->summary)) {
845             $xs->element('summary', null, $this->summary);
846         }
847
848         if (!empty($this->link)) {
849             $xs->element('link', array('rel' => 'alternate',
850                                        'type' => 'text/html'),
851                          $this->link);
852         }
853
854         // XXX: add context
855         // XXX: add target
856
857         $xs->raw($this->actor->asString('activity:actor'));
858         $xs->element('activity:verb', null, $this->verb);
859         $xs->raw($this->object->asString());
860
861         $xs->elementEnd('entry');
862
863         return $xs->getString();
864     }
865
866     private function _child($element, $tag, $namespace=self::SPEC)
867     {
868         return ActivityUtils::child($element, $tag, $namespace);
869     }
870 }