ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilLuceneSearchGUI.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/class.ilSearchSettings.php';
25 include_once './Services/Search/classes/class.ilSearchBaseGUI.php';
26 include_once './Services/Search/classes/Lucene/class.ilLuceneAdvancedSearchFields.php';
27 include_once './Services/PersonalDesktop/interfaces/interface.ilDesktopItemHandling.php';
28 include_once './Services/Administration/interfaces/interface.ilAdministrationCommandHandling.php';
29 
45 {
46  protected $ilTabs;
47 
51  public function __construct()
52  {
53  global $ilTabs;
54 
55  $this->tabs_gui = $ilTabs;
58  $this->initUserSearchCache();
59 
60  }
61 
65  public function executeCommand()
66  {
67  global $ilBench, $ilCtrl;
68 
69  $ilBench->start('Lucene','0900_executeCommand');
70  $next_class = $this->ctrl->getNextClass($this);
71  $cmd = $this->ctrl->getCmd();
72 
73  $this->prepareOutput();
74  switch($next_class)
75  {
76  case "ilpropertyformgui":
77  /*$this->initStandardSearchForm(ilSearchBaseGUI::SEARCH_FORM_LUCENE);
78  $ilCtrl->setReturn($this, 'storeRoot');
79  $ilCtrl->forwardCommand($this->form);*/
80  $form = $this->getSearchAreaForm();
81  $ilCtrl->setReturn($this, 'storeRoot');
82  $ilCtrl->forwardCommand($form);
83  break;
84 
85  case 'ilobjectcopygui':
86  include_once './Services/Object/classes/class.ilObjectCopyGUI.php';
87  $cp = new ilObjectCopyGUI($this);
88  $this->ctrl->forwardCommand($cp);
89  break;
90 
91  default:
93  if(!$cmd)
94  {
95  $cmd = "showSavedResults";
96  }
97  $this->handleCommand($cmd);
98  break;
99  }
100  $ilBench->stop('Lucene','0900_executeCommand');
101  return true;
102  }
103 
107  public function prepareOutput()
108  {
110  $this->getTabs();
111  return true;
112  }
113 
119  protected function getType()
120  {
121  if(count($this->search_cache))
122  {
124  }
126  }
127 
133  protected function getDetails()
134  {
135  return (array) $this->search_cache->getItemFilter();
136  }
137 
143  protected function getMimeDetails()
144  {
145  return (array) $this->search_cache->getMimeFilter();
146  }
147 
151  protected function remoteSearch()
152  {
153  $_POST['query'] = $_POST['queryString'];
154  $this->search_cache->setRoot((int) $_POST['root_id']);
155  $this->search_cache->setQuery(ilUtil::stripSlashes($_POST['queryString']));
156  $this->search_cache->save();
157 
158  $this->search();
159  }
160 
165  protected function showSavedResults()
166  {
167  global $ilUser,$ilBench;
168 
169  if(!strlen($this->search_cache->getQuery()))
170  {
171  $this->showSearchForm();
172  return false;
173  }
174 
175  include_once './Services/Search/classes/Lucene/class.ilLuceneSearcher.php';
176  include_once './Services/Search/classes/Lucene/class.ilLuceneQueryParser.php';
177  $qp = new ilLuceneQueryParser($this->search_cache->getQuery());
178  $qp->parse();
179  $searcher = ilLuceneSearcher::getInstance($qp);
180  $searcher->search();
181 
182  // Load saved results
183  include_once './Services/Search/classes/Lucene/class.ilLuceneSearchResultFilter.php';
184  $filter = ilLuceneSearchResultFilter::getInstance($ilUser->getId());
185  $filter->loadFromDb();
186 
187  // Highlight
188  $searcher->highlight($filter->getResultObjIds());
189 
190  include_once './Services/Search/classes/class.ilSearchResultPresentation.php';
191  $presentation = new ilSearchResultPresentation($this);
192  $presentation->setResults($filter->getResultIds());
193 
194  $presentation->setSearcher($searcher);
195 
196  // TODO: other handling required
197  $this->addPager($filter,'max_page');
198 
199  $presentation->setPreviousNext($this->prev_link, $this->next_link);
200 
201  $this->showSearchForm();
202 
203  if($presentation->render())
204  {
205  $this->tpl->setVariable('SEARCH_RESULTS',$presentation->getHTML(true));
206  }
207  elseif(strlen($this->search_cache->getQuery()))
208  {
209  ilUtil::sendInfo(sprintf($this->lng->txt('search_no_match_hint'),$qp->getQuery()));
210  }
211  }
212 
217  protected function search()
218  {
219  if(!$this->form->checkInput())
220  {
221  $this->search_cache->deleteCachedEntries();
222  // Reset details
223  include_once './Services/Object/classes/class.ilSubItemListGUI.php';
225  $this->showSearchForm();
226  return false;
227  }
228 
229  unset($_SESSION['max_page']);
230  $this->search_cache->deleteCachedEntries();
231 
232  // Reset details
233  include_once './Services/Object/classes/class.ilSubItemListGUI.php';
235 
236  $this->performSearch();
237  }
238 
242  protected function performSearch()
243  {
244  global $ilUser,$ilBench;
245 
246  unset($_SESSION['vis_references']);
247 
248  $filter_query = '';
249  if($this->search_cache->getItemFilter() and ilSearchSettings::getInstance()->isLuceneItemFilterEnabled())
250  {
251  $filter_settings = ilSearchSettings::getInstance()->getEnabledLuceneItemFilterDefinitions();
252  foreach((array) $this->search_cache->getItemFilter() as $obj => $value)
253  {
254  if(!$filter_query)
255  {
256  $filter_query .= '+( ';
257  }
258  else
259  {
260  $filter_query .= 'OR';
261  }
262  $filter_query .= (' '. (string) $filter_settings[$obj]['filter'].' ');
263  }
264  $filter_query .= ') ';
265  }
266  // begin-patch mime_filter
267  $mime_query = '';
268  if($this->search_cache->getMimeFilter() and ilSearchSettings::getInstance()->isLuceneMimeFilterEnabled())
269  {
270  $filter_settings = ilSearchSettings::getInstance()->getEnabledLuceneMimeFilterDefinitions();
271  foreach($this->search_cache->getMimeFilter() as $mime => $value)
272  {
273  if(!$mime_query)
274  {
275  $mime_query .= '+( ';
276  }
277  else
278  {
279  $mime_query .= 'OR';
280  }
281  $mime_query .= (' '. (string) $filter_settings[$mime]['filter'].' ');
282  }
283  $mime_query .= ') ';
284  }
285  $filter_query = $filter_query . ' '. $mime_query;
286 
287  include_once './Services/Search/classes/Lucene/class.ilLuceneSearcher.php';
288  include_once './Services/Search/classes/Lucene/class.ilLuceneQueryParser.php';
289  $qp = new ilLuceneQueryParser($filter_query.' +('.$this->search_cache->getQuery().')');
290  $qp->parse();
291  $searcher = ilLuceneSearcher::getInstance($qp);
292  $searcher->search();
293 
294  // Filter results
295  include_once './Services/Search/classes/Lucene/class.ilLuceneSearchResultFilter.php';
296  include_once './Services/Search/classes/Lucene/class.ilLucenePathFilter.php';
297  $filter = ilLuceneSearchResultFilter::getInstance($ilUser->getId());
298  $filter->addFilter(new ilLucenePathFilter($this->search_cache->getRoot()));
299  $filter->setCandidates($searcher->getResult());
300  $filter->filter();
301 
302  if($filter->getResultObjIds()) {
303  $searcher->highlight($filter->getResultObjIds());
304  }
305 
306  // Show results
307  $this->showSearchForm();
308 
309  include_once './Services/Search/classes/class.ilSearchResultPresentation.php';
310  $presentation = new ilSearchResultPresentation($this);
311  $presentation->setResults($filter->getResultIds());
312  $presentation->setSearcher($searcher);
313 
314  // TODO: other handling required
315  $ilBench->start('Lucene','1500_fo');
316  $this->addPager($filter,'max_page');
317  $ilBench->stop('Lucene','1500_fo');
318 
319  $presentation->setPreviousNext($this->prev_link, $this->next_link);
320 
321  if($presentation->render())
322  {
323  $this->tpl->setVariable('SEARCH_RESULTS',$presentation->getHTML(true));
324  }
325  else
326  {
327  ilUtil::sendInfo(sprintf($this->lng->txt('search_no_match_hint'),$this->search_cache->getQuery()));
328  }
329  }
330 
334  protected function storeRoot()
335  {
336  $form = $this->getSearchAreaForm();
337 
338  $this->root_node = $form->getItemByPostVar('area')->getValue();
339  $this->search_cache->setRoot($this->root_node);
340  $this->search_cache->save();
341  $this->search_cache->deleteCachedEntries();
342 
343  include_once './Services/Object/classes/class.ilSubItemListGUI.php';
345 
346  $this->performSearch();
347  }
348 
352  protected function getTabs()
353  {
354  if(ilSearchSettings::getInstance()->getHideAdvancedSearch())
355  {
356  return false;
357  }
358 
359  $this->tabs_gui->addTarget('search',$this->ctrl->getLinkTarget($this));
360 
361  if(ilSearchSettings::getInstance()->isLuceneUserSearchEnabled())
362  {
363  $this->tabs_gui->addTarget('search_user',$this->ctrl->getLinkTargetByClass('illuceneusersearchgui'));
364  }
365 
366  if($this->fields->getActiveFields())
367  {
368  $this->tabs_gui->addTarget('search_advanced',$this->ctrl->getLinkTargetByClass('illuceneAdvancedSearchgui'));
369  }
370 
371  $this->tabs_gui->setTabActive('search');
372 
373  }
374 
381  protected function initUserSearchCache()
382  {
383  global $ilUser;
384 
385  include_once('Services/Search/classes/class.ilUserSearchCache.php');
386  $this->search_cache = ilUserSearchCache::_getInstance($ilUser->getId());
387  $this->search_cache->switchSearchType(ilUserSearchCache::LUCENE_DEFAULT);
388  if((int) $_GET['page_number'])
389  {
390  $this->search_cache->setResultPageNumber((int) $_GET['page_number']);
391  }
392  if(isset($_POST['term']))
393  {
394  $this->search_cache->setQuery(ilUtil::stripSlashes($_POST['term']));
395  if($_POST['item_filter_enabled'])
396  {
397  $filtered = array();
398  foreach(ilSearchSettings::getInstance()->getEnabledLuceneItemFilterDefinitions() as $type => $data)
399  {
400  if($_POST['filter_type'][$type])
401  {
402  $filtered[$type] = 1;
403  }
404  }
405  $this->search_cache->setItemFilter($filtered);
406 
407  // Mime filter
408  $mime = array();
409  foreach(ilSearchSettings::getInstance()->getEnabledLuceneMimeFilterDefinitions() as $type => $data)
410  {
411  if($_POST['filter_type'][$type])
412  {
413  $mime[$type] = 1;
414  }
415  }
416  $this->search_cache->setMimeFilter($mime);
417  }
418  else
419  {
420  // @todo: keep item filter settings
421  $this->search_cache->setItemFilter(array());
422  $this->search_cache->setMimeFilter(array());
423  }
424  }
425  }
426 
432  protected function fillAdminPanel()
433  {
434  global $lng;
435 
436  $adm_view_cmp = $adm_cmds = $creation_selector = $adm_view = false;
437 
438  // admin panel commands
439  if ((count($this->admin_panel_commands) > 0))
440  {
441  foreach($this->admin_panel_commands as $cmd)
442  {
443  $this->tpl->setCurrentBlock("lucene_admin_panel_cmd");
444  $this->tpl->setVariable("LUCENE_PANEL_CMD", $cmd["cmd"]);
445  $this->tpl->setVariable("LUCENE_TXT_PANEL_CMD", $cmd["txt"]);
446  $this->tpl->parseCurrentBlock();
447  }
448 
449  $adm_cmds = true;
450  }
451  if ($adm_cmds)
452  {
453  $this->tpl->setCurrentBlock("lucene_adm_view_components");
454  $this->tpl->setVariable("LUCENE_ADM_IMG_ARROW", ilUtil::getImagePath("arrow_upright.png"));
455  $this->tpl->setVariable("LUCENE_ADM_ALT_ARROW", $lng->txt("actions"));
456  $this->tpl->parseCurrentBlock();
457  $adm_view_cmp = true;
458  }
459 
460  // admin view button
461  if (is_array($this->admin_view_button))
462  {
463  if (is_array($this->admin_view_button))
464  {
465  $this->tpl->setCurrentBlock("lucene_admin_button");
466  $this->tpl->setVariable("LUCENE_ADMIN_MODE_LINK",
467  $this->admin_view_button["link"]);
468  $this->tpl->setVariable("LUCENE_TXT_ADMIN_MODE",
469  $this->admin_view_button["txt"]);
470  $this->tpl->parseCurrentBlock();
471  }
472  $this->tpl->setCurrentBlock("lucene_admin_view");
473  $this->tpl->parseCurrentBlock();
474  $adm_view = true;
475  }
476 
477  // creation selector
478  if (is_array($this->creation_selector))
479  {
480  $this->tpl->setCurrentBlock("lucene_add_commands");
481  if ($adm_cmds)
482  {
483  $this->tpl->setVariable("LUCENE_ADD_COM_WIDTH", 'width="1"');
484  }
485  $this->tpl->setVariable("LUCENE_SELECT_OBJTYPE_REPOS",
486  $this->creation_selector["options"]);
487  $this->tpl->setVariable("LUCENE_BTN_NAME_REPOS",
488  $this->creation_selector["command"]);
489  $this->tpl->setVariable("LUCENE_TXT_ADD_REPOS",
490  $this->creation_selector["txt"]);
491  $this->tpl->parseCurrentBlock();
492  $creation_selector = true;
493  }
494  if ($adm_view || $creation_selector)
495  {
496  $this->tpl->setCurrentBlock("lucene_adm_panel");
497  if ($adm_view_cmp)
498  {
499  $this->tpl->setVariable("LUCENE_ADM_TBL_WIDTH", 'width:"100%";');
500  }
501  $this->tpl->parseCurrentBlock();
502  }
503  }
504 
508  protected function addAdminPanelCommand($a_cmd, $a_txt)
509  {
510  $this->admin_panel_commands[] =
511  array("cmd" => $a_cmd, "txt" => $a_txt);
512  }
513 
517  protected function setAdminViewButton($a_link, $a_txt)
518  {
519  $this->admin_view_button =
520  array("link" => $a_link, "txt" => $a_txt);
521  }
522 
523  protected function setPageFormAction($a_action)
524  {
525  $this->page_form_action = $a_action;
526  }
527 
532  protected function showSearchForm()
533  {
534  global $ilCtrl, $lng;
535 
536  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.lucene_search.html','Services/Search');
537 
538  // include js needed
539  include_once("./Services/UIComponent/Overlay/classes/class.ilOverlayGUI.php");
541  $this->tpl->addJavascript("./Services/Search/js/Search.js");
542 
543  $this->tpl->setVariable("FORM_ACTION", $ilCtrl->getFormAction($this,'performSearch'));
544  $this->tpl->setVariable("TERM", ilUtil::prepareFormOutput($this->search_cache->getQuery()));
545  $this->tpl->setVariable("TXT_SEARCH", $lng->txt("search"));
546  $this->tpl->setVariable("TXT_OPTIONS", $lng->txt("options"));
547  $this->tpl->setVariable("ARR_IMG", ilUtil::img(ilUtil::getImagePath("mm_down_arrow_dark.png")));
548  $this->tpl->setVariable("TXT_COMBINATION", $lng->txt("search_term_combination"));
549  $this->tpl->setVariable('TXT_COMBINATION_DEFAULT', ilSearchSettings::getInstance()->getDefaultOperator() == ilSearchSettings::OPERATOR_AND ? $lng->txt('search_all_words') : $lng->txt('search_any_word'));
550  $this->tpl->setVariable("TXT_AREA", $lng->txt("search_area"));
551 
552  if (ilSearchSettings::getInstance()->isLuceneItemFilterEnabled())
553  {
554  $this->tpl->setCurrentBlock("type_sel");
555  $this->tpl->setVariable('TXT_TYPE_DEFAULT',$lng->txt("search_off"));
556  $this->tpl->setVariable("ARR_IMGT", ilUtil::img(ilUtil::getImagePath("mm_down_arrow_dark.png")));
557  $this->tpl->setVariable("TXT_FILTER_BY_TYPE", $lng->txt("search_filter_by_type"));
558  $this->tpl->setVariable('FORM',$this->form->getHTML());
559  $this->tpl->parseCurrentBlock();
560  }
561 
562  // search area form
563  $this->tpl->setVariable('SEARCH_AREA_FORM', $this->getSearchAreaForm()->getHTML());
564  $this->tpl->setVariable("TXT_CHANGE", $lng->txt("change"));
565 
566  return true;
567  }
568 }
569 ?>