`
tdwm
  • 浏览: 49498 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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'=>'phpcms@phpcms.cn',
    'baidunum'=>'20',
);
//当然你可以通过外部传递参数,修改$request的值
$maps = new gbsitemap();
$maps->set($request);

?>

 

 

shell中

crontab -e

 

*/5 * * * * 

分享到:
评论

相关推荐

    PHPCMS代码生成器

    PHPCMS代码生成器,直接生成调用代码,很方便。

    phpcms模板生成器

    phpcms模板生成器 phpcms模板生成器

    phpcms2008 生成HTML

    根据phpcms2008sp4_gbk_111018 这个版本进行二次开发 实现把最近N天有评论的信息生成HTML文件。

    PHPCMS V9 定时发布文章的方法

    PHPCMS V9 定时发布文章的方法,PHPCMS V9 定时发布文章的方法

    PhpCMS V9代码生成器 v1.5 Final 绿色版.rar

    PhpCMS V9代码生成器可以帮助PhpCMS V9模板制作者快速高效地制作模板,功能代码速查,代码自动生成,方便调用,让您在制作PhpCMS V9模板时得心应手,PhpCMS V9爱好者必备! PhpCMS V9模板代码生成器 V1.5 版本更新...

    phpcms 定时任务模块

    好不好用我就不多说了。不用win的任务那样虚拟主机不能用。不用客户触发。那样没人访问还是触发不了

    PHPCMS定时自动采集

    此压缩包用于实现PHPCMS系统内容自动采集 使用前请替换包内所有文件,对文件有修改的请慎重操作。 一、文件修改 除采集模块本身进行修改外,还修改了以下两个文件: phpcms/libs/functions/global.func.php --&gt; ...

    phpcms 代码生成器

    | PhpCMS V9模板代码生成器 V1.3 版本更新说明 | --------------------------------------------- [更新] [√]新增GET标签调用代码范例; [√]新增内容页调用栏目名及链接代码; [√]新增小工具中编码转换接口...

    PhpCMS V9代码生成器

    phpcms V9模板代码生成器是一款非常专业的phpcms V9模板代码快速生成软件。这款phpcms v9代码生成器功能强大,内置多种常用模板标签语言以及常用内容调用方法

    PHPCMS_V9百度推送插件

    PHPCMS_V9百度推送插件,发布文章的时候会自动推送到百度站长平台。

    PhpCMS V9代码生成器 V1.5 Final版本PHPCMS中国发布

    PHPCMS V9代码生成器 V1.5版本PHPCMS中国发布 ------------------------------ | PhpCMS V9代码生成器 使用说明 | ------------------------------ 一、平台需求 运行环境:Windows 2000/XP/Vista/Win7 二、...

    PhpCMS V9模板代码生成器

    PhpCMS V9模板代码生成器

    PhpCMS V9模板代码生成器 V1.5

    PhpCMS V9代码生成器,可以方便获取代码

    PhpCMSV9代码生成器

    软件名称:PhpCMS V9代码生成器工具简介:phpcms V9模板代码生成器是一款非常专业的phpcms V9模板代码快速生成软件。这款phpcms v9代码生成器功能强大,内置多种常用模板标签语言以及常用内容调用方法,让你轻松修改...

    PHPCMS V9代码生成器 2.0

    PHPCMS V9代码生成器 2.0

    Phpcms V9 自定义生成html名称

    PHPCMS V9网址生成优化:内容页HTML文件名称自定义教程 在几天前的一片教程中,CMSYOU与大家分享了团队以往的经验:PHPCMS V9静态化HTML生成设置及URL规则优化(具体网址:http://www.cmsyou.com/support/85.html...

    PHPCMS V9 代码生成器

    使用这个工具,编写phpcms的模板,就会非常方便了。自带所有标签 能按需生成相关代码 超级好用

    phpcms v9二次开发文档资料汇总

    模块【标签】参考手册.(第一版),PHPCMS V9 产品开发权威指南,phpcms_v9_代码分析(一、二、三),phpcms_v9_二次开发及标签制作讲义,PHPCMS_V9安装教程,PHPCMS_V9帮助中心,PHPCMS_V9开发文档,PHPCMS_V9模板制作...

    phpcms代码生成器

    PhpCMS V9代码生成器可以帮助PhpCMS V9模板制作者快速高效地制作模板 功能代码速查 代码自动生成 方便调用 让您在制作PhpCMS V9模板时得心应手 PhpCMS V9爱好者必备

Global site tag (gtag.js) - Google Analytics