From 2444e23474f4db24a2dda8cb2528dfb248397e83 Mon Sep 17 00:00:00 2001 From: Lucas Azevedo Date: Tue, 8 Oct 2024 18:36:56 -0300 Subject: [PATCH 1/4] Fix search index for entries without parent books This commit enhances the search index generation process by providing more meaningful descriptions for entries that lack a parent element. Additionally, refactors writeJsonIndex() into smaller methods. Fixes #159 --- phpdotnet/phd/Package/PHP/Web.php | 76 +++++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/phpdotnet/phd/Package/PHP/Web.php b/phpdotnet/phd/Package/PHP/Web.php index 73f086e9..8afa4b1a 100644 --- a/phpdotnet/phd/Package/PHP/Web.php +++ b/phpdotnet/phd/Package/PHP/Web.php @@ -234,8 +234,24 @@ public function footer($id) { protected function writeJsonIndex() { v("Writing search indexes..", VERBOSE_FORMAT_RENDERING); - $ids = array(); - $desc = array(); + [$entries, $descriptions] = $this->processIndexForSearch(); + file_put_contents( + $this->getOutputDir() . "search-index.json", + json_encode($entries) + ); + file_put_contents( + $this->getOutputDir() . "search-description.json", + json_encode($descriptions) + ); + v("Index written", VERBOSE_FORMAT_RENDERING); + } + + /** + * Processes the search index to extract entries and descriptions. + */ + private function processIndexForSearch(): array { + $entries = []; + $descriptions = []; foreach($this->indexes as $id => $index) { if (!$index["chunk"]) { continue; @@ -243,26 +259,50 @@ protected function writeJsonIndex() { if ($index["sdesc"] === "" && $index["ldesc"] !== "") { $index["sdesc"] = $index["ldesc"]; - - $parentId = $index['parent_id']; - // isset() to guard against undefined array keys, either for root - // elements (no parent) or in case the index structure is broken. - while (isset($this->indexes[$parentId])) { - $parent = $this->indexes[$parentId]; - if ($parent['element'] === 'book') { - $index["ldesc"] = Format::getLongDescription($parent['docbook_id']); - break; - } - $parentId = $parent['parent_id']; + $bookOrSet = $this->findParentBookOrSet($index['parent_id']); + if ($bookOrSet) { + $index["ldesc"] = Format::getLongDescription( + $bookOrSet['docbook_id'] + ); } } - $ids[] = array($index["sdesc"], $index["filename"], $index["element"]); - $desc[$id] = $index["ldesc"]; + $entries[] = [ + $index["sdesc"], $index["filename"], $index["element"] + ]; + $descriptions[$id] = $index["ldesc"]; } - file_put_contents($this->getOutputDir() . "search-index.json", json_encode($ids)); - file_put_contents($this->getOutputDir() . "search-description.json", json_encode($desc)); - v("Index written", VERBOSE_FORMAT_RENDERING); + return [$entries, $descriptions]; + } + + /** + * Finds the closest parent book or set in the index hierarchy. + * + * Books are prioritized over sets. If no book is found, + * the closest set is returned. If neither is found, null is returned. + */ + private function findParentBookOrSet(string $id): ?array + { + $set = null; + + // isset() to guard against undefined array keys, either for root + // elements (no parent) or in case the index structure is broken. + while (isset($this->indexes[$id])) { + $parent = $this->indexes[$id]; + $element = $parent['element']; + + if ($element === 'book') { + return $parent; + } + + if ($element === 'set') { + $set ??= $parent; + } + + $id = $parent['parent_id']; + } + + return $set; } public function loadSourcesInfo() { From 4ba7d7859dd54a842fb167ab6b2b7548355a9dcb Mon Sep 17 00:00:00 2001 From: Lucas Azevedo Date: Thu, 31 Oct 2024 15:41:08 -0300 Subject: [PATCH 2/4] Prefer array_key_exists over isset --- phpdotnet/phd/Package/PHP/Web.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/phpdotnet/phd/Package/PHP/Web.php b/phpdotnet/phd/Package/PHP/Web.php index 8afa4b1a..ea9fa54f 100644 --- a/phpdotnet/phd/Package/PHP/Web.php +++ b/phpdotnet/phd/Package/PHP/Web.php @@ -285,9 +285,9 @@ private function findParentBookOrSet(string $id): ?array { $set = null; - // isset() to guard against undefined array keys, either for root - // elements (no parent) or in case the index structure is broken. - while (isset($this->indexes[$id])) { + // array_key_exists() to guard against undefined array keys, either for + // root elements (no parent) or in case the index structure is broken. + while (array_key_exists($id, $this->indexes)) { $parent = $this->indexes[$id]; $element = $parent['element']; From 0b70198d2219229f7b4ceb63e551e3c2f5bdbf02 Mon Sep 17 00:00:00 2001 From: Lucas Azevedo Date: Thu, 31 Oct 2024 17:06:01 -0300 Subject: [PATCH 3/4] Return set elements immediately --- phpdotnet/phd/Package/PHP/Web.php | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/phpdotnet/phd/Package/PHP/Web.php b/phpdotnet/phd/Package/PHP/Web.php index ea9fa54f..dcc370a1 100644 --- a/phpdotnet/phd/Package/PHP/Web.php +++ b/phpdotnet/phd/Package/PHP/Web.php @@ -277,32 +277,23 @@ private function processIndexForSearch(): array { /** * Finds the closest parent book or set in the index hierarchy. - * - * Books are prioritized over sets. If no book is found, - * the closest set is returned. If neither is found, null is returned. */ private function findParentBookOrSet(string $id): ?array { - $set = null; - // array_key_exists() to guard against undefined array keys, either for // root elements (no parent) or in case the index structure is broken. while (array_key_exists($id, $this->indexes)) { $parent = $this->indexes[$id]; $element = $parent['element']; - if ($element === 'book') { + if ($element === 'book' || $element === 'set') { return $parent; } - if ($element === 'set') { - $set ??= $parent; - } - $id = $parent['parent_id']; } - return $set; + return null; } public function loadSourcesInfo() { From 177c4e4fef4ca3af9a0707b821fe1423b4752163 Mon Sep 17 00:00:00 2001 From: Lucas Azevedo Date: Thu, 31 Oct 2024 17:17:00 -0300 Subject: [PATCH 4/4] Rename processIndexForSearch to improve clarity Also, updated the doc block with additional context. --- phpdotnet/phd/Package/PHP/Web.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/phpdotnet/phd/Package/PHP/Web.php b/phpdotnet/phd/Package/PHP/Web.php index dcc370a1..452a897b 100644 --- a/phpdotnet/phd/Package/PHP/Web.php +++ b/phpdotnet/phd/Package/PHP/Web.php @@ -234,7 +234,7 @@ public function footer($id) { protected function writeJsonIndex() { v("Writing search indexes..", VERBOSE_FORMAT_RENDERING); - [$entries, $descriptions] = $this->processIndexForSearch(); + [$entries, $descriptions] = $this->processJsonIndex(); file_put_contents( $this->getOutputDir() . "search-index.json", json_encode($entries) @@ -247,9 +247,10 @@ protected function writeJsonIndex() { } /** - * Processes the search index to extract entries and descriptions. + * Processes the index to extract entries and descriptions. These are + * used to generate the search index and the descriptions JSON files. */ - private function processIndexForSearch(): array { + private function processJsonIndex(): array { $entries = []; $descriptions = []; foreach($this->indexes as $id => $index) {