Skip to content

A free, self-hostable news aggregator…

feat(cli): add purge.php to apply purge policy from CLI (#8740)

polybjornbb9349f4e976
diff --git a/cli/README.md b/cli/README.md
index c7c0da905..57b0a03a1 100644
--- a/cli/README.md
+++ b/cli/README.md
@@ -134,6 +134,11 @@ cd /usr/share/FreshRSS
./cli/db-optimize.php --user username
# Optimize database (reduces the size) for a given user (perform `OPTIMIZE TABLE` in MySQL, `VACUUM` in SQLite)
+
+./cli/purge.php --user username
+# Apply the purge policy (max number of articles, max age, exceptions) to all feeds of the given user.
+# Equivalent to clicking ‘Purge now’ in the GUI, but suitable for cron.
+# Purge queries are expensive and blocking; running daily or weekly is usually enough.
```
### Translation
diff --git a/cli/purge.php b/cli/purge.php
new file mode 100755
index 000000000..6fc889fca
--- /dev/null
+++ b/cli/purge.php
@@ -0,0 +1,43 @@
+#!/usr/bin/env php
+<?php
+declare(strict_types=1);
+require __DIR__ . '/_cli.php';
+
+performRequirementCheck(FreshRSS_Context::systemConf()->db['type'] ?? '');
+
+$cliOptions = new class extends CliOptionsParser {
+ public string $user;
+
+ public function __construct() {
+ $this->addRequiredOption('user', (new CliOption('user')));
+ parent::__construct();
+ }
+};
+
+if (!empty($cliOptions->errors)) {
+ fail('FreshRSS error: ' . array_shift($cliOptions->errors) . "\n" . $cliOptions->usage);
+}
+
+$username = cliInitUser($cliOptions->user);
+
+echo 'FreshRSS purging old entries for user “', $username, "”…\n";
+
+$databaseDAO = FreshRSS_Factory::createDatabaseDAO();
+$databaseDAO->minorDbMaintenance();
+
+$feedDAO = FreshRSS_Factory::createFeedDao();
+$feeds = $feedDAO->listFeeds();
+
+$nb_total = 0;
+$feedDAO->beginTransaction();
+foreach ($feeds as $feed) {
+ $nb_total += ($feed->cleanOldEntries() ?: 0);
+}
+$feedDAO->updateCachedValues();
+$feedDAO->commit();
+
+invalidateHttpCache($username);
+
+echo "FreshRSS purged {$nb_total} old entries for {$username}\n";
+
+done(true);