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\Database\DBA;
25 use Friendica\Model\ItemURI;
28 * Do some repairs in database entries
33 public static function execute()
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']]);
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']]);
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']]);
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` != ?", '');
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`)");