网站统计

This commit is contained in:
浪子 2024-05-26 14:06:03 +08:00
parent f8fc4f293a
commit aec71a535b
4 changed files with 421 additions and 1 deletions

1
dist/js/chart.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -182,4 +182,267 @@ function show_first_image($content) {
return $matches[1][0];
}
return false; // 没有找到图片,返回 false
}
//开始增加某些奇怪的东西
// 获取月份
function getMonth() {
$path = $_SERVER['PHP_SELF']; // 获取路劲
preg_match('/\d{4}\/\d{2}\/\d{2}|\d{4}\/\d{2}/', $path, $date); // 匹配路劲中的日期
if (is_array($date) && count($date)) {
$date = explode('/', $date[0]); // 如果匹配到就分割日期
}else {
$date = date('Y/m/d', time()); // 如果没有匹配到就获取当前月
$date = explode('/', $date); // 分割日期
}
return $date;
}
// 获取指定月份的文章
function getMonthPost() {
$date = getMonth(); // 获取要查询文章的月份
$start = $date[0] . '-' . $date[1] . '-01 00:00:00'; // 月的第一天
$end = date('Y-m-t', strtotime($date[0] . '-' . $date[1] . '-' . '1 23:59:59')); // 月最后一天
$start = strtotime($start); // 把月的第一天转换为时间戳
$end = strtotime($end . ' 23:59:59'); // 把月的最后一天转换为时间戳
$db = Typecho_Db::get();
// 按照提供的月份查询出文件的时间
$post = $db->fetchAll($db->select('table.contents.created')->from('table.contents')->where('created >= ?', $start)->where('created <= ?', $end)->where('type = ?', 'post')->where('status = ?', 'publish'));
// 按照提供的月份查询前一个月的文章
$previous = $db->fetchAll($db->select('table.contents.created')->from('table.contents')->where('created < ?', $start)->where('type = ?', 'post')->where('status = ?', 'publish')->offset(0)->limit(1)->order('created', Typecho_Db::SORT_DESC));
// 按照提供的月份查询后一个月的文章
$next = $db->fetchAll($db->select('table.contents.created')->from('table.contents')->where('created > ?', $end)->where('type = ?', 'post')->where('status = ?', 'publish')->offset(0)->limit(1)->order('created', Typecho_Db::SORT_ASC));
if (count($next)) {
$next = date('Y/m/', $next[0]['created']); // 格式化前一个月的文章时间
}
if (count($previous)) {
$previous = date('Y/m/', $previous[0]['created']); // 格式化后一个月的文章时间
}
$day = array();
foreach ($post as $val) {
array_push($day, date('j', $val['created'])); // 把查询出的文章日加入数组
}
return array(
'post'=> $day,
'previous' => $previous,
'next' => $next
);
}
// 生成日历
function calendar($month, $url, $rewrite) {
$monthArr = getMonth(); // 获取月份
$post = getMonthPost(); // 获取文章日期
// 判断是否启用了地址重写功能
if ($rewrite) {
$monthUrl = $url . $monthArr[0] . '/' . $monthArr[1] . '/'; // 生成日期链接前缀
$previousUrl = is_array($post['previous'])?'':$url . $post['previous']; // 生成前一个月的跳转链接地址
$nextUrl = is_array($post['next'])?'':$url . $post['next']; // 生成后一个月的跳转链接地址
}else {
$monthUrl = $url . 'index.php/' . $monthArr[0] . '/' . $monthArr[1] . '/'; // 生成日期链接前缀
$previousUrl = is_array($post['previous'])?'':$url . 'index.php/' . $post['previous']; // 生成前一个月的跳转链接地址
$nextUrl = is_array($post['next'])?'':$url . 'index.php/' . $post['next']; // 生成后一个月的跳转链接地址
}
$postCount = array_count_values($post['post']); // 统计每天的文章数量
$calendar = ''; // 初始化
$week_arr = ['日', '一', '二', '三', '四', '五', '六']; // 表头
$this_month_days = (int)date('t', strtotime($month)); // 本月共多少天
$this_month_one_n = (int)date('w', strtotime($month)); // 本月1号星期几
$calendar .= '<table aria-label="' . $monthArr[0] . '年' . $monthArr[1] . '月日历" class="table table-bordered table-sm m-0"><thead><tr>'; // 表头
foreach ($week_arr as $k => $v){
if($k == 0){
$class = ' class="sunday"';
}elseif ($k == 6){
$class = ' class="saturday"';
}else{
$class = '';
}
$calendar .= '<th class="text-center py-2">' . $v . '</th>';
}
$calendar .= '</tr></thead><tbody>';
// 表身
// 计算本月共几行数据
$total_rows = ceil(($this_month_days - (7 - $this_month_one_n)) / 7) + 1;
$number = 1;
$flag = 0;
for ($row = 1;$row <= $total_rows;$row++){
$calendar .= '<tr>';
for ($week = 0;$week <= 6;$week ++){
if($number < 10){
$numbera = '0' . $number;
}else{
$numbera = $number;
}
if($number <= $this_month_days){
if ($number < 10) {
$zero = '0';
}else {
$zero = '';
}
if($row == 1){
if($week >= $this_month_one_n){
if (in_array($number, $post['post'])) {
$calendar .= '<td class="active text-center py-2">' . '<a rel="archives" href="' . $monthUrl . $zero . $number . '/' . '" class="p-0" title="' . $postCount[$number] . '篇文章" data-toggle="tooltip" data-placement="top"><b>' . $number . '</b></a>' . '</td>';
}else {
$calendar .= '<td class="text-center py-2">' . $number . '</td>';
}
$flag = 1;
}else{
$calendar .= '<td></td>';
}
}else{
if (in_array($number, $post['post'])) {
$calendar .= '<td class="active text-center py-2">' . '<a rel="archives" href="' . $monthUrl . $zero . $number . '/' . '" class="p-0" title="' . $postCount[$number] . '篇文章" data-toggle="tooltip" data-placement="top"><b>' . $number . '</b></a>' . '</td>';
}else {
$calendar .= '<td class="text-center py-2">' . $number . '</td>';
}
}
if($flag){
$number ++;
}
}else{
$calendar .= '<td></td>';
}
}
$calendar .= '</tr>';
}
$calendar .= '</tbody></table>';
return array(
'calendar' => $calendar,
'previous' => is_array($post['previous'])?false:$post['previous'],
'next' => is_array($post['next'])?false:$post['next'],
'previousUrl' => $previousUrl,
'nextUrl' => $nextUrl
);
}
// 获取分类数量
function categoryCount() {
$db = Typecho_Db::get();
$count = $db->fetchRow($db->select('COUNT(*)')->from('table.metas')->where('type = ?', 'category'));
return $count['COUNT(*)'];
}
// 获取标签数量
function tagCount() {
$db = Typecho_Db::get();
$count = $db->fetchRow($db->select('COUNT(*)')->from('table.metas')->where('type = ?', 'tag'));
return $count['COUNT(*)'];
}
// 获取总阅读量
function viewsCount() {
$db = Typecho_Db::get();
$count = $db->fetchRow($db->select('SUM(views) AS viewsCount')->from('table.contents'));
return $count['viewsCount'];
}
// 获取阅读量排名前 5 的 5 篇文章的信息
function top5post() {
$db = Typecho_Db::get();
$top5Post = $db->fetchAll($db->select()->from('table.contents')->where('type = ?', 'post')->where('status = ?', 'publish')->order('views', Typecho_Db::SORT_DESC)->offset(0)->limit(5));
$postList =array();
foreach ($top5Post as $post) {
$post = Typecho_Widget::widget('Widget_Abstract_Contents')->filter($post);
array_push($postList, array(
'title' => $post['title'],
'link' => $post['permalink'],
'views' => $post['views']
));
}
return $postList;
}
// 获取评论数排名前 5 的 5 篇文章的信息
function top5CommentPost() {
$db = Typecho_Db::get();
$top5Post = $db->fetchAll($db->select()->from('table.contents')->where('type = ?', 'post')->where('status = ?', 'publish')->order('commentsNum', Typecho_Db::SORT_DESC)->offset(0)->limit(5));
$postList = array();
foreach ($top5Post as $post) {
$post = Typecho_Widget::widget('Widget_Abstract_Contents')->filter($post);
array_push($postList, array(
'title' => $post['title'],
'link' => $post['permalink'],
'commentsNum' => $post['commentsNum']
));
}
return $postList;
}
// 获取 ECharts 格式要求的文章更新日历
function postCalendar($start, $end) {
$db = Typecho_Db::get();
$dateList = $db->fetchAll($db->select('created')->from('table.contents')->where('created > ?', $start)->where('created < ?', $end));
if (count($dateList) < 1) {
return array();
}
$dateList2 = array();
foreach ($dateList as $val) {
array_push($dateList2, date('Y-m-d', $val['created']));
}
$dateList2 = array_count_values($dateList2);
$key = array_keys($dateList2);
$dateList = array();
for ($i = 0;$i < count($dateList2);$i ++) {
array_push($dateList, array(
$key[$i],
$dateList2[$key[$i]]
));
}
return $dateList;
}
// 获取 ECharts 格式要求的评论更新日历
function commentCalendar($start, $end) {
$db = Typecho_Db::get();
$dateList = $db->fetchAll($db->select('created')->from('table.comments')->where('created > ?', $start)->where('created < ?', $end));
if (count($dateList) < 1) {
return array();
}
$dateList2 = array();
foreach ($dateList as $val) {
array_push($dateList2, date('Y-m-d', $val['created']));
}
$dateList2 = array_count_values($dateList2);
$key = array_keys($dateList2);
$dateList = array();
for ($i = 0;$i < count($dateList2);$i ++) {
array_push($dateList, array(
$key[$i],
$dateList2[$key[$i]]
));
}
return $dateList;
}
// 获取个分类的文章数量
function categoryPostCount() {
$db = Typecho_Db::get();
$count = $db->fetchAll($db->select('name', 'count AS value')->from('table.metas')->where('type = ?', 'category'));
if (count($count) < 1) {
return array();
}
return $count;
}
// 获取父分类的名称
function getParentCategory($categoryId) {
$db = Typecho_Db::get();
$category = $db->fetchRow($db->select()->from('table.metas')->where('mid = ?', $categoryId));
return $category['name'];
}
// 计算两个时间之间相差的天数
function getDays($time1, $time2) {
return floor(($time2 - $time1) / 86400);
}

