Skip to content

Commit

Permalink
3.0.2
Browse files Browse the repository at this point in the history
添加整站文章字数统计功能,影响文件:functions.php、sidebar.php。
  • Loading branch information
happyet authored Sep 17, 2023
1 parent 90e6259 commit fdd1b7c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
69 changes: 69 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
5 changes: 4 additions & 1 deletion sidebar.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@
}else{
echo '网海沉浮 ' .$day_pass. '';
}
echo ',留下 '. get_blog_txtnum() .' 字。';
}else{
echo '猴年马月 加入独立博客';
}?></p>
</div>
</section>

<?php if (!empty($this->options->sidebarBlock) && in_array('ShowCategory', $this->options->sidebarBlock)): ?>
<section class="widget loft-inner">
<h3 class="widget-title"><?php _e('分类栏目'); ?></h3>
Expand Down Expand Up @@ -84,7 +86,8 @@
<section class="widget loft-inner">
<h3 class="widget-title"><?php _e('最近回复'); ?></h3>
<ul class="widget-list recent-comments">
<?php $this->widget('Widget_Comments_Recent','ignoreAuthor=true&pageSize=5')->to($comments); ?>
<?php $this->widget('Widget_Comments_Recent','pageSize=10&ignoreAuthor=true')->to($comments); ?>
<?php //\Widget\Comments\Recent::alloc()->to($comments); ?>
<?php while($comments->next()): ?>
<li>
<img class="avatar" src="<?php getAvatarByEmail($comments->mail,45); ?>">
Expand Down

0 comments on commit fdd1b7c

Please sign in to comment.