Search by

phil / ranking

phil

Sort threads, ideas and questions fairly, with explainable keys a database can store: Hot, Active, Scaled, Controversial, Top by period, Best, New Comments.

0.1.0-alpha 2026-11-29 22:00 UTC

This package is auto-updated.

Last update: 2026-11-30 02:52:42 UTC


README

Sort threads, ideas and questions fairly, with explainable keys a database can store: Hot, Active, Scaled, Controversial, Top by period, Best and New Comments.

phil/ranking is a plain PHP library without dependencies. It turns the public signals of an entry — its votes, its publication, its latest activity, the size of its community — into a key for each sort, sorts a list by that key, and explains the key term by term. A forum, a box of ideas, a questions-and-answers section and a public roadmap share the same sorts, so that members meet the same orders everywhere.

  • Eight sorts with published formulas: Hot (the score on a logarithm, halved every half-life), Active (Hot from the latest activity), Scaled (Hot divided by the size of the community, so that small communities surface in a mixed list), Controversial (many votes evenly split), Top (the score, usually over a period), Best (the lower bound of the share of votes for, so that a few votes count less than many), New and New Comments.
  • No hidden signal: only votes, dates and the size of the community enter a key. Time spent on a page, hovering or an author's reputation never do.
  • Late comments do not bump an old entry: an activity counts only within a bump window after publication, two days by default.
  • Keys a database can store: a key is a float that depends only on the entry, the policy and the instant given as now. An application stores it in a column and sorts in SQL; a decaying key settles to zero after a horizon, so that a batch refresh only reads the entries still before theirs.
  • Explained to members: explain() names every term of the formula with its value.
  • Deterministic and bounded: ties fall to the most recent publication, then to the identifier compared as bytes; one call sorts at most 100,000 entries by default, and vote counts are bounded so that no sum overflows.

Status

Part State
The eight sorts and their keys Implemented and tested
Sorting a list, with a period Implemented and tested
The explanation of a key Implemented and tested
Stored keys and their refresh horizon Implemented and tested

Documentation

Document What it covers
Installation Requirements, installing, a first sorted list.
Sorts Each sort, its formula, and the policy that tunes it.
Stored keys Keys in a database column, periods in SQL, the batch refresh.
Extension points The policy, a sort of your own, what is stable.
Testing The tests and the benchmark.
Reference Every public type, with its role.

Quick start

composer require phil/ranking
use Phil\Ranking\Entry;
use Phil\Ranking\Period;
use Phil\Ranking\Ranker;
use Phil\Ranking\Sort;

$ranker = new Ranker();
$now = new DateTimeImmutable();

$entries = [
    new Entry('thread-1', upvotes: 42, downvotes: 3, publishedAt: new DateTimeImmutable('-5 hours')),
    new Entry('thread-2', upvotes: 7, downvotes: 0, publishedAt: new DateTimeImmutable('-1 hour'),
        lastActivityAt: new DateTimeImmutable('-10 minutes')),
];

foreach ($ranker->rank(Sort::Hot, $entries, $now) as $ranked) {
    echo $ranked->position, '. ', $ranked->entry->id, PHP_EOL;
}

$week = $ranker->rank(Sort::Top, $entries, $now, Period::Week);
$why = $ranker->explain(Sort::Hot, $entries[0], $now)->terms;   // ['score' => 39, 'weight' => …, 'age_hours' => …, 'decay' => …]

Requirements

PHP 8.5 or later.

Contributing

Contributions are welcome on the PHIL monorepo: read the contribution guide and the code of conduct. Report a vulnerability privately, as the security policy explains, never in a public issue.

License

MIT, © Philippe Gamache, doing business as Le Gnome Archiviste; a Parler Haut Interagir Librement project. See LICENSE.