Fulltext search index

This commit is contained in:
Dan 2025-02-14 10:21:17 +07:00
commit e4dfc429d7
10 changed files with 500 additions and 168 deletions

View file

@ -206,3 +206,6 @@ views.layout = "layout"
; Admin theme mode: light or dark
admin.theme = "light"
; Fulltext search
fulltext.search = "false"

View file

@ -239,11 +239,46 @@ function add_content($title, $tag, $url, $content, $user, $draft, $category, $ty
}
$searchFile = "content/data/search.json";
$search = array();
$oldfile = $oldfile;
$newfile = $dir . $filename;
if ($oldfile !== $newfile && !is_null($autoSave)) {
if (file_exists($oldfile)) {
rename($oldfile, $newfile);
if (config('fulltext.search') == "true") {
if (file_exists($searchFile)) {
$search = json_decode(file_get_data($searchFile), true);
}
$old_filename = pathinfo($oldfile, PATHINFO_FILENAME);
$old_ex = explode('_', $old_filename);
$old_url = $old_ex[2];
$oKey = 'post_' . $old_url;
$nKey = 'post_' . $post_url;
if ($old_url != $post_url) {
if (isset($search[$oKey])) {
$arr = replace_key($search, $oKey, $nKey);
$arr[$nKey] = $post_content;
save_json_pretty($searchFile, $arr);
}
}
}
}
} else {
if (config('fulltext.search') == "true") {
if (file_exists($searchFile)) {
$search = json_decode(file_get_data($searchFile), true);
}
if (!isset($search['flock_fail'])) {
$search['post_' . $post_url] = $post_content;
save_json_pretty($searchFile, $search);
}
}
}
@ -473,20 +508,46 @@ function edit_content($title, $tag, $url, $content, $oldfile, $revertPost, $publ
rebuilt_cache('all');
clear_post_cache($dt, $post_tag, $post_url, $oldfile, $category, $type);
$searchFile = "content/data/search.json";
$search = array();
$old_filename = pathinfo($oldfile, PATHINFO_FILENAME);
$old_ex = explode('_', $old_filename);
$old_url = $old_ex[2];
if ($old_url != $post_url) {
$oKey = 'post_' . $old_url;
$nKey = 'post_' . $post_url;
if (file_exists($viewsFile)) {
$views = json_decode(file_get_data($viewsFile), true);
$oKey = 'post_' . $old_url;
$nKey = 'post_' . $post_url;
if (isset($views[$oKey])) {
$arr = replace_key($views, $oKey, $nKey);
save_json_pretty($viewsFile, $arr);
}
}
if (config('fulltext.search') == "true") {
if (file_exists($searchFile)) {
$search = json_decode(file_get_data($searchFile), true);
if (isset($search[$oKey])) {
$arr = replace_key($search, $oKey, $nKey);
$arr[$nKey] = $post_content;
save_json_pretty($searchFile, $arr);
}
}
}
} else {
if (config('fulltext.search') == "true") {
if (file_exists($searchFile)) {
$search = json_decode(file_get_data($searchFile), true);
}
if (!isset($search['flock_fail'])) {
$search['post_' . $post_url] = $post_content;
save_json_pretty($searchFile, $search);
}
}
}
if (!is_null($autoSave)) {
@ -1831,3 +1892,25 @@ function authorized ($data = null)
}
}
}
// Add search index
function add_search_index($id, $content)
{
$dir = 'content/data/';
if (!is_dir($dir)) {
mkdir($dir, 0775, true);
}
$filename = "content/data/search.json";
$search = array();
if (file_exists($filename)) {
$search = json_decode(file_get_data($filename), true);
}
if (isset($search['flock_fail'])) {
return;
} else {
if (!isset($search[$id])) {
$search[$id] = $content;
save_json_pretty($filename, $search);
}
}
}

View file

