3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Console;
24 use Friendica\Core\L10n;
25 use Friendica\Database\Database;
26 use Friendica\Model\Contact;
29 * tool to find and merge duplicated contact entries.
31 class MergeContacts extends \Asika\SimpleConsole\Console
33 protected $helpOptions = ['h', 'help', '?'];
45 protected function getHelp()
48 console mergecontacts - Merge duplicated contact entries
50 bin/console mergecontacts
53 bin/console mergecontacts
54 Remove duplicated contact entries
57 -h|--help|-? Show help information
58 -e|--execute Execute the merge
63 public function __construct(Database $dba, L10n $l10n, array $argv = null)
65 parent::__construct($argv);
71 protected function doExecute(): int
73 $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");
74 while ($duplicate = $this->dba->fetch($duplicates)) {
75 $this->out($this->l10n->t('%d %s, %d duplicates.', $duplicate['uri-id'], $duplicate['url'], $duplicate['total']));
76 if ($this->getOption(['e', 'execute'], false)) {
77 if (empty($duplicate['uri-id'])) {
78 $this->err($this->l10n->t('uri-id is empty for contact %s.', $duplicate['url']));
81 $this->mergeContacts($duplicate['uri-id']);
87 private function mergeContacts(int $uriid)
89 $first = $this->dba->selectFirst('contact', ['id', 'nurl', 'url'], ["`uri-id` = ? AND `nurl` != ? AND `url` != ?", $uriid, '', ''], ['order' => ['id']]);
91 $this->err($this->l10n->t('No valid first contact found for uri-id %d.', $uriid));
94 $this->out($first['url']);
96 $duplicates = $this->dba->select('contact', ['id', 'nurl', 'url'], ['uri-id' => $uriid]);
97 while ($duplicate = $this->dba->fetch($duplicates)) {
98 if ($first['id'] == $duplicate['id']) {
101 if ($first['url'] != $duplicate['url']) {
102 $this->err($this->l10n->t('Wrong duplicate found for uri-id %d in %d (url: %s != %s).', $uriid, $duplicate['id'], $first['url'], $duplicate['url']));
105 if ($first['nurl'] != $duplicate['nurl']) {
106 $this->err($this->l10n->t('Wrong duplicate found for uri-id %d in %d (nurl: %s != %s).', $uriid, $duplicate['id'], $first['nurl'], $duplicate['nurl']));
109 $this->out($duplicate['id'] . "\t" . $duplicate['url']);
110 $this->mergeContactInTables($duplicate['id'], $first['id']);
114 private function mergeContactInTables(int $from, int $to)
116 $this->out($from . "\t=> " . $to);
118 foreach (['post', 'post-thread', 'post-thread-user', 'post-user'] as $table) {
119 foreach (['author-id', 'causer-id', 'owner-id'] as $field) {
120 $this->updateTable($table, $field, $from, $to, false);
124 $this->updateTable('contact-relation', 'cid', $from, $to, true);
125 $this->updateTable('contact-relation', 'relation-cid', $from, $to, true);
126 $this->updateTable('event', 'cid', $from, $to, false);
127 $this->updateTable('fsuggest', 'cid', $from, $to, false);
128 $this->updateTable('group', 'cid', $from, $to, false);
129 $this->updateTable('group_member', 'contact-id', $from, $to, true);
130 $this->updateTable('intro', 'contact-id', $from, $to, false);
131 $this->updateTable('intro', 'suggest-cid', $from, $to, false);
132 $this->updateTable('mail', 'author-id', $from, $to, false);
133 $this->updateTable('mail', 'contact-id', $from, $to, false);
134 $this->updateTable('notification', 'actor-id', $from, $to, false);
135 $this->updateTable('photo', 'contact-id', $from, $to, false);
136 $this->updateTable('post-tag', 'cid', $from, $to, true);
137 $this->updateTable('post-user', 'contact-id', $from, $to, false);
138 $this->updateTable('post-thread-user', 'contact-id', $from, $to, false);
139 $this->updateTable('user-contact', 'cid', $from, $to, true);
141 if (!Contact::deleteById($from)) {
142 $this->err($this->l10n->t('Deletion of id %d failed', $from));
144 $this->out($this->l10n->t('Deletion of id %d was successful', $from));
148 private function updateTable(string $table, string $field, int $from, int $to, bool $in_unique_key)
150 $this->out($this->l10n->t('Updating "%s" in "%s" from %d to %d', $field, $table, $from, $to), false);
151 if ($this->dba->exists($table, [$field => $from])) {
152 $this->out($this->l10n->t(' - found'), false);
153 if ($in_unique_key) {
154 $params = ['ignore' => true];
158 if (!$this->dba->update($table, [$field => $to], [$field => $from], [], $params)) {
159 $this->out($this->l10n->t(' - failed'), false);
161 $this->out($this->l10n->t(' - success'), false);
163 if ($in_unique_key && $this->dba->exists($table, [$field => $from])) {
164 $this->dba->delete($table, [$field => $from]);
165 $this->out($this->l10n->t(' - deleted'), false);
168 $this->out($this->l10n->t(' - done'));