use Friendica\Core\L10n;
use Friendica\Database\Database;
+use Friendica\Model\Contact;
/**
* tool to find and merge duplicated contact entries.
Options
-h|--help|-? Show help information
- -e|--execute Execute the dropping
+ -e|--execute Execute the merge
HELP;
return $help;
}
{
parent::__construct($argv);
- $this->dba = $dba;
- $this->l10n = $l10n;
+ $this->dba = $dba;
+ $this->l10n = $l10n;
}
- protected function doExecute()
+ protected function doExecute(): int
{
$duplicates = $this->dba->p("SELECT COUNT(*) AS `total`, `uri-id`, MAX(`url`) AS `url` FROM `contact` WHERE `uid` = 0 GROUP BY `uri-id` HAVING total > 1");
while ($duplicate = $this->dba->fetch($duplicates)) {
$this->out($this->l10n->t('%d %s, %d duplicates.', $duplicate['uri-id'], $duplicate['url'], $duplicate['total']));
if ($this->getOption(['e', 'execute'], false)) {
+ if (empty($duplicate['uri-id'])) {
+ $this->err($this->l10n->t('uri-id is empty for contact %s.', $duplicate['url']));
+ continue;
+ }
$this->mergeContacts($duplicate['uri-id']);
}
}
{
$first = $this->dba->selectFirst('contact', ['id', 'nurl', 'url'], ["`uri-id` = ? AND `nurl` != ? AND `url` != ?", $uriid, '', ''], ['order' => ['id']]);
if (empty($first)) {
- $this->err($this->l10n->t('No valid first countact found for uri-id %d.', $uriid));
+ $this->err($this->l10n->t('No valid first contact found for uri-id %d.', $uriid));
return;
}
$this->out($first['url']);
$this->updateTable('post-thread-user', 'contact-id', $from, $to, false);
$this->updateTable('user-contact', 'cid', $from, $to, true);
- if (!$this->dba->delete('contact', ['id' => $from])) {
+ if (!Contact::deleteById($from)) {
$this->err($this->l10n->t('Deletion of id %d failed', $from));
} else {
$this->out($this->l10n->t('Deletion of id %d was successful', $from));
- }
+ }
}
private function updateTable(string $table, string $field, int $from, int $to, bool $in_unique_key)