156
page-data.php Normal file
View File

@ -0,0 +1,156 @@
<?php
/**
* 网站数据
* @package custom
*/
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
$GLOBALS['page'] = 'page-data';
$this->need('header.php');
?>
<section class="site--main">
<header class="archive--header">
<h1 class="post--single__title"><?php $this->title() ?></h1>
<h2 class="archive--title__year"> </h2>
</header>
<article class="post--single">
<div class="graph u-marginBottom30">
<div data-target="<?php $this->options->postLinkOpen(); ?>" class="post-content">
<h2>分类占比</h2>
<p>下面是个分类的文章占比:</p>
<div id="category-chart" style="height: 390px;"></div>
<h2>文章更新</h2>
<p>下面是 <?php echo date('Y年m月d日', time() - 20736000); ?> 到 <?php echo date('Y年m月d日', time()); ?> 的文章更新情况</p>
<div id="post-chart" style="height: 250px;"></div>
<h2>评论动态</h2>
<p>下面是 <?php echo date('Y年m月d日', time() - 20736000); ?> 到 <?php echo date('Y年m月d日', time()); ?> 的评论动态</p>
<div id="comment-chart" style="height: 250px;"></div>
<h2>最多阅读的文章</h2>
<?php $top5Post = top5post(); ?>
<p>下面是阅读量排名前 <?php echo count($top5Post); ?> 的文章</p>
<table class="pure-table pure-table-bordered">
<thead>
<tr>
<th>排名</th>
<th>文章</th>
<th>阅读量</th>
</tr>
</thead>
<tbody>
<?php $top = 1; ?>
<?php foreach ($top5Post as $post): ?>
<tr>
<td><?php echo $top; ?></td>
<td><a href="<?php echo $post['link']; ?>"><?php echo $post['title']; ?></a></td>
<td><?php echo $post['views']; ?></td>
</tr>
<?php $top ++; ?>
<?php endforeach; ?>
</tbody>
</table>
<h2>最多评论的文章</h2>
<?php $top5CommentPost = top5CommentPost(); ?>
<p>下面是评论数排名前 <?php echo count($top5CommentPost); ?> 的文章:</p>
<table class="pure-table pure-table-bordered">
<thead>
<tr>
<th>排名</th>
<th>文章</th>
<th>评论数</th>
</tr>
</thead>
<tbody>
<?php $top = 1; ?>
<?php foreach ($top5CommentPost as $post): ?>
<tr>
<td><?php echo $top; ?></td>
<td><a href="<?php echo $post['link']; ?>"><?php echo $post['title']; ?></a></td>
<td><?php echo $post['commentsNum']; ?></td>
</tr>
<?php $top ++; ?>
<?php endforeach; ?>
</tbody>
</table>
</div>
</article>
</section>
<script type="text/javascript">
var data = {
post: <?php echo json_encode(postCalendar(time() - 20736000, time())); ?>,
comment: <?php echo json_encode(commentCalendar(time() - 20736000, time())); ?>,
category: <?php echo json_encode(categoryPostCount()); ?>
};
</script>
<script type="text/javascript" src="<?php $this->options->themeUrl('/dist/js/chart.js'); ?>"></script>
<script id="MathJax-script" async src="https://jsd.onmicrosoft.cn/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<script>
// 配置 MathJax
MathJax = {
tex: {
inlineMath: [['$', '$']],
displayMath: [['$$', '$$']],
processEscapes: true,
processEnvironments: true,
},
options: {
skipHtmlTags: ['script', 'noscript', 'style', 'textarea', 'pre'],
},
};
// 刷新预时重新渲染
document.addEventListener('pjax:complete', () => {
MathJax.typesetPromise();
});
</script>
<style>
table {
border-collapse: collapse;
border-spacing: 0;
}
td,th {
padding: 0;
}
.pure-table {
border-collapse: collapse;
border-spacing: 0;
empty-cells: show;
border: 1px solid #cbcbcb;
}
.pure-table caption {
color: #000;
font: italic 85%/1 arial,sans-serif;
padding: 1em 0;
text-align: center;
}
.pure-table td,.pure-table th {
border-left: 1px solid #cbcbcb;
border-width: 0 0 0 1px;
font-size: inherit;
margin: 0;
overflow: visible;
padding: .5em 1em;
}
.pure-table thead {
background-color: #e0e0e0;
color: #000;
text-align: left;
vertical-align: bottom;
}
.pure-table td {
background-color: transparent;
}
.pure-table-bordered td {
border-bottom: 1px solid #cbcbcb;
}
.pure-table-bordered tbody>tr:last-child>td {
border-bottom-width: 0;
}
</style>
<?php $this->need('footer.php'); ?>

View File

@ -38,7 +38,7 @@
</div>
<h2 class="post--single__title"><?php $this->title() ?></h2>
<div class="post--single__content graph" ><?php $this->content(); ?></div>
<!--打赏 -->
<!--打赏 -->
<?php if($this->options->donate): ?>
<div class="post--single__action">
<link rel="stylesheet" href="<?php $this->options->themeUrl('/dist/css/donate.css'); ?>">