@ -188,8 +188,25 @@ Please install and enable the INTL extension to format the date format to your l
</div>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Fulltext search</label>
<div class="col-sm-10">
<div class="col-sm-10">
<div class="form-check">
<input class="form-check-input" type="radio" name="-config-fulltext.search" id="fulltext.search1" value="true" <?php if (config('fulltext.search') === 'true'):?>checked<?php endif;?>>
<label class="form-check-label" for="fulltext.search1">
<?php echo i18n('Enable');?>
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="-config-fulltext.search" id="fulltext.search2" value="false" <?php if (config('fulltext.search') === 'false'):?>checked<?php endif;?>>
<label class="form-check-label" for="fulltext.search2">
<?php echo i18n('Disable');?>
</label>
</div>
</div>
</div>
</div>
<hr />
<div class="form-group row">
<div class="col-sm-10">

View file

@ -198,6 +198,15 @@ if (isset($author[0])) {
</p>
</a>
</li>
<?php if (config('fulltext.search') == 'true') : ?>
<li class="nav-item">
<a href="<?php echo site_url();?>admin/search" class="nav-link">
<p>
<?php echo i18n('Search');?> Index
</p>
</a>
</li>
<?php endif; ?>
<?php if ($role === 'admin'):?>
<li class="nav-item">
<a href="<?php echo site_url();?>admin/update" class="nav-link">

View file

@ -0,0 +1,57 @@
<?php if (!defined('HTMLY')) die('HTMLy'); ?>
<!DOCTYPE html>
<html lang="<?php echo blog_language();?>">
<head>
<?php echo head_contents();?>
<title><?php echo $title;?></title>
<meta name="description" content="<?php echo $description; ?>"/>
<link rel="canonical" href="<?php echo $canonical; ?>" />
</head>
<body>
<div id="progress" style="border:1px solid #ccc;"></div>
<div id="information" style="width:100%"></div>
</body>
</html>
<?php
$search = $search;
$tmp = array();
foreach ($search as $k => $v) {
$tmp[] = $v;
}
// Loop through process
for($i = 0, $size = count($tmp); $i < $size; ++$i) {
$content = file_get_contents($tmp[$i][1]);
add_search_index('post_' . $tmp[$i][0], $content);
// Calculate the percentage
$percent = intval($i/$size * 100)."%";
// Progress bar and information
echo '<script language="javascript">
document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.';background-color:#ddd;\">&nbsp;</div>";
document.getElementById("information").innerHTML="File <strong>'.$tmp[$i][1].'</strong> processed.";
</script>';
// Buffer
echo str_repeat(' ',1024*64);
// Send output to browser
ob_flush();
// Sleep one second
sleep(1);
}
// The process is completed
echo '<script language="javascript">
document.getElementById("progress").innerHTML="<div style=\"width:100%;background-color:#ddd;\">&nbsp;</div>";
document.getElementById("information").innerHTML="Process completed";
</script>';
echo '<a href="' . site_url() .'admin/search">Back to search index</a>';
// Redir
echo '<script language="javascript">window.location.href = "' .site_url() . 'admin/search"</script>';

View file

@ -0,0 +1,59 @@
<?php if (!defined('HTMLY')) die('HTMLy'); ?>
<h2 class="post-index"><?php echo $heading ?></h2>
<br>
<?php $search_index = array(); if (!empty($posts)) { ?>
<form method="POST" action="<?php echo site_url();?>admin/search/reindex">
<p>The following are posts on the website that have not yet been included in the website search index.</p>
<input type="submit" class="btn btn-primary" value="Add below posts to Index">
<br><br>
<table class="table post-list">
<thead>
<tr class="head">
<th><?php echo i18n('Title');?></th>
<th><?php echo i18n('Published');?></th><?php if (config("views.counter") == "true"): ?>
<th><?php echo i18n('Views');?></th><?php endif; ?>
<th><?php echo i18n('Author');?></th>
<th><?php echo i18n('Category');?></th>
<th><?php echo i18n('Tags');?></th>
</tr>
</thead>
<tbody>
<?php foreach ($posts as $p): ?>
<tr>
<td><a target="_blank" href="<?php echo $p->url ?>"><?php echo $p->title ?></a></td>
<td><?php echo format_date($p->date) ?></td>
<?php if (config("views.counter") == "true"): ?>
<td><?php echo $p->views ?></td><?php endif; ?>
<td><a target="_blank" href="<?php echo $p->authorUrl ?>"><?php echo $p->author ?></a></td>
<td><a href="<?php echo site_url() . 'admin/categories/' . $p->categorySlug; ?>"><?php echo $p->categoryTitle;?></a></td>
<td><?php echo str_replace('rel="tag"', 'rel="tag" class="badge badge-light text-primary font-weight-normal"', $p->tag); ?></td>
</tr>
<?php $search_index[] = array($p->slug, $p->file);?>
<?php endforeach; ?>
</tbody>
</table>
<input type="submit" class="btn btn-primary" value="Add above posts to Index">
<br><br>
<?php if (!empty($pagination['prev']) || !empty($pagination['next'])): ?>
<br>
<div class="pager">
<ul class="pagination">
<?php if (!empty($pagination['prev'])) { ?>
<li class="newer page-item"><a class="page-link" href="?page=<?php echo $page - 1 ?>" rel="prev">&#8592; <?php echo i18n('Newer');?></a></li>
<?php } else { ?>
<li class="page-item disabled" ><span class="page-link">&#8592; <?php echo i18n('Newer');?></span></li>
<?php } ?>
<li class="page-number page-item disabled"><span class="page-link"><?php echo $pagination['pagenum'];?></span></li>
<?php if (!empty($pagination['next'])) { ?>
<li class="older page-item" ><a class="page-link" href="?page=<?php echo $page + 1 ?>" rel="next"><?php echo i18n('Older');?> &#8594;</a></li>
<?php } else { ?>
<li class="page-item disabled" ><span class="page-link"><?php echo i18n('Older');?> &#8594;</span></li>
<?php } ?>
</ul>
</div>
<?php endif; ?>
<input type="hidden" name="search_index" value="<?php print_r(htmlspecialchars(json_encode($search_index)));?>">
</form>
<?php } else {
echo count(get_blog_posts()) . ' published posts has been indexed!';
} ?>

View file

@ -95,5 +95,6 @@
"show.version",
"thumbnail.width",
"rss.description",
"admin.theme"
"admin.theme",
"fulltext.search"
]

