From e97634b7bf4d20d77dde39f0ed2c949d739ca0eb Mon Sep 17 00:00:00 2001 From: "E. S." Date: Sun, 28 Jul 2024 21:47:18 +0300 Subject: [PATCH] files/collections: more robust sphinxsearch reindexing; add reindex command to tools/cli --- lib/files.php | 14 +++++++++----- skin/files.phps | 2 +- tools/cli_util.php | 11 +++++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/files.php b/lib/files.php index e1d33e4..185d13f 100644 --- a/lib/files.php +++ b/lib/files.php @@ -320,13 +320,14 @@ function wff_reindex(): void { $q = $db->query("SELECT * FROM wff_collection"); while ($row = $db->fetch($q)) { $item = new WFFCollectionItem($row); + $text = ''; if ($item->isFile()) { - $txt = file_get_contents('/home/user/nsa/txt/'.str_replace('.pdf', '.txt', basename($item->path))); - } else { - $txt = ''; + $text_q = $db->query("SELECT text FROM wff_texts WHERE wff_id=?", $item->id); + if ($db->numRows($text_q)) + $text = $db->result($text_q); } sphinx_execute("INSERT INTO ".WFF_ARCHIVE_SPHINX_RTINDEX." (id, document_id, title, text, is_folder, parent_id) VALUES (?, ?, ?, ?, ?, ?)", - $item->id, $item->getDocumentId(), $item->title, $txt, (int)$item->isFolder(), $item->parentId); + $item->id, $item->getDocumentId(), $item->title, $text, (int)$item->isFolder(), $item->parentId); } } @@ -585,7 +586,10 @@ function baconiana_reindex(): void { $db = DB(); $baconiana = baconiana_get(null); foreach ($baconiana as $item) { - $text = $db->result($db->query("SELECT text FROM baconiana_texts WHERE bcn_id=?", $item->id)); + $text_q = $db->query("SELECT text FROM baconiana_texts WHERE bcn_id=?", $item->id); + if (!$db->numRows($text_q)) + continue; + $text = $db->result($text_q); sphinx_execute("INSERT INTO ".BACONIANA_ARCHIVE_SPHINX_RTINDEX." (id, title, year, text) VALUES (?, ?, ?, ?)", $item->id, "$item->year ($item->issues)", $item->year, $text); } diff --git a/skin/files.phps b/skin/files.phps index c9305df..ecaa952 100644 --- a/skin/files.phps +++ b/skin/files.phps @@ -186,7 +186,7 @@ function file(SkinContext $ctx, FilesItemInterface $file, ?SkinString $unsafe_query = null, bool $disabled = false, - ?array $text_excerpts = null,) { + ?array $text_excerpts = null) { $icons = svg(); if ($file instanceof BookItem && $file->fileType == BookFileType::BOOK) $icon = $icons->book_20(); diff --git a/tools/cli_util.php b/tools/cli_util.php index 39eceb2..b3405bd 100755 --- a/tools/cli_util.php +++ b/tools/cli_util.php @@ -79,6 +79,17 @@ require_once 'lib/admin.php'; echo "upload id: $id\n"; }) +->on('collection-reindex', function() { + require_once 'lib/files.php'; + $collections = array_map(fn($c) => $c->value, FilesCollection::cases()); + $s = cli::input('Enter collection to reindex (variants: '.implode(', ', $collections).': '); + $c = FilesCollection::from($s); + $f = "{$s}_reindex"; + echo "calling $f()... "; + call_user_func($f); + echo "done\n"; +}) + ->run(); function _get_admin_login_password_input(): array {