在小編的博客中有一個 博客統(tǒng)計 頁面,這個頁面可以顯示網(wǎng)站的一些基本數(shù)據(jù),小編開發(fā)的 Typecho 主題中也包含統(tǒng)計頁模板,這里就來簡單寫一下獲取各種統(tǒng)計信息的方法。
這里主要是網(wǎng)站的統(tǒng)計信息,不包含瀏覽用戶的統(tǒng)計信息,如果需要瀏覽用戶的統(tǒng)計信息可以使用百度統(tǒng)計或谷歌分析。
Typecho 官方是沒有提供獲取統(tǒng)計數(shù)據(jù)的 API 的,只能手動查詢數(shù)據(jù)庫獲取統(tǒng)計信息,Typecho 也提供了一些操作數(shù)據(jù)庫的方法。
下面的代碼都是編寫成函數(shù),放在主題的 functions.php 文件中,在每個頁面都能調(diào)用。
獲取文章數(shù)量
下面是獲取文章總數(shù)量的函數(shù),不包含獨立頁面:
function postCount() {
$db = Typecho_Db::get();
// 查詢出文章數(shù)量并轉(zhuǎn)換為數(shù)組
$count = $db->fetchRow($db->select('COUNT(*)')->from('table.contents')->where('type = ?', 'post')->where('status = ?', 'publish'));
// 返回文章數(shù)量
return $count['COUNT(*)'];
}
上面的函數(shù)可以獲取文章數(shù)量,不包含草稿和未公開的文章。
Typecho 的文章和獨立頁面都存儲在 Typecho 數(shù)據(jù)庫的 typecho_contents 表中,使用 Typecho 提供的 API 查詢時可以不需要加 typecho 前綴。
typecho_contents 表中有個 type 字段是用來存儲內(nèi)容類型的,post 是普通文章,page 是獨立頁面。status 字段是存儲文章狀態(tài),publish 是公開的,draft 是草稿。
如果要獲取獨立頁面數(shù)量可以把 where('type = ?', 'post') 換為 where('type = ?', 'page') ,如果要獲取所有公開、未公開、和草稿文章數(shù)量可以直接把 where('status = ?', 'publish') 方法刪除。
獲取評論數(shù)量
下面是獲取評論總數(shù)量的函數(shù),只包含通過審核的評論:
function commentsCount() {
$db = Typecho_Db::get();
// 查詢出評論數(shù)量并轉(zhuǎn)換為數(shù)組
$count = $db->fetchRow($db->select('COUNT(*)')->from('table.comments')->where('status = ?', 'approved'));
// 返回評論數(shù)量
return $count['COUNT(*)'];
}
Typecho 的評論存儲在 typecho_comments 表中,status 字段存儲評論的狀態(tài),approved 是通過審核的評論,waiting 是待審核的評論,spam 是標記為垃圾的評論。
如果要獲取所有評論,不區(qū)分狀態(tài)的話可以把 where('status = ?', 'approved') 方法刪除。
獲取分類數(shù)量
下面是獲取分類數(shù)量的函數(shù):
function categoryCount() {
$db = Typecho_Db::get();
// 查詢出分類數(shù)量并轉(zhuǎn)換為數(shù)組
$count = $db->fetchRow($db->select('COUNT(*)')->from('table.metas')->where('type = ?', 'category'));
// 返回分類數(shù)量
return $count['COUNT(*)'];
}
Typecho 的分類和標簽都存儲在 typecho_metas 表中,這個表中有一個 type 字段用來區(qū)分標簽和分類,tag 是標簽,category 是分類。
獲取標簽數(shù)量
下面是獲取標簽數(shù)量的函數(shù)
function tagCount() {
$db = Typecho_Db::get();
$count = $db->fetchRow($db->select('COUNT(*)')->from('table.metas')->where('type = ?', 'tag'));
return $count['COUNT(*)'];
}
Typecho 的分類和標簽都存儲在一個表中,只需要把 where('type = ?', 'category') 換為 where('type = ?', 'tag') 就可以查詢出標簽數(shù)量。
獲取總點贊數(shù)量
Typecho 是不包含點贊功能的,如果你開發(fā)了點贊功能,在文章表中加入了 agree 字段的話,可以使用下面的函數(shù)獲取點贊總數(shù):
function agreeCount() {
$db = Typecho_Db::get();
// 對文章表的 agree 字段進行求和并把查詢出的結(jié)果轉(zhuǎn)換為數(shù)組
$count = $db->fetchRow($db->select('SUM(agree) AS agreeCount')->from('table.contents'));
// 返回總點贊數(shù)
return $count['agreeCount'];
}
獲取評論數(shù)量排名前 5 的 5 篇文章
下面是獲取評論數(shù)排名前 5 的 5 篇文章的函數(shù),除了文章外也會獲取評論數(shù):
function top5CommentPost() {
$db = Typecho_Db::get();
// 查詢出 5 條評論數(shù)最大的文章,按照評論數(shù)從大到小排序,然后把查詢出的數(shù)據(jù)轉(zhuǎn)換為數(shù)組
$top5Post = $db->fetchAll($db->select()->from('table.contents')->where('type = ?', 'post')->where('status = ?', 'publish')->order('commentsNum', Typecho_Db::SORT_DESC)->offset(0)->limit(5));
// 用來存儲文章信息的數(shù)組
$postList = array();
foreach ($top5Post as $post) {
// 使用 Typecho 提供的 API 獲取每篇文章的鏈接地址
$post = Typecho_Widget::widget('Widget_Abstract_Contents')->filter($post);
// 把文章標題、鏈接、點贊數(shù)加入數(shù)組
array_push($postList, array(
'title' => $post['title'],
'link' => $post['permalink'],
'commentsNum' => $post['commentsNum']
));
}
// 返回 5 篇文章的標題、鏈接、點贊數(shù)
return $postList;
}
在 typecho_contents 文章表中有一個 commentsNum 字段就是用來存儲評論數(shù)量的。
上面的 top5CommentPost 函數(shù)返回的數(shù)據(jù)如下:
Array
(
[0] => Array
(
[title] => MWordStar 響應式雙欄 Typecho 博客主題
[link] => http://localhost/php/typecho/index.php/archives/812/
[commentsNum] => 159
)
[1] => Array
(
[title] => MWordStar 使用說明
[link] => http://localhost/php/typecho/index.php/archives/819/
[commentsNum] => 127
)
[2] => Array
(
[title] => 紅米 AC2100 刷入 Padavan 固件
[link] => http://localhost/php/typecho/index.php/archives/883/
[commentsNum] => 38
)
[3] => Array
(
[title] => Facile 一個簡潔的 Typecho 博客主題
[link] => http://localhost/php/typecho/index.php/archives/899/
[commentsNum] => 27
)
[4] => Array
(
[title] => Linux 搭建 Minecraft 基巖版服務器
[link] => http://localhost/php/typecho/index.php/archives/865/
[commentsNum] => 26
)
)
獲取各分類的文章數(shù)量
下面是獲取分類文章數(shù)量的函數(shù),可以用來生成統(tǒng)計圖表:
function categoryPostCount() {
$db = Typecho_Db::get();
// 查詢出每個分類的文章數(shù)量并轉(zhuǎn)換為數(shù)組
$count = $db->fetchAll($db->select('name', 'count')->from('table.metas')->where('type = ?', 'category'));
// 如果沒有查詢到數(shù)據(jù)就返回空數(shù)組
if (count($count) < 1) {
return array();
}
// 返回每個分類的文章數(shù)量
return $count;
}
分類文章數(shù)量也是存儲在 typecho_metas 表中,count 字段就是分類或標簽的文章數(shù)量。
下面是獲取的數(shù)據(jù):
Array
(
[0] => Array
(
[name] => PHP
[count] => 14
)
[1] => Array
(
[name] => 前端開發(fā)
[count] => 69
)
[2] => Array
(
[name] => 數(shù)據(jù)庫
[count] => 6
)
[3] => Array
(
[name] => Linux相關(guān)
[count] => 23
)
[4] => Array
(
[name] => 實用教程
[count] => 28
)
)
在小編的博客中還有閱讀量和圖表展示,但是 Typecho 是不包含統(tǒng)計閱讀量的功能的,小編也還沒有寫過開發(fā)閱讀統(tǒng)計的文章,小編的方法也不一定適用于其他網(wǎng)站的教程,所以這里不包含閱讀量統(tǒng)計的函數(shù)。
圖表展示的話,不同的圖表對數(shù)據(jù)格式的要求也不一樣,小編使用的是百度的 ECharts,完整的 ECharts 比較大,只能按需引入打包,內(nèi)容很多,寫在這里不方便閱讀,后面可能會考慮再寫一篇。





















