friendica-github/src/Worker/RepairDatabase.php

93 lines
3.5 KiB
PHP
Raw Normal View History

<?php
/**
* @copyright Copyright (C) 2020, Friendica
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Worker;
use Friendica\Core\Logger;
use Friendica\Core\Protocol;
use Friendica\Database\DBA;
use Friendica\Model\Contact;
2020-09-23 18:55:16 +00:00
use Friendica\Model\ItemURI;
use Friendica\Util\Strings;
/**
* Do some repairs in database entries
*
*/
class RepairDatabase
{
public static function execute()
{
// Sometimes there seem to be issues where the "self" contact vanishes.
// We haven't found the origin of the problem by now.
$users = DBA::select('user', ['uid'], ["NOT EXISTS (SELECT `uid` FROM `contact` WHERE `contact`.`uid` = `user`.`uid` AND `contact`.`self`)"]);
while ($user = DBA::fetch($users)) {
Logger::notice('Create missing self contact', ['user'=> $user['uid']]);
Contact::createSelfFromUserId($user['uid']);
}
DBA::close($users);
// There was an issue where the nick vanishes from the contact table
DBA::e("UPDATE `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid` SET `nick` = `nickname` WHERE `self` AND `nick`=''");
/// @todo
/// - remove thread entries without item
/// - remove sign entries without item
/// - remove children when parent got lost
/// - set contact-id in item when not present
// Add intro entries for pending contacts
// We don't do this for DFRN entries since such revived contact requests seem to mostly fail.
$pending_contacts = DBA::p("SELECT `uid`, `id`, `url`, `network`, `created` FROM `contact`
2020-08-24 21:03:00 +00:00
WHERE `pending` AND `rel` IN (?, ?) AND `network` != ? AND `uid` != ?
AND NOT EXISTS (SELECT `id` FROM `intro` WHERE `contact-id` = `contact`.`id`)",
2020-08-24 21:03:00 +00:00
0, Contact::FOLLOWER, Protocol::DFRN, 0);
while ($contact = DBA::fetch($pending_contacts)) {
DBA::insert('intro', ['uid' => $contact['uid'], 'contact-id' => $contact['id'], 'blocked' => false,
'hash' => Strings::getRandomHex(), 'datetime' => $contact['created']]);
}
DBA::close($pending_contacts);
2020-09-23 18:55:16 +00:00
// Ensure that there are no "uri-id", "parent-uri-id" or "thr-parent-id" fields that are NULL
$items = DBA::select('item', ['id', 'uri', 'guid'], ["`uri-id` IS NULL"]);
while ($item = DBA::fetch($items)) {
$uriid = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
DBA::update('item', ['uri-id' => $uriid], ['id' => $item['id']]);
}
DBA::close($items);
$items = DBA::select('item', ['id', 'parent-uri'], ["`parent-uri-id` IS NULL"]);
while ($item = DBA::fetch($items)) {
$uriid = ItemURI::getIdByURI($item['parent-uri']);
DBA::update('item', ['parent-uri-id' => $uriid], ['id' => $item['id']]);
}
DBA::close($items);
$items = DBA::select('item', ['id', 'thr-parent'], ["`thr-parent-id` IS NULL"]);
while ($item = DBA::fetch($items)) {
$uriid = ItemURI::getIdByURI($item['thr-parent']);
DBA::update('item', ['thr-parent-id' => $uriid], ['id' => $item['id']]);
}
DBA::close($items);
}
}