_binary From 7b2e8d3eb033e6b125b90bfbf50ac30bf7c85ceb Mon Sep 17 00:00:00 2001
From: Gargi Sheguri <gargiegs@gmail.com>
Date: Mon, 27 Feb 2023 11:38:29 -0800
Subject: [PATCH] Fixing deprecation warnings on upgrade to PHP v8.2

---
 src/library/CrawlQueueBundle.php        |  9 +++----
 src/library/IndexDocumentBundle.php     | 23 +++++++++++-----
 src/library/IndexShard.php              | 20 ++++++++++++++
 src/library/LinearHashTable.php         | 11 +++++---
 src/library/PartitionDocumentBundle.php | 12 ++++++---
 src/library/StochasticTermSegmenter.php | 36 ++++++++++++++++---------
 tests/IndexManagerTest.php              |  5 ++++
 tests/LinearHashTableTest.php           |  5 ++++
 8 files changed, 91 insertions(+), 30 deletions(-)

diff --git a/src/library/CrawlQueueBundle.php b/src/library/CrawlQueueBundle.php
index 07f1fd6b6..74ac57071 100644
--- a/src/library/CrawlQueueBundle.php
+++ b/src/library/CrawlQueueBundle.php
@@ -208,7 +208,6 @@ class CrawlQueueBundle
         $this->etag_table = new LinearHashTable($dir_name .
             '/EtagExpiresInfo', ["PRIMARY KEY" => "URL",
             "ETAG" => "TEXT", "EXPIRES" => "INT"]);
-        $this->notify_buffer = [];
     }
     //Filter and Filter Bundle Methods
     /**
@@ -272,7 +271,7 @@ class CrawlQueueBundle
             $tuples_by_host[$hash_host] ??= [];
             $tuples_by_host[$hash_host][] = $url_tuple;
         }
-        foreach($tuples_by_host as $hash_host => $host_tuples) {
+        foreach ($tuples_by_host as $hash_host => $host_tuples) {
             $hash_host_dir = $robot_wait_dir . "/$hash_host";
             $this->addUrlsDirectory($hash_host_dir, $host_tuples);
         }
@@ -587,7 +586,7 @@ class CrawlQueueBundle
         }
         if ($crawl_order == CrawlConstants::BREADTH_FIRST) {
             $is_empty = true;
-            foreach($sub_dirs as $sub_dir) {
+            foreach ($sub_dirs as $sub_dir) {
                 $day_folders = $this->getDayFolders($sub_dir);
                 if (!empty($day_folders)) {
                     $is_empty = false;
@@ -649,7 +648,7 @@ class CrawlQueueBundle
         $day_folders = $this->getDayFolders($crawl_delayed_folder);
         //maximum crawl delay will honor is one day
         $yesterday = floor((time() - C\ONE_DAY)/C\ONE_DAY);
-        if(empty($day_folders)) {
+        if (empty($day_folders)) {
             return; //no one is waiting
         }
         $robot_rows = [];
@@ -894,4 +893,4 @@ class CrawlQueueBundle
         file_put_contents($this->dir_name."/url_timestamp.txt", time());
         $this->url_exists_filter_bundle->reset();
     }
-}
+}
\ No newline at end of file
diff --git a/src/library/IndexDocumentBundle.php b/src/library/IndexDocumentBundle.php
index 03dea8009..7e7a909b8 100644
--- a/src/library/IndexDocumentBundle.php
+++ b/src/library/IndexDocumentBundle.php
@@ -239,6 +239,17 @@ class IndexDocumentBundle implements CrawlConstants
      * @var PackedTableTools
      */
     public $postings_tools;
