Commit | Line | Data |
---|---|---|
749cca08 | 1 | <?php |
a9229942 | 2 | |
749cca08 | 3 | namespace wcf\page; |
a9229942 | 4 | |
9fa8ee15 | 5 | use wcf\data\search\ISearchResultObject; |
749cca08 MW |
6 | use wcf\data\search\Search; |
7 | use wcf\system\event\EventHandler; | |
8 | use wcf\system\exception\IllegalLinkException; | |
7b9ff46b | 9 | use wcf\system\exception\ImplementationException; |
ac4ff35d | 10 | use wcf\system\page\PageLocationManager; |
20fe9131 | 11 | use wcf\system\request\LinkHandler; |
749cca08 MW |
12 | use wcf\system\search\SearchEngine; |
13 | use wcf\system\WCF; | |
20fe9131 | 14 | use wcf\util\HeaderUtil; |
749cca08 MW |
15 | |
16 | /** | |
17 | * Shows the result of a search request. | |
a9229942 TD |
18 | * |
19 | * @author Marcel Werk | |
20 | * @copyright 2001-2019 WoltLab GmbH | |
21 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
7eefee80 | 22 | * @deprecated 5.5 |
749cca08 | 23 | */ |
a9229942 TD |
24 | class SearchResultPage extends MultipleLinkPage |
25 | { | |
a9229942 TD |
26 | /** |
27 | * @inheritDoc | |
28 | */ | |
29 | public $itemsPerPage = SEARCH_RESULTS_PER_PAGE; | |
30 | ||
31 | /** | |
32 | * highlight string | |
33 | * @var string | |
34 | */ | |
35 | public $highlight = ''; | |
36 | ||
37 | /** | |
38 | * search id | |
39 | * @var int | |
40 | */ | |
41 | public $searchID = 0; | |
42 | ||
43 | /** | |
44 | * search object | |
45 | * @var Search | |
46 | */ | |
47 | public $search; | |
48 | ||
49 | /** | |
50 | * messages | |
51 | * @var array | |
52 | */ | |
53 | public $messages = []; | |
54 | ||
55 | /** | |
56 | * search data | |
57 | * @var array | |
58 | */ | |
59 | public $searchData; | |
60 | ||
61 | /** | |
62 | * result list template | |
63 | * @var string | |
64 | */ | |
65 | public $resultListTemplateName = 'searchResultList'; | |
66 | ||
67 | /** | |
68 | * result list template's application | |
69 | * @var string | |
70 | */ | |
71 | public $resultListApplication = 'wcf'; | |
72 | ||
73 | /** | |
74 | * @inheritDoc | |
75 | */ | |
76 | public function readParameters() | |
77 | { | |
78 | parent::readParameters(); | |
79 | ||
80 | if (isset($_REQUEST['highlight'])) { | |
81 | $this->highlight = $_REQUEST['highlight']; | |
82 | } | |
83 | if (isset($_REQUEST['id'])) { | |
84 | $this->searchID = \intval($_REQUEST['id']); | |
85 | } | |
86 | $this->search = new Search($this->searchID); | |
87 | if (!$this->search->searchID || $this->search->searchType != 'messages') { | |
88 | $this->redirectOrReject(); | |
89 | } | |
90 | if ($this->search->userID && $this->search->userID != WCF::getUser()->userID) { | |
91 | $this->redirectOrReject(); | |
92 | } | |
93 | ||
94 | // get search data | |
95 | $this->searchData = \unserialize($this->search->searchData); | |
96 | } | |
97 | ||
98 | /** | |
99 | * Attempts to start a new search if the search id is invalid or unavailable, and the | |
100 | * highlight parameter is available. | |
101 | */ | |
102 | protected function redirectOrReject() | |
103 | { | |
104 | if (!empty($this->highlight)) { | |
105 | HeaderUtil::redirect( | |
106 | LinkHandler::getInstance()->getLink('Search', ['q' => $this->highlight]) | |
107 | ); | |
108 | ||
109 | exit; | |
110 | } else { | |
111 | throw new IllegalLinkException(); | |
112 | } | |
113 | } | |
114 | ||
115 | /** | |
116 | * @inheritDoc | |
117 | */ | |
118 | public function readData() | |
119 | { | |
120 | parent::readData(); | |
121 | ||
122 | // cache message data | |
123 | $this->cacheMessageData(); | |
124 | ||
125 | // get messages | |
126 | $this->readMessages(); | |
127 | ||
128 | // set active menu item | |
129 | if (isset($this->searchData['selectedObjectTypes']) && \count($this->searchData['selectedObjectTypes']) == 1) { | |
130 | $objectType = SearchEngine::getInstance()->getObjectType(\reset($this->searchData['selectedObjectTypes'])); | |
131 | $objectType->setLocation(); | |
132 | } | |
133 | ||
134 | // add breadcrumbs | |
135 | PageLocationManager::getInstance()->addParentLocation('com.woltlab.wcf.Search'); | |
136 | } | |
137 | ||
138 | /** | |
139 | * Caches the message data. | |
140 | */ | |
141 | protected function cacheMessageData() | |
142 | { | |
143 | $types = []; | |
144 | ||
145 | // group object id by object type | |
146 | for ($i = $this->startIndex - 1; $i < $this->endIndex; $i++) { | |
147 | $type = $this->searchData['results'][$i]['objectType']; | |
148 | $objectID = $this->searchData['results'][$i]['objectID']; | |
149 | ||
150 | if (!isset($types[$type])) { | |
151 | $types[$type] = []; | |
152 | } | |
153 | $types[$type][] = $objectID; | |
154 | } | |
155 | ||
156 | foreach ($types as $type => $objectIDs) { | |
157 | $objectType = SearchEngine::getInstance()->getObjectType($type); | |
158 | $objectType->cacheObjects($objectIDs, ($this->searchData['additionalData'][$type] ?? null)); | |
159 | } | |
160 | } | |
161 | ||
162 | /** | |
163 | * Reads the data of the search result messages. | |
164 | */ | |
165 | protected function readMessages() | |
166 | { | |
167 | for ($i = $this->startIndex - 1; $i < $this->endIndex; $i++) { | |
168 | $type = $this->searchData['results'][$i]['objectType']; | |
169 | $objectID = $this->searchData['results'][$i]['objectID']; | |
170 | ||
171 | $objectType = SearchEngine::getInstance()->getObjectType($type); | |
172 | if (($message = $objectType->getObject($objectID)) !== null) { | |
173 | if (!($message instanceof ISearchResultObject)) { | |
174 | throw new ImplementationException(\get_class($message), ISearchResultObject::class); | |
175 | } | |
176 | ||
a9229942 | 177 | $this->messages[] = $message; |
a9229942 TD |
178 | } |
179 | } | |
180 | } | |
181 | ||
a9229942 TD |
182 | /** |
183 | * @inheritDoc | |
184 | */ | |
185 | public function countItems() | |
186 | { | |
187 | // call countItems event | |
188 | EventHandler::getInstance()->fireAction($this, 'countItems'); | |
189 | ||
190 | return \count($this->searchData['results']); | |
191 | } | |
192 | ||
193 | /** | |
194 | * @inheritDoc | |
195 | */ | |
196 | protected function initObjectList() | |
197 | { | |
198 | } | |
199 | ||
200 | /** | |
201 | * @inheritDoc | |
202 | */ | |
203 | protected function readObjects() | |
204 | { | |
205 | } | |
55ff970b MW |
206 | |
207 | /** | |
208 | * @inheritDoc | |
209 | */ | |
210 | public function __run() | |
211 | { | |
212 | throw new IllegalLinkException(); | |
213 | } | |
749cca08 | 214 | } |