From e4dfc429d7396d6b28a3d4018af73cfd5a3bdbd2 Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 14 Feb 2025 10:21:17 +0700 Subject: [PATCH] Fulltext search index --- config/config.ini.example | 5 +- system/admin/admin.php | 89 +++++- system/admin/views/config.html.php | 21 +- system/admin/views/layout.html.php | 9 + system/admin/views/search-reindex.html.php | 57 ++++ system/admin/views/search.html.php | 59 ++++ system/configList.json | 3 +- system/htmly.php | 119 +++++++- system/includes/dispatch.php | 4 +- system/includes/functions.php | 308 ++++++++++----------- 10 files changed, 503 insertions(+), 171 deletions(-) create mode 100644 system/admin/views/search-reindex.html.php create mode 100644 system/admin/views/search.html.php diff --git a/config/config.ini.example b/config/config.ini.example index 972f790..2e11465 100644 --- a/config/config.ini.example +++ b/config/config.ini.example @@ -205,4 +205,7 @@ views.root = "themes/tailwind" views.layout = "layout" ; Admin theme mode: light or dark -admin.theme = "light" \ No newline at end of file +admin.theme = "light" + +; Fulltext search +fulltext.search = "false" \ No newline at end of file diff --git a/system/admin/admin.php b/system/admin/admin.php index 3452179..7c78821 100644 --- a/system/admin/admin.php +++ b/system/admin/admin.php @@ -238,12 +238,47 @@ function add_content($title, $tag, $url, $content, $user, $draft, $category, $ty mkdir($dir, 0775, true); } + + $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); + } } } @@ -472,22 +507,48 @@ 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)) { return json_encode(array('message' => 'Auto Saved', 'file' => $newfile)); @@ -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); + } + } +} \ No newline at end of file diff --git a/system/admin/views/config.html.php b/system/admin/views/config.html.php index d8998e6..2940a22 100644 --- a/system/admin/views/config.html.php +++ b/system/admin/views/config.html.php @@ -188,8 +188,25 @@ Please install and enable the INTL extension to format the date format to your l - - +
+ +
+
+
+ checked> + +
+
+ checked> + +
+
+
+

diff --git a/system/admin/views/layout.html.php b/system/admin/views/layout.html.php index fb68517..a7a5221 100644 --- a/system/admin/views/layout.html.php +++ b/system/admin/views/layout.html.php @@ -198,6 +198,15 @@ if (isset($author[0])) {

+ + +