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