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;
28 * tool to find and merge duplicated contact entries.
30 class MergeContacts extends \Asika\SimpleConsole\Console
32 protected $helpOptions = ['h', 'help', '?'];
44 protected function getHelp()
47 console mergecontacts - Merge duplicated contact entries
49 bin/console mergecontacts
52 bin/console mergecontacts
53 Remove duplicated contact entries
56 -h|--help|-? Show help information
57 -e|--execute Execute the merge
62 public function __construct(Database $dba, L10n $l10n, array $argv = null)
64 parent::__construct($argv);
70 protected function doExecute()
72 $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");
73 while ($duplicate = $this->dba->fetch($duplicates)) {
74 $this->out($this->l10n->t('%d %s, %d duplicates.', $duplicate['uri-id'], $duplicate['url'], $duplicate['total']));
75 if ($this->getOption(['e', 'execute'], false)) {
76 $this->mergeContacts($duplicate['uri-id']);
82 private function mergeContacts(int $uriid)
84 $first = $this->dba->selectFirst('contact', ['id', 'nurl', 'url'], ["`uri-id` = ? AND `nurl` != ? AND `url` != ?", $uriid, '', ''], ['order' => ['id']]);
86 $this->err($this->l10n->t('No valid first countact found for uri-id %d.', $uriid));
89 $this->out($first['url']);
91 $duplicates = $this->dba->select('contact', ['id', 'nurl', 'url'], ['uri-id' => $uriid]);
92 while ($duplicate = $this->dba->fetch($duplicates)) {
93 if ($first['id'] == $duplicate['id']) {
96 if ($first['url'] != $duplicate['url']) {
97 $this->err($this->l10n->t('Wrong duplicate found for uri-id %d in %d (url: %s != %s).', $uriid, $duplicate['id'], $first['url'], $duplicate['url']));
100 if ($first['nurl'] != $duplicate['nurl']) {
101 $this->err($this->l10n->t('Wrong duplicate found for uri-id %d in %d (nurl: %s != %s).', $uriid, $duplicate['id'], $first['nurl'], $duplicate['nurl']));
104 $this->out($duplicate['id'] . "\t" . $duplicate['url']);
105 $this->mergeContactInTables($duplicate['id'], $first['id']);
109 private function mergeContactInTables(int $from, int $to)
111 $this->out($from . "\t=> " . $to);
113 foreach (['post', 'post-thread', 'post-thread-user', 'post-user'] as $table) {
114 foreach (['author-id', 'causer-id', 'owner-id'] as $field) {
115 $this->updateTable($table, $field, $from, $to, false);
119 $this->updateTable('contact-relation', 'cid', $from, $to, true);
120 $this->updateTable('contact-relation', 'relation-cid', $from, $to, true);
121 $this->updateTable('event', 'cid', $from, $to, false);
122 $this->updateTable('fsuggest', 'cid', $from, $to, false);
123 $this->updateTable('group', 'cid', $from, $to, false);
124 $this->updateTable('group_member', 'contact-id', $from, $to, true);
125 $this->updateTable('intro', 'contact-id', $from, $to, false);
126 $this->updateTable('intro', 'suggest-cid', $from, $to, false);
127 $this->updateTable('mail', 'author-id', $from, $to, false);
128 $this->updateTable('mail', 'contact-id', $from, $to, false);
129 $this->updateTable('notification', 'actor-id', $from, $to, false);
130 $this->updateTable('photo', 'contact-id', $from, $to, false);
131 $this->updateTable('post-tag', 'cid', $from, $to, true);
132 $this->updateTable('post-user', 'contact-id', $from, $to, false);
133 $this->updateTable('post-thread-user', 'contact-id', $from, $to, false);
134 $this->updateTable('user-contact', 'cid', $from, $to, true);
136 if (!$this->dba->delete('contact', ['id' => $from])) {
137 $this->err($this->l10n->t('Deletion of id %d failed', $from));
139 $this->out($this->l10n->t('Deletion of id %d was successful', $from));
143 private function updateTable(string $table, string $field, int $from, int $to, bool $in_unique_key)
145 $this->out($this->l10n->t('Updating "%s" in "%s" from %d to %d', $field, $table, $from, $to), false);
146 if ($this->dba->exists($table, [$field => $from])) {
147 $this->out($this->l10n->t(' - found'), false);
148 if ($in_unique_key) {
149 $params = ['ignore' => true];
153 if (!$this->dba->update($table, [$field => $to], [$field => $from], [], $params)) {
154 $this->out($this->l10n->t(' - failed'), false);
156 $this->out($this->l10n->t(' - success'), false);
158 if ($in_unique_key && $this->dba->exists($table, [$field => $from])) {
159 $this->dba->delete($table, [$field => $from]);
160 $this->out($this->l10n->t(' - deleted'), false);
163 $this->out($this->l10n->t(' - done'));