Files
FreshRSS/cli/compile.plurals.php
Christian Weiske 1acc646222 Show time since when a feed has problems + new timeago() method and i18n plurals (#8670)
Closes https://github.com/FreshRSS/FreshRSS/issues/8508

Changes proposed in this pull request:

- Use an integer for `Feed::error` everywhere (follow up to #8646)
- Extract `Entry::machineReadableDate()` into function for use in HTML templates
- Add `timeago()` function that converts a unix timestamp into a "4 weeks ago" string
- Show the last successful feed update, and the last erroneous update

How to test the feature manually:

1. Update a feed
2. Modify the feed URL in the database and set it to a non-existing URL
3. Update the feed again
4. Open the "Manage feed" and see the expanded error message:

>  Blast! This feed has encountered a problem. If this situation persists, please verify that it is still reachable.
> Last successful update 3 hours ago, last erroneous update 1 hour ago. 

You can hover the relative dates to see the timestamp.

* Make Feed::error an int everywhere

Related: https://github.com/FreshRSS/FreshRSS/pull/8646

* Extract timestamptomachinedate()

.. for later usage in the feed error time display.

* Show time since when a feed has problems

We add our own "timeago" function that converts a unix timestamp
into a "4 weeks ago" string.

Resolves: https://github.com/FreshRSS/FreshRSS/issues/8508

* Add new translation keys

* i18n fr, en-US

* Minor XHTML preference

* Slightly shorter rewrite, also hopefully easier to read

* Rewrite to allow (simple) plural
I also moved some functions around for hopefully a more generic and better structure.
I made some changes for the sake of speed (e.g. second-based logic instead of datetime intervals).
Note: I used automatic translation as I was worried it would be too complicated to explain to translators... I proofread the few languages I have some familiarity with.

* Add reference to CLDR

* Slightly more compact syntax

* Always show last update, fix case of unknown error date

* Remove forgotten span

* No need for multi-lines anymore

* Fix error date thresshold

* plurals forms

* Extract gettext formula conversion script to cli

* Simplify a bit

* Escort excess parentheses to the door

* Simplify

* Avoid being too clever in localization

* Fix German

* Fix plural TODO parsing

* Ignore en-US translation

* make fix-all

* git update-index --chmod=+x cli/compile.plurals.php

* Heredoc indent PHP 7.3+

* compileAll: Continue on error

* PHP strict comparisons

* Light logical simplification

* Cache plural_message_families

* Avoid case of empty value

* A bit of documentation

---------

Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
Co-authored-by: Frans de Jonge <frans@clevercast.com>
Co-authored-by: Frans de Jonge <fransdejonge@gmail.com>
2026-04-07 22:56:02 +02:00

75 lines
2.1 KiB
PHP
Executable File

#!/usr/bin/env php
<?php
declare(strict_types=1);
require_once __DIR__ . '/_cli.php';
require_once __DIR__ . '/i18n/PluralFormsCompiler.php';
$cliOptions = new class extends CliOptionsParser {
public bool $all;
public string $file;
public string $formula;
public bool $help;
public function __construct() {
$this->addOption('all', (new CliOption('all', 'a'))->withValueNone());
$this->addOption('file', new CliOption('file', 'f'));
$this->addOption('formula', new CliOption('formula', 'p'));
$this->addOption('help', (new CliOption('help', 'h'))->withValueNone());
parent::__construct();
}
};
if (!empty($cliOptions->errors)) {
fail('FreshRSS error: ' . array_shift($cliOptions->errors) . "\n" . $cliOptions->usage);
}
if ($cliOptions->help || (!isset($cliOptions->formula) && !isset($cliOptions->file) && !$cliOptions->all)) {
compilePluralsHelp();
}
$compiler = new PluralFormsCompiler();
if (isset($cliOptions->formula)) {
echo $compiler->compileFormulaToLambda($cliOptions->formula) . "\n";
done();
}
if (isset($cliOptions->file)) {
$compiler->compileFile($cliOptions->file);
echo 'Compiled ' . $cliOptions->file . "\n";
done();
}
$changed = $compiler->compileAll();
echo 'Compiled ' . $changed . " plural file(s).\n";
done();
function compilePluralsHelp(): never {
$file = str_replace(__DIR__ . '/', '', __FILE__);
echo <<<HELP
NAME
$file
SYNOPSIS
php $file [ --all | --file=<path> | --formula='<plural-forms>' ]
DESCRIPTION
Compile gettext plural formulas into PHP callables for runtime consumption.
-a, --all compile all app/i18n/*/plurals.php files in place.
-f, --file=FILE compile a single plural file in place.
-p, --formula=FORMULA output the compiled PHP lambda for a gettext plural formula.
-h, --help display this help and exit.
EXAMPLES
php $file --formula 'nplurals=2; plural=(n != 1);'
php $file --file app/i18n/en/plurals.php
php $file --all
REFERENCES
https://www.gnu.org/software/gettext/manual/html_node/Plural-forms.html
https://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html
HELP, PHP_EOL;
exit();
}