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 24include_once './Services/Search/classes/Lucene/class.ilLuceneSearchResult.php'; 25 26/** 27* Reads and parses lucene search results 28* 29* @author Stefan Meyer <meyer@leifos.com> 30* @version $Id$ 31* 32* 33* @ingroup 34*/ 35class ilLuceneSearcher 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; 46 private $type = self::TYPE_STANDARD; 47 48 /** 49 * Constructor 50 * @param object ilLuceneQueryParser 51 * @return ilLuceneSearcher 52 */ 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 60 /** 61 * Get singleton instance 62 * 63 * @param object ilLuceneQueryParser 64 * @return ilLuceneSearcher 65 * @static 66 */ 67 public static function getInstance(ilLuceneQueryParser $qp) 68 { 69 if (self::$instance) { 70 return self::$instance; 71 } 72 return self::$instance = new ilLuceneSearcher($qp); 73 } 74 75 /** 76 * Set search type 77 * @param type $a_type 78 */ 79 public function setType($a_type) 80 { 81 $this->type = $a_type; 82 } 83 84 /** 85 * Get type 86 * @return int 87 */ 88 public function getType() 89 { 90 return $this->type; 91 } 92 93 94 /** 95 * Search 96 * @return 97 */ 98 public function search() 99 { 100 $this->performSearch(); 101 } 102 103 /** 104 * Highlight/Detail query 105 * @param array $a_obj_ids Arry of obj_ids 106 * @return object ilLuceneHighlightResultParser 107 */ 108 public function highlight($a_obj_ids) 109 { 110 global $DIC; 111 112 $ilBench = $DIC['ilBench']; 113 $ilSetting = $DIC['ilSetting']; 114 115 include_once './Services/Search/classes/Lucene/class.ilLuceneHighlighterResultParser.php'; 116 117 // TODO error handling 118 if (!$this->query_parser->getQuery()) { 119 return; 120 } 121 122 // Search in combined index 123 $ilBench->start('Lucene', 'SearchHighlight'); 124 try { 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 } catch (Exception $e) { 132 ilLoggerFactory::getLogger('src')->error('Highlighting failed with message: ' . $e->getMessage()); 133 return new ilLuceneHighlighterResultParser(); 134 } 135 136 include_once './Services/Search/classes/Lucene/class.ilLuceneHighlighterResultParser.php'; 137 $this->highlighter = new ilLuceneHighlighterResultParser(); 138 $this->highlighter->setResultString($res); 139 $this->highlighter->parse(); 140 141 return $this->highlighter; 142 } 143 144 /** 145 * get next result page 146 * @param 147 * @return 148 */ 149 public function nextResultPage() 150 { 151 $this->page_number++; 152 $this->performSearch(); 153 } 154 155 /** 156 * get highlighter 157 * @return ilLuceneHighlightResultParser 158 */ 159 public function getHighlighter() 160 { 161 return $this->highlighter; 162 } 163 164 /** 165 * Get result 166 * @param 167 * @return ilLuceneSearchResult 168 */ 169 public function getResult() 170 { 171 if ($this->result instanceof ilLuceneSearchResult) { 172 return $this->result; 173 } 174 // TODO Error handling 175 } 176 177 /** 178 * get current page number 179 * @param 180 * @return 181 */ 182 public function getPageNumber() 183 { 184 return $this->page_number; 185 } 186 187 /** 188 * search lucene 189 * @return 190 */ 191 protected function performSearch() 192 { 193 global $DIC; 194 195 $ilBench = $DIC['ilBench']; 196 $ilSetting = $DIC['ilSetting']; 197 198 // TODO error handling 199 if (!$this->query_parser->getQuery()) { 200 return; 201 } 202 $ilBench->start('Lucene', 'SearchCombinedIndex'); 203 try { 204 switch ($this->getType()) { 205 206 case self::TYPE_USER: 207 include_once './Services/WebServices/RPC/classes/class.ilRpcClientFactory.php'; 208 $res = ilRpcClientFactory::factory('RPCSearchHandler')->searchUsers( 209 CLIENT_ID . '_' . $ilSetting->get('inst_id', 0), 210 (string) $this->query_parser->getQuery() 211 ); 212 break; 213 214 case self::TYPE_STANDARD: 215 default: 216 include_once './Services/WebServices/RPC/classes/class.ilRpcClientFactory.php'; 217 $res = ilRpcClientFactory::factory('RPCSearchHandler')->search( 218 CLIENT_ID . '_' . $ilSetting->get('inst_id', 0), 219 (string) $this->query_parser->getQuery(), 220 $this->getPageNumber() 221 ); 222 break; 223 224 } 225 ilLoggerFactory::getLogger('src')->debug('Searching for: ' . $this->query_parser->getQuery()); 226 } catch (Exception $e) { 227 ilLoggerFactory::getLogger('src')->error('Searching failed with message: ' . $e->getMessage()); 228 return; 229 } 230 231 // Parse results 232 include_once './Services/Search/classes/Lucene/class.ilLuceneSearchResultParser.php'; 233 $parser = new ilLuceneSearchResultParser($res); 234 $parser->parse($this->result); 235 return; 236 } 237} 238