From 010a350715f1a36eab666fe26d3118ed025133c1 Mon Sep 17 00:00:00 2001
From: Thomas Bruederli <thomas@roundcube.net>
Date: Tue, 15 Oct 2013 05:44:34 -0400
Subject: [PATCH] Minor improvements to threaded searching

---
 program/lib/Roundcube/rcube_imap.php        |    8 +++-----
 program/lib/Roundcube/rcube_imap_search.php |   36 +++++++++++++++++++++++-------------
 2 files changed, 26 insertions(+), 18 deletions(-)

diff --git a/program/lib/Roundcube/rcube_imap.php b/program/lib/Roundcube/rcube_imap.php
index 98c8f7a..ff88bdc 100644
--- a/program/lib/Roundcube/rcube_imap.php
+++ b/program/lib/Roundcube/rcube_imap.php
@@ -934,7 +934,7 @@
             $to    = $from + $page_size;
 
             // sort headers
-            if (!$this->threading) {
+            if (!$this->threading && !empty($a_msg_headers)) {
                 $a_msg_headers = $this->conn->sortHeaders($a_msg_headers, $this->sort_field, $this->sort_order);
             }
 
@@ -1441,10 +1441,8 @@
             new rcube_result_index; // trigger autoloader and make these classes available for threaded context
             new rcube_result_thread;
 
-            // connect IMAP
-            if (!defined('PTHREADS_INHERIT_ALL')) {
-                $this->check_connection();
-            }
+            // connect IMAP to have all the required classes and settings loaded
+            $this->check_connection();
 
             $searcher = new rcube_imap_search($this->options, $this->conn);
             $results = $searcher->exec(
diff --git a/program/lib/Roundcube/rcube_imap_search.php b/program/lib/Roundcube/rcube_imap_search.php
index ed4face..d82ec8a 100644
--- a/program/lib/Roundcube/rcube_imap_search.php
+++ b/program/lib/Roundcube/rcube_imap_search.php
@@ -172,9 +172,9 @@
 
     public function run()
     {
-        #trigger_error("Start search $this->folder", E_USER_NOTICE);
+        // trigger_error("Start search $this->folder", E_USER_NOTICE);
         $this->result = $this->search_index();
-        #trigger_error("End search $this->folder: " . $this->result->count(), E_USER_NOTICE);
+        // trigger_error("End search $this->folder: " . $this->result->count(), E_USER_NOTICE);
     }
 
     /**
@@ -182,6 +182,7 @@
      */
     protected function search_index()
     {
+        $pthreads = defined('PTHREADS_INHERIT_ALL');
         $criteria = $this->search;
         $charset = $this->charset;
 
@@ -216,6 +217,10 @@
                     rcube_imap::convert_criteria($criteria, $charset), true, 'US-ASCII');
             }
 
+            // close IMAP connection again
+            if ($pthreads)
+                $imap->closeConnection();
+
             return $threads;
         }
 
@@ -228,20 +233,22 @@
                 $messages = $imap->sort($this->folder, $this->sort_field,
                     rcube_imap::convert_criteria($criteria, $charset), true, 'US-ASCII');
             }
+        }
 
-            if (!$messages->is_error()) {
-                return $messages;
+        if (!$messages || !$messages->is_error()) {
+            $messages = $imap->search($this->folder,
+                ($charset && $charset != 'US-ASCII' ? "CHARSET $charset " : '') . $criteria, true);
+
+            // Error, try with US-ASCII (some servers may support only US-ASCII)
+            if ($messages->is_error() && $charset && $charset != 'US-ASCII') {
+                $messages = $imap->search($this->folder,
+                    rcube_imap::convert_criteria($criteria, $charset), true);
             }
         }
 
-        $messages = $imap->search($this->folder,
-            ($charset && $charset != 'US-ASCII' ? "CHARSET $charset " : '') . $criteria, true);
-
-        // Error, try with US-ASCII (some servers may support only US-ASCII)
-        if ($messages->is_error() && $charset && $charset != 'US-ASCII') {
-            $messages = $imap->search($this->folder,
-                rcube_imap::convert_criteria($criteria, $charset), true);
-        }
+        // close IMAP connection again
+        if ($pthreads)
+            $imap->closeConnection();
 
         return $messages;
     }
@@ -279,6 +286,8 @@
      */
     public function __construct($id, $options)
     {
+        $options['ident']['command'] = 'search-'.$id;
+
         $this->id = $id;
         $this->options = $options;
     }
@@ -296,11 +305,12 @@
         # $conn->setDebug(true, function($conn, $message){ trigger_error($message, E_USER_NOTICE); });
 
         if ($this->options['user'] && $this->options['password']) {
+            // TODO: do this synchronized to avoid warnings like "Only one Id allowed in non-authenticated state"
             $conn->connect($this->options['host'], $this->options['user'], $this->options['password'], $this->options);
         }
 
         if ($conn->error)
-            trigger_error($this->conn->error, E_USER_WARNING);
+            trigger_error($conn->error, E_USER_WARNING);
 
         #$this->conn = $conn;
         return $conn;

--
Gitblit v1.9.1