tiki-searchindex.php 6.34 KB
Newer Older
1
<?php
changi67's avatar
changi67 committed
2 3 4
/**
 * @package tikiwiki
 */
5
// (c) Copyright 2002-2015 by authors of the Tiki Wiki CMS Groupware Project
6
//
7 8
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
changi67's avatar
changi67 committed
9 10
// $Id$

11 12 13 14
$inputConfiguration = array(
  array( 'staticKeyFilters' => array(
    'date' => 'digits',
    'maxRecords' => 'digits',
15
    'highlight' => 'text',
16 17
    'where' => 'text',
    'find' => 'text',
18
    'searchLang' => 'word',
19
    'words' =>'text',
20
    'boolean' =>'word',
21
	'storeAs' => 'int',
22 23 24 25
    )
  )
);

26 27
$section = 'search';
require_once ('tiki-setup.php');
28 29
$access->check_feature('feature_search');
$access->check_permission('tiki_p_search');
30

31
//get_strings tra("Searchindex")
32 33
//ini_set('display_errors', true);
//error_reporting(E_ALL);
34

35 36 37 38
foreach (array('find', 'highlight', 'where') as $possibleKey) {
	if (empty($_REQUEST['filter']) && !empty($_REQUEST[$possibleKey])) {
		$_REQUEST['filter']['content'] = $_REQUEST[$possibleKey];
	}
39
}
40
$filter = isset($_REQUEST['filter']) ? $_REQUEST['filter'] : array();
41
$postfilter = isset($_REQUEST['postfilter']) ? $_REQUEST['postfilter'] : array();
42
$facets = array();
43

44
if (count($filter) || count($postfilter)) {
45 46 47
	if (isset($_REQUEST['save_query'])) {
		$_SESSION['quick_search'][(int) $_REQUEST['save_query']] = $_REQUEST;
	}
48 49
	$offset = isset($_REQUEST['offset']) ? $_REQUEST['offset'] : 0;
	$maxRecords = empty($_REQUEST['maxRecords'])?$prefs['maxRecords']: $_REQUEST['maxRecords'];
50

51
	if ($access->is_serializable_request(true)) {
52 53 54
		$jitRequest->replaceFilter('fields', 'word');
		$fetchFields = array_merge(array('title', 'modification_date', 'url'), $jitRequest->asArray('fields', ','));;

55
		$results = tiki_searchindex_get_results($filter, $postfilter, $offset, $maxRecords);
56

57 58
		$smarty->loadPlugin('smarty_function_object_link');
		$smarty->loadPlugin('smarty_modifier_sefurl');
59
		foreach ($results as &$res) {
60 61 62 63 64
			foreach ($fetchFields as $f) {
				if (isset($res[$f])) {
					$res[$f]; // Dynamic load if applicable
				}
			}
65
			$res['link'] = smarty_function_object_link(
66 67 68 69 70 71
				array(
					'type' => $res['object_type'],
					'id' => $res['object_id'],
					'title' => $res['title'],
				),
				$smarty
72
			);
changi67's avatar
changi67 committed
73 74 75 76 77 78
			$res = array_filter(
				$res,
				function ($v) {
					return !is_null($v);
				}
			);	// strip out null values
79
		}
80
		$access->output_serialized(
81 82
			$results,
			array(
nkoth's avatar
nkoth committed
83
				'feedTitle' => tr('%0: Results for "%1"', $prefs['sitetitle'], isset($filter['content']) ? $filter['content'] : ''),
84 85 86 87 88 89
				'feedDescription' => tr('Search Results'),
				'entryTitleKey' => 'title',
				'entryUrlKey' => 'url',
				'entryModificationKey' => 'modification_date',
				'entryObjectDescriptors' => array('object_type', 'object_id'),
			)
90
		);
91 92
		exit;
	} else {
93 94 95 96 97 98 99 100 101 102 103
		$cachelib = TikiLib::lib('cache');
		$cacheType = 'search';
		$cacheName = $user.'/'.$offset.'/'.$maxRecords.'/'.serialize($filter);
		$isCached = false;
		if (!empty($prefs['unified_user_cache']) && $cachelib->isCached($cacheName, $cacheType)) {
			list($date, $html) = $cachelib->getSerialized($cacheName, $cacheType);
			if ($date > $tikilib->now - $prefs['unified_user_cache'] * 60) {
				$isCached = true;
			}
		}
		if (!$isCached) {
104
			$results = tiki_searchindex_get_results($filter, $postfilter, $offset, $maxRecords);
changi67's avatar
changi67 committed
105 106 107 108 109
			$facets = array_map(
				function ($facet) {
					return $facet->getName();
				}, $results->getFacets()
			);
110

111
			$plugin = new Search_Formatter_Plugin_SmartyTemplate(realpath('templates/searchresults-plain.tpl'));
112
			$plugin->setData(
113 114 115
				array(
					'prefs' => $prefs,
				)
116
			);
117 118 119 120 121
			$fields = array(
				'title' => null,
				'url' => null,
				'modification_date' => null,
				'highlight' => null,
122
			);
123 124 125 126
			if ($prefs['feature_search_show_visit_count'] === 'y') {
				$fields['visits'] = null;
			}
			$plugin->setFields($fields);
127

128
			$formatter = new Search_Formatter($plugin);
129

130
			$wiki = $formatter->format($results);
131
			$html = $tikilib->parse_data(
132 133 134 135
				$wiki,
				array(
					'is_html' => true,
				)
136
			);
137 138 139 140 141
			if (!empty($prefs['unified_user_cache'])) {
				$cachelib->cacheItem($cacheName, serialize(array($tikilib->now, $html)), $cacheType);
			}
		}
		$smarty->assign('results', $html);
142
	}
143
}
144

