3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
22 define('LISTENER', 1);
23 define('LISTENEE', -1);
26 class FoafAction extends Action
28 function isReadOnly($args)
33 function prepare($args)
35 parent::prepare($args);
37 $nickname_arg = $this->arg('nickname');
39 if (empty($nickname_arg)) {
40 $this->clientError(_('No such user.'), 404);
44 $this->nickname = common_canonical_nickname($nickname_arg);
46 // Permanent redirect on non-canonical nickname
48 if ($nickname_arg != $this->nickname) {
49 common_redirect(common_local_url('foaf',
50 array('nickname' => $this->nickname)),
55 $this->user = User::staticGet('nickname', $this->nickname);
58 $this->clientError(_('No such user.'), 404);
62 $this->profile = $this->user->getProfile();
64 if (!$this->profile) {
65 $this->serverError(_('User has no profile.'), 500);
72 function handle($args)
74 parent::handle($args);
76 header('Content-Type: application/rdf+xml');
79 $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
80 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
82 'http://www.w3.org/2000/01/rdf-schema#',
84 'http://www.w3.org/2003/01/geo/wgs84_pos#',
86 'http://purl.org/vocab/bio/0.1/',
88 'http://rdfs.org/sioc/ns#',
89 'xmlns' => 'http://xmlns.com/foaf/0.1/'));
91 // This is the document about the user
93 $this->showPpd('', $this->user->uri);
95 // Would be nice to tell if they were a Person or not (e.g. a #person usertag?)
96 $this->elementStart('Agent', array('rdf:about' =>
98 if ($this->user->email) {
99 $this->element('mbox_sha1sum', null, sha1('mailto:' . $this->user->email));
101 if ($this->profile->fullname) {
102 $this->element('name', null, $this->profile->fullname);
104 if ($this->profile->homepage) {
105 $this->element('homepage', array('rdf:resource' => $this->profile->homepage));
107 if ($this->profile->profileurl) {
108 $this->element('weblog', array('rdf:resource' => $this->profile->profileurl));
110 if ($this->profile->bio) {
111 $this->element('bio:olb', null, $this->profile->bio);
114 $location = $this->profile->getLocation();
117 if ($location->getRdfURL()) {
118 $attr['rdf:about'] = $location->getRdfURL();
120 $location_name = $location->getName();
122 $this->elementStart('based_near');
123 $this->elementStart('geo:SpatialThing', $attr);
124 if ($location_name) {
125 $this->element('name', null, $location_name);
127 if ($location->lat) {
128 $this->element('geo:lat', null, $location->lat);
130 if ($location->lon) {
131 $this->element('geo:long', null, $location->lon);
133 if ($location->getURL()) {
134 $this->element('page', array('rdf:resource'=>$location->getURL()));
136 $this->elementEnd('geo:SpatialThing');
137 $this->elementEnd('based_near');
140 $avatar = $this->profile->getOriginalAvatar();
142 $this->elementStart('img');
143 $this->elementStart('Image', array('rdf:about' => $avatar->url));
144 foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
145 $scaled = $this->profile->getAvatar($size);
146 if (!$scaled->original) { // sometimes the original has one of our scaled sizes
147 $this->elementStart('thumbnail');
148 $this->element('Image', array('rdf:about' => $scaled->url));
149 $this->elementEnd('thumbnail');
152 $this->elementEnd('Image');
153 $this->elementEnd('img');
156 $person = $this->showMicrobloggingAccount($this->profile,
157 common_root_url(), $this->user->uri,
158 /*$fetchSubscriptions*/true,
159 /*$isSubscriber*/false);
161 // Get people who subscribe to user
163 $sub = new Subscription();
164 $sub->subscribed = $this->profile->id;
165 $sub->whereAdd('subscriber != subscribed');
168 while ($sub->fetch()) {
169 $profile = Profile::staticGet('id', $sub->subscriber);
170 if (empty($profile)) {
171 common_debug('Got a bad subscription: '.print_r($sub,true));
174 $user = $profile->getUser();
175 $other_uri = $profile->getUri();
176 if (array_key_exists($other_uri, $person)) {
177 $person[$other_uri][0] = BOTH;
179 $person[$other_uri] = array(LISTENER,
182 $user ? 'local' : 'remote');
190 foreach ($person as $uri => $p) {
191 list($type, $id, $nickname, $local) = $p;
193 $this->element('knows', array('rdf:resource' => $uri));
197 $this->elementEnd('Agent');
200 foreach ($person as $uri => $p) {
202 list($type, $id, $nickname, $local) = $p;
203 if ($local == 'local') {
204 $foaf_url = common_local_url('foaf', array('nickname' => $nickname));
206 $profile = Profile::staticGet($id);
207 $this->elementStart('Agent', array('rdf:about' => $uri));
209 $this->element('knows', array('rdf:resource' => $this->user->uri));
211 $this->showMicrobloggingAccount($profile,
212 ($local == 'local') ? common_root_url() : null,
214 /*$fetchSubscriptions*/false,
215 /*$isSubscriber*/($type == LISTENER || $type == BOTH));
217 $this->element('rdfs:seeAlso', array('rdf:resource' => $foaf_url));
219 $this->elementEnd('Agent');
221 $this->showPpd($foaf_url, $uri);
228 $this->elementEnd('rdf:RDF');
232 function showPpd($foaf_url, $person_uri)
234 $this->elementStart('PersonalProfileDocument', array('rdf:about' => $foaf_url));
235 $this->element('maker', array('rdf:resource' => $person_uri));
236 $this->element('primaryTopic', array('rdf:resource' => $person_uri));
237 $this->elementEnd('PersonalProfileDocument');
241 * Output FOAF <account> bit for the given profile.
243 * @param Profile $profile
244 * @param mixed $service Root URL of this StatusNet instance for a local
245 * user, otherwise null.
246 * @param mixed $useruri URI string for the referenced profile..
247 * @param boolean $fetchSubscriptions Should we load and list all their subscriptions?
248 * @param boolean $isSubscriber if not fetching subs, we can still mark the user as following the current page.
250 * @return array if $fetchSubscribers is set, return a list of info on those
254 function showMicrobloggingAccount($profile, $service=null, $useruri=null, $fetchSubscriptions=false, $isSubscriber=false)
258 $attr['rdf:about'] = $useruri . '#acct';
262 $this->elementStart('account');
263 $this->elementStart('OnlineAccount', $attr);
265 $this->element('accountServiceHomepage', array('rdf:resource' =>
268 $this->element('accountName', null, $profile->nickname);
269 $this->element('accountProfilePage', array('rdf:resource' => $profile->profileurl));
271 $this->element('sioc:account_of', array('rdf:resource'=>$useruri));
276 if ($fetchSubscriptions) {
277 // Get people user is subscribed to
278 $sub = new Subscription();
279 $sub->subscriber = $profile->id;
280 $sub->whereAdd('subscriber != subscribed');
283 while ($sub->fetch()) {
284 $profile = Profile::staticGet('id', $sub->subscribed);
285 if (empty($profile)) {
286 common_debug('Got a bad subscription: '.print_r($sub,true));
289 $user = $profile->getUser();
290 $other_uri = $profile->getUri();
291 $this->element('sioc:follows', array('rdf:resource' => $other_uri.'#acct'));
292 $person[$other_uri] = array(LISTENEE,
295 $user ? 'local' : 'remote');
301 } else if ($isSubscriber) {
302 // Just declare that they follow the user whose FOAF we're showing.
303 $this->element('sioc:follows', array('rdf:resource' => $this->user->uri . '#acct'));
306 $this->elementEnd('OnlineAccount');
307 $this->elementEnd('account');