3 * @copyright Copyright (C) 2010-2021, 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\Worker;
24 use Friendica\Core\Logger;
25 use Friendica\Database\DBA;
26 use Friendica\Database\DBStructure;
31 * Replace all occurences of the given contact id and replace it
33 * @param integer $new_cid
34 * @param integer $old_cid
37 public static function execute(int $new_cid, int $old_cid, int $uid)
39 if (empty($new_cid) || empty($old_cid) || ($new_cid == $old_cid)) {
44 Logger::info('Handling duplicate', ['search' => $old_cid, 'replace' => $new_cid]);
46 foreach (['item', 'thread', 'post-user', 'post-thread-user'] as $table) {
47 if (DBStructure::existsTable($table)) {
48 DBA::update($table, ['contact-id' => $new_cid], ['contact-id' => $old_cid]);
51 DBA::update('mail', ['contact-id' => $new_cid], ['contact-id' => $old_cid]);
52 DBA::update('photo', ['contact-id' => $new_cid], ['contact-id' => $old_cid]);
53 DBA::update('event', ['cid' => $new_cid], ['cid' => $old_cid]);
55 // These fields only contain public contact entries (uid = 0)
57 DBA::update('post-tag', ['cid' => $new_cid], ['cid' => $old_cid]);
58 DBA::delete('post-tag', ['cid' => $old_cid]);
59 foreach (['item', 'post', 'post-thread', 'post-user', 'post-thread-user'] as $table) {
60 if (DBStructure::existsTable($table)) {
61 DBA::update($table, ['author-id' => $new_cid], ['author-id' => $old_cid]);
62 DBA::update($table, ['owner-id' => $new_cid], ['owner-id' => $old_cid]);
63 DBA::update($table, ['causer-id' => $new_cid], ['causer-id' => $old_cid]);
66 if (DBStructure::existsTable('thread')) {
67 DBA::update('thread', ['author-id' => $new_cid], ['author-id' => $old_cid]);
68 DBA::update('thread', ['owner-id' => $new_cid], ['owner-id' => $old_cid]);
71 /// @todo Check if some other data needs to be adjusted as well, possibly the "rel" status?
74 // Remove the duplicate
75 DBA::delete('contact', ['id' => $old_cid]);