3 * @copyright Copyright (C) 2020, Friendica
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\Core\Protocol;
26 use Friendica\Database\DBA;
27 use Friendica\Model\Contact;
28 use Friendica\Util\Strings;
31 * Do some repairs in database entries
36 public static function execute()
38 // Sometimes there seem to be issues where the "self" contact vanishes.
39 // We haven't found the origin of the problem by now.
41 $users = DBA::select('user', ['uid'], ["NOT EXISTS (SELECT `uid` FROM `contact` WHERE `contact`.`uid` = `user`.`uid` AND `contact`.`self`)"]);
42 while ($user = DBA::fetch($users)) {
43 Logger::notice('Create missing self contact', ['user'=> $user['uid']]);
44 Contact::createSelfFromUserId($user['uid']);
48 // There was an issue where the nick vanishes from the contact table
49 DBA::e("UPDATE `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid` SET `nick` = `nickname` WHERE `self` AND `nick`=''");
52 /// - remove thread entries without item
53 /// - remove sign entries without item
54 /// - remove children when parent got lost
55 /// - set contact-id in item when not present
57 // Add intro entries for pending contacts
58 // We don't do this for DFRN entries since such revived contact requests seem to mostly fail.
59 $pending_contacts = DBA::p("SELECT `uid`, `id`, `url`, `network`, `created` FROM `contact`
60 WHERE `pending` AND `rel` IN (?, ?) AND `network` != ? AND `uid` != ?
61 AND NOT EXISTS (SELECT `id` FROM `intro` WHERE `contact-id` = `contact`.`id`)",
62 0, Contact::FOLLOWER, Protocol::DFRN, 0);
63 while ($contact = DBA::fetch($pending_contacts)) {
64 DBA::insert('intro', ['uid' => $contact['uid'], 'contact-id' => $contact['id'], 'blocked' => false,
65 'hash' => Strings::getRandomHex(), 'datetime' => $contact['created']]);
67 DBA::close($pending_contacts);