Skip to content

Commit

Permalink
perf: 增加字数缓存字段,提升性能
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanDecode committed Mar 31, 2019
1 parent 65109f7 commit b1466ba
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 28 deletions.
5 changes: 2 additions & 3 deletions Archives.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
<?php if(!$setting['titleinbanner']): ?>
<p class="post-meta">
<?php
echo Utils::getCatNum()." 分类 × ".Utils::getPostNum()." 文章 × ".Utils::getTagNum()." 标签 × ".Utils::getWordCount()."";
echo Utils::getCatNum()." 分类 × ".Utils::getPostNum()." 文章 × ".Utils::getTagNum()." 标签 × <span id=\"totalWordCount\"></span>";
?>
</p>
<?php endif; ?>
<?php $archives = Contents::archives(); $index = 0; foreach ($archives as $year => $posts): ?>
<?php $archives = Contents::archives($this); $index = 0; foreach ($archives as $year => $posts): ?>
<section aria-label="<?php echo $year; ?>年归档列表" class="year<?php if($index > 0) echo ' shrink'; ?>" data-year="<?php echo $year; ?>" data-num="<?php echo count($posts); ?>">
<ul>
<?php foreach($posts as $created => $post): ?>
Expand All @@ -47,7 +47,6 @@
</section>
<?php $index = $index + 1; endforeach; ?>
</section>

<!--评论区,可选-->
<?php if ($this->allow('comment')) $this->need('includes/comments.php'); ?>
</div>
Expand Down
12 changes: 12 additions & 0 deletions assets/VOID.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var getPrefersDarkModeState = function () {
var VOID = {
// 初始化单页应用
init : function(){
VOID.countWords();
VOID.parseTOC();
VOID.parsePhotos();
VOID.parseUrl();
Expand Down Expand Up @@ -234,6 +235,7 @@ var VOID = {
}else{
$('body>header').addClass('no-banner');
}
VOID.countWords();
VOID.parseTOC();
VOID.parsePhotos();
VOID.parseUrl();
Expand Down Expand Up @@ -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);
}
}
};

Expand Down
8 changes: 8 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;');

/**
* 主题启用
*/
Expand Down
2 changes: 1 addition & 1 deletion includes/archives.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<meta itemprop="url" content="<?php if($this->fields->banner != '') echo $this->fields->banner; else Utils::gravatar($this->author->mail, 200); ?>">
</div>
<a class="title" href="<?php $this->permalink(); ?>">
<h1 itemprop="name" data-words="<?php echo mb_strlen(preg_replace("/[^\x{4e00}-\x{9fa5}]/u", "", $this->content), 'UTF-8'); ?>"><?php $this->title(); ?></h1>
<h1 itemprop="name" data-words="<?php echo Utils::wordCount($this); ?>"><?php $this->title(); ?></h1>
</a>
<?php if($this->fields->excerpt != ''): ?>
<p itemprop="headline" class="headline single"><?php echo $this->fields->excerpt; ?></p>
Expand Down
2 changes: 1 addition & 1 deletion includes/banner.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<?php if(!$this->is('archive')): ?>
<p class="post-meta">
<?php if($this->template == 'Archives.php')
echo Utils::getCatNum()." 分类 × ".Utils::getPostNum()." 文章 × ".Utils::getTagNum()." 标签 × ".Utils::getWordCount()."";
echo Utils::getCatNum()." 分类 × ".Utils::getPostNum()." 文章 × ".Utils::getTagNum()." 标签 × <span id=\"totalWordCount\"></span>";
else{ ?>
<span itemprop="author"><?php $this->author(); ?></span>&nbsp;•&nbsp;
<time datetime="<?php echo date('c', $this->created); ?>" itemprop="datePublished"><?php echo date('Y-m-d', $this->created); ?></time>
Expand Down
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<?php endif; ?>
<div class="content-wrap">
<a class="title" href="<?php $this->permalink(); ?>">
<h1 itemprop="name" data-words="<?php echo mb_strlen(preg_replace("/[^\x{4e00}-\x{9fa5}]/u", "", $this->content), 'UTF-8'); ?>"><?php $this->title(); ?></h1>
<h1 itemprop="name" data-words="<?php echo Utils::wordCount($this); ?>"><?php $this->title(); ?></h1>
</a>
<?php if($this->fields->excerpt != '') echo "<p itemprop=\"headline\" class=\"headline content\">{$this->fields->excerpt}</p>"; ?>

Expand Down
21 changes: 11 additions & 10 deletions libs/Contents.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,6 @@ public static function thePrev($archive)
} else {
return null;
}

}

/**
Expand Down Expand Up @@ -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;
}
Expand Down
38 changes: 26 additions & 12 deletions libs/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit b1466ba

Please sign in to comment.