代码 1 0

    Typecho 获取站点统计信息(带缓存)

    在主题中最后插入

    // 获取站点统计信息(带缓存)
    function getSiteStatsWithCache() {
        // 使用文件缓存(兼容性最好)
        $cacheFile = __TYPECHO_ROOT_DIR__ . '/usr/cache/site_stats.cache';
        $cacheTime = 3600; // 1小时缓存
    
        if (file_exists($cacheFile)) {
            $cacheData = json_decode(file_get_contents($cacheFile), true);
            if (time() - $cacheData['cache_time'] < $cacheTime) {
                return $cacheData;
            }
        }
        
        $db = Typecho_Db::get();
        
        // 1. 正确的总分类数
        $stats['totalCategories'] = $db->fetchObject($db->select('COUNT(*) AS cnt')
            ->from('table.metas')
            ->where('type = ?', 'category'))->cnt;
        
        // 2. 正确的总标签数
        $stats['totalTags'] = $db->fetchObject($db->select('COUNT(*) AS cnt')
            ->from('table.metas')
            ->where('type = ?', 'tag'))->cnt;
        
        // 3. 总文章数
        $stats['totalPosts'] = $db->fetchObject($db->select('COUNT(*) AS cnt')
            ->from('table.contents')
            ->where('type = ?', 'post')
            ->where('status = ?', 'publish'))->cnt;
        
        // 4. 总文章字数
        $stats['totalWords'] = $db->fetchObject($db->select('SUM(LENGTH(text)) AS total')
            ->from('table.contents')
            ->where('type = ?', 'post')
            ->where('status = ?', 'publish'))->total;
        
        // 5. 建站时间
        $oldestPost = $db->fetchObject($db->select('MIN(created) AS created')
            ->from('table.contents')
            ->where('type = ?', 'post')
            ->where('status = ?', 'publish'));
        $stats['siteCreationDate'] = date('Y-m-d', $oldestPost->created);
        $stats['siteDays'] = ceil((time() - $oldestPost->created) / 86400);
        
        // 6. 友情链接数量
        $stats['totalLinks'] = 0;
        if (class_exists('Links_Plugin')) {
            $stats['totalLinks'] = $db->fetchObject($db->select('COUNT(*) AS cnt')
                ->from('table.links'))->cnt;
        }
        
        // 7. 总留言数量
        $stats['totalComments'] = $db->fetchObject($db->select('COUNT(*) AS cnt')
            ->from('table.comments')
            ->where('status != ?', 'spam'))->cnt;
        
        // 保存缓存
        $stats['cache_time'] = time();
        if (!is_dir(dirname($cacheFile))) {
            mkdir(dirname($cacheFile), 0755, true);
        }
        file_put_contents($cacheFile, json_encode($stats));
        
        return $stats;
    }

    在前端调用

    <?php $stats = getSiteStatsWithCache(); ?>
    
    <span>文章总数</span><small><?php echo $stats['totalPosts']; ?></small>
    
    <span>撰写字数</span><small><?php echo number_format($stats['totalWords']); ?>字</small>
    
    <span>友情链接数量</span><small><?php echo $stats['totalLinks']; ?></small>
    
    <span>留言总数</span><small><?php echo $stats['totalComments']; ?></small>
    
    <span>标签数量</span><small><?php echo $stats['totalTags']; ?></small>
    
    <span>分类数量</span><small><?php echo $stats['totalCategories']; ?></small>
    
    <span>运营时间至今有</span><small><?php echo $stats['siteDays']; ?>天</small>