phpcms 自动(定时)生产 txt格式的sitemap

 
在根目录新建 setsitemap.php ,代码如下:
 
<?php

define('PHPCMS_PATH', dirname(__FILE__).DIRECTORY_SEPARATOR);
include PHPCMS_PATH.'phpcms/base.php';

pc_base::load_sys_class('form', '', 0);
class gbsitemap
{
function __construct()
{
$fp = fopen(PHPCMS_PATH.'sitemap.txt', 'w');


$this->siteid = 1;
$this->categorys = getcache('category_content_' . $this->siteid, 'commons');

//生成栏目sitemap
foreach($this->categorys as $val){
fwrite($fp, $val['url']."\n");
}

pc_base::load_sys_class('param');
$this->content_db = pc_base::load_model('content_model');


//生成内容sitemap

$modelcache = getcache('model', 'commons');

$new_model = array();
foreach ($modelcache as $modelid => $mod) {
if ($mod['siteid'] == $this->siteid) {
$new_model[$modelid]['modelid'] = $modelid;
$new_model[$modelid]['name'] = $mod['name'];
}
}

//每次处理5000条
foreach ($new_model as $modelid => $m) {

$i = 0;

$this->content_db->set_model($modelid);
$count = $this->content_db->count(array('status' => 99), '*');


for($i=0;$i<$count;$i+=5000){
$result = $this->content_db->select(array('status' => 99), '*', $limit = "$i,5000", $order = 'inputtime
desc');

foreach ($result as $arr) {
if (substr($arr['url'], 0, 1) == '/') {
$url = htmlspecialchars(strip_tags($domain . $arr['url']));
} else {
$url = htmlspecialchars(strip_tags($arr['url']));
}
fwrite($fp, $url."\n");
}


}
}

fclose($fp);
}
}

$maps = new gbsitemap();

?>

自动(定时)执行(使用crontab):
 
每隔30分钟生成一次
#*/30 * * * * PHP安装路径(注意:和web用同一个PHP) 绝对路径/setsitemap.php

0 个评论

要回复文章请先登录注册