ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 $DIC;
54 
55  $ilTabs = $DIC['ilTabs'];
56 
57  $this->tabs_gui = $ilTabs;
58  parent::__construct();
60  $this->initUserSearchCache();
61  }
62 
66  public function executeCommand()
67  {
68  global $DIC;
69 
70  $ilBench = $DIC['ilBench'];
71  $ilCtrl = $DIC['ilCtrl'];
72 
73  $ilBench->start('Lucene', '0900_executeCommand');
74  $next_class = $this->ctrl->getNextClass($this);
75  $cmd = $this->ctrl->getCmd();
76 
77  $this->prepareOutput();
78  switch ($next_class) {
79  case "ilpropertyformgui":
80  /*$this->initStandardSearchForm(ilSearchBaseGUI::SEARCH_FORM_LUCENE);
81  $ilCtrl->setReturn($this, 'storeRoot');
82  $ilCtrl->forwardCommand($this->form);*/
83  $form = $this->getSearchAreaForm();
84  $ilCtrl->setReturn($this, 'storeRoot');
85  $ilCtrl->forwardCommand($form);
86  break;
87 
88  case 'ilobjectcopygui':
89  $this->ctrl->setReturn($this, '');
90  include_once './Services/Object/classes/class.ilObjectCopyGUI.php';
91  $cp = new ilObjectCopyGUI($this);
92  $this->ctrl->forwardCommand($cp);
93  break;
94 
95  default:
97  if (!$cmd) {
98  $cmd = "showSavedResults";
99  }
100  $this->handleCommand($cmd);
101  break;
102  }
103  $ilBench->stop('Lucene', '0900_executeCommand');
104  return true;
105  }
106 
110  public function prepareOutput()
111  {
112  parent::prepareOutput();
113  $this->getTabs();
114  return true;
115  }
116 
122  protected function getType()
123  {
124  if (count($this->search_cache)) {
126  }
128  }
129 
135  protected function getDetails()
136  {
137  return (array) $this->search_cache->getItemFilter();
138  }
139 
145  protected function getMimeDetails()
146  {
147  return (array) $this->search_cache->getMimeFilter();
148  }
149 
153  protected function remoteSearch()
154  {
155  $query = trim(ilUtil::stripSlashes($_POST['queryString']));
156 
157  include_once './Services/Search/classes/Lucene/class.ilLuceneQueryParser.php';
158  $qp = new ilLuceneQueryParser($query);
159  $qp->parseAutoWildcard();
160 
161  $query = $qp->getQuery();
162 
163  $this->search_cache->setRoot((int) $_POST['root_id']);
164  $this->search_cache->setQuery(ilUtil::stripSlashes($query));
165  $this->search_cache->save();
166 
167  $this->search();
168  }
169 
174  protected function showSavedResults()
175  {
176  global $DIC;
177 
178  $ilUser = $DIC['ilUser'];
179  $ilBench = $DIC['ilBench'];
180 
181  if (!strlen($this->search_cache->getQuery())) {
182  $this->showSearchForm();
183  return false;
184  }
185 
186  include_once './Services/Search/classes/Lucene/class.ilLuceneSearcher.php';
187  include_once './Services/Search/classes/Lucene/class.ilLuceneQueryParser.php';
188  $qp = new ilLuceneQueryParser($this->search_cache->getQuery());
189  $qp->parse();
190  $searcher = ilLuceneSearcher::getInstance($qp);
191  $searcher->search();
192 
193  // Load saved results
194  include_once './Services/Search/classes/Lucene/class.ilLuceneSearchResultFilter.php';
196  $filter->loadFromDb();
197 
198  // Highlight
199  $searcher->highlight($filter->getResultObjIds());
200 
201  include_once './Services/Search/classes/class.ilSearchResultPresentation.php';
202  $presentation = new ilSearchResultPresentation($this);
203  $presentation->setResults($filter->getResultIds());
204 
205  $presentation->setSearcher($searcher);
206 
207  // TODO: other handling required
208  $this->addPager($filter, 'max_page');
209 
210  $presentation->setPreviousNext($this->prev_link, $this->next_link);
211 
212  $this->showSearchForm();
213 
214  if ($presentation->render()) {
215  $this->tpl->setVariable('SEARCH_RESULTS', $presentation->getHTML(true));
216  } elseif (strlen($this->search_cache->getQuery())) {
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  $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 $DIC;
252 
253  $ilUser = $DIC['ilUser'];
254  $ilBench = $DIC['ilBench'];
255 
256  unset($_SESSION['vis_references']);
257 
258  $filter_query = '';
259  if ($this->search_cache->getItemFilter() and ilSearchSettings::getInstance()->isLuceneItemFilterEnabled()) {
260  $filter_settings = ilSearchSettings::getInstance()->getEnabledLuceneItemFilterDefinitions();
261  foreach ((array) $this->search_cache->getItemFilter() as $obj => $value) {
262  if (!$filter_query) {
263  $filter_query .= '+( ';
264  } else {
265  $filter_query .= 'OR';
266  }
267  $filter_query .= (' ' . (string) $filter_settings[$obj]['filter'] . ' ');
268  }
269  $filter_query .= ') ';
270  }
271  // begin-patch mime_filter
272  $mime_query = '';
273  if ($this->search_cache->getMimeFilter() and ilSearchSettings::getInstance()->isLuceneMimeFilterEnabled()) {
274  $filter_settings = ilSearchSettings::getInstance()->getEnabledLuceneMimeFilterDefinitions();
275  foreach ($this->search_cache->getMimeFilter() as $mime => $value) {
276  if (!$mime_query) {
277  $mime_query .= '+( ';
278  } else {
279  $mime_query .= 'OR';
280  }
281  $mime_query .= (' ' . (string) $filter_settings[$mime]['filter'] . ' ');
282  }
283  $mime_query .= ') ';
284  }
285 
286  // begin-patch creation_date
287  $cdate_query = $this->parseCreationFilter();
288 
289 
290 
291  $filter_query = $filter_query . ' ' . $mime_query . ' ' . $cdate_query;
292 
293  include_once './Services/Search/classes/Lucene/class.ilLuceneSearcher.php';
294  include_once './Services/Search/classes/Lucene/class.ilLuceneQueryParser.php';
295 
296  $query = $this->search_cache->getQuery();
297  if ($query) {
298  $query = ' +(' . $query . ')';
299  }
300  $qp = new ilLuceneQueryParser($filter_query . $query);
301  $qp->parse();
302  $searcher = ilLuceneSearcher::getInstance($qp);
303  $searcher->search();
304 
305  // Filter results
306  include_once './Services/Search/classes/Lucene/class.ilLuceneSearchResultFilter.php';
307  include_once './Services/Search/classes/Lucene/class.ilLucenePathFilter.php';
309  $filter->addFilter(new ilLucenePathFilter($this->search_cache->getRoot()));
310  $filter->setCandidates($searcher->getResult());
311  $filter->filter();
312 
313  if ($filter->getResultObjIds()) {
314  $searcher->highlight($filter->getResultObjIds());
315  }
316 
317  // Show results
318  $this->showSearchForm();
319 
320  include_once './Services/Search/classes/class.ilSearchResultPresentation.php';
321  $presentation = new ilSearchResultPresentation($this);
322  $presentation->setResults($filter->getResultIds());
323  $presentation->setSearcher($searcher);
324 
325  // TODO: other handling required
326  $ilBench->start('Lucene', '1500_fo');
327  $this->addPager($filter, 'max_page');
328  $ilBench->stop('Lucene', '1500_fo');
329 
330  $presentation->setPreviousNext($this->prev_link, $this->next_link);
331 
332  if ($presentation->render()) {
333  $this->tpl->setVariable('SEARCH_RESULTS', $presentation->getHTML(true));
334  } else {
335  ilUtil::sendInfo(sprintf($this->lng->txt('search_no_match_hint'), $this->search_cache->getQuery()));
336  }
337  }
338 
342  protected function storeRoot()
343  {
344  $form = $this->getSearchAreaForm();
345 
346  $this->root_node = $form->getItemByPostVar('area')->getValue();
347  $this->search_cache->setRoot($this->root_node);
348  $this->search_cache->save();
349  $this->search_cache->deleteCachedEntries();
350 
351  include_once './Services/Object/classes/class.ilSubItemListGUI.php';
353 
354  $this->performSearch();
355  }
356 
360  protected function getTabs()
361  {
362  global $DIC;
363 
364  $ilHelp = $DIC['ilHelp'];
365 
366  $ilHelp->setScreenIdComponent("src_luc");
367 
368  $this->tabs_gui->addTarget('search', $this->ctrl->getLinkTarget($this));
369 
370  if (ilSearchSettings::getInstance()->isLuceneUserSearchEnabled()) {
371  $this->tabs_gui->addTarget('search_user', $this->ctrl->getLinkTargetByClass('illuceneusersearchgui'));
372  }
373 
374  if ($this->fields->getActiveFields() && !ilSearchSettings::getInstance()->getHideAdvancedSearch()) {
375  $this->tabs_gui->addTarget('search_advanced', $this->ctrl->getLinkTargetByClass('illuceneAdvancedSearchgui'));
376  }
377 
378  $this->tabs_gui->setTabActive('search');
379  }
380 
387  protected function initUserSearchCache()
388  {
389  global $DIC;
390 
391  $ilUser = $DIC['ilUser'];
392 
393  include_once('Services/Search/classes/class.ilUserSearchCache.php');
394  $this->search_cache = ilUserSearchCache::_getInstance($ilUser->getId());
395  $this->search_cache->switchSearchType(ilUserSearchCache::LUCENE_DEFAULT);
396  if ((int) $_GET['page_number']) {
397  $this->search_cache->setResultPageNumber((int) $_GET['page_number']);
398  }
399  if (isset($_POST['term'])) {
400  $this->search_cache->setQuery(ilUtil::stripSlashes($_POST['term']));
401  if ($_POST['item_filter_enabled']) {
402  $filtered = array();
403  foreach (ilSearchSettings::getInstance()->getEnabledLuceneItemFilterDefinitions() as $type => $data) {
404  if ($_POST['filter_type'][$type]) {
405  $filtered[$type] = 1;
406  }
407  }
408  $this->search_cache->setItemFilter($filtered);
409 
410  // Mime filter
411  $mime = array();
412  foreach (ilSearchSettings::getInstance()->getEnabledLuceneMimeFilterDefinitions() as $type => $data) {
413  if ($_POST['filter_type'][$type]) {
414  $mime[$type] = 1;
415  }
416  }
417  $this->search_cache->setMimeFilter($mime);
418  }
419  $this->search_cache->setCreationFilter($this->loadCreationFilter());
420  if (!$_POST['item_filter_enabled']) {
421  // @todo: keep item filter settings
422  $this->search_cache->setItemFilter(array());
423  $this->search_cache->setMimeFilter(array());
424  }
425  if (!$_POST['screation']) {
426  $this->search_cache->setCreationFilter(array());
427  }
428  }
429  }
430 
436  protected function fillAdminPanel()
437  {
438  global $DIC;
439 
440  $lng = $DIC['lng'];
441 
442  $adm_view_cmp = $adm_cmds = $creation_selector = $adm_view = false;
443 
444  // admin panel commands
445  if ((count((array) $this->admin_panel_commands) > 0)) {
446  foreach ($this->admin_panel_commands as $cmd) {
447  $this->tpl->setCurrentBlock("lucene_admin_panel_cmd");
448  $this->tpl->setVariable("LUCENE_PANEL_CMD", $cmd["cmd"]);
449  $this->tpl->setVariable("LUCENE_TXT_PANEL_CMD", $cmd["txt"]);
450  $this->tpl->parseCurrentBlock();
451  }
452 
453  $adm_cmds = true;
454  }
455  if ($adm_cmds) {
456  $this->tpl->setCurrentBlock("lucene_adm_view_components");
457  $this->tpl->setVariable("LUCENE_ADM_IMG_ARROW", ilUtil::getImagePath("arrow_upright.svg"));
458  $this->tpl->setVariable("LUCENE_ADM_ALT_ARROW", $lng->txt("actions"));
459  $this->tpl->parseCurrentBlock();
460  $adm_view_cmp = true;
461  }
462 
463  // admin view button
464  if (is_array($this->admin_view_button)) {
465  if (is_array($this->admin_view_button)) {
466  $this->tpl->setCurrentBlock("lucene_admin_button");
467  $this->tpl->setVariable(
468  "LUCENE_ADMIN_MODE_LINK",
469  $this->admin_view_button["link"]
470  );
471  $this->tpl->setVariable(
472  "LUCENE_TXT_ADMIN_MODE",
473  $this->admin_view_button["txt"]
474  );
475  $this->tpl->parseCurrentBlock();
476  }
477  $this->tpl->setCurrentBlock("lucene_admin_view");
478  $this->tpl->parseCurrentBlock();
479  $adm_view = true;
480  }
481 
482  // creation selector
483  if (is_array($this->creation_selector)) {
484  $this->tpl->setCurrentBlock("lucene_add_commands");
485  if ($adm_cmds) {
486  $this->tpl->setVariable("LUCENE_ADD_COM_WIDTH", 'width="1"');
487  }
488  $this->tpl->setVariable(
489  "LUCENE_SELECT_OBJTYPE_REPOS",
490  $this->creation_selector["options"]
491  );
492  $this->tpl->setVariable(
493  "LUCENE_BTN_NAME_REPOS",
494  $this->creation_selector["command"]
495  );
496  $this->tpl->setVariable(
497  "LUCENE_TXT_ADD_REPOS",
498  $this->creation_selector["txt"]
499  );
500  $this->tpl->parseCurrentBlock();
501  $creation_selector = true;
502  }
503  if ($adm_view || $creation_selector) {
504  $this->tpl->setCurrentBlock("lucene_adm_panel");
505  if ($adm_view_cmp) {
506  $this->tpl->setVariable("LUCENE_ADM_TBL_WIDTH", 'width:"100%";');
507  }
508  $this->tpl->parseCurrentBlock();
509  }
510  }
511 
515  protected function addAdminPanelCommand($a_cmd, $a_txt)
516  {
517  $this->admin_panel_commands[] =
518  array("cmd" => $a_cmd, "txt" => $a_txt);
519  }
520 
524  protected function setAdminViewButton($a_link, $a_txt)
525  {
526  $this->admin_view_button =
527  array("link" => $a_link, "txt" => $a_txt);
528  }
529 
530  protected function setPageFormAction($a_action)
531  {
532  $this->page_form_action = $a_action;
533  }
534 
539  protected function showSearchForm()
540  {
541  global $DIC;
542 
543  $ilCtrl = $DIC['ilCtrl'];
544  $lng = $DIC['lng'];
545 
546  $this->tpl->addBlockFile('ADM_CONTENT', 'adm_content', 'tpl.lucene_search.html', 'Services/Search');
547 
548  // include js needed
549  include_once("./Services/UIComponent/Overlay/classes/class.ilOverlayGUI.php");
551  $this->tpl->addJavascript("./Services/Search/js/Search.js");
552 
553  include_once("./Services/UIComponent/Glyph/classes/class.ilGlyphGUI.php");
554 
555  $this->tpl->setVariable("FORM_ACTION", $ilCtrl->getFormAction($this, 'performSearch'));
556  $this->tpl->setVariable("TERM", ilUtil::prepareFormOutput($this->search_cache->getQuery()));
557  $this->tpl->setVariable("SEARCH_LABEL", $lng->txt("search"));
558  include_once("./Services/UIComponent/Button/classes/class.ilSubmitButton.php");
560  $btn->setCommand("performSearch");
561  $btn->setCaption("search");
562  $this->tpl->setVariable("SUBMIT_BTN", $btn->render());
563  $this->tpl->setVariable("TXT_OPTIONS", $lng->txt("options"));
564  $this->tpl->setVariable("ARR_IMG", ilGlyphGUI::get(ilGlyphGUI::CARET));
565  $this->tpl->setVariable("TXT_COMBINATION", $lng->txt("search_term_combination"));
566  $this->tpl->setVariable('TXT_COMBINATION_DEFAULT', ilSearchSettings::getInstance()->getDefaultOperator() == ilSearchSettings::OPERATOR_AND ? $lng->txt('search_all_words') : $lng->txt('search_any_word'));
567  $this->tpl->setVariable("TXT_AREA", $lng->txt("search_area"));
568 
569  if (ilSearchSettings::getInstance()->isLuceneItemFilterEnabled()) {
570  $this->tpl->setCurrentBlock("type_sel");
571  $this->tpl->setVariable('TXT_TYPE_DEFAULT', $lng->txt("search_off"));
572  $this->tpl->setVariable("ARR_IMGT", ilGlyphGUI::get(ilGlyphGUI::CARET));
573  $this->tpl->setVariable("TXT_FILTER_BY_TYPE", $lng->txt("search_filter_by_type"));
574  $this->tpl->setVariable('FORM', $this->form->getHTML());
575  $this->tpl->parseCurrentBlock();
576  }
577 
578  // search area form
579  $this->tpl->setVariable('SEARCH_AREA_FORM', $this->getSearchAreaForm()->getHTML());
580  $this->tpl->setVariable("TXT_CHANGE", $lng->txt("change"));
581 
582  if (ilSearchSettings::getInstance()->isDateFilterEnabled()) {
583  // begin-patch creation_date
584  $this->tpl->setVariable('TXT_FILTER_BY_CDATE', $this->lng->txt('search_filter_cd'));
585  $this->tpl->setVariable('TXT_CD_OFF', $this->lng->txt('search_off'));
586  $this->tpl->setVariable('FORM_CD', $this->getCreationDateForm()->getHTML());
587  $this->tpl->setVariable("ARR_IMG_CD", ilGlyphGUI::get(ilGlyphGUI::CARET));
588  // end-patch creation_date
589  }
590 
591 
592  return true;
593  }
594 
595 
600  protected function parseCreationFilter()
601  {
602  $options = $this->search_cache->getCreationFilter();
603 
604  if (!$options['enabled']) {
605  return '';
606  }
607  $limit = new ilDate($options['date'], IL_CAL_UNIX);
608 
609  switch ($options['ontype']) {
610  case 1:
611  // after
612  $limit->increment(IL_CAL_DAY, 1);
613  $now = new ilDate(time(), IL_CAL_UNIX);
614  return '+(cdate:[' . $limit->get(IL_CAL_DATE) . ' TO ' . $now->get(IL_CAL_DATE) . '*]) ';
615 
616  case 2:
617  // before
618  return '+(cdate:[* TO ' . $limit->get(IL_CAL_DATE) . ']) ';
619 
620  case 3:
621  // on
622  return '+(cdate:' . $limit->get(IL_CAL_DATE) . '*) ';
623 
624  }
625  return '';
626  }
627  // end-patch creation_date
628 }
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
loadCreationFilter()
Load creation date filter.
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.
$type
global $DIC
Definition: saml.php:7
parse()
parse query string
$_GET["client_id"]
static getInstance(ilLuceneQueryParser $qp)
Get singleton instance.
static get($a_glyph, $a_text="")
Get glyph html.
parseCreationFilter()
Parse creation date.
getMimeDetails()
Needed for base class search form.
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
$errors fields
Definition: imgupload.php:51
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.
addPager($result, $a_session_key)
Add Pager.
Class for single dates.
if(isset($_POST['submit'])) $form
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.
$ilUser
Definition: imgupload.php:18
performSearch()
Perform search.
$query
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 $ilBench
Definition: ilias.php:18
fillAdminPanel()
Put admin panel into template:
static getInstance()
Get singleton instance.
$_POST["username"]
$data
Definition: bench.php:6
static initJavascript()
Init javascript.