cache subscribedFolders Call to be used in getfolderStatus; control param if to fetch subscribed Info on getFolderStatus at all; own getMailboxCounters wrapper, instead of getFolderStatus, if one needs the counters only

This commit is contained in:
Klaus Leithoff 2014-07-31 10:22:47 +00:00
parent d2ba902fd1
commit 5acbcad35d
3 changed files with 72 additions and 21 deletions

View File

@ -458,6 +458,29 @@ class emailadmin_imap extends Horde_Imap_Client_Socket implements defaultimap
return $suF; return $suF;
} }
/**
* getMailboxCounters
*
* @param array/string $mailbox
* @return array with counters
*/
function getMailboxCounters($mailbox)
{
try
{
$status = $this->status($mailbox);
foreach ($status as $key => $v)
{
$_status[strtoupper($key)]=$v;
}
return $_status;
}
catch (Exception $e)
{
return false;
}
}
/** /**
* getStatus * getStatus
* *

View File

@ -1106,10 +1106,10 @@ class emailadmin_imapbase
* @param _folderName string the foldername * @param _folderName string the foldername
* @param ignoreStatusCache bool ignore the cache used for counters * @param ignoreStatusCache bool ignore the cache used for counters
* @param basicInfoOnly bool retrieve only names and stuff returned by getMailboxes * @param basicInfoOnly bool retrieve only names and stuff returned by getMailboxes
* * @param fetchSubscribedInfo bool fetch Subscribed Info on folder
* @return array * @return array
*/ */
function getFolderStatus($_folderName,$ignoreStatusCache=false,$basicInfoOnly=false) function getFolderStatus($_folderName,$ignoreStatusCache=false,$basicInfoOnly=false,$fetchSubscribedInfo=true)
{ {
if (self::$debug) error_log(__METHOD__.' ('.__LINE__.') '." called with:$_folderName,$ignoreStatusCache,$basicInfoOnly"); if (self::$debug) error_log(__METHOD__.' ('.__LINE__.') '." called with:$_folderName,$ignoreStatusCache,$basicInfoOnly");
if (!is_string($_folderName) || empty($_folderName)) // something is wrong. Do not proceed if (!is_string($_folderName) || empty($_folderName)) // something is wrong. Do not proceed
@ -1196,8 +1196,13 @@ class emailadmin_imapbase
return $retValue; return $retValue;
} }
// fetch all in one go for one request, instead of querying them one by one // fetch all in one go for one request, instead of querying them one by one
// cache it for a minute 60*60*1
// this should reduce communication to the imap server // this should reduce communication to the imap server
static $subscribedFolders; static $subscribedFolders;
if ($fetchSubscribedInfo && is_null($subscribedFolders)||empty($subscribedFolders[$this->profileID]))
{
$subscribedFolders = egw_cache::getCache(egw_cache::INSTANCE,'email','subscribedFolders'.trim($GLOBALS['egw_info']['user']['account_id']),null,array(),$expiration=60*60*1);
}
static $nameSpace; static $nameSpace;
static $prefix; static $prefix;
if (is_null($nameSpace) || empty($nameSpace[$this->profileID])) $nameSpace[$this->profileID] = $this->_getNameSpaces(); if (is_null($nameSpace) || empty($nameSpace[$this->profileID])) $nameSpace[$this->profileID] = $this->_getNameSpaces();
@ -1212,16 +1217,20 @@ class emailadmin_imapbase
} }
if (is_null($prefix) || empty($prefix[$this->profileID]) || empty($prefix[$this->profileID][$_folderName])) $prefix[$this->profileID][$_folderName] = $this->getFolderPrefixFromNamespace($nameSpace[$this->profileID], $_folderName); if (is_null($prefix) || empty($prefix[$this->profileID]) || empty($prefix[$this->profileID][$_folderName])) $prefix[$this->profileID][$_folderName] = $this->getFolderPrefixFromNamespace($nameSpace[$this->profileID], $_folderName);
//$subscribedFolders[$this->profileID] = $this->icServer->listSubscribedMailboxes('', $_folderName); if ($fetchSubscribedInfo && is_null($subscribedFolders) || empty($subscribedFolders[$this->profileID]))
if (is_null($subscribedFolders) || empty($subscribedFolders[$this->profileID])) $subscribedFolders[$this->profileID] = $this->icServer->listSubscribedMailboxes(); {
$subscribedFolders[$this->profileID] = $this->icServer->listSubscribedMailboxes();
egw_cache::setCache(egw_cache::INSTANCE,'email','subscribedFolders'.trim($GLOBALS['egw_info']['user']['account_id']),$subscribedFolders,$expiration=60*60*1);
}
if(is_array($subscribedFolders[$this->profileID]) && in_array($_folderName,$subscribedFolders[$this->profileID])) { if($fetchSubscribedInfo && is_array($subscribedFolders[$this->profileID]) && in_array($_folderName,$subscribedFolders[$this->profileID])) {
$retValue['subscribed'] = true; $retValue['subscribed'] = true;
} }
try try
{ {
$folderStatus = $this->_getStatus($_folderName,$ignoreStatusCache); //$folderStatus = $this->_getStatus($_folderName,$ignoreStatusCache);
$folderStatus = $this->getMailBoxCounters($_folderName,false);
$retValue['messages'] = $folderStatus['MESSAGES']; $retValue['messages'] = $folderStatus['MESSAGES'];
$retValue['recent'] = $folderStatus['RECENT']; $retValue['recent'] = $folderStatus['RECENT'];
$retValue['uidnext'] = $folderStatus['UIDNEXT']; $retValue['uidnext'] = $folderStatus['UIDNEXT'];
@ -2180,6 +2189,14 @@ class emailadmin_imapbase
return true; return true;
} }
/**
* subscribe: do the subscription or unsubscribe on a given folder
* returns a boolean on success or failure.
*
* @param string $_folderName
* @param boolean $_status subscribe on true, unsubscribe on false
* @return boolean
*/
function subscribe($_folderName, $_status) function subscribe($_folderName, $_status)
{ {
if (self::$debug) error_log(__METHOD__."::".($_status?"":"un")."subscribe:".$_folderName); if (self::$debug) error_log(__METHOD__."::".($_status?"":"un")."subscribe:".$_folderName);
@ -2280,7 +2297,7 @@ class emailadmin_imapbase
$inboxData->shortDisplayName = lang('INBOX'); $inboxData->shortDisplayName = lang('INBOX');
$inboxData->subscribed = true; $inboxData->subscribed = true;
if($_getCounters == true) { if($_getCounters == true) {
$inboxData->counter = self::getMailBoxCounters('INBOX'); $inboxData->counter = $this->getMailBoxCounters('INBOX');
} }
// force unsubscribed by preference showAllFoldersInFolderPane // force unsubscribed by preference showAllFoldersInFolderPane
if ($_subscribedOnly == true && if ($_subscribedOnly == true &&
@ -2335,6 +2352,7 @@ class emailadmin_imapbase
foreach ($subscribedMailboxes as $k => $finfo) foreach ($subscribedMailboxes as $k => $finfo)
{ {
//error_log(__METHOD__.__LINE__.$k.':#:'.array2string($finfo)); //error_log(__METHOD__.__LINE__.$k.':#:'.array2string($finfo));
$subscribedFoldersForCache[$this->icServer->ImapServerId][$k]=
$folderBasicInfo[$this->icServer->ImapServerId][$k]=array( $folderBasicInfo[$this->icServer->ImapServerId][$k]=array(
'MAILBOX'=>$finfo['MAILBOX'], 'MAILBOX'=>$finfo['MAILBOX'],
'ATTRIBUTES'=>$finfo['ATTRIBUTES'], 'ATTRIBUTES'=>$finfo['ATTRIBUTES'],
@ -2446,6 +2464,10 @@ class emailadmin_imapbase
'delimiter'=>$mbx['delimiter'],//lowercase for some reason??? 'delimiter'=>$mbx['delimiter'],//lowercase for some reason???
'SUBSCRIBED'=>$mbx['SUBSCRIBED'],//seeded by getMailboxes 'SUBSCRIBED'=>$mbx['SUBSCRIBED'],//seeded by getMailboxes
); );
if ($mbx['SUBSCRIBED'] && !isset($subscribedFoldersForCache[$this->icServer->ImapServerId][$mbx['MAILBOX']]))
{
$subscribedFoldersForCache[$this->icServer->ImapServerId][$mbx['MAILBOX']] = $folderBasicInfo[$this->icServer->ImapServerId][$mbx['MAILBOX']];
}
} }
if ($mbx['SUBSCRIBED'] && (empty($foldersNameSpace[$type]['subscribed']) || !in_array($mbx['MAILBOX'],$foldersNameSpace[$type]['subscribed']))) if ($mbx['SUBSCRIBED'] && (empty($foldersNameSpace[$type]['subscribed']) || !in_array($mbx['MAILBOX'],$foldersNameSpace[$type]['subscribed'])))
{ {
@ -2589,6 +2611,8 @@ class emailadmin_imapbase
} }
*/ */
} }
//subscribed folders may be used in getFolderStatus
egw_cache::setCache(egw_cache::INSTANCE,'email','subscribedFolders'.trim($GLOBALS['egw_info']['user']['account_id']),$subscribedFoldersForCache,$expiration=60*60*1);
//echo "<br>FolderNameSpace To Process:";_debug_array($foldersNameSpace); //echo "<br>FolderNameSpace To Process:";_debug_array($foldersNameSpace);
$autoFolderObjects = array(); $autoFolderObjects = array();
foreach( array('personal', 'others', 'shared') as $type) { foreach( array('personal', 'others', 'shared') as $type) {
@ -2726,14 +2750,15 @@ class emailadmin_imapbase
* *
* function to retrieve the counters for a given folder * function to retrieve the counters for a given folder
* @param string $folderName * @param string $folderName
* @return mixed false or array of counters array(MESSAGES,UNSEEN,RECENT,UIDNEXT,UIDVALIDITY) * @param boolean $_returnObject return the counters as object rather than an array
* @return mixed false or array of counters array(MESSAGES,UNSEEN,RECENT,UIDNEXT,UIDVALIDITY) or object
*/ */
function getMailBoxCounters($folderName) function getMailBoxCounters($folderName,$_returnObject=true)
{ {
try try
{ {
$folderStatus = $this->_getStatus($folderName); $folderStatus = $this->icServer->getMailboxCounters($folderName);
//error_log(__METHOD__.' ('.__LINE__.') '." FolderStatus:".array2string($folderStatus)); //error_log(__METHOD__.' ('.__LINE__.') '.$folderName.": FolderStatus:".array2string($folderStatus).function_backtrace());
} }
catch (Exception $e) catch (Exception $e)
{ {
@ -2741,6 +2766,7 @@ class emailadmin_imapbase
return false; return false;
} }
if(is_array($folderStatus)) { if(is_array($folderStatus)) {
if ($_returnObject===false) return $folderStatus;
$status = new stdClass; $status = new stdClass;
$status->messages = $folderStatus['MESSAGES']; $status->messages = $folderStatus['MESSAGES'];
$status->unseen = $folderStatus['UNSEEN']; $status->unseen = $folderStatus['UNSEEN'];

View File

@ -873,8 +873,10 @@ class mail_ui
if ($levelCt>$cmblevelsCt+1) $fetchCounters=false; if ($levelCt>$cmblevelsCt+1) $fetchCounters=false;
} }
//error_log(__METHOD__.__LINE__.' fc:'.$fetchCounters.'/'.$_fetchCounters.'('.$levelCt.'/'.$cmblevelsCt.')'.' for:'.array2string($key)); //error_log(__METHOD__.__LINE__.' fc:'.$fetchCounters.'/'.$_fetchCounters.'('.$levelCt.'/'.$cmblevelsCt.')'.' for:'.array2string($key));
$fS = $this->mail_bo->getFolderStatus($key,false,($fetchCounters?false:true)); $fS = $this->mail_bo->getFolderStatus($key,false,($fetchCounters?false:true),false);
//error_log(__METHOD__.__LINE__.array2string($fS)); //error_log(__METHOD__.__LINE__.'Object:'.array2string($obj));
//error_log(__METHOD__.__LINE__.'Status:'.array2string($fS));
//error_log(__METHOD__.__LINE__."-------------------------");
$fFP = $folderParts = explode($obj->delimiter, $key); $fFP = $folderParts = explode($obj->delimiter, $key);
if (in_array($key,$userDefinedFunctionFolders)) $obj->shortDisplayName = lang($obj->shortDisplayName); if (in_array($key,$userDefinedFunctionFolders)) $obj->shortDisplayName = lang($obj->shortDisplayName);
//get rightmost folderpart //get rightmost folderpart
@ -3321,7 +3323,7 @@ class mail_ui
if ($profileID != $this->mail_bo->profileID) continue; // only current connection if ($profileID != $this->mail_bo->profileID) continue; // only current connection
if ($folderName) if ($folderName)
{ {
$fS = $this->mail_bo->getFolderStatus($folderName,false); $fS = $this->mail_bo->getFolderStatus($folderName,false,false,false);
if (in_array($fS['shortDisplayName'],mail_bo::$autoFolders)) $fS['shortDisplayName']=lang($fS['shortDisplayName']); if (in_array($fS['shortDisplayName'],mail_bo::$autoFolders)) $fS['shortDisplayName']=lang($fS['shortDisplayName']);
//error_log(__METHOD__.__LINE__.array2string($fS)); //error_log(__METHOD__.__LINE__.array2string($fS));
if ($fS['unseen']) if ($fS['unseen'])
@ -3563,7 +3565,7 @@ class mail_ui
if (!empty($folderName)) if (!empty($folderName))
{ {
$parentFolder=(!empty($folderName)?$folderName:'INBOX'); $parentFolder=(!empty($folderName)?$folderName:'INBOX');
$folderInfo = $this->mail_bo->getFolderStatus($parentFolder,false); $folderInfo = $this->mail_bo->getFolderStatus($parentFolder,false,false,false);
if ($folderInfo['unseen']) if ($folderInfo['unseen'])
{ {
$folderInfo['shortDisplayName'] = $folderInfo['shortDisplayName'].' ('.$folderInfo['unseen'].')'; $folderInfo['shortDisplayName'] = $folderInfo['shortDisplayName'].' ('.$folderInfo['unseen'].')';
@ -3627,7 +3629,7 @@ class mail_ui
strpos($parentFolder,$folderName)===false)))) // indicates that we move the older up the tree within its own branch strpos($parentFolder,$folderName)===false)))) // indicates that we move the older up the tree within its own branch
{ {
//error_log(__METHOD__.__LINE__."$folderName, $parentFolder, $_newName"); //error_log(__METHOD__.__LINE__."$folderName, $parentFolder, $_newName");
$oldFolderInfo = $this->mail_bo->getFolderStatus($folderName,false); $oldFolderInfo = $this->mail_bo->getFolderStatus($folderName,false,false,false);
//error_log(__METHOD__.__LINE__.array2string($oldFolderInfo)); //error_log(__METHOD__.__LINE__.array2string($oldFolderInfo));
if (!empty($oldFolderInfo['attributes']) && stripos(array2string($oldFolderInfo['attributes']),'\hasnochildren')=== false) if (!empty($oldFolderInfo['attributes']) && stripos(array2string($oldFolderInfo['attributes']),'\hasnochildren')=== false)
{ {
@ -3672,7 +3674,7 @@ class mail_ui
$msg = $e->getMessage(); $msg = $e->getMessage();
} }
$this->mail_bo->reopen($parentFolder); $this->mail_bo->reopen($parentFolder);
$this->mail_bo->getFolderStatus($parentFolder,false); $this->mail_bo->getFolderStatus($parentFolder,false,false,false);
//error_log(__METHOD__.__LINE__.array2string($fS)); //error_log(__METHOD__.__LINE__.array2string($fS));
if ($hasChildren) if ($hasChildren)
{ {
@ -3704,8 +3706,8 @@ class mail_ui
{ {
translation::add_app('mail'); translation::add_app('mail');
$oldFolderInfo = $this->mail_bo->getFolderStatus($oldParentFolder,false); $oldFolderInfo = $this->mail_bo->getFolderStatus($oldParentFolder,false,false,false);
$folderInfo = $this->mail_bo->getFolderStatus($parentFolder,false); $folderInfo = $this->mail_bo->getFolderStatus($parentFolder,false,false,false);
$refreshData = array( $refreshData = array(
$profileID.self::$delimiter.$oldParentFolder=>$oldFolderInfo['shortDisplayName'], $profileID.self::$delimiter.$oldParentFolder=>$oldFolderInfo['shortDisplayName'],
$profileID.self::$delimiter.$parentFolder=>$folderInfo['shortDisplayName']); $profileID.self::$delimiter.$parentFolder=>$folderInfo['shortDisplayName']);
@ -3754,7 +3756,7 @@ class mail_ui
//error_log(__METHOD__.__LINE__."$folderName, implode($del,$pA), $_newName"); //error_log(__METHOD__.__LINE__."$folderName, implode($del,$pA), $_newName");
$oA = array(); $oA = array();
$subFolders = array(); $subFolders = array();
$oldFolderInfo = $this->mail_bo->getFolderStatus($folderName,false); $oldFolderInfo = $this->mail_bo->getFolderStatus($folderName,false,false,false);
//error_log(__METHOD__.__LINE__.array2string($oldFolderInfo)); //error_log(__METHOD__.__LINE__.array2string($oldFolderInfo));
if (!empty($oldFolderInfo['attributes']) && stripos(array2string($oldFolderInfo['attributes']),'\hasnochildren')=== false) if (!empty($oldFolderInfo['attributes']) && stripos(array2string($oldFolderInfo['attributes']),'\hasnochildren')=== false)
{ {
@ -4013,7 +4015,7 @@ class mail_ui
} }
if ($rememberServerID != $this->mail_bo->profileID) if ($rememberServerID != $this->mail_bo->profileID)
{ {
$oldFolderInfo = $this->mail_bo->getFolderStatus($trashFolder,false); $oldFolderInfo = $this->mail_bo->getFolderStatus($trashFolder,false,false,false);
$response = egw_json_response::get(); $response = egw_json_response::get();
$response->call('egw.message',lang('empty trash')); $response->call('egw.message',lang('empty trash'));
$response->call('app.mail.mail_reloadNode',array($icServerID.self::$delimiter.$trashFolder=>$oldFolderInfo['shortDisplayName'])); $response->call('app.mail.mail_reloadNode',array($icServerID.self::$delimiter.$trashFolder=>$oldFolderInfo['shortDisplayName']));