]> git.mxchange.org Git - friendica.git/blob - src/Worker/UpdateGContacts.php
Add license info at Friendica PHP files
[friendica.git] / src / Worker / UpdateGContacts.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Worker;
23
24 use Friendica\Core\Logger;
25 use Friendica\Core\Protocol;
26 use Friendica\Core\Worker;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model\GContact;
30 use Friendica\Model\GServer;
31 use Friendica\Util\DateTimeFormat;
32 use Friendica\Util\Strings;
33
34 class UpdateGContacts
35 {
36         /**
37          * Updates global contacts
38          */
39         public static function execute()
40         {
41                 if (!DI::config()->get('system', 'poco_completion')) {
42                         return;
43                 }
44
45                 Logger::info('Update global contacts');
46
47                 $starttime = time();
48
49                 $contacts = DBA::p("SELECT `url`, `created`, `updated`, `last_failure`, `last_contact`, `server_url`, `network` FROM `gcontact`
50                                 WHERE `last_contact` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
51                                         `last_failure` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
52                                         `network` IN (?, ?, ?, ?, ?, '') ORDER BY rand()",
53                                 Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED);
54
55                 $checked = 0;
56
57                 while ($contact = DBA::fetch($contacts)) {
58                         $urlparts = parse_url($contact['url']);
59                         if (empty($urlparts['scheme'])) {
60                                 DBA::update('gcontact', ['network' => Protocol::PHANTOM],
61                                         ['nurl' => Strings::normaliseLink($contact['url'])]);
62                                 continue;
63                          }
64
65                         if (in_array($urlparts['host'], ['twitter.com', 'identi.ca'])) {
66                                 $networks = ['twitter.com' => Protocol::TWITTER, 'identi.ca' => Protocol::PUMPIO];
67
68                                 DBA::update('gcontact', ['network' => $networks[$urlparts['host']]],
69                                         ['nurl' => Strings::normaliseLink($contact['url'])]);
70                                 continue;
71                         }
72
73                         $server_url = GContact::getBasepath($contact['url'], true);
74                         $force_update = false;
75
76                         if (!empty($contact['server_url'])) {
77                                 $force_update = (Strings::normaliseLink($contact['server_url']) != Strings::normaliseLink($server_url));
78
79                                 $server_url = $contact['server_url'];
80                         }
81
82                         if ((empty($server_url) && ($contact['network'] == Protocol::FEED)) || $force_update || GServer::check($server_url, $contact['network'])) {
83                                 Logger::info('Check profile', ['profile' => $contact['url']]);
84                                 Worker::add(PRIORITY_LOW, 'UpdateGContact', $contact['url'], 'force');
85
86                                 if (++$checked > 100) {
87                                         return;
88                                 }
89                         } else {
90                                 DBA::update('gcontact', ['last_failure' => DateTimeFormat::utcNow()],
91                                         ['nurl' => Strings::normaliseLink($contact['url'])]);
92                         }
93
94                         // Quit the loop after 3 minutes
95                         if (time() > ($starttime + 180)) {
96                                 return;
97                         }
98                 }
99         }
100 }