]> git.mxchange.org Git - friendica.git/blob - src/Worker/RepairDatabase.php
Merge pull request #9018 from annando/split-cronjobs
[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\Util\Strings;
29
30 /**
31  * Do some repairs in database entries
32  *
33  */
34 class RepairDatabase
35 {
36         public static function execute()
37         {
38                 // Sometimes there seem to be issues where the "self" contact vanishes.
39                 // We haven't found the origin of the problem by now.
40
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']);
45                 }
46                 DBA::close($users);
47
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`=''");
50
51                 /// @todo
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
56
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` != ?
61                                 AND NOT EXISTS (SELECT `id` FROM `intro` WHERE `contact-id` = `contact`.`id`)",
62                         0, Contact::FOLLOWER, Protocol::DFRN);
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']]);
66                 }
67                 DBA::close($pending_contacts);
68         }
69 }