Changeset 601 in subversion


Ignore:
Timestamp:
Jun 5, 2007 9:17:03 PM (6 years ago)
Author:
till
Message:

+ CS

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/devel-vnext/program/include/rcube_imap.inc

    r597 r601  
    492492 
    493493 
    494   /** 
    495    * Public method for listing headers 
    496    * convert mailbox name with root dir first 
    497    * 
    498    * @param   string   Mailbox/folder name 
    499    * @param   number   Current page to list 
    500    * @param   string   Header field to sort by 
    501    * @param   string   Sort order [ASC|DESC] 
    502    * @return  array    Indexed array with message header objects 
    503    * @access  public 
    504    */ 
    505   function list_headers($mbox_name='', $page=NULL, $sort_field=NULL, $sort_order=NULL) 
    506     { 
    507     $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox; 
    508     return $this->_list_headers($mailbox, $page, $sort_field, $sort_order); 
    509     } 
    510  
    511  
    512   /** 
    513    * Private method for listing message headers 
     494    /** 
     495     * Public method for listing headers 
     496     * convert mailbox name with root dir first 
     497     * 
     498     * @param   string   Mailbox/folder name 
     499     * @param   number   Current page to list 
     500     * @param   string   Header field to sort by 
     501     * @param   string   Sort order [ASC|DESC] 
     502     * @return  array    Indexed array with message header objects 
     503     * @access  public 
     504     */ 
     505    function list_headers($mbox_name='', $page=NULL, $sort_field=NULL, $sort_order=NULL) 
     506    { 
     507        $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox; 
     508        return $this->_list_headers($mailbox, $page, $sort_field, $sort_order); 
     509    } 
     510 
     511 
     512    /** 
     513     * Private method for listing message headers 
     514     * 
     515     * @access  private 
     516     * @see     rcube_imap::list_headers 
     517     */ 
     518    function _list_headers($mailbox='', $page=NULL, $sort_field=NULL, $sort_order=NULL, $recursive=FALSE) 
     519    { 
     520        if (!strlen($mailbox)) { 
     521            return array(); 
     522        } 
     523        // use saved message set 
     524        if ($this->search_set && $mailbox == $this->mailbox) { 
     525            return $this->_list_header_set($mailbox, $this->search_set, $page, $sort_field, $sort_order); 
     526        } 
     527        if (is_null($sort_field) === FALSE) { 
     528            $this->sort_field = $sort_field; 
     529        } 
     530        if (is_null($sort_order) === FALSE) { 
     531            $this->sort_order = strtoupper($sort_order); 
     532        } 
     533 
     534        $max = $this->_messagecount($mailbox); 
     535        $start_msg = ($this->list_page-1) * $this->page_size; 
     536 
     537        list($begin, $end) = $this->_get_message_range($max, $page); 
     538 
     539        // mailbox is empty 
     540        if ($begin >= $end) { 
     541            return array(); 
     542        } 
     543 
     544        $headers_sorted = FALSE; 
     545        $cache_key = $mailbox.'.msg'; 
     546        $cache_status = $this->check_cache_status($mailbox, $cache_key); 
     547 
     548        // cache is OK, we can get all messages from local cache 
     549        if ($cache_status>0) { 
     550            $a_msg_headers = $this->get_message_cache($cache_key, $start_msg, $start_msg+$this->page_size, $this->sort_field, $this->sort_order); 
     551            $headers_sorted = TRUE; 
     552        } 
     553        // cache is dirty, sync it 
     554        elseif ($this->caching_enabled && $cache_status==-1 && !$recursive) { 
     555            $this->sync_header_index($mailbox); 
     556            return $this->_list_headers($mailbox, $page, $this->sort_field, $this->sort_order, TRUE); 
     557        } 
     558        else { 
     559            // retrieve headers from IMAP 
     560            if ($this->get_capability('sort') && ($msg_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field, $this->skip_deleted ? 'UNDELETED' : ''))) { 
     561                $msgs = $msg_index[$begin]; 
     562                for ($i=$begin+1; $i < $end; $i++) { 
     563                    $msgs = $msgs.','.$msg_index[$i]; 
     564                } 
     565            } 
     566            else { 
     567                $msgs = sprintf("%d:%d", $begin+1, $end); 
     568 
     569                $i = 0; 
     570                for ($msg_seqnum = $begin; $msg_seqnum <= $end; $msg_seqnum++) { 
     571                    $msg_index[$i++] = $msg_seqnum; 
     572                } 
     573            } 
     574 
     575            // use this class for message sorting 
     576            $sorter = new rcube_header_sorter(); 
     577            $sorter->set_sequence_numbers($msg_index); 
     578 
     579            // fetch reuested headers from server 
     580            $a_msg_headers = array(); 
     581            $deleted_count = $this->_fetch_headers($mailbox, $msgs, $a_msg_headers, $cache_key); 
     582 
     583            // delete cached messages with a higher index than $max+1 
     584            // Changed $max to $max+1 to fix this bug : #1484295 
     585            $this->clear_message_cache($cache_key, $max + 1); 
     586 
     587 
     588            // kick child process to sync cache 
     589            // ... 
     590 
     591        } 
     592 
     593 
     594        // return empty array if no messages found 
     595            if (!is_array($a_msg_headers) || empty($a_msg_headers)) { 
     596                    return array(); 
     597        } 
     598 
     599        // if not already sorted 
     600        if (!$headers_sorted) { 
     601            $sorter->sort_headers($a_msg_headers); 
     602 
     603            if ($this->sort_order == 'DESC') { 
     604                $a_msg_headers = array_reverse($a_msg_headers); 
     605            } 
     606        } 
     607        return array_values($a_msg_headers); 
     608    } 
     609 
     610 
     611 
     612    /** 
     613     * Public method for listing a specific set of headers 
     614     * convert mailbox name with root dir first 
     615     * 
     616     * @param   string   Mailbox/folder name 
     617     * @param   array    List of message ids to list 
     618     * @param   number   Current page to list 
     619     * @param   string   Header field to sort by 
     620     * @param   string   Sort order [ASC|DESC] 
     621     * @return  array    Indexed array with message header objects 
     622     * @access  public 
     623     */ 
     624    function list_header_set($mbox_name='', $msgs, $page=NULL, $sort_field=NULL, $sort_order=NULL) 
     625    { 
     626        $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox; 
     627        return $this->_list_header_set($mailbox, $msgs, $page, $sort_field, $sort_order); 
     628    } 
     629 
     630 
     631  /** 
     632   * Private method for listing a set of message headers 
    514633   * 
    515634   * @access  private 
    516    * @see     rcube_imap::list_headers 
    517    */ 
    518   function _list_headers($mailbox='', $page=NULL, $sort_field=NULL, $sort_order=NULL, $recursive=FALSE) 
    519     { 
    520     if (!strlen($mailbox)) 
     635   * @see     rcube_imap::list_header_set 
     636   */ 
     637  function _list_header_set($mailbox, $msgs, $page=NULL, $sort_field=NULL, $sort_order=NULL) 
     638    { 
     639    // also accept a comma-separated list of message ids 
     640    if (is_string($msgs)) 
     641      $msgs = split(',', $msgs); 
     642 
     643    if (!strlen($mailbox) || empty($msgs)) 
    521644      return array(); 
    522  
    523     // use saved message set 
    524     if ($this->search_set && $mailbox == $this->mailbox) 
    525       return $this->_list_header_set($mailbox, $this->search_set, $page, $sort_field, $sort_order); 
    526645 
    527646    if ($sort_field!=NULL) 
     
    530649      $this->sort_order = strtoupper($sort_order); 
    531650 
    532     $max = $this->_messagecount($mailbox); 
    533     $start_msg = ($this->list_page-1) * $this->page_size; 
    534  
    535     list($begin, $end) = $this->_get_message_range($max, $page); 
    536  
    537     // mailbox is empty 
    538     if ($begin >= $end) 
    539       return array(); 
    540  
    541     $headers_sorted = FALSE; 
    542     $cache_key = $mailbox.'.msg'; 
    543     $cache_status = $this->check_cache_status($mailbox, $cache_key); 
    544  
    545     // cache is OK, we can get all messages from local cache 
    546     if ($cache_status>0) 
    547       { 
    548       $a_msg_headers = $this->get_message_cache($cache_key, $start_msg, $start_msg+$this->page_size, $this->sort_field, $this->sort_order); 
    549       $headers_sorted = TRUE; 
    550       } 
    551     // cache is dirty, sync it 
    552     else if ($this->caching_enabled && $cache_status==-1 && !$recursive) 
    553       { 
    554       $this->sync_header_index($mailbox); 
    555       return $this->_list_headers($mailbox, $page, $this->sort_field, $this->sort_order, TRUE); 
    556       } 
    557     else 
    558       { 
    559       // retrieve headers from IMAP 
    560       if ($this->get_capability('sort') && ($msg_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field, $this->skip_deleted ? 'UNDELETED' : ''))) 
    561         { 
    562         $msgs = $msg_index[$begin]; 
    563         for ($i=$begin+1; $i < $end; $i++) 
    564           $msgs = $msgs.','.$msg_index[$i]; 
    565         } 
    566       else 
    567         { 
    568         $msgs = sprintf("%d:%d", $begin+1, $end); 
    569  
    570         $i = 0; 
    571         for ($msg_seqnum = $begin; $msg_seqnum <= $end; $msg_seqnum++) 
    572           $msg_index[$i++] = $msg_seqnum; 
    573         } 
    574  
    575       // use this class for message sorting 
    576       $sorter = new rcube_header_sorter(); 
    577       $sorter->set_sequence_numbers($msg_index); 
    578  
    579       // fetch reuested headers from server 
    580       $a_msg_headers = array(); 
    581       $deleted_count = $this->_fetch_headers($mailbox, $msgs, $a_msg_headers, $cache_key); 
    582  
    583       // delete cached messages with a higher index than $max+1 
    584       // Changed $max to $max+1 to fix this bug : #1484295 
    585       $this->clear_message_cache($cache_key, $max + 1); 
    586  
    587  
    588       // kick child process to sync cache 
    589       // ... 
    590  
    591       } 
    592  
    593  
    594     // return empty array if no messages found 
    595         if (!is_array($a_msg_headers) || empty($a_msg_headers)) 
    596                 return array(); 
    597  
    598  
    599     // if not already sorted 
    600     if (!$headers_sorted) 
    601       { 
    602       $sorter->sort_headers($a_msg_headers); 
    603  
    604       if ($this->sort_order == 'DESC') 
    605         $a_msg_headers = array_reverse($a_msg_headers); 
    606       } 
    607  
    608     return array_values($a_msg_headers); 
    609     } 
    610  
    611  
    612  
    613   /** 
    614    * Public method for listing a specific set of headers 
    615    * convert mailbox name with root dir first 
    616    * 
    617    * @param   string   Mailbox/folder name 
    618    * @param   array    List of message ids to list 
    619    * @param   number   Current page to list 
    620    * @param   string   Header field to sort by 
    621    * @param   string   Sort order [ASC|DESC] 
    622    * @return  array    Indexed array with message header objects 
    623    * @access  public 
    624    */ 
    625   function list_header_set($mbox_name='', $msgs, $page=NULL, $sort_field=NULL, $sort_order=NULL) 
    626     { 
    627     $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox; 
    628     return $this->_list_header_set($mailbox, $msgs, $page, $sort_field, $sort_order); 
    629     } 
    630  
    631  
    632   /** 
    633    * Private method for listing a set of message headers 
    634    * 
    635    * @access  private 
    636    * @see     rcube_imap::list_header_set 
    637    */ 
    638   function _list_header_set($mailbox, $msgs, $page=NULL, $sort_field=NULL, $sort_order=NULL) 
    639     { 
    640     // also accept a comma-separated list of message ids 
    641     if (is_string($msgs)) 
    642       $msgs = split(',', $msgs); 
    643  
    644     if (!strlen($mailbox) || empty($msgs)) 
    645       return array(); 
    646  
    647     if ($sort_field!=NULL) 
    648       $this->sort_field = $sort_field; 
    649     if ($sort_order!=NULL) 
    650       $this->sort_order = strtoupper($sort_order); 
    651  
    652651    $max = count($msgs); 
    653652    $start_msg = ($this->list_page-1) * $this->page_size; 
     
    669668 
    670669 
    671   /** 
    672    * Helper function to get first and last index of the requested set 
    673    * 
    674    * @param  number  message count 
    675    * @param  mixed   page number to show, or string 'all' 
    676    * @return array   array with two values: first index, last index 
    677    * @access private 
    678    */ 
    679   function _get_message_range($max, $page) 
    680     { 
    681     $start_msg = ($this->list_page-1) * $this->page_size; 
    682  
    683     if ($page=='all') 
    684       { 
    685       $begin = 0; 
    686       $end = $max; 
    687       } 
    688     else if ($this->sort_order=='DESC') 
    689       { 
    690       $begin = $max - $this->page_size - $start_msg; 
    691       $end =   $max - $start_msg; 
    692       } 
    693     else 
    694       { 
    695       $begin = $start_msg; 
    696       $end   = $start_msg + $this->page_size; 
    697       } 
    698  
    699     if ($begin < 0) $begin = 0; 
    700     if ($end < 0) $end = $max; 
    701     if ($end > $max) $end = $max; 
    702  
    703     return array($begin, $end); 
     670    /** 
     671     * Helper function to get first and last index of the requested set 
     672     * 
     673     * @param  number  message count 
     674     * @param  mixed   page number to show, or string 'all' 
     675     * @return array   array with two values: first index, last index 
     676     * @access private 
     677     */ 
     678    function _get_message_range($max, $page) 
     679    { 
     680        $start_msg = ($this->list_page-1) * $this->page_size; 
     681 
     682        if ($page=='all') { 
     683            $begin = 0; 
     684            $end   = $max; 
     685        } 
     686        elseif ($this->sort_order=='DESC') { 
     687            $begin = $max - $this->page_size - $start_msg; 
     688            $end   = $max - $start_msg; 
     689        } 
     690        else { 
     691            $begin = $start_msg; 
     692            $end   = $start_msg + $this->page_size; 
     693        } 
     694 
     695        if ($begin < 0) { 
     696            $begin = 0; 
     697        } 
     698        if ($end < 0) { 
     699            $end = $max; 
     700        } 
     701        if ($end > $max) { 
     702            $end = $max; 
     703        } 
     704        return array($begin, $end); 
    704705    } 
    705706 
Note: See TracChangeset for help on using the changeset viewer.