From 40dfeaddf8ae05604f803b55f9f5c69c04a764a6 Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Sat, 11 Jul 2009 10:34:48 -0400
Subject: [PATCH] - Performance improvements by use UID commands (#1485690)

---
 CHANGELOG                      |    1 
 program/include/rcube_imap.php |   43 ++++++---------------
 program/lib/imap.inc           |   20 +++-------
 3 files changed, 19 insertions(+), 45 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index c61b7b9..f74d246 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 CHANGELOG RoundCube Webmail
 ===========================
 
+- Performance improvements by use UID commands (#1485690)
 - Fix HTML editor tabIndex setting (#1485972)
 - Added 'imap_debug' option
 - Support strftime's format modifiers in date_* options (#1484806)
diff --git a/program/include/rcube_imap.php b/program/include/rcube_imap.php
index 5bd3542..be8e084 100644
--- a/program/include/rcube_imap.php
+++ b/program/include/rcube_imap.php
@@ -1119,21 +1119,16 @@
       return $headers->structure;
     }
 
-    // resolve message sequence number
-    if (!($msg_id = $this->_uid2id($uid))) {
-      return FALSE;
-    }
-
     if (!$structure_str)
-      $structure_str = iil_C_FetchStructureString($this->conn, $this->mailbox, $msg_id);
+      $structure_str = iil_C_FetchStructureString($this->conn, $this->mailbox, $uid, true);
     $structure = iml_GetRawStructureArray($structure_str);
     $struct = false;
 
     // parse structure and add headers
     if (!empty($structure))
       {
-      $this->_msg_id = $msg_id;
       $headers = $this->get_headers($uid);
+      $this->_msg_id = $headers->id;
 
       // set message charset from message headers
       if ($headers->charset)
@@ -1271,7 +1266,7 @@
     // fetch message headers if message/rfc822 or named part (could contain Content-Location header)
     if ($struct->ctype_primary == 'message' || ($struct->ctype_parameters['name'] && !$struct->content_id)) {
       if (empty($raw_headers))
-        $raw_headers = iil_C_FetchPartHeader($this->conn, $this->mailbox, $this->_msg_id, $struct->mime_id);
+        $raw_headers = iil_C_FetchPartHeader($this->conn, $this->mailbox, $this->_msg_id, false, $struct->mime_id);
       $struct->headers = $this->_parse_headers($raw_headers) + $struct->headers;
     }
 
@@ -1314,7 +1309,7 @@
       // we must fetch and parse headers "manually"
       if ($i<2) {
 	if (!$headers)
-          $headers = iil_C_FetchPartHeader($this->conn, $this->mailbox, $this->_msg_id, $part->mime_id);
+          $headers = iil_C_FetchPartHeader($this->conn, $this->mailbox, $this->_msg_id, false, $part->mime_id);
         $filename_mime = '';
         $i = 0;
         while (preg_match('/filename\*'.$i.'\s*=\s*"*([^"\n;]+)[";]*/', $headers, $matches)) {
@@ -1331,7 +1326,7 @@
       }
       if ($i<2) {
 	if (!$headers)
-          $headers = iil_C_FetchPartHeader($this->conn, $this->mailbox, $this->_msg_id, $part->mime_id);
+          $headers = iil_C_FetchPartHeader($this->conn, $this->mailbox, $this->_msg_id, false, $part->mime_id);
         $filename_encoded = '';
         $i = 0; $matches = array();
         while (preg_match('/filename\*'.$i.'\*\s*=\s*"*([^"\n;]+)[";]*/', $headers, $matches)) {
@@ -1348,7 +1343,7 @@
       }
       if ($i<2) {
 	if (!$headers)
-          $headers = iil_C_FetchPartHeader($this->conn, $this->mailbox, $this->_msg_id, $part->mime_id);
+          $headers = iil_C_FetchPartHeader($this->conn, $this->mailbox, $this->_msg_id, false, $part->mime_id);
         $filename_mime = '';
         $i = 0; $matches = array();
         while (preg_match('/\s+name\*'.$i.'\s*=\s*"*([^"\n;]+)[";]*/', $headers, $matches)) {
@@ -1365,7 +1360,7 @@
       }
       if ($i<2) {
 	if (!$headers)
-          $headers = iil_C_FetchPartHeader($this->conn, $this->mailbox, $this->_msg_id, $part->mime_id);
+          $headers = iil_C_FetchPartHeader($this->conn, $this->mailbox, $this->_msg_id, false, $part->mime_id);
         $filename_encoded = '';
         $i = 0; $matches = array();
         while (preg_match('/\s+name\*'.$i.'\*\s*=\s*"*([^"\n;]+)[";]*/', $headers, $matches)) {
@@ -1429,13 +1424,10 @@
    */
   function &get_message_part($uid, $part=1, $o_part=NULL, $print=NULL, $fp=NULL)
     {
-    if (!($msg_id = $this->_uid2id($uid)))
-      return FALSE;
-    
     // get part encoding if not provided
     if (!is_object($o_part))
       {
-      $structure_str = iil_C_FetchStructureString($this->conn, $this->mailbox, $msg_id); 
+      $structure_str = iil_C_FetchStructureString($this->conn, $this->mailbox, $uid, true); 
       $structure = iml_GetRawStructureArray($structure_str);
       $part_type = iml_GetPartTypeCode($structure, $part);
       $o_part = new rcube_message_part;
@@ -1448,7 +1440,7 @@
 
     if (!$part) $part = 'TEXT';
 
-    $body = iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id, $part,
+    $body = iil_C_HandlePartBody($this->conn, $this->mailbox, $uid, true, $part,
         $o_part->encoding, $print, $fp);
       
     if ($fp || $print)
@@ -1490,10 +1482,7 @@
    */
   function &get_raw_body($uid)
     {
-    if (!($msg_id = $this->_uid2id($uid)))
-      return FALSE;
-
-    return iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id);
+    return iil_C_HandlePartBody($this->conn, $this->mailbox, $uid, true);
     }
 
 
@@ -1505,12 +1494,7 @@
    */
   function &get_raw_headers($uid)
     {
-    if (!($msg_id = $this->_uid2id($uid)))
-      return FALSE;
-
-    $headers = iil_C_FetchPartHeader($this->conn, $this->mailbox, $msg_id, NULL);
-
-    return $headers;    
+    return iil_C_FetchPartHeader($this->conn, $this->mailbox, $uid, true);
     }
     
 
@@ -1521,10 +1505,7 @@
    */ 
   function print_raw_body($uid)
     {
-    if (!($msg_id = $this->_uid2id($uid)))
-      return FALSE;
-
-    iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id, NULL, NULL, true);
+    iil_C_HandlePartBody($this->conn, $this->mailbox, $uid, true, NULL, NULL, true);
     }
 
 
diff --git a/program/lib/imap.inc b/program/lib/imap.inc
index a8f858e..0ab2b7f 100644
--- a/program/lib/imap.inc
+++ b/program/lib/imap.inc
@@ -2190,14 +2190,14 @@
 	return $result;
 }
 
-function iil_C_FetchPartHeader(&$conn, $mailbox, $id, $part) {
+function iil_C_FetchPartHeader(&$conn, $mailbox, $id, $is_uid=false, $part=NULL) {
 
 	$part = empty($part) ? 'HEADER' : $part.'.MIME';
 
-        return iil_C_HandlePartBody($conn, $mailbox, $id, $part);
+        return iil_C_HandlePartBody($conn, $mailbox, $id, $is_uid, $part);
 }
 
-function iil_C_HandlePartBody(&$conn, $mailbox, $id, $part='', $encoding=NULL, $print=NULL, $file=NULL) {
+function iil_C_HandlePartBody(&$conn, $mailbox, $id, $is_uid=false, $part='', $encoding=NULL, $print=NULL, $file=NULL) {
 	
 	$fp     = $conn->fp;
 	$result = false;
@@ -2224,7 +2224,7 @@
 
     		// format request
 		$key     = 'ftch0';
-		$request = $key . " FETCH $id (BODY.PEEK[$part])";
+		$request = $key . ($is_uid ? ' UID' : '') . " FETCH $id (BODY.PEEK[$part])";
     		// send request
 		if (!iil_PutLine($fp, $request)) {
 		    return false;
@@ -2344,14 +2344,6 @@
 	}
     
 	return false;
-}
-
-function iil_C_FetchPartBody(&$conn, $mailbox, $id, $part, $file=NULL) {
-	return iil_C_HandlePartBody($conn, $mailbox, $id, $part, NULL, NULL, $file);
-}
-
-function iil_C_PrintPartBody(&$conn, $mailbox, $id, $part) {
-	iil_C_HandlePartBody($conn, $mailbox, $id, $part, NULL, true, NULL);
 }
 
 function iil_C_CreateFolder(&$conn, $folder) {
@@ -2477,14 +2469,14 @@
 	return false;
 }
 
-function iil_C_FetchStructureString(&$conn, $folder, $id) {
+function iil_C_FetchStructureString(&$conn, $folder, $id, $is_uid=false) {
 	$fp     = $conn->fp;
 	$result = false;
 	
 	if (iil_C_Select($conn, $folder)) {
 		$key = 'F1247';
 
-		if (iil_PutLine($fp, "$key FETCH $id (BODYSTRUCTURE)")) {
+		if (iil_PutLine($fp, $key . ($is_uid ? ' UID' : '') ." FETCH $id (BODYSTRUCTURE)")) {
 			do {
 				$line = iil_ReadLine($fp, 5000);
 				$line = iil_MultLine($fp, $line);

--
Gitblit v1.9.1