]> git.mxchange.org Git - friendica.git/blob - src/Worker/RepairDatabase.php
Merge pull request #9397 from vinzv/9238-red-color-unread-messages-faded
[friendica.git] / src / Worker / RepairDatabase.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\Core\Protocol;
26 use Friendica\Database\DBA;
27 use Friendica\Model\Contact;
28 use Friendica\Model\ItemURI;
29 use Friendica\Util\Strings;
30
31 /**
32  * Do some repairs in database entries
33  *
34  */
35 class RepairDatabase
36 {
37         public static function execute()
38         {
39                 // Sometimes there seem to be issues where the "self" contact vanishes.
40                 // We haven't found the origin of the problem by now.
41
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']);
46                 }
47                 DBA::close($users);
48
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`=''");
51
52                 /// @todo
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
57
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']]);
67                 }
68                 DBA::close($pending_contacts);
69
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']]);
75                 }
76                 DBA::close($items);
77
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']]);
82                 }
83                 DBA::close($items);
84
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']]);
89                 }
90                 DBA::close($items);
91         }
92 }