]> git.mxchange.org Git - friendica.git/blob - src/Worker/MergeContact.php
15fedd4b86edcea5ee9a059d3314ca66408b966e
[friendica.git] / src / Worker / MergeContact.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\Worker;
23
24 use Friendica\Core\Logger;
25 use Friendica\Database\DBA;
26 use Friendica\Database\DBStructure;
27 use Friendica\Model\Contact;
28
29 class MergeContact
30 {
31         /**
32          * Replace all occurences of the given contact id and replace it
33          *
34          * @param integer $new_cid New contact id
35          * @param integer $old_cid Old contact id
36          * @param integer $uid User id
37          */
38         public static function execute(int $new_cid, int $old_cid, int $uid)
39         {
40                 if (empty($new_cid) || empty($old_cid) || ($new_cid == $old_cid)) {
41                         // Invalid request
42                         return;
43                 }
44
45                 Logger::info('Handling duplicate', ['search' => $old_cid, 'replace' => $new_cid]);
46
47                 foreach (['item', 'thread', 'post-user', 'post-thread-user'] as $table) {
48                         if (DBStructure::existsTable($table)) {
49                                 DBA::update($table, ['contact-id' => $new_cid], ['contact-id' => $old_cid]);
50                         }
51                 }
52                 DBA::update('mail', ['contact-id' => $new_cid], ['contact-id' => $old_cid]);
53                 DBA::update('photo', ['contact-id' => $new_cid], ['contact-id' => $old_cid]);
54                 DBA::update('event', ['cid' => $new_cid], ['cid' => $old_cid]);
55
56                 // These fields only contain public contact entries (uid = 0)
57                 if ($uid == 0) {
58                         DBA::update('post-tag', ['cid' => $new_cid], ['cid' => $old_cid]);
59                         DBA::delete('post-tag', ['cid' => $old_cid]);
60                         foreach (['item', 'post', 'post-thread', 'post-user', 'post-thread-user'] as $table) {
61                                 if (DBStructure::existsTable($table)) {
62                                         DBA::update($table, ['author-id' => $new_cid], ['author-id' => $old_cid]);
63                                         DBA::update($table, ['owner-id' => $new_cid], ['owner-id' => $old_cid]);
64                                         DBA::update($table, ['causer-id' => $new_cid], ['causer-id' => $old_cid]);
65                                 }
66                         }
67                         if (DBStructure::existsTable('thread')) {
68                                 DBA::update('thread', ['author-id' => $new_cid], ['author-id' => $old_cid]);
69                                 DBA::update('thread', ['owner-id' => $new_cid], ['owner-id' => $old_cid]);
70                         }
71                 } else {
72                         self::mergePersonalContacts($new_cid, $old_cid);
73                 }
74
75                 // Remove the duplicate
76                 Contact::deleteById($old_cid);
77         }
78
79         /**
80          * Merge important fields between two contacts
81          *
82          * @param integer $first
83          * @param integer $duplicate
84          * @return void
85          */
86         private static function mergePersonalContacts(int $first, int $duplicate)
87         {
88                 $fields = ['self', 'remote_self', 'rel', 'prvkey', 'subhub', 'hub-verify', 'priority', 'writable', 'archive', 'pending',
89                         'rating', 'notify_new_posts', 'fetch_further_information', 'ffi_keyword_denylist', 'block_reason'];
90                 $c1 = Contact::getById($first, $fields);
91                 $c2 = Contact::getById($duplicate, $fields);
92
93                 $ctarget = $c1;
94
95                 if ($c1['self'] || $c2['self']) {
96                         return;
97                 }
98
99                 $ctarget['rel'] = $c1['rel'] | $c2['rel'];
100                 foreach (['prvkey', 'hub-verify', 'priority', 'rating', 'fetch_further_information', 'ffi_keyword_denylist', 'block_reason'] as $field) {
101                         $ctarget[$field] = $c1[$field] ?: $c2[$field];
102                 }
103
104                 foreach (['remote_self', 'subhub', 'writable', 'notify_new_posts'] as $field) {
105                         $ctarget[$field] = $c1[$field] || $c2[$field];
106                 }
107
108                 foreach (['archive', 'pending'] as $field) {
109                         $ctarget[$field] = $c1[$field] && $c2[$field];
110                 }
111
112                 $data = [];
113
114                 foreach ($fields as $field) {
115                         if ($ctarget[$field] != $c1[$field]) {
116                                 $data[$field] = $ctarget[$field];
117                         }
118                 }
119
120                 if (empty($data)) {
121                         return;
122                 }
123                 Contact::update($data, ['id' => $first]);
124         }
125 }