Queries need to be built for each mailbox
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 10 Nov 2010 20:21:12 +0000 (13:21 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 11 Nov 2010 16:11:53 +0000 (09:11 -0700)
Right now, it is impossible to do an advanced search query - in other
words, a query that involves more than an IMAP query statement.
Creating queries by mailbox allows advanced queries to be performed.
For example, a query element can independently run a query, get the list
of indices that match, and then put this result into the master IMAP
server query with a UID sequence search.

14 files changed:
imp/lib/Search.php
imp/lib/Search/Element.php
imp/lib/Search/Element/Bulk.php
imp/lib/Search/Element/Date.php
imp/lib/Search/Element/Flag.php
imp/lib/Search/Element/Header.php
imp/lib/Search/Element/Mailinglist.php
imp/lib/Search/Element/Or.php
imp/lib/Search/Element/Personal.php
imp/lib/Search/Element/Recipient.php
imp/lib/Search/Element/Size.php
imp/lib/Search/Element/Text.php
imp/lib/Search/Element/Within.php
imp/lib/Search/Query.php

index 71f4db9..92d14a2 100644 (file)
@@ -95,25 +95,22 @@ class IMP_Search implements ArrayAccess, Iterator, Serializable
         $mbox = '';
         $sorted = new IMP_Indices();
 
-        $query = $this[$id]->query;
-        if (!$query) {
+        if (!($query_list = $this[$id]->query)) {
             return $sorted;
         }
 
-        /* Prepare the search query. */
-        if (!empty($ob)) {
-            $query->andSearch(array($ob));
-        }
-
         /* How do we want to sort results? */
         $sortpref = IMP::getSort(null, true);
         if ($sortpref['by'] == Horde_Imap_Client::SORT_THREAD) {
             $sortpref['by'] = $GLOBALS['prefs']->getValue('sortdate');
         }
 
-        foreach ($this[$id]->mboxes as $val) {
-            $results = $this->imapSearch($val, $query, array('reverse' => $sortpref['dir'], 'sort' => array($sortpref['by'])));
-            $sorted->add($val, $results['sort']);
+        foreach ($query_list as $mbox => $query) {
+            if (!empty($ob)) {
+                $query->andSearch(array($ob));
+            }
+            $results = $this->imapSearch($mbox, $query, array('reverse' => $sortpref['dir'], 'sort' => array($sortpref['by'])));
+            $sorted->add($mbox, $results['sort']);
         }
 
         return $sorted;
index 2c4694b..3e7244f 100644 (file)
@@ -34,9 +34,13 @@ abstract class IMP_Search_Element implements Serializable
     /**
      * Adds the current query item to the query object.
      *
-     * @param Horde_Imap_Client_Search_Query  The query object.
+     * @param string $mbox                             The mailbox to create
+     *                                                 the query for.
+     * @param Horde_Imap_Client_Search_Query $queryob  The query object.
+     *
+     * @return Horde_Imap_Client_Search_Query  The altered query object.
      */
-    abstract public function createQuery($queryob);
+    abstract public function createQuery($mbox, $queryob);
 
     /**
      * Return search query text representation.
index b51d7eb..8c579a8 100644 (file)
@@ -31,7 +31,7 @@ class IMP_Search_Element_Bulk extends IMP_Search_Element
 
     /**
      */
-    public function createQuery($queryob)
+    public function createQuery($mbox, $queryob)
     {
         $queryob->headerText('precedence', 'bulk', $this->_data);
 
index ab3b0e0..7b78b65 100644 (file)
@@ -42,7 +42,7 @@ class IMP_Search_Element_Date extends IMP_Search_Element
 
     /**
      */
-    public function createQuery($queryob)
+    public function createQuery($mbox, $queryob)
     {
         $date = new DateTime($this->_data->d);
         $queryob->dateSearch($date, ($this->_data->t == self::DATE_ON) ? Horde_Imap_Client_Search_Query::DATE_ON : (($this->_data->t == self::DATE_BEFORE) ? Horde_Imap_Client_Search_Query::DATE_BEFORE : Horde_Imap_Client_Search_Query::DATE_SINCE));
index 0be7a42..3d102fa 100644 (file)
@@ -41,7 +41,7 @@ class IMP_Search_Element_Flag extends IMP_Search_Element
 
     /**
      */
-    public function createQuery($queryob)
+    public function createQuery($mbox, $queryob)
     {
         $queryob->flag($this->_data->f, $this->_data->s);
 
index e796b29..4b04064 100644 (file)
@@ -35,7 +35,7 @@ class IMP_Search_Element_Header extends IMP_Search_Element
 
     /**
      */
-    public function createQuery($queryob)
+    public function createQuery($mbox, $queryob)
     {
         $queryob->headerText($this->_data->h, $this->_data->t, $this->_data->n);
 
index e58f343..1cbf9ab 100644 (file)
@@ -30,7 +30,7 @@ class IMP_Search_Element_Mailinglist extends IMP_Search_Element
 
     /**
      */
-    public function createQuery($queryob)
+    public function createQuery($mbox, $queryob)
     {
         $queryob->headerText('list-id', '', $this->_data);
 
index 3ded706..ecbc738 100644 (file)
@@ -16,7 +16,7 @@ class IMP_Search_Element_Or extends IMP_Search_Element
 {
     /**
      */
-    public function createQuery($queryob)
+    public function createQuery($mbox, $queryob)
     {
         $ob = new Horde_Imap_Client_Search_Query();
         $ob->orSearch(array($queryob));
index ba0c6c9..6a26742 100644 (file)
@@ -30,7 +30,7 @@ class IMP_Search_Element_Personal extends IMP_Search_Element
 
     /**
      */
-    public function createQuery($queryob)
+    public function createQuery($mbox, $queryob)
     {
         $and_ob = new Horde_Imap_Client_Search_Query();
         $identity = $GLOBALS['injector']->getInstance('IMP_Identity');
index 219a089..a5977ce 100644 (file)
@@ -32,7 +32,7 @@ class IMP_Search_Element_Recipient extends IMP_Search_Element
 
     /**
      */
-    public function createQuery($queryob)
+    public function createQuery($mbox, $queryob)
     {
         $and_ob = new Horde_Imap_Client_Search_Query();
 
index 9023dfa..1ede153 100644 (file)
@@ -39,7 +39,7 @@ class IMP_Search_Element_Size extends IMP_Search_Element
 
     /**
      */
-    public function createQuery($queryob)
+    public function createQuery($mbox, $queryob)
     {
         $queryob->size($this->_data->s, $this->_data->l);
 
index d93ad63..a32fdc4 100644 (file)
@@ -36,7 +36,7 @@ class IMP_Search_Element_Text extends IMP_Search_Element
 
     /**
      */
-    public function createQuery($queryob)
+    public function createQuery($mbox, $queryob)
     {
         $queryob->text($this->_data->t, $this->_data->b, $this->_data->n);
 
index 2986eb3..b86d03d 100644 (file)
@@ -45,7 +45,7 @@ class IMP_Search_Element_Within extends IMP_Search_Element
 
     /**
      */
-    public function createQuery($queryob)
+    public function createQuery($mbox, $queryob)
     {
         /* Limited to day granularity because that is the technical
          * limit for IMAP servers without 'WITHIN' extension. */
index 3f4f12c..606a810 100644 (file)
@@ -116,7 +116,9 @@ class IMP_Search_Query implements Serializable
      * 'label' - (string) The query label.
      * 'mboxes' - (array) The list of mailboxes to query.
      * 'mid' - (string) The query ID with the search mailbox prefix.
-     * 'query' - (Horde_Imap_Client_Search_Query) The IMAP query object.
+     * 'query' - (array) The list of IMAP queries that comprise this search.
+     *           Keys are mailbox names, values are
+     *           Horde_Imap_Client_Search_Query objects.
      * 'querytext' - (string) The textual representation of the query.
      * </pre>
      *
@@ -151,11 +153,17 @@ class IMP_Search_Query implements Serializable
             return IMP_Search::MBOX_PREFIX . $this->_id;
 
         case 'query':
-            $query = new Horde_Imap_Client_Search_Query();
-            foreach ($this->_criteria as $elt) {
-                $query = $elt->createQuery($query);
+            $qout = array();
+
+            foreach ($this->_mboxes as $mbox) {
+                $query = new Horde_Imap_Client_Search_Query();
+                foreach ($this->_criteria as $elt) {
+                    $query = $elt->createQuery($mbox, $query);
+                }
+                $qout[$mbox] = $query;
             }
-            return $query;
+
+            return $qout;
 
         case 'querytext':
             $text = array(_("Search"));