Removes index-size scaling from doc rank, fixes postgres complaints about addQUeryImpression, fixes empty file handle crashes in word iterator, a=chris

Chris Pollett [2022-07-25 21:Jul:th]
Removes index-size scaling from doc rank, fixes postgres complaints about addQUeryImpression, fixes empty file handle crashes in word iterator, a=chris
Filename
src/controllers/components/SocialComponent.php
src/library/index_bundle_iterators/WordIterator.php
src/models/ImpressionModel.php
diff --git a/src/controllers/components/SocialComponent.php b/src/controllers/components/SocialComponent.php
index defae9bcc..7eca73dfd 100644
--- a/src/controllers/components/SocialComponent.php
+++ b/src/controllers/components/SocialComponent.php
@@ -3806,7 +3806,8 @@ EOD;
         $folder_hash_id = L\crawlHash(($data['PAGE_ID'] ?? -1) . $sub_path);
         if (!empty($_REQUEST['sort']) && in_array($_REQUEST['sort'],
             array_keys($data['sort_fields']))) {
-            if (count($_SESSION['media_sorts']) > 10) {
+            if (!empty($_SESSION['media_sorts']) &&
+                count($_SESSION['media_sorts']) > 10) {
                 $first_key = array_key_first($_SESSION['media_sorts']);
                 unset($_SESSION['media_sorts'][$first_key]);
             }
diff --git a/src/library/index_bundle_iterators/WordIterator.php b/src/library/index_bundle_iterators/WordIterator.php
index 63b74d4c9..e75730ed5 100644
--- a/src/library/index_bundle_iterators/WordIterator.php
+++ b/src/library/index_bundle_iterators/WordIterator.php
@@ -429,8 +429,8 @@ class WordIterator extends IndexBundleIterator
         $doc_map_tools = $index->doc_map_tools;
         $positions_filename = $base_folder . "/" .
             IndexDocumentBundle::POSITIONS_FILENAME;
-        $index_scaling_factor = (10000000000/$this->total_num_docs);
-        $fh = fopen($positions_filename, "r");
+        $fh = (file_exists($positions_filename)) ?
+            fopen($positions_filename, "r") : false;
         $number_of_partitions = $this->total_number_of_partitions;
         $num_doc_keys = $doc_map_tools->countTableEntries($doc_map_filename);
         $is_ascending = ($this->direction == self::ASCENDING);
@@ -482,9 +482,8 @@ class WordIterator extends IndexBundleIterator
                     $number_of_partitions - $num_seen_partitions :
                     $num_seen_partitions - 1;
                 $posting[self::DOC_RANK] = log(
-                    (
                     $remaining_partitions * $this->avg_items_per_partition +
-                    $posting[self::SCORE]), 10);
+                    $posting[self::SCORE], 10);
                 if(L\IndexDocumentBundle::isAHostDocId($doc_key)) {
                     $posting[self::DOC_RANK] += 2;
                 }
@@ -511,7 +510,9 @@ class WordIterator extends IndexBundleIterator
             $posting[self::INDEX_VERSION] = $this->index_version;
             $key_postings[$doc_key] = $posting;
         }
-        fclose($fh);
+        if (!empty($fh)) {
+            fclose($fh);
+        }
         return $key_postings;
     }
     /**
diff --git a/src/models/ImpressionModel.php b/src/models/ImpressionModel.php
index 7e08abf26..773118caa 100644
--- a/src/models/ImpressionModel.php
+++ b/src/models/ImpressionModel.php
@@ -98,6 +98,9 @@ class ImpressionModel extends Model
             return;
         }
         $db = $this->db;
+        $dbinfo = ["DBMS" => C\DBMS, "DB_HOST" => C\DB_HOST,
+            "DB_USER" => C\DB_USER, "DB_PASSWORD" => C\DB_PASSWORD,
+            "DB_NAME" => C\DB_NAME];
         $query_hash = L\crawlHash($query);
         $sql = "SELECT ID FROM QUERY_ITEM WHERE QUERY_HASH = ?";
         $result = $db->execute($sql, [$query_hash]);
@@ -105,6 +108,7 @@ class ImpressionModel extends Model
         if (empty($row['ID'])) {
             $sql = "INSERT INTO QUERY_ITEM (QUERY_HASH, QUERY, CREATION)
                 VALUES (?, ?, ?)";
+            $sql = $db->insertIgnore($sql, $dbinfo);
             $result = $db->execute($sql, [$query_hash, $query, time()]);
             $this->initWithDb(C\PUBLIC_USER_ID, $db->insertID("QUERY_ITEM"),
                 C\QUERY_IMPRESSION, $db);
ViewGit