Changeset 89 in subversion


Ignore:
Timestamp:
Dec 11, 2005 5:56:46 PM (7 years ago)
Author:
roundcube
Message:

Bugfixes on listing and caching functions

Location:
trunk/roundcubemail/program
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/roundcubemail/program/include/bugs.inc

    r83 r89  
    9898  if ($CONFIG['debug_level'] & 4) 
    9999    { 
    100     print "<b>$program Error in $arg_arr[file] ($arg_arr[line]):</b>&nbsp;"; 
     100    print "<b>$program Error"; 
     101 
     102    if (!empty($arg_arr['file']) && !empty($arg_arr['line'])) 
     103      print " in $arg_arr[file] ($arg_arr[line])"; 
     104 
     105    print ":</b>&nbsp;"; 
    101106    print nl2br($arg_arr['message']); 
    102107    print '<br />'; 
  • trunk/roundcubemail/program/include/rcube_imap.inc

    r88 r89  
    3636  var $list_page = 1; 
    3737  var $page_size = 10; 
     38  var $sort_field = 'date'; 
     39  var $sort_order = 'DESC'; 
    3840  var $delimiter = NULL; 
    3941  var $caching_enabled = FALSE; 
     
    280282    // return cached value 
    281283    if (!$force && is_array($a_mailbox_cache[$mailbox]) && isset($a_mailbox_cache[$mailbox][$mode])) 
    282       return $a_mailbox_cache[$mailbox][$mode];       
     284      return $a_mailbox_cache[$mailbox][$mode]; 
     285       
     286    $search_str = "ALL UNDELETED"; 
    283287 
    284288    // get message count and store in cache 
    285289    if ($mode == 'UNSEEN') 
    286       $count = iil_C_CountUnseen($this->conn, $mailbox); 
    287     else 
    288       $count = iil_C_CountMessages($this->conn, $mailbox); 
     290      $search_str .= " UNSEEN"; 
     291 
     292    // get message count using SEARCH 
     293    // not very performant but more precise (using UNDELETED) 
     294    $count = 0; 
     295    $index = $this->_search_index($mailbox, $search_str); 
     296    if (is_array($index)) 
     297      { 
     298      $str = implode(",", $index); 
     299      if (!empty($str)) 
     300        $count = count($index); 
     301      } 
    289302 
    290303    if (is_array($a_mailbox_cache[$mailbox])) 
     
    292305       
    293306    $a_mailbox_cache[$mailbox][$mode] = (int)$count; 
    294      
     307 
    295308    // write back to cache 
    296309    $this->update_cache('messagecount', $a_mailbox_cache); 
     
    302315  // public method for listing headers 
    303316  // convert mailbox name with root dir first 
    304   function list_headers($mbox='', $page=NULL, $sort_field='date', $sort_order='DESC') 
     317  function list_headers($mbox='', $page=NULL, $sort_field=NULL, $sort_order=NULL) 
    305318    { 
    306319    $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox; 
     
    310323 
    311324  // private method for listing message header 
    312   function _list_headers($mailbox='', $page=NULL, $sort_field='date', $sort_order='DESC') 
     325  function _list_headers($mailbox='', $page=NULL, $sort_field=NULL, $sort_order=NULL, $recursive=FALSE) 
    313326    { 
    314327    if (!strlen($mailbox)) 
    315328      return array(); 
     329       
     330    if ($sort_field!=NULL) 
     331      $this->sort_field = $sort_field; 
     332    if ($sort_order!=NULL) 
     333      $this->sort_order = strtoupper($sort_order); 
    316334 
    317335    $max = $this->_messagecount($mailbox); 
     
    323341      $end = $max; 
    324342      } 
    325     else if ($sort_order=='DESC') 
     343    else if ($this->sort_order=='DESC') 
    326344      { 
    327345      $begin = $max - $this->page_size - $start_msg; 
     
    349367    if ($cache_status>0) 
    350368      { 
    351       $a_msg_headers = $this->get_message_cache($cache_key, $start_msg, $start_msg+$this->page_size, $sort_field, $sort_order); 
     369      $a_msg_headers = $this->get_message_cache($cache_key, $start_msg, $start_msg+$this->page_size, $this->sort_field, $this->sort_order); 
    352370      $headers_sorted = TRUE; 
    353371      } 
     
    355373      { 
    356374      // retrieve headers from IMAP 
    357       if ($this->get_capability('sort') && ($msg_index = iil_C_Sort($this->conn, $mailbox, $sort_field))) 
     375      if ($this->get_capability('sort') && ($msg_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field))) 
    358376        { 
    359377//console("$mailbox: ".count($msg_index)); 
     
    362380        for ($i=$begin; $i < $end; $i++) 
    363381          { 
    364           if ($sort_order == 'DESC') 
     382          if ($this->sort_order == 'DESC') 
    365383            $msgs = $msg_index[$i].','.$msgs; 
    366384          else 
     
    378396 
    379397      // cache is dirty, sync it 
    380       if ($this->caching_enabled && $cache_status==-1) 
     398      if ($this->caching_enabled && $cache_status==-1 && !$recursive) 
    381399        { 
    382400        $this->sync_header_index($mailbox); 
    383         return $this->_list_headers($mailbox, $page, $sort_field, $sort_order); 
     401        return $this->_list_headers($mailbox, $page, $this->sort_field, $this->sort_order, TRUE); 
    384402        }       
    385403 
     
    391409      $a_header_index = iil_C_FetchHeaders($this->conn, $mailbox, $msgs); 
    392410      $a_msg_headers = array(); 
    393        
     411      $deleted_count = 0; 
    394412       
    395413      if (!empty($a_header_index)) 
     
    403421              $this->remove_message_cache($cache_key, $headers->id); 
    404422 
     423            $deleted_count++; 
    405424            continue; 
    406425            } 
     
    418437 
    419438 
     439      // fetch more headers of there were any deleted messages 
     440      // ... 
     441 
    420442      // kick child process to sync cache 
     443      // ... 
    421444       
    422445      } 
     
    430453    // if not already sorted 
    431454    if (!$headers_sorted) 
    432       $a_msg_headers = iil_SortHeaders($a_msg_headers, $sort_field, $sort_order); 
     455      $a_msg_headers = iil_SortHeaders($a_msg_headers, $this->sort_field, $this->sort_order); 
    433456 
    434457    return array_values($a_msg_headers); 
     
    437460 
    438461  // return sorted array of message UIDs 
    439   function message_index($mbox='', $sort_field='date', $sort_order='DESC') 
    440     { 
     462  function message_index($mbox='', $sort_field=NULL, $sort_order=NULL) 
     463    { 
     464    if ($sort_field!=NULL) 
     465      $this->sort_field = $sort_field; 
     466    if ($sort_order!=NULL) 
     467      $this->sort_order = strtoupper($sort_order); 
     468 
    441469    $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox; 
    442     $a_out = array(); 
    443  
    444     // get array of message headers 
    445     $a_headers = $this->_list_headers($mailbox, 'all', $sort_field, $sort_order); 
    446  
    447     if (is_array($a_headers)) 
    448       foreach ($a_headers as $header) 
    449         $a_out[] = $header->uid; 
    450  
    451     return $a_out; 
     470    $key = "$mbox:".$this->sort_field.":".$this->sort_order.".msgi"; 
     471 
     472    // have stored it in RAM 
     473    if (isset($this->cache[$key])) 
     474      return $this->cache[$key]; 
     475 
     476    // check local cache 
     477    $cache_key = $mailbox.'.msg'; 
     478    $cache_status = $this->check_cache_status($mailbox, $cache_key); 
     479 
     480    // cache is OK 
     481    if ($cache_status>0) 
     482      { 
     483      $a_index = get_message_cache_index($cache_key, FALSE, $this->sort_field); 
     484      return array_values($a_index); 
     485      } 
     486 
     487 
     488    // fetch complete message index 
     489    $msg_count = $this->_messagecount($mailbox); 
     490    if ($this->get_capability('sort') && ($a_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field))) 
     491      { 
     492      $a_uids = iil_C_FetchUIDs($this->conn, $mailbox); 
     493 
     494      if ($this->sort_order == 'DESC') 
     495        $a_index = array_reverse($a_index); 
     496 
     497      $i = 0; 
     498      $this->cache[$key] = array(); 
     499      foreach ($a_index as $index => $value) 
     500        $this->cache[$key][$i++] = $a_uids[$value]; 
     501      } 
     502    else 
     503      { 
     504      $a_index = iil_C_FetchHeaderIndex($this->conn, $mailbox, "1:$msg_count", $this->sort_field); 
     505      $a_uids = iil_C_FetchUIDs($this->conn, $mailbox); 
     506     
     507      if ($this->sort_order=="ASC") 
     508        asort($a_index); 
     509      else if ($this->sort_order=="DESC") 
     510        arsort($a_index); 
     511         
     512      $i = 0; 
     513      $this->cache[$key] = array(); 
     514      foreach ($a_index as $index => $value) 
     515        $this->cache[$key][$i++] = $a_uids[$index]; 
     516      } 
     517 
     518    return $this->cache[$key]; 
    452519    } 
    453520 
     
    507574    { 
    508575    $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox; 
     576    return $this->_search_index($mailbox, $criteria); 
     577    } 
     578     
     579     
     580  function _search_index($mailbox, $criteria='ALL') 
     581    { 
    509582    $a_messages = iil_C_Search($this->conn, $mailbox, $criteria); 
    510583    return $a_messages; 
     
    571644       
    572645    foreach ($uids as $uid) 
    573       $msg_ids[] = $this->_uid2id($uid); 
     646      $msg_ids[$uid] = $this->_uid2id($uid); 
    574647       
    575648    if ($flag=='UNSEEN') 
    576       $result = iil_C_Unseen($this->conn, $this->mailbox, join(',', $msg_ids)); 
     649      $result = iil_C_Unseen($this->conn, $this->mailbox, join(',', array_values($msg_ids))); 
    577650    else 
    578       $result = iil_C_Flag($this->conn, $this->mailbox, join(',', $msg_ids), $flag); 
     651      $result = iil_C_Flag($this->conn, $this->mailbox, join(',', array_values($msg_ids)), $flag); 
    579652 
    580653    // reload message headers if cached 
     
    582655    if ($this->caching_enabled) 
    583656      { 
    584       foreach ($msg_ids as $id) 
    585         { 
    586         if ($cached_headers = $this->get_cached_message($cache_key, $id)) 
     657      foreach ($msg_ids as $uid => $id) 
     658        { 
     659        if ($cached_headers = $this->get_cached_message($cache_key, $uid)) 
    587660          { 
    588661          $this->remove_message_cache($cache_key, $id); 
     
    597670 
    598671    // set nr of messages that were flaged 
    599     $count = sizeof($msg_ids); 
     672    $count = count($msg_ids); 
    600673 
    601674    // clear message count cache 
     
    11611234 
    11621235    
    1163   function get_message_cache_index($key, $force=FALSE) 
     1236  function get_message_cache_index($key, $force=FALSE, $sort_col='idx') 
    11641237    { 
    11651238    static $sa_message_index = array(); 
     
    11741247       WHERE  user_id=? 
    11751248       AND    cache_key=? 
    1176        ORDER BY idx ASC", 
     1249       ORDER BY ".$sort_col." ASC", 
    11771250      $_SESSION['user_id'], 
    11781251      $key); 
     
    11871260  function add_message_cache($key, $index, $headers) 
    11881261    { 
     1262    if (!is_object($headers) || empty($headers->uid)) 
     1263      return; 
     1264 
    11891265    $this->db->query( 
    11901266      "INSERT INTO ".get_table_name('messages')." 
     
    11991275      $this->decode_header($headers->to, TRUE), 
    12001276      $this->decode_header($headers->cc, TRUE), 
    1201       $headers->size, 
     1277      (int)$headers->size, 
    12021278      serialize($headers)); 
    12031279    } 
     
    12691345  function decode_header($input, $remove_quotes=FALSE) 
    12701346    { 
    1271     $str = $this->decode_mime_string($input); 
     1347    $str = $this->decode_mime_string((string)$input); 
    12721348    if ($str{0}=='"' && $remove_quotes) 
    12731349      { 
     
    15191595    // add incremental value to messagecount 
    15201596    $a_mailbox_cache[$mailbox][$mode] += $increment; 
     1597     
     1598    // there's something wrong, delete from cache 
     1599    if ($a_mailbox_cache[$mailbox][$mode] < 0) 
     1600      unset($a_mailbox_cache[$mailbox][$mode]); 
    15211601 
    15221602    // write back to cache 
  • trunk/roundcubemail/program/lib/imap.inc

    r62 r89  
    619619         
    620620        $fp = $conn->fp; 
    621         $command = 's SORT ('.$field.') US-ASCII ALL'."\r\n"; 
     621        $command = 's SORT ('.$field.') US-ASCII ALL UNDELETED'."\r\n"; 
    622622        $line = $data = ''; 
    623623         
  • trunk/roundcubemail/program/steps/mail/func.inc

    r88 r89  
    529529  if (isset($MESSAGE['index'])) 
    530530    { 
    531     $a_msg_index = $IMAP->message_index(); 
    532531    return rcube_label(array('name' => 'messagenrof', 
    533532                             'vars' => array('nr'  => $MESSAGE['index']+1, 
    534                                              'count' => sizeof($a_msg_index)))); 
    535     } 
    536    
     533                                             'count' => $IMAP->messagecount()))); 
     534    } 
     535 
    537536  $start_msg = ($IMAP->list_page-1) * $IMAP->page_size + 1; 
    538537  $max = $IMAP->messagecount(); 
Note: See TracChangeset for help on using the changeset viewer.