You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rcube_result_multifolder.php 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | This file is part of the Roundcube Webmail client |
  5. | Copyright (C) 2005-2011, The Roundcube Dev Team |
  6. | Copyright (C) 2011, Kolab Systems AG |
  7. | |
  8. | Licensed under the GNU General Public License version 3 or |
  9. | any later version with exceptions for skins & plugins. |
  10. | See the README file for a full license statement. |
  11. | |
  12. | PURPOSE: |
  13. | SORT/SEARCH/ESEARCH response handler |
  14. +-----------------------------------------------------------------------+
  15. | Author: Thomas Bruederli <roundcube@gmail.com> |
  16. +-----------------------------------------------------------------------+
  17. */
  18. /**
  19. * Class holding a set of rcube_result_index instances that together form a
  20. * result set of a multi-folder search
  21. *
  22. * @package Framework
  23. * @subpackage Storage
  24. */
  25. class rcube_result_multifolder
  26. {
  27. public $multi = true;
  28. public $sets = array();
  29. public $incomplete = false;
  30. public $folder;
  31. protected $meta = array();
  32. protected $index = array();
  33. protected $folders = array();
  34. protected $sdata = array();
  35. protected $order = 'ASC';
  36. protected $sorting;
  37. /**
  38. * Object constructor.
  39. */
  40. public function __construct($folders = array())
  41. {
  42. $this->folders = $folders;
  43. $this->meta = array('count' => 0);
  44. }
  45. /**
  46. * Initializes object with SORT command response
  47. *
  48. * @param string $data IMAP response string
  49. */
  50. public function add($result)
  51. {
  52. $this->sets[] = $result;
  53. if ($result->count()) {
  54. $this->append_result($result);
  55. }
  56. else if ($result->incomplete) {
  57. $this->incomplete = true;
  58. }
  59. }
  60. /**
  61. * Append message UIDs from the given result to our index
  62. */
  63. protected function append_result($result)
  64. {
  65. $this->meta['count'] += $result->count();
  66. // append UIDs to global index
  67. $folder = $result->get_parameters('MAILBOX');
  68. $index = array_map(function($uid) use ($folder) { return $uid . '-' . $folder; }, $result->get());
  69. $this->index = array_merge($this->index, $index);
  70. }
  71. /**
  72. * Store a global index of (sorted) message UIDs
  73. */
  74. public function set_message_index($headers, $sort_field, $sort_order)
  75. {
  76. $this->index = array();
  77. foreach ($headers as $header) {
  78. $this->index[] = $header->uid . '-' . $header->folder;
  79. }
  80. $this->sorting = $sort_field;
  81. $this->order = $sort_order;
  82. }
  83. /**
  84. * Checks the result from IMAP command
  85. *
  86. * @return bool True if the result is an error, False otherwise
  87. */
  88. public function is_error()
  89. {
  90. return false;
  91. }
  92. /**
  93. * Checks if the result is empty
  94. *
  95. * @return bool True if the result is empty, False otherwise
  96. */
  97. public function is_empty()
  98. {
  99. return empty($this->sets) || $this->meta['count'] == 0;
  100. }
  101. /**
  102. * Returns number of elements in the result
  103. *
  104. * @return int Number of elements
  105. */
  106. public function count()
  107. {
  108. return $this->meta['count'];
  109. }
  110. /**
  111. * Returns number of elements in the result.
  112. * Alias for count() for compatibility with rcube_result_thread
  113. *
  114. * @return int Number of elements
  115. */
  116. public function count_messages()
  117. {
  118. return $this->count();
  119. }
  120. /**
  121. * Reverts order of elements in the result
  122. */
  123. public function revert()
  124. {
  125. $this->order = $this->order == 'ASC' ? 'DESC' : 'ASC';
  126. $this->index = array_reverse($this->index);
  127. // revert order in all sub-sets
  128. foreach ($this->sets as $set) {
  129. if ($this->order != $set->get_parameters('ORDER')) {
  130. $set->revert();
  131. }
  132. }
  133. }
  134. /**
  135. * Check if the given message ID exists in the object
  136. *
  137. * @param int $msgid Message ID
  138. * @param bool $get_index When enabled element's index will be returned.
  139. * Elements are indexed starting with 0
  140. * @return mixed False if message ID doesn't exist, True if exists or
  141. * index of the element if $get_index=true
  142. */
  143. public function exists($msgid, $get_index = false)
  144. {
  145. if (!empty($this->folder)) {
  146. $msgid .= '-' . $this->folder;
  147. }
  148. return array_search($msgid, $this->index);
  149. }
  150. /**
  151. * Filters data set. Removes elements listed in $ids list.
  152. *
  153. * @param array $ids List of IDs to remove.
  154. * @param string $folder IMAP folder
  155. */
  156. public function filter($ids = array(), $folder = null)
  157. {
  158. $this->meta['count'] = 0;
  159. foreach ($this->sets as $set) {
  160. if ($set->get_parameters('MAILBOX') == $folder) {
  161. $set->filter($ids);
  162. }
  163. $this->meta['count'] += $set->count();
  164. }
  165. }
  166. /**
  167. * Slices data set.
  168. *
  169. * @param int $offset Offset (as for PHP's array_slice())
  170. * @param int $length Number of elements (as for PHP's array_slice())
  171. */
  172. public function slice($offset, $length)
  173. {
  174. $data = array_slice($this->get(), $offset, $length);
  175. $this->index = $data;
  176. $this->meta['count'] = count($data);
  177. }
  178. /**
  179. * Filters data set. Removes elements not listed in $ids list.
  180. *
  181. * @param array $ids List of IDs to keep.
  182. */
  183. public function intersect($ids = array())
  184. {
  185. // not implemented
  186. }
  187. /**
  188. * Return all messages in the result.
  189. *
  190. * @return array List of message IDs
  191. */
  192. public function get()
  193. {
  194. return $this->index;
  195. }
  196. /**
  197. * Return all messages in the result in compressed form
  198. *
  199. * @return string List of message IDs in compressed form
  200. */
  201. public function get_compressed()
  202. {
  203. return '';
  204. }
  205. /**
  206. * Return result element at specified index
  207. *
  208. * @param int|string $index Element's index or "FIRST" or "LAST"
  209. *
  210. * @return int Element value
  211. */
  212. public function get_element($idx)
  213. {
  214. switch ($idx) {
  215. case 'FIRST': return $this->index[0];
  216. case 'LAST': return end($this->index);
  217. default: return $this->index[$idx];
  218. }
  219. }
  220. /**
  221. * Returns response parameters, e.g. ESEARCH's MIN/MAX/COUNT/ALL/MODSEQ
  222. * or internal data e.g. MAILBOX, ORDER
  223. *
  224. * @param string $param Parameter name
  225. *
  226. * @return array|string Response parameters or parameter value
  227. */
  228. public function get_parameters($param=null)
  229. {
  230. $params = array(
  231. 'SORT' => $this->sorting,
  232. 'ORDER' => $this->order,
  233. 'MAILBOX' => $this->folders,
  234. );
  235. if ($param !== null) {
  236. return $params[$param];
  237. }
  238. return $params;
  239. }
  240. /**
  241. * Returns the stored result object for a particular folder
  242. *
  243. * @param string $folder Folder name
  244. *
  245. * @return false|object rcube_result_* instance of false if none found
  246. */
  247. public function get_set($folder)
  248. {
  249. foreach ($this->sets as $set) {
  250. if ($set->get_parameters('MAILBOX') == $folder) {
  251. return $set;
  252. }
  253. }
  254. return false;
  255. }
  256. /**
  257. * Returns length of internal data representation
  258. *
  259. * @return int Data length
  260. */
  261. protected function length()
  262. {
  263. return $this->count();
  264. }
  265. /* Serialize magic methods */
  266. public function __sleep()
  267. {
  268. $this->sdata = array('incomplete' => array(), 'error' => array());
  269. foreach ($this->sets as $set) {
  270. if ($set->incomplete) {
  271. $this->sdata['incomplete'][] = $set->get_parameters('MAILBOX');
  272. }
  273. else if ($set->is_error()) {
  274. $this->sdata['error'][] = $set->get_parameters('MAILBOX');
  275. }
  276. }
  277. return array('sdata', 'index', 'folders', 'sorting', 'order');
  278. }
  279. public function __wakeup()
  280. {
  281. $this->meta = array('count' => count($this->index));
  282. $this->incomplete = count($this->sdata['incomplete']) > 0;
  283. // restore result sets from saved index
  284. $data = array();
  285. foreach ($this->index as $item) {
  286. list($uid, $folder) = explode('-', $item, 2);
  287. $data[$folder] .= ' ' . $uid;
  288. }
  289. foreach ($this->folders as $folder) {
  290. if (in_array($folder, $this->sdata['error'])) {
  291. $data_str = null;
  292. }
  293. else {
  294. $data_str = '* SORT' . $data[$folder];
  295. }
  296. $set = new rcube_result_index($folder, $data_str, strtoupper($this->order));
  297. if (in_array($folder, $this->sdata['incomplete'])) {
  298. $set->incomplete = true;
  299. }
  300. $this->sets[] = $set;
  301. }
  302. }
  303. }