diff options
Diffstat (limited to 'program/lib/Roundcube/rcube_imap_cache.php')
-rw-r--r-- | program/lib/Roundcube/rcube_imap_cache.php | 340 |
1 files changed, 99 insertions, 241 deletions
diff --git a/program/lib/Roundcube/rcube_imap_cache.php b/program/lib/Roundcube/rcube_imap_cache.php index a8166545e..5170e9e21 100644 --- a/program/lib/Roundcube/rcube_imap_cache.php +++ b/program/lib/Roundcube/rcube_imap_cache.php @@ -27,9 +27,6 @@ */ class rcube_imap_cache { - const MODE_INDEX = 1; - const MODE_MESSAGE = 2; - /** * Instance of rcube_imap * @@ -52,20 +49,6 @@ class rcube_imap_cache private $userid; /** - * Expiration time in seconds - * - * @var int - */ - private $ttl; - - /** - * Maximum cached message size - * - * @var int - */ - private $threshold; - - /** * Internal (in-memory) cache * * @var array @@ -73,7 +56,6 @@ class rcube_imap_cache private $icache = array(); private $skip_deleted = false; - private $mode; /** * List of known flags. Thanks to this we can handle flag changes @@ -99,32 +81,15 @@ class rcube_imap_cache ); - /** * Object constructor. - * - * @param rcube_db $db DB handler - * @param rcube_imap $imap IMAP handler - * @param int $userid User identifier - * @param bool $skip_deleted skip_deleted flag - * @param string $ttl Expiration time of memcache/apc items - * @param int $threshold Maximum cached message size */ - function __construct($db, $imap, $userid, $skip_deleted, $ttl=0, $threshold=0) + function __construct($db, $imap, $userid, $skip_deleted) { - // convert ttl string to seconds - $ttl = get_offset_sec($ttl); - if ($ttl > 2592000) $ttl = 2592000; - $this->db = $db; $this->imap = $imap; $this->userid = $userid; $this->skip_deleted = $skip_deleted; - $this->ttl = $ttl; - $this->threshold = $threshold; - - // cache all possible information by default - $this->mode = self::MODE_INDEX | self::MODE_MESSAGE; } @@ -139,17 +104,6 @@ class rcube_imap_cache /** - * Set cache mode - * - * @param int $mode Cache mode - */ - public function set_mode($mode) - { - $this->mode = $mode; - } - - - /** * Return (sorted) messages index (UIDs). * If index doesn't exist or is invalid, will be updated. * @@ -261,7 +215,9 @@ class rcube_imap_cache * Return messages thread. * If threaded index doesn't exist or is invalid, will be updated. * - * @param string $mailbox Folder name + * @param string $mailbox Folder name + * @param string $sort_field Sorting column + * @param string $sort_order Sorting order (ASC|DESC) * * @return array Messages threaded index */ @@ -300,11 +256,19 @@ class rcube_imap_cache if ($index === null) { // Get mailbox data (UIDVALIDITY, counters, etc.) for status check $mbox_data = $this->imap->folder_data($mailbox); - // Get THREADS result - $index['object'] = $this->get_thread_data($mailbox, $mbox_data); + + if ($mbox_data['EXISTS']) { + // get all threads (default sort order) + $threads = $this->imap->fetch_threads($mailbox, true); + } + else { + $threads = new rcube_result_thread($mailbox, '* THREAD'); + } + + $index['object'] = $threads; // insert/update - $this->add_thread_row($mailbox, $index['object'], $mbox_data, $exists); + $this->add_thread_row($mailbox, $threads, $mbox_data, $exists); } $this->icache[$mailbox]['thread'] = $index; @@ -327,46 +291,38 @@ class rcube_imap_cache return array(); } - $result = array(); - - if ($this->mode & self::MODE_MESSAGE) { - // Fetch messages from cache - $sql_result = $this->db->query( - "SELECT uid, data, flags" - ." FROM ".$this->db->table_name('cache_messages') - ." WHERE user_id = ?" - ." AND mailbox = ?" - ." AND uid IN (".$this->db->array2list($msgs, 'integer').")", - $this->userid, $mailbox); + // Fetch messages from cache + $sql_result = $this->db->query( + "SELECT uid, data, flags" + ." FROM ".$this->db->table_name('cache_messages') + ." WHERE user_id = ?" + ." AND mailbox = ?" + ." AND uid IN (".$this->db->array2list($msgs, 'integer').")", + $this->userid, $mailbox); - $msgs = array_flip($msgs); + $msgs = array_flip($msgs); + $result = array(); - while ($sql_arr = $this->db->fetch_assoc($sql_result)) { - $uid = intval($sql_arr['uid']); - $result[$uid] = $this->build_message($sql_arr); + while ($sql_arr = $this->db->fetch_assoc($sql_result)) { + $uid = intval($sql_arr['uid']); + $result[$uid] = $this->build_message($sql_arr); - if (!empty($result[$uid])) { - // save memory, we don't need message body here (?) - $result[$uid]->body = null; + if (!empty($result[$uid])) { + // save memory, we don't need message body here (?) + $result[$uid]->body = null; - unset($msgs[$uid]); - } + unset($msgs[$uid]); } - - $msgs = array_flip($msgs); } // Fetch not found messages from IMAP server if (!empty($msgs)) { - $messages = $this->imap->fetch_headers($mailbox, $msgs, false, true); + $messages = $this->imap->fetch_headers($mailbox, array_keys($msgs), false, true); // Insert to DB and add to result list if (!empty($messages)) { foreach ($messages as $msg) { - if ($this->mode & self::MODE_MESSAGE) { - $this->add_message($mailbox, $msg, !array_key_exists($msg->uid, $result)); - } - + $this->add_message($mailbox, $msg, !array_key_exists($msg->uid, $result)); $result[$msg->uid] = $msg; } } @@ -397,19 +353,17 @@ class rcube_imap_cache return $this->icache['__message']['object']; } - if ($this->mode & self::MODE_MESSAGE) { - $sql_result = $this->db->query( - "SELECT flags, data" - ." FROM ".$this->db->table_name('cache_messages') - ." WHERE user_id = ?" - ." AND mailbox = ?" - ." AND uid = ?", - $this->userid, $mailbox, (int)$uid); + $sql_result = $this->db->query( + "SELECT flags, data" + ." FROM ".$this->db->table_name('cache_messages') + ." WHERE user_id = ?" + ." AND mailbox = ?" + ." AND uid = ?", + $this->userid, $mailbox, (int)$uid); - if ($sql_arr = $this->db->fetch_assoc($sql_result)) { - $message = $this->build_message($sql_arr); - $found = true; - } + if ($sql_arr = $this->db->fetch_assoc($sql_result)) { + $message = $this->build_message($sql_arr); + $found = true; } // Get the message from IMAP server @@ -418,10 +372,6 @@ class rcube_imap_cache // cache will be updated in close(), see below } - if (!($this->mode & self::MODE_MESSAGE)) { - return $message; - } - // Save the message in internal cache, will be written to DB in close() // Common scenario: user opens unseen message // - get message (SELECT) @@ -457,10 +407,6 @@ class rcube_imap_cache return; } - if (!($this->mode & self::MODE_MESSAGE)) { - return; - } - $flags = 0; $msg = clone $message; @@ -480,40 +426,23 @@ class rcube_imap_cache if (!$force) { $res = $this->db->query( "UPDATE ".$this->db->table_name('cache_messages') - ." SET flags = ?, data = ?, expires = " . ($this->ttl ? $this->db->now($this->ttl) : 'NULL') + ." SET flags = ?, data = ?, changed = ".$this->db->now() ." WHERE user_id = ?" ." AND mailbox = ?" ." AND uid = ?", $flags, $msg, $this->userid, $mailbox, (int) $message->uid); - if ($this->db->affected_rows($res)) { + if ($this->db->affected_rows()) { return; } } - $this->db->set_option('ignore_key_errors', true); - // insert new record - $res = $this->db->query( + $this->db->query( "INSERT INTO ".$this->db->table_name('cache_messages') - ." (user_id, mailbox, uid, flags, expires, data)" - ." VALUES (?, ?, ?, ?, ". ($this->ttl ? $this->db->now($this->ttl) : 'NULL') . ", ?)", + ." (user_id, mailbox, uid, flags, changed, data)" + ." VALUES (?, ?, ?, ?, ".$this->db->now().", ?)", $this->userid, $mailbox, (int) $message->uid, $flags, $msg); - - // race-condition, insert failed so try update (#1489146) - // thanks to ignore_key_errors "duplicate row" errors will be ignored - if ($force && !$res && !$this->db->is_error($res)) { - $this->db->query( - "UPDATE ".$this->db->table_name('cache_messages') - ." SET expires = " . ($this->ttl ? $this->db->now($this->ttl) : 'NULL') - .", flags = ?, data = ?" - ." WHERE user_id = ?" - ." AND mailbox = ?" - ." AND uid = ?", - $flags, $msg, $this->userid, $mailbox, (int) $message->uid); - } - - $this->db->set_option('ignore_key_errors', false); } @@ -532,10 +461,6 @@ class rcube_imap_cache return; } - if (!($this->mode & self::MODE_MESSAGE)) { - return; - } - $flag = strtoupper($flag); $idx = (int) array_search($flag, $this->flags); $uids = (array) $uids; @@ -558,7 +483,7 @@ class rcube_imap_cache $this->db->query( "UPDATE ".$this->db->table_name('cache_messages') - ." SET expires = ". ($this->ttl ? $this->db->now($this->ttl) : 'NULL') + ." SET changed = ".$this->db->now() .", flags = flags ".($enabled ? "+ $idx" : "- $idx") ." WHERE user_id = ?" ." AND mailbox = ?" @@ -576,10 +501,6 @@ class rcube_imap_cache */ function remove_message($mailbox = null, $uids = null) { - if (!($this->mode & self::MODE_MESSAGE)) { - return; - } - if (!strlen($mailbox)) { $this->db->query( "DELETE FROM ".$this->db->table_name('cache_messages') @@ -685,21 +606,23 @@ class rcube_imap_cache /** - * Delete expired cache entries + * Delete cache entries older than TTL + * + * @param string $ttl Lifetime of message cache entries */ - static function gc() + function expunge($ttl) { - $rcube = rcube::get_instance(); - $db = $rcube->get_dbh(); + // get expiration timestamp + $ts = get_offset_time($ttl, -1); - $db->query("DELETE FROM ".$db->table_name('cache_messages') - ." WHERE expires < " . $db->now()); + $this->db->query("DELETE FROM ".$this->db->table_name('cache_messages') + ." WHERE changed < " . $this->db->fromunixtime($ts)); - $db->query("DELETE FROM ".$db->table_name('cache_index') - ." WHERE expires < " . $db->now()); + $this->db->query("DELETE FROM ".$this->db->table_name('cache_index') + ." WHERE changed < " . $this->db->fromunixtime($ts)); - $db->query("DELETE FROM ".$db->table_name('cache_thread') - ." WHERE expires < " . $db->now()); + $this->db->query("DELETE FROM ".$this->db->table_name('cache_thread') + ." WHERE changed < " . $this->db->fromunixtime($ts)); } @@ -791,38 +714,20 @@ class rcube_imap_cache $data = implode('@', $data); if ($exists) { - $res = $this->db->query( + $sql_result = $this->db->query( "UPDATE ".$this->db->table_name('cache_index') - ." SET data = ?, valid = 1, expires = " . ($this->ttl ? $this->db->now($this->ttl) : 'NULL') + ." SET data = ?, valid = 1, changed = ".$this->db->now() ." WHERE user_id = ?" ." AND mailbox = ?", $data, $this->userid, $mailbox); - - if ($this->db->affected_rows($res)) { - return; - } } - - $this->db->set_option('ignore_key_errors', true); - - $res = $this->db->query( - "INSERT INTO ".$this->db->table_name('cache_index') - ." (user_id, mailbox, valid, expires, data)" - ." VALUES (?, ?, 1, ". ($this->ttl ? $this->db->now($this->ttl) : 'NULL') .", ?)", - $this->userid, $mailbox, $data); - - // race-condition, insert failed so try update (#1489146) - // thanks to ignore_key_errors "duplicate row" errors will be ignored - if (!$exists && !$res && !$this->db->is_error($res)) { - $res = $this->db->query( - "UPDATE ".$this->db->table_name('cache_index') - ." SET data = ?, valid = 1, expires = " . ($this->ttl ? $this->db->now($this->ttl) : 'NULL') - ." WHERE user_id = ?" - ." AND mailbox = ?", - $data, $this->userid, $mailbox); + else { + $sql_result = $this->db->query( + "INSERT INTO ".$this->db->table_name('cache_index') + ." (user_id, mailbox, data, valid, changed)" + ." VALUES (?, ?, ?, 1, ".$this->db->now().")", + $this->userid, $mailbox, $data); } - - $this->db->set_option('ignore_key_errors', false); } @@ -839,41 +744,21 @@ class rcube_imap_cache ); $data = implode('@', $data); - $expires = ($this->ttl ? $this->db->now($this->ttl) : 'NULL'); - if ($exists) { - $res = $this->db->query( + $sql_result = $this->db->query( "UPDATE ".$this->db->table_name('cache_thread') - ." SET data = ?, expires = $expires" + ." SET data = ?, changed = ".$this->db->now() ." WHERE user_id = ?" ." AND mailbox = ?", $data, $this->userid, $mailbox); - - if ($this->db->affected_rows($res)) { - return; - } } - - $this->db->set_option('ignore_key_errors', true); - - $res = $this->db->query( - "INSERT INTO ".$this->db->table_name('cache_thread') - ." (user_id, mailbox, expires, data)" - ." VALUES (?, ?, $expires, ?)", - $this->userid, $mailbox, $data); - - // race-condition, insert failed so try update (#1489146) - // thanks to ignore_key_errors "duplicate row" errors will be ignored - if (!$exists && !$res && !$this->db->is_error($res)) { - $this->db->query( - "UPDATE ".$this->db->table_name('cache_thread') - ." SET expires = $expires, data = ?" - ." WHERE user_id = ?" - ." AND mailbox = ?", - $data, $this->userid, $mailbox); + else { + $sql_result = $this->db->query( + "INSERT INTO ".$this->db->table_name('cache_thread') + ." (user_id, mailbox, data, changed)" + ." VALUES (?, ?, ?, ".$this->db->now().")", + $this->userid, $mailbox, $data); } - - $this->db->set_option('ignore_key_errors', false); } @@ -1081,17 +966,15 @@ class rcube_imap_cache $removed = array(); // Get known UIDs - if ($this->mode & self::MODE_MESSAGE) { - $sql_result = $this->db->query( - "SELECT uid" - ." FROM ".$this->db->table_name('cache_messages') - ." WHERE user_id = ?" - ." AND mailbox = ?", - $this->userid, $mailbox); + $sql_result = $this->db->query( + "SELECT uid" + ." FROM ".$this->db->table_name('cache_messages') + ." WHERE user_id = ?" + ." AND mailbox = ?", + $this->userid, $mailbox); - while ($sql_arr = $this->db->fetch_assoc($sql_result)) { - $uids[] = $sql_arr['uid']; - } + while ($sql_arr = $this->db->fetch_assoc($sql_result)) { + $uids[] = $sql_arr['uid']; } // Synchronize messages data @@ -1102,7 +985,7 @@ class rcube_imap_cache $uids, true, array('FLAGS'), $index['modseq'], $qresync); if (!empty($result)) { - foreach ($result as $msg) { + foreach ($result as $id => $msg) { $uid = $msg->uid; // Remove deleted message if ($this->skip_deleted && !empty($msg->flags['DELETED'])) { @@ -1123,7 +1006,7 @@ class rcube_imap_cache $this->db->query( "UPDATE ".$this->db->table_name('cache_messages') - ." SET flags = ?, expires = " . ($this->ttl ? $this->db->now($this->ttl) : 'NULL') + ." SET flags = ?, changed = ".$this->db->now() ." WHERE user_id = ?" ." AND mailbox = ?" ." AND uid = ?" @@ -1151,18 +1034,17 @@ class rcube_imap_cache } } + // Invalidate thread index (?) + if (!$index['valid']) { + $this->remove_thread($mailbox); + } + $sort_field = $index['sort_field']; $sort_order = $index['object']->get_parameters('ORDER'); $exists = true; // Validate index if (!$this->validate($mailbox, $index, $exists)) { - // Invalidate (remove) thread index - // if $exists=false it was already removed in validate() - if ($exists) { - $this->remove_thread($mailbox); - } - // Update index $data = $this->get_index_data($mailbox, $sort_field, $sort_order, $mbox_data); } @@ -1229,16 +1111,11 @@ class rcube_imap_cache * * @param rcube_message_header|rcube_message_part */ - private function message_object_prepare(&$msg, &$size = 0) + private function message_object_prepare(&$msg) { - // Remove body too big - if ($msg->body && ($length = strlen($msg->body))) { - $size += $length; - - if ($size > $this->threshold * 1024) { - $size -= $length; - unset($msg->body); - } + // Remove body too big (>25kB) + if ($msg->body && strlen($msg->body) > 25 * 1024) { + unset($msg->body); } // Fix mimetype which might be broken by some code when message is displayed @@ -1252,13 +1129,13 @@ class rcube_imap_cache if (is_array($msg->structure->parts)) { foreach ($msg->structure->parts as $part) { - $this->message_object_prepare($part, $size); + $this->message_object_prepare($part); } } if (is_array($msg->parts)) { foreach ($msg->parts as $part) { - $this->message_object_prepare($part, $size); + $this->message_object_prepare($part); } } } @@ -1283,25 +1160,6 @@ class rcube_imap_cache return $index; } - - - /** - * Fetches thread data from IMAP server - */ - private function get_thread_data($mailbox, $mbox_data = array()) - { - if (empty($mbox_data)) { - $mbox_data = $this->imap->folder_data($mailbox); - } - - if ($mbox_data['EXISTS']) { - // get all threads (default sort order) - return $this->imap->threads_direct($mailbox); - } - - return new rcube_result_thread($mailbox, '* THREAD'); - } - } // for backward compat. |