This repository has been archived by the owner on Mar 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
DocumentationSearch.php
executable file
·443 lines (374 loc) · 13.1 KB
/
DocumentationSearch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
set_include_path(
dirname(dirname(__FILE__)) . '/thirdparty/'. PATH_SEPARATOR .
get_include_path()
);
require_once 'Zend/Search/Lucene.php';
/**
* Documentation Search powered by Lucene. You will need Zend_Lucene installed
* on your path.
*
* To rebuild the indexes run the {@link RebuildLuceneDocsIndex} task. You may
* wish to setup a cron job to remake the indexes on a regular basis.
*
* This class has the ability to generate an OpenSearch RSS formatted feeds
* simply by using the URL:
*
* <code>
* yoursite.com/search/?q=Foo&format=rss. // Format can either be specified as rss or left off.
* </code>
*
* To get a specific amount of results you can also use the modifiers start and
* limit:
*
* <code>
* yoursite.com/search/?q=Foo&start=10&limit=10
* </code>
*
* @package docsviewer
*/
class DocumentationSearch
{
/**
* @var bool - Is search enabled
*/
private static $enabled = false;
/**
* @var bool - Is advanced search enabled
*/
private static $advanced_search_enabled = true;
/**
* @var string - OpenSearch metadata. Please use {@link DocumentationSearch::set_meta_data()}
*/
private static $meta_data = array();
/**
* @var Array Regular expression mapped to a "boost factor" for the searched document.
* Defaults to 1.0, lower to decrease relevancy. Requires reindex.
* Uses {@link DocumentationPage->getRelativePath()} for comparison.
*/
private static $boost_by_path = array();
/**
* @var ArrayList - Results
*/
private $results;
/**
* @var int
*/
private $totalResults;
/**
* @var string
*/
private $query;
/**
* @var Controller
*/
private $outputController;
/**
* Optionally filter by module and version
*
* @var array
*/
private $modules, $versions;
public function setModules($modules)
{
$this->modules = $modules;
}
public function setVersions($versions)
{
$this->versions = $versions;
}
/**
* Set the current search query
*
* @param string
*/
public function setQuery($query)
{
$this->query = $query;
}
/**
* Returns the current search query
*
* @return string
*/
public function getQuery()
{
return $this->query;
}
/**
* Sets the {@link DocumentationViewer} or {@link DocumentationSearch} instance which this search is rendering
* on based on whether it is the results display or RSS feed
*
* @param Controller
*/
public function setOutputController($controller)
{
$this->outputController = $controller;
}
/**
* Folder name for indexes (in the temp folder).
*
* @config
* @var string
*/
private static $index_location;
/**
* @return string
*/
public static function get_index_location()
{
$location = Config::inst()->get('DocumentationSearch', 'index_location');
if (!$location) {
return Controller::join_links(TEMP_FOLDER, 'RebuildLuceneDocsIndex');
}
return $location;
}
/**
* Perform a search query on the index
*/
public function performSearch()
{
try {
$index = Zend_Search_Lucene::open(self::get_index_location());
Zend_Search_Lucene::setResultSetLimit(100);
$query = new Zend_Search_Lucene_Search_Query_Boolean();
$term = Zend_Search_Lucene_Search_QueryParser::parse($this->getQuery());
$query->addSubquery($term, true);
if ($this->modules) {
$moduleQuery = new Zend_Search_Lucene_Search_Query_MultiTerm();
foreach ($this->modules as $module) {
$moduleQuery->addTerm(new Zend_Search_Lucene_Index_Term($module, 'Entity'));
}
$query->addSubquery($moduleQuery, true);
}
if ($this->versions) {
$versionQuery = new Zend_Search_Lucene_Search_Query_MultiTerm();
foreach ($this->versions as $version) {
$versionQuery->addTerm(new Zend_Search_Lucene_Index_Term($version, 'Version'));
}
$query->addSubquery($versionQuery, true);
}
$er = error_reporting();
error_reporting('E_ALL ^ E_NOTICE');
$this->results = $index->find($query);
error_reporting($er);
$this->totalResults = $index->numDocs();
} catch (Zend_Search_Lucene_Exception $e) {
user_error($e .'. Ensure you have run the rebuld task (/dev/tasks/RebuildLuceneDocsIndex)', E_USER_ERROR);
}
}
/**
* @return ArrayData
*/
public function getSearchResults($request)
{
$pageLength = (isset($_GET['length'])) ? (int) $_GET['length'] : 10;
$data = array(
'Results' => null,
'Query' => null,
'Versions' => DBField::create_field('Text', implode(', ', $this->versions)),
'Modules' => DBField::create_field('Text', implode(', ', $this->modules)),
'Title' => _t('DocumentationSearch.SEARCHRESULTS', 'Search Results'),
'TotalResults' => null,
'TotalPages' => null,
'ThisPage' => null,
'StartResult' => null,
'PageLength' => $pageLength,
'EndResult' => null,
'PrevUrl' => DBField::create_field('Text', 'false'),
'NextUrl' => DBField::create_field('Text', 'false'),
'SearchPages' => new ArrayList()
);
$start = ($request->requestVar('start')) ? (int)$request->requestVar('start') : 0;
$query = ($request->requestVar('q')) ? $request->requestVar('q') : '';
$currentPage = floor($start / $pageLength) + 1;
$totalPages = ceil(count($this->results) / $pageLength);
if ($totalPages == 0) {
$totalPages = 1;
}
if ($currentPage > $totalPages) {
$currentPage = $totalPages;
}
$results = new ArrayList();
if ($this->results) {
foreach ($this->results as $k => $hit) {
if ($k < ($currentPage-1)*$pageLength || $k >= ($currentPage*$pageLength)) {
continue;
}
$doc = $hit->getDocument();
$content = $hit->content;
$obj = new ArrayData(
array(
'Title' => DBField::create_field('Varchar', $doc->getFieldValue('Title')),
'BreadcrumbTitle' => DBField::create_field('HTMLText', $doc->getFieldValue('BreadcrumbTitle')),
'Link' => DBField::create_field('Varchar', $doc->getFieldValue('Link')),
'Language' => DBField::create_field('Varchar', $doc->getFieldValue('Language')),
'Version' => DBField::create_field('Varchar', $doc->getFieldValue('Version')),
'Entity' => DBField::create_field('Varchar', $doc->getFieldValue('Entity')),
'Content' => DBField::create_field('HTMLText', $content),
'Score' => $hit->score,
'Number' => $k + 1,
'ID' => md5($doc->getFieldValue('Link'))
)
);
$results->push($obj);
}
}
$data['Results'] = $results;
$data['Query'] = DBField::create_field('Text', $query);
$data['TotalResults'] = DBField::create_field('Text', count($this->results));
$data['TotalPages'] = DBField::create_field('Text', $totalPages);
$data['ThisPage'] = DBField::create_field('Text', $currentPage);
$data['StartResult'] = $start + 1;
$data['EndResult'] = $start + count($results);
// Pagination links
if ($currentPage > 1) {
$data['PrevUrl'] = DBField::create_field(
'Text',
$this->buildQueryUrl(array('start' => ($currentPage - 2) * $pageLength))
);
}
if ($currentPage < $totalPages) {
$data['NextUrl'] = DBField::create_field(
'Text',
$this->buildQueryUrl(array('start' => $currentPage * $pageLength))
);
}
if ($totalPages > 1) {
// Always show a certain number of pages at the start
for ($i = 1; $i <= $totalPages; $i++) {
$obj = new DataObject();
$obj->IsEllipsis = false;
$obj->PageNumber = $i;
$obj->Link = $this->buildQueryUrl(
array(
'start' => ($i - 1) * $pageLength
)
);
$obj->Current = false;
if ($i == $currentPage) {
$obj->Current = true;
}
$data['SearchPages']->push($obj);
}
}
return new ArrayData($data);
}
/**
* Build a nice query string for the results
*
* @return string
*/
private function buildQueryUrl($params)
{
$url = parse_url($_SERVER['REQUEST_URI']);
if (! array_key_exists('query', $url)) {
$url['query'] = '';
}
parse_str($url['query'], $url['query']);
if (! is_array($url['query'])) {
$url['query'] = array();
}
// Remove 'start parameter if it exists
if (array_key_exists('start', $url['query'])) {
unset($url['query']['start']);
}
// Add extra parameters from argument
$url['query'] = array_merge($url['query'], $params);
$url['query'] = http_build_query($url['query']);
$url = $url['path'] . ($url['query'] ? '?'.$url['query'] : '');
return $url;
}
/**
* @return int
*/
public function getTotalResults()
{
return (int) $this->totalResults;
}
/**
* Optimizes the search indexes on the File System
*
* @return void
*/
public function optimizeIndex()
{
$index = Zend_Search_Lucene::open(self::get_index_location());
if ($index) {
$index->optimize();
}
}
/**
* @return String
*/
public function getTitle()
{
return ($this->outputController) ? $this->outputController->Title : _t('DocumentationSearch.SEARCH', 'Search');
}
/**
* OpenSearch MetaData fields. For a list of fields consult
* {@link self::get_meta_data()}
*
* @param array
*/
public static function set_meta_data($data)
{
if (is_array($data)) {
foreach ($data as $key => $value) {
self::$meta_data[strtolower($key)] = $value;
}
} else {
user_error("set_meta_data must be passed an array", E_USER_ERROR);
}
}
/**
* Returns the meta data needed by opensearch.
*
* @return array
*/
public static function get_meta_data()
{
$data = self::$meta_data;
$defaults = array(
'Description' => _t('DocumentationViewer.OPENSEARCHDESC', 'Search the documentation'),
'Tags' => _t('DocumentationViewer.OPENSEARCHTAGS', 'documentation'),
'Contact' => Config::inst()->get('Email', 'admin_email'),
'ShortName' => _t('DocumentationViewer.OPENSEARCHNAME', 'Documentation Search'),
'Author' => 'SilverStripe'
);
foreach ($defaults as $key => $value) {
if (isset($data[$key])) {
$defaults[$key] = $data[$key];
}
}
return $defaults;
}
/**
* Renders the search results into a template. Either the search results
* template or the Atom feed.
*/
public function renderResults()
{
if (!$this->results && $this->query) {
$this->performSearch();
}
if (!$this->outputController) {
return user_error('Call renderResults() on a DocumentationViewer instance.', E_USER_ERROR);
}
$request = $this->outputController->getRequest();
$data = $this->getSearchResults($request);
$templates = array('DocumentationViewer_search');
if ($request->requestVar('format') && $request->requestVar('format') == "atom") {
// alter the fields for the opensearch xml.
$title = ($title = $this->getTitle()) ? ' - '. $title : "";
$link = Controller::join_links(
$this->outputController->Link(),
'DocumentationOpenSearchController/description/'
);
$data->setField('Title', $data->Title . $title);
$data->setField('DescriptionURL', $link);
array_unshift($templates, 'OpenSearchResults');
}
return $this->outputController->customise($data)->renderWith($templates);
}
}