+    /**
+     * Keeps track of the number of documents present in the current partition
+     * @var int
+     */
+    public $doc_map_counter;
+    /**
+     * Holds the total time needed to extract phrases (sequences of adjacent
+     * words) from site descriptions for a partition
+     * @var int
+     */
+    public $extract_phrase_time;
     /**
      * Makes or initializes an IndexDocumentBundle with the provided parameters
      *
@@ -270,7 +281,7 @@ class IndexDocumentBundle implements CrawlConstants
             $this->archive_info["DESCRIPTION"] = $description;
         }
         $just_got_info = false;
-        if(file_exists($archive_info_path)) {
+        if (file_exists($archive_info_path)) {
             $this->archive_info = unserialize(file_get_contents(
                 $archive_info_path));
             $just_got_info = true;
@@ -504,7 +515,7 @@ class IndexDocumentBundle implements CrawlConstants
             $row = decode255(substr($pre_row, $key_len));
             $postings_len = strlen($row);
             $out_postings .= $row;
-            if(crawlTimeoutLog("..Indexer Still processing partition ".
+            if (crawlTimeoutLog("..Indexer Still processing partition ".
                 "$partition. Have completed $i postings of $num_postings.") &&
                 $taking_too_long_touch) {
                 if (file_exists($taking_too_long_touch)) {
@@ -938,7 +949,7 @@ class IndexDocumentBundle implements CrawlConstants
             (empty($site[self::USER_RANKS])) ? [] :
             $site[self::USER_RANKS];
         $num_words = 0;
-        foreach($word_lists as $word => $position_list)
+        foreach ($word_lists as $word => $position_list)
         {
             $num_words += count($position_list);
         }
@@ -1036,10 +1047,10 @@ class IndexDocumentBundle implements CrawlConstants
         $out_rows = [["POS" => $num_words, "SCORE" => floatval($score)],
             ["POS" => $preface_positions, "SCORE" =>
                 floatval($num_description_scores)]];
-        foreach($description_scores as $position => $score) {
+        foreach ($description_scores as $position => $score) {
             $out_rows[] = ["POS" => $position, "SCORE" => floatval($score)];
         }
-        foreach($user_ranks as $user_rank) {
+        foreach ($user_ranks as $user_rank) {
             $out_rows[] = ["POS" => 0, "SCORE" => floatval($score)];
         }
         $entry = $this->doc_map_tools->pack($out_rows);
@@ -1625,4 +1636,4 @@ class IndexDocumentBundle implements CrawlConstants
         }
         return false;
     }
-}
+}
\ No newline at end of file
diff --git a/src/library/IndexShard.php b/src/library/IndexShard.php
index d2def0053..1df0ab8f2 100644
--- a/src/library/IndexShard.php
+++ b/src/library/IndexShard.php
@@ -282,6 +282,26 @@ class IndexShard extends PersistentStructure implements CrawlConstants
      * @var string
      */
     public $word_postings;
+    /**
+     * Used to hold the computed 8 byte hash of the index shard filename
+     * @var string
+     */
+    public $hash_name;
+    /**
+     * Stores $blocks contents in (32 bit) unsigned int
+     * @var array
+     */
+    public $blocks_words;
+    /**
+     * Holds offset of the word_docs strings
+     * @var int
+     */
+    public $word_doc_offset;
+    /**
+     * Holds offset of the doc_infos strings
+     * @var int
+     */
+    public $doc_info_offset;
     /**
      * Makes an index shard with the given file name and generation offset
      *
diff --git a/src/library/LinearHashTable.php b/src/library/LinearHashTable.php
index 85d85f222..4c394d6bd 100644
--- a/src/library/LinearHashTable.php
+++ b/src/library/LinearHashTable.php
@@ -184,6 +184,11 @@ class LinearHashTable
      * @var object
      */
     public $table_tools;
