Food: all products, barcode prefixes, meals that follow their ingredients

- "All products" lists what's here first (My foods, kept products, the food
  database), then Open Food Facts; if the archive can't be reached, what's
  here is still shown. A My food picked there is used as it is.
- The start of a barcode (4+ digits) finds the products whose barcode starts
  with it: in the search box, in all products and in the server's search
  (an index range, so it stays fast).
- A meal made with a changed food is worked out again, and so are meals made
  with that meal and their logged entries (db.follow_meals, app _followMeals).
- Saving a food that didn't change writes nothing, so nothing is synced again.
- Food search knows a few words in other languages: stomach / žalúdok /
  pipotă find gizzards, kurací / pui chicken, morčací / curcan turkey.
- App 1.1.5 changelog entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Bashy 2026-10-26 15:41:23 +02:00
parent d9460a2293
commit eeca189b7f
11 changed files with 287 additions and 71 deletions

View file

@ -70,7 +70,7 @@ function food_row(array $r): array {
/**
* GET /v1/food/search: q (every word a prefix of the name / brand / category), brand, country (a name or
* ro / sk / de …), category, nutriscore ("a" or "ab"), barcode (exact, the merged duplicates' too), limit (≤ 50).
* ro / sk / de …), category, nutriscore ("a" or "ab"), barcode (exact, the merged duplicates' too; then, 4+ digits, the ones starting with it), limit (≤ 50).
* Ranked as the dashboard's fooddb.search: all of q in the name first, home-country products, the name
* starting with the first word, fewer words, FTS relevance (name ≫ brand), shorter names.
*/
@ -78,10 +78,21 @@ function food_search(PDO $db, array $p, array $home = []): array { // $home: c
$home = array_map(fn($c) => food_fold((string)$c), $home);
$limit = min(50, max(1, (int)($p['limit'] ?? 20)));
$code = preg_replace('/\D/', '', (string)($p['barcode'] ?? ''));
if ($code !== '') {
if ($code !== '') { // that barcode, then (4+ digits) the ones starting with it
$st = $db->prepare('SELECT * FROM food WHERE barcode = ? UNION SELECT f.* FROM alias a JOIN food f ON f.id = a.food_id WHERE a.barcode = ?');
$st->execute([$code, $code]);
return array_map('food_row', $st->fetchAll());
$rows = $st->fetchAll();
if (strlen($code) >= 4 && count($rows) < $limit) { // a range, so the barcode index does it (':' follows '9')
$st = $db->prepare('SELECT * FROM food WHERE barcode > ? AND barcode < ? ORDER BY barcode LIMIT ?');
$st->execute([$code, $code . ':', $limit]);
$ids = array_column($rows, 'id');
foreach ($st->fetchAll() as $r) {
if (count($rows) < $limit && !in_array($r['id'], $ids)) {
$rows[] = $r;
}
}
}
return array_map('food_row', $rows);
}
$words = food_words((string)($p['q'] ?? ''));
$terms = array_map(fn($w) => food_term($w), $words);