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 // @todo XXX: Documentation missing.
27 class FoafAction extends Action
29 function isReadOnly($args)
34 function prepare($args)
36 parent::prepare($args);
38 $nickname_arg = $this->arg('nickname');
40 if (empty($nickname_arg)) {
41 // TRANS: Client error displayed when requesting Friends of a Friend feed without providing a user nickname.
42 $this->clientError(_('No such user.'), 404);
46 $this->nickname = common_canonical_nickname($nickname_arg);
48 // Permanent redirect on non-canonical nickname
50 if ($nickname_arg != $this->nickname) {
51 common_redirect(common_local_url('foaf',
52 array('nickname' => $this->nickname)),
57 $this->user = User::getKV('nickname', $this->nickname);
60 // TRANS: Client error displayed when requesting Friends of a Friend feed for an object that is not a user.
61 $this->clientError(_('No such user.'), 404);
65 $this->profile = $this->user->getProfile();
67 if (!$this->profile) {
68 // TRANS: Error message displayed when referring to a user without a profile.
69 $this->serverError(_('User has no profile.'), 500);
76 function handle($args)
78 parent::handle($args);
80 header('Content-Type: application/rdf+xml');
83 $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
84 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
86 'http://www.w3.org/2000/01/rdf-schema#',
88 'http://www.w3.org/2003/01/geo/wgs84_pos#',
90 'http://purl.org/vocab/bio/0.1/',
92 'http://rdfs.org/sioc/ns#',
93 'xmlns' => 'http://xmlns.com/foaf/0.1/'));
95 // This is the document about the user
97 $this->showPpd('', $this->user->uri);
99 // Would be nice to tell if they were a Person or not (e.g. a #person usertag?)
100 $this->elementStart('Agent', array('rdf:about' =>
102 if ($this->user->email) {
103 $this->element('mbox_sha1sum', null, sha1('mailto:' . $this->user->email));
105 if ($this->profile->fullname) {
106 $this->element('name', null, $this->profile->fullname);
108 if ($this->profile->homepage) {
109 $this->element('homepage', array('rdf:resource' => $this->profile->homepage));
111 if ($this->profile->profileurl) {
112 $this->element('weblog', array('rdf:resource' => $this->profile->profileurl));
114 if ($this->profile->bio) {
115 $this->element('bio:olb', null, $this->profile->bio);
118 $location = $this->profile->getLocation();
121 if ($location->getRdfURL()) {
122 $attr['rdf:about'] = $location->getRdfURL();
124 $location_name = $location->getName();
126 $this->elementStart('based_near');
127 $this->elementStart('geo:SpatialThing', $attr);
128 if ($location_name) {
129 $this->element('name', null, $location_name);
131 if ($location->lat) {
132 $this->element('geo:lat', null, $location->lat);
134 if ($location->lon) {
135 $this->element('geo:long', null, $location->lon);
137 if ($location->getURL()) {
138 $this->element('page', array('rdf:resource'=>$location->getURL()));
140 $this->elementEnd('geo:SpatialThing');
141 $this->elementEnd('based_near');
145 $avatar = Avatar::getUploaded($this->profile);
146 $this->elementStart('img');
147 $this->elementStart('Image', array('rdf:about' => $avatar->displayUrl()));
148 foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
150 $scaled = $this->profile->getAvatar($size);
151 $this->elementStart('thumbnail');
152 $this->element('Image', array('rdf:about' => $scaled->displayUrl()));
153 $this->elementEnd('thumbnail');
154 } catch (Exception $e) {
155 // This avatar did not exist
158 $this->elementEnd('Image');
159 $this->elementEnd('img');
160 } catch (Exception $e) {
161 // No avatar for this user!
164 $person = $this->showMicrobloggingAccount($this->profile,
165 common_root_url(), $this->user->uri,
166 /*$fetchSubscriptions*/true,
167 /*$isSubscriber*/false);
169 // Get people who subscribe to user
171 $sub = new Subscription();
172 $sub->subscribed = $this->profile->id;
173 $sub->whereAdd('subscriber != subscribed');
176 while ($sub->fetch()) {
177 $profile = Profile::getKV('id', $sub->subscriber);
178 if (empty($profile)) {
179 common_debug('Got a bad subscription: '.print_r($sub,true));
182 $other_uri = $profile->getUri();
183 if (array_key_exists($other_uri, $person)) {
184 $person[$other_uri][0] = BOTH;
186 $person[$other_uri] = array(LISTENER,
189 $profile->isLocal() ? 'local' : 'remote');
197 foreach ($person as $uri => $p) {
198 list($type, $id, $nickname, $local) = $p;
200 $this->element('knows', array('rdf:resource' => $uri));
204 $this->elementEnd('Agent');
207 foreach ($person as $uri => $p) {
209 list($type, $id, $nickname, $local) = $p;
210 if ($local == 'local') {
211 $foaf_url = common_local_url('foaf', array('nickname' => $nickname));
213 $profile = Profile::getKV($id);
214 $this->elementStart('Agent', array('rdf:about' => $uri));
216 $this->element('knows', array('rdf:resource' => $this->user->uri));
218 $this->showMicrobloggingAccount($profile,
219 ($local == 'local') ? common_root_url() : null,
221 /*$fetchSubscriptions*/false,
222 /*$isSubscriber*/($type == LISTENER || $type == BOTH));
224 $this->element('rdfs:seeAlso', array('rdf:resource' => $foaf_url));
226 $this->elementEnd('Agent');
228 $this->showPpd($foaf_url, $uri);
235 $this->elementEnd('rdf:RDF');
239 function showPpd($foaf_url, $person_uri)
241 $this->elementStart('PersonalProfileDocument', array('rdf:about' => $foaf_url));
242 $this->element('maker', array('rdf:resource' => $person_uri));
243 $this->element('primaryTopic', array('rdf:resource' => $person_uri));
244 $this->elementEnd('PersonalProfileDocument');
248 * Output FOAF <account> bit for the given profile.
250 * @param Profile $profile
251 * @param mixed $service Root URL of this StatusNet instance for a local
252 * user, otherwise null.
253 * @param mixed $useruri URI string for the referenced profile..
254 * @param boolean $fetchSubscriptions Should we load and list all their subscriptions?
255 * @param boolean $isSubscriber if not fetching subs, we can still mark the user as following the current page.
257 * @return array if $fetchSubscribers is set, return a list of info on those
260 function showMicrobloggingAccount($profile, $service=null, $useruri=null, $fetchSubscriptions=false, $isSubscriber=false)
264 $attr['rdf:about'] = $useruri . '#acct';
268 $this->elementStart('account');
269 $this->elementStart('OnlineAccount', $attr);
271 $this->element('accountServiceHomepage', array('rdf:resource' =>
274 $this->element('accountName', null, $profile->nickname);
275 $this->element('accountProfilePage', array('rdf:resource' => $profile->profileurl));
277 $this->element('sioc:account_of', array('rdf:resource'=>$useruri));
282 if ($fetchSubscriptions) {
283 // Get people user is subscribed to
284 $sub = new Subscription();
285 $sub->subscriber = $profile->id;
286 $sub->whereAdd('subscriber != subscribed');
289 while ($sub->fetch()) {
290 $profile = Profile::getKV('id', $sub->subscribed);
291 if (empty($profile)) {
292 common_debug('Got a bad subscription: '.print_r($sub,true));
295 $other_uri = $profile->getUri();
296 $this->element('sioc:follows', array('rdf:resource' => $other_uri.'#acct'));
297 $person[$other_uri] = array(LISTENEE,
300 $profile->isLocal() ? 'local' : 'remote');
306 } else if ($isSubscriber) {
307 // Just declare that they follow the user whose FOAF we're showing.
308 $this->element('sioc:follows', array('rdf:resource' => $this->user->uri . '#acct'));
311 $this->elementEnd('OnlineAccount');
312 $this->elementEnd('account');