]> git.mxchange.org Git - friendica.git/blob - src/Worker/MergeContact.php
6fdb0140bc04f157e36feff15ef2a8f3e4071ada
[friendica.git] / src / Worker / MergeContact.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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
27 class MergeContact
28 {
29         /**
30          * Replace all occurences of the given contact id and replace it
31          *
32          * @param integer $new_cid
33          * @param integer $old_cid
34          * @param integer $uid
35          */
36         public static function execute(int $new_cid, int $old_cid, int $uid)
37         {
38                 if (empty($new_cid) || empty($old_cid) || ($new_cid == $old_cid)) {
39                         // Invalid request
40                         return;
41                 }
42
43                 Logger::info('Handling duplicate', ['search' => $old_cid, 'replace' => $new_cid]);
44
45                 // Search and replace
46                 DBA::update('item', ['contact-id' => $new_cid], ['contact-id' => $old_cid]);
47                 DBA::update('thread', ['contact-id' => $new_cid], ['contact-id' => $old_cid]);
48                 DBA::update('mail', ['contact-id' => $new_cid], ['contact-id' => $old_cid]);
49                 DBA::update('photo', ['contact-id' => $new_cid], ['contact-id' => $old_cid]);
50                 DBA::update('event', ['cid' => $new_cid], ['cid' => $old_cid]);
51
52                 // These fields only contain public contact entries (uid = 0)
53                 if ($uid == 0) {
54                         DBA::update('post-tag', ['cid' => $new_cid], ['cid' => $old_cid]);
55                         DBA::update('item', ['author-id' => $new_cid], ['author-id' => $old_cid]);
56                         DBA::update('item', ['owner-id' => $new_cid], ['owner-id' => $old_cid]);
57                         DBA::update('thread', ['author-id' => $new_cid], ['author-id' => $old_cid]);
58                         DBA::update('thread', ['owner-id' => $new_cid], ['owner-id' => $old_cid]);
59                 } else {
60                         /// @todo Check if some other data needs to be adjusted as well, possibly the "rel" status?
61                 }
62
63                 // Remove the duplicate
64                 DBA::delete('contact', ['id' => $old_cid]);
65         }
66 }