From fdd1b7c1d74d04883b75870b3313983a58f6286a Mon Sep 17 00:00:00 2001
From: "LMS.im"
Date: Sun, 17 Sep 2023 10:42:13 +0800
Subject: [PATCH] 3.0.2
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
添加整站文章字数统计功能,影响文件:functions.php、sidebar.php。
---
functions.php | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++
sidebar.php | 5 +++-
2 files changed, 73 insertions(+), 1 deletion(-)
diff --git a/functions.php b/functions.php
index 44e7bff..8ee3134 100644
--- a/functions.php
+++ b/functions.php
@@ -364,4 +364,73 @@ function get_humanized_date(int $created){
return date("Y-m-d", $created);
}
}
+}
+/**全站字数统计**/
+function get_blog_txtnum() {
+ $db = Typecho_Db::get();
+ $create = $db->fetchRow($db->select('created')->from('table.contents')->where('table.contents.type != ?', 'attachment')->limit('1')->order('created',Typecho_Db::SORT_DESC));
+ $update = $db->fetchRow($db->select('modified')->from('table.contents')->where('table.contents.type != ?', 'attachment')->limit('1')->order('modified',Typecho_Db::SORT_DESC));
+ $set_time = ($create>=$update) ? $create['modified'] : $update['modified'];
+
+ $Characters = Typecho_Cookie::get('site_Characters');
+ if($Characters){
+ $Characters = json_decode($Characters,true);
+ if($Characters['set'] <> $set_time){
+ $chars = 0;
+ $select = $db ->select('text')->from('table.contents')->where('table.contents.type != ?', 'attachment');
+ $rows = $db->fetchAll($select);
+ foreach ($rows as $row) {
+ $md = new Markdown();
+ $html_content = $md::convert($row['text']);
+ $html_content = preg_replace("/[^\x{4e00}-\x{9fa5}]/u", "", $html_content);
+ $chars += mb_strlen(trim($html_content), 'UTF-8');
+ }
+ $unit = '';
+ if($chars >= 10000) {
+ $chars = round($chars /= 10000,2); $unit = 'w';
+ }else if($chars >= 1000) {
+ $chars = round($chars /= 1000,2); $unit = 'k';
+ }
+ $out = array(
+ 'num' => $chars.$unit,
+ 'set' => $set_time
+ );
+ Typecho_Cookie::set('site_Characters',json_encode($out));
+ $total_num = $out['num'];
+ }else{
+ $total_num = $Characters['num'];
+ }
+ }else{
+ $chars = 0;
+ $select = $db ->select('text')->from('table.contents')->where('table.contents.type != ?', 'attachment');
+ $rows = $db->fetchAll($select);
+ foreach ($rows as $row) {
+ $md = new Markdown();
+ $html_content = $md::convert($row['text']);
+ $html_content = preg_replace("/[^\x{4e00}-\x{9fa5}]/u", "", $html_content);
+ $chars += mb_strlen(trim($html_content), 'UTF-8');
+ }
+ $unit = '';
+ if($chars >= 10000) {
+ $chars = round($chars /= 10000,2); $unit = 'w';
+ }else if($chars >= 1000) {
+ $chars = round($chars /= 1000,2); $unit = 'k';
+ }
+ $out = array(
+ 'num' => $chars.' '.$unit,
+ 'set' => $set_time
+ );
+ Typecho_Cookie::set('site_Characters',json_encode($out));
+ $total_num = $out['num'];
+ }
+
+ return $total_num;
+}
+/**文章字数统计**/
+function article_txt_count ($cid){
+ $db=Typecho_Db::get ();
+ $rs=$db->fetchRow ($db->select ('table.contents.text')->from ('table.contents')->where ('table.contents.cid=?',$cid)->order ('table.contents.cid',Typecho_Db::SORT_ASC)->limit (1));
+ $text = preg_replace("/[^\x{4e00}-\x{9fa5}]/u", "", $rs['text']);
+
+ echo mb_strlen($text,'UTF-8');
}
\ No newline at end of file
diff --git a/sidebar.php b/sidebar.php
index d85ad90..0b7fab3 100644
--- a/sidebar.php
+++ b/sidebar.php
@@ -39,11 +39,13 @@
}else{
echo '网海沉浮 ' .$day_pass. ' 天';
}
+ echo ',留下 '. get_blog_txtnum() .' 字。';
}else{
echo '猴年马月 加入独立博客';
}?>
+
options->sidebarBlock) && in_array('ShowCategory', $this->options->sidebarBlock)): ?>