]> git.mxchange.org Git - friendica.git/blob - src/Worker/DiscoverContacts.php
Added "DiscoverContacts"
[friendica.git] / src / Worker / DiscoverContacts.php
1 <?php
2 /**
3  * @file src/Worker/DiscoverContacts.php
4  */
5 namespace Friendica\Worker;
6
7 use Friendica\Core\Config;
8 use Friendica\Core\Logger;
9 use Friendica\Core\Protocol;
10 use Friendica\Core\Worker;
11 use Friendica\Database\DBA;
12 use Friendica\Model\Contact;
13 use Friendica\Model\GServer;
14 use Friendica\Util\DateTimeFormat;
15 use Friendica\Util\Strings;
16
17 class DiscoverContacts
18 {
19         // Updates gcontact entries
20         public static function execute()
21         {
22                 if (!Config::get('system', 'poco_completion')) {
23                         return;
24                 }
25
26                 Logger::info('Discover contacts');
27
28                 $starttime = time();
29
30                 $contacts = DBA::select("SELECT `url`, `created`, `updated`, `last_failure`, `last_contact`, `server_url`, `network` FROM `gcontact`
31                                 WHERE `last_contact` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
32                                         `last_failure` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
33                                         `network` IN (?, ?, ?, ?, '') ORDER BY rand()",
34                                 Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED);
35
36                 $checked = 0;
37
38                 while ($contact = DBA::fetch($contacts)) {
39                         $urlparts = parse_url($contact['url']);
40                         if (empty($urlparts['scheme'])) {
41                                 DBA::update('gcontact', ['network' => Protocol::PHANTOM],
42                                         ['nurl' => Strings::normaliseLink($contact['url'])]);
43                                 continue;
44                          }
45
46                         if (in_array($urlparts['host'], ['twitter.com', 'identi.ca'])) {
47                                 $networks = ['twitter.com' => Protocol::TWITTER, 'identi.ca' => Protocol::PUMPIO];
48
49                                 DBA::update('gcontact', ['network' => $networks[$urlparts['host']]],
50                                         ['nurl' => Strings::normaliseLink($contact['url'])]);
51                                 continue;
52                         }
53
54                         $server_url = Contact::getBasepath($contact['url']);
55                         $force_update = false;
56
57                         if (!empty($contact['server_url'])) {
58                                 $force_update = (Strings::normaliseLink($contact['server_url']) != Strings::normaliseLink($server_url));
59
60                                 $server_url = $contact['server_url'];
61                         }
62
63                         if ((empty($server_url) && ($contact['network'] == Protocol::FEED)) || $force_update || GServer::check($server_url, $contact['network'])) {
64                                 Logger::info('Check profile', ['profile' => $contact['url']]);
65                                 Worker::add(PRIORITY_LOW, 'UpdateGContact', $contact['url'], 'force');
66
67                                 if (++$checked > 100) {
68                                         return;
69                                 }
70                         } else {
71                                 DBA::update('gcontact', ['last_failure' => DateTimeFormat::utcNow()],
72                                         ['nurl' => Strings::normaliseLink($contact['url'])]);
73                         }
74
75                         // Quit the loop after 3 minutes
76                         if (time() > ($starttime + 180)) {
77                                 return;
78                         }
79                 }
80         }
81 }