/** * protected method for getting nr of messages * * @param string $folder Folder name * @param string $mode Mode for count [ALL|THREADS|UNSEEN|RECENT] * @param boolean $force Force reading from server and update cache * @param boolean $status Enables storing folder status info (max UID/count), * required for folder_status() * * @return int Number of messages * @see rcube_imap::count() */ protected function countmessages($folder, $mode = 'ALL', $force = false, $status = true) { $mode = strtoupper($mode); // count search set, assume search set is always up-to-date (don't check $force flag) if ($this->search_string && $folder == $this->folder && ($mode == 'ALL' || $mode == 'THREADS')) { if ($mode == 'ALL') { return $this->search_set->count_messages(); } else { return $this->search_set->count(); } } $a_folder_cache = $this->get_cache('messagecount'); // return cached value if (!$force && is_array($a_folder_cache[$folder]) && isset($a_folder_cache[$folder][$mode])) { return $a_folder_cache[$folder][$mode]; } if (!is_array($a_folder_cache[$folder])) { $a_folder_cache[$folder] = array(); } if ($mode == 'THREADS') { $res = $this->fetch_threads($folder, $force); $count = $res->count(); if ($status) { $msg_count = $res->count_messages(); $this->set_folder_stats($folder, 'cnt', $msg_count); $this->set_folder_stats($folder, 'maxuid', $msg_count ? $this->id2uid($msg_count, $folder) : 0); } } else { if (!$this->check_connection()) { return 0; } else { if ($mode == 'RECENT') { $count = $this->conn->countRecent($folder); } else { if (!empty($this->options['skip_deleted'])) { $search_str = "ALL UNDELETED"; $keys = array('COUNT'); if ($mode == 'UNSEEN') { $search_str .= " UNSEEN"; } else { if ($this->messages_caching) { $keys[] = 'ALL'; } if ($status) { $keys[] = 'MAX'; } } // @TODO: if $force==false && $mode == 'ALL' we could try to use cache index here // get message count using (E)SEARCH // not very performant but more precise (using UNDELETED) $index = $this->conn->search($folder, $search_str, true, $keys); $count = $index->count(); if ($mode == 'ALL') { // Cache index data, will be used in index_direct() $this->icache['undeleted_idx'] = $index; if ($status) { $this->set_folder_stats($folder, 'cnt', $count); $this->set_folder_stats($folder, 'maxuid', $index->max()); } } } else { if ($mode == 'UNSEEN') { $count = $this->conn->countUnseen($folder); } else { $count = $this->conn->countMessages($folder); if ($status) { $this->set_folder_stats($folder, 'cnt', $count); $this->set_folder_stats($folder, 'maxuid', $count ? $this->id2uid($count, $folder) : 0); } } } } } } $a_folder_cache[$folder][$mode] = (int) $count; // write back to cache $this->update_cache('messagecount', $a_folder_cache); return (int) $count; }
/** * Protected method for getting number of messages * * @param string $folder Folder name * @param string $mode Mode for count [ALL|THREADS|UNSEEN|RECENT|EXISTS] * @param boolean $force Force reading from server and update cache * @param boolean $status Enables storing folder status info (max UID/count), * required for folder_status() * @param boolean $no_search Ignore current search result * * @return int Number of messages * @see rcube_imap::count() */ protected function countmessages($folder, $mode = 'ALL', $force = false, $status = true, $no_search = false) { $mode = strtoupper($mode); // Count search set, assume search set is always up-to-date (don't check $force flag) // @TODO: this could be handled in more reliable way, e.g. a separate method // maybe in rcube_imap_search if (!$no_search && $this->search_string && $folder == $this->folder) { if ($mode == 'ALL') { return $this->search_set->count_messages(); } else { if ($mode == 'THREADS') { return $this->search_set->count(); } } } // EXISTS is a special alias for ALL, it allows to get the number // of all messages in a folder also when search is active and with // any skip_deleted setting $a_folder_cache = $this->get_cache('messagecount'); // return cached value if (!$force && is_array($a_folder_cache[$folder]) && isset($a_folder_cache[$folder][$mode])) { return $a_folder_cache[$folder][$mode]; } if (!is_array($a_folder_cache[$folder])) { $a_folder_cache[$folder] = array(); } if ($mode == 'THREADS') { $res = $this->threads($folder); $count = $res->count(); if ($status) { $msg_count = $res->count_messages(); $this->set_folder_stats($folder, 'cnt', $msg_count); $this->set_folder_stats($folder, 'maxuid', $msg_count ? $this->id2uid($msg_count, $folder) : 0); } } else { if (!$this->check_connection()) { return 0; } else { if ($mode == 'RECENT') { $count = $this->conn->countRecent($folder); } else { if ($mode != 'EXISTS' && !empty($this->options['skip_deleted'])) { $search_str = "ALL UNDELETED"; $keys = array('COUNT'); if ($mode == 'UNSEEN') { $search_str .= " UNSEEN"; } else { if ($this->messages_caching) { $keys[] = 'ALL'; } if ($status) { $keys[] = 'MAX'; } } // @TODO: if $mode == 'ALL' we could try to use cache index here // get message count using (E)SEARCH // not very performant but more precise (using UNDELETED) $index = $this->conn->search($folder, $search_str, true, $keys); $count = $index->count(); if ($mode == 'ALL') { // Cache index data, will be used in index_direct() $this->icache['undeleted_idx'] = $index; if ($status) { $this->set_folder_stats($folder, 'cnt', $count); $this->set_folder_stats($folder, 'maxuid', $index->max()); } } } else { if ($mode == 'UNSEEN') { $count = $this->conn->countUnseen($folder); } else { $count = $this->conn->countMessages($folder); if ($status && $mode == 'ALL') { $this->set_folder_stats($folder, 'cnt', $count); $this->set_folder_stats($folder, 'maxuid', $count ? $this->id2uid($count, $folder) : 0); } } } } } } $a_folder_cache[$folder][$mode] = (int) $count; // write back to cache $this->update_cache('messagecount', $a_folder_cache); return (int) $count; }
/** * Private method for getting nr of messages * * @param string $mailbox Mailbox name * @param string $mode Mode for count [ALL|THREADS|UNSEEN|RECENT] * @param boolean $force Force reading from server and update cache * @param boolean $status Enables storing folder status info (max UID/count), * required for mailbox_status() * @return int Number of messages * @access private * @see rcube_imap::messagecount() */ private function _messagecount($mailbox = '', $mode = 'ALL', $force = false, $status = true) { $mode = strtoupper($mode); if (!strlen($mailbox)) { $mailbox = $this->mailbox; } // count search set if ($this->search_string && $mailbox == $this->mailbox && ($mode == 'ALL' || $mode == 'THREADS') && !$force) { if ($this->search_threads) { return $mode == 'ALL' ? count((array) $this->search_set['depth']) : count((array) $this->search_set['tree']); } else { return count((array) $this->search_set); } } $a_mailbox_cache = $this->get_cache('messagecount'); // return cached value if (!$force && is_array($a_mailbox_cache[$mailbox]) && isset($a_mailbox_cache[$mailbox][$mode])) { return $a_mailbox_cache[$mailbox][$mode]; } if (!is_array($a_mailbox_cache[$mailbox])) { $a_mailbox_cache[$mailbox] = array(); } if ($mode == 'THREADS') { $res = $this->_threadcount($mailbox, $msg_count); $count = $res['count']; if ($status) { $this->set_folder_stats($mailbox, 'cnt', $res['msgcount']); $this->set_folder_stats($mailbox, 'maxuid', $res['maxuid'] ? $this->_id2uid($res['maxuid'], $mailbox) : 0); } } else { if ($mode == 'RECENT') { $count = $this->conn->countRecent($mailbox); } else { if ($this->skip_deleted) { $search_str = "ALL UNDELETED"; $keys = array('COUNT'); $need_uid = false; if ($mode == 'UNSEEN') { $search_str .= " UNSEEN"; } else { if ($this->caching_enabled) { $keys[] = 'ALL'; } if ($status) { $keys[] = 'MAX'; $need_uid = true; } } // get message count using (E)SEARCH // not very performant but more precise (using UNDELETED) $index = $this->conn->search($mailbox, $search_str, $need_uid, $keys); $count = is_array($index) ? $index['COUNT'] : 0; if ($mode == 'ALL') { if ($need_uid && $this->caching_enabled) { // Save messages index for check_cache_status() $this->icache['all_undeleted_idx'] = $index['ALL']; } if ($status) { $this->set_folder_stats($mailbox, 'cnt', $count); $this->set_folder_stats($mailbox, 'maxuid', is_array($index) ? $index['MAX'] : 0); } } } else { if ($mode == 'UNSEEN') { $count = $this->conn->countUnseen($mailbox); } else { $count = $this->conn->countMessages($mailbox); if ($status) { $this->set_folder_stats($mailbox, 'cnt', $count); $this->set_folder_stats($mailbox, 'maxuid', $count ? $this->_id2uid($count, $mailbox) : 0); } } } } } $a_mailbox_cache[$mailbox][$mode] = (int) $count; // write back to cache $this->update_cache('messagecount', $a_mailbox_cache); return (int) $count; }