天天看点

phpcms 定时生成 sitemap 包括 百度 xml文件

很简单的修改了 原phpcms自带的 生成xml的程序文件 ,

原来的生成程序文件是 /phpcms/modules/admin/googlesitemap.php

修改这个文件放到根目录下命名为sitemap.php,代码如下:

<?php
if(PHP_SAPI != 'cli')
{
    header('location: sitemap.xml');
    exit;
} 
define('PHPCMS_PATH', dirname(__FILE__).DIRECTORY_SEPARATOR);
include PHPCMS_PATH.'phpcms/base.php';

pc_base::load_sys_class('form', '', 0);

class gbsitemap {
    function __construct() {
        $this->header = "<\x3Fxml version=\"1.0\" encoding=\"UTF-8\"\x3F>\n\t<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">";
        $this->charset = "UTF-8";
        $this->footer = "\t</urlset>\n";
        $this->baidunews_footer = "</document>";
        $this->items = array();
        $this->baidunew_items = array();
        //生成栏目级别选项
        $this->siteid = 1;
        $this->categorys = getcache('category_content_'.$this->siteid,'commons');
    }

    function add_item2($new_item) {
        $this->items[] = $new_item;
    }

    function build( $file_name = null ) {
        $map = $this->header . "\n";
        foreach ($this->items AS $item){
            $map .= "\t\t<url>\n\t\t\t<loc>$item[loc]</loc>\n";
            $map .= "\t\t\t<lastmod>$item[lastmod]</lastmod>\n";
            $map .= "\t\t\t<changefreq>$item[changefreq]</changefreq>\n";
            $map .= "\t\t\t<priority>$item[priority]</priority>\n";
            $map .= "\t\t</url>\n\n";
        }
        $map .= $this->footer . "\n";
        if (!is_null($file_name)){
            return file_put_contents($file_name, $map);
        } else {
            return $map;
        }
    }

    function google_sitemap_item($loc, $lastmod = '', $changefreq = '', $priority = '') {
        $data = array();
        $data['loc'] =  $loc;
        $data['lastmod'] =  $lastmod;
        $data['changefreq'] =  $changefreq;
        $data['priority'] =  $priority;
        return $data;
    } 
    /**
     * 
     * 百度新闻数组 组成
     * @param $title
     * @param $link
     * @param $description
     * @param $text
     * @param $image
     * @param $keywords
     * @param $category
     * @param $author
     * @param $source
     * @param $pubDate
     */
    function baidunews_item($title, $link = '', $description = '',$text = '',$image = '', $keywords = '',$category = '',$author = '',$source='',$pubDate='') {
        $data = array();
        $data['title'] =  $title;
        $data['link'] =  $link;
        $data['description'] =  $description;
        $data['text'] =  $text;
        $data['image'] =  $image;
        $data['keywords'] =  $keywords;
        $data['category'] =  $category;
        $data['author'] =  $author;
        $data['source'] =  $source;
        $data['pubDate'] =  $pubDate;
        return $data;
    }

    function add_baidunews_item($new_item){
        $this->baidunew_items[] = $new_item;
    }

    function baidunews_build( $file_name = null ,$this_domain,$email,$time) {
        //百度头部
        $this->baidunews = '';
        $this->baidunews = "<?xml version=\"1.0\" encoding=\"".CHARSET."\" ?>\n";
        $this->baidunews .= "<document>\n";
        $this->baidunews .= "<webSite>".$this_domain."</webSite>\n";
        $this->baidunews .= "<webMaster>".$email."</webMaster>\n";
        $this->baidunews .= "<updatePeri>".$time."</updatePeri>\n";
        foreach ($this->baidunew_items AS $item){ 
            $this->baidunews .= "<item>\n";
            $this->baidunews .= "<title>".$item['title']."</title>\n";
            $this->baidunews .= "<link>".$item['link']."</link>\n";
            $this->baidunews .= "<description>".$item['description'] ."</description>\n";
            $this->baidunews .= "<text>".$item['text']."</text>\n";
            $this->baidunews .= "<image>".$item['image']."</image>\n";
            $this->baidunews .= "<keywords>".$item['keywords']."</keywords>\n";
            $this->baidunews .= "<category>".$item['category']."</category>\n";
            $this->baidunews .= "<author>".$item['author']."</author>\n";
            $this->baidunews .= "<source>".$item['source']."</source>\n";
            $this->baidunews .= "<pubDate>".$item['pubDate']."</pubDate>\n";
            $this->baidunews .= "</item>\n";
        } 
        $this->baidunews .= $this->baidunews_footer . "\n";
        if (!is_null($file_name)){
            return file_put_contents($file_name, $this->baidunews);
        } else {
            return $this->baidunews;
        }
    }

