1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once './Services/Search/classes/class.ilSearchSettings.php';
5
6/**
7 * Class ilRepositoryObjectSearchGUI
8 * Repository object detail search
9 *
10 *
11 * @author Stefan Meyer <meyer@leifos.com>
12 * @version $Id$
13 *
14 * @package ServicesSearch
15 *
16 */
17class ilRepositoryObjectDetailSearch
18{
19    protected $settings = null;
20
21    protected $obj_id;
22    protected $type;
23    protected $query_string;
24
25    /**
26     * constructor
27     */
28    public function __construct($a_obj_id)
29    {
30        $this->obj_id = $a_obj_id;
31        $this->type = ilObject::_lookupType($this->getObjId());
32
33        $this->settings = ilSearchSettings::getInstance();
34    }
35
36    /**
37     * Get settings
38     * @return ilSearchSettings
39     */
40    public function getSettings()
41    {
42        return $this->settings;
43    }
44
45    /**
46     * get obj id
47     * @return type
48     */
49    public function getObjId()
50    {
51        return $this->obj_id;
52    }
53
54    public function getType()
55    {
56        return $this->type;
57    }
58
59
60    public function setQueryString($a_query)
61    {
62        $this->query_string = $a_query;
63    }
64
65
66    /**
67     * get query string
68     */
69    public function getQueryString()
70    {
71        return $this->query_string;
72    }
73
74    /**
75     * Perform search
76     * @return ilRepositoryObjectDetailSearchResult
77     *
78     * @throws ilLuceneQueryParserException
79     */
80    public function performSearch()
81    {
82        if ($this->getSettings()->enabledLucene()) {
83            return $this->performLuceneSearch();
84        } else {
85            return $this->performDBSearch();
86        }
87    }
88
89    /**
90     * Perform lucene search
91     * @throws ilLuceneQueryParserException
92     */
93    protected function performLuceneSearch()
94    {
95        include_once './Services/Search/classes/Lucene/class.ilLuceneQueryParser.php';
96        try {
97            $qp = new ilLuceneQueryParser($this->getQueryString());
98            $qp->parse();
99        } catch (ilLuceneQueryParserException $e) {
100            ilLoggerFactory::getLogger('src')->warning('Invalid query: ' . $e->getMessage());
101            throw $e;
102        }
103
104        include_once './Services/Search/classes/Lucene/class.ilLuceneSearcher.php';
105        $searcher = ilLuceneSearcher::getInstance($qp);
106        $searcher->highlight(array($this->getObjId()));
107
108        include_once './Services/Search/classes/class.ilRepositoryObjectDetailSearchResult.php';
109        $detail_search_result = new ilRepositoryObjectDetailSearchResult();
110
111        if ($searcher->getHighlighter() instanceof ilLuceneHighlighterResultParser) {
112            foreach ($searcher->getHighlighter()->getSubItemIds($this->getObjId()) as $sub_id) {
113                $detail_search_result->addResultSet(
114                    array(
115                        'obj_id' => $this->getObjId(),
116                        'item_id' => $sub_id,
117                        'relevance' => $searcher->getHighlighter()->getRelevance($this->getObjId(), $sub_id),
118                        'content' => $searcher->getHighlighter()->getContent($this->getObjId(), $sub_id)
119                    )
120                );
121            }
122        }
123        return $detail_search_result;
124    }
125
126
127    /**
128     * Perform DB  search
129     * @return ilRepositoryObjectDetailSearchResult
130     */
131    protected function performDBSearch()
132    {
133        // query parser
134        include_once 'Services/Search/classes/class.ilQueryParser.php';
135
136        $query_parser = new ilQueryParser($this->getQueryString());
137
138        $query_parser->setCombination(
139            ($this->getSettings()->getDefaultOperator() == ilSearchSettings::OPERATOR_AND) ?
140                QP_COMBINATION_AND :
141                QP_COMBINATION_OR
142        );
143        $query_parser->parse();
144
145        if (!$query_parser->validate()) {
146            throw new Exception($query_parser->getMessage());
147        }
148        include_once 'Services/Search/classes/class.ilSearchResult.php';
149        $search_result = new ilSearchResult();
150
151        include_once 'Services/Search/classes/class.ilObjectSearchFactory.php';
152        $search = ilObjectSearchFactory::getByTypeSearchInstance($this->getType(), $query_parser);
153
154        switch ($this->getType()) {
155            case 'wiki':
156                $search->setFilter(array('wpg'));
157                break;
158        }
159
160        $search->setIdFilter(array($this->getObjId()));
161
162        $search_result->mergeEntries($search->performSearch());
163
164        include_once './Services/Search/classes/class.ilRepositoryObjectDetailSearchResult.php';
165        $detail_search_result = new ilRepositoryObjectDetailSearchResult();
166
167        foreach ($search_result->getEntries() as $entry) {
168            foreach ((array) $entry['child'] as $child) {
169                $detail_search_result->addResultSet(
170                    array(
171                            'obj_id' => $entry['obj_id'],
172                            'item_id' => $child
173                        )
174                );
175            }
176        }
177        return $detail_search_result;
178    }
179}
180