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\Model\ItemURI;
29 use Friendica\Util\Strings;
32 * Do some repairs in database entries
37 public static function execute()
39 // Sometimes there seem to be issues where the "self" contact vanishes.
40 // We haven't found the origin of the problem by now.
42 $users = DBA::select('user', ['uid'], ["NOT EXISTS (SELECT `uid` FROM `contact` WHERE `contact`.`uid` = `user`.`uid` AND `contact`.`self`)"]);
43 while ($user = DBA::fetch($users)) {
44 Logger::notice('Create missing self contact', ['user'=> $user['uid']]);
45 Contact::createSelfFromUserId($user['uid']);
49 // There was an issue where the nick vanishes from the contact table
50 DBA::e("UPDATE `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid` SET `nick` = `nickname` WHERE `self` AND `nick`=''");
53 /// - remove thread entries without item
54 /// - remove sign entries without item
55 /// - remove children when parent got lost
56 /// - set contact-id in item when not present
58 // Add intro entries for pending contacts
59 // We don't do this for DFRN entries since such revived contact requests seem to mostly fail.
60 $pending_contacts = DBA::p("SELECT `uid`, `id`, `url`, `network`, `created` FROM `contact`
61 WHERE `pending` AND `rel` IN (?, ?) AND `network` != ? AND `uid` != ?
62 AND NOT EXISTS (SELECT `id` FROM `intro` WHERE `contact-id` = `contact`.`id`)",
63 0, Contact::FOLLOWER, Protocol::DFRN, 0);
64 while ($contact = DBA::fetch($pending_contacts)) {
65 DBA::insert('intro', ['uid' => $contact['uid'], 'contact-id' => $contact['id'], 'blocked' => false,
66 'hash' => Strings::getRandomHex(), 'datetime' => $contact['created']]);
68 DBA::close($pending_contacts);
70 // Ensure that there are no "uri-id", "parent-uri-id" or "thr-parent-id" fields that are NULL
71 $items = DBA::select('item', ['id', 'uri', 'guid'], ["`uri-id` IS NULL"]);
72 while ($item = DBA::fetch($items)) {
73 $uriid = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
74 DBA::update('item', ['uri-id' => $uriid], ['id' => $item['id']]);
78 $items = DBA::select('item', ['id', 'parent-uri'], ["`parent-uri-id` IS NULL"]);
79 while ($item = DBA::fetch($items)) {
80 $uriid = ItemURI::getIdByURI($item['parent-uri']);
81 DBA::update('item', ['parent-uri-id' => $uriid], ['id' => $item['id']]);
85 $items = DBA::select('item', ['id', 'thr-parent'], ["`thr-parent-id` IS NULL"]);
86 while ($item = DBA::fetch($items)) {
87 $uriid = ItemURI::getIdByURI($item['thr-parent']);
88 DBA::update('item', ['thr-parent-id' => $uriid], ['id' => $item['id']]);