condex i7p php q_www/index.php · Gitee 极速下载/phinde - Gitee.com

namespace phinde;

// web interface to search

require 'www-header.php';

if (!isset($_GET['q'])) {

$_GET['q'] = '';

}

$query = trim($_GET['q']);

$page = 0;

if (isset($_GET['page'])) {

if (!is_numeric($_GET['page'])) {

throw new Exception_Input('List page is not numeric');

}

//PEAR Pager begins at 1

$page = (int)$_GET['page'] - 1;

if ($page >= 100) {

//Elasticsearch by default only allows 100 pages

// we do not want exceptions in our logs

header('HTTP/1.0 400 Bad Request');

echo "List page number is too large\n";

exit(1);

}

}

$perPage = 10;//$GLOBALS['phinde']['perPage'];

$site = null;

$siteParam = false;

$baseLink = '?q=' . urlencode($query);

if (preg_match('#site:([^ ]*)#', $query, $matches)) {

$site = $matches[1];

$cleanQuery = trim(str_replace('site:' . $site, '', $query));

$site = Helper::noSchema($site);

} else if (isset($_GET['site']) && trim(isset($_GET['site'])) != '') {

$site = trim($_GET['site']);

$siteParam = true;

$cleanQuery = $query;

$baseLink .= '&site=' . urlencode($site);

} else {

$cleanQuery = $query;

}

if (isset($_GET['sort'])

&& ($_GET['sort'] === 'date' || $_GET['sort'] === 'score')

) {

$sortMode = $_GET['sort'];

} else {

$sortMode = $GLOBALS['phinde']['defaultSort'];

}

$sort = $sortMode;

if ($sortMode !== $GLOBALS['phinde']['defaultSort']) {

$baseLink .= '&sort=' . $sortMode;

}

$filters = array();

if (isset($_GET['filter'])) {

$allowedFilter = array('domain', 'language', 'tags', 'term');

foreach ($_GET['filter'] as $type => $value) {

if (in_array($type, $allowedFilter)) {

$filters[$type] = filter_var($value, FILTER_SANITIZE_STRING);

}

}

}

$activeFilters = array();

foreach ($filters as $type => $value) {

$activeFilters[$type] = array(

'label' => $value,

'removeUrl' => buildLink($baseLink, $filters, $type, null),

);

}

function buildLink($baseLink, $filters, $addFilterType, $addFilterValue)

{

if ($addFilterValue === null) {

if (array_key_exists($addFilterType, $filters)) {

unset($filters[$addFilterType]);

}

} else {

$filters[$addFilterType] = $addFilterValue;

}

$params = http_build_query(array('filter' => $filters));

if (strlen($params)) {

return $baseLink . '&' . $params;

}

return $baseLink;

}

if (preg_match('#site:([^ ]*)#', $query, $matches)) {

$site = $matches[1];

$cleanQuery = trim(str_replace('site:' . $site, '', $query));

$site = Helper::noSchema($site);

$urlNoSite = buildLink('?q=' . urlencode($cleanQuery), $filters, null, null);

} else {

$cleanQuery = $query;

$urlNoSite = null;

}

$timeBegin = microtime(true);

$es = new Elasticsearch($GLOBALS['phinde']['elasticsearch']);

$res = $es->search($cleanQuery, $filters, $site, $page, $perPage, $sort);

$timeEnd = microtime(true);

$pager = new Html_Pager(

$res->hits->total, $perPage, $page + 1,

buildLink($baseLink, $filters, null, null)

);

foreach ($res->hits->hits as &$hit) {

$doc = $hit->_source;

if (!isset($doc->title) || $doc->title == '') {

$doc->title = '(no title)';

$doc->htmlTitle = '(no title)';

}

if (isset($hit->highlight->title[0])) {

$doc->htmlTitle = $hit->highlight->title[0];

} else {

$doc->htmlTitle = htmlspecialchars($doc->title);

}

if (isset($hit->highlight->text[0])) {

$doc->htmlText = $hit->highlight->text[0];

} else {

$doc->htmlText = null;

}

$doc->extra = new \stdClass();

$doc->extra->cleanUrl = preg_replace('#^.*://#', '', $doc->url);

if (isset($doc->status->modate)) {

$doc->extra->day = substr($doc->status->modate, 0, 10);

}

}

foreach ($res->aggregations as $key => &$aggregation) {

foreach ($aggregation->buckets as &$bucket) {

$bucket->url = buildLink($baseLink, $filters, $key, $bucket->key);

}

}

if ($site !== null) {

$urlNoSite = buildLink('?q=' . urlencode($cleanQuery), $filters, null, null);

} else {

$urlNoSite = null;

}

$urlSortBase = buildLink(

preg_replace('#&sort=[^&]+#', '', $baseLink), $filters, null, null

);

$urlSorts = [];

foreach (['date', 'score'] as $sortMode) {

if ($sortMode === $GLOBALS['phinde']['defaultSort']) {

$urlSorts[$sortMode] = $urlSortBase;

} else {

$urlSorts[$sortMode] = $urlSortBase . '&sort=' . $sortMode;

}

}

if (isset($_GET['format']) && $_GET['format'] == 'opensearch') {

$template = 'opensearch';

$baseLink .= '&format=opensearch';

header('Content-type: application/atom+xml');

} else {

$template = 'search';

}

render(

$template,

array(

'queryTime' => round($timeEnd - $timeBegin, 2) . 's',

'query' => $query,

'fullUrl' => Helper::fullUrl($baseLink),

'cleanQuery' => $cleanQuery,

'urlNoSite' => $urlNoSite,

'site' => $site,

'siteParam' => $siteParam,

'hitcount' => $res->hits->total,

'hits' => $res->hits->hits,

'aggregations' => $res->aggregations,

'activeFilters' => $activeFilters,

'pager' => $pager,

'sort' => $sort,

'urlSorts' => $urlSorts,

'hitTemplate' => 'search/' . $GLOBALS['phinde']['hitTemplate'],

'sidebarinclude' => $GLOBALS['phinde']['sidebarinclude'],

)

);

?>

一键复制

编辑

Web IDE

原始数据

按行查看

历史

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值