From b1466ba77e56810e56210bd57996c687456d8349 Mon Sep 17 00:00:00 2001 From: AlanDecode Date: Sun, 31 Mar 2019 15:20:50 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=A2=9E=E5=8A=A0=E5=AD=97=E6=95=B0?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E5=AD=97=E6=AE=B5=EF=BC=8C=E6=8F=90=E5=8D=87?= =?UTF-8?q?=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Archives.php | 5 ++--- assets/VOID.js | 12 ++++++++++++ functions.php | 8 ++++++++ includes/archives.php | 2 +- includes/banner.php | 2 +- index.php | 2 +- libs/Contents.php | 21 +++++++++++---------- libs/Utils.php | 38 ++++++++++++++++++++++++++------------ 8 files changed, 62 insertions(+), 28 deletions(-) diff --git a/Archives.php b/Archives.php index 02bdf045..9b3b92e7 100644 --- a/Archives.php +++ b/Archives.php @@ -32,11 +32,11 @@

字"; ?>

- $posts): ?> + $posts): ?>
- allow('comment')) $this->need('includes/comments.php'); ?> diff --git a/assets/VOID.js b/assets/VOID.js index c77c2d33..74b269fa 100644 --- a/assets/VOID.js +++ b/assets/VOID.js @@ -66,6 +66,7 @@ var getPrefersDarkModeState = function () { var VOID = { // 初始化单页应用 init : function(){ + VOID.countWords(); VOID.parseTOC(); VOID.parsePhotos(); VOID.parseUrl(); @@ -234,6 +235,7 @@ var VOID = { }else{ $('body>header').addClass('no-banner'); } + VOID.countWords(); VOID.parseTOC(); VOID.parsePhotos(); VOID.parseUrl(); @@ -347,6 +349,16 @@ var VOID = { } } }); + }, + + countWords : function(){ + if($('#totalWordCount').length) { + var total=0; + $.each($('a.archive-title'), function(i, item){ + total += parseInt($(item).attr('data-words')); + }); + $('#totalWordCount').html(total); + } } }; diff --git a/functions.php b/functions.php index 2c97107b..3e92da39 100644 --- a/functions.php +++ b/functions.php @@ -26,6 +26,14 @@ Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = array('Contents','parseContent'); Typecho_Plugin::factory('Widget_Abstract_Contents')->excerptEx = array('Contents','parseContent'); +$db = Typecho_Db::get(); +$prefix = $db->getPrefix(); +// contents 表中若无 wordCount 字段则添加 +if (!array_key_exists('wordCount', $db->fetchRow($db->select()->from('table.contents')))) + $db->query('ALTER TABLE `'. $prefix .'contents` ADD `wordCount` INT(10) DEFAULT 0;'); +if (!array_key_exists('wordCountTime', $db->fetchRow($db->select()->from('table.contents')))) + $db->query('ALTER TABLE `'. $prefix .'contents` ADD `wordCountTime` INT(10) DEFAULT 0;'); + /** * 主题启用 */ diff --git a/includes/archives.php b/includes/archives.php index 150b65f5..e2e2ea2c 100644 --- a/includes/archives.php +++ b/includes/archives.php @@ -34,7 +34,7 @@ -

content), 'UTF-8'); ?>">title(); ?>

+

title(); ?>

fields->excerpt != ''): ?>

fields->excerpt; ?>

diff --git a/includes/banner.php b/includes/banner.php index b52844ce..05083ddc 100644 --- a/includes/banner.php +++ b/includes/banner.php @@ -45,7 +45,7 @@ is('archive')): ?>

template == 'Archives.php') - echo Utils::getCatNum()." 分类 × ".Utils::getPostNum()." 文章 × ".Utils::getTagNum()." 标签 × ".Utils::getWordCount()." 字"; + echo Utils::getCatNum()." 分类 × ".Utils::getPostNum()." 文章 × ".Utils::getTagNum()." 标签 × 字"; else{ ?>  •  diff --git a/index.php b/index.php index 67000ee7..8c5ab8dd 100644 --- a/index.php +++ b/index.php @@ -45,7 +45,7 @@

-

content), 'UTF-8'); ?>">title(); ?>

