2020-10-15 02:02:17 -04:00
|
|
|
<?php
|
|
|
|
/**
|
2022-01-02 02:27:47 -05:00
|
|
|
* @copyright Copyright (C) 2010-2022, the Friendica project
|
2020-10-15 02:02:17 -04:00
|
|
|
*
|
|
|
|
* @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;
|
2021-03-02 01:50:12 -05:00
|
|
|
use Friendica\Core\Worker;
|
2021-03-03 02:07:29 -05:00
|
|
|
use Friendica\Database\Database;
|
2020-10-15 02:02:17 -04:00
|
|
|
use Friendica\Database\DBA;
|
2021-03-02 01:50:12 -05:00
|
|
|
use Friendica\Database\DBStructure;
|
2020-10-15 02:02:17 -04:00
|
|
|
use Friendica\DI;
|
2021-02-08 02:48:36 -05:00
|
|
|
use Friendica\Model\Item;
|
2021-03-02 01:50:12 -05:00
|
|
|
use Friendica\Model\Post;
|
2021-12-02 09:19:01 -05:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
2020-10-15 02:02:17 -04:00
|
|
|
|
|
|
|
class ExpirePosts
|
|
|
|
{
|
|
|
|
/**
|
2021-03-02 01:50:12 -05:00
|
|
|
* Expire posts and remove unused item-uri entries
|
|
|
|
*
|
|
|
|
* @return void
|
2020-10-15 02:02:17 -04:00
|
|
|
*/
|
|
|
|
public static function execute()
|
2021-03-02 01:50:12 -05:00
|
|
|
{
|
|
|
|
self::deleteExpiredOriginPosts();
|
|
|
|
|
2021-03-03 02:07:29 -05:00
|
|
|
self::deleteOrphanedEntries();
|
|
|
|
|
2021-03-02 01:50:12 -05:00
|
|
|
self::deleteUnusedItemUri();
|
|
|
|
|
|
|
|
self::deleteExpiredExternalPosts();
|
|
|
|
|
2021-03-03 06:01:16 -05:00
|
|
|
if (DI::config()->get('system', 'add_missing_posts')) {
|
|
|
|
self::addMissingEntries();
|
|
|
|
}
|
2021-03-03 02:07:29 -05:00
|
|
|
|
2021-03-02 02:06:22 -05:00
|
|
|
// Set the expiry for origin posta
|
|
|
|
Worker::add(PRIORITY_LOW, 'Expire');
|
|
|
|
|
2021-03-02 01:50:12 -05:00
|
|
|
// update nodeinfo data after everything is cleaned up
|
2021-03-02 02:06:22 -05:00
|
|
|
Worker::add(PRIORITY_LOW, 'NodeInfo');
|
2021-03-02 01:50:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete expired origin posts and orphaned post related table entries
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private static function deleteExpiredOriginPosts()
|
|
|
|
{
|
2021-03-03 02:07:29 -05:00
|
|
|
Logger::notice('Delete expired posts');
|
2021-03-02 01:50:12 -05:00
|
|
|
// physically remove anything that has been deleted for more than two months
|
2021-12-02 09:19:01 -05:00
|
|
|
$condition = ["`gravity` = ? AND `deleted` AND `changed` < ?", GRAVITY_PARENT, DateTimeFormat::utc('now - 60 days')];
|
2021-03-02 01:50:12 -05:00
|
|
|
$rows = Post::select(['guid', 'uri-id', 'uid'], $condition);
|
|
|
|
while ($row = Post::fetch($rows)) {
|
|
|
|
Logger::info('Delete expired item', ['uri-id' => $row['uri-id'], 'guid' => $row['guid']]);
|
|
|
|
Post\User::delete(['parent-uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
|
|
|
|
}
|
|
|
|
DBA::close($rows);
|
|
|
|
|
2021-03-03 02:07:29 -05:00
|
|
|
Logger::notice('Delete expired posts - done');
|
|
|
|
}
|
2021-03-02 01:50:12 -05:00
|
|
|
|
2021-03-03 02:07:29 -05:00
|
|
|
/**
|
|
|
|
* Delete orphaned entries in the post related tables
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private static function deleteOrphanedEntries()
|
|
|
|
{
|
|
|
|
Logger::notice('Delete orphaned entries');
|
2021-03-02 01:50:12 -05:00
|
|
|
|
2021-03-03 02:07:29 -05:00
|
|
|
// "post-user" is the leading table. So we delete every entry that isn't found there
|
|
|
|
$tables = ['item', 'post', 'post-content', 'post-thread', 'post-thread-user'];
|
|
|
|
foreach ($tables as $table) {
|
|
|
|
if (($table == 'item') && !DBStructure::existsTable('item')) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-02 01:50:12 -05:00
|
|
|
|
2021-03-03 02:07:29 -05:00
|
|
|
Logger::notice('Start collecting orphaned entries', ['table' => $table]);
|
|
|
|
$uris = DBA::select($table, ['uri-id'], ["NOT `uri-id` IN (SELECT `uri-id` FROM `post-user`)"]);
|
|
|
|
$affected_count = 0;
|
|
|
|
Logger::notice('Deleting orphaned entries - start', ['table' => $table]);
|
|
|
|
while ($rows = DBA::toArray($uris, false, 100)) {
|
|
|
|
$ids = array_column($rows, 'uri-id');
|
|
|
|
DBA::delete($table, ['uri-id' => $ids]);
|
|
|
|
$affected_count += DBA::affectedRows();
|
|
|
|
}
|
|
|
|
DBA::close($uris);
|
|
|
|
Logger::notice('Orphaned entries deleted', ['table' => $table, 'rows' => $affected_count]);
|
|
|
|
}
|
|
|
|
Logger::notice('Delete orphaned entries - done');
|
|
|
|
}
|
2021-03-02 01:50:12 -05:00
|
|
|
|
2021-03-03 02:07:29 -05:00
|
|
|
/**
|
|
|
|
* Add missing entries in some post related tables
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private static function addMissingEntries()
|
|
|
|
{
|
|
|
|
Logger::notice('Adding missing entries');
|
|
|
|
|
|
|
|
$rows = 0;
|
2021-05-29 17:51:33 -04:00
|
|
|
$userposts = DBA::select('post-user', [], ["`uri-id` not in (select `uri-id` from `post`)"]);
|
2021-03-03 02:07:29 -05:00
|
|
|
while ($fields = DBA::fetch($userposts)) {
|
2022-07-12 18:23:12 -04:00
|
|
|
$post_fields = DI::dbaDefinition()->truncateFieldsForTable('post', $fields);
|
2021-03-03 02:07:29 -05:00
|
|
|
DBA::insert('post', $post_fields, Database::INSERT_IGNORE);
|
|
|
|
$rows++;
|
|
|
|
}
|
|
|
|
DBA::close($userposts);
|
|
|
|
if ($rows > 0) {
|
|
|
|
Logger::notice('Added post entries', ['rows' => $rows]);
|
|
|
|
} else {
|
|
|
|
Logger::notice('No post entries added');
|
|
|
|
}
|
|
|
|
|
|
|
|
$rows = 0;
|
2021-05-29 17:51:33 -04:00
|
|
|
$userposts = DBA::select('post-user', [], ["`gravity` = ? AND `uri-id` not in (select `uri-id` from `post-thread`)", GRAVITY_PARENT]);
|
2021-03-03 02:07:29 -05:00
|
|
|
while ($fields = DBA::fetch($userposts)) {
|
2022-07-12 18:23:12 -04:00
|
|
|
$post_fields = DI::dbaDefinition()->truncateFieldsForTable('post-thread', $fields);
|
2021-03-03 02:07:29 -05:00
|
|
|
$post_fields['commented'] = $post_fields['changed'] = $post_fields['created'];
|
|
|
|
DBA::insert('post-thread', $post_fields, Database::INSERT_IGNORE);
|
|
|
|
$rows++;
|
|
|
|
}
|
|
|
|
DBA::close($userposts);
|
|
|
|
if ($rows > 0) {
|
|
|
|
Logger::notice('Added post-thread entries', ['rows' => $rows]);
|
|
|
|
} else {
|
|
|
|
Logger::notice('No post-thread entries added');
|
|
|
|
}
|
|
|
|
|
|
|
|
$rows = 0;
|
|
|
|
$userposts = DBA::select('post-user', [], ["`gravity` = ? AND `id` not in (select `post-user-id` from `post-thread-user`)", GRAVITY_PARENT]);
|
|
|
|
while ($fields = DBA::fetch($userposts)) {
|
2022-07-12 18:23:12 -04:00
|
|
|
$post_fields = DI::dbaDefinition()->truncateFieldsForTable('post-thread-user', $fields);
|
2021-03-03 02:07:29 -05:00
|
|
|
$post_fields['commented'] = $post_fields['changed'] = $post_fields['created'];
|
|
|
|
DBA::insert('post-thread-user', $post_fields, Database::INSERT_IGNORE);
|
|
|
|
$rows++;
|
|
|
|
}
|
|
|
|
DBA::close($userposts);
|
|
|
|
if ($rows > 0) {
|
|
|
|
Logger::notice('Added post-thread-user entries', ['rows' => $rows]);
|
|
|
|
} else {
|
|
|
|
Logger::notice('No post-thread-user entries added');
|
|
|
|
}
|
2021-03-02 01:50:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete unused item-uri entries
|
|
|
|
*/
|
|
|
|
private static function deleteUnusedItemUri()
|
|
|
|
{
|
|
|
|
// We have to avoid deleting newly created "item-uri" entries.
|
|
|
|
// So we fetch a post that had been stored yesterday and only delete older ones.
|
2021-12-02 09:19:01 -05:00
|
|
|
$item = Post::selectFirstThread(['uri-id'], ["`uid` = ? AND `received` < ?", 0, DateTimeFormat::utc('now - 1 day')],
|
2021-03-02 01:50:12 -05:00
|
|
|
['order' => ['received' => true]]);
|
|
|
|
if (empty($item['uri-id'])) {
|
|
|
|
Logger::warning('No item with uri-id found - we better quit here');
|
|
|
|
return;
|
|
|
|
}
|
2021-03-03 02:07:29 -05:00
|
|
|
Logger::notice('Start collecting orphaned URI-ID', ['last-id' => $item['uri-id']]);
|
2021-03-02 01:50:12 -05:00
|
|
|
$uris = DBA::select('item-uri', ['id'], ["`id` < ?
|
2021-03-26 16:36:22 -04:00
|
|
|
AND NOT EXISTS(SELECT `uri-id` FROM `post-user` WHERE `uri-id` = `item-uri`.`id`)
|
|
|
|
AND NOT EXISTS(SELECT `parent-uri-id` FROM `post-user` WHERE `parent-uri-id` = `item-uri`.`id`)
|
|
|
|
AND NOT EXISTS(SELECT `thr-parent-id` FROM `post-user` WHERE `thr-parent-id` = `item-uri`.`id`)
|
2021-05-22 09:37:04 -04:00
|
|
|
AND NOT EXISTS(SELECT `external-id` FROM `post-user` WHERE `external-id` = `item-uri`.`id`)
|
|
|
|
AND NOT EXISTS(SELECT `uri-id` FROM `mail` WHERE `uri-id` = `item-uri`.`id`)
|
2021-07-08 09:47:46 -04:00
|
|
|
AND NOT EXISTS(SELECT `uri-id` FROM `event` WHERE `uri-id` = `item-uri`.`id`)
|
2021-09-10 09:05:16 -04:00
|
|
|
AND NOT EXISTS(SELECT `uri-id` FROM `user-contact` WHERE `uri-id` = `item-uri`.`id`)
|
2021-07-09 00:38:36 -04:00
|
|
|
AND NOT EXISTS(SELECT `uri-id` FROM `contact` WHERE `uri-id` = `item-uri`.`id`)
|
2021-07-09 04:08:48 -04:00
|
|
|
AND NOT EXISTS(SELECT `uri-id` FROM `apcontact` WHERE `uri-id` = `item-uri`.`id`)
|
|
|
|
AND NOT EXISTS(SELECT `uri-id` FROM `fcontact` WHERE `uri-id` = `item-uri`.`id`)
|
2022-05-02 01:15:27 -04:00
|
|
|
AND NOT EXISTS(SELECT `uri-id` FROM `inbox-status` WHERE `uri-id` = `item-uri`.`id`)
|
|
|
|
AND NOT EXISTS(SELECT `uri-id` FROM `post-delivery` WHERE `uri-id` = `item-uri`.`id`)
|
|
|
|
AND NOT EXISTS(SELECT `uri-id` FROM `post-delivery` WHERE `inbox-id` = `item-uri`.`id`)
|
2021-05-22 09:37:04 -04:00
|
|
|
AND NOT EXISTS(SELECT `parent-uri-id` FROM `mail` WHERE `parent-uri-id` = `item-uri`.`id`)
|
|
|
|
AND NOT EXISTS(SELECT `thr-parent-id` FROM `mail` WHERE `thr-parent-id` = `item-uri`.`id`)", $item['uri-id']]);
|
2021-03-02 01:50:12 -05:00
|
|
|
|
2021-03-03 02:07:29 -05:00
|
|
|
Logger::notice('Start deleting orphaned URI-ID', ['last-id' => $item['uri-id']]);
|
2021-03-02 01:50:12 -05:00
|
|
|
$affected_count = 0;
|
|
|
|
while ($rows = DBA::toArray($uris, false, 100)) {
|
|
|
|
$ids = array_column($rows, 'id');
|
|
|
|
DBA::delete('item-uri', ['id' => $ids]);
|
|
|
|
$affected_count += DBA::affectedRows();
|
|
|
|
Logger::info('Deleted', ['rows' => $affected_count]);
|
|
|
|
}
|
|
|
|
DBA::close($uris);
|
|
|
|
Logger::notice('Orphaned URI-ID entries removed', ['rows' => $affected_count]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete old external post entries
|
|
|
|
*/
|
|
|
|
private static function deleteExpiredExternalPosts()
|
2020-10-15 02:02:17 -04:00
|
|
|
{
|
|
|
|
$expire_days = DI::config()->get('system', 'dbclean-expire-days');
|
|
|
|
$expire_days_unclaimed = DI::config()->get('system', 'dbclean-expire-unclaimed');
|
|
|
|
if (empty($expire_days_unclaimed)) {
|
|
|
|
$expire_days_unclaimed = $expire_days;
|
|
|
|
}
|
|
|
|
|
2020-10-17 04:16:17 -04:00
|
|
|
$limit = DI::config()->get('system', 'dbclean-expire-limit');
|
|
|
|
if (empty($limit)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-15 02:02:17 -04:00
|
|
|
if (!empty($expire_days)) {
|
2021-02-28 12:56:56 -05:00
|
|
|
Logger::notice('Start collecting expired threads', ['expiry_days' => $expire_days]);
|
|
|
|
$uris = DBA::select('item-uri', ['id'], ["`id` IN
|
2021-12-02 09:19:01 -05:00
|
|
|
(SELECT `uri-id` FROM `post-thread` WHERE `received` < ?
|
2021-02-28 12:56:56 -05:00
|
|
|
AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-thread-user`
|
2022-04-07 17:52:25 -04:00
|
|
|
WHERE (`mention` OR `starred` OR `wall`) AND `uri-id` = `post-thread`.`uri-id`)
|
2021-02-28 12:56:56 -05:00
|
|
|
AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-category`
|
|
|
|
WHERE `uri-id` = `post-thread`.`uri-id`)
|
2022-04-07 17:52:25 -04:00
|
|
|
AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-collection`
|
|
|
|
WHERE `uri-id` = `post-thread`.`uri-id`)
|
2021-02-28 12:56:56 -05:00
|
|
|
AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-media`
|
|
|
|
WHERE `uri-id` = `post-thread`.`uri-id`)
|
|
|
|
AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` INNER JOIN `contact` ON `contact`.`id` = `contact-id` AND `notify_new_posts`
|
|
|
|
WHERE `parent-uri-id` = `post-thread`.`uri-id`)
|
|
|
|
AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user`
|
2021-04-07 02:02:06 -04:00
|
|
|
WHERE (`origin` OR `event-id` != 0 OR `post-type` = ?) AND `parent-uri-id` = `post-thread`.`uri-id`)
|
2021-02-28 12:56:56 -05:00
|
|
|
AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-content`
|
|
|
|
WHERE `resource-id` != 0 AND `uri-id` = `post-thread`.`uri-id`))",
|
2021-12-02 09:19:01 -05:00
|
|
|
DateTimeFormat::utc('now - ' . (int)$expire_days . ' days'), Item::PT_PERSONAL_NOTE]);
|
2020-10-15 02:02:17 -04:00
|
|
|
|
2021-02-28 12:56:56 -05:00
|
|
|
Logger::notice('Start deleting expired threads');
|
|
|
|
$affected_count = 0;
|
|
|
|
while ($rows = DBA::toArray($uris, false, 100)) {
|
|
|
|
$ids = array_column($rows, 'id');
|
|
|
|
DBA::delete('item-uri', ['id' => $ids]);
|
|
|
|
$affected_count += DBA::affectedRows();
|
|
|
|
}
|
|
|
|
DBA::close($uris);
|
2020-10-17 08:39:42 -04:00
|
|
|
|
2021-02-28 12:56:56 -05:00
|
|
|
Logger::notice('Deleted expired threads', ['rows' => $affected_count]);
|
2020-10-15 02:02:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($expire_days_unclaimed)) {
|
2021-02-28 12:56:56 -05:00
|
|
|
Logger::notice('Start collecting unclaimed public items', ['expiry_days' => $expire_days_unclaimed]);
|
|
|
|
$uris = DBA::select('item-uri', ['id'], ["`id` IN
|
2021-12-02 09:19:01 -05:00
|
|
|
(SELECT `uri-id` FROM `post-user` WHERE `gravity` = ? AND `uid` = ? AND `received` < ?
|
2021-02-28 12:56:56 -05:00
|
|
|
AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` != ?
|
|
|
|
AND `i`.`parent-uri-id` = `post-user`.`uri-id`)
|
|
|
|
AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `post-user` AS `i` WHERE `i`.`uid` = ?
|
2021-12-02 09:19:01 -05:00
|
|
|
AND `i`.`parent-uri-id` = `post-user`.`uri-id` AND `i`.`received` > ?))",
|
|
|
|
GRAVITY_PARENT, 0, DateTimeFormat::utc('now - ' . (int)$expire_days_unclaimed . ' days'), 0, 0, DateTimeFormat::utc('now - ' . (int)$expire_days_unclaimed . ' days')]);
|
2020-10-15 02:02:17 -04:00
|
|
|
|
2021-02-28 12:56:56 -05:00
|
|
|
Logger::notice('Start deleting unclaimed public items');
|
|
|
|
$affected_count = 0;
|
|
|
|
while ($rows = DBA::toArray($uris, false, 100)) {
|
|
|
|
$ids = array_column($rows, 'id');
|
|
|
|
DBA::delete('item-uri', ['id' => $ids]);
|
|
|
|
$affected_count += DBA::affectedRows();
|
|
|
|
}
|
|
|
|
DBA::close($uris);
|
|
|
|
Logger::notice('Deleted unclaimed public items', ['rows' => $affected_count]);
|
2020-10-15 02:02:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|