- Logger::info('Deleting orphaned post entries - start');
- $condition = ["NOT EXISTS (SELECT `uri-id` FROM `post-user` WHERE `post-user`.`uri-id` = `post`.`uri-id`)"];
- DBA::delete('post', $condition);
- Logger::info('Orphaned post entries deleted', ['rows' => DBA::affectedRows()]);
+ Logger::notice('Delete expired posts - done');
+ }
+
+ /**
+ * Delete orphaned entries in the post related tables
+ *
+ * @return void
+ */
+ private static function deleteOrphanedEntries()
+ {
+ Logger::notice('Delete orphaned entries');
+
+ // "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;
+ }
+
+ 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');
+ }