ILIAS  release_8 Revision v8.24
class.ilUserSearchCache.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4/*
5 +-----------------------------------------------------------------------------+
6 | ILIAS open source |
7 +-----------------------------------------------------------------------------+
8 | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
9 | |
10 | This program is free software; you can redistribute it and/or |
11 | modify it under the terms of the GNU General Public License |
12 | as published by the Free Software Foundation; either version 2 |
13 | of the License, or (at your option) any later version. |
14 | |
15 | This program is distributed in the hope that it will be useful, |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | GNU General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU General Public License |
21 | along with this program; if not, write to the Free Software |
22 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23 +-----------------------------------------------------------------------------+
24*/
25
37{
38 public const DEFAULT_SEARCH = 0;
39 public const ADVANCED_SEARCH = 1;
40 public const ADVANCED_MD_SEARCH = 4;
41 public const LUCENE_DEFAULT = 5;
42 public const LUCENE_ADVANCED = 6;
43
44 public const LAST_QUERY = 7;
45
46 public const LUCENE_USER_SEARCH = 8;
47
48 private static ?ilUserSearchCache $instance = null;
49 protected ilDBInterface $db;
50
51 private int $usr_id;
53
54 private array $search_result = array();
55 private array $checked = array();
56 private array $failed = array();
57 private int $page_number = 1;
58
62 private $query;
63 private int $root;
64 private array $item_filter = array();
65 private bool $isAnonymous = false;
66 private array $mime_filter = array();
67 private array $creation_filter = array();
68
69
70
77 private function __construct(int $a_usr_id)
78 {
79 global $DIC;
80
81 $this->db = $DIC->database();
82
83 if ($a_usr_id == ANONYMOUS_USER_ID) {
84 $this->isAnonymous = true;
85 }
86
87 $this->root = ROOT_FOLDER_ID;
88 $this->usr_id = $a_usr_id;
89 $this->search_type = self::DEFAULT_SEARCH;
90 $this->read();
91 }
92
93 public static function _getInstance(int $a_usr_id): ilUserSearchCache
94 {
95 if (self::$instance instanceof ilUserSearchCache) {
96 return self::$instance;
97 }
98 return self::$instance = new ilUserSearchCache($a_usr_id);
99 }
100
105 public function isAnonymous(): bool
106 {
107 return $this->isAnonymous;
108 }
109
114 public function switchSearchType(int $a_type): bool
115 {
116 $this->search_type = $a_type;
117 $this->read();
118 return true;
119 }
120
127 public function getResults(): array
128 {
129 return $this->search_result ?: array();
130 }
131
139 public function setResults(array $a_results): void
140 {
141 $this->search_result = $a_results;
142 }
143
151 public function addResult(array $a_result_item): bool
152 {
153 $this->search_result[$a_result_item['ref_id']]['ref_id'] = $a_result_item['ref_id'];
154 $this->search_result[$a_result_item['ref_id']]['obj_id'] = $a_result_item['obj_id'];
155 $this->search_result[$a_result_item['ref_id']]['type'] = $a_result_item['type'];
156 return true;
157 }
158
162 public function appendToFailed(int $a_ref_id): void
163 {
164 $this->failed[$a_ref_id] = $a_ref_id;
165 }
166
170 public function isFailed(int $a_ref_id): bool
171 {
172 return in_array($a_ref_id, $this->failed);
173 }
174
183 public function appendToChecked(int $a_ref_id, int $a_obj_id): void
184 {
185 $this->checked[$a_ref_id] = $a_obj_id;
186 }
187
195 public function isChecked(int $a_ref_id): bool
196 {
197 return array_key_exists($a_ref_id, $this->checked) and $this->checked[$a_ref_id];
198 }
199
205 public function getCheckedItems(): array
206 {
207 return $this->checked ?: array();
208 }
209
216 public function setResultPageNumber(int $a_number): void
217 {
218 if ($a_number) {
219 $this->page_number = $a_number;
220 }
221 }
222
226 public function getResultPageNumber(): int
227 {
228 return $this->page_number ?: 1;
229 }
230
236 public function setQuery($a_query): void
237 {
238 $this->query = $a_query;
239 }
240
244 public function getQuery()
245 {
246 if (is_array($this->query)) {
247 return $this->query;
248 }
249 return $this->query ?? '';
250 }
251
255 public function getUrlEncodedQuery(): string
256 {
257 if (is_array($this->getQuery())) {
258 $query = $this->getQuery();
259
260 return urlencode(str_replace('"', '.', $query['lom_content']));
261 }
262 return urlencode(str_replace('"', '.', $this->getQuery()));
263 }
264
268 public function setRoot(int $a_root): void
269 {
270 $this->root = $a_root;
271 }
272
277 public function getRoot(): int
278 {
279 return $this->root ?: ROOT_FOLDER_ID;
280 }
281
282 public function setItemFilter(array $a_filter): void
283 {
284 $this->item_filter = $a_filter;
285 }
286
287 public function getItemFilter(): array
288 {
289 return $this->item_filter;
290 }
291
292 public function setMimeFilter(array $a_filter): void
293 {
294 $this->mime_filter = $a_filter;
295 }
296
297 public function getMimeFilter(): array
298 {
299 return $this->mime_filter;
300 }
301
302 public function setCreationFilter(array $a_filter): void
303 {
304 $this->creation_filter = $a_filter;
305 }
306
307 public function getCreationFilter(): array
308 {
310 }
311
312
316 public function deleteCachedEntries(): void
317 {
318 if ($this->isAnonymous()) {
320 return;
321 }
322 $query = "SELECT COUNT(*) num FROM usr_search " .
323 "WHERE usr_id = " . $this->db->quote($this->usr_id, 'integer') . " " .
324 "AND search_type = " . $this->db->quote($this->search_type, 'integer');
325 $res = $this->db->query($query);
326 $row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT);
327
328 if ($row->num > 0) {
329 $this->db->update(
330 'usr_search',
331 array(
332 'search_result' => array('clob',serialize(array(0))),
333 'checked' => array('clob',serialize(array(0))),
334 'failed' => array('clob',serialize(array(0))),
335 'page' => array('integer',0)),
336 array(
337 'usr_id' => array('integer', $this->usr_id),
338 'search_type' => array('integer', $this->search_type)
339 )
340 );
341 } else {
342 $this->db->insert(
343 'usr_search',
344 array(
345 'search_result' => array('clob',serialize(array(0))),
346 'checked' => array('clob',serialize(array(0))),
347 'failed' => array('clob',serialize(array(0))),
348 'page' => array('integer',0),
349 'usr_id' => array('integer', $this->usr_id),
350 'search_type' => array('integer', $this->search_type)
351 )
352 );
353 }
354
355 $this->setResultPageNumber(1);
356 $this->search_result = array();
357 $this->checked = array();
358 $this->failed = array();
359 }
360
365 public function deleteCachedEntriesAnonymous(): bool
366 {
367 $this->setResultPageNumber(1);
368 $this->search_result = array();
369 $this->checked = array();
370 $this->failed = array();
371
372 return true;
373 }
374
375
376
377 public function delete(): bool
378 {
379 $query = "DELETE FROM usr_search " .
380 "WHERE usr_id = " . $this->db->quote($this->usr_id, 'integer') . " " .
381 "AND search_type = " . $this->db->quote($this->search_type, 'integer');
382 $res = $this->db->manipulate($query);
383
384 $this->read();
385 return true;
386 }
387
388 public function save(): void
389 {
390 if ($this->isAnonymous()) {
391 $this->saveForAnonymous();
392 return;
393 }
394
395 $query = "DELETE FROM usr_search " .
396 "WHERE usr_id = " . $this->db->quote($this->usr_id, 'integer') . " " .
397 "AND ( search_type = " . $this->db->quote($this->search_type, 'integer') . ' ' .
398 "OR search_type = " . $this->db->quote(self::LAST_QUERY, 'integer') . ')';
399 $res = $this->db->manipulate($query);
400
401 $this->db->insert('usr_search', array(
402 'usr_id' => array('integer', $this->usr_id),
403 'search_result' => array('clob',serialize($this->search_result)),
404 'checked' => array('clob',serialize($this->checked)),
405 'failed' => array('clob',serialize($this->failed)),
406 'page' => array('integer', $this->page_number),
407 'search_type' => array('integer', $this->search_type),
408 'query' => array('clob',serialize($this->getQuery())),
409 'root' => array('integer',$this->getRoot()),
410 'item_filter' => array('text',serialize($this->getItemFilter())),
411 'mime_filter' => array('text', serialize($this->getMimeFilter())),
412 'creation_filter' => array('text', serialize($this->getCreationFilter()))
413 ));
414
415
416 // Write last query information
417 $this->db->insert(
418 'usr_search',
419 array(
420 'usr_id' => array('integer',$this->usr_id),
421 'search_type' => array('integer',self::LAST_QUERY),
422 'query' => array('text',serialize($this->getQuery()))
423 )
424 );
425 }
426
427 public function saveForAnonymous(): void
428 {
429 ilSession::clear('usr_search_cache');
430 $session_usr_search = [];
431 $session_usr_search[$this->search_type]['search_result'] = $this->search_result;
432 $session_usr_search[$this->search_type]['checked'] = $this->checked;
433 $session_usr_search[$this->search_type]['failed'] = $this->failed;
434 $session_usr_search[$this->search_type]['page'] = $this->page_number;
435 $session_usr_search[$this->search_type]['query'] = $this->getQuery();
436 $session_usr_search[$this->search_type]['root'] = $this->getRoot();
437 $session_usr_search[$this->search_type]['item_filter'] = $this->getItemFilter();
438 $session_usr_search[$this->search_type]['mime_filter'] = $this->getMimeFilter();
439 $session_usr_search[$this->search_type]['creation_filter'] = $this->getCreationFilter();
440 $session_usr_search[self::LAST_QUERY]['query'] = $this->getQuery();
441 ilSession::set('usr_search_cache', $session_usr_search);
442 }
443
444
451 private function read(): void
452 {
453 $this->failed = array();
454 $this->checked = array();
455 $this->search_result = array();
456 $this->page_number = 0;
457
458 if ($this->isAnonymous()) {
459 $this->readAnonymous();
460 return;
461 }
462
463 $query = "SELECT * FROM usr_search " .
464 "WHERE usr_id = " . $this->db->quote($this->usr_id, 'integer') . " " .
465 "AND search_type = " . $this->db->quote($this->search_type, 'integer');
466
467 $res = $this->db->query($query);
468 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
469 $this->search_result = (array) unserialize((string) $row->search_result);
470 if (strlen((string) $row->checked)) {
471 $this->checked = (array) unserialize((string) $row->checked);
472 }
473 if (strlen((string) $row->failed)) {
474 $this->failed = (array) unserialize((string) $row->failed);
475 }
476 $this->page_number = (int) $row->page;
477 $this->setQuery(unserialize((string) $row->query));
478 $this->setRoot((int) $row->root);
479 $this->setItemFilter((array) unserialize((string) $row->item_filter));
480 $this->setCreationFilter((array) unserialize((string) $row->creation_filter));
481 }
482 }
483
487 private function readAnonymous(): void
488 {
489 $usr_search_cache = ilSession::get('usr_search_cache') ?? [];
490
491 $this->search_result = (array) ($usr_search_cache[$this->search_type]['search_result'] ?? []);
492 $this->checked = (array) ($usr_search_cache[$this->search_type]['checked'] ?? []);
493 $this->failed = (array) ($usr_search_cache[$this->search_type]['failed'] ?? []);
494 $this->page_number = (int) ($usr_search_cache[$this->search_type]['page_number'] ?? 1);
495 $this->setQuery((string) ($usr_search_cache[$this->search_type]['query'] ?? ''));
496 $this->setRoot((int) ($usr_search_cache[$this->search_type]['root'] ?? ROOT_FOLDER_ID));
497 $this->setItemFilter((array) ($usr_search_cache[$this->search_type]['item_filter'] ?? []));
498 $this->setMimeFilter((array) ($usr_search_cache[$this->search_type]['mime_filter'] ?? []));
499 $this->setCreationFilter((array) ($usr_search_cache[$this->search_type]['creation_filter'] ?? []));
500 }
501}
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)
Append checked id.
setCreationFilter(array $a_filter)
deleteCachedEntriesAnonymous()
Delete cached entries for anonymous user.
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.
deleteCachedEntries()
delete cached entries
__construct(int $a_usr_id)
Constructor.
setQuery($a_query)
set query
setResultPageNumber(int $a_number)
Set result page number.
isAnonymous()
Check if current user is anonymous user.
isChecked(int $a_ref_id)
Check if reference was already checked.
read()
Read user entries.
const ANONYMOUS_USER_ID
Definition: constants.php:27
const ROOT_FOLDER_ID
Definition: constants.php:32
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$res
Definition: ltiservices.php:69