+    /**
+     * File handle for index file of current partition
+     * @var resource
+     */
+    public $save_partition_handle;
     /**
      * Creates/Loads LinearHashTable having specified folder and parameters
      *
@@ -222,7 +227,7 @@ class LinearHashTable
         foreach ($folder_paths as $folder_path) {
             if (!file_exists($folder_path)) {
                 $changed_parameters = true;
-                if(!mkdir($folder_path)) {
+                if (!mkdir($folder_path)) {
                     return null;
                 }
             }
@@ -792,7 +797,7 @@ class LinearHashTable
     public static function getParameterInfo($folder)
     {
         $parameter_path = $folder . "/" . self::PARAMETERS_FILE;
-        if(file_exists($parameter_path)) {
+        if (file_exists($parameter_path)) {
             return unserialize(file_get_contents($parameter_path));
         } else {
             return [];
@@ -1119,4 +1124,4 @@ class LinearHashTable
         $next_expected_num_files = ceil($new_count/$max_items_per_file);
         return $next_expected_num_files - $current_expected_num_files;
     }
-}
+}
\ No newline at end of file
diff --git a/src/library/PartitionDocumentBundle.php b/src/library/PartitionDocumentBundle.php
index 5f8a13306..f992149be 100644
--- a/src/library/PartitionDocumentBundle.php
+++ b/src/library/PartitionDocumentBundle.php
@@ -171,6 +171,12 @@ class PartitionDocumentBundle
      * @var object
      */
     public $table_tools;
+    /**
+     * Holds loaded unserialized index file data from $partition partition
+     * bundle
+     * @var mixed
+     */
+    public $save_index;
     /**
      * Used to create a new instance of a PartitionDocumentBundle
      *
@@ -214,7 +220,7 @@ class PartitionDocumentBundle
         foreach ($folder_paths as $folder_path) {
             if (!file_exists($folder_path)) {
                 $changed_parameters = true;
-                if(!mkdir($folder_path)) {
+                if (!mkdir($folder_path)) {
                     return null;
                 }
             }
@@ -621,7 +627,7 @@ class PartitionDocumentBundle
     public static function getParameterInfo($folder)
     {
         $parameter_path = $folder . "/" . self::PARAMETERS_FILE;
-        if(file_exists($parameter_path)) {
+        if (file_exists($parameter_path)) {
             $parameters = unserialize(file_get_contents($parameter_path));
             if (!is_array($parameters)) {
                  $parameters = [];
@@ -674,4 +680,4 @@ class PartitionDocumentBundle
             $previous_instance_time];
         return [$offset, $len, $save_partition];
     }
-}
+}
\ No newline at end of file
diff --git a/src/library/StochasticTermSegmenter.php b/src/library/StochasticTermSegmenter.php
index 4a76e681d..e1a22a483 100644
--- a/src/library/StochasticTermSegmenter.php
+++ b/src/library/StochasticTermSegmenter.php
@@ -95,6 +95,16 @@ class StochasticTermSegmenter
      * Maximum character length of a term
      */
     const MAX_TERM_LENGTH = 7;
