ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilLuceneSearchResultFilter.php
Go to the documentation of this file.
1<?php
2/*
3 +-----------------------------------------------------------------------------+
4 | ILIAS open source |
5 +-----------------------------------------------------------------------------+
6 | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7 | |
8 | This program is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU General Public License |
10 | as published by the Free Software Foundation; either version 2 |
11 | of the License, or (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 +-----------------------------------------------------------------------------+
22*/
23
35{
36 protected static $instance = null;
37
38 protected $user_id = null;
39 protected $result = array();
40 protected $checked = array();
41 protected $settings;
42 protected $cache;
43 protected $required_permission = 'visible';
44 protected $limit_reached = false;
45 protected $offset = 0;
46
47 protected $filters = array();
48
49
55 protected function __construct($a_user_id)
56 {
57 $this->user_id = $a_user_id;
59
60 include_once('Services/Search/classes/class.ilUserSearchCache.php');
61 $this->cache = ilUserSearchCache::_getInstance($this->getUserId());
62
63 $this->offset = $this->settings->getMaxHits() * ($this->cache->getResultPageNumber() - 1);
64 }
65
72 public static function getInstance($a_user_id)
73 {
74 if(self::$instance == null)
75 {
76 return self::$instance = new ilLuceneSearchResultFilter($a_user_id);
77 }
78 return self::$instance;
79 }
80
86 public function addFilter(ilLuceneResultFilter $filter)
87 {
88 $this->filters[] = $filter;
89 }
90
96 public function setCandidates($a_ids)
97 {
98 $this->result = $a_ids;
99 }
100
105 public function getCandidates()
106 {
107 return $this->result ? $this->result : array();
108 }
109
114 public function getUserId()
115 {
116 return $this->user_id;
117 }
118
123 public function getRequiredPermission()
124 {
126 }
127
132 public function isLimitReached()
133 {
134 return (bool) $this->limit_reached;
135 }
136
141 public function getResultIds()
142 {
143 return $this->checked ? $this->checked : array();
144 }
145
150 public function getResultObjIds()
151 {
152 foreach($this->checked as $obj_id)
153 {
154 $obj_ids[] = $obj_id;
155 }
156 return $obj_ids ? $obj_ids : array();
157 }
158
163 public function getResults()
164 {
165 return $this->checked ? $this->checked : array();
166 }
167
172 public function getMaxHits()
173 {
174 return $this->settings->getMaxHits();
175 }
176
181 public function loadFromDb()
182 {
183 $this->checked = $this->cache->getResults();
184 }
185
197 public function filter()
198 {
199 global $ilAccess,$tree;
200
201 // get ref_ids and check access
202 $counter = 0;
203 $offset_counter = 0;
204
205 foreach($this->getCandidates() as $obj_id)
206 {
207 // Check referenced objects
208 foreach(ilObject::_getAllReferences($obj_id) as $ref_id)
209 {
210 // Check filter
211 if(!$this->checkFilter($ref_id))
212 {
213 $this->cache->appendToFailed($ref_id);
214 continue;
215 }
216
217 // Access failed by prior check
218 if($this->cache->isFailed($ref_id))
219 {
220 continue;
221 }
222 // Offset check
223 if($this->cache->isChecked($ref_id) and !$this->isOffsetReached($offset_counter))
224 {
225 ilLoggerFactory::getLogger('src')->debug('Result was checked');
226 $offset_counter++;
227 break;
228 }
229
230 // RBAC check
231 if($ilAccess->checkAccessOfUser($this->getUserId(),
232 $this->getRequiredPermission(),
233 '',
234 $ref_id,
235 '',
236 $obj_id))
237 {
238 ++$counter;
239 $offset_counter++;
240 $this->append($ref_id,$obj_id);
241 $this->cache->appendToChecked($ref_id,$obj_id);
242 break;
243 }
244 else
245 {
246 $this->cache->appendToFailed($ref_id);
247 }
248 }
249 if($counter >= $this->settings->getMaxHits())
250 {
251 $this->limit_reached = true;
252 $this->cache->setResults($this->getResultIds());
253 $this->cache->save();
254 return false;
255 }
256 }
257 $this->cache->setResults($this->getResultIds());
258 $this->cache->save();
259 return true;
260 }
261
267 protected function checkFilter($a_ref_id)
268 {
269 foreach($this->filters as $filter)
270 {
271 if(!$filter->filter($a_ref_id))
272 {
273 return false;
274 }
275 }
276 return true;
277 }
278
284 protected function append($a_ref_id,$a_obj_id)
285 {
286 $this->checked[$a_ref_id] = $a_obj_id;
287 }
288
294 protected function isOffsetReached($a_current_nr)
295 {
296 return $a_current_nr < $this->offset ? false : true;
297 }
298}
299?>
An exception for terminatinating execution or to throw for unit testing.
static getLogger($a_component_id)
Get component logger.
Validate Lucene search results Do access checks, create ref_ids from obj_ids...
addFilter(ilLuceneResultFilter $filter)
add filter
__construct($a_user_id)
Singleton constructor.
checkFilter($a_ref_id)
check appended filter
append($a_ref_id, $a_obj_id)
Append to filtered results.
getRequiredPermission()
Get required permission.
isOffsetReached($a_current_nr)
Check if offset is reached.
isLimitReached()
Check if search max hits is reached.
static _getAllReferences($a_id)
get all reference ids of object
static _getInstance($a_usr_id)
Get singleton instance.
$counter
$ref_id
Definition: sahs_server.php:39
settings()
Definition: settings.php:2