ILIAS  Release_5_0_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  $query = trim(ilUtil::stripSlashes($_POST['queryString']));
154 
155  include_once './Services/Search/classes/Lucene/class.ilLuceneQueryParser.php';
156  $qp = new ilLuceneQueryParser($query);
157  $qp->parseAutoWildcard();
158 
159  $query = $qp->getQuery();
160 
161  $this->search_cache->setRoot((int) $_POST['root_id']);
162  $this->search_cache->setQuery(ilUtil::stripSlashes($query));
163  $this->search_cache->save();
164 
165  $this->search();
166  }
167 
172  protected function showSavedResults()
173  {
174  global $ilUser,$ilBench;
175 
176  if(!strlen($this->search_cache->getQuery()))
177  {
178  $this->showSearchForm();
179  return false;
180  }
181 
182  include_once './Services/Search/classes/Lucene/class.ilLuceneSearcher.php';
183  include_once './Services/Search/classes/Lucene/class.ilLuceneQueryParser.php';
184  $qp = new ilLuceneQueryParser($this->search_cache->getQuery());
185  $qp->parse();
186  $searcher = ilLuceneSearcher::getInstance($qp);
187  $searcher->search();
188 
189  // Load saved results
190  include_once './Services/Search/classes/Lucene/class.ilLuceneSearchResultFilter.php';
191  $filter = ilLuceneSearchResultFilter::getInstance($ilUser->getId());
192  $filter->loadFromDb();
193 
194  // Highlight
195  $searcher->highlight($filter->getResultObjIds());
196 
197  include_once './Services/Search/classes/class.ilSearchResultPresentation.php';
198  $presentation = new ilSearchResultPresentation($this);
199  $presentation->setResults($filter->getResultIds());
200 
201  $presentation->setSearcher($searcher);
202 
203  // TODO: other handling required
204  $this->addPager($filter,'max_page');
205 
206  $presentation->setPreviousNext($this->prev_link, $this->next_link);
207 
208  $this->showSearchForm();
209 
210  if($presentation->render())
211  {
212  $this->tpl->setVariable('SEARCH_RESULTS',$presentation->getHTML(true));
213  }
214  elseif(strlen($this->search_cache->getQuery()))
215  {
216  ilUtil::sendInfo(sprintf($this->lng->txt('search_no_match_hint'),$qp->getQuery()));
217  }
218  }
219 
224  protected function search()
225  {
226  if(!$this->form->checkInput())
227  {
228  $this->search_cache->deleteCachedEntries();
229  // Reset details
230  include_once './Services/Object/classes/class.ilSubItemListGUI.php';
232  $this->showSearchForm();
233  return false;
234  }
235 
236  unset($_SESSION['max_page']);
237  $this->search_cache->deleteCachedEntries();
238 
239  // Reset details
240  include_once './Services/Object/classes/class.ilSubItemListGUI.php';
242 
243  $this->performSearch();
244  }
245 
249  protected function performSearch()
250  {
251  global $ilUser,$ilBench;
252 
253  unset($_SESSION['vis_references']);
254 
255  $filter_query = '';
256  if($this->search_cache->getItemFilter() and ilSearchSettings::getInstance()->isLuceneItemFilterEnabled())
257  {
258  $filter_settings = ilSearchSettings::getInstance()->getEnabledLuceneItemFilterDefinitions();
259  foreach((array) $this->search_cache->getItemFilter() as $obj => $value)
260  {
261  if(!$filter_query)
262  {
263  $filter_query .= '+( ';
264  }
265  else
266  {
267  $filter_query .= 'OR';
268  }
269  $filter_query .= (' '. (string) $filter_settings[$obj]['filter'].' ');
270  }
271  $filter_query .= ') ';
272  }
273  // begin-patch mime_filter
274  $mime_query = '';
275  if($this->search_cache->getMimeFilter() and ilSearchSettings::getInstance()->isLuceneMimeFilterEnabled())
276  {
277  $filter_settings = ilSearchSettings::getInstance()->getEnabledLuceneMimeFilterDefinitions();
278  foreach($this->search_cache->getMimeFilter() as $mime => $value)
279  {
280  if(!$mime_query)
281  {
282  $mime_query .= '+( ';
283  }
284  else
285  {
286  $mime_query .= 'OR';
287  }
288  $mime_query .= (' '. (string) $filter_settings[$mime]['filter'].' ');
289  }
290  $mime_query .= ') ';
291  }
292  $filter_query = $filter_query . ' '. $mime_query;
293 
294  include_once './Services/Search/classes/Lucene/class.ilLuceneSearcher.php';
295  include_once './Services/Search/classes/Lucene/class.ilLuceneQueryParser.php';
296  $qp = new ilLuceneQueryParser($filter_query.' +('.$this->search_cache->getQuery().')');
297  $qp->parse();
298  $searcher = ilLuceneSearcher::getInstance($qp);
299  $searcher->search();
300 
301  // Filter results
302  include_once './Services/Search/classes/Lucene/class.ilLuceneSearchResultFilter.php';
303  include_once './Services/Search/classes/Lucene/class.ilLucenePathFilter.php';
304  $filter = ilLuceneSearchResultFilter::getInstance($ilUser->getId());
305  $filter->addFilter(new ilLucenePathFilter($this->search_cache->getRoot()));
306  $filter->setCandidates($searcher->getResult());
307  $filter->filter();
308 
309  if($filter->getResultObjIds()) {
310  $searcher->highlight($filter->getResultObjIds());
311  }
312 
313  // Show results
314  $this->showSearchForm();
315 
316  include_once './Services/Search/classes/class.ilSearchResultPresentation.php';
317  $presentation = new ilSearchResultPresentation($this);
318  $presentation->setResults($filter->getResultIds());
319  $presentation->setSearcher($searcher);
320 
321  // TODO: other handling required
322  $ilBench->start('Lucene','1500_fo');
323  $this->addPager($filter,'max_page');
324  $ilBench->stop('Lucene','1500_fo');
325 
326  $presentation->setPreviousNext($this->prev_link, $this->next_link);
327 
328  if($presentation->render())
329  {
330  $this->tpl->setVariable('SEARCH_RESULTS',$presentation->getHTML(true));
331  }
332  else
333  {
334  ilUtil::sendInfo(sprintf($this->lng->txt('search_no_match_hint'),$this->search_cache->getQuery()));
335  }
336  }
337 
341  protected function storeRoot()
342  {
343  $form = $this->getSearchAreaForm();
344 
345  $this->root_node = $form->getItemByPostVar('area')->getValue();
346  $this->search_cache->setRoot($this->root_node);
347  $this->search_cache->save();
348  $this->search_cache->deleteCachedEntries();
349 
350  include_once './Services/Object/classes/class.ilSubItemListGUI.php';
352 
353  $this->performSearch();
354  }
355 
359  protected function getTabs()
360  {
361  global $ilHelp;
362 
363  $ilHelp->setScreenIdComponent("src_luc");
364 
365  $this->tabs_gui->addTarget('search',$this->ctrl->getLinkTarget($this));
366 
367  if(ilSearchSettings::getInstance()->isLuceneUserSearchEnabled())
368  {
369  $this->tabs_gui->addTarget('search_user',$this->ctrl->getLinkTargetByClass('illuceneusersearchgui'));
370  }
371 
372  if($this->fields->getActiveFields() && !ilSearchSettings::getInstance()->getHideAdvancedSearch())
373  {
374  $this->tabs_gui->addTarget('search_advanced',$this->ctrl->getLinkTargetByClass('illuceneAdvancedSearchgui'));
375  }
376 
377  $this->tabs_gui->setTabActive('search');
378 
379  }
380 
387  protected function initUserSearchCache()
388  {
389  global $ilUser;
390 
391  include_once('Services/Search/classes/class.ilUserSearchCache.php');
392  $this->search_cache = ilUserSearchCache::_getInstance($ilUser->getId());
393  $this->search_cache->switchSearchType(ilUserSearchCache::LUCENE_DEFAULT);
394  if((int) $_GET['page_number'])
395  {
396  $this->search_cache->setResultPageNumber((int) $_GET['page_number']);
397  }
398  if(isset($_POST['term']))
399  {
400  $this->search_cache->setQuery(ilUtil::stripSlashes($_POST['term']));
401  if($_POST['item_filter_enabled'])
402  {
403  $filtered = array();
404  foreach(ilSearchSettings::getInstance()->getEnabledLuceneItemFilterDefinitions() as $type => $data)
405  {
406  if($_POST['filter_type'][$type])
407  {
408  $filtered[$type] = 1;
409  }
410  }
411  $this->search_cache->setItemFilter($filtered);
412 
413  // Mime filter
414  $mime = array();
415  foreach(ilSearchSettings::getInstance()->getEnabledLuceneMimeFilterDefinitions() as $type => $data)
416  {
417  if($_POST['filter_type'][$type])
418  {
419  $mime[$type] = 1;
420  }
421  }
422  $this->search_cache->setMimeFilter($mime);
423  }
424  else
425  {
426  // @todo: keep item filter settings
427  $this->search_cache->setItemFilter(array());
428  $this->search_cache->setMimeFilter(array());
429  }
430  }
431  }
432 
438  protected function fillAdminPanel()
439  {
440  global $lng;
441 
442  $adm_view_cmp = $adm_cmds = $creation_selector = $adm_view = false;
443 
444  // admin panel commands
445  if ((count($this->admin_panel_commands) > 0))
446  {
447  foreach($this->admin_panel_commands as $cmd)
448  {
449  $this->tpl->setCurrentBlock("lucene_admin_panel_cmd");
450  $this->tpl->setVariable("LUCENE_PANEL_CMD", $cmd["cmd"]);
451  $this->tpl->setVariable("LUCENE_TXT_PANEL_CMD", $cmd["txt"]);
452  $this->tpl->parseCurrentBlock();
453  }
454 
455  $adm_cmds = true;
456  }
457  if ($adm_cmds)
458  {
459  $this->tpl->setCurrentBlock("lucene_adm_view_components");
460  $this->tpl->setVariable("LUCENE_ADM_IMG_ARROW", ilUtil::getImagePath("arrow_upright.svg"));
461  $this->tpl->setVariable("LUCENE_ADM_ALT_ARROW", $lng->txt("actions"));
462  $this->tpl->parseCurrentBlock();
463  $adm_view_cmp = true;
464  }
465 
466  // admin view button
467  if (is_array($this->admin_view_button))
468  {
469  if (is_array($this->admin_view_button))
470  {
471  $this->tpl->setCurrentBlock("lucene_admin_button");
472  $this->tpl->setVariable("LUCENE_ADMIN_MODE_LINK",
473  $this->admin_view_button["link"]);
474  $this->tpl->setVariable("LUCENE_TXT_ADMIN_MODE",
475  $this->admin_view_button["txt"]);
476  $this->tpl->parseCurrentBlock();
477  }
478  $this->tpl->setCurrentBlock("lucene_admin_view");
479  $this->tpl->parseCurrentBlock();
480  $adm_view = true;
481  }
482 
483  // creation selector
484  if (is_array($this->creation_selector))
485  {
486  $this->tpl->setCurrentBlock("lucene_add_commands");
487  if ($adm_cmds)
488  {
489  $this->tpl->setVariable("LUCENE_ADD_COM_WIDTH", 'width="1"');
490  }
491  $this->tpl->setVariable("LUCENE_SELECT_OBJTYPE_REPOS",
492  $this->creation_selector["options"]);
493  $this->tpl->setVariable("LUCENE_BTN_NAME_REPOS",
494  $this->creation_selector["command"]);
495  $this->tpl->setVariable("LUCENE_TXT_ADD_REPOS",
496  $this->creation_selector["txt"]);
497  $this->tpl->parseCurrentBlock();
498  $creation_selector = true;
499  }
500  if ($adm_view || $creation_selector)
501  {
502  $this->tpl->setCurrentBlock("lucene_adm_panel");
503  if ($adm_view_cmp)
504  {
505  $this->tpl->setVariable("LUCENE_ADM_TBL_WIDTH", 'width:"100%";');
506  }
507  $this->tpl->parseCurrentBlock();
508  }
509  }
510 
514  protected function addAdminPanelCommand($a_cmd, $a_txt)
515  {
516  $this->admin_panel_commands[] =
517  array("cmd" => $a_cmd, "txt" => $a_txt);
518  }
519 
523  protected function setAdminViewButton($a_link, $a_txt)
524  {
525  $this->admin_view_button =
526  array("link" => $a_link, "txt" => $a_txt);
527  }
528 
529  protected function setPageFormAction($a_action)
530  {
531  $this->page_form_action = $a_action;
532  }
533 
538  protected function showSearchForm()
539  {
540  global $ilCtrl, $lng;
541 
542  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.lucene_search.html','Services/Search');
543 
544  // include js needed
545  include_once("./Services/UIComponent/Overlay/classes/class.ilOverlayGUI.php");
547  $this->tpl->addJavascript("./Services/Search/js/Search.js");
548 
549  include_once("./Services/UIComponent/Glyph/classes/class.ilGlyphGUI.php");
550 
551  $this->tpl->setVariable("FORM_ACTION", $ilCtrl->getFormAction($this,'performSearch'));
552  $this->tpl->setVariable("TERM", ilUtil::prepareFormOutput($this->search_cache->getQuery()));
553  include_once("./Services/UIComponent/Button/classes/class.ilSubmitButton.php");
555  $btn->setCommand("performSearch");
556  $btn->setCaption("search");
557  $this->tpl->setVariable("SUBMIT_BTN",$btn->render());
558  $this->tpl->setVariable("TXT_OPTIONS", $lng->txt("options"));
559  $this->tpl->setVariable("ARR_IMG", ilGlyphGUI::get(ilGlyphGUI::CARET));
560  $this->tpl->setVariable("TXT_COMBINATION", $lng->txt("search_term_combination"));
561  $this->tpl->setVariable('TXT_COMBINATION_DEFAULT', ilSearchSettings::getInstance()->getDefaultOperator() == ilSearchSettings::OPERATOR_AND ? $lng->txt('search_all_words') : $lng->txt('search_any_word'));
562  $this->tpl->setVariable("TXT_AREA", $lng->txt("search_area"));
563 
564  if (ilSearchSettings::getInstance()->isLuceneItemFilterEnabled())
565  {
566  $this->tpl->setCurrentBlock("type_sel");
567  $this->tpl->setVariable('TXT_TYPE_DEFAULT',$lng->txt("search_off"));
568  $this->tpl->setVariable("ARR_IMGT", ilGlyphGUI::get(ilGlyphGUI::CARET));
569  $this->tpl->setVariable("TXT_FILTER_BY_TYPE", $lng->txt("search_filter_by_type"));
570  $this->tpl->setVariable('FORM',$this->form->getHTML());
571  $this->tpl->parseCurrentBlock();
572  }
573 
574  // search area form
575  $this->tpl->setVariable('SEARCH_AREA_FORM', $this->getSearchAreaForm()->getHTML());
576  $this->tpl->setVariable("TXT_CHANGE", $lng->txt("change"));
577 
578  return true;
579  }
580 }
581 ?>