2020-09-01 10:09:16 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2021-03-29 08:40:20 +02:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-09-01 10:09:16 +02:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Worker;
|
|
|
|
|
|
|
|
use Friendica\Core\Logger;
|
|
|
|
use Friendica\Core\Protocol;
|
|
|
|
use Friendica\Core\Worker;
|
|
|
|
use Friendica\Database\DBA;
|
2020-09-27 13:55:31 +02:00
|
|
|
use Friendica\DI;
|
2020-09-01 10:09:16 +02:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
|
|
|
|
|
|
|
/**
|
2020-12-04 06:53:11 +01:00
|
|
|
* Update federated contacts
|
2020-09-01 10:09:16 +02:00
|
|
|
*/
|
2020-12-04 06:53:11 +01:00
|
|
|
class UpdateContacts
|
2020-09-01 10:09:16 +02:00
|
|
|
{
|
|
|
|
public static function execute()
|
|
|
|
{
|
2020-12-04 06:53:11 +01:00
|
|
|
$base_condition = ['network' => array_merge(Protocol::FEDERATED, [Protocol::ZOT, Protocol::PHANTOM]), 'self' => false];
|
2020-09-01 10:09:16 +02:00
|
|
|
|
2020-12-04 06:53:11 +01:00
|
|
|
$update_limit = DI::config()->get('system', 'contact_update_limit');
|
|
|
|
if (empty($update_limit)) {
|
2020-12-03 16:47:50 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-04 06:53:11 +01:00
|
|
|
$updating = Worker::countWorkersByCommand('UpdateContact');
|
|
|
|
$limit = $update_limit - $updating;
|
|
|
|
if ($limit <= 0) {
|
|
|
|
Logger::info('The number of currently running jobs exceed the limit');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-01 23:19:47 +01:00
|
|
|
$condition = DBA::mergeConditions($base_condition,
|
2021-03-07 21:34:13 +01:00
|
|
|
["`uid` != ? AND (`last-update` < ? OR (NOT `failed` AND `last-update` < ?))",
|
2020-12-04 06:53:11 +01:00
|
|
|
0, DateTimeFormat::utc('now - 1 month'), DateTimeFormat::utc('now - 1 week')]);
|
2020-12-04 08:54:29 +01:00
|
|
|
$ids = self::getContactsToUpdate($condition, [], $limit);
|
2021-03-01 23:19:47 +01:00
|
|
|
Logger::info('Fetched federated user contacts', ['count' => count($ids)]);
|
|
|
|
|
|
|
|
$conditions = ["`id` IN (SELECT `author-id` FROM `post-user`)", "`id` IN (SELECT `owner-id` FROM `post-user`)",
|
|
|
|
"`id` IN (SELECT `causer-id` FROM `post-user`)", "`id` IN (SELECT `cid` FROM `post-tag`)",
|
|
|
|
"`id` IN (SELECT `cid` FROM `user-contact`)"];
|
|
|
|
|
|
|
|
foreach ($conditions as $contact_condition) {
|
|
|
|
$condition = DBA::mergeConditions($base_condition,
|
2021-03-07 21:34:13 +01:00
|
|
|
[$contact_condition . " AND (`last-update` < ? OR (NOT `failed` AND `last-update` < ?))",
|
2021-03-01 23:19:47 +01:00
|
|
|
DateTimeFormat::utc('now - 1 month'), DateTimeFormat::utc('now - 1 week')]);
|
|
|
|
$ids = self::getContactsToUpdate($condition, $ids, $limit);
|
|
|
|
Logger::info('Fetched interacting federated contacts', ['count' => count($ids), 'condition' => $contact_condition]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($ids) > $limit) {
|
|
|
|
$ids = array_slice($ids, 0, $limit, true);
|
|
|
|
}
|
2020-12-03 16:47:50 +01:00
|
|
|
|
2020-10-03 12:52:34 +02:00
|
|
|
if (!DI::config()->get('system', 'update_active_contacts')) {
|
|
|
|
// Add every contact (mostly failed ones) that hadn't been updated for six months
|
2020-12-04 06:53:11 +01:00
|
|
|
// and every non failed contact that hadn't been updated for a month
|
2020-10-03 12:52:34 +02:00
|
|
|
$condition = DBA::mergeConditions($base_condition,
|
2021-03-07 21:34:13 +01:00
|
|
|
["(`last-update` < ? OR (NOT `failed` AND `last-update` < ?))",
|
2020-12-04 06:53:11 +01:00
|
|
|
DateTimeFormat::utc('now - 6 month'), DateTimeFormat::utc('now - 1 month')]);
|
2020-12-04 08:54:29 +01:00
|
|
|
$previous = count($ids);
|
|
|
|
$ids = self::getContactsToUpdate($condition, $ids, $limit - $previous);
|
|
|
|
Logger::info('Fetched federated contacts', ['count' => count($ids) - $previous]);
|
2020-10-03 12:52:34 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 08:54:29 +01:00
|
|
|
$count = 0;
|
2020-10-03 12:52:34 +02:00
|
|
|
foreach ($ids as $id) {
|
2020-12-05 22:07:48 +01:00
|
|
|
if (Worker::add(PRIORITY_LOW, "UpdateContact", $id)) {
|
|
|
|
++$count;
|
|
|
|
}
|
2020-09-27 13:55:31 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 06:53:11 +01:00
|
|
|
Logger::info('Initiated update for federated contacts', ['count' => $count]);
|
2020-10-03 12:52:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns contact ids based on a given condition
|
|
|
|
*
|
|
|
|
* @param array $condition
|
|
|
|
* @param array $ids
|
|
|
|
* @return array contact ids
|
|
|
|
*/
|
2020-12-03 16:47:50 +01:00
|
|
|
private static function getContactsToUpdate(array $condition, array $ids = [], int $limit)
|
2020-10-03 12:52:34 +02:00
|
|
|
{
|
2021-03-03 08:07:29 +01:00
|
|
|
$contacts = DBA::select('contact', ['id'], $condition, ['limit' => $limit]);
|
2020-09-01 10:09:16 +02:00
|
|
|
while ($contact = DBA::fetch($contacts)) {
|
2021-03-01 23:19:47 +01:00
|
|
|
$ids[$contact['id']] = $contact['id'];
|
2020-09-01 10:09:16 +02:00
|
|
|
}
|
|
|
|
DBA::close($contacts);
|
2020-10-03 12:52:34 +02:00
|
|
|
return $ids;
|
2020-09-01 10:09:16 +02:00
|
|
|
}
|
|
|
|
}
|