ILIAS  Release_4_2_x_branch Revision 61807
 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  private static $instance = null;
38 
39  private $query_parser = null;
40  private $result = null;
41  private $highlighter = null;
42  private $page_number = 1;
43 
49  private function __construct($qp)
50  {
51  $this->result = new ilLuceneSearchResult();
52  $this->result->setCallback(array($this,'nextResultPage'));
53  $this->query_parser = $qp;
54  }
55 
63  public static function getInstance(ilLuceneQueryParser $qp)
64  {
65  if(self::$instance)
66  {
67  return self::$instance;
68  }
69  return self::$instance = new ilLuceneSearcher($qp);
70  }
71 
76  public function search()
77  {
78  $this->performSearch();
79  }
80 
86  public function highlight($a_obj_ids)
87  {
88  global $ilBench,$ilSetting, $ilLog;
89 
90  include_once './Services/Search/classes/Lucene/class.ilLuceneHighlighterResultParser.php';
91 
92  // TODO error handling
93  if(!$this->query_parser->getQuery())
94  {
95  return;
96  }
97 
98  // Search in combined index
99  $ilBench->start('Lucene','SearchHighlight');
100  try
101  {
102  include_once './Services/WebServices/RPC/classes/class.ilRpcClientFactory.php';
103  $res = ilRpcClientFactory::factory('RPCSearchHandler')->highlight(
104  CLIENT_ID.'_'.$ilSetting->get('inst_id',0),
105  $a_obj_ids,
106  $this->query_parser->getQuery()
107  );
108  }
109  catch(XML_RPC2_FaultException $e)
110  {
111  // TODO: better error handling
112  $ilLog->write(__METHOD__.': '.$e->getMessage());
113  return new ilLuceneHighlighterResultParser();
114  }
115  catch(Exception $e)
116  {
117  $ilLog->write(__METHOD__.': '.$e->getMessage());
118  return new ilLuceneHighlighterResultParser();
119  }
120  $ilBench->stop('Lucene','SearchHighlight');
121 
122  $ilBench->start('Lucene','SearchHighlightParser');
123  include_once './Services/Search/classes/Lucene/class.ilLuceneHighlighterResultParser.php';
124  $this->highlighter = new ilLuceneHighlighterResultParser();
125  $this->highlighter->setResultString($res);
126  $this->highlighter->parse();
127  $ilBench->stop('Lucene','SearchHighlightParser');
128 
129  return $this->highlighter;
130  }
131 
137  public function nextResultPage()
138  {
139  $this->page_number++;
140  $this->performSearch();
141  }
142 
147  public function getHighlighter()
148  {
149  return $this->highlighter;
150  }
151 
157  public function getResult()
158  {
159  if($this->result instanceof ilLuceneSearchResult)
160  {
161  return $this->result;
162  }
163  // TODO Error handling
164  }
165 
171  public function getPageNumber()
172  {
173  return $this->page_number;
174  }
175 
180  protected function performSearch()
181  {
182  global $ilBench,$ilSetting, $ilLog;
183 
184  // TODO error handling
185  if(!$this->query_parser->getQuery())
186  {
187  return;
188  }
189  $ilBench->start('Lucene','SearchCombinedIndex');
190  try
191  {
192  include_once './Services/WebServices/RPC/classes/class.ilRpcClientFactory.php';
193  $res = ilRpcClientFactory::factory('RPCSearchHandler')->search(
194  CLIENT_ID.'_'.$ilSetting->get('inst_id',0),
195  (string) $this->query_parser->getQuery(),
196  $this->getPageNumber()
197  );
198  }
199  catch(XML_RPC2_FaultException $e)
200  {
201  // TODO: better error handling
202  $ilBench->stop('Lucene','SearchCombinedIndex');
203  $ilLog->write(__METHOD__.': '.$e->getMessage());
204  return;
205  }
206  catch(Exception $e)
207  {
208  $ilBench->stop('Lucene','SearchCombinedIndex');
209  $ilLog->write(__METHOD__.': '.$e->getMessage());
210  return;
211  }
212  $ilBench->stop('Lucene','SearchCombinedIndex');
213 
214 
215  // Parse results
216  $ilBench->start('Lucene','ParseSearchResult');
217  include_once './Services/Search/classes/Lucene/class.ilLuceneSearchResultParser.php';
218  $parser = new ilLuceneSearchResultParser($res);
219  $parser->parse($this->result);
220  $ilBench->stop('Lucene','ParseSearchResult');
221  return;
222  }
223 }
224 ?>