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\Net\Exceptions\*
* @throws \MailSo\Imap\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 = new \MailSo\Imap\Requests\SORT($this);
$oSort->sCriterias = $sSearchCriterias; $oSort->sCriterias = $sSearchCriterias ?: 'ALL';
$oSort->bUid = $bReturnUid; $oSort->bUid = $bReturnUid;
$oSort->aSortTypes = $aSortTypes; $oSort->aSortTypes = $aSortTypes;
$oSort->SendRequest(); $oSort->SendRequest();
@ -392,10 +392,10 @@ trait Messages
* @throws \MailSo\Net\Exceptions\* * @throws \MailSo\Net\Exceptions\*
* @throws \MailSo\Imap\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 = new \MailSo\Imap\Requests\ESEARCH($this);
$oESearch->sCriterias = $sSearchCriterias; $oESearch->sCriterias = $sSearchCriterias ?: 'ALL';
$oESearch->aReturn = $aSearchReturn; $oESearch->aReturn = $aSearchReturn;
$oESearch->bUid = $bReturnUid; $oESearch->bUid = $bReturnUid;
$oESearch->sLimit = $sLimit; $oESearch->sLimit = $sLimit;
@ -413,10 +413,10 @@ trait Messages
* @throws \MailSo\Net\Exceptions\* * @throws \MailSo\Net\Exceptions\*
* @throws \MailSo\Imap\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 = new \MailSo\Imap\Requests\SORT($this);
$oSort->sCriterias = $sSearchCriterias; $oSort->sCriterias = $sSearchCriterias ?: 'ALL';
$oSort->bUid = $bReturnUid; $oSort->bUid = $bReturnUid;
$oSort->aSortTypes = $aSortTypes; $oSort->aSortTypes = $aSortTypes;
$oSort->aReturn = $aSearchReturn ?: ['ALL']; $oSort->aReturn = $aSearchReturn ?: ['ALL'];
@ -431,7 +431,7 @@ trait Messages
* @throws \MailSo\Net\Exceptions\* * @throws \MailSo\Net\Exceptions\*
* @throws \MailSo\Imap\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(); $aRequest = array();
// if (!$this->UTF8 && !\mb_check_encoding($sSearchCriterias, 'UTF-8')) { // if (!$this->UTF8 && !\mb_check_encoding($sSearchCriterias, 'UTF-8')) {
@ -482,10 +482,10 @@ trait Messages
* @throws \MailSo\Net\Exceptions\* * @throws \MailSo\Net\Exceptions\*
* @throws \MailSo\Imap\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 = new \MailSo\Imap\Requests\THREAD($this);
$oThread->sCriterias = $sSearchCriterias; $oThread->sCriterias = $sSearchCriterias ?: 'ALL';
$oThread->bUid = $bReturnUid; $oThread->bUid = $bReturnUid;
yield from $oThread->SendRequestIterateResponse(); yield from $oThread->SendRequestIterateResponse();
} }

View file

@ -16,7 +16,7 @@ namespace MailSo\Imap;
* @category MailSo * @category MailSo
* @package Mail * @package Mail
*/ */
abstract class SearchCriterias class SearchCriterias
{ {
const 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'; 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 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; $iTimeFilter = 0;
$aCriteriasResult = array(); $aCriteriasResult = array();
@ -332,9 +354,9 @@ abstract class SearchCriterias
$aCriteriasResult[] = $oImapClient->Settings->search_filter; $aCriteriasResult[] = $oImapClient->Settings->search_filter;
} }
$sCriteriasResult = \trim(\implode(' ', $aCriteriasResult)); $search = new self;
$search->criterias = $aCriteriasResult;
return $sCriteriasResult ?: 'ALL'; return $search;
} }
public static function escapeSearchString(\MailSo\Imap\ImapClient $oImapClient, string $sSearch) : string public static function escapeSearchString(\MailSo\Imap\ImapClient $oImapClient, string $sSearch) : string

View file

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

View file

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