ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.ilUserSearchCache.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27{
28 public const int DEFAULT_SEARCH = 0;
29 public const int LUCENE_DEFAULT = 5;
30
31 public const int LAST_QUERY = 7;
32
33 public const int LUCENE_USER_SEARCH = 8;
34
35 private static ?ilUserSearchCache $instance = null;
36 protected ilDBInterface $db;
37
38 private int $usr_id;
40
41 private array $search_result = [];
42 private array $checked = [];
43 private array $failed = [];
44 private int $page_number = 1;
45
49 private $query;
50 private int $root;
51 private array $item_filter = [];
52 private bool $isAnonymous = false;
53 private array $mime_filter = [];
54 private array $creation_filter = [];
55 private array $copyright_filter = [];
56
57
58
65 private function __construct(int $a_usr_id)
66 {
67 global $DIC;
68
69 $this->db = $DIC->database();
70
71 if ($a_usr_id == ANONYMOUS_USER_ID) {
72 $this->isAnonymous = true;
73 }
74
75 $this->root = ROOT_FOLDER_ID;
76 $this->usr_id = $a_usr_id;
77 $this->search_type = self::DEFAULT_SEARCH;
78 $this->read();
79 }
80
81 public static function _getInstance(int $a_usr_id): ilUserSearchCache
82 {
83 if (self::$instance instanceof ilUserSearchCache) {
84 return self::$instance;
85 }
86 return self::$instance = new ilUserSearchCache($a_usr_id);
87 }
88
93 public function isAnonymous(): bool
94 {
95 return $this->isAnonymous;
96 }
97
102 public function switchSearchType(int $a_type): bool
103 {
104 $this->search_type = $a_type;
105 $this->read();
106 return true;
107 }
108
115 public function getResults(): array
116 {
117 return $this->search_result ?: [];
118 }
119
127 public function setResults(array $a_results): void
128 {
129 $this->search_result = $a_results;
130 }
131
139 public function addResult(array $a_result_item): bool
140 {
141 $this->search_result[$a_result_item['ref_id']]['ref_id'] = $a_result_item['ref_id'];
142 $this->search_result[$a_result_item['ref_id']]['obj_id'] = $a_result_item['obj_id'];
143 $this->search_result[$a_result_item['ref_id']]['type'] = $a_result_item['type'];
144 return true;
145 }
146
150 public function appendToFailed(int $a_ref_id): void
151 {
152 $this->failed[$a_ref_id] = $a_ref_id;
153 }
154
158 public function isFailed(int $a_ref_id): bool
159 {
160 return in_array($a_ref_id, $this->failed);
161 }
162
163 public function appendToChecked(int $a_ref_id, int $a_obj_id): void
164 {
165 $this->checked[$a_ref_id] = $a_obj_id;
166 }
167
168 public function isChecked(int $a_ref_id): bool
169 {
170 return array_key_exists($a_ref_id, $this->checked) and $this->checked[$a_ref_id];
171 }
172
178 public function getCheckedItems(): array
179 {
180 return $this->checked ?: [];
181 }
182
189 public function setResultPageNumber(int $a_number): void
190 {
191 if ($a_number) {
192 $this->page_number = $a_number;
193 }
194 }
195
199 public function getResultPageNumber(): int
200 {
201 return $this->page_number ?: 1;
202 }
203
204 public function setQuery(string $a_query): void
205 {
206 $this->query = $a_query;
207 }
208
209 public function getQuery(): string
210 {
211 return $this->query ?? '';
212 }
213
217 public function getUrlEncodedQuery(): string
218 {
219 return urlencode(str_replace('"', '.', $this->getQuery()));
220 }
221
225 public function setRoot(int $a_root): void
226 {
227 $this->root = $a_root;
228 }
229
230 public function getRoot(): int
231 {
232 return $this->root ?: ROOT_FOLDER_ID;
233 }
234
235 public function setItemFilter(array $a_filter): void
236 {
237 $this->item_filter = $a_filter;
238 }
239
240 public function getItemFilter(): array
241 {
242 return $this->item_filter;
243 }
244
245 public function setMimeFilter(array $a_filter): void
246 {
247 $this->mime_filter = $a_filter;
248 }
249
250 public function getMimeFilter(): array
251 {
252 return $this->mime_filter;
253 }
254
255 public function setCreationFilter(array $a_filter): void
256 {
257 $this->creation_filter = $a_filter;
258 }
259
260 public function getCreationFilter(): array
261 {
263 }
264
265 public function setCopyrightFilter(string ...$copyright_identifiers): void
266 {
267 $this->copyright_filter = $copyright_identifiers;
268 }
269
273 public function getCopyrightFilter(): array
274 {
276 }
277
278 public function deleteCachedEntries(): void
279 {
280 if ($this->isAnonymous()) {
282 return;
283 }
284 $query = "SELECT COUNT(*) num FROM usr_search " .
285 "WHERE usr_id = " . $this->db->quote($this->usr_id, 'integer') . " " .
286 "AND search_type = " . $this->db->quote($this->search_type, 'integer');
287 $res = $this->db->query($query);
288 $row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT);
289
290 if ($row->num > 0) {
291 $this->db->update(
292 'usr_search',
293 [
294 'search_result' => ['clob', serialize([0])],
295 'checked' => ['clob', serialize([0])],
296 'failed' => ['clob', serialize([0])],
297 'page' => ['integer', 0]
298 ],
299 [
300 'usr_id' => ['integer', $this->usr_id],
301 'search_type' => ['integer', $this->search_type]
302 ]
303 );
304 } else {
305 $this->db->insert(
306 'usr_search',
307 [
308 'search_result' => ['clob', serialize([0])],
309 'checked' => ['clob', serialize([0])],
310 'failed' => ['clob', serialize([0])],
311 'page' => ['integer', 0],
312 'usr_id' => ['integer', $this->usr_id],
313 'search_type' => ['integer', $this->search_type],
314 'query' => ['clob', serialize('')]
315 ]
316 );
317 }
318
319 $this->setResultPageNumber(1);
320 $this->search_result = [];
321 $this->checked = [];
322 $this->failed = [];
323 }
324
325 public function deleteCachedEntriesAnonymous(): bool
326 {
327 $this->setResultPageNumber(1);
328 $this->search_result = [];
329 $this->checked = [];
330 $this->failed = [];
331
332 return true;
333 }
334
335 public function delete(): bool
336 {
337 $query = "DELETE FROM usr_search " .
338 "WHERE usr_id = " . $this->db->quote($this->usr_id, 'integer') . " " .
339 "AND search_type = " . $this->db->quote($this->search_type, 'integer');
340 $res = $this->db->manipulate($query);
341
342 $this->read();
343 return true;
344 }
345
346 public function save(): void
347 {
348 if ($this->isAnonymous()) {
349 $this->saveForAnonymous();
350 return;
351 }
352
353 $query = "DELETE FROM usr_search " .
354 "WHERE usr_id = " . $this->db->quote($this->usr_id, 'integer') . " " .
355 "AND ( search_type = " . $this->db->quote($this->search_type, 'integer') . ' ' .
356 "OR search_type = " . $this->db->quote(self::LAST_QUERY, 'integer') . ')';
357 $res = $this->db->manipulate($query);
358
359 $this->db->insert('usr_search', [
360 'usr_id' => ['integer', $this->usr_id],
361 'search_result' => ['clob', serialize($this->search_result)],
362 'checked' => ['clob', serialize($this->checked)],
363 'failed' => ['clob', serialize($this->failed)],
364 'page' => ['integer', $this->page_number],
365 'search_type' => ['integer', $this->search_type],
366 'query' => ['clob', serialize($this->getQuery())],
367 'root' => ['integer', $this->getRoot()],
368 'item_filter' => ['text', serialize($this->getItemFilter())],
369 'mime_filter' => ['text', serialize($this->getMimeFilter())],
370 'creation_filter' => ['text', serialize($this->getCreationFilter())],
371 'copyright_filter' => ['text', serialize($this->getCopyrightFilter())]
372 ]);
373
374
375 // Write last query information
376 $this->db->insert(
377 'usr_search',
378 [
379 'usr_id' => ['integer', $this->usr_id],
380 'search_type' => ['integer', self::LAST_QUERY],
381 'query' => ['text', serialize($this->getQuery())]
382 ]
383 );
384 }
385
386 public function saveForAnonymous(): void
387 {
388 ilSession::clear('usr_search_cache');
389 $session_usr_search = [];
390 $session_usr_search[$this->search_type]['search_result'] = $this->search_result;
391 $session_usr_search[$this->search_type]['checked'] = $this->checked;
392 $session_usr_search[$this->search_type]['failed'] = $this->failed;
393 $session_usr_search[$this->search_type]['page'] = $this->page_number;
394 $session_usr_search[$this->search_type]['query'] = $this->getQuery();
395 $session_usr_search[$this->search_type]['root'] = $this->getRoot();
396 $session_usr_search[$this->search_type]['item_filter'] = $this->getItemFilter();
397 $session_usr_search[$this->search_type]['mime_filter'] = $this->getMimeFilter();
398 $session_usr_search[$this->search_type]['creation_filter'] = $this->getCreationFilter();
399 $session_usr_search[$this->search_type]['copyright_filter'] = $this->getCopyrightFilter();
400 $session_usr_search[self::LAST_QUERY]['query'] = $this->getQuery();
401 ilSession::set('usr_search_cache', $session_usr_search);
402 }
403
404 private function read(): void
405 {
406 $this->failed = [];
407 $this->checked = [];
408 $this->search_result = [];
409 $this->page_number = 0;
410
411 if ($this->isAnonymous()) {
412 $this->readAnonymous();
413 return;
414 }
415
416 $query = "SELECT * FROM usr_search " .
417 "WHERE usr_id = " . $this->db->quote($this->usr_id, 'integer') . " " .
418 "AND search_type = " . $this->db->quote($this->search_type, 'integer');
419
420 $res = $this->db->query($query);
421 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
422 $this->search_result = (array) unserialize((string) $row->search_result);
423 if (strlen((string) $row->checked)) {
424 $this->checked = (array) unserialize((string) $row->checked);
425 }
426 if (strlen((string) $row->failed)) {
427 $this->failed = (array) unserialize((string) $row->failed);
428 }
429 $this->page_number = (int) $row->page;
430 $this->setQuery(unserialize((string) $row->query));
431 $this->setRoot((int) $row->root);
432 $this->setItemFilter((array) unserialize((string) $row->item_filter));
433 $this->setCreationFilter((array) unserialize((string) $row->creation_filter));
434 if ($row->copyright_filter !== null) {
435 $this->setCopyrightFilter(...(array) unserialize((string) $row->copyright_filter));
436 }
437 }
438 }
439
443 private function readAnonymous(): void
444 {
445 $usr_search_cache = ilSession::get('usr_search_cache') ?? [];
446
447 $this->search_result = (array) ($usr_search_cache[$this->search_type]['search_result'] ?? []);
448 $this->checked = (array) ($usr_search_cache[$this->search_type]['checked'] ?? []);
449 $this->failed = (array) ($usr_search_cache[$this->search_type]['failed'] ?? []);
450 $this->page_number = (int) ($usr_search_cache[$this->search_type]['page_number'] ?? 1);
451 $this->setQuery((string) ($usr_search_cache[$this->search_type]['query'] ?? ''));
452 $this->setRoot((int) ($usr_search_cache[$this->search_type]['root'] ?? ROOT_FOLDER_ID));
453 $this->setItemFilter((array) ($usr_search_cache[$this->search_type]['item_filter'] ?? []));
454 $this->setMimeFilter((array) ($usr_search_cache[$this->search_type]['mime_filter'] ?? []));
455 $this->setCreationFilter((array) ($usr_search_cache[$this->search_type]['creation_filter'] ?? []));
456 $this->setCopyrightFilter(...(array) ($usr_search_cache[$this->search_type]['copyright_filter'] ?? []));
457 }
458}
static get(string $a_var)
static clear(string $a_var)
static set(string $a_var, $a_val)
Set a value.
Class for storing search result.
appendToChecked(int $a_ref_id, int $a_obj_id)
setCreationFilter(array $a_filter)
readAnonymous()
Read from session for anonymous user.
setItemFilter(array $a_filter)
getCheckedItems()
Get all checked items @access public.
getResultPageNumber()
get result page number
setResults(array $a_results)
Set results.
isFailed(int $a_ref_id)
check if reference has failed access
addResult(array $a_result_item)
Append result.
setMimeFilter(array $a_filter)
getUrlEncodedQuery()
Urlencode query for further use in e.g glossariers (highlighting off search terms).
setRoot(int $a_root)
set root node of search
switchSearchType(int $a_type)
switch to search type reads entries from database
static _getInstance(int $a_usr_id)
static ilUserSearchCache $instance
appendToFailed(int $a_ref_id)
Append failed id.
__construct(int $a_usr_id)
Constructor.
setResultPageNumber(int $a_number)
Set result page number.
isAnonymous()
Check if current user is anonymous user.
setCopyrightFilter(string ... $copyright_identifiers)
const ANONYMOUS_USER_ID
Definition: constants.php:27
const ROOT_FOLDER_ID
Definition: constants.php:32
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26