Skip to content

Commit

Permalink
Fix search index for entries without parent book (#160)
Browse files Browse the repository at this point in the history
* 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 <book>
element. Additionally, refactors writeJsonIndex() into smaller methods.

Fixes #159

* Prefer array_key_exists over isset

* Return set elements immediately

* Rename processIndexForSearch to improve clarity

Also, updated the doc block with additional context.
  • Loading branch information
lhsazevedo authored Nov 3, 2024
1 parent 0364e4f commit c578cc9
Showing 1 changed file with 50 additions and 18 deletions.
68 changes: 50 additions & 18 deletions phpdotnet/phd/Package/PHP/Web.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,35 +234,67 @@ public function footer($id) {

protected function writeJsonIndex() {
v("Writing search indexes..", VERBOSE_FORMAT_RENDERING);
$ids = array();
$desc = array();
[$entries, $descriptions] = $this->processJsonIndex();
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 index to extract entries and descriptions. These are
* used to generate the search index and the descriptions JSON files.
*/
private function processJsonIndex(): array {
$entries = [];
$descriptions = [];
foreach($this->indexes as $id => $index) {
if (!$index["chunk"]) {
continue;
}

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.
*/
private function findParentBookOrSet(string $id): ?array
{
// 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' || $element === 'set') {
return $parent;
}

$id = $parent['parent_id'];
}

return null;
}

public function loadSourcesInfo() {
Expand Down

0 comments on commit c578cc9

Please sign in to comment.