]> git.mxchange.org Git - friendica.git/blob - src/Console/MergeContacts.php
spelling: unable
[friendica.git] / src / Console / MergeContacts.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Console;
23
24 use Friendica\Core\L10n;
25 use Friendica\Database\Database;
26 use Friendica\Model\Contact;
27
28 /**
29  * tool to find and merge duplicated contact entries.
30  */
31 class MergeContacts extends \Asika\SimpleConsole\Console
32 {
33         protected $helpOptions = ['h', 'help', '?'];
34
35         /**
36          * @var $dba Database
37          */
38         private $dba;
39
40         /**
41          * @var L10n
42          */
43         private $l10n;
44
45         protected function getHelp()
46         {
47                 $help = <<<HELP
48 console mergecontacts - Merge duplicated contact entries
49 Synopsis
50         bin/console mergecontacts
51
52 Description
53         bin/console mergecontacts
54                 Remove duplicated contact entries
55
56 Options
57         -h|--help|-? Show help information
58         -e|--execute Execute the merge
59 HELP;
60                 return $help;
61         }
62
63         public function __construct(Database $dba, L10n $l10n, array $argv = null)
64         {
65                 parent::__construct($argv);
66
67                 $this->dba  = $dba;
68                 $this->l10n = $l10n;
69         }
70
71         protected function doExecute(): int
72         {
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']));
79                                         continue;
80                                 }
81                                 $this->mergeContacts($duplicate['uri-id']);
82                         }
83                 }
84                 return 0;
85         }
86
87         private function mergeContacts(int $uriid)
88         {
89                 $first = $this->dba->selectFirst('contact', ['id', 'nurl', 'url'], ["`uri-id` = ? AND `nurl` != ? AND `url` != ?", $uriid, '', ''], ['order' => ['id']]);
90                 if (empty($first)) {
91                         $this->err($this->l10n->t('No valid first contact found for uri-id %d.', $uriid));
92                         return;
93                 }
94                 $this->out($first['url']);
95
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']) {
99                                 continue;
100                         }
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']));
103                                 continue;
104                         }
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']));
107                                 continue;
108                         }
109                         $this->out($duplicate['id'] . "\t" . $duplicate['url']);
110                         $this->mergeContactInTables($duplicate['id'], $first['id']);
111                 }
112         }
113
114         private function mergeContactInTables(int $from, int $to)
115         {
116                 $this->out($from . "\t=> " . $to);
117
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);
121                         }
122                 }
123
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);
140
141                 if (!Contact::deleteById($from)) {
142                         $this->err($this->l10n->t('Deletion of id %d failed', $from));
143                 } else {
144                         $this->out($this->l10n->t('Deletion of id %d was successful', $from));
145                 }
146         }
147
148         private function updateTable(string $table, string $field, int $from, int $to, bool $in_unique_key)
149         {
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];
155                         } else {
156                                 $params = [];
157                         }
158                         if (!$this->dba->update($table, [$field => $to], [$field => $from], [], $params)) {
159                                 $this->out($this->l10n->t(' - failed'), false);
160                         } else {
161                                 $this->out($this->l10n->t(' - success'), false);
162                         }
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);
166                         }
167                 }
168                 $this->out($this->l10n->t(' - done'));
169         }
170 }