    /**
     * 
     * Enter 生成google sitemap, 百度新闻协议
     */
    function set ($request) {
        $hits_db = pc_base::load_model('hits_model');
        $dosubmit = isset($request['dosubmit']) ? $request['dosubmit'] : $_GET['dosubmit'];

        //读站点缓存
        $siteid = $this->siteid;
        $sitecache = getcache('sitelist','commons');
        //根据当前站点,取得文件存放路径
        $systemconfig = pc_base::load_config('system');
        $html_root = substr($systemconfig['html_root'], 1);
        //判断当前站点目录,是PHPCMS则把文件写到根目录下, 不是则写到分站目录下.(分站目录用由静态文件路经html_root和分站目录dirname组成)
        if($siteid==1){
            $dir = PHPCMS_PATH;
        }else {
            $dir = PHPCMS_PATH.$html_root.DIRECTORY_SEPARATOR.$sitecache[$siteid]['dirname'].DIRECTORY_SEPARATOR;
        }
        //模型缓存
        $modelcache = getcache('model','commons');

        //获取当前站点域名,下面生成URL时会用到.
        $this_domain = substr($sitecache[$siteid]['domain'], 0,strlen($sitecache[$siteid]['domain'])-1);
        //生成百度新闻
        if($request['mark']) {
            $baidunum = $request['baidunum'] ? intval($request['baidunum']) : 20;
            if($request['catids']=="")showmessage(L('choose_category'), HTTP_REFERER);
            $catids = $request['catids'];
            $catid_cache = $this->categorys;//栏目缓存
            $this->content_db = pc_base::load_model('content_model');
            foreach ($catids as $catid) {
                $modelid = $catid_cache[$catid]['modelid'];//根据栏目ID查出modelid 进而确定表名,并结合栏目ID:catid 检索出对应栏目下的新闻条数
                $this->content_db->set_model($modelid);
                $result = $this->content_db->select(array('catid'=>$catid,'status'=>99), '*', $limit = "0,$baidunum", 'id desc');
                //重设表前缀,for循环时用来查,文章正文 
                $this->content_db->table_name = $this->content_db->table_name.'_data';
                foreach ($result as $arr){
                    //把每一条数据都装入数组中
                    extract($arr);
                    if(!preg_match('/^(http|https):\/\//', $url)){
                        $url = $this_domain.$url;
                    }
                    if($thumb != ""){
                        if(!preg_match('/^(http|https):\/\//', $thumb)){
                            $thumb = $this_domain.$thumb;
                        }
                    }
                    //取当前新闻模型 附属表 取 新闻正文
                    $url = htmlspecialchars($url);
                    $description = htmlspecialchars(strip_tags($description));
                    //根据本条ID,从对应tablename_data取出正文内容
                    $content_arr = $this->content_db->get_one(array('id'=>$id),'content');
                    $content = htmlspecialchars(strip_tags($content_arr['content']));
                    //组合数据
                    $smi = $this->baidunews_item($title,$url,$description,$content,$thumb, $keywords,$category,$author,$source,date('Y-m-d', $inputtime));//推荐文件
                    $this->add_baidunews_item($smi);
                } 
            }
            $baidunews_file = $dir.'baidunews.xml';

            @mkdir($dir,0777,true);
            $this->baidunews_build($baidunews_file,$this_domain,$request['email'],$request['time']); 
        }

        //生成网站地图
        $content_priority = $request['content_priority'];
        $content_changefreq = $request['content_changefreq']; 
        $num = $request['num'] ? intval($request['num']) : 100;

        $today = date('Y-m-d');
        $domain = $this_domain;
        //生成地图头部 -第一条
        $smi = $this->google_sitemap_item($domain, $today, 'daily', '1.0');
        $this->add_item2($smi);

        $this->content_db = pc_base::load_model('content_model');
        //只提取该站点的模型.再循环取数据,生成站点地图.
        $modelcache = getcache('model','commons');
        $new_model = array();
        foreach ($modelcache as $modelid => $mod){
            if($mod['siteid']==$siteid){
                $new_model[$modelid]['modelid'] = $modelid;
                $new_model[$modelid]['name'] = $mod['name'];						
            }
        }
        foreach($new_model as $modelid=>$m) {//每个模块取出num条数据 
            $this->content_db->set_model($modelid);// 或者 $this->conetnt_db->set_model($modelid);
            $result = $this->content_db->select(array('status'=>99), '*', $limit = "0,$num", $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']));
                }
                $hit_r = $hits_db->get_one(array('hitsid'=>'c-'.$modelid.'-'.$arr['id']));
                if($hit_r['views']>1000) $content_priority = 0.9;
                $smi    = $this->google_sitemap_item($url, $today, $content_changefreq, $content_priority);//推荐文件
                $this->add_item2($smi);
            }
        }

        $sm_file = $dir.'sitemaps.xml';
        if($this->build($sm_file)){
            showmessage(L('create_success'), HTTP_REFERER);
        } 
    }
} 
//这个地方自己设定,catids是个数组,
$request = array(
    'tabletype'=>'phpcmstables',
    'content_priority'=>'0.7',
    'content_changefreq'=>'weekly',//daily,monthly……
    'num'=>'20',
    'mark'=>'1',
    'catids'=>range(6,17),
    'time'=>'40',
    'email'=>'[email protected]',
    'baidunum'=>'20',
);
//当然你可以通过外部传递参数,修改$request的值
$maps = new gbsitemap();
$maps->set($request);

?>
           

shell中

crontab -e

*/5 * * * *