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