Get rid of the "item" table, enhanced "post" tables
[friendica.git/.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\Database\DBA;
25 use Friendica\Model\ItemURI;
26
27 /**
28  * Do some repairs in database entries
29  * @todo This class can be deleted without replacement when the item table is removed
30  */
31 class RepairDatabase
32 {
33         public static function execute()
34         {
35                 // Ensure that there are no "uri-id", "parent-uri-id" or "thr-parent-id" fields that are NULL
36                 $items = DBA::select('item', ['id', 'uri', 'guid'], ["`uri-id` IS NULL"]);
37                 while ($item = DBA::fetch($items)) {
38                         $uriid = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
39                         DBA::update('item', ['uri-id' => $uriid], ['id' => $item['id']]);
40                 }
41                 DBA::close($items);
42
43                 $items = DBA::select('item', ['id', 'parent-uri'], ["`parent-uri-id` IS NULL"]);
44                 while ($item = DBA::fetch($items)) {
45                         $uriid = ItemURI::getIdByURI($item['parent-uri']);
46                         DBA::update('item', ['parent-uri-id' => $uriid], ['id' => $item['id']]);
47                 }
48                 DBA::close($items);
49
50                 $items = DBA::select('item', ['id', 'thr-parent'], ["`thr-parent-id` IS NULL"]);
51                 while ($item = DBA::fetch($items)) {
52                         $uriid = ItemURI::getIdByURI($item['thr-parent']);
53                         DBA::update('item', ['thr-parent-id' => $uriid], ['id' => $item['id']]);
54                 }
55                 DBA::close($items);
56
57                 // Ensure that all uri-id are set correctly
58                 DBA::e("UPDATE `item` INNER JOIN `item-uri` ON `item-uri`.`uri` = `item`.`uri`
59                         SET `uri-id` = `item-uri`.`id` WHERE `item`.`uri-id` != `item-uri`.`id` AND `item`.`uri` != ?", '');
60                 DBA::e("UPDATE `item` INNER JOIN `item-uri` ON `item-uri`.`uri` = `item`.`parent-uri`
61                         SET `parent-uri-id` = `item-uri`.`id` WHERE `item`.`parent-uri-id` != `item-uri`.`id` AND `item`.`parent-uri` != ?", '');
62                 DBA::e("UPDATE `item` INNER JOIN `item-uri` ON `item-uri`.`uri` = `item`.`thr-parent`
63                         SET `thr-parent-id` = `item-uri`.`id` WHERE `item`.`thr-parent-id` != `item-uri`.`id` AND `item`.`thr-parent` != ?", '');
64
65                 // Delete orphaned data from notify table.
66                 DBA::e("DELETE FROM `notify` WHERE NOT `type` IN (1, 2, 16, 32, 512) AND NOT `iid` IN (SELECT `id` FROM `item`)");
67         }
68 }