aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/FilesSearch/Result.php
blob: c4cfb1f13b96a79a2cb8db6184b2d67b0a0cf491 (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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php

namespace FilesSearch;

/**
 * This class is responsible for fetching and representing the results
 * of a search using a Query instance.
 *
 * @license GPL2 or any later version
 *
 * @since   Stud.IP 4.1
 */
class Result
{
    // how many search items are prefetched to decrease number of
    // database selects
    const FETCH_BUFFER_SIZE = 30;

    private $query;

    private $count = 0;
    private $rawCount;
    private $resultPage;
    private $rowsSeen = 0;

    private $statement;

    private $fetchBuffer = [];
    private $fetchBufferDone = false;
    private $typedFoldersBuffer = [];

    /**
     * Creates a new Result object using a Query and fetching results
     * from the database.
     *
     * @param Query $query the search query to use
     */
    public function __construct(Query $query)
    {
        $this->query = $query;
        $this->search();
    }

    /**
     * Returns the current result page. The Query object associated
     * with this instance is used to determince the slice of the
     * results to be used as a page.
     *
     * @return array an array of search results
     */
    public function getResultPage()
    {
        if (!isset($this->resultPage)) {
            $this->resultPage = $this->fetchResultPage();
        }

        return $this->resultPage;
    }

    private function fetchResultPage()
    {
        if (!$this->statement) {
            throw new \RuntimeException();
        }

        $this->drop($this->query->getOffset());
        $results = $this->take($this->query->getResultsPerPage());

        $fileRefs = $this->findFileRefs($results);

        return array_map(
            function ($result) use ($fileRefs) {
                if (isset($fileRefs[$result['id']])) {
                    $result['fileRef'] = $fileRefs[$result['id']];
                }

                return $result;
            },
            $results
        );
    }

    /**
     * Returns whether there COULD be more results.
     *
     * @return bool true if there could be more results; false if
     *              all possible matches were already evaluated
     */
    public function hasMore()
    {
        return $this->rowsSeen < $this->rawCount;
    }

    /**
     * If we already evaluated all possible matches, returns the total
     * number of all actual matches that the user may see.
     *
     * @return int the number of all actual matches that the user may
     *             see
     *
     * @throws RuntimeException if there could be more results
     *
     * @see hasMore
     */
    public function getTotal()
    {
        if ($this->hasMore()) {
            throw new \LogicException('You must exhaust the search before calling `Result::getTotal`.');
        }

        return $this->count;
    }

    private function search()
    {
        $this->statement = $this->getStatement();
        $this->rawCount = $this->statement->rowCount();
    }

    private function findFileRefs(array $results)
    {
        return array_reduce(
            \FileRef::findMany(
                array_map(
                    function ($result) {
                        return $result['id'];
                    },
                    $results
                )
            ),
            function ($memo, $fileRef) {
                $memo[$fileRef->id] = $fileRef;

                return $memo;
            },
            []
        );
    }

    private function drop($num)
    {
        for ($i = 0; $i < $num; ++$i) {
            if (false === $this->next()) {
                break;
            }
        }
    }

    private function take($num)
    {
        $result = [];
        for ($i = 0; $i < $num; ++$i) {
            if (($object = $this->next()) === false) {
                break;
            }
            $result[] = $object;
        }

        return $result;
    }

    private function next()
    {
        while ($object = $this->fetch()) {
            if (isset($this->typedFoldersBuffer[$object['folder_id']])) {
                $folder = $this->typedFoldersBuffer[$object['folder_id']];
            } elseif (!$folder = $this->getTypedFolder($object['folder_id'])) {
                continue;
            }

            if ($this->checkPermission($folder)) {
                ++$this->count;

                $object['folder'] = $folder;

                return $object;
            }
        }

        return false;
    }

    private function fetch()
    {
        if (!$this->fetchBufferDone && !count($this->fetchBuffer)) {
            $this->fetchBuffer = [];
            for ($i = 0; $i < self::FETCH_BUFFER_SIZE; ++$i) {
                $next = $this->statement->fetch();
                if ($next) {
                    $this->fetchBuffer[] = $next;
                } else {
                    $this->fetchBufferDone = true;
                    break;
                }
            }

            $this->typedFoldersBuffer = $this->prefetchTypedFolders();
        }

        if ($result = array_shift($this->fetchBuffer)) {
            ++$this->rowsSeen;
        }

        return $result;
    }

    private function prefetchTypedFolders()
    {
        return array_reduce(
            $this->getFoldersFromFetchBuffer(),
            function ($memo, $folder) {
                if ($folder) {
                    $memo[$folder->id] = $folder->getTypedFolder();
                }

                return $memo;
            },
            []
        );
    }

    private function getFoldersFromFetchBuffer()
    {
        return \Folder::findMany(
            array_unique(
                array_map(
                    function ($row) {
                        return $row['folder_id'];
                    },
                        $this->fetchBuffer
                )
            )
        );
    }

    private function getTypedFolder($folderId)
    {
        $sormFolder = \Folder::find($folderId);

        return $sormFolder ? $sormFolder->getTypedFolder() : null;
    }

    private function checkPermission($folder)
    {
        $userId = $this->getUserId();

        return $folder->isVisible($userId) && $folder->isReadable($userId);
    }

    private function getStatement()
    {
        list($query, $params) = $this->getSqlQueryAndParams();
        $statement = \DBManager::get()->prepare($query);
        foreach ($params as $key => $value) {
            $statement->bindValue($key, $value);
        }

        $statement->execute();
        $statement->setFetchMode(\PDO::FETCH_ASSOC);

        return $statement;
    }

    private function getSqlQueryAndParams()
    {
        list($whereSql, $whereParams) = $this->getWhereCondition();

        return [
            implode(' ', [$this->getBaseSqlQuery(), $whereSql, $this->getOrderBy()]),
            array_merge(
                [
                    ':query' => $this->getSearchPhrase(),
                ],
                $whereParams
            ),
        ];
    }

    private function getBaseSqlQuery()
    {
        $ftQuery = '
            SELECT
                file_ref_id,
                text,
                relevance,
                SUM(MATCH (text) AGAINST (:query IN BOOLEAN MODE) * relevance) as ranking
            FROM files_search_index
            WHERE MATCH (text) AGAINST (:query IN BOOLEAN MODE)
            GROUP BY file_ref_id';

        return '
            SELECT sr.text, sr.ranking, fsa.id, fsa.folder_id, fsa.semester_start, fsa.semester_end, fsa.file_ref_mkdate
            FROM ( '.$ftQuery.' ) as sr
            JOIN files_search_attributes fsa
            ON (file_ref_id = fsa.id)';
    }

    private function getSearchPhrase()
    {
        $words = explode(' ', $this->query->getQuery());

        return implode('* ', array_merge($words, ['\''.$this->query->getQuery().'\'']));
    }

    private function getWhereCondition()
    {
        $filter = $this->query->getFilter();
        $withCategory = $filter->getCategory() !== null
                      && !empty(trim($filter->getCategory()));
        $withSemester = $filter->getSemester() !== null;

        $sql = [];
        $params = [];

        if ($withCategory) {
            $sql[] = 'fsa.folder_range_type = :category';
            $params[':category'] = $filter->getCategory();
        }

        if ($withSemester) {
            $sql[] = '
              IF(
                semester_start,
                IF(
                  semester_end,
                  :semesterstart BETWEEN semester_start AND semester_end,
                  semester_start <= :semesterstart
                ),
                file_ref_mkdate BETWEEN :semesterstart AND :semesterend
              )';
            $semester = $filter->getSemester();
            $params[':semesterstart'] = $semester->beginn;
            $params[':semesterend'] = $semester->ende;
        }

        if (count($sql)) {
            $sql = 'WHERE '.join(' AND ', $sql).' ';
        } else {
            $sql = '';
        }

        return [$sql, $params];
    }

    private function getOrderBy()
    {
        switch ($this->query->getSort()) {
            case QUERY::SORT_CHDATE:
                $orderBy = 'ORDER BY fsa.file_ref_mkdate DESC';
                break;

            case QUERY::SORT_RELEVANCE:
                $orderBy = 'ORDER BY sr.ranking DESC';
                break;

            default:
                $orderBy = '';
        }

        return $orderBy;
    }

    private function getUserId()
    {
        return $this->query->getUser()->id;
    }
}