ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilLuceneSearcher.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 
24 include_once './Services/Search/classes/Lucene/class.ilLuceneSearchResult.php';
25 
36 {
37  const TYPE_STANDARD = 1;
38  const TYPE_USER = 2;
39 
40  private static $instance = null;
41 
42  private $query_parser = null;
43  private $result = null;
44  private $highlighter = null;
45  private $page_number = 1;
47 
53  private function __construct($qp)
54  {
55  $this->result = new ilLuceneSearchResult();
56  $this->result->setCallback(array($this,'nextResultPage'));
57  $this->query_parser = $qp;
58  }
59 
67  public static function getInstance(ilLuceneQueryParser $qp)
68  {
69  if(self::$instance)
70  {
71  return self::$instance;
72  }
73  return self::$instance = new ilLuceneSearcher($qp);
74  }
75 
80  public function setType($a_type)
81  {
82  $this->type = $a_type;
83  }
84 
89  public function getType()
90  {
91  return $this->type;
92  }
93 
94 
99  public function search()
100  {
101  $this->performSearch();
102  }
103 
109  public function highlight($a_obj_ids)
110  {
111  global $ilBench,$ilSetting, $ilLog;
112 
113  include_once './Services/Search/classes/Lucene/class.ilLuceneHighlighterResultParser.php';
114 
115  // TODO error handling
116  if(!$this->query_parser->getQuery())
117  {
118  return;
119  }
120 
121  // Search in combined index
122  $ilBench->start('Lucene','SearchHighlight');
123  try
124  {
125  include_once './Services/WebServices/RPC/classes/class.ilRpcClientFactory.php';
126  $res = ilRpcClientFactory::factory('RPCSearchHandler')->highlight(
127  CLIENT_ID.'_'.$ilSetting->get('inst_id',0),
128  $a_obj_ids,
129  $this->query_parser->getQuery()
130  );
131  }
132  catch(XML_RPC2_FaultException $e)
133  {
134  // TODO: better error handling
135  $ilLog->write(__METHOD__.': '.$e->getMessage());
136  return new ilLuceneHighlighterResultParser();
137  }
138  catch(Exception $e)
139  {
140  $ilLog->write(__METHOD__.': '.$e->getMessage());
141  return new ilLuceneHighlighterResultParser();
142  }
143  $ilBench->stop('Lucene','SearchHighlight');
144 
145  $ilBench->start('Lucene','SearchHighlightParser');
146  include_once './Services/Search/classes/Lucene/class.ilLuceneHighlighterResultParser.php';
147  $this->highlighter = new ilLuceneHighlighterResultParser();
148  #$GLOBALS['ilLog']->write(__METHOD__.' Result is '. $res);
149  $this->highlighter->setResultString($res);
150  $this->highlighter->parse();
151  $ilBench->stop('Lucene','SearchHighlightParser');
152 
153  return $this->highlighter;
154  }
155 
161  public function nextResultPage()
162  {
163  $this->page_number++;
164  $this->performSearch();
165  }
166 
171  public function getHighlighter()
172  {
173  return $this->highlighter;
174  }
175 
181  public function getResult()
182  {
183  if($this->result instanceof ilLuceneSearchResult)
184  {
185  return $this->result;
186  }
187  // TODO Error handling
188  }
189 
195  public function getPageNumber()
196  {
197  return $this->page_number;
198  }
199 
204  protected function performSearch()
205  {
206  global $ilBench,$ilSetting, $ilLog;
207 
208  // TODO error handling
209  if(!$this->query_parser->getQuery())
210  {
211  return;
212  }
213  $ilBench->start('Lucene','SearchCombinedIndex');
214  try
215  {
216  switch($this->getType())
217  {
218 
219  case self::TYPE_USER:
220  include_once './Services/WebServices/RPC/classes/class.ilRpcClientFactory.php';
221  $res = ilRpcClientFactory::factory('RPCSearchHandler')->searchUsers(
222  CLIENT_ID.'_'.$ilSetting->get('inst_id',0),
223  (string) $this->query_parser->getQuery()
224  );
225  break;
226 
227  case self::TYPE_STANDARD:
228  default:
229  include_once './Services/WebServices/RPC/classes/class.ilRpcClientFactory.php';
230  $res = ilRpcClientFactory::factory('RPCSearchHandler')->search(
231  CLIENT_ID.'_'.$ilSetting->get('inst_id',0),
232  (string) $this->query_parser->getQuery(),
233  $this->getPageNumber()
234  );
235  break;
236 
237  }
238  }
239  catch(XML_RPC2_FaultException $e)
240  {
241  // TODO: better error handling
242  $ilBench->stop('Lucene','SearchCombinedIndex');
243  $ilLog->write(__METHOD__.': '.$e->getMessage());
244  return;
245  }
246  catch(Exception $e)
247  {
248  $ilBench->stop('Lucene','SearchCombinedIndex');
249  $ilLog->write(__METHOD__.': '.$e->getMessage());
250  return;
251  }
252  $ilBench->stop('Lucene','SearchCombinedIndex');
253 
254 
255  // Parse results
256  $ilBench->start('Lucene','ParseSearchResult');
257  include_once './Services/Search/classes/Lucene/class.ilLuceneSearchResultParser.php';
258  $parser = new ilLuceneSearchResultParser($res);
259 
260  #$GLOBALS['ilLog']->write(__METHOD__.' Result is: ' . $res);
261 
262  $parser->parse($this->result);
263  $ilBench->stop('Lucene','ParseSearchResult');
264  return;
265  }
266 }
267 ?>