+

title(); ?>

fields->excerpt != '') echo "

{$this->fields->excerpt}

"; ?> diff --git a/libs/Contents.php b/libs/Contents.php index 8255f4b4..c0e3fc43 100644 --- a/libs/Contents.php +++ b/libs/Contents.php @@ -309,7 +309,6 @@ public static function thePrev($archive) } else { return null; } - } /** @@ -338,25 +337,27 @@ public static function theNext($archive) * * @return array */ - public static function archives($excerpt = false) + public static function archives($widget, $excerpt = false) { $db = Typecho_Db::get(); - $cids = $db->fetchAll($db->select('table.contents.cid') + $rows = $db->fetchAll($db->select() ->from('table.contents') ->order('table.contents.created', Typecho_Db::SORT_DESC) ->where('table.contents.type = ?', 'post') ->where('table.contents.status = ?', 'publish')); + $stat = array(); - foreach ($cids as $cid) { - $post = self::getPost($cid); + foreach ($rows as $row) { + $row = $widget->filter($row); + if($row['wordCount']==0) $row['wordCount'] = Utils::wordCountByCid($row['cid']); $arr = array( - 'title' => $post->title, - 'permalink' => $post->permalink, - 'words' => mb_strlen(preg_replace("/[^\x{4e00}-\x{9fa5}]/u", "", $post->content), 'UTF-8')); + 'title' => $row['title'], + 'permalink' => $row['permalink'], + 'words' => $row['wordCount']); if($excerpt){ - $arr['excerpt'] = substr($post->content, 30); + $arr['excerpt'] = substr($row['content'], 30); } - $stat[date('Y', $post->created)][$post->created] = $arr; + $stat[date('Y', $row['created'])][$row['created']] = $arr; } return $stat; } diff --git a/libs/Utils.php b/libs/Utils.php index 039dd093..cb6ac01f 100644 --- a/libs/Utils.php +++ b/libs/Utils.php @@ -222,22 +222,36 @@ public static function getTagNum() } /** - * 总字数 + * 单文章字数 * * @return int */ - public static function getWordCount() - { - $db = Typecho_Db::get(); - $posts = $db->fetchAll($db->select('table.contents.text') - ->from('table.contents') - ->where('table.contents.type = ?', 'post') - ->where('table.contents.status = ?', 'publish')); - $total = 0; - foreach ($posts as $post) { - $total = $total + mb_strlen(preg_replace("/[^\x{4e00}-\x{9fa5}]/u", "", $post['text']), 'UTF-8'); + public static function wordCount($archive){ + if($archive->wordCount == 0 || ($archive->modified > $archive->wordCountTime)){ + $db = Typecho_Db::get(); + $dbname =$db->getPrefix() . 'contents'; + $row = $db->fetchRow($db->select()->from('table.contents')->where('cid = ?', $archive->cid)); + $count = mb_strlen(preg_replace("/[^\x{4e00}-\x{9fa5}]/u", "", $row['text']), 'UTF-8'); + $db->query('update `'.$dbname.'` set `wordCount`='.$count.' where `cid`='.$archive->cid); + $db->query('update `'.$dbname.'` set `wordCountTime`='.$archive->modified.' where `cid`='.$archive->cid); + return $count; + } + else{ + return $archive->wordCount; } - return $total; + } + + /** + * 根据 cid 直接更新字数 + */ + public static function wordCountByCid($cid){ + $db = Typecho_Db::get(); + $dbname =$db->getPrefix() . 'contents'; + $row = $db->fetchRow($db->select()->from('table.contents')->where('cid = ?', $cid)); + $count = mb_strlen(preg_replace("/[^\x{4e00}-\x{9fa5}]/u", "", $row['text']), 'UTF-8'); + $db->query('update `'.$dbname.'` set `wordCount`='.$count.' where `cid`='.$cid); + $db->query('update `'.$dbname.'` set `wordCountTime`='.$row['modified'].' where `cid`='.$cid); + return $count; } /**