View file

@ -1703,6 +1703,112 @@ post('/admin/import', function () {
}
});
// Show admin/search
get('/admin/search', function () {
$user = $_SESSION[site_url()]['user'];
$role = user('role', $user);
config('views.root', 'system/admin/views');
if (login()) {
if ($role === 'editor' || $role === 'admin' && config('fulltext.search') == "true") {
$page = from($_GET, 'page');
$page = $page ? (int)$page : 1;
$perpage = 40;
$tmp = array();
$search = array();
$total = '';
$searchFile = "content/data/search.json";
if (file_exists($searchFile)) {
$search = json_decode(file_get_contents($searchFile), true);
}
$posts = get_blog_posts();
foreach ($posts as $index => $v) {
$arr = explode('_', $v['filename']);
if (!isset($search['post_' . $arr[2]])) {
$tmp[] = $v;
}
}
if (!empty($tmp)) {
$posts = get_posts($tmp, $page, $perpage);
$total = count($tmp);
}
if (empty($tmp) || $page < 1) {
render('search', array(
'title' => generate_title('is_default', i18n('Search')),
'heading' => i18n('Search') . ' Index',
'description' => safe_html(strip_tags(blog_description())),
'canonical' => site_url(),
'metatags' => generate_meta(null, null),
'bodyclass' => 'no-posts',
'breadcrumb' => '<a href="' . site_url() . '">' . config('breadcrumb.home') . '</a> &#187; ' . i18n('Search')
));
die;
}
render('search', array(
'title' => generate_title('is_default', i18n('Search')),
'description' => safe_html(strip_tags(blog_description())),
'canonical' => site_url(),
'metatags' => generate_meta(null, null),
'heading' => i18n('Search') . ' Index',
'page' => $page,
'posts' => $posts,
'bodyclass' => 'all-index-posts',
'type' => 'is_admin-index-posts',
'is_admin' => true,
'breadcrumb' => '<a href="' . site_url() . '">' . config('breadcrumb.home') . '</a> &#187; ' . i18n('Search'),
'pagination' => has_pagination($total, $perpage, $page)
));
} else {
render('denied', array(
'title' => generate_title('is_default', i18n('Search')),
'description' => safe_html(strip_tags(blog_description())),
'canonical' => site_url(),
'metatags' => generate_meta(null, null),
'type' => 'is_admin-index-posts',
'is_admin' => true,
'bodyclass' => 'denied',
'breadcrumb' => '<a href="' . site_url() . '">' . config('breadcrumb.home') . '</a> &#187; ' . i18n('Search')
));
}
} else {
$login = site_url() . 'login';
header("location: $login");
}
});
post('/admin/search/reindex', function () {
if (login()) {
$user = $_SESSION[site_url()]['user'];
$role = user('role', $user);
$search = json_decode(htmlspecialchars_decode($_POST['search_index']));
config('views.root', 'system/admin/views');
if ($role === 'editor' || $role === 'admin' && config('fulltext.search') == "true") {
render('search-reindex', array(
'title' => generate_title('is_default', i18n('Search')),
'description' => safe_html(strip_tags(blog_description())),
'canonical' => site_url(),
'metatags' => generate_meta(null, null),
'type' => 'is_admin-search',
'search' => $search,
'is_admin' => true,
'bodyclass' => 'admin-search',
'breadcrumb' => '<a href="' . site_url() . '">' . config('breadcrumb.home') . '</a> &#187; ' . i18n('Search')
));
}
}
});
// Show Config page
get('/admin/config', function () {
@ -3409,9 +3515,12 @@ get('/search/:keyword', function ($keyword) {
$page = from($_GET, 'page');
$page = $page ? (int)$page : 1;
$perpage = config('search.perpage');
$total = '';
$posts = get_keyword($keyword, $page, $perpage);
$total = keyword_count($keyword);
if ($posts) {
$total = $posts[1];
}
$tsearch = new stdClass;
$tsearch->title = $keyword;
@ -3466,7 +3575,7 @@ get('/search/:keyword', function ($keyword) {
'canonical' => $tsearch->url . $CanonicalPageNum,
'metatags' => generate_meta('is_search', $tsearch),
'page' => $page,
'posts' => $posts,
'posts' => $posts[0],
'search' => $tsearch,
'taxonomy' => $tsearch,
'bodyclass' => 'in-search search-' . strtolower($keyword),
@ -3482,7 +3591,11 @@ get('/search/:keyword/feed', function ($keyword) {
header('Content-Type: application/rss+xml');
$posts = array();
$posts = get_keyword($keyword, 1, config('rss.count'));
if ($posts) {
$posts = $posts[0];
}
$data = new stdClass;
$data->title = $keyword;

View file

@ -407,7 +407,9 @@ function render($view, $locals = null, $layout = null)
$layout = ($layout == null) ? 'layout' : $layout;
}
$layout = "{$view_root}/{$layout}.html.php";
header('Content-type: text/html; charset=utf-8');
if ($view != 'search-reindex') {
header('Content-type: text/html; charset=utf-8');
}
if (config('generation.time') == 'true') {
ob_start();
require $layout;

View file

@ -11,13 +11,11 @@ function get_blog_posts()
{
static $_posts = array();
if (empty($_posts)) {
$url = 'cache/index/index-posts.txt';
if (!file_exists($url)) {
rebuilt_cache('all');
}
$_posts = unserialize(file_get_contents($url));
$url = 'cache/index/index-posts.txt';
if (!file_exists($url)) {
rebuilt_cache('all');
}
$_posts = unserialize(file_get_contents($url));
return $_posts;
}
@ -26,13 +24,11 @@ function get_static_pages()
{
static $_page = array();
if (empty($_page)) {
$url = 'cache/index/index-pages.txt';
if (!file_exists($url)) {
rebuilt_cache('all');
}
$_page = unserialize(file_get_contents($url));
$url = 'cache/index/index-pages.txt';
if (!file_exists($url)) {
rebuilt_cache('all');
}
$_page = unserialize(file_get_contents($url));
return $_page;
}
@ -41,13 +37,12 @@ function get_static_subpages($static = null)
{
static $_sub_page = array();
if (empty($_sub_page)) {
$url = 'cache/index/index-subpages.txt';
if (!file_exists($url)) {
rebuilt_cache('all');
}
$_sub_page = unserialize(file_get_contents($url));
$url = 'cache/index/index-subpages.txt';
if (!file_exists($url)) {
rebuilt_cache('all');
}
$_sub_page = unserialize(file_get_contents($url));
if ($static != null) {
$stringLen = strlen($static);
return array_filter($_sub_page, function ($sub_page) use ($static, $stringLen) {
@ -72,13 +67,11 @@ function get_author_name()
{
static $_author = array();
if (empty($_author)) {
$url = 'cache/index/index-author.txt';
if (!file_exists($url)) {
rebuilt_cache('all');
}
$_author = unserialize(file_get_contents($url));
$url = 'cache/index/index-author.txt';
if (!file_exists($url)) {
rebuilt_cache('all');
}
$_author = unserialize(file_get_contents($url));
return $_author;
}
@ -87,13 +80,13 @@ function get_author_name()
function get_draft_posts()
{
static $_draft = array();
if (empty($_draft)) {
$url = 'cache/index/index-draft.txt';
if (!file_exists($url)) {
rebuilt_cache('all');
}
$_draft = unserialize(file_get_contents($url));
$url = 'cache/index/index-draft.txt';
if (!file_exists($url)) {
rebuilt_cache('all');
}
$_draft = unserialize(file_get_contents($url));
return $_draft;
}
@ -101,16 +94,16 @@ function get_draft_posts()
function get_draft_pages()
{
static $_draftPage = array();
if (empty($_draftPage)) {
$tmp = array();
$tmp = glob('content/static/draft/*.md', GLOB_NOSORT);
if (is_array($tmp)) {
foreach ($tmp as $file) {
$_draftPage[] = pathinfo($file);
}
$tmp = array();
$tmp = glob('content/static/draft/*.md', GLOB_NOSORT);
if (is_array($tmp)) {
foreach ($tmp as $file) {
$_draftPage[] = pathinfo($file);
}
usort($_draftPage, "sortfile_a");
}
usort($_draftPage, "sortfile_a");
return $_draftPage;
}
@ -118,16 +111,16 @@ function get_draft_pages()
function get_draft_subpages($static = null)
{
static $_draftSubpage = array();
if (empty($_draftSubpage)) {
$tmp = array();
$tmp = glob('content/static/*/draft/*.md', GLOB_NOSORT);
if (is_array($tmp)) {
foreach ($tmp as $file) {
$_draftSubpage[] = pathinfo($file);
}
$tmp = array();
$tmp = glob('content/static/*/draft/*.md', GLOB_NOSORT);
if (is_array($tmp)) {
foreach ($tmp as $file) {
$_draftSubpage[] = pathinfo($file);
}
usort($_draftSubpage, "sortfile_a");
}
usort($_draftSubpage, "sortfile_a");
if ($static != null) {
$stringLen = strlen($static);
return array_filter($_draftSubpage, function ($sub_page) use ($static, $stringLen) {
@ -151,13 +144,13 @@ function get_draft_subpages($static = null)
function get_scheduled_posts()
{
static $_scheduled = array();
if (empty($_scheduled)) {
$url = 'cache/index/index-scheduled.txt';
if (!file_exists($url)) {
rebuilt_cache('all');
}
$_scheduled = unserialize(file_get_contents($url));
$url = 'cache/index/index-scheduled.txt';
if (!file_exists($url)) {
rebuilt_cache('all');
}
$_scheduled = unserialize(file_get_contents($url));
return $_scheduled;
}
@ -165,13 +158,13 @@ function get_scheduled_posts()
function get_category_files()
{
static $_desc = array();
if (empty($_desc)) {
$url = 'cache/index/index-category-files.txt';
if (!file_exists($url)) {
rebuilt_cache('all');
}
$_desc = unserialize(file_get_contents($url));
$url = 'cache/index/index-category-files.txt';
if (!file_exists($url)) {
rebuilt_cache('all');
}
$_desc = unserialize(file_get_contents($url));
return $_desc;
}
@ -179,15 +172,15 @@ function get_category_files()
function get_category_folder()
{
static $_dfolder = array();
if (empty($_dfolder)) {
$tmp = array();
$tmp = glob('content/*/blog/*/', GLOB_ONLYDIR);
if (is_array($tmp)) {
foreach ($tmp as $dir) {
$_dfolder[] = $dir;
}
$tmp = array();
$tmp = glob('content/*/blog/*/', GLOB_ONLYDIR);
if (is_array($tmp)) {
foreach ($tmp as $dir) {
$_dfolder[] = $dir;
}
}
return $_dfolder;
}
@ -195,13 +188,13 @@ function get_category_folder()
function get_category_slug()
{
static $_cslug = array();
if (empty($_cslug)) {
$url = 'cache/index/index-category.txt';
if (!file_exists($url)) {
rebuilt_cache('all');
}
$_cslug = unserialize(file_get_contents($url));
$url = 'cache/index/index-category.txt';
if (!file_exists($url)) {
rebuilt_cache('all');
}
$_cslug = unserialize(file_get_contents($url));
return $_cslug;
}
@ -210,13 +203,7 @@ function get_zip_files()
{
static $_zip = array();
if (empty($_zip)) {
// Get the names of all the
// zip files.
$_zip = glob('backup/*.zip');
}
$_zip = glob('backup/*.zip');
return $_zip;
}
@ -224,16 +211,16 @@ function get_zip_files()
// Get images in content/images folder
function scan_images() {
static $_images = array();
if (empty($_images)) {
$tmp = array();
$tmp = array_filter(glob('content/images/*', GLOB_NOSORT), 'is_file');
if (is_array($tmp)) {
foreach ($tmp as $file) {
$_images[] = pathinfo($file);
}
$tmp = array();
$tmp = array_filter(glob('content/images/*', GLOB_NOSORT), 'is_file');
if (is_array($tmp)) {
foreach ($tmp as $file) {
$_images[] = pathinfo($file);
}
usort($_images, "sortfile_d");
}
usort($_images, "sortfile_d");
return $_images;
}
@ -390,6 +377,7 @@ function get_posts($posts, $page = 1, $perpage = 0)
}
$tmp = array();
$views = array();
// Extract a specific page with results
$posts = array_slice($posts, ($page - 1) * $perpage, $perpage);
@ -398,13 +386,10 @@ function get_posts($posts, $page = 1, $perpage = 0)
$auto = config('toc.automatic');
$counter = config('views.counter');
$caption = config('fig.captions');
if ($counter == 'true') {
$viewsFile = "content/data/views.json";
if (file_exists($viewsFile)) {
$views = json_decode(file_get_contents($viewsFile), true);
}
$viewsFile = "content/data/views.json";
if (file_exists($viewsFile) && $counter == 'true') {
$views = json_decode(file_get_contents($viewsFile), true);
}
foreach ($posts as $index => $v) {
@ -550,11 +535,6 @@ function get_posts($posts, $page = 1, $perpage = 0)
}
}
// Convert image tags to figures
if ($caption == 'true') {
$post->body = preg_replace( '/<p>(<img .*?alt="(.*?)"\s*\/>)<\/p>/', '<figure>$1<figcaption>$2</figcaption></figure>', $post->body );
}
if ($counter == 'true') {
$post->views = get_views('post_' . $post->slug, $post->file, $views);
} else {
@ -578,11 +558,9 @@ function get_pages($pages, $page = 1, $perpage = 0)
$auto = config('toc.automatic');
$counter = config('views.counter');
if ($counter == 'true') {
$viewsFile = "content/data/views.json";
if (file_exists($viewsFile)) {
$views = json_decode(file_get_contents($viewsFile), true);
}
$viewsFile = "content/data/views.json";
if (file_exists($viewsFile) && $counter == 'true') {
$views = json_decode(file_get_contents($viewsFile), true);
}
// Extract a specific page with results
@ -657,11 +635,9 @@ function get_subpages($sub_pages, $page = 1, $perpage = 0)
$auto = config('toc.automatic');
$counter = config('views.counter');
if ($counter == 'true') {
$viewsFile = "content/data/views.json";
if (file_exists($viewsFile)) {
$views = json_decode(file_get_contents($viewsFile), true);
}
$viewsFile = "content/data/views.json";
if (file_exists($viewsFile) && $counter == 'true') {
$views = json_decode(file_get_contents($viewsFile), true);
}
// Extract a specific page with results
@ -1319,38 +1295,45 @@ function get_frontpage()
// Return search page.
function get_keyword($keyword, $page, $perpage)
{
if (strlen($keyword) >= 3) { // three-character minimum
$posts = get_blog_posts();
if (strlen($keyword) < 3) {
return false;
}
$tmp = array();
$posts = get_blog_posts();
foreach ($posts as $index => $v) {
$tmp = array();
$search = array();
$filepath = $v['dirname'] . '/' . $v['basename'];
$searchFile = "content/data/search.json";
$findRxWhole = '\b' . preg_quote($keyword, '~') . '\b'; // whole-words only
if (file_exists($searchFile) && config('fulltext.search') == "true") {
$search = json_decode(file_get_contents($searchFile), true);
}
$findRx = "~{$findRxWhole}~iu"; // Case-insensitive and UTF-8 mode
foreach ($posts as $index => $v) {
$arr = explode('_', $v['filename']);
$lines = file($filepath);
if (isset($search['post_' . $arr[2]])) {
$filter = $search['post_' . $arr[2]];
} else {
$filter = $arr[1] . ' ' . $arr[2];
}
foreach ($lines as $line) {
if (preg_match ($findRx, $line)) {
if (!in_array($v, $tmp)) {
$tmp[] = $v;
}
}
if (stripos($filter, $keyword) !== false) {
if (!in_array($v, $tmp)) {
$tmp[] = $v;
}
}
if (empty($tmp)) {
return false;
}
return $tmp = get_posts($tmp, $page, $perpage);
}
if (empty($tmp)) {
return false;
}
return $tmp = array(get_posts($tmp, $page, $perpage), count($tmp));
}
// Get related posts base on post category.
@ -1516,34 +1499,41 @@ function get_tagcount($var)
// Return search result count
function keyword_count($keyword)
{
if (strlen($keyword) >= 3) { // three-character minimum
if (strlen($keyword) < 3) {
return false;
}
$posts = get_blog_posts();
$posts = get_blog_posts();
$tmp = array();
$tmp = array();
$search = array();
foreach ($posts as $index => $v) {
$searchFile = "content/data/search.json";
$filepath = $v['dirname'] . '/' . $v['basename'];
if (file_exists($searchFile) && config('fulltext.search') == "true") {
$search = json_decode(file_get_contents($searchFile), true);
}
$findRxWhole = '\b' . preg_quote($keyword, '~') . '\b'; // whole-words only
foreach ($posts as $index => $v) {
$arr = explode('_', $v['filename']);
$findRx = "~{$findRxWhole}~iu"; // Case-insensitive and UTF-8 mode
if (isset($search['post_' . $arr[2]])) {
$filter = $search['post_' . $arr[2]];
} else {
$filter = $arr[1] . ' ' . $arr[2];
}
$lines = file($filepath);
foreach ($lines as $line) {
if (preg_match ($findRx, $line)) {
if (!in_array($v, $tmp)) {
$tmp[] = $v;
}
}
if (stripos($filter, $keyword) !== false) {
if (!in_array($v, $tmp)) {
$tmp[] = $v;
}
}
$tmp = array_unique($tmp, SORT_REGULAR);
return count($tmp);
}
$tmp = array_unique($tmp, SORT_REGULAR);
return count($tmp);
}
// Return recent posts lists
@ -3848,11 +3838,9 @@ function tag_i18n($tag)
{
static $tags = array();
if (empty($tags)) {
$filename = "content/data/tags.lang";
if (file_exists($filename)) {
$tags = unserialize(file_get_contents($filename));
}
$filename = "content/data/tags.lang";
if (file_exists($filename)) {
$tags = unserialize(file_get_contents($filename));
}
if (isset($tags[$tag])) {
return $tags[$tag];