Skip to content

Commit c578cc9

Browse files
authored
Fix search index for entries without parent book (#160)
* 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.
1 parent 0364e4f commit c578cc9

File tree

1 file changed

+50
-18
lines changed

1 file changed

+50
-18
lines changed

phpdotnet/phd/Package/PHP/Web.php

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -234,35 +234,67 @@ public function footer($id) {
234234

235235
protected function writeJsonIndex() {
236236
v("Writing search indexes..", VERBOSE_FORMAT_RENDERING);
237-
$ids = array();
238-
$desc = array();
237+
[$entries, $descriptions] = $this->processJsonIndex();
238+
file_put_contents(
239+
$this->getOutputDir() . "search-index.json",
240+
json_encode($entries)
241+
);
242+
file_put_contents(
243+
$this->getOutputDir() . "search-description.json",
244+
json_encode($descriptions)
245+
);
246+
v("Index written", VERBOSE_FORMAT_RENDERING);
247+
}
248+
249+
/**
250+
* Processes the index to extract entries and descriptions. These are
251+
* used to generate the search index and the descriptions JSON files.
252+
*/
253+
private function processJsonIndex(): array {
254+
$entries = [];
255+
$descriptions = [];
239256
foreach($this->indexes as $id => $index) {
240257
if (!$index["chunk"]) {
241258
continue;
242259
}
243260

244261
if ($index["sdesc"] === "" && $index["ldesc"] !== "") {
245262
$index["sdesc"] = $index["ldesc"];
246-
247-
$parentId = $index['parent_id'];
248-
// isset() to guard against undefined array keys, either for root
249-
// elements (no parent) or in case the index structure is broken.
250-
while (isset($this->indexes[$parentId])) {
251-
$parent = $this->indexes[$parentId];
252-
if ($parent['element'] === 'book') {
253-
$index["ldesc"] = Format::getLongDescription($parent['docbook_id']);
254-
break;
255-
}
256-
$parentId = $parent['parent_id'];
263+
$bookOrSet = $this->findParentBookOrSet($index['parent_id']);
264+
if ($bookOrSet) {
265+
$index["ldesc"] = Format::getLongDescription(
266+
$bookOrSet['docbook_id']
267+
);
257268
}
258269
}
259270

260-
$ids[] = array($index["sdesc"], $index["filename"], $index["element"]);
261-
$desc[$id] = $index["ldesc"];
271+
$entries[] = [
272+
$index["sdesc"], $index["filename"], $index["element"]
273+
];
274+
$descriptions[$id] = $index["ldesc"];
262275
}
263-
file_put_contents($this->getOutputDir() . "search-index.json", json_encode($ids));
264-
file_put_contents($this->getOutputDir() . "search-description.json", json_encode($desc));
265-
v("Index written", VERBOSE_FORMAT_RENDERING);
276+
return [$entries, $descriptions];
277+
}
278+
279+
/**
280+
* Finds the closest parent book or set in the index hierarchy.
281+
*/
282+
private function findParentBookOrSet(string $id): ?array
283+
{
284+
// array_key_exists() to guard against undefined array keys, either for
285+
// root elements (no parent) or in case the index structure is broken.
286+
while (array_key_exists($id, $this->indexes)) {
287+
$parent = $this->indexes[$id];
288+
$element = $parent['element'];
289+
290+
if ($element === 'book' || $element === 'set') {
291+
return $parent;
292+
}
293+
294+
$id = $parent['parent_id'];
295+
}
296+
297+
return null;
266298
}
267299

268300
public function loadSourcesInfo() {

0 commit comments

Comments
 (0)