2020-12-05 22:07:48 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2022-01-02 08:27:47 +01:00
|
|
|
* @copyright Copyright (C) 2010-2022, the Friendica project
|
2020-12-05 22:07:48 +01: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;
|
|
|
|
|
2022-05-09 08:27:46 +02:00
|
|
|
use Friendica\Contact\Avatar;
|
2020-12-05 22:07:48 +01:00
|
|
|
use Friendica\Core\Logger;
|
|
|
|
use Friendica\Core\Protocol;
|
|
|
|
use Friendica\Database\DBA;
|
2021-02-04 06:51:25 +01:00
|
|
|
use Friendica\Database\DBStructure;
|
2022-05-08 07:37:17 +02:00
|
|
|
use Friendica\Model\Contact;
|
2020-12-05 22:07:48 +01:00
|
|
|
use Friendica\Model\Photo;
|
2021-12-02 15:19:01 +01:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
2020-12-05 22:07:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes public contacts that aren't in use
|
|
|
|
*/
|
2020-12-05 22:58:15 +01:00
|
|
|
class RemoveUnusedContacts
|
|
|
|
{
|
|
|
|
public static function execute()
|
|
|
|
{
|
2021-04-01 06:58:53 +02:00
|
|
|
$condition = ["`id` != ? AND `uid` = ? AND NOT `self` AND NOT `nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` != ?)
|
2021-12-02 15:19:01 +01:00
|
|
|
AND (NOT `network` IN (?, ?, ?, ?, ?, ?) OR (`archive` AND `success_update` < ?))
|
2021-02-14 10:43:27 +01:00
|
|
|
AND NOT `id` IN (SELECT `author-id` FROM `post-user`) AND NOT `id` IN (SELECT `owner-id` FROM `post-user`)
|
2022-08-08 22:00:21 +02:00
|
|
|
AND NOT `id` IN (SELECT `causer-id` FROM `post-user` WHERE `causer-id` IS NOT NULL) AND NOT `id` IN (SELECT `cid` FROM `post-tag`)
|
2021-02-14 10:43:27 +01:00
|
|
|
AND NOT `id` IN (SELECT `contact-id` FROM `post-user`) AND NOT `id` IN (SELECT `cid` FROM `user-contact`)
|
2021-02-26 20:41:51 +01:00
|
|
|
AND NOT `id` IN (SELECT `cid` FROM `event`) AND NOT `id` IN (SELECT `contact-id` FROM `group_member`)
|
2021-12-02 15:19:01 +01:00
|
|
|
AND `created` < ?",
|
|
|
|
0, 0, 0, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED, Protocol::MAIL, Protocol::ACTIVITYPUB, DateTimeFormat::utc('now - 365 days'), DateTimeFormat::utc('now - 30 days')];
|
2020-12-05 22:07:48 +01:00
|
|
|
|
|
|
|
$total = DBA::count('contact', $condition);
|
|
|
|
Logger::notice('Starting removal', ['total' => $total]);
|
|
|
|
$count = 0;
|
2022-05-08 07:37:17 +02:00
|
|
|
$contacts = DBA::select('contact', ['id', 'uid', 'photo', 'thumb', 'micro'], $condition);
|
2020-12-05 22:07:48 +01:00
|
|
|
while ($contact = DBA::fetch($contacts)) {
|
2022-05-08 07:37:17 +02:00
|
|
|
Photo::delete(['uid' => $contact['uid'], 'contact-id' => $contact['id']]);
|
2022-05-09 08:27:46 +02:00
|
|
|
Avatar::deleteCache($contact);
|
2021-02-14 10:43:27 +01:00
|
|
|
|
2022-05-08 07:37:17 +02:00
|
|
|
if (DBStructure::existsTable('thread')) {
|
|
|
|
DBA::delete('thread', ['owner-id' => $contact['id']]);
|
|
|
|
DBA::delete('thread', ['author-id' => $contact['id']]);
|
|
|
|
}
|
|
|
|
if (DBStructure::existsTable('item')) {
|
|
|
|
DBA::delete('item', ['owner-id' => $contact['id']]);
|
|
|
|
DBA::delete('item', ['author-id' => $contact['id']]);
|
|
|
|
DBA::delete('item', ['causer-id' => $contact['id']]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// There should be none entry for the contact in these tables when none was found in "post-user".
|
|
|
|
// But we want to be sure since the foreign key prohibits deletion otherwise.
|
|
|
|
DBA::delete('post', ['owner-id' => $contact['id']]);
|
|
|
|
DBA::delete('post', ['author-id' => $contact['id']]);
|
|
|
|
DBA::delete('post', ['causer-id' => $contact['id']]);
|
|
|
|
|
|
|
|
DBA::delete('post-thread', ['owner-id' => $contact['id']]);
|
|
|
|
DBA::delete('post-thread', ['author-id' => $contact['id']]);
|
|
|
|
DBA::delete('post-thread', ['causer-id' => $contact['id']]);
|
2021-02-14 10:43:27 +01:00
|
|
|
|
2022-05-08 07:37:17 +02:00
|
|
|
DBA::delete('post-thread-user', ['owner-id' => $contact['id']]);
|
|
|
|
DBA::delete('post-thread-user', ['author-id' => $contact['id']]);
|
|
|
|
DBA::delete('post-thread-user', ['causer-id' => $contact['id']]);
|
2021-02-14 10:43:27 +01:00
|
|
|
|
2022-05-08 07:37:17 +02:00
|
|
|
DBA::delete('contact', ['id' => $contact['id']]);
|
|
|
|
if ((++$count % 1000) == 0) {
|
2022-08-30 21:45:30 +02:00
|
|
|
Logger::info('In removal', ['count' => $count, 'total' => $total]);
|
2020-12-05 22:07:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
DBA::close($contacts);
|
|
|
|
Logger::notice('Removal done', ['count' => $count, 'total' => $total]);
|
|
|
|
}
|
|
|
|
}
|