Skip to content

Commit

Permalink
Fix pagination for ActiveSync mailbox searches.
Browse files Browse the repository at this point in the history
Fixes search when a range is sent that is less than the total number of results.
Like, for example, in Android 4.4
  • Loading branch information
mrubinsk committed Jan 4, 2014
1 parent 1f55964 commit 9c49264
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
10 changes: 7 additions & 3 deletions framework/ActiveSync/lib/Horde/ActiveSync/Imap/Adapter.php
Expand Up @@ -666,7 +666,7 @@ public function ping(Horde_ActiveSync_Folder_Imap $folder)
*/
public function queryMailbox($query)
{
return $this->_doQuery($query['query'], $query['range']);
return $this->_doQuery($query['query']);
}

/**
Expand Down Expand Up @@ -1245,7 +1245,7 @@ protected function _getNamespace($path)
*
* @throws Horde_ActiveSync_Exception
*/
protected function _doQuery(array $query, $range = null)
protected function _doQuery(array $query)
{
$imap_query = new Horde_Imap_Client_Search_Query();
$mboxes = array();
Expand Down Expand Up @@ -1291,7 +1291,11 @@ protected function _doQuery(array $query, $range = null)
}
foreach ($mboxes as $mbox) {
try {
$search_res = $this->_getImapOb()->search($mbox, $imap_query);
$search_res = $this->_getImapOb()->search(
$mbox,
$imap_query,
array('results' => array(Horde_Imap_Client::SEARCH_RESULTS_MATCH, Horde_Imap_Client::SEARCH_RESULTS_SAVE, Horde_Imap_Client::SEARCH_RESULTS_COUNT))
);
} catch (Horde_Imap_Client_Exception $e) {
throw new Horde_ActiveSync_Exception($e);
}
Expand Down
15 changes: 9 additions & 6 deletions framework/ActiveSync/lib/Horde/ActiveSync/Request/Search.php
Expand Up @@ -144,7 +144,7 @@ protected function _handle()
$searchbodypreference = array();
while(1) {
if ($this->_decoder->getElementStartTag(self::SEARCH_RANGE)) {
$search_query['search_range'] = $this->_decoder->getElementContent();
$search_query['range'] = $this->_decoder->getElementContent();
if (!$this->_decoder->getElementEndTag()) {
$search_status = self::SEARCH_STATUS_ERROR;
$store_status = self::STORE_STATUS_PROTERR;
Expand Down Expand Up @@ -276,7 +276,10 @@ protected function _handle()
$this->_encoder->endTag();

if (is_array($search_result['rows']) && !empty($search_result['rows'])) {
$count = 0;
$this->_logger->debug(print_r($search_result['rows'], true));
foreach ($search_result['rows'] as $u) {
$count++;
switch (strtolower($search_name)) {
case 'documentlibrary':
// not supported
Expand Down Expand Up @@ -358,14 +361,14 @@ protected function _handle()
}
}

if (!empty($search_query['search_range'])) {
$range = explode('-', $search_query['search_range']);
if (!empty($search_query['range'])) {
$range = explode('-', $search_query['range']);
// If total results are less than max range,
// we have all results and must modify the returned range.
if ($search_result['total'] < ($range[1] + 1)) {
$search_range = $range[0] . '-' . ($search_result['total'] - 1);
if ($count < ($range[1] - $range[0] + 1)) {
$search_range = $range[0] . '-' . ($count - 1);
} else {
$search_range = $search_query['search_range'];
$search_range = $search_query['range'];
}
}
$this->_encoder->startTag(self::SEARCH_RANGE);
Expand Down
10 changes: 9 additions & 1 deletion framework/Core/lib/Horde/Core/ActiveSync/Driver.php
Expand Up @@ -1696,15 +1696,23 @@ public function changeMessage($folderid, $id, Horde_ActiveSync_Message_Base $mes
* @return array An array containing:
* - rows: An array of search results
* - status: The search store status code.
* - total: The total number of matches (for mailbox searches).
*/
public function getSearchResults($type, array $query)
{
switch (strtolower($type)) {
case 'gal':
return $this->_searchGal($query);
case 'mailbox':
$results = $this->_searchMailbox($query);
$count = count($results);
if (!empty($query['range'])) {
$range = explode('-', $query['range']);
$results = array_slice($results, $range[0], $range[1] - $range[0] + 1);
}
return array(
'rows' => $this->_searchMailbox($query),
'rows' => $results,
'total' => $count,
'status' => Horde_ActiveSync_Request_Search::STORE_STATUS_SUCCESS);
}
}
Expand Down

0 comments on commit 9c49264

Please sign in to comment.