[DOC-CVS] [phd] master: Fix missing pages in search indexes (#212)

[email protected] (AllenJB via GitHub)
Newsgroups php.doc.cvs
Message-ID <[email protected]>
Author: AllenJB (AllenJB)
Committer: GitHub (web-flow)
Pusher: Girgias
Date: 2025-11-04T06:19:18Z

Commit: https://github.com/php/phd/commit/264c65b187740324f6187cbed9850e10fdab1085
Raw diff: https://github.com/php/phd/commit/264c65b187740324f6187cbed9850e10fdab1085.diff

Fix missing pages in search indexes (#212)

* Search indexes: Fix missing search entries

This handles duplicated ids - but requires changes on web-php to use new indexes

Changed paths:
  M  phpdotnet/phd/IndexRepository.php
  M  phpdotnet/phd/Package/PHP/Web.php
  M  tests/render/render_001.phpt


Diff:

diff --git a/phpdotnet/phd/IndexRepository.php b/phpdotnet/phd/IndexRepository.php
index cd947699..0ecf5dd1 100644
--- a/phpdotnet/phd/IndexRepository.php
+++ b/phpdotnet/phd/IndexRepository.php
@@ -162,6 +162,16 @@ protected function SQLiteIndex($context, $index, $id, $filename, $parent, $sdesc
         ];
     }
 
+    public function getIndexesWithDuplicates(): array
+    {
+        $results =  $this->db->query('SELECT docbook_id, filename, parent_id, sdesc, ldesc, element, previous, next, chunk FROM ids');
+        $indexes = [];
+        while ($row = $results->fetchArray(SQLITE3_ASSOC)) {
+            $indexes[] = $row;
+        }
+        return $indexes;
+    }
+
     private static function SQLiteFinal($context): mixed {
         return $context;
     }
diff --git a/phpdotnet/phd/Package/PHP/Web.php b/phpdotnet/phd/Package/PHP/Web.php
index 68cae82d..54a24216 100644
--- a/phpdotnet/phd/Package/PHP/Web.php
+++ b/phpdotnet/phd/Package/PHP/Web.php
@@ -249,6 +249,18 @@ protected function writeJsonIndex() {
             json_encode($descriptions)
         );
         $this->outputHandler->v("Index written", VERBOSE_FORMAT_RENDERING);
+
+        $entries = $this->processCombinedJsonIndex();
+        file_put_contents(
+            $this->getOutputDir() . "search-combined.json",
+            json_encode($entries)
+        );
+        $entries = 'var localSearchIndexes = '. json_encode($entries) .';';
+        file_put_contents(
+            $this->getOutputDir() . "search-combined.js",
+            $entries
+        );
+        $this->outputHandler->v("Combined Index written", VERBOSE_FORMAT_RENDERING);
     }
 
     /**
@@ -256,10 +268,21 @@ protected function writeJsonIndex() {
      * used to generate the search index and the descriptions JSON files.
      */
     private function processJsonIndex(): array {
+        $alwaysIncludeElements = [
+            'refentry',
+            'stream_wrapper',
+            'phpdoc:classref',
+            'phpdoc:exceptionref',
+            'phpdoc:varentry',
+        ];
+
         $entries = [];
         $descriptions = [];
         foreach($this->indexes as $id => $index) {
-            if (!$index["chunk"]) {
+            if (
+                (! $index['chunk'])
+                && (! in_array($index['element'], $alwaysIncludeElements, true))
+            ) {
                 continue;
             }
 
@@ -281,6 +304,80 @@ private function processJsonIndex(): array {
         return [$entries, $descriptions];
     }
 
+    private function processCombinedJsonIndex(): array
+    {
+        $alwaysIncludeElements = [
+            'refentry',
+            'stream_wrapper',
+            'phpdoc:classref',
+            'phpdoc:exceptionref',
+            'phpdoc:varentry',
+        ];
+
+        $entries = [];
+        $indexes = $this->indexRepository->getIndexesWithDuplicates();
+        foreach ($indexes as $index) {
+            if (
+                (! $index['chunk'])
+                && (! in_array($index['element'], $alwaysIncludeElements, true))
+            ) {
+                continue;
+            }
+
+            if ($index["sdesc"] === "" && $index["ldesc"] !== "") {
+                $index["sdesc"] = $index["ldesc"];
+                $bookOrSet = $this->findParentBookOrSet($index['parent_id']);
+                if ($bookOrSet) {
+                    $index["ldesc"] = Format::getLongDescription(
+                        $bookOrSet['docbook_id']
+                    );
+                }
+            }
+
+            $nameParts = explode('::', $index['sdesc']);
+            $methodName = array_pop($nameParts);
+
+            if (str_contains('wrapper', $index['filename'])) {
+                print "Combined index: adding " . $index['filename'] . " :: " . $index['sdesc'] . "\n";
+            }
+
+            $type = 'General';
+            switch ($index['element']) {
+                case "phpdoc:varentry":
+                    $type = "Variable";
+                    break;
+
+                case "refentry":
+                    $type = "Function";
+                    break;
+
+                case "phpdoc:exceptionref":
+                    $type = "Exception";
+                    break;
+
+                case "phpdoc:classref":
+                    $type = "Class";
+                    break;
+
+                case "set":
+                case "book":
+                case "reference":
+                    $type = "Extension";
+                    break;
+            }
+
+            $entries[] = [
+                'id' => $index['filename'],
+                'name' => $index['sdesc'],
+                'description' => html_entity_decode($index['ldesc']),
+                'tag' => $index['element'],
+                'type' => $type,
+                'methodName' => $methodName,
+            ];
+        }
+        return $entries;
+    }
+
     /**
      * Finds the closest parent book or set in the index hierarchy.
      */
diff --git a/tests/render/render_001.phpt b/tests/render/render_001.phpt
index 2056232d..813bfa9d 100644
--- a/tests/render/render_001.phpt
+++ b/tests/render/render_001.phpt
@@ -16,4 +16,5 @@ require_once __DIR__ . "/../../render.php";
 %s[%d:%d:%d - Rendering Format      ]%s Starting PHP-Web rendering
 %s[%d:%d:%d - Rendering Format      ]%s Writing search indexes..
 %s[%d:%d:%d - Rendering Format      ]%s Index written
+%s[%d:%d:%d - Rendering Format      ]%s Combined Index written
 %s[%d:%d:%d - Rendering Format      ]%s Finished rendering
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.