ILIAS  Release_4_1_x_branch Revision 61804
 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 
44 {
45  protected $ilTabs;
46 
50  public function __construct()
51  {
52  global $ilTabs;
53 
54  $this->tabs_gui = $ilTabs;
57  $this->initUserSearchCache();
58 
59  }
60 
64  public function executeCommand()
65  {
66  global $ilBench;
67 
68  $ilBench->start('Lucene','0900_executeCommand');
69  $next_class = $this->ctrl->getNextClass($this);
70  $cmd = $this->ctrl->getCmd();
71 
72  $this->prepareOutput();
73  switch($next_class)
74  {
75  case 'ilobjectcopygui':
76  include_once './Services/Object/classes/class.ilObjectCopyGUI.php';
77  $cp = new ilObjectCopyGUI($this);
78  $this->ctrl->forwardCommand($cp);
79  break;
80 
81  default:
82  if(!$cmd)
83  {
84  $cmd = "showSavedResults";
85  }
86  $this->handleCommand($cmd);
87  break;
88  }
89  $ilBench->stop('Lucene','0900_executeCommand');
90  return true;
91  }
92 
96  public function prepareOutput()
97  {
99  $this->getTabs();
100  return true;
101 
102  global $ilAccess, $ilSetting;
103  global $ilUser;
104 
105  if($_SESSION['il_cont_admin_panel'])
106  {
107  $GLOBALS["tpl"]->setAdminViewButton(
108  $this->ctrl->getLinkTarget($this, "disableAdministrationPanel"),
109  $this->lng->txt("basic_commands"));
110 
111  $GLOBALS["tpl"]->addAdminPanelCommand("delete",
112  $this->lng->txt("delete_selected_items"));
113 
114  if(!$_SESSION["clipboard"])
115  {
116  $GLOBALS["tpl"]->addAdminPanelCommand("cut",
117  $this->lng->txt("move_selected_items"));
118 
119  $GLOBALS["tpl"]->addAdminPanelCommand("link",
120  $this->lng->txt("link_selected_items"));
121  }
122  else
123  {
124  $GLOBALS["tpl"]->addAdminPanelCommand("paste",
125  $this->lng->txt("paste_clipboard_items"));
126  $GLOBALS["tpl"]->addAdminPanelCommand("clear",
127  $this->lng->txt("clear_clipboard"));
128  }
129  }
130  elseif($ilUser->getId() != ANONYMOUS_USER_ID)
131  {
132  $GLOBALS["tpl"]->setAdminViewButton(
133  $this->ctrl->getLinkTarget($this, "enableAdministrationPanel"),
134  $this->lng->txt("all_commands"));
135  }
136 
137  $this->ctrl->setParameter($this, "type", "");
138  $this->ctrl->setParameter($this, "item_ref_id", "");
139  $GLOBALS["tpl"]->setPageFormAction($this->ctrl->getFormAction($this));
140 
141  }
142 
143 
147  protected function remoteSearch()
148  {
149  $_POST['query'] = $_POST['queryString'];
150  $this->search_cache->setRoot((int) $_POST['root_id']);
151  $this->search_cache->setQuery(ilUtil::stripSlashes($_POST['queryString']));
152  $this->search_cache->save();
153 
154  $this->initFormSearch();
155  $this->search();
156  }
157 
162  protected function showSavedResults()
163  {
164  global $ilUser,$ilBench;
165 
166  $ilBench->start('Lucene','1000_savedResults');
167  $ilBench->start('Lucene','1000_qp');
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  $ilBench->stop('Lucene','1000_qp');
183 
184  $ilBench->start('Lucene','1100_lr');
185  // Load saved results
186  include_once './Services/Search/classes/Lucene/class.ilLuceneSearchResultFilter.php';
187  $filter = ilLuceneSearchResultFilter::getInstance($ilUser->getId());
188  $filter->loadFromDb();
189  $ilBench->stop('Lucene','1100_lr');
190 
191  // Highlight
192  $ilBench->start('Lucene','1200_hi');
193  $searcher->highlight($filter->getResultObjIds());
194  $ilBench->stop('Lucene','1200_hi');
195 
196  $ilBench->start('Lucene','1300_pr');
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  $ilBench->start('Lucene','1500_fo');
205  $this->addPager($filter,'max_page');
206  $ilBench->stop('Lucene','1500_fo');
207 
208  $presentation->setPreviousNext($this->prev_link, $this->next_link);
209  $ilBench->stop('Lucene','1300_pr');
210 
211  $ilBench->start('Lucene','1400_re');
212 
213  $this->showSearchForm();
214 
215  if($presentation->render())
216  {
217  $this->tpl->setVariable('SEARCH_RESULTS',$presentation->getHTML(true));
218  }
219  elseif(strlen($this->search_cache->getQuery()))
220  {
221  ilUtil::sendInfo(sprintf($this->lng->txt('search_no_match_hint'),$qp->getQuery()));
222  }
223  $ilBench->stop('Lucene','1400_re');
224 
225  $ilBench->stop('Lucene','1000_savedResults');
226  }
227 
232  protected function search()
233  {
234  $this->initFormSearch();
235 
236  if(!$this->form->checkInput())
237  {
238  $this->search_cache->deleteCachedEntries();
239  // Reset details
240  include_once './Services/Object/classes/class.ilSubItemListGUI.php';
242  $this->showSearchForm();
243  return false;
244  }
245 
246  unset($_SESSION['max_page']);
247  $this->search_cache->deleteCachedEntries();
248 
249  // Reset details
250  include_once './Services/Object/classes/class.ilSubItemListGUI.php';
252 
253  $this->performSearch();
254  }
255 
259  protected function performSearch()
260  {
261  global $ilUser,$ilBench;
262 
263  unset($_SESSION['vis_references']);
264 
265  $filter_query = '';
266  if($this->search_cache->getItemFilter())
267  {
268  $filter_settings = ilSearchSettings::getInstance()->getEnabledLuceneItemFilterDefinitions();
269  foreach((array) $this->search_cache->getItemFilter() as $obj => $value)
270  {
271  if(!$filter_query)
272  {
273  $filter_query .= '+( ';
274  }
275  else
276  {
277  $filter_query .= 'OR';
278  }
279  $filter_query .= (' '. (string) $filter_settings[$obj]['filter'].' ');
280  }
281  $filter_query .= ') ';
282  }
283 
284  include_once './Services/Search/classes/Lucene/class.ilLuceneSearcher.php';
285  include_once './Services/Search/classes/Lucene/class.ilLuceneQueryParser.php';
286  $qp = new ilLuceneQueryParser($filter_query.' '.$this->search_cache->getQuery());
287  $qp->parse();
288  $searcher = ilLuceneSearcher::getInstance($qp);
289  $searcher->search();
290 
291  // Filter results
292  $ilBench->start('Lucene','ResultFilter');
293  include_once './Services/Search/classes/Lucene/class.ilLuceneSearchResultFilter.php';
294  include_once './Services/Search/classes/Lucene/class.ilLucenePathFilter.php';
295  $filter = ilLuceneSearchResultFilter::getInstance($ilUser->getId());
296  $filter->addFilter(new ilLucenePathFilter($this->search_cache->getRoot()));
297  $filter->setCandidates($searcher->getResult());
298  $filter->filter();
299  $ilBench->stop('Lucene','ResultFilter');
300 
301  if($filter->getResultObjIds()) {
302  $searcher->highlight($filter->getResultObjIds());
303  }
304 
305  // Show results
306  $this->showSearchForm();
307 
308  include_once './Services/Search/classes/class.ilSearchResultPresentation.php';
309  $presentation = new ilSearchResultPresentation($this);
310  $presentation->setResults($filter->getResultIds());
311  $presentation->setSearcher($searcher);
312 
313  // TODO: other handling required
314  $ilBench->start('Lucene','1500_fo');
315  $this->addPager($filter,'max_page');
316  $ilBench->stop('Lucene','1500_fo');
317 
318  $presentation->setPreviousNext($this->prev_link, $this->next_link);
319 
320  if($presentation->render())
321  {
322  $this->tpl->setVariable('SEARCH_RESULTS',$presentation->getHTML(true));
323  }
324  else
325  {
326  ilUtil::sendInfo(sprintf($this->lng->txt('search_no_match_hint'),$this->search_cache->getQuery()));
327  }
328 
329  if($filter->getResultIds())
330  {
331  #$this->fillAdminPanel();
332  }
333  }
334 
338  protected function initFormSearch()
339  {
340  global $tree, $ilCtrl,$lng,$ilUser;
341 
342  include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
343 
344  if(is_object($this->form))
345  {
346  return true;
347  }
348 
349  $this->form = new ilPropertyFormGUI();
350  $this->form->setFormAction($this->ctrl->getFormAction($this,'search'));
351  $this->form->setTitle($this->lng->txt('search'));
352  $this->form->addCommandButton('search',$this->lng->txt('search'));
353 
354  include_once './Services/Search/classes/Form/class.ilLuceneQueryInputGUI.php';
355  $term = new ilLuceneQueryInputGUI($this->lng->txt('search_search_term'),'query');
356  $term->setValue($this->search_cache->getQuery());
357  $term->setSize(40);
358  $term->setMaxLength(255);
359  $term->setRequired(true);
360  if($lng->exists('search_form_hint'))
361  {
362  $term->setInfo($lng->txt('search_form_hint'));
363  }
364 
365  $dsSchema = array("resultsList" => 'response.results',
366  "fields" => array('term'));
367  $term->setDataSource($ilCtrl->getLinkTarget($this, "autoComplete"));
368  $term->setDataSourceSchema($dsSchema);
369  $term->setDataSourceResultFormat($dsFormatCallback);
370  $term->setDataSourceDelimiter($dsDelimiter);
371 
372  $this->form->addItem($term);
373 
374  if($ilUser->getId() != ANONYMOUS_USER_ID)
375  {
376  $path = new ilCustomInputGUI($this->lng->txt('search_area'),'root');
377  $tpl = new ilTemplate('tpl.root_selection.html',true,true,'Services/Search');
378  switch($this->search_cache->getRoot())
379  {
380  default:
381  $pathIds = $tree->getPathId($this->search_cache->getRoot(),ROOT_FOLDER_ID);
382  $counter = 0;
383  foreach($pathIds as $ref_id)
384  {
385  if($counter++) {
386  $tpl->touchBlock('path_separator');
387  }
388  if(($counter % 3) == 0) {
389  $tpl->touchBlock('line_break');
390  }
391  if($ref_id == ROOT_FOLDER_ID) {
392  $title = $this->lng->txt('search_in_magazin');
393  }
394  else {
396  }
397  $this->ctrl->setParameter($this,'root_id',$ref_id);
398  $tpl->setCurrentBlock('item');
399  $tpl->setVariable('ITEM_LINK',$this->ctrl->getLinkTarget($this,'selectRoot'));
400  $tpl->setVariable('NAME_WITH_DOTS',$title);
401  $tpl->parseCurrentBlock();
402  }
403  $tpl->setVariable('LINK_SELECT',$this->ctrl->getLinkTarget($this,'chooseRoot'));
404  $tpl->setVariable('TXT_CHANGE',$this->lng->txt('change'));
405  break;
406 
407  }
408  $path->setHTML($tpl->get());
409  $this->form->addItem($path);
410  }
411 
412  // object filter
413  $itemFilter = ilSearchSettings::getInstance()->getEnabledLuceneItemFilterDefinitions();
414  $currentFilter = $this->search_cache->getItemFilter();
415  if(count($itemFilter))
416  {
417  $if = new ilCheckboxInputGUI($this->lng->txt('search_item_filter'),'item_filter_enabled');
418  $if->setValue(1);
419  $if->setChecked(count($currentFilter));
420  $this->form->addItem($if);
421  foreach($itemFilter as $obj => $def)
422  {
423  $ch = new ilCheckboxInputGUI($this->lng->txt($def['trans']),'filter['.$obj.']');
424  $ch->setChecked(isset($currentFilter[$obj]));
425  $ch->setValue(1);
426  $if->addSubItem($ch);
427  }
428 
429  }
430 
431 
432 
433  return true;
434  }
435 
441  protected function chooseRoot()
442  {
443  global $tree;
444 
445  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.search_root_selector.html','Services/Search');
446 
447  include_once 'Services/Search/classes/class.ilSearchRootSelector.php';
448 
449  ilUtil::sendInfo($this->lng->txt('search_area_info'));
450 
451  $exp = new ilSearchRootSelector($this->ctrl->getLinkTarget($this,'chooseRoot'));
452  $exp->setTargetClass(get_class($this));
453  $exp->setExpand($_GET["search_root_expand"] ? $_GET["search_root_expand"] : $tree->readRootId());
454  $exp->setExpandTarget($this->ctrl->getLinkTarget($this,'chooseRoot'));
455 
456  // build html-output
457  $exp->setOutput(0);
458 
459  $this->tpl->setVariable("EXPLORER",$exp->getOutput());
460 
461  }
462 
467  protected function selectRoot()
468  {
469  $this->search_cache->setRoot((int) $_GET['root_id']);
470  $this->search_cache->save();
471  $this->search_cache->deleteCachedEntries();
472  // Reset details
473  include_once './Services/Object/classes/class.ilSubItemListGUI.php';
475  $this->showSearchForm();
476  }
477 
481  protected function getTabs()
482  {
483  if(ilSearchSettings::getInstance()->getHideAdvancedSearch())
484  {
485  return false;
486  }
487 
488  $this->tabs_gui->addTarget('search',$this->ctrl->getLinkTarget($this));
489  if($this->fields->getActiveFields())
490  {
491  $this->tabs_gui->addTarget('search_advanced',$this->ctrl->getLinkTargetByClass('illuceneAdvancedSearchgui'));
492  }
493 
494  $this->tabs_gui->setTabActive('search');
495 
496  }
497 
504  protected function initUserSearchCache()
505  {
506  global $ilUser;
507 
508  include_once('Services/Search/classes/class.ilUserSearchCache.php');
509  $this->search_cache = ilUserSearchCache::_getInstance($ilUser->getId());
510  $this->search_cache->switchSearchType(ilUserSearchCache::LUCENE_DEFAULT);
511  if((int) $_GET['page_number'])
512  {
513  $this->search_cache->setResultPageNumber((int) $_GET['page_number']);
514  }
515  if(isset($_POST['query']))
516  {
517  $this->search_cache->setQuery(ilUtil::stripSlashes($_POST['query']));
518  if($_POST['item_filter_enabled'])
519  {
520  $this->search_cache->setItemFilter($_POST['filter']);
521  }
522  else
523  {
524  $this->search_cache->setItemFilter(array());
525  }
526  }
527  }
528 
534  protected function fillAdminPanel()
535  {
536  global $lng;
537 
538  $adm_view_cmp = $adm_cmds = $creation_selector = $adm_view = false;
539 
540  // admin panel commands
541  if ((count($this->admin_panel_commands) > 0))
542  {
543  foreach($this->admin_panel_commands as $cmd)
544  {
545  $this->tpl->setCurrentBlock("lucene_admin_panel_cmd");
546  $this->tpl->setVariable("LUCENE_PANEL_CMD", $cmd["cmd"]);
547  $this->tpl->setVariable("LUCENE_TXT_PANEL_CMD", $cmd["txt"]);
548  $this->tpl->parseCurrentBlock();
549  }
550 
551  $adm_cmds = true;
552  }
553  if ($adm_cmds)
554  {
555  $this->tpl->setCurrentBlock("lucene_adm_view_components");
556  $this->tpl->setVariable("LUCENE_ADM_IMG_ARROW", ilUtil::getImagePath("arrow_upright.gif"));
557  $this->tpl->setVariable("LUCENE_ADM_ALT_ARROW", $lng->txt("actions"));
558  $this->tpl->parseCurrentBlock();
559  $adm_view_cmp = true;
560  }
561 
562  // admin view button
563  if (is_array($this->admin_view_button))
564  {
565  if (is_array($this->admin_view_button))
566  {
567  $this->tpl->setCurrentBlock("lucene_admin_button");
568  $this->tpl->setVariable("LUCENE_ADMIN_MODE_LINK",
569  $this->admin_view_button["link"]);
570  $this->tpl->setVariable("LUCENE_TXT_ADMIN_MODE",
571  $this->admin_view_button["txt"]);
572  $this->tpl->parseCurrentBlock();
573  }
574  $this->tpl->setCurrentBlock("lucene_admin_view");
575  $this->tpl->parseCurrentBlock();
576  $adm_view = true;
577  }
578 
579  // creation selector
580  if (is_array($this->creation_selector))
581  {
582  $this->tpl->setCurrentBlock("lucene_add_commands");
583  if ($adm_cmds)
584  {
585  $this->tpl->setVariable("LUCENE_ADD_COM_WIDTH", 'width="1"');
586  }
587  $this->tpl->setVariable("LUCENE_SELECT_OBJTYPE_REPOS",
588  $this->creation_selector["options"]);
589  $this->tpl->setVariable("LUCENE_BTN_NAME_REPOS",
590  $this->creation_selector["command"]);
591  $this->tpl->setVariable("LUCENE_TXT_ADD_REPOS",
592  $this->creation_selector["txt"]);
593  $this->tpl->parseCurrentBlock();
594  $creation_selector = true;
595  }
596  if ($adm_view || $creation_selector)
597  {
598  $this->tpl->setCurrentBlock("lucene_adm_panel");
599  if ($adm_view_cmp)
600  {
601  $this->tpl->setVariable("LUCENE_ADM_TBL_WIDTH", 'width:"100%";');
602  }
603  $this->tpl->parseCurrentBlock();
604  }
605  }
606 
610  protected function addAdminPanelCommand($a_cmd, $a_txt)
611  {
612  $this->admin_panel_commands[] =
613  array("cmd" => $a_cmd, "txt" => $a_txt);
614  }
615 
619  protected function setAdminViewButton($a_link, $a_txt)
620  {
621  $this->admin_view_button =
622  array("link" => $a_link, "txt" => $a_txt);
623  }
624 
625  protected function setPageFormAction($a_action)
626  {
627  $this->page_form_action = $a_action;
628  }
629 
630  protected function showSearchForm()
631  {
632  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.lucene_search.html','Services/Search');
633  $this->initFormSearch();
634  $this->tpl->setVariable('SEARCH_TABLE',$this->form->getHTML());
635  return true;
636  }
637 }
638 ?>