ILIAS  Release_4_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 
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';
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($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';
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;
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  $dsSchema = array("resultsList" => 'response.results',
361  "fields" => array('term'));
362  $term->setDataSource($ilCtrl->getLinkTarget($this, "autoComplete"));
363  $term->setDataSourceSchema($dsSchema);
364  $term->setDataSourceResultFormat($dsFormatCallback);
365  $term->setDataSourceDelimiter($dsDelimiter);
366 
367  $this->form->addItem($term);
368 
369 
370  $path = new ilCustomInputGUI($this->lng->txt('search_area'),'root');
371  $tpl = new ilTemplate('tpl.root_selection.html',true,true,'Services/Search');
372  switch($this->search_cache->getRoot())
373  {
374  default:
375  $pathIds = $tree->getPathId($this->search_cache->getRoot(),ROOT_FOLDER_ID);
376  $counter = 0;
377  foreach($pathIds as $ref_id)
378  {
379  if($counter++) {
380  $tpl->touchBlock('path_separator');
381  }
382  if(($counter % 3) == 0) {
383  $tpl->touchBlock('line_break');
384  }
385  if($ref_id == ROOT_FOLDER_ID) {
386  $title = $this->lng->txt('search_in_magazin');
387  }
388  else {
390  }
391  $this->ctrl->setParameter($this,'root_id',$ref_id);
392  $tpl->setCurrentBlock('item');
393  $tpl->setVariable('ITEM_LINK',$this->ctrl->getLinkTarget($this,'selectRoot'));
394  $tpl->setVariable('NAME_WITH_DOTS',$title);
395  $tpl->parseCurrentBlock();
396  }
397  $tpl->setVariable('LINK_SELECT',$this->ctrl->getLinkTarget($this,'chooseRoot'));
398  $tpl->setVariable('TXT_CHANGE',$this->lng->txt('change'));
399  break;
400 
401  }
402  $path->setHTML($tpl->get());
403  $this->form->addItem($path);
404 
405  // object filter
406  $itemFilter = ilSearchSettings::getInstance()->getEnabledLuceneItemFilterDefinitions();
407  $currentFilter = $this->search_cache->getItemFilter();
408  if(count($itemFilter))
409  {
410  $if = new ilCheckboxInputGUI($this->lng->txt('search_item_filter'),'item_filter_enabled');
411  $if->setValue(1);
412  $if->setChecked(count($currentFilter));
413  $this->form->addItem($if);
414  foreach($itemFilter as $obj => $def)
415  {
416  $ch = new ilCheckboxInputGUI($this->lng->txt($def['trans']),'filter['.$obj.']');
417  $ch->setChecked(isset($currentFilter[$obj]));
418  $ch->setValue(1);
419  $if->addSubItem($ch);
420  }
421 
422  }
423 
424 
425 
426  return true;
427  }
428 
434  protected function chooseRoot()
435  {
436  global $tree;
437 
438  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.search_root_selector.html','Services/Search');
439 
440  include_once 'Services/Search/classes/class.ilSearchRootSelector.php';
441 
442  ilUtil::sendInfo($this->lng->txt('search_area_info'));
443 
444  $exp = new ilSearchRootSelector($this->ctrl->getLinkTarget($this,'chooseRoot'));
445  $exp->setTargetClass(get_class($this));
446  $exp->setExpand($_GET["search_root_expand"] ? $_GET["search_root_expand"] : $tree->readRootId());
447  $exp->setExpandTarget($this->ctrl->getLinkTarget($this,'chooseRoot'));
448 
449  // build html-output
450  $exp->setOutput(0);
451 
452  $this->tpl->setVariable("EXPLORER",$exp->getOutput());
453 
454  }
455 
460  protected function selectRoot()
461  {
462  $this->search_cache->setRoot((int) $_GET['root_id']);
463  $this->search_cache->save();
464  $this->search_cache->deleteCachedEntries();
465  // Reset details
466  include_once './Services/Object/classes/class.ilSubItemListGUI.php';
468  $this->showSearchForm();
469  }
470 
474  protected function getTabs()
475  {
476  if(ilSearchSettings::getInstance()->getHideAdvancedSearch())
477  {
478  return false;
479  }
480 
481  $this->tabs_gui->addTarget('search',$this->ctrl->getLinkTarget($this));
482  if($this->fields->getActiveFields())
483  {
484  $this->tabs_gui->addTarget('search_advanced',$this->ctrl->getLinkTargetByClass('illuceneAdvancedSearchgui'));
485  }
486 
487  $this->tabs_gui->setTabActive('search');
488 
489  }
490 
497  protected function initUserSearchCache()
498  {
499  global $ilUser;
500 
501  include_once('Services/Search/classes/class.ilUserSearchCache.php');
502  $this->search_cache = ilUserSearchCache::_getInstance($ilUser->getId());
503  $this->search_cache->switchSearchType(ilUserSearchCache::LUCENE_DEFAULT);
504  if((int) $_GET['page_number'])
505  {
506  $this->search_cache->setResultPageNumber((int) $_GET['page_number']);
507  }
508  if(isset($_POST['query']))
509  {
510  $this->search_cache->setQuery(ilUtil::stripSlashes($_POST['query']));
511  if($_POST['item_filter_enabled'])
512  {
513  $this->search_cache->setItemFilter($_POST['filter']);
514  }
515  else
516  {
517  $this->search_cache->setItemFilter(array());
518  }
519  }
520  }
521 
527  protected function fillAdminPanel()
528  {
529  global $lng;
530 
531  $adm_view_cmp = $adm_cmds = $creation_selector = $adm_view = false;
532 
533  // admin panel commands
534  if ((count($this->admin_panel_commands) > 0))
535  {
536  foreach($this->admin_panel_commands as $cmd)
537  {
538  $this->tpl->setCurrentBlock("lucene_admin_panel_cmd");
539  $this->tpl->setVariable("LUCENE_PANEL_CMD", $cmd["cmd"]);
540  $this->tpl->setVariable("LUCENE_TXT_PANEL_CMD", $cmd["txt"]);
541  $this->tpl->parseCurrentBlock();
542  }
543 
544  $adm_cmds = true;
545  }
546  if ($adm_cmds)
547  {
548  $this->tpl->setCurrentBlock("lucene_adm_view_components");
549  $this->tpl->setVariable("LUCENE_ADM_IMG_ARROW", ilUtil::getImagePath("arrow_upright.gif"));
550  $this->tpl->setVariable("LUCENE_ADM_ALT_ARROW", $lng->txt("actions"));
551  $this->tpl->parseCurrentBlock();
552  $adm_view_cmp = true;
553  }
554 
555  // admin view button
556  if (is_array($this->admin_view_button))
557  {
558  if (is_array($this->admin_view_button))
559  {
560  $this->tpl->setCurrentBlock("lucene_admin_button");
561  $this->tpl->setVariable("LUCENE_ADMIN_MODE_LINK",
562  $this->admin_view_button["link"]);
563  $this->tpl->setVariable("LUCENE_TXT_ADMIN_MODE",
564  $this->admin_view_button["txt"]);
565  $this->tpl->parseCurrentBlock();
566  }
567  $this->tpl->setCurrentBlock("lucene_admin_view");
568  $this->tpl->parseCurrentBlock();
569  $adm_view = true;
570  }
571 
572  // creation selector
573  if (is_array($this->creation_selector))
574  {
575  $this->tpl->setCurrentBlock("lucene_add_commands");
576  if ($adm_cmds)
577  {
578  $this->tpl->setVariable("LUCENE_ADD_COM_WIDTH", 'width="1"');
579  }
580  $this->tpl->setVariable("LUCENE_SELECT_OBJTYPE_REPOS",
581  $this->creation_selector["options"]);
582  $this->tpl->setVariable("LUCENE_BTN_NAME_REPOS",
583  $this->creation_selector["command"]);
584  $this->tpl->setVariable("LUCENE_TXT_ADD_REPOS",
585  $this->creation_selector["txt"]);
586  $this->tpl->parseCurrentBlock();
587  $creation_selector = true;
588  }
589  if ($adm_view || $creation_selector)
590  {
591  $this->tpl->setCurrentBlock("lucene_adm_panel");
592  if ($adm_view_cmp)
593  {
594  $this->tpl->setVariable("LUCENE_ADM_TBL_WIDTH", 'width:"100%";');
595  }
596  $this->tpl->parseCurrentBlock();
597  }
598  }
599 
603  protected function addAdminPanelCommand($a_cmd, $a_txt)
604  {
605  $this->admin_panel_commands[] =
606  array("cmd" => $a_cmd, "txt" => $a_txt);
607  }
608 
612  protected function setAdminViewButton($a_link, $a_txt)
613  {
614  $this->admin_view_button =
615  array("link" => $a_link, "txt" => $a_txt);
616  }
617 
618  protected function setPageFormAction($a_action)
619  {
620  $this->page_form_action = $a_action;
621  }
622 
623  protected function showSearchForm()
624  {
625  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.lucene_search.html','Services/Search');
626  $this->initFormSearch();
627  $this->tpl->setVariable('SEARCH_TABLE',$this->form->getHTML());
628  return true;
629  }
630 }
631 ?>