]> git.mxchange.org Git - friendica.git/blob - src/Console/MergeContacts.php
Move server domain pattern blocklist features to its own class
[friendica.git] / src / Console / MergeContacts.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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
27 /**
28  * tool to find and merge duplicated contact entries.
29  */
30 class MergeContacts extends \Asika\SimpleConsole\Console
31 {
32         protected $helpOptions = ['h', 'help', '?'];
33
34         /**
35          * @var $dba Database
36          */
37         private $dba;
38
39         /**
40          * @var L10n
41          */
42         private $l10n;
43
44         protected function getHelp()
45         {
46                 $help = <<<HELP
47 console mergecontacts - Merge duplicated contact entries
48 Synopsis
49         bin/console mergecontacts
50
51 Description
52         bin/console mergecontacts
53                 Remove duplicated contact entries
54
55 Options
56         -h|--help|-? Show help information
57         -e|--execute Execute the merge
58 HELP;
59                 return $help;
60         }
61
62         public function __construct(Database $dba, L10n $l10n, array $argv = null)
63         {
64                 parent::__construct($argv);
65
66                 $this->dba  = $dba;
67                 $this->l10n = $l10n;
68         }
69
70         protected function doExecute(): int
71         {
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                                 if (empty($duplicate['uri-id'])) {
77                                         $this->err($this->l10n->t('uri-id is empty for contact %s.', $duplicate['url']));
78                                         continue;
79                                 }
80                                 $this->mergeContacts($duplicate['uri-id']);
81                         }
82                 }
83                 return 0;
84         }
85
86         private function mergeContacts(int $uriid)
87         {
88                 $first = $this->dba->selectFirst('contact', ['id', 'nurl', 'url'], ["`uri-id` = ? AND `nurl` != ? AND `url` != ?", $uriid, '', ''], ['order' => ['id']]);
89                 if (empty($first)) {
90                         $this->err($this->l10n->t('No valid first countact found for uri-id %d.', $uriid));
91                         return;
92                 }
93                 $this->out($first['url']);
94
95                 $duplicates = $this->dba->select('contact', ['id', 'nurl', 'url'], ['uri-id' => $uriid]);
96                 while ($duplicate = $this->dba->fetch($duplicates)) {
97                         if ($first['id'] == $duplicate['id']) {
98                                 continue;
99                         }
100                         if ($first['url'] != $duplicate['url']) {
101                                 $this->err($this->l10n->t('Wrong duplicate found for uri-id %d in %d (url: %s != %s).', $uriid, $duplicate['id'], $first['url'], $duplicate['url']));
102                                 continue;
103                         }
104                         if ($first['nurl'] != $duplicate['nurl']) {
105                                 $this->err($this->l10n->t('Wrong duplicate found for uri-id %d in %d (nurl: %s != %s).', $uriid, $duplicate['id'], $first['nurl'], $duplicate['nurl']));
106                                 continue;
107                         }
108                         $this->out($duplicate['id'] . "\t" . $duplicate['url']);
109                         $this->mergeContactInTables($duplicate['id'], $first['id']);
110                 }
111         }
112
113         private function mergeContactInTables(int $from, int $to)
114         {
115                 $this->out($from . "\t=> " . $to);
116
117                 foreach (['post', 'post-thread', 'post-thread-user', 'post-user'] as $table) {
118                         foreach (['author-id', 'causer-id', 'owner-id'] as $field) {
119                                 $this->updateTable($table, $field, $from, $to, false);
120                         }
121                 }
122
123                 $this->updateTable('contact-relation', 'cid', $from, $to, true);
124                 $this->updateTable('contact-relation', 'relation-cid', $from, $to, true);
125                 $this->updateTable('event', 'cid', $from, $to, false);
126                 $this->updateTable('fsuggest', 'cid', $from, $to, false);
127                 $this->updateTable('group', 'cid', $from, $to, false);
128                 $this->updateTable('group_member', 'contact-id', $from, $to, true);
129                 $this->updateTable('intro', 'contact-id', $from, $to, false);
130                 $this->updateTable('intro', 'suggest-cid', $from, $to, false);
131                 $this->updateTable('mail', 'author-id', $from, $to, false);
132                 $this->updateTable('mail', 'contact-id', $from, $to, false);
133                 $this->updateTable('notification', 'actor-id', $from, $to, false);
134                 $this->updateTable('photo', 'contact-id', $from, $to, false);
135                 $this->updateTable('post-tag', 'cid', $from, $to, true);
136                 $this->updateTable('post-user', 'contact-id', $from, $to, false);
137                 $this->updateTable('post-thread-user', 'contact-id', $from, $to, false);
138                 $this->updateTable('user-contact', 'cid', $from, $to, true);
139
140                 if (!$this->dba->delete('contact', ['id' => $from])) {
141                         $this->err($this->l10n->t('Deletion of id %d failed', $from));
142                 } else {
143                         $this->out($this->l10n->t('Deletion of id %d was successful', $from));
144                 }
145         }
146
147         private function updateTable(string $table, string $field, int $from, int $to, bool $in_unique_key)
148         {
149                 $this->out($this->l10n->t('Updating "%s" in "%s" from %d to %d', $field, $table, $from, $to), false);
150                 if ($this->dba->exists($table, [$field => $from])) {
151                         $this->out($this->l10n->t(' - found'), false);
152                         if ($in_unique_key) {
153                                 $params = ['ignore' => true];
154                         } else {
155                                 $params = [];
156                         }
157                         if (!$this->dba->update($table, [$field => $to], [$field => $from], [], $params)) {
158                                 $this->out($this->l10n->t(' - failed'), false);
159                         } else {
160                                 $this->out($this->l10n->t(' - success'), false);
161                         }
162                         if ($in_unique_key && $this->dba->exists($table, [$field => $from])) {
163                                 $this->dba->delete($table, [$field => $from]);
164                                 $this->out($this->l10n->t(' - deleted'), false);
165                         }
166                 }
167                 $this->out($this->l10n->t(' - done'));
168         }
169 }