From a65b0d36e7a0ae0cddb367938c235651e44b40b9 Mon Sep 17 00:00:00 2001 From: BentiGorlich Date: Thu, 26 Dec 2024 09:54:31 +0000 Subject: [PATCH] Remove default local domain for entries (#1280) Co-authored-by: Melroy van den Berg --- docs/02-admin/04-running-mbin/05-cli.md | 12 +++- ...oveRemoteEntriesFromLocalDomainCommand.php | 63 +++++++++++++++++++ src/Service/DomainManager.php | 9 +-- 3 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 src/Command/Update/RemoveRemoteEntriesFromLocalDomainCommand.php diff --git a/docs/02-admin/04-running-mbin/05-cli.md b/docs/02-admin/04-running-mbin/05-cli.md index cb8b2e8a9..83f2459c3 100644 --- a/docs/02-admin/04-running-mbin/05-cli.md +++ b/docs/02-admin/04-running-mbin/05-cli.md @@ -344,5 +344,15 @@ This command removes post and user duplicates by their ActivityPub ID. Usage: ```bash -php bin/console mbin:user:create [-r|--remove] [--admin] [--moderator] [--] +php bin/console mbin:user:create [-r|--remove] [--admin] [--moderator] +``` + +### Update-Local-Domain + +This command will remove all remote posts from belonging to the local domain. This command is only relevant for instances +created before v1.7.4 as the local domain was the fallback if no domain could be extracted from a post. + +Usage: +```bash +php bin/console mbin:update:local-domain ``` diff --git a/src/Command/Update/RemoveRemoteEntriesFromLocalDomainCommand.php b/src/Command/Update/RemoveRemoteEntriesFromLocalDomainCommand.php new file mode 100644 index 000000000..425300ce8 --- /dev/null +++ b/src/Command/Update/RemoveRemoteEntriesFromLocalDomainCommand.php @@ -0,0 +1,63 @@ +settingsManager->get('KBIN_DOMAIN'); + $domainName = preg_replace('/^www\./i', '', parse_url($domainName)['host']); + + $domain = $this->repository->findOneByName($domainName); + if (!$domain) { + $io->warning(\sprintf('There is no local domain like %s', $domainName)); + + return Command::SUCCESS; + } + + $countBeforeSql = 'SELECT COUNT(*) as ctn FROM entry WHERE domain_id = :dId'; + $stmt1 = $this->entityManager->getConnection()->prepare($countBeforeSql); + $countBefore = \intval($stmt1->executeQuery(['dId' => $domain->getId()])->fetchOne()); + + $sql = 'UPDATE entry SET domain_id = NULL WHERE domain_id = :dId AND ap_id IS NOT NULL'; + $stmt2 = $this->entityManager->getConnection()->prepare($sql); + $stmt2->executeStatement(['dId' => $domain->getId()]); + + $countAfterSql = 'SELECT COUNT(*) as ctn FROM entry WHERE domain_id = :dId'; + $stmt3 = $this->entityManager->getConnection()->prepare($countAfterSql); + $countAfter = \intval($stmt3->executeQuery(['dId' => $domain->getId()])->fetchOne()); + + $sql = 'UPDATE domain SET entry_count = :c WHERE id = :dId'; + $stmt4 = $this->entityManager->getConnection()->prepare($sql); + $stmt4->executeStatement(['c' => $countAfter, 'dId' => $domain->getId()]); + + $io->success(\sprintf('Removed %d entries from the domain %s, now only %d entries are left', $countBefore - $countAfter, $domainName, $countAfter)); + + return Command::SUCCESS; + } +} diff --git a/src/Service/DomainManager.php b/src/Service/DomainManager.php index 443abcbcb..823a23657 100644 --- a/src/Service/DomainManager.php +++ b/src/Service/DomainManager.php @@ -23,9 +23,12 @@ public function __construct( ) { } - public function extract(DomainInterface $subject): DomainInterface + public function extract(DomainInterface $subject): void { - $domainName = $subject->getUrl() ?? 'https://'.$this->settingsManager->get('KBIN_DOMAIN'); + $domainName = $subject->getUrl(); + if (!$domainName) { + return; + } $domainName = preg_replace('/^www\./i', '', parse_url($domainName)['host']); @@ -41,8 +44,6 @@ public function extract(DomainInterface $subject): DomainInterface $domain->updateCounts(); $this->entityManager->flush(); - - return $subject; } public function subscribe(Domain $domain, User $user): void