+    /**
+     * Holds instance of Tokenizer for $lang language
+     * @var object
+     */
+    public $tokenizer;
+    /**
+     * Holds $tokenizer's instance of NamedEntityContextTagger
+     * @var object
+     */
+    public $named_entity_tagger;
     /**
      * Constructs an instance of this class used for segmenting string with
      * respect to words in a locale using a probabilistic approach to evaluate
@@ -185,9 +195,9 @@ class StochasticTermSegmenter
     {
         $ctb_fmt = false;
         switch ($format) {
-            case("default"):
+            case ("default"):
                 break;
-            case("CTB"):
+            case ("CTB"):
                 $ctb_fmt = true;
                 break;
             default:
@@ -200,7 +210,7 @@ class StochasticTermSegmenter
         if (is_string($text_files)) {
             $text_files = [$text_files];
         }
-        foreach($text_files as $text_file) {
+        foreach ($text_files as $text_file) {
             if (file_exists($text_file) && !is_dir($text_file)) {
                 $fh = fopen($text_file, "r");
                 while(!feof($fh))  {
@@ -265,7 +275,7 @@ class StochasticTermSegmenter
         if (is_string($text_files)) {
             $text_files = [$text_files];
         }
-        foreach($text_files as $text_file) {
+        foreach ($text_files as $text_file) {
             if (file_exists($text_file)) {
                 $fh = fopen($text_file, "r");
                 while(! feof($fh))  {
@@ -319,9 +329,9 @@ class StochasticTermSegmenter
     public function segmentSentence($sentence)
     {
         $t = preg_split("/[\s　]+/u", trim($sentence));
-        if(count($t) > 1) {
+        if (count($t) > 1) {
             $ret = [];
-            foreach($t as $s) {
+            foreach ($t as $s) {
                 $segments = $this->segmentSentence($s);
                 if (is_array($segments)) {
                     $ret = array_merge($ret, $segments);
@@ -358,7 +368,7 @@ class StochasticTermSegmenter
         if (isset($this->named_entity_tagger)) {
             $named_entities = $this->named_entity_tagger->predict(
                 $characters);
-            foreach($named_entities as $e) {
+            foreach ($named_entities as $e) {
                 $this->add($e[0], 1, $net_dict);
             }
         }
@@ -366,12 +376,12 @@ class StochasticTermSegmenter
         $path = [];
         //init base
         $score[-1] = 0;
-        for($index = 0; $index < count($characters); $index++) {
+        for ($index = 0; $index < count($characters); $index++) {
             //If not current language
             if ($this->notCurrentLang($characters[$index]) &&
                 !$this->isPunctuation($characters[$index])) {
                 $current_char = $characters[$index];
-                for($j = $index + 1; $j < count($characters); $j++) {
+                for ($j = $index + 1; $j < count($characters); $j++) {
                     if ($this->notCurrentLang($current_char . $characters[$j])&&
                         !$this->isPunctuation($characters[$j])) {
                         $current_char .= $characters[$j];
@@ -389,7 +399,7 @@ class StochasticTermSegmenter
             //If date or number
             if ($this->isException($characters[$index])) {
                 $current_char = $characters[$index];
-                for($j = $index + 1; $j < count($characters); $j++) {
+                for ($j = $index + 1; $j < count($characters); $j++) {
                     if (!$this->isException(
                         $current_char . $characters[$j])) {
                         break;
@@ -407,7 +417,7 @@ class StochasticTermSegmenter
             //If is punctuation, give slightly better score than unknown words
             if ($this->isPunctuation($characters[$index])) {
                 $current_char = $characters[$index];
-                for($j = $index + 1; $j < count($characters); $j++) {
+                for ($j = $index + 1; $j < count($characters); $j++) {
                     if (!$this->isPunctuation(
                         $current_char . $characters[$j])) {
                         break;
@@ -486,7 +496,7 @@ class StochasticTermSegmenter
         }
         $result = [];
         $t = 0;
-        foreach(array_reverse($tmp) as $next_node) {
+        foreach (array_reverse($tmp) as $next_node) {
             $result_word = "";
             while($t <= $next_node) {
               $result_word .= $characters[$t];
@@ -535,4 +545,4 @@ class StochasticTermSegmenter
         // Set end of term marker
         $sub_trie['$'] = $frequency;
     }
-}
+}
\ No newline at end of file
diff --git a/tests/IndexManagerTest.php b/tests/IndexManagerTest.php
index 16c726f5f..20bd55afd 100644
--- a/tests/IndexManagerTest.php
+++ b/tests/IndexManagerTest.php
@@ -58,6 +58,11 @@ use seekquarry\yioop\library\UnitTest;
      * Prefix of folders for index manager test
      */
     const TEST_DIR = __DIR__ . '/test_files/index_manager_test';
+    /**
+     * Holds current instance of IndexDocumentBundle
+     * @var object
+     */
+    public $index_archive;
     /**
      * Before each test sets up a test directory with an old and new bundle in
      * it
diff --git a/tests/LinearHashTableTest.php b/tests/LinearHashTableTest.php
index dfb8e583e..9733b0457 100644
--- a/tests/LinearHashTableTest.php
+++ b/tests/LinearHashTableTest.php
@@ -49,6 +49,11 @@ use seekquarry\yioop\library\UnitTest;
      * Prefix of folders for linear hashing test
      */
     const TEST_DIR = '/test_files/linear_test';
+    /**
+     * Maintains all linear hash tables created for testing
+     * @var array
+     */
+    public $table_dirs;
     /**
      * Sets up an array to keep track of what linear hash tables we've made
      * so that we can delete them when done a test.
-- 
2.39.1

