ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilUserSearchCache.php
Go to the documentation of this file.
1 <?php
2 
3 declare(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;
52  private int $search_type = self::DEFAULT_SEARCH;
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  {
309  return $this->creation_filter;
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  'query' => array('clob',serialize(''))
352  )
353  );
354  }
355 
356  $this->setResultPageNumber(1);
357  $this->search_result = array();
358  $this->checked = array();
359  $this->failed = array();
360  }
361 
366  public function deleteCachedEntriesAnonymous(): bool
367  {
368  $this->setResultPageNumber(1);
369  $this->search_result = array();
370  $this->checked = array();
371  $this->failed = array();
372 
373  return true;
374  }
375 
376 
377 
378  public function delete(): bool
379  {
380  $query = "DELETE FROM usr_search " .
381  "WHERE usr_id = " . $this->db->quote($this->usr_id, 'integer') . " " .
382  "AND search_type = " . $this->db->quote($this->search_type, 'integer');
383  $res = $this->db->manipulate($query);
384 
385  $this->read();
386  return true;
387  }
388 
389  public function save(): void
390  {
391  if ($this->isAnonymous()) {
392  $this->saveForAnonymous();
393  return;
394  }
395 
396  $query = "DELETE FROM usr_search " .
397  "WHERE usr_id = " . $this->db->quote($this->usr_id, 'integer') . " " .
398  "AND ( search_type = " . $this->db->quote($this->search_type, 'integer') . ' ' .
399  "OR search_type = " . $this->db->quote(self::LAST_QUERY, 'integer') . ')';
400  $res = $this->db->manipulate($query);
401 
402  $this->db->insert('usr_search', array(
403  'usr_id' => array('integer', $this->usr_id),
404  'search_result' => array('clob',serialize($this->search_result)),
405  'checked' => array('clob',serialize($this->checked)),
406  'failed' => array('clob',serialize($this->failed)),
407  'page' => array('integer', $this->page_number),
408  'search_type' => array('integer', $this->search_type),
409  'query' => array('clob',serialize($this->getQuery())),
410  'root' => array('integer',$this->getRoot()),
411  'item_filter' => array('text',serialize($this->getItemFilter())),
412  'mime_filter' => array('text', serialize($this->getMimeFilter())),
413  'creation_filter' => array('text', serialize($this->getCreationFilter()))
414  ));
415 
416 
417  // Write last query information
418  $this->db->insert(
419  'usr_search',
420  array(
421  'usr_id' => array('integer',$this->usr_id),
422  'search_type' => array('integer',self::LAST_QUERY),
423  'query' => array('text',serialize($this->getQuery()))
424  )
425  );
426  }
427 
428  public function saveForAnonymous(): void
429  {
430  ilSession::clear('usr_search_cache');
431  $session_usr_search = [];
432  $session_usr_search[$this->search_type]['search_result'] = $this->search_result;
433  $session_usr_search[$this->search_type]['checked'] = $this->checked;
434  $session_usr_search[$this->search_type]['failed'] = $this->failed;
435  $session_usr_search[$this->search_type]['page'] = $this->page_number;
436  $session_usr_search[$this->search_type]['query'] = $this->getQuery();
437  $session_usr_search[$this->search_type]['root'] = $this->getRoot();
438  $session_usr_search[$this->search_type]['item_filter'] = $this->getItemFilter();
439  $session_usr_search[$this->search_type]['mime_filter'] = $this->getMimeFilter();
440  $session_usr_search[$this->search_type]['creation_filter'] = $this->getCreationFilter();
441  $session_usr_search[self::LAST_QUERY]['query'] = $this->getQuery();
442  ilSession::set('usr_search_cache', $session_usr_search);
443  }
444 
445 
452  private function read(): void
453  {
454  $this->failed = array();
455  $this->checked = array();
456  $this->search_result = array();
457  $this->page_number = 0;
458 
459  if ($this->isAnonymous()) {
460  $this->readAnonymous();
461  return;
462  }
463 
464  $query = "SELECT * FROM usr_search " .
465  "WHERE usr_id = " . $this->db->quote($this->usr_id, 'integer') . " " .
466  "AND search_type = " . $this->db->quote($this->search_type, 'integer');
467 
468  $res = $this->db->query($query);
469  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
470  $this->search_result = (array) unserialize((string) $row->search_result);
471  if (strlen((string) $row->checked)) {
472  $this->checked = (array) unserialize((string) $row->checked);
473  }
474  if (strlen((string) $row->failed)) {
475  $this->failed = (array) unserialize((string) $row->failed);
476  }
477  $this->page_number = (int) $row->page;
478  $this->setQuery(unserialize((string) $row->query));
479  $this->setRoot((int) $row->root);
480  $this->setItemFilter((array) unserialize((string) $row->item_filter));
481  $this->setCreationFilter((array) unserialize((string) $row->creation_filter));
482  }
483  }
484 
488  private function readAnonymous(): void
489  {
490  $usr_search_cache = ilSession::get('usr_search_cache') ?? [];
491 
492  $this->search_result = (array) ($usr_search_cache[$this->search_type]['search_result'] ?? []);
493  $this->checked = (array) ($usr_search_cache[$this->search_type]['checked'] ?? []);
494  $this->failed = (array) ($usr_search_cache[$this->search_type]['failed'] ?? []);
495  $this->page_number = (int) ($usr_search_cache[$this->search_type]['page_number'] ?? 1);
496  $this->setQuery((string) ($usr_search_cache[$this->search_type]['query'] ?? ''));
497  $this->setRoot((int) ($usr_search_cache[$this->search_type]['root'] ?? ROOT_FOLDER_ID));
498  $this->setItemFilter((array) ($usr_search_cache[$this->search_type]['item_filter'] ?? []));
499  $this->setMimeFilter((array) ($usr_search_cache[$this->search_type]['mime_filter'] ?? []));
500  $this->setCreationFilter((array) ($usr_search_cache[$this->search_type]['creation_filter'] ?? []));
501  }
502 }
static get(string $a_var)
setQuery($a_query)
set query
appendToFailed(int $a_ref_id)
Append failed id.
$res
Definition: ltiservices.php:69
const ANONYMOUS_USER_ID
Definition: constants.php:27
const ROOT_FOLDER_ID
Definition: constants.php:32
getCheckedItems()
Get all checked items public.
deleteCachedEntries()
delete cached entries
switchSearchType(int $a_type)
switch to search type reads entries from database
static _getInstance(int $a_usr_id)
deleteCachedEntriesAnonymous()
Delete cached entries for anonymous user.
readAnonymous()
Read from session for anonymous user.
getResultPageNumber()
get result page number
getUrlEncodedQuery()
Urlencode query for further use in e.g glossariers (highlighting off search terms).
global $DIC
Definition: shib_login.php:25
setResultPageNumber(int $a_number)
Set result page number.
setItemFilter(array $a_filter)
addResult(array $a_result_item)
Append result.
isAnonymous()
Check if current user is anonymous user.
setRoot(int $a_root)
set root node of search
isFailed(int $a_ref_id)
check if reference has failed access
setResults(array $a_results)
Set results.
Class for storing search result.
appendToChecked(int $a_ref_id, int $a_obj_id)
Append checked id.
setMimeFilter(array $a_filter)
static ilUserSearchCache $instance
read()
Read user entries.
setCreationFilter(array $a_filter)
__construct(int $a_usr_id)
Constructor.
isChecked(int $a_ref_id)
Check if reference was already checked.
static clear(string $a_var)
static set(string $a_var, $a_val)
Set a value.