_binary From 4889c7e6032d92fbfc2cdaa31268f53fb58be584 Mon Sep 17 00:00:00 2001
From: Gargi Sheguri <gargiegs@gmail.com>
Date: Thu, 21 Sep 2023 19:55:07 -0700
Subject: [PATCH] Fix for backward compatibility of
 IndexDocumentBundle::isType() with old letter code format

---
 src/library/IndexDocumentBundle.php | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/src/library/IndexDocumentBundle.php b/src/library/IndexDocumentBundle.php
index 21668fa12..42661a6b6 100644
--- a/src/library/IndexDocumentBundle.php
+++ b/src/library/IndexDocumentBundle.php
@@ -1214,6 +1214,21 @@ class IndexDocumentBundle implements CrawlConstants
             56 => "text",
             64 => "video",
         ];
+        /**
+         * This map is maintained for backward compatibility, i.e., for the $key values
+         * using the previous letter code format.
+         */
+        $old_type_map = [
+            "b" => "binary",
+            "d" => "old_doc",
+            "e" => "external_link",
+            "f" => "feed",
+            "i" => "internal_link",
+            "l" => "old_link",
+            "p" => "image",
+            "t" => "text",
+            "v" => "video",
+        ];
         if (is_string($types)) {
             $types = [$types];
         }
@@ -1224,8 +1239,14 @@ class IndexDocumentBundle implements CrawlConstants
             $types = array_merge($types, ["binary", "feed", "image",
                 "old_doc", "text", "video"]);
         }
-        $key_type = ord($key[self::DOCID_PART_LEN << 1] ?? 0) & 120;
-        return in_array($type_map[$key_type] ?? "old_link", $types);
+        $doc_id_format = ord($key[self::DOCID_PART_LEN << 1] ?? 0) & 96;
+        if ($doc_id_format != 96) {
+            $key_type = ord($key[self::DOCID_PART_LEN << 1] ?? 0) & 120;
+            return in_array($type_map[$key_type] ?? "old_link", $types);
+        }
+        // $key uses the old letter code format
+        $key_type = chr(ord($key[self::DOCID_PART_LEN << 1] ?? 0) & 127);
+        return in_array($old_type_map[$key_type] ?? "old_link", $types);
     }
     /**
      * As pre-step to calculating the inverted index information for a partition
-- 
2.42.0

