Added draft code for FUZZY search RFC 6851

This commit is contained in:
the-djmaze 2024-02-05 16:12:56 +01:00
parent a9669e430f
commit d4cdfb8fa5
4 changed files with 51 additions and 23 deletions

View file

@ -363,10 +363,10 @@ trait Messages
* @throws \MailSo\Net\Exceptions\*
* @throws \MailSo\Imap\Exceptions\*
*/
public function MessageSimpleSort(array $aSortTypes, string $sSearchCriterias = 'ALL', bool $bReturnUid = true) : array
public function MessageSimpleSort(array $aSortTypes, string $sSearchCriterias, bool $bReturnUid = true) : array
{
$oSort = new \MailSo\Imap\Requests\SORT($this);
$oSort->sCriterias = $sSearchCriterias;
$oSort->sCriterias = $sSearchCriterias ?: 'ALL';
$oSort->bUid = $bReturnUid;
$oSort->aSortTypes = $aSortTypes;
$oSort->SendRequest();
@ -392,10 +392,10 @@ trait Messages
* @throws \MailSo\Net\Exceptions\*
* @throws \MailSo\Imap\Exceptions\*
*/
public function MessageSimpleESearch(string $sSearchCriterias = 'ALL', array $aSearchReturn = null, bool $bReturnUid = true, string $sLimit = '') : array
public function MessageSimpleESearch(string $sSearchCriterias, array $aSearchReturn = null, bool $bReturnUid = true, string $sLimit = '') : array
{
$oESearch = new \MailSo\Imap\Requests\ESEARCH($this);
$oESearch->sCriterias = $sSearchCriterias;
$oESearch->sCriterias = $sSearchCriterias ?: 'ALL';
$oESearch->aReturn = $aSearchReturn;
$oESearch->bUid = $bReturnUid;
$oESearch->sLimit = $sLimit;
@ -413,10 +413,10 @@ trait Messages
* @throws \MailSo\Net\Exceptions\*
* @throws \MailSo\Imap\Exceptions\*
*/
public function MessageSimpleESort(array $aSortTypes, string $sSearchCriterias = 'ALL', array $aSearchReturn = ['ALL'], bool $bReturnUid = true, string $sLimit = '') : array
public function MessageSimpleESort(array $aSortTypes, string $sSearchCriterias, array $aSearchReturn = ['ALL'], bool $bReturnUid = true, string $sLimit = '') : array
{
$oSort = new \MailSo\Imap\Requests\SORT($this);
$oSort->sCriterias = $sSearchCriterias;
$oSort->sCriterias = $sSearchCriterias ?: 'ALL';
$oSort->bUid = $bReturnUid;
$oSort->aSortTypes = $aSortTypes;
$oSort->aReturn = $aSearchReturn ?: ['ALL'];
@ -431,7 +431,7 @@ trait Messages
* @throws \MailSo\Net\Exceptions\*
* @throws \MailSo\Imap\Exceptions\*
*/
public function MessageSimpleSearch(string $sSearchCriterias = 'ALL', bool $bReturnUid = true) : array
public function MessageSimpleSearch(string $sSearchCriterias, bool $bReturnUid = true) : array
{
$aRequest = array();
// if (!$this->UTF8 && !\mb_check_encoding($sSearchCriterias, 'UTF-8')) {
@ -482,10 +482,10 @@ trait Messages
* @throws \MailSo\Net\Exceptions\*
* @throws \MailSo\Imap\Exceptions\*
*/
public function MessageSimpleThread(string $sSearchCriterias = 'ALL', bool $bReturnUid = true) : iterable
public function MessageSimpleThread(string $sSearchCriterias, bool $bReturnUid = true) : iterable
{
$oThread = new \MailSo\Imap\Requests\THREAD($this);
$oThread->sCriterias = $sSearchCriterias;
$oThread->sCriterias = $sSearchCriterias ?: 'ALL';
$oThread->bUid = $bReturnUid;
yield from $oThread->SendRequestIterateResponse();
}

View file

@ -16,7 +16,7 @@ namespace MailSo\Imap;
* @category MailSo
* @package Mail
*/
abstract class SearchCriterias
class SearchCriterias
{
const
RegEx = 'in|e?mail|from|to|subject|has|is|date|since|before|text|body|size|larger|bigger|smaller|maxsize|minsize|keyword|older_than|newer_than|on|senton|sentsince|sentbefore';
@ -126,7 +126,29 @@ abstract class SearchCriterias
X RECENT
*/
public static function fromString(\MailSo\Imap\ImapClient $oImapClient, string $sFolderName, string $sSearch, bool $bHideDeleted, bool &$bUseCache = true) : string
private array $criterias = [];
public bool $fuzzy = false;
function prepend(string $rule)
{
\array_unshift($this->criterias, $rule);
}
function __toString() : string
{
if ($this->fuzzy) {
$keys = ['BCC','BODY','CC','FROM','SUBJECT','TEXT','TO'];
foreach ($this->criterias as $i => $key) {
if (\in_array($key, $keys)) {
$this->criterias[$i] = "FUZZY {$key}";
}
}
}
$sCriteriasResult = \trim(\implode(' ', $this->criterias));
return $sCriteriasResult ?: 'ALL';
}
public static function fromString(\MailSo\Imap\ImapClient $oImapClient, string $sFolderName, string $sSearch, bool $bHideDeleted, bool &$bUseCache = true) : self
{
$iTimeFilter = 0;
$aCriteriasResult = array();
@ -332,9 +354,9 @@ abstract class SearchCriterias
$aCriteriasResult[] = $oImapClient->Settings->search_filter;
}
$sCriteriasResult = \trim(\implode(' ', $aCriteriasResult));
return $sCriteriasResult ?: 'ALL';
$search = new self;
$search->criterias = $aCriteriasResult;
return $search;
}
public static function escapeSearchString(\MailSo\Imap\ImapClient $oImapClient, string $sSearch) : string

View file

@ -593,7 +593,7 @@ class MailClient
$oParams->sSort = \implode(' ', $aSortTypes);
$bUseCache = $oCacher && $oCacher->IsInited();
$sSearchCriterias = \MailSo\Imap\SearchCriterias::fromString(
$oSearchCriterias = \MailSo\Imap\SearchCriterias::fromString(
$this->oImapClient,
$sFolderName,
$oParams->sSearch,
@ -606,16 +606,20 @@ class MailClient
$bReturnUid = true;
if ($oParams->oSequenceSet) {
$bReturnUid = $oParams->oSequenceSet->UID;
$sSearchCriterias = $oParams->oSequenceSet . ' ' . $sSearchCriterias;
$oSearchCriterias->prepend($oParams->oSequenceSet);
}
/*
$oSearchCriterias->fuzzy = $oParams->bSearchFuzzy && $this->oImapClient->hasCapability('SEARCH=FUZZY');
*/
$sSerializedHash = '';
$sSerializedLog = '';
if ($bUseCache) {
$sSerializedHash = 'Get'
. ($bReturnUid ? 'UIDS/' : 'IDS/')
. "{$oParams->sSort}/{$this->oImapClient->Hash()}/{$sFolderName}/{$sSearchCriterias}";
$sSerializedLog = "\"{$sFolderName}\" / {$oParams->sSort} / {$sSearchCriterias}";
. "{$oParams->sSort}/{$this->oImapClient->Hash()}/{$sFolderName}/{$oSearchCriterias}";
$sSerializedLog = "\"{$sFolderName}\" / {$oParams->sSort} / {$oSearchCriterias}";
$sSerialized = $oCacher->Get($sSerializedHash);
if (!empty($sSerialized)) {
@ -638,12 +642,12 @@ class MailClient
$aResultUids = [];
if ($bUseSort) {
// $this->oImapClient->hasCapability('ESORT')
// $aResultUids = $this->oImapClient->MessageSimpleESort($aSortTypes, $sSearchCriterias)['ALL'];
$aResultUids = $this->oImapClient->MessageSimpleSort($aSortTypes, $sSearchCriterias, $bReturnUid);
// $aResultUids = $this->oImapClient->MessageSimpleESort($aSortTypes, $oSearchCriterias)['ALL'];
$aResultUids = $this->oImapClient->MessageSimpleSort($aSortTypes, $oSearchCriterias, $bReturnUid);
} else {
// $this->oImapClient->hasCapability('ESEARCH')
// $aResultUids = $this->oImapClient->MessageSimpleESearch($sSearchCriterias, null, $bReturnUid)
$aResultUids = $this->oImapClient->MessageSimpleSearch($sSearchCriterias, $bReturnUid);
// $aResultUids = $this->oImapClient->MessageSimpleESearch($oSearchCriterias, null, $bReturnUid)
$aResultUids = $this->oImapClient->MessageSimpleSearch($oSearchCriterias, $bReturnUid);
}
if ($bUseCache) {

View file

@ -24,7 +24,8 @@ class MessageListParams
public bool
$bUseSort = true,
$bUseThreads = false,
$bHideDeleted = true;
$bHideDeleted = true,
$bSearchFuzzy = false;
protected int
$iOffset = 0,
@ -60,6 +61,7 @@ class MessageListParams
$this->iLimit,
$this->bHideDeleted ? '1' : '0',
$this->sSearch,
$this->bSearchFuzzy ? '1' : '0',
$this->bUseSort ? $this->sSort : '0',
$this->bUseThreads ? $this->iThreadUid : '',
$this->iPrevUidNext