Imap ESEARCH/ESORT helper

This commit is contained in:
RainLoop Team 2014-05-05 22:09:23 +04:00
parent 26cd65d763
commit 29f579dc4e
8 changed files with 159 additions and 38 deletions

View file

@ -331,7 +331,7 @@ MailBoxMessageViewViewModel.prototype.onBuild = function (oDom)
}); });
oDom oDom
.on('click', '.messageView .messageItem', function () { .on('click', '.messageView .messageItem .messageItemHeader', function () {
if (oData.useKeyboardShortcuts() && self.message()) if (oData.useKeyboardShortcuts() && self.message())
{ {
self.message.focused(true); self.message.focused(true);

View file

@ -1180,7 +1180,7 @@ class Utils
} }
/** /**
* @param string $sString * @param string $sUtfString
* *
* @return bool * @return bool
*/ */
@ -1259,7 +1259,7 @@ class Utils
* *
* @return array * @return array
*/ */
public static function ExpandFetchSequence($sSequence) public static function ParseFetchSequence($sSequence)
{ {
$aResult = array(); $aResult = array();
$sSequence = \trim($sSequence); $sSequence = \trim($sSequence);
@ -1287,6 +1287,56 @@ class Utils
return $aResult; return $aResult;
} }
/**
* @param array $aSequence
*
* @return string
*/
public static function PrepearFetchSequence($aSequence)
{
$aResult = array();
if (\is_array($aSequence) && 0 < \count($aSequence))
{
$iStart = null;
$iPrev = null;
foreach ($aSequence as $sItem)
{
// simple protection
if (false !== \strpos($sItem, ':'))
{
$aResult[] = $sItem;
continue;
}
$iItem = (int) $sItem;
if (null === $iStart || null === $iPrev)
{
$iStart = $iItem;
$iPrev = $iItem;
continue;
}
if ($iPrev === $iItem - 1)
{
$iPrev = $iItem;
}
else
{
$aResult[] = $iStart === $iPrev ? $iStart : $iStart.':'.$iPrev;
$iStart = $iItem;
$iPrev = $iItem;
}
}
if (null !== $iStart && null !== $iPrev)
{
$aResult[] = $iStart === $iPrev ? $iStart : $iStart.':'.$iPrev;
}
}
return \implode(',', $aResult);
}
/** /**
* *

View file

@ -1035,16 +1035,17 @@ class ImapClient extends \MailSo\Net\NetClient
} }
} }
$aReturn = array_reverse($aReturn);
return $aReturn; return $aReturn;
} }
/** /**
* @param bool $bSort = false
* @param string $sSearchCriterias = 'ALL' * @param string $sSearchCriterias = 'ALL'
* @param array $aSearchReturn = null * @param array $aSearchOrSortReturn = null
* @param bool $bReturnUid = true * @param bool $bReturnUid = true
* @param string $sLimit = '' * @param string $sLimit = ''
* @param string $sCharset = '' * @param string $sCharset = ''
* @param array $aSortTypes = null
* *
* @return array * @return array
* *
@ -1052,33 +1053,52 @@ class ImapClient extends \MailSo\Net\NetClient
* @throws \MailSo\Net\Exceptions\Exception * @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception * @throws \MailSo\Imap\Exceptions\Exception
*/ */
public function MessageSimpleESearch($sSearchCriterias = 'ALL', $aSearchReturn = null, $bReturnUid = true, $sLimit = '', $sCharset = '') private function simpleESearchOrESortHelper($bSort = false, $sSearchCriterias = 'ALL', $aSearchOrSortReturn = null, $bReturnUid = true, $sLimit = '', $sCharset = '', $aSortTypes = null)
{ {
$sCommandPrefix = ($bReturnUid) ? 'UID ' : ''; $sCommandPrefix = ($bReturnUid) ? 'UID ' : '';
$sSearchCriterias = 0 === \strlen($sSearchCriterias) || '*' === $sSearchCriterias $sSearchCriterias = 0 === \strlen($sSearchCriterias) || '*' === $sSearchCriterias
? 'ALL' : $sSearchCriterias; ? 'ALL' : $sSearchCriterias;
if (!$this->IsSupported('ESEARCH')) $sCmd = $bSort ? 'SORT': 'SEARCH';
if ($bSort && (!\is_array($aSortTypes) || 0 === \count($aSortTypes) || !$this->IsSupported('SORT')))
{ {
$this->writeLogException( $this->writeLogException(
new \MailSo\Base\Exceptions\InvalidArgumentException(), new \MailSo\Base\Exceptions\InvalidArgumentException(),
\MailSo\Log\Enumerations\Type::ERROR, true); \MailSo\Log\Enumerations\Type::ERROR, true);
} }
if (!\is_array($aSearchReturn) || 0 === \count($aSearchReturn)) if (!$this->IsSupported($bSort ? 'ESORT' : 'ESEARCH'))
{ {
$aSearchReturn = array('ALL'); $this->writeLogException(
new \MailSo\Base\Exceptions\InvalidArgumentException(),
\MailSo\Log\Enumerations\Type::ERROR, true);
}
if (!\is_array($aSearchOrSortReturn) || 0 === \count($aSearchOrSortReturn))
{
$aSearchOrSortReturn = array('ALL');
} }
$aRequest = array(); $aRequest = array();
if (0 < \strlen($sCharset)) if ($bSort)
{ {
$aRequest[] = 'CHARSET'; $aRequest[] = 'RETURN';
$aRequest[] = \strtoupper($sCharset); $aRequest[] = $aSearchOrSortReturn;
}
$aRequest[] = 'RETURN'; $aRequest[] = $aSortTypes;
$aRequest[] = $aSearchReturn; $aRequest[] = \MailSo\Base\Utils::IsAscii($sSearchCriterias) ? 'US-ASCII' : 'UTF-8';
}
else
{
if (0 < \strlen($sCharset))
{
$aRequest[] = 'CHARSET';
$aRequest[] = \strtoupper($sCharset);
}
$aRequest[] = 'RETURN';
$aRequest[] = $aSearchOrSortReturn;
}
$aRequest[] = $sSearchCriterias; $aRequest[] = $sSearchCriterias;
@ -1087,7 +1107,7 @@ class ImapClient extends \MailSo\Net\NetClient
$aRequest[] = $sLimit; $aRequest[] = $sLimit;
} }
$this->SendRequest($sCommandPrefix.('SEARCH'), $aRequest); $this->SendRequest($sCommandPrefix.$sCmd, $aRequest);
$sRequestTag = $this->getCurrentTag(); $sRequestTag = $this->getCurrentTag();
$aResult = array(); $aResult = array();
@ -1132,6 +1152,42 @@ class ImapClient extends \MailSo\Net\NetClient
return $aResult; return $aResult;
} }
/**
* @param string $sSearchCriterias = 'ALL'
* @param array $aSearchReturn = null
* @param bool $bReturnUid = true
* @param string $sLimit = ''
* @param string $sCharset = ''
*
* @return array
*
* @throws \MailSo\Base\Exceptions\InvalidArgumentException
* @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception
*/
public function MessageSimpleESearch($sSearchCriterias = 'ALL', $aSearchReturn = null, $bReturnUid = true, $sLimit = '', $sCharset = '')
{
return $this->simpleESearchOrESortHelper(false, $sSearchCriterias, $aSearchReturn, $bReturnUid, $sLimit, $sCharset);
}
/**
* @param array $aSortTypes
* @param string $sSearchCriterias = 'ALL'
* @param array $aSearchReturn = null
* @param bool $bReturnUid = true
* @param string $sLimit = ''
*
* @return array
*
* @throws \MailSo\Base\Exceptions\InvalidArgumentException
* @throws \MailSo\Net\Exceptions\Exception
* @throws \MailSo\Imap\Exceptions\Exception
*/
public function MessageSimpleESort($aSortTypes, $sSearchCriterias = 'ALL', $aSearchReturn = null, $bReturnUid = true, $sLimit = '')
{
return $this->simpleESearchOrESortHelper(true, $sSearchCriterias, $aSearchReturn, $bReturnUid, $sLimit, '', $aSortTypes);
}
/** /**
* @param string $sSearchCriterias * @param string $sSearchCriterias
* @param bool $bReturnUid = true * @param bool $bReturnUid = true

View file

@ -239,8 +239,8 @@ class MailClient
: \MailSo\Imap\Enumerations\StoreAction::REMOVE_FLAGS_SILENT : \MailSo\Imap\Enumerations\StoreAction::REMOVE_FLAGS_SILENT
; ;
$sIndexRange = \implode(',', $aIndexRange); $this->oImapClient->MessageStoreFlag(\MailSo\Base\Utils::PrepearFetchSequence($aIndexRange),
$this->oImapClient->MessageStoreFlag($sIndexRange, $bIndexIsUid, array($sMessageFlag), $sStoreAction); $bIndexIsUid, array($sMessageFlag), $sStoreAction);
} }
} }
@ -536,7 +536,7 @@ class MailClient
$this->oImapClient->FolderSelect($sFolder); $this->oImapClient->FolderSelect($sFolder);
$sIndexRange = \implode(',', $aIndexRange); $sIndexRange = \MailSo\Base\Utils::PrepearFetchSequence($aIndexRange);
$this->oImapClient->MessageStoreFlag($sIndexRange, $bIndexIsUid, $this->oImapClient->MessageStoreFlag($sIndexRange, $bIndexIsUid,
array(\MailSo\Imap\Enumerations\MessageFlag::DELETED), array(\MailSo\Imap\Enumerations\MessageFlag::DELETED),
@ -576,11 +576,14 @@ class MailClient
if ($bUseMoveSupported && $this->oImapClient->IsSupported('MOVE')) if ($bUseMoveSupported && $this->oImapClient->IsSupported('MOVE'))
{ {
$this->oImapClient->MessageMove($sToFolder, \implode(',', $aIndexRange), $bIndexIsUid); $this->oImapClient->MessageMove($sToFolder,
\MailSo\Base\Utils::PrepearFetchSequence($aIndexRange), $bIndexIsUid);
} }
else else
{ {
$this->oImapClient->MessageCopy($sToFolder, \implode(',', $aIndexRange), $bIndexIsUid); $this->oImapClient->MessageCopy($sToFolder,
\MailSo\Base\Utils::PrepearFetchSequence($aIndexRange), $bIndexIsUid);
$this->MessageDelete($sFromFolder, $aIndexRange, $bIndexIsUid, true); $this->MessageDelete($sFromFolder, $aIndexRange, $bIndexIsUid, true);
} }
@ -608,7 +611,8 @@ class MailClient
} }
$this->oImapClient->FolderSelect($sFromFolder); $this->oImapClient->FolderSelect($sFromFolder);
$this->oImapClient->MessageCopy($sToFolder, \implode(',', $aIndexRange), $bIndexIsUid); $this->oImapClient->MessageCopy($sToFolder,
\MailSo\Base\Utils::PrepearFetchSequence($aIndexRange), $bIndexIsUid);
return $this; return $this;
} }
@ -806,7 +810,7 @@ class MailClient
\MailSo\Imap\Enumerations\FetchType::INDEX, \MailSo\Imap\Enumerations\FetchType::INDEX,
\MailSo\Imap\Enumerations\FetchType::UID, \MailSo\Imap\Enumerations\FetchType::UID,
\MailSo\Imap\Enumerations\FetchType::FLAGS \MailSo\Imap\Enumerations\FetchType::FLAGS
), \implode(',', $aUids), true); ), \MailSo\Base\Utils::PrepearFetchSequence($aUids), true);
if (\is_array($aFetchResponse) && 0 < \count($aFetchResponse)) if (\is_array($aFetchResponse) && 0 < \count($aFetchResponse))
{ {
@ -1469,7 +1473,7 @@ class MailClient
\MailSo\Imap\Enumerations\FetchType::FLAGS, \MailSo\Imap\Enumerations\FetchType::FLAGS,
\MailSo\Imap\Enumerations\FetchType::BODYSTRUCTURE, \MailSo\Imap\Enumerations\FetchType::BODYSTRUCTURE,
$this->getEnvelopeOrHeadersRequestString() $this->getEnvelopeOrHeadersRequestString()
), \implode(',', $aRequestIndexOrUids), $bIndexAsUid); ), \MailSo\Base\Utils::PrepearFetchSequence($aRequestIndexOrUids), $bIndexAsUid);
if (\is_array($aFetchResponse) && 0 < \count($aFetchResponse)) if (\is_array($aFetchResponse) && 0 < \count($aFetchResponse))
{ {
@ -1520,7 +1524,7 @@ class MailClient
* @param bool $bUseSortIfSupported = false * @param bool $bUseSortIfSupported = false
* @param bool $bUseThreadSortIfSupported = false * @param bool $bUseThreadSortIfSupported = false
* @param array $aExpandedThreadsUids = array() * @param array $aExpandedThreadsUids = array()
* @param bool $bUseESearchOrESortRequest = true * @param bool $bUseESearchOrESortRequest = false
* *
* @return \MailSo\Mail\MessageCollection * @return \MailSo\Mail\MessageCollection
* *
@ -1530,7 +1534,7 @@ class MailClient
*/ */
public function MessageList($sFolderName, $iOffset = 0, $iLimit = 10, $sSearch = '', $sPrevUidNext = '', public function MessageList($sFolderName, $iOffset = 0, $iLimit = 10, $sSearch = '', $sPrevUidNext = '',
$oCacher = null, $bUseSortIfSupported = false, $bUseThreadSortIfSupported = false, $aExpandedThreadsUids = array(), $oCacher = null, $bUseSortIfSupported = false, $bUseThreadSortIfSupported = false, $aExpandedThreadsUids = array(),
$bUseESearchOrESortRequest = true) $bUseESearchOrESortRequest = false)
{ {
$sSearch = \trim($sSearch); $sSearch = \trim($sSearch);
if (!\MailSo\Base\Validator::RangeInt($iOffset, 0) || if (!\MailSo\Base\Validator::RangeInt($iOffset, 0) ||
@ -1623,12 +1627,17 @@ class MailClient
{ {
if ($bESortSupported) if ($bESortSupported)
{ {
// TODO $aESorthData = $this->oImapClient->MessageSimpleESort(array('ARRIVAL'), $sSearchCriterias, array('ALL'), $bIndexAsUid, '');
$aIndexOrUids = $this->oImapClient->MessageSimpleSort(array('ARRIVAL'), $sSearchCriterias, $bIndexAsUid); if (isset($aESorthData['ALL']))
{
$aIndexOrUids = \MailSo\Base\Utils::ParseFetchSequence($aESorthData['ALL']);
$aIndexOrUids = \array_reverse($aIndexOrUids);
}
unset($aESorthData);
} }
else else
{ {
$aIndexOrUids = $this->oImapClient->MessageSimpleSort(array('ARRIVAL'), $sSearchCriterias, $bIndexAsUid); $aIndexOrUids = $this->oImapClient->MessageSimpleSort(array('REVERSE ARRIVAL'), $sSearchCriterias, $bIndexAsUid);
} }
} }
else else
@ -1642,8 +1651,10 @@ class MailClient
$aESearchData = $this->oImapClient->MessageSimpleESearch($sSearchCriterias, array('ALL'), $bIndexAsUid, '', 'UTF-8'); $aESearchData = $this->oImapClient->MessageSimpleESearch($sSearchCriterias, array('ALL'), $bIndexAsUid, '', 'UTF-8');
if (isset($aESearchData['ALL'])) if (isset($aESearchData['ALL']))
{ {
$aIndexOrUids = \MailSo\Base\Utils::ExpandFetchSequence($aESearchData['ALL']); $aIndexOrUids = \MailSo\Base\Utils::ParseFetchSequence($aESearchData['ALL']);
$aIndexOrUids = \array_reverse($aIndexOrUids);
} }
unset($aESearchData);
} }
else else
{ {
@ -1664,8 +1675,10 @@ class MailClient
$aESearchData = $this->oImapClient->MessageSimpleESearch($sSearchCriterias, array('ALL'), $bIndexAsUid); $aESearchData = $this->oImapClient->MessageSimpleESearch($sSearchCriterias, array('ALL'), $bIndexAsUid);
if (isset($aESearchData['ALL'])) if (isset($aESearchData['ALL']))
{ {
$aIndexOrUids = \MailSo\Base\Utils::ExpandFetchSequence($aESearchData['ALL']); $aIndexOrUids = \MailSo\Base\Utils::ParseFetchSequence($aESearchData['ALL']);
$aIndexOrUids = \array_reverse($aIndexOrUids);
} }
unset($aESearchData);
} }
else else
{ {
@ -1686,7 +1699,7 @@ class MailClient
$oCacher); $oCacher);
$aIndexOrUids = $this->compileLineThreadUids($aThreads, $aLastCollapsedThreadUids, $aExpandedThreadsUids, 0); $aIndexOrUids = $this->compileLineThreadUids($aThreads, $aLastCollapsedThreadUids, $aExpandedThreadsUids, 0);
$iMessageCount = count($aIndexOrUids); $iMessageCount = \count($aIndexOrUids);
} }
else else
{ {

View file

@ -3959,7 +3959,8 @@ class Actions
($this->Config()->Get('cache', 'enable', true) && $this->Config()->Get('cache', 'server_uids', false)) ? $this->Cacher() : null, ($this->Config()->Get('cache', 'enable', true) && $this->Config()->Get('cache', 'server_uids', false)) ? $this->Cacher() : null,
!!$this->Config()->Get('labs', 'use_imap_sort', false), !!$this->Config()->Get('labs', 'use_imap_sort', false),
$bUseThreads, $bUseThreads,
$aExpandedThreadUid $aExpandedThreadUid,
!!$this->Config()->Get('labs', 'use_imap_esearch_esort', false)
); );
} }
catch (\Exception $oException) catch (\Exception $oException)

View file

@ -225,7 +225,8 @@ Enables caching in the system'),
'allow_html_editor_source_button' => array(false), 'allow_html_editor_source_button' => array(false),
'use_app_debug_js' => array(false), 'use_app_debug_js' => array(false),
'use_app_debug_css' => array(false), 'use_app_debug_css' => array(false),
'use_imap_sort' => array(false), 'use_imap_sort' => array(true),
'use_imap_esearch_esort' => array(false),
'use_imap_force_selection' => array(false), 'use_imap_force_selection' => array(false),
'use_imap_list_subscribe' => array(true), 'use_imap_list_subscribe' => array(true),
'use_imap_thread' => array(true), 'use_imap_thread' => array(true),

View file

@ -13362,7 +13362,7 @@ MailBoxMessageViewViewModel.prototype.onBuild = function (oDom)
}); });
oDom oDom
.on('click', '.messageView .messageItem', function () { .on('click', '.messageView .messageItem .messageItemHeader', function () {
if (oData.useKeyboardShortcuts() && self.message()) if (oData.useKeyboardShortcuts() && self.message())
{ {
self.message.focused(true); self.message.focused(true);

File diff suppressed because one or more lines are too long