猛击下载:olcms的全站静态生成html类http://www.olcms.com/uploads/file/20140103/20140103101134_12209.rar
<?php
/*
静态化
www.olcms.com
*/
class HtmlAction extends CommonAction{
function index(){
echo "<a href="./Admin/Article."/htmlForAll>一键生成全站</a><br/>";
echo "<a href="./Admin/Article."/htmlForIndex>生成网站首页</a><br/>";
echo "<a href="./Admin/Article."/htmlForList>生成所有列表</a><br/>";
echo "<a href="./Admin/Article."/htmlForArt>生成所有文章</a><br/>";
}
//一键更新网站
function htmlForAll(){
cookie('htmlForAll',1);
$this->htmlForArt();
}
//文章静态化
function htmlForArt(){
//如果id为空,就获取第一个id
if(empty($_GET['id'])){
$tmp = M('article')->field('id')->order('id asc')->limit('1')->select();
//dump($tmp);
$_GET['id'] = $tmp[0]['id'];
}
//获取文章ID并读取文章
$info = M("article")->where('id='.$_GET['id'])->select();
//获取文章评论内容
$comment = D('comment')->field("adduser,content,addtime")->where('cid='.$_GET['id'].' and isable=1')->order(' addtime desc')->select();
//判断文章
if(empty($info)){
$this->success('文章已经全部生成静态。',3,'/list/index');
}else{
foreach($info as $v){
$articleInfo = array('ti
}
//文章点击次数
//$_POST['clicknum'] = $info[0]['clicknum']+1;
//D('article')->where('id='.$_GET['id'])->update();
//上一篇and下一篇
$pre = M('article')->field('id,ti
$next = M('article')->field('id,ti
$this->assign('pre',$pre);
$this->assign('next',$next);
$this->assign('articleInfo',$articleInfo);
$this->assign('commentInfo',$commentInfo);
//$this->display();
//将html代码缓存到变量方便生成静态
ob_start();
$this->display('Article:index');
$_cache = ob_get_contents();
ob_end_clean();
//echo $_cache;
echo '正在生成文章ing...<br/>';
file_put_contents('html/'.$_GET['id'].'.html',$_cache);
//跳转来继续生成
if($_GET['id']){
$urlList = M('article')->where(' id>'.$_GET['id'])->order(' id asc')->limit('1')->select();
//P($urlList);
//判断是否结束
if(empty($urlList)){
echo "<br/>文章已经全部生成静态!<br />";
//跳转列表页
if(cookie('htmlForAll')){
$this->htmlForList();
}
exit();
}else{
$this->redirect("/Html/htmlForArt/id/".$urlList[0]['id'],'',0.1);
}
}
}
}
//列表页静态化
function htmlForList(){
//如果id为空,就获取第一个id
if(empty($_GET['id'])){
$tmp = M('type')->order(' id asc')->limit('1')->select();
$_GET['id'] = $tmp[0]['id'];
}
//获取类型
$type = $_GET['id'];
$where = 'typeid='.$type.' and isable=1';
$db = M("article")->where($where);
//导入分页类
import('ORG.Util.Page');
$count = $db->where($where)->count();
//获取静态化列表名
$typeHtmlName = M('type')->field('htmlname')->where('id='.$_GET['id'])->select();
$typeHtmlName = $typeHtmlName[0]['htmlname'];
//创建分页对象
$p = new Page($count,10,$typeHtmlName);
$show = $p->show();
//P($page);
$info = D("article")->where($where)->limit($p->firstRow.','.$p->listRows)->order('id desc ')->select();
//dump($artInfoList);
$this->assign('artInfoList',$info);
$this->assign("pageinfo",$show);
//频道名
$typeName = D('type')->where("id=".$type)->select();
/* if($typeName[0]['pid'] == 0){
$typeName = D('article')->select();
$this->assign('typeName',$typeName);
}else{
$this->assign('typeName',$typeName[0]['name']);
} */
$this->assign('typeName',$typeName[0]['name']);
//$this->display();
//将html代码缓存到变量方便生成静态
ob_start();
$this->display('Type:index');
$_cache = ob_get_contents();
ob_end_clean();
//echo $_cache;
if($p->nowPage == 0){
file_put_contents('html/'.$typeHtmlName.'_1.html',$_cache);
}else{
file_put_contents('html/'.$typeHtmlName.'_'.$p->nowPage.'.html',$_cache);
}
//跳转来继续生成
//echo $page->page.$page->pageNum;
//列表页的所有分页都需要生成
if($p->nowPage < $p->totalPages){
//$urlList = D('article')->where(' id>'.$_GET['id'])->order(' id asc')->limit('1')->select();
//P($urlList);
//echo '应该跳转';
$i = $p->nowPage + 1;
//exit();
$this->redirect("/Html/htmlForList/id/".$_GET['id'].'/p/'.$i,'',0.1);
}else{
//跳转到下一个类别
$urlList = D('type')->where(' id>'.$_GET['id'])->order(' id asc')->limit('1')->select();
//P($urlList);
//判断是否结束
if(empty($urlList)){
echo "列表页已经全部生成静态!<br /><br/>";
if(cookie('htmlForAll')){
cookie('htmlForAll',null);
$this->htmlForIndex();
}
exit();
}else{
//exit();
$this->redirect("/Html/htmlForList/id/".$urlList[0]['id'],'',0.1);
}
}
exit();
}
//生成首页
function htmlForIndex(){
//默认每个栏目读取个数
$artNum = 8;
//加载类别菜单
$type = M('type')->field('id,name')->where('isable=1')->order('id')->select();
//dump($type);
//获取栏目总数
$totalType = count($type);
//循环读取每个栏目的7条数据
foreach($type as $v){
$art[$v['id']] = D('IndexView')->where('typeid='.$v['id'])->order('id desc')->limit($artNum)->select();
}
//dump($art);
//友情链接
$li
//dump($li
$this->assign('li
$this->assign('totalType',$totalType);
$this->assign('art',$art);
//$this->display();
ob_start();
$this->display('Index:index');
$_cache = ob_get_contents();
ob_end_clean();
//echo $_cache;
file_put_contents('index.html',$_cache);
echo "首页生成完成!<a href=".."/index.html target=new>点此查看</a><br/><br/><br/>注意:<br/>1.请在系统设置里面开启全站静态化<br/>2.请设置index.html优先级高于index.php";
exit();
}
}
本文地址:http://www.olcms.com/html/109.html
最佳答案