Skip to content

Commit 84b36bc

Browse files
committed
搜索支持关键词高亮,修复最后一页错误的问题
1 parent b39c1be commit 84b36bc

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

server/apps/controllers/Wiki.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ function index()
4040
$this->swoole->tpl->display("wiki/noframe/index.html");
4141
}
4242

43+
/**
44+
* 高亮关键词
45+
* @param \XSSearch $xs
46+
* @param $subject
47+
* @return mixed
48+
*/
49+
protected static function highlight($xs, $subject)
50+
{
51+
return $xs->highlight(htmlspecialchars($subject, ENT_QUOTES, 'UTF-8'));
52+
}
53+
4354
function search()
4455
{
4556
if (isset($_GET['prid']))
@@ -63,12 +74,14 @@ function search()
6374

6475
$pagesize = 10;
6576
$page = empty($_GET['page']) ? 1: intval($_GET['page']);
77+
78+
$s = microtime(true);
6679
$xs = new \XS(WEBPATH.'/search.ini');
6780
$search = $xs->getSearch();
6881
$q = trim($_GET['q']);
6982
$search->setQuery($q);
7083
$total = $search->count();
71-
if ($page * $pagesize > $total)
84+
if (($page - 1) * $pagesize > $total)
7285
{
7386
$page = 1;
7487
}
@@ -79,12 +92,14 @@ function search()
7992
foreach ($docs as $doc)
8093
{
8194
$li['id'] = $doc->pid;
82-
$li['title'] = $doc->subject;
83-
$li['desc'] = $doc->message;
95+
$li['title'] = self::highlight($search, $doc->subject);
96+
$li['desc'] = self::highlight($search, $doc->message);
8497
$list[] = $li;
8598
}
8699
$pager->page_tpl = "/wiki/search/?q=".urlencode($_GET['q']).'&page={page}';
87100
$this->tpl->assign('list', $list);
101+
$this->tpl->assign('cost_time', round(microtime(true) - $s, 3));
102+
$this->tpl->assign('count', $total);
88103
$this->tpl->assign('pager', $pager->render());
89104
$this->tpl->display("wiki/noframe/search.html");
90105
}

server/apps/templates/wiki/noframe/search.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ <h3><a href="/wiki/page/{{$v.id}}.html">{{$v.text}}</a></h3>
7575
</div>
7676
</div>
7777
<div class="wiki_content blob instapaper_body">
78-
<article class="markdown-body entry-content" itemprop="mainContentOfPage">
78+
<article class="markdown-body" id="search_result">
79+
<div id="ResultStats">找到约 {{$count}} 条结果 (用时约 {{$cost_time}} 秒)</div>
7980
{{foreach item=li from=$list}}
8081
<h2><a href="/wiki/page/{{$li.id}}.html" target="_blank">{{$li.title}}</a></h2>
8182
<div>

web/static/css/wiki/noframe.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,8 @@
6666
background-color: #ffffff;
6767
border: 1px solid #dddddd;
6868
}
69+
70+
#search_result em {
71+
color: #dd4b39;
72+
font-style: normal;
73+
}

0 commit comments

Comments
 (0)