Procházet zdrojové kódy

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

- Adds `cli/purge.php` so the purge policy can be applied from cron, mirroring `cli/db-optimize.php`.
- Documents it in `cli/README.md`.

Aims to be a small, contained answer to #3636: reuses the same `Feed::cleanOldEntries()` logic the GUI's "Purge now" button calls, packaged as a standard `cli/` script.

Mirror the existing db-optimize.php pattern so administrators can run the
purge policy on a schedule instead of waiting for the 1/30 random trigger
in feedController, or clicking 'Purge now' in the GUI.

The script applies the same archiving policy as the GUI button: per-feed
attribute, per-category attribute, then user config; with the existing
keep_favourites / keep_unreads / keep_labels / keep_min / keep_period /
keep_max guards. With no policy configured, it is a no-op.

Refs: https://github.com/FreshRSS/FreshRSS/issues/3636

Co-authored-by: Bjørn A. Andersen <polybjorn@users.noreply.github.com>
polybjorn před 2 týdny
rodič
revize
bb9349f4e9
2 změnil soubory, kde provedl 48 přidání a 0 odebrání
  1. 5 0
      cli/README.md
  2. 43 0
      cli/purge.php

+ 5 - 0
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

+ 43 - 0
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);