]> git.mxchange.org Git - friendica.git/blob - include/socgraph.php
poco_load aborting after debug statement
[friendica.git] / include / socgraph.php
1 <?php
2
3 require_once('include/datetime.php');
4
5
6 /*
7  * poco_load
8  *
9  * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
10  * and add the entries to the gcontact (Global Contact) table, or update existing entries
11  * if anything (name or photo) has changed.
12  * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
13  *
14  * Once the global contact is stored add (if necessary) the contact linkage which associates
15  * the given uid, cid to the global contact entry. There can be many uid/cid combinations
16  * pointing to the same global contact id. 
17  *
18  */
19  
20
21
22
23 function poco_load($cid,$uid = 0,$url = null) {
24         $a = get_app();
25
26         if($cid) {
27                 if((! $url) || (! $uid)) {
28                         $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
29                                 intval($cid)
30                         );
31                         if(count($r)) {
32                                 $url = $r[0]['poco'];
33                                 $uid = $r[0]['uid'];
34                         }
35                 }
36                 if(! $uid)
37                         return;
38         }
39
40         if(! $url)
41                 return;
42
43         $url = $url . (($uid) ? '/@me/@all?fields=displayName,urls,photos' : '?fields=displayName,urls,photos') ;
44
45         logger('poco_load: ' . $url, LOGGER_DEBUG);
46
47         $s = fetch_url($url);
48
49         logger('poco_load: returns ' . $s, LOGGER_DATA);
50
51         logger('poco_load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
52
53         if(($a->get_curl_code() > 299) || (! $s))
54                 return;
55
56
57         $j = json_decode($s);
58
59         logger('poco_load: json: ' . print_r($j,true),LOGGER_DATA);
60
61         $total = 0;
62         foreach($j->entry as $entry) {
63
64                 $total ++;
65                 $profile_url = '';
66                 $profile_photo = '';
67                 $connect_url = '';
68                 $name = '';
69
70                 $name = $entry->displayName;
71
72                 foreach($entry->urls as $url) {
73                         if($url->type == 'profile') {
74                                 $profile_url = $url->value;
75                                 continue;
76                         }
77                         if($url->type == 'webfinger') {
78                                 $connect_url = str_replace('acct:' , '', $url->value);
79                                 continue;
80                         }
81
82                 } 
83                 foreach($entry->photos as $photo) {
84                         if($photo->type == 'profile') {
85                                 $profile_photo = $photo->value;
86                                 continue;
87                         }
88                 }
89
90                 if((! $name) || (! $profile_url) || (! $profile_photo))
91                         continue; 
92                  
93                 $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
94                         dbesc(normalise_link($profile_url))
95                 );
96
97                 if(count($x)) {
98                         $gcid = $x[0]['id'];
99
100                         if($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo) {
101                                 q("update gcontact set `name` = '%s', `photo` = '%s', `connect` = '%s' 
102                                         where `nurl` = '%s' limit 1",
103                                         dbesc($name),
104                                         dbesc($profile_photo),
105                                         dbesc($connect_url),
106                                         dbesc(normalise_link($profile_url))
107                                 );
108                         }
109                 }
110                 else {
111                         q("insert into `gcontact` (`name`,`url`,`nurl`,`photo`,`connect`)
112                                 values ( '%s', '%s', '%s', '%s','%s') ",
113                                 dbesc($name),
114                                 dbesc($profile_url),
115                                 dbesc(normalise_link($profile_url)),
116                                 dbesc($profile_photo),
117                                 dbesc($connect_url)
118                         );
119                         $x = q("select * from `gcontact` where `nurl` = '%s' limit 1",
120                                 dbesc(normalise_link($profile_url))
121                         );
122                         if(count($x))
123                                 $gcid = $x[0]['id'];
124                 }
125                 if(! $gcid)
126                         return;
127
128                 $r = q("select * from glink where `cid` = %d and `uid` = %d and `gcid` = %d limit 1",
129                         intval($cid),
130                         intval($uid),
131                         intval($gcid)
132                 );
133                 if(! count($r)) {
134                         q("insert into glink ( `cid`,`uid`,`gcid`,`updated`) values (%d,%d,%d,'%s') ",
135                                 intval($cid),
136                                 intval($uid),
137                                 intval($gcid),
138                                 dbesc(datetime_convert())
139                         );
140                 }
141                 else {
142                         q("update glink set updated = '%s' where `cid` = %d and `uid` = %d and `gcid` = %d limit 1",
143                                 dbesc(datetime_convert()),
144                                 intval($cid),
145                                 intval($uid),
146                                 intval($gcid)
147                         );
148                 }
149
150         }
151         logger("poco_load: loaded $total entries",LOGGER_DEBUG);
152
153         q("delete from glink where `cid` = %d and `uid` = %d and `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
154                 intval($cid),
155                 intval($uid)
156         );
157
158 }
159
160
161 function count_common_friends($uid,$cid) {
162
163         $r = q("SELECT count(*) as `total`
164                 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
165                 where `glink`.`cid` = %d and `glink`.`uid` = %d
166                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and id != %d ) ",
167                 intval($cid),
168                 intval($uid),
169                 intval($uid),
170                 intval($cid)
171         );
172
173         if(count($r))
174                 return $r[0]['total'];
175         return 0;
176
177 }
178
179
180 function common_friends($uid,$cid) {
181
182         $r = q("SELECT `gcontact`.* 
183                 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
184                 where `glink`.`cid` = %d and `glink`.`uid` = %d
185                 and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and id != %d ) 
186                 order by `gcontact`.`name` asc ",
187                 intval($cid),
188                 intval($uid),
189                 intval($uid),
190                 intval($cid)
191         );
192
193         return $r;
194
195 }
196
197 function count_all_friends($uid,$cid) {
198
199         $r = q("SELECT count(*) as `total`
200                 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
201                 where `glink`.`cid` = %d and `glink`.`uid` = %d ",
202                 intval($cid),
203                 intval($uid)
204         );
205
206         if(count($r))
207                 return $r[0]['total'];
208         return 0;
209
210 }
211
212
213 function all_friends($uid,$cid,$start = 0, $limit = 80) {
214
215         $r = q("SELECT `gcontact`.* 
216                 FROM `glink` left join `gcontact` on `glink`.`gcid` = `gcontact`.`id`
217                 where `glink`.`cid` = %d and `glink`.`uid` = %d 
218                 order by `gcontact`.`name` asc LIMIT %d, %d ",
219                 intval($cid),
220                 intval($uid),
221                 intval($start),
222                 intval($limit)
223         );
224
225         return $r;
226 }
227
228
229
230 function suggestion_query($uid, $start = 0, $limit = 40) {
231
232         if(! $uid)
233                 return array();
234
235         $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact 
236                 left join glink on glink.gcid = gcontact.id 
237                 where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d)
238                 and not gcontact.id in ( select gcid from gcign where uid = %d )
239                 group by glink.gcid order by total desc limit %d, %d ",
240                 intval($uid),
241                 intval($uid),
242                 intval($uid),
243                 intval($start),
244                 intval($limit)
245         );
246
247         if(count($r) && count($r) >= ($limit -1))
248                 return $r;
249
250         $r2 = q("SELECT gcontact.* from gcontact 
251                 left join glink on glink.gcid = gcontact.id 
252                 where glink.uid = 0 and glink.cid = 0 and not gcontact.nurl in ( select nurl from contact where uid = %d)
253                 and not gcontact.id in ( select gcid from gcign where uid = %d )
254                 order by rand() limit %d, %d ",
255                 intval($uid),
256                 intval($uid),
257                 intval($start),
258                 intval($limit)
259         );
260
261
262         return array_merge($r,$r2);
263
264 }
265
266 function update_suggestions() {
267
268         $a = get_app();
269
270         $done = array();
271
272         poco_load(0,0,$a->get_baseurl() . '/poco');
273
274         $done[] = $a->get_baseurl() . '/poco';
275
276         if(strlen(get_config('system','directory_submit_url'))) {
277                 $x = fetch_url('http://dir.friendica.com/pubsites');
278                 if($x) {
279                         $j = json_decode($x);
280                         if($j->entries) {
281                                 foreach($j->entries as $entry) {
282                                         $url = $entry->url . '/poco';
283                                         if(! in_array($url,$done))
284                                                 poco_load(0,0,$entry->url . '/poco');
285                                 }
286                         }
287                 }
288         }
289
290         $r = q("select distinct(poco) as poco from contact where network = '%s'",
291                 dbesc(NETWORK_DFRN)
292         );
293
294         if(count($r)) {
295                 foreach($r as $rr) {
296                         $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
297                         if(! in_array($base,$done))
298                                 poco_load(0,0,$base);
299                 }
300         }
301 }