ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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;
56  parent::__construct();
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  $this->ctrl->setReturn($this,'');
87  include_once './Services/Object/classes/class.ilObjectCopyGUI.php';
88  $cp = new ilObjectCopyGUI($this);
89  $this->ctrl->forwardCommand($cp);
90  break;
91 
92  default:
94  if(!$cmd)
95  {
96  $cmd = "showSavedResults";
97  }
98  $this->handleCommand($cmd);
99  break;
100  }
101  $ilBench->stop('Lucene','0900_executeCommand');
102  return true;
103  }
104 
108  public function prepareOutput()
109  {
110  parent::prepareOutput();
111  $this->getTabs();
112  return true;
113  }
114 
120  protected function getType()
121  {
122  if(count($this->search_cache))
123  {
125  }
127  }
128 
134  protected function getDetails()
135  {
136  return (array) $this->search_cache->getItemFilter();
137  }
138 
144  protected function getMimeDetails()
145  {
146  return (array) $this->search_cache->getMimeFilter();
147  }
148 
152  protected function remoteSearch()
153  {
154  $query = trim(ilUtil::stripSlashes($_POST['queryString']));
155 
156  include_once './Services/Search/classes/Lucene/class.ilLuceneQueryParser.php';
157  $qp = new ilLuceneQueryParser($query);
158  $qp->parseAutoWildcard();
159 
160  $query = $qp->getQuery();
161 
162  $this->search_cache->setRoot((int) $_POST['root_id']);
163  $this->search_cache->setQuery(ilUtil::stripSlashes($query));
164  $this->search_cache->save();
165 
166  $this->search();
167  }
168 
173  protected function showSavedResults()
174  {
175  global $ilUser,$ilBench;
176 
177  if(!strlen($this->search_cache->getQuery()))
178  {
179  $this->showSearchForm();
180  return false;
181  }
182 
183  include_once './Services/Search/classes/Lucene/class.ilLuceneSearcher.php';
184  include_once './Services/Search/classes/Lucene/class.ilLuceneQueryParser.php';
185  $qp = new ilLuceneQueryParser($this->search_cache->getQuery());
186  $qp->parse();
187  $searcher = ilLuceneSearcher::getInstance($qp);
188  $searcher->search();
189 
190  // Load saved results
191  include_once './Services/Search/classes/Lucene/class.ilLuceneSearchResultFilter.php';
192  $filter = ilLuceneSearchResultFilter::getInstance($ilUser->getId());
193  $filter->loadFromDb();
194 
195  // Highlight
196  $searcher->highlight($filter->getResultObjIds());
197 
198  include_once './Services/Search/classes/class.ilSearchResultPresentation.php';
199  $presentation = new ilSearchResultPresentation($this);
200  $presentation->setResults($filter->getResultIds());
201 
202  $presentation->setSearcher($searcher);
203 
204  // TODO: other handling required
205  $this->addPager($filter,'max_page');
206 
207  $presentation->setPreviousNext($this->prev_link, $this->next_link);
208 
209  $this->showSearchForm();
210 
211  if($presentation->render())
212  {
213  $this->tpl->setVariable('SEARCH_RESULTS',$presentation->getHTML(true));
214  }
215  elseif(strlen($this->search_cache->getQuery()))
216  {
217  ilUtil::sendInfo(sprintf($this->lng->txt('search_no_match_hint'),$qp->getQuery()));
218  }
219  }
220 
225  protected function search()
226  {
227  if(!$this->form->checkInput())
228  {
229  $this->search_cache->deleteCachedEntries();
230  // Reset details
231  include_once './Services/Object/classes/class.ilSubItemListGUI.php';
233  $this->showSearchForm();
234  return false;
235  }
236 
237  unset($_SESSION['max_page']);
238  $this->search_cache->deleteCachedEntries();
239 
240  // Reset details
241  include_once './Services/Object/classes/class.ilSubItemListGUI.php';
243 
244  $this->performSearch();
245  }
246 
250  protected function performSearch()
251  {
252  global $ilUser,$ilBench;
253 
254  unset($_SESSION['vis_references']);
255 
256  $filter_query = '';
257  if($this->search_cache->getItemFilter() and ilSearchSettings::getInstance()->isLuceneItemFilterEnabled())
258  {
259  $filter_settings = ilSearchSettings::getInstance()->getEnabledLuceneItemFilterDefinitions();
260  foreach((array) $this->search_cache->getItemFilter() as $obj => $value)
261  {
262  if(!$filter_query)
263  {
264  $filter_query .= '+( ';
265  }
266  else
267  {
268  $filter_query .= 'OR';
269  }
270  $filter_query .= (' '. (string) $filter_settings[$obj]['filter'].' ');
271  }
272  $filter_query .= ') ';
273  }
274  // begin-patch mime_filter
275  $mime_query = '';
276  if($this->search_cache->getMimeFilter() and ilSearchSettings::getInstance()->isLuceneMimeFilterEnabled())
277  {
278  $filter_settings = ilSearchSettings::getInstance()->getEnabledLuceneMimeFilterDefinitions();
279  foreach($this->search_cache->getMimeFilter() as $mime => $value)
280  {
281  if(!$mime_query)
282  {
283  $mime_query .= '+( ';
284  }
285  else
286  {
287  $mime_query .= 'OR';
288  }
289  $mime_query .= (' '. (string) $filter_settings[$mime]['filter'].' ');
290  }
291  $mime_query .= ') ';
292  }
293 
294  // begin-patch creation_date
295  $cdate_query = $this->parseCreationFilter();
296 
297 
298 
299  $filter_query = $filter_query . ' '. $mime_query.' '.$cdate_query;
300 
301  include_once './Services/Search/classes/Lucene/class.ilLuceneSearcher.php';
302  include_once './Services/Search/classes/Lucene/class.ilLuceneQueryParser.php';
303 
304  $query = $this->search_cache->getQuery();
305  if($query)
306  {
307  $query = ' +('.$query.')';
308  }
309  $qp = new ilLuceneQueryParser($filter_query.$query);
310  $qp->parse();
311  $searcher = ilLuceneSearcher::getInstance($qp);
312  $searcher->search();
313 
314  // Filter results
315  include_once './Services/Search/classes/Lucene/class.ilLuceneSearchResultFilter.php';
316  include_once './Services/Search/classes/Lucene/class.ilLucenePathFilter.php';
317  $filter = ilLuceneSearchResultFilter::getInstance($ilUser->getId());
318  $filter->addFilter(new ilLucenePathFilter($this->search_cache->getRoot()));
319  $filter->setCandidates($searcher->getResult());
320  $filter->filter();
321 
322  if($filter->getResultObjIds()) {
323  $searcher->highlight($filter->getResultObjIds());
324  }
325 
326  // Show results
327  $this->showSearchForm();
328 
329  include_once './Services/Search/classes/class.ilSearchResultPresentation.php';
330  $presentation = new ilSearchResultPresentation($this);
331  $presentation->setResults($filter->getResultIds());
332  $presentation->setSearcher($searcher);
333 
334  // TODO: other handling required
335  $ilBench->start('Lucene','1500_fo');
336  $this->addPager($filter,'max_page');
337  $ilBench->stop('Lucene','1500_fo');
338 
339  $presentation->setPreviousNext($this->prev_link, $this->next_link);
340 
341  if($presentation->render())
342  {
343  $this->tpl->setVariable('SEARCH_RESULTS',$presentation->getHTML(true));
344  }
345  else
346  {
347  ilUtil::sendInfo(sprintf($this->lng->txt('search_no_match_hint'),$this->search_cache->getQuery()));
348  }
349  }
350 
354  protected function storeRoot()
355  {
356  $form = $this->getSearchAreaForm();
357 
358  $this->root_node = $form->getItemByPostVar('area')->getValue();
359  $this->search_cache->setRoot($this->root_node);
360  $this->search_cache->save();
361  $this->search_cache->deleteCachedEntries();
362 
363  include_once './Services/Object/classes/class.ilSubItemListGUI.php';
365 
366  $this->performSearch();
367  }
368 
372  protected function getTabs()
373  {
374  global $ilHelp;
375 
376  $ilHelp->setScreenIdComponent("src_luc");
377 
378  $this->tabs_gui->addTarget('search',$this->ctrl->getLinkTarget($this));
379 
380  if(ilSearchSettings::getInstance()->isLuceneUserSearchEnabled())
381  {
382  $this->tabs_gui->addTarget('search_user',$this->ctrl->getLinkTargetByClass('illuceneusersearchgui'));
383  }
384 
385  if($this->fields->getActiveFields() && !ilSearchSettings::getInstance()->getHideAdvancedSearch())
386  {
387  $this->tabs_gui->addTarget('search_advanced',$this->ctrl->getLinkTargetByClass('illuceneAdvancedSearchgui'));
388  }
389 
390  $this->tabs_gui->setTabActive('search');
391 
392  }
393 
400  protected function initUserSearchCache()
401  {
402  global $ilUser;
403 
404  include_once('Services/Search/classes/class.ilUserSearchCache.php');
405  $this->search_cache = ilUserSearchCache::_getInstance($ilUser->getId());
406  $this->search_cache->switchSearchType(ilUserSearchCache::LUCENE_DEFAULT);
407  if((int) $_GET['page_number'])
408  {
409  $this->search_cache->setResultPageNumber((int) $_GET['page_number']);
410  }
411  if(isset($_POST['term']))
412  {
413  $this->search_cache->setQuery(ilUtil::stripSlashes($_POST['term']));
414  if($_POST['item_filter_enabled'])
415  {
416  $filtered = array();
417  foreach(ilSearchSettings::getInstance()->getEnabledLuceneItemFilterDefinitions() as $type => $data)
418  {
419  if($_POST['filter_type'][$type])
420  {
421  $filtered[$type] = 1;
422  }
423  }
424  $this->search_cache->setItemFilter($filtered);
425 
426  // Mime filter
427  $mime = array();
428  foreach(ilSearchSettings::getInstance()->getEnabledLuceneMimeFilterDefinitions() as $type => $data)
429  {
430  if($_POST['filter_type'][$type])
431  {
432  $mime[$type] = 1;
433  }
434  }
435  $this->search_cache->setMimeFilter($mime);
436 
437 
438  }
439  $this->search_cache->setCreationFilter($this->loadCreationFilter());
440  if(!$_POST['item_filter_enabled'])
441  {
442  // @todo: keep item filter settings
443  $this->search_cache->setItemFilter(array());
444  $this->search_cache->setMimeFilter(array());
445  }
446  if(!$_POST['screation'])
447  {
448  $this->search_cache->setCreationFilter(array());
449  }
450  }
451  }
452 
458  protected function fillAdminPanel()
459  {
460  global $lng;
461 
462  $adm_view_cmp = $adm_cmds = $creation_selector = $adm_view = false;
463 
464  // admin panel commands
465  if ((count($this->admin_panel_commands) > 0))
466  {
467  foreach($this->admin_panel_commands as $cmd)
468  {
469  $this->tpl->setCurrentBlock("lucene_admin_panel_cmd");
470  $this->tpl->setVariable("LUCENE_PANEL_CMD", $cmd["cmd"]);
471  $this->tpl->setVariable("LUCENE_TXT_PANEL_CMD", $cmd["txt"]);
472  $this->tpl->parseCurrentBlock();
473  }
474 
475  $adm_cmds = true;
476  }
477  if ($adm_cmds)
478  {
479  $this->tpl->setCurrentBlock("lucene_adm_view_components");
480  $this->tpl->setVariable("LUCENE_ADM_IMG_ARROW", ilUtil::getImagePath("arrow_upright.svg"));
481  $this->tpl->setVariable("LUCENE_ADM_ALT_ARROW", $lng->txt("actions"));
482  $this->tpl->parseCurrentBlock();
483  $adm_view_cmp = true;
484  }
485 
486  // admin view button
487  if (is_array($this->admin_view_button))
488  {
489  if (is_array($this->admin_view_button))
490  {
491  $this->tpl->setCurrentBlock("lucene_admin_button");
492  $this->tpl->setVariable("LUCENE_ADMIN_MODE_LINK",
493  $this->admin_view_button["link"]);
494  $this->tpl->setVariable("LUCENE_TXT_ADMIN_MODE",
495  $this->admin_view_button["txt"]);
496  $this->tpl->parseCurrentBlock();
497  }
498  $this->tpl->setCurrentBlock("lucene_admin_view");
499  $this->tpl->parseCurrentBlock();
500  $adm_view = true;
501  }
502 
503  // creation selector
504  if (is_array($this->creation_selector))
505  {
506  $this->tpl->setCurrentBlock("lucene_add_commands");
507  if ($adm_cmds)
508  {
509  $this->tpl->setVariable("LUCENE_ADD_COM_WIDTH", 'width="1"');
510  }
511  $this->tpl->setVariable("LUCENE_SELECT_OBJTYPE_REPOS",
512  $this->creation_selector["options"]);
513  $this->tpl->setVariable("LUCENE_BTN_NAME_REPOS",
514  $this->creation_selector["command"]);
515  $this->tpl->setVariable("LUCENE_TXT_ADD_REPOS",
516  $this->creation_selector["txt"]);
517  $this->tpl->parseCurrentBlock();
518  $creation_selector = true;
519  }
520  if ($adm_view || $creation_selector)
521  {
522  $this->tpl->setCurrentBlock("lucene_adm_panel");
523  if ($adm_view_cmp)
524  {
525  $this->tpl->setVariable("LUCENE_ADM_TBL_WIDTH", 'width:"100%";');
526  }
527  $this->tpl->parseCurrentBlock();
528  }
529  }
530 
534  protected function addAdminPanelCommand($a_cmd, $a_txt)
535  {
536  $this->admin_panel_commands[] =
537  array("cmd" => $a_cmd, "txt" => $a_txt);
538  }
539 
543  protected function setAdminViewButton($a_link, $a_txt)
544  {
545  $this->admin_view_button =
546  array("link" => $a_link, "txt" => $a_txt);
547  }
548 
549  protected function setPageFormAction($a_action)
550  {
551  $this->page_form_action = $a_action;
552  }
553 
558  protected function showSearchForm()
559  {
560  global $ilCtrl, $lng;
561 
562  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.lucene_search.html','Services/Search');
563 
564  // include js needed
565  include_once("./Services/UIComponent/Overlay/classes/class.ilOverlayGUI.php");
567  $this->tpl->addJavascript("./Services/Search/js/Search.js");
568 
569  include_once("./Services/UIComponent/Glyph/classes/class.ilGlyphGUI.php");
570 
571  $this->tpl->setVariable("FORM_ACTION", $ilCtrl->getFormAction($this,'performSearch'));
572  $this->tpl->setVariable("TERM", ilUtil::prepareFormOutput($this->search_cache->getQuery()));
573  include_once("./Services/UIComponent/Button/classes/class.ilSubmitButton.php");
575  $btn->setCommand("performSearch");
576  $btn->setCaption("search");
577  $this->tpl->setVariable("SUBMIT_BTN",$btn->render());
578  $this->tpl->setVariable("TXT_OPTIONS", $lng->txt("options"));
579  $this->tpl->setVariable("ARR_IMG", ilGlyphGUI::get(ilGlyphGUI::CARET));
580  $this->tpl->setVariable("TXT_COMBINATION", $lng->txt("search_term_combination"));
581  $this->tpl->setVariable('TXT_COMBINATION_DEFAULT', ilSearchSettings::getInstance()->getDefaultOperator() == ilSearchSettings::OPERATOR_AND ? $lng->txt('search_all_words') : $lng->txt('search_any_word'));
582  $this->tpl->setVariable("TXT_AREA", $lng->txt("search_area"));
583 
584  if (ilSearchSettings::getInstance()->isLuceneItemFilterEnabled())
585  {
586  $this->tpl->setCurrentBlock("type_sel");
587  $this->tpl->setVariable('TXT_TYPE_DEFAULT',$lng->txt("search_off"));
588  $this->tpl->setVariable("ARR_IMGT", ilGlyphGUI::get(ilGlyphGUI::CARET));
589  $this->tpl->setVariable("TXT_FILTER_BY_TYPE", $lng->txt("search_filter_by_type"));
590  $this->tpl->setVariable('FORM',$this->form->getHTML());
591  $this->tpl->parseCurrentBlock();
592  }
593 
594  // search area form
595  $this->tpl->setVariable('SEARCH_AREA_FORM', $this->getSearchAreaForm()->getHTML());
596  $this->tpl->setVariable("TXT_CHANGE", $lng->txt("change"));
597 
598  if(ilSearchSettings::getInstance()->isDateFilterEnabled())
599  {
600  // begin-patch creation_date
601  $this->tpl->setVariable('TXT_FILTER_BY_CDATE',$this->lng->txt('search_filter_cd'));
602  $this->tpl->setVariable('TXT_CD_OFF',$this->lng->txt('search_off'));
603  $this->tpl->setVariable('FORM_CD',$this->getCreationDateForm()->getHTML());
604  $this->tpl->setVariable("ARR_IMG_CD", ilGlyphGUI::get(ilGlyphGUI::CARET));
605  // end-patch creation_date
606  }
607 
608 
609  return true;
610  }
611 
612 
617  protected function parseCreationFilter()
618  {
619  $options = $this->search_cache->getCreationFilter();
620 
621  if(!$options['enabled'])
622  {
623  return '';
624  }
625  $limit = new ilDate($options['date'],IL_CAL_UNIX);
626 
627  switch($options['ontype'])
628  {
629  case 1:
630  // after
631  $limit->increment(IL_CAL_DAY, 1);
632  $now = new ilDate(time(),IL_CAL_UNIX);
633  return '+(cdate:['.$limit->get(IL_CAL_DATE).' TO '.$now->get(IL_CAL_DATE).'*]) ';
634 
635  case 2:
636  // before
637  return '+(cdate:[* TO '.$limit->get(IL_CAL_DATE).']) ';
638 
639  case 3:
640  // on
641  return '+(cdate:'.$limit->get(IL_CAL_DATE).'*) ';
642 
643  }
644  return '';
645  }
646  // end-patch creation_date
647 }
648 ?>
showSearchForm()
Show search form.
getType()
Get type of search (details | fast)
static prepareFormOutput($a_str, $a_strip=false)
prepares string output for html forms public
$_POST['username']
Definition: cron.php:12
getSearchAreaForm()
Init standard search form.
remoteSearch()
Search from main menu.
$_SESSION["AccountId"]
getDetails()
Needed for base class search form.
GUI class for the workflow of copying objects.
parse()
parse query string
$_GET["client_id"]
static getInstance(ilLuceneQueryParser $qp)
Get singleton instance.
$cmd
Definition: sahs_server.php:35
static get($a_glyph, $a_text="")
Get glyph html.
parseCreationFilter()
Parse creation date.
getMimeDetails()
Needed for base class search form.
$errors fields
Definition: imgupload.php:48
executeCommand()
Execute Command.
const IL_CAL_UNIX
prepareOutput()
Add admin panel command.
static resetDetails()
reset details As long as static::resetDetails is not possible this method is final ...
global $ilCtrl
Definition: ilias.php:18
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
GUI for simple Lucene search
setAdminViewButton($a_link, $a_txt)
Show admin view button.
showSavedResults()
Show saved results.
const IL_CAL_DAY
static _getInstance($a_usr_id)
Get singleton instance.
$data
addPager($result, $a_session_key)
Add Pager.
Class for single dates.
if(!is_array($argv)) $options
Presentation of search results using object list gui.
static getImagePath($img, $module_path="", $mode="output", $offline=false)
get image path (for images located in a template directory)
initStandardSearchForm($a_mode)
Init standard search form.
addAdminPanelCommand($a_cmd, $a_txt)
Add a command to the admin panel.
performSearch()
Perform search.
handleCommand($a_cmd)
Handle command.
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
search()
Search (button pressed)
storeRoot()
Store new root node.
initUserSearchCache()
Init user search cache.
const IL_CAL_DATE
global $ilUser
Definition: imgupload.php:15
global $ilBench
Definition: ilias.php:18
fillAdminPanel()
Put admin panel into template:
static getInstance()
Get singleton instance.
static initJavascript()
Init javascript.