145
$smarty->assign('filter', $filter);
146
$smarty->assign('postfilter', $postfilter);
147
$smarty->assign('facets', $facets);
148

149 150
// disallow robots to index page:
$smarty->assign('metatag_robots', 'NOINDEX, NOFOLLOW');
151 152 153 154 155 156

if ($prefs['search_use_facets'] == 'y') {
	$smarty->display("tiki-searchfacets.tpl");
} else {
	$smarty->display("tiki-searchindex.tpl");
}
157

158 159 160 161 162 163
/**
 * @param $filter
 * @param $offset
 * @param $maxRecords
 * @return mixed
 */
164
function tiki_searchindex_get_results($filter, $postfilter, $offset, $maxRecords)
165
{
166
	global $prefs;
changi67's avatar
changi67 committed
167

168
	$unifiedsearchlib = TikiLib::lib('unifiedsearch');
169

170 171 172
	$query = new Search_Query;
	$unifiedsearchlib->initQueryBase($query);
	$query = $unifiedsearchlib->buildQuery($filter, $query);
173
	$query->filterContent('y', 'searchable');
174

175 176 177 178
	if (count($postfilter)) {
		$unifiedsearchlib->buildQuery($postfilter, $query->getPostFilter());
	}

179 180 181 182
	if (isset($_REQUEST['sort_mode']) && $order = Search_Query_Order::parse($_REQUEST['sort_mode'])) {
		$query->setOrder($order);
	}

183 184 185
	if ($prefs['storedsearch_enabled'] == 'y' && ! empty($_POST['storeAs'])) {
		$storedsearch = TikiLib::lib('storedsearch');
		$storedsearch->storeUserQuery($_POST['storeAs'], $query);
186
		TikiLib::lib('smarty')->assign('display_msg', tr('Your query was stored.'));
187 188 189 190 191 192
	}

	$unifiedsearchlib->initQueryPermissions($query);

	$query->setRange($offset, $maxRecords);

193 194 195 196 197 198 199
	if ($prefs['feature_search_stats'] == 'y') {
		$stats = TikiLib::lib('searchstats');
		foreach ($query->getTerms() as $term) {
			$stats->register_term_hit($term);
		}
	}

200 201 202 203 204 205 206
	if ($prefs['search_use_facets'] == 'y') {
		$provider = $unifiedsearchlib->getFacetProvider();

		foreach ($provider->getFacets() as $facet) {
			$query->requestFacet($facet);
		}
	}
207

208
	try {
209 210 211 212 213
		if ($prefs['federated_enabled'] == 'y' && ! empty($filter['content'])) {
			$fed = TikiLib::lib('federatedsearch');
			$fed->augmentSimpleQuery($query, $filter['content']);
		}

214 215 216
		$resultset = $query->search($unifiedsearchlib->getIndex());

		return $resultset;
217 218 219 220 221 222
	} catch (Search_Elastic_TransportException $e) {
		TikiLib::lib('errorreport')->report('Search functionality currently unavailable.');
	} catch (Exception $e) {
		TikiLib::lib('errorreport')->report($e->getMessage());
	}

223
	return new Search_ResultSet(array(), 0, 0, -1);
224
}
225