summaryrefslogtreecommitdiff
path: root/program/lib/Roundcube/rcube_result_multifolder.php
blob: 786ee85f65a842c89adf8e104c5c3f533d19a228 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php

/*
 +-----------------------------------------------------------------------+
 | This file is part of the Roundcube Webmail client                     |
 | Copyright (C) 2005-2011, The Roundcube Dev Team                       |
 | Copyright (C) 2011, Kolab Systems AG                                  |
 |                                                                       |
 | Licensed under the GNU General Public License version 3 or            |
 | any later version with exceptions for skins & plugins.                |
 | See the README file for a full license statement.                     |
 |                                                                       |
 | PURPOSE:                                                              |
 |   SORT/SEARCH/ESEARCH response handler                                |
 +-----------------------------------------------------------------------+
 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
 +-----------------------------------------------------------------------+
*/

/**
 * Class holding a set of rcube_result_index instances that together form a
 * result set of a multi-folder search
 *
 * @package    Framework
 * @subpackage Storage
 */
class rcube_result_multifolder
{
    public $multi      = true;
    public $sets       = array();
    public $incomplete = false;
    public $folder;

    protected $meta    = array();
    protected $index   = array();
    protected $folders = array();
    protected $order   = 'ASC';
    protected $sorting;


    /**
     * Object constructor.
     */
    public function __construct($folders = array())
    {
        $this->folders = $folders;
        $this->meta    = array('count' => 0);
    }


    /**
     * Initializes object with SORT command response
     *
     * @param string $data IMAP response string
     */
    public function add($result)
    {
        $this->sets[] = $result;

        if ($result->count()) {
            $this->append_result($result);
        }
        else if ($result->incomplete) {
            $this->incomplete = true;
        }
    }

    /**
     * Append message UIDs from the given result to our index
     */
    protected function append_result($result)
    {
        $this->meta['count'] += $result->count();

        // append UIDs to global index
        $folder = $result->get_parameters('MAILBOX');
        $index  = array_map(function($uid) use ($folder) { return $uid . '-' . $folder; }, $result->get());

        $this->index = array_merge($this->index, $index);
    }

    /**
     * Store a global index of (sorted) message UIDs
     */
    public function set_message_index($headers, $sort_field, $sort_order)
    {
        $this->index = array();
        foreach ($headers as $header) {
            $this->index[] = $header->uid . '-' . $header->folder;
        }

        $this->sorting = $sort_field;
        $this->order   = $sort_order;
    }

    /**
     * Checks the result from IMAP command
     *
     * @return bool True if the result is an error, False otherwise
     */
    public function is_error()
    {
        return false;
    }


    /**
     * Checks if the result is empty
     *
     * @return bool True if the result is empty, False otherwise
     */
    public function is_empty()
    {
        return empty($this->sets) || $this->meta['count'] == 0;
    }


    /**
     * Returns number of elements in the result
     *
     * @return int Number of elements
     */
    public function count()
    {
        return $this->meta['count'];
    }


    /**
     * Returns number of elements in the result.
     * Alias for count() for compatibility with rcube_result_thread
     *
     * @return int Number of elements
     */
    public function count_messages()
    {
        return $this->count();
    }


    /**
     * Reverts order of elements in the result
     */
    public function revert()
    {
        $this->order = $this->order == 'ASC' ? 'DESC' : 'ASC';
        $this->index = array();

        // revert order in all sub-sets
        foreach ($this->sets as $set) {
            if ($this->order != $set->get_parameters('ORDER')) {
                $set->revert();
            }

            $folder = $set->get_parameters('MAILBOX');
            $index  = array_map(function($uid) use ($folder) { return $uid . '-' . $folder; }, $set->get());

            $this->index = array_merge($this->index, $index);
        }
    }


    /**
     * Check if the given message ID exists in the object
     *
     * @param int  $msgid     Message ID
     * @param bool $get_index When enabled element's index will be returned.
     *                        Elements are indexed starting with 0
     * @return mixed False if message ID doesn't exist, True if exists or
     *               index of the element if $get_index=true
     */
    public function exists($msgid, $get_index = false)
    {
        if (!empty($this->folder)) {
            $msgid .= '-' . $this->folder;
        }

        return array_search($msgid, $this->index);
    }


    /**
     * Filters data set. Removes elements listed in $ids list.
     *
     * @param array $ids List of IDs to remove.
     * @param string $folder IMAP folder
     */
    public function filter($ids = array(), $folder = null)
    {
        $this->meta['count'] = 0;
        foreach ($this->sets as $set) {
            if ($set->get_parameters('MAILBOX') == $folder) {
                $set->filter($ids);
            }

            $this->meta['count'] += $set->count();
        }
    }

    /**
     * Slices data set.
     *
     * @param $offset Offset (as for PHP's array_slice())
     * @param $length Number of elements (as for PHP's array_slice())
     *
     */
    public function slice($offset, $length)
    {
        $data = array_slice($this->get(), $offset, $length);

        $this->index = $data;
        $this->meta['count'] = count($data);
    }

    /**
     * Filters data set. Removes elements not listed in $ids list.
     *
     * @param array $ids List of IDs to keep.
     */
    public function intersect($ids = array())
    {
        // not implemented
    }

    /**
     * Return all messages in the result.
     *
     * @return array List of message IDs
     */
    public function get()
    {
        return $this->index;
    }


    /**
     * Return all messages in the result.
     *
     * @return array List of message IDs
     */
    public function get_compressed()
    {
        return '';
    }


    /**
     * Return result element at specified index
     *
     * @param int|string  $index  Element's index or "FIRST" or "LAST"
     *
     * @return int Element value
     */
    public function get_element($idx)
    {
        switch ($idx) {
            case 'FIRST': return $this->index[0];
            case 'LAST':  return end($this->index);
            default:      return $this->index[$idx];
        }
    }


    /**
     * Returns response parameters, e.g. ESEARCH's MIN/MAX/COUNT/ALL/MODSEQ
     * or internal data e.g. MAILBOX, ORDER
     *
     * @param string $param  Parameter name
     *
     * @return array|string Response parameters or parameter value
     */
    public function get_parameters($param=null)
    {
        $params = array(
            'SORT'    => $this->sorting,
            'ORDER'   => $this->order,
            'MAILBOX' => $this->folders,
        );

        if ($param !== null) {
            return $params[$param];
        }

        return $params;
    }

    /**
     * Returns the stored result object for a particular folder
     *
     * @param string $folder  Folder name
     * @return false|obejct rcube_result_* instance of false if none found
     */
    public function get_set($folder)
    {
        foreach ($this->sets as $set) {
            if ($set->get_parameters('MAILBOX') == $folder) {
                return $set;
            }
        }

        return false;
    }

    /**
     * Returns length of internal data representation
     *
     * @return int Data length
     */
    protected function length()
    {
        return $this->count();
    }


    /* Serialize magic methods */

    public function __sleep()
    {
        return array('sets','folders','sorting','order');
    }

    public function __wakeup()
    {
        // restore index from saved result sets
        $this->meta = array('count' => 0);

        foreach ($this->sets as $result) {
            if ($result->count()) {
                $this->append_result($result);
            }
            else if ($result->incomplete) {
                $this->incomplete = true;
            }
        }
    }

}