<?php
/**
* 生成文本缓存类
*/
class mkcache {
var $db;
var $db_prefix;
function mkcache($dbhandle, $db_prefix)
{
$this->db = $dbhandle;
$this->db_prefix = $db_prefix;
}
/**
* 站点配置缓存
*/
function mc_options()
{
$options_cache = array();
$res = $this->db->query("SELECT * FROM ".$this->db_prefix."options");
while($row = $this->db->fetch_array($res))
{
if(in_array($row['option_name'],array('site_key', 'blogname', 'bloginfo', 'blogurl', 'icp')))
{
$row['option_value'] = htmlspecialchars($row['option_value']);
}
$options_cache[$row['option_name']] = $row['option_value'];
}
$cacheData = serialize($options_cache);
$this->cacheWrite($cacheData,'options');
}
/**
* 用户信息缓存
*/
function mc_user()
{
$user_cache = array();
$query = $this->db->query("SELECT * FROM ".$this->db_prefix."user");
while($row = $this->db->fetch_array($query))
{
$logNum = $this->db->num_rows($this->db->query("SELECT gid FROM ".$this->db_prefix."blog WHERE author={$row['uid']} and hide='n' and type='blog'"));
$draftNum = $this->db->num_rows($this->db->query("SELECT gid FROM ".$this->db_prefix."blog WHERE author={$row['uid']} and hide='y' and type='blog'"));
$commentNum = $this->db->num_rows($this->db->query("SELECT a.cid FROM ".$this->db_prefix."comment as a, ".$this->db_prefix."blog as b where a.gid=b.gid and b.author={$row['uid']}"));
$hidecommentNum = $this->db->num_rows($this->db->query("SELECT a.cid FROM ".$this->db_prefix."comment as a, ".$this->db_prefix."blog as b where a.gid=b.gid and a.hide='y' and b.author={$row['uid']}"));
$tbNum = $this->db->num_rows($this->db->query("SELECT a.tbid FROM ".$this->db_prefix."trackback as a, ".$this->db_prefix."blog as b where a.gid=b.gid and b.author={$row['uid']}"));
$icon = array();
$row['photo'] = !empty($row['photo']) && file_exists(substr($row['photo'], 1)) ? substr($row['photo'], 1) : $row['photo'];
if(!empty($row['photo']) && file_exists($row['photo']))
{
$photosrc = preg_replace("/(\.+\/)(.+)/", '$2', $row['photo']);
$imgsize = chImageSize($row['photo'],ICON_MAX_W,ICON_MAX_H);
$icon['src'] = htmlspecialchars($photosrc);
$icon['width'] = $imgsize['w'];
$icon['height'] = $imgsize['h'];
}
$row['nickname'] = empty($row['nickname']) ? $row['username'] : $row['nickname'];
$user_cache[$row['uid']] = array(
'photo' => $icon,
'name' =>htmlspecialchars($row['nickname']),
'mail' =>htmlspecialchars($row['email']),
'des'=>$row['description'],
'lognum' => $logNum,
'draftnum' => $draftNum,
'commentnum' => $commentNum,
'hidecommentnum' => $hidecommentNum,
'tbnum' => $tbNum
);
}
$cacheData = serialize($user_cache);
$this->cacheWrite($cacheData,'user');
}
/**
* 博客统计缓存
*/
function mc_sta()
{
$lognum = $this->db->num_rows($this->db->query("SELECT gid FROM ".$this->db_prefix."blog WHERE type='blog' and hide='n' "));
$draftnum = $this->db->num_rows($this->db->query("SELECT gid FROM ".$this->db_prefix."blog WHERE type='blog' and hide='y'"));
$comnum = $this->db->num_rows($this->db->query("SELECT cid FROM ".$this->db_prefix."comment WHERE hide='n' "));
$hidecom = $this->db->num_rows($this->db->query("SELECT gid FROM ".$this->db_prefix."comment where hide='y' "));
$tbnum = $this->db->num_rows($this->db->query("SELECT gid FROM ".$this->db_prefix."trackback "));
$twnum = $this->db->num_rows($this->db->query("SELECT id FROM ".$this->db_prefix."twitter "));
$sta_cache = array(
'lognum'=>$lognum,
'draftnum'=>$draftnum,
'comnum'=>$comnum,
'comnum_all'=>$comnum + $hidecom,
'twnum'=>$twnum,
'hidecomnum'=>$hidecom,
'tbnum'=>$tbnum
);
$cacheData = serialize($sta_cache);
$this->cacheWrite($cacheData,'sta');
}
/**
* 最新评论缓存
*/
function mc_comment()
{
$show_config=$this->db->fetch_array($this->db->query("SELECT option_value FROM ".$this->db_prefix."options where option_name='index_comnum'"));
$index_comnum = $show_config['option_value'];
$show_config=$this->db->fetch_array($this->db->query("SELECT option_value FROM ".$this->db_prefix."options where option_name='comment_subnum'"));
$comment_subnum = $show_config['option_value'];
$query=$this->db->query("SELECT cid,gid,comment,date,poster,reply FROM ".$this->db_prefix."comment WHERE hide='n' ORDER BY cid DESC LIMIT 0, $index_comnum ");
$com_cache = array();
while($show_com=$this->db->fetch_array($query))
{
$com_cache[] = array(
'url' => "./?post={$show_com['gid']}#{$show_com['cid']}",
'name' => htmlspecialchars($show_com['poster']),
'content' => htmlClean(subString($show_com['comment'],0,$comment_subnum), false),
'reply' => $show_com['reply']
);
}
$cacheData = serialize($com_cache);
$this->cacheWrite($cacheData,'comments');
}
/**
* 侧边栏标签缓存
*/
function mc_tags()
{
$tag_cache = array();
$query=$this->db->query("SELECT gid FROM ".$this->db_prefix."tag");
$i = 0;
$j = 0;
$tagnum = 0;
$maxuse = 0;
$minuse = 0;
while($row = $this->db->fetch_array($query))
{
$usenum = substr_count($row['gid'], ',') - 1;
if($usenum > $i)
{
$maxuse = $usenum;
$i = $usenum;
}
if($usenum < $j)
{
$minuse = $usenum;
}
$j = $usenum;
$tagnum++;
}
$spread = ($tagnum>12?12:$tagnum);
$rank = $maxuse-$minuse;
$rank = ($rank==0?1:$rank);
$rank = $spread/$rank;
//获取草稿id
$hideGids = array();
$query=$this->db->query("SELECT gid FROM ".$this->db_prefix."blog where hide='y' and type='blog'");
while($row = $this->db->fetch_array($query))
{
$hideGids[] = $row['gid'];
}
$query=$this->db->query("SELECT tagname,gid FROM ".$this->db_prefix."tag");
while($show_tag = $this->db->fetch_array($query))
{
//排除草稿在tag日志数里的统计
foreach ($hideGids as $val)
{
$show_tag['gid'] = str_replace(','.$val.',', ',', $show_tag['gid']);
}
if($show_tag['gid'] == ',')
{
continue;
}
$usenum = substr_count($show_tag['gid'], ',') - 1;
$fontsize = 10 + round(($usenum - $minuse) * $rank);//maxfont:22pt,minfont:10pt
$tag_cache[] = array(
'tagurl' => urlencode($show_tag['tagname']),
'tagname' => htmlspecialchars($show_tag['tagname']),
'fontsize' => $fontsize,
'usenum' => $usenum
);
}
$cacheData = serialize($tag_cache);
$this->cacheWrite($cacheData,'tags');
}
/**
* 侧边栏分类缓存
*/
function mc_sort()
{
$sort_cache = array();
$query = $this->db->query("SELECT sid,sortname,taxis FROM ".$this->db_prefix."sort ORDER BY taxis ASC");
while($row = $this->db->fetch_array($query))
{
$logNum = $this->db->num_rows($this->db->query("SELECT sortid FROM ".$this->db_prefix."blog WHERE sortid=".$row['sid']." and hide='n' and type='blog'"));
$sort_cache[$row['sid']] = array(
'lognum' => $logNum,
'sortname' => htmlspecialchars($row['sortname']),
'sid' => intval($row['sid']),
'taxis' => intval($row['taxis'])
);
}
$cacheData = serialize($sort_cache);
$this->cacheWrite($cacheData,'sort');
}
/**
* 友站缓存
*/
function mc_link()
{
$link_cache = array();
$query=$this->db->query("SELECT siteurl,sitename,description FROM ".$this->db_prefix."link ORDER BY taxis ASC");
while($show_link=$this->db->fetch_array($query))
{
$link_cache[] = array(
'link'=>htmlspecialchars($show_link['sitename']),
'url'=>htmlspecialchars($show_link['siteurl']),
'des'=>htmlspecialchars($show_link['description'])
);
}
$cacheData = serialize($link_cache);
$this->cacheWrite($cacheData,'links');
}
/**
* twitter缓存
*/
function mc_twitter()
{
$show_config=$this->db->fetch_array($this->db->query("SELECT option_value FROM ".$this->db_prefix."options