ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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 = array();
42 private array $checked = array();
43 private array $failed = array();
44 private int $page_number = 1;
45
49 private $query;
50 private int $root;
51 private array $item_filter = array();
52 private bool $isAnonymous = false;
53 private array $mime_filter = array();
54 private array $creation_filter = array();
55
56
57
64 private function __construct(int $a_usr_id)
65 {
66 global $DIC;
67
68 $this->db = $DIC->database();
69
70 if ($a_usr_id == ANONYMOUS_USER_ID) {
71 $this->isAnonymous = true;
72 }
73
74 $this->root = ROOT_FOLDER_ID;
75 $this->usr_id = $a_usr_id;
76 $this->search_type = self::DEFAULT_SEARCH;
77 $this->read();
78 }
79
80 public static function _getInstance(int $a_usr_id): ilUserSearchCache
81 {
82 if (self::$instance instanceof ilUserSearchCache) {
83 return self::$instance;
84 }
85 return self::$instance = new ilUserSearchCache($a_usr_id);
86 }
87
92 public function isAnonymous(): bool
93 {
94 return $this->isAnonymous;
95 }
96
101 public function switchSearchType(int $a_type): bool
102 {
103 $this->search_type = $a_type;
104 $this->read();
105 return true;
106 }
107
114 public function getResults(): array
115 {
116 return $this->search_result ?: array();
117 }
118
126 public function setResults(array $a_results): void
127 {
128 $this->search_result = $a_results;
129 }
130
138 public function addResult(array $a_result_item): bool
139 {
140 $this->search_result[$a_result_item['ref_id']]['ref_id'] = $a_result_item['ref_id'];
141 $this->search_result[$a_result_item['ref_id']]['obj_id'] = $a_result_item['obj_id'];
142 $this->search_result[$a_result_item['ref_id']]['type'] = $a_result_item['type'];
143 return true;
144 }
145
149 public function appendToFailed(int $a_ref_id): void
150 {
151 $this->failed[$a_ref_id] = $a_ref_id;
152 }
153
157 public function isFailed(int $a_ref_id): bool
158 {
159 return in_array($a_ref_id, $this->failed);
160 }
161
162 public function appendToChecked(int $a_ref_id, int $a_obj_id): void
163 {
164 $this->checked[$a_ref_id] = $a_obj_id;
165 }
166
167 public function isChecked(int $a_ref_id): bool
168 {
169 return array_key_exists($a_ref_id, $this->checked) and $this->checked[$a_ref_id];
170 }
171
177 public function getCheckedItems(): array
178 {
179 return $this->checked ?: array();
180 }
181
188 public function setResultPageNumber(int $a_number): void
189 {
190 if ($a_number) {
191 $this->page_number = $a_number;
192 }
193 }
194
198 public function getResultPageNumber(): int
199 {
200 return $this->page_number ?: 1;
201 }
202
203 public function setQuery(string $a_query): void
204 {
205 $this->query = $a_query;
206 }
207
208 public function getQuery(): string
209 {
210 return $this->query ?? '';
211 }
212
216 public function getUrlEncodedQuery(): string
217 {
218 return urlencode(str_replace('"', '.', $this->getQuery()));
219 }
220
224 public function setRoot(int $a_root): void
225 {
226 $this->root = $a_root;
227 }
228
229 public function getRoot(): int
230 {
231 return $this->root ?: ROOT_FOLDER_ID;
232 }
233
234 public function setItemFilter(array $a_filter): void
235 {
236 $this->item_filter = $a_filter;
237 }
238
239 public function getItemFilter(): array
240 {
241 return $this->item_filter;
242 }
243
244 public function setMimeFilter(array $a_filter): void
245 {
246 $this->mime_filter = $a_filter;
247 }
248
249 public function getMimeFilter(): array
250 {
251 return $this->mime_filter;
252 }
253
254 public function setCreationFilter(array $a_filter): void
255 {
256 $this->creation_filter = $a_filter;
257 }
258
259 public function getCreationFilter(): array
260 {
262 }
263
264 public function deleteCachedEntries(): void
265 {
266 if ($this->isAnonymous()) {
268 return;
269 }
270 $query = "SELECT COUNT(*) num FROM usr_search " .
271 "WHERE usr_id = " . $this->db->quote($this->usr_id, 'integer') . " " .
272 "AND search_type = " . $this->db->quote($this->search_type, 'integer');
273 $res = $this->db->query($query);
274 $row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT);
275
276 if ($row->num > 0) {
277 $this->db->update(
278 'usr_search',
279 array(
280 'search_result' => array('clob',serialize(array(0))),
281 'checked' => array('clob',serialize(array(0))),
282 'failed' => array('clob',serialize(array(0))),
283 'page' => array('integer',0)),
284 array(
285 'usr_id' => array('integer', $this->usr_id),
286 'search_type' => array('integer', $this->search_type)
287 )
288 );
289 } else {
290 $this->db->insert(
291 'usr_search',
292 array(
293 'search_result' => array('clob',serialize(array(0))),
294 'checked' => array('clob',serialize(array(0))),
295 'failed' => array('clob',serialize(array(0))),
296 'page' => array('integer',0),
297 'usr_id' => array('integer', $this->usr_id),
298 'search_type' => array('integer', $this->search_type),
299 'query' => array('clob',serialize(''))
300 )
301 );
302 }
303
304 $this->setResultPageNumber(1);
305 $this->search_result = array();
306 $this->checked = array();
307 $this->failed = array();
308 }
309
310 public function deleteCachedEntriesAnonymous(): bool
311 {
312 $this->setResultPageNumber(1);
313 $this->search_result = array();
314 $this->checked = array();
315 $this->failed = array();
316
317 return true;
318 }
319
320 public function delete(): bool
321 {
322 $query = "DELETE 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->manipulate($query);
326
327 $this->read();
328 return true;
329 }
330
331 public function save(): void
332 {
333 if ($this->isAnonymous()) {
334 $this->saveForAnonymous();
335 return;
336 }
337
338 $query = "DELETE FROM usr_search " .
339 "WHERE usr_id = " . $this->db->quote($this->usr_id, 'integer') . " " .
340 "AND ( search_type = " . $this->db->quote($this->search_type, 'integer') . ' ' .
341 "OR search_type = " . $this->db->quote(self::LAST_QUERY, 'integer') . ')';
342 $res = $this->db->manipulate($query);
343
344 $this->db->insert('usr_search', array(
345 'usr_id' => array('integer', $this->usr_id),
346 'search_result' => array('clob',serialize($this->search_result)),
347 'checked' => array('clob',serialize($this->checked)),
348 'failed' => array('clob',serialize($this->failed)),
349 'page' => array('integer', $this->page_number),
350 'search_type' => array('integer', $this->search_type),
351 'query' => array('clob',serialize($this->getQuery())),
352 'root' => array('integer',$this->getRoot()),
353 'item_filter' => array('text',serialize($this->getItemFilter())),
354 'mime_filter' => array('text', serialize($this->getMimeFilter())),
355 'creation_filter' => array('text', serialize($this->getCreationFilter()))
356 ));
357
358
359 // Write last query information
360 $this->db->insert(
361 'usr_search',
362 array(
363 'usr_id' => array('integer',$this->usr_id),
364 'search_type' => array('integer',self::LAST_QUERY),
365 'query' => array('text',serialize($this->getQuery()))
366 )
367 );
368 }
369
370 public function saveForAnonymous(): void
371 {
372 ilSession::clear('usr_search_cache');
373 $session_usr_search = [];
374 $session_usr_search[$this->search_type]['search_result'] = $this->search_result;
375 $session_usr_search[$this->search_type]['checked'] = $this->checked;
376 $session_usr_search[$this->search_type]['failed'] = $this->failed;
377 $session_usr_search[$this->search_type]['page'] = $this->page_number;
378 $session_usr_search[$this->search_type]['query'] = $this->getQuery();
379 $session_usr_search[$this->search_type]['root'] = $this->getRoot();
380 $session_usr_search[$this->search_type]['item_filter'] = $this->getItemFilter();
381 $session_usr_search[$this->search_type]['mime_filter'] = $this->getMimeFilter();
382 $session_usr_search[$this->search_type]['creation_filter'] = $this->getCreationFilter();
383 $session_usr_search[self::LAST_QUERY]['query'] = $this->getQuery();
384 ilSession::set('usr_search_cache', $session_usr_search);
385 }
386
387 private function read(): void
388 {
389 $this->failed = array();
390 $this->checked = array();
391 $this->search_result = array();
392 $this->page_number = 0;
393
394 if ($this->isAnonymous()) {
395 $this->readAnonymous();
396 return;
397 }
398
399 $query = "SELECT * FROM usr_search " .
400 "WHERE usr_id = " . $this->db->quote($this->usr_id, 'integer') . " " .
401 "AND search_type = " . $this->db->quote($this->search_type, 'integer');
402
403 $res = $this->db->query($query);
404 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
405 $this->search_result = (array) unserialize((string) $row->search_result);
406 if (strlen((string) $row->checked)) {
407 $this->checked = (array) unserialize((string) $row->checked);
408 }
409 if (strlen((string) $row->failed)) {
410 $this->failed = (array) unserialize((string) $row->failed);
411 }
412 $this->page_number = (int) $row->page;
413 $this->setQuery(unserialize((string) $row->query));
414 $this->setRoot((int) $row->root);
415 $this->setItemFilter((array) unserialize((string) $row->item_filter));
416 $this->setCreationFilter((array) unserialize((string) $row->creation_filter));
417 }
418 }
419
423 private function readAnonymous(): void
424 {
425 $usr_search_cache = ilSession::get('usr_search_cache') ?? [];
426
427 $this->search_result = (array) ($usr_search_cache[$this->search_type]['search_result'] ?? []);
428 $this->checked = (array) ($usr_search_cache[$this->search_type]['checked'] ?? []);
429 $this->failed = (array) ($usr_search_cache[$this->search_type]['failed'] ?? []);
430 $this->page_number = (int) ($usr_search_cache[$this->search_type]['page_number'] ?? 1);
431 $this->setQuery((string) ($usr_search_cache[$this->search_type]['query'] ?? ''));
432 $this->setRoot((int) ($usr_search_cache[$this->search_type]['root'] ?? ROOT_FOLDER_ID));
433 $this->setItemFilter((array) ($usr_search_cache[$this->search_type]['item_filter'] ?? []));
434 $this->setMimeFilter((array) ($usr_search_cache[$this->search_type]['mime_filter'] ?? []));
435 $this->setCreationFilter((array) ($usr_search_cache[$this->search_type]['creation_filter'] ?? []));
436 }
437}
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.
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