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