PHP生成sitemap.xml地图函数


Posted in PHP onNovember 13, 2013
<?php/**
 *    网站地图更新控制器
 *
 *    @author    Garbin
 *    @usage    none
 */
class SitemapApp extends FrontendApp
{
    function __construct()
    {
        $this->SitemapApp();
    }
    function SitemapApp()
    {
        parent::__construct();
        $this->_google_sitemmap_file = ROOT_PATH . '/data/google_sitemmap.xml';
    }
    function index()
    {
        if (!Conf::get('sitemap_enabled'))
        {
            return;
        }
        $from = empty($_GET['from']) ? 'google' : trim($_GET['from']);
        switch ($from)
        {
            case 'google':
                $this->_output_google_sitemap();
            break;
        }
    }
    /**
     *    输出Google sitemap
     *
     *    @author    Garbin
     *    @return    void
     */
    function _output_google_sitemap()
    {
        header("Content-type: application/xml");
        echo $this->_get_google_sitemap();
    }
    /**
     *    获取Google sitemap
     *
     *    @author    Garbin
     *    @return    string
     */
    function _get_google_sitemap()
    {
        $sitemap = "";
        if ($this->_google_sitemap_expired())
        {
            /* 已过期,重新生成 */
            /* 获取有更新的项目 */
            $updated_items = $this->_get_updated_items($this->_get_google_sitemap_lastupdate());
            /* 重建sitemap */
            $sitemap = $this->_build_google_sitemap($updated_items);
            /* 写入文件 */
            $this->_write_google_sitemap($sitemap);
        }
        else
        {
            /* 直接返回旧的sitemap */
            $sitemap = file_get_contents($this->_google_sitemmap_file);
        }
        return $sitemap;
    }
    /**
     *    判断Google sitemap是否过期
     *
     *    @author    Garbin
     *    @return    boolean
     */
    function _google_sitemap_expired()
    {
        if (!is_file($this->_google_sitemmap_file))
        {
            return true;
        }
        $frequency = Conf::get('sitemap_frequency') * 3600;
        $filemtime = $this->_get_google_sitemap_lastupdate();
        return (time() >= $filemtime + $frequency);
    }
    /**
     *    获取上次更新日期
     *
     *    @author    Garbin
     *    @return    int
     */
    function _get_google_sitemap_lastupdate()
    {
        return is_file($this->_google_sitemmap_file) ? filemtime($this->_google_sitemmap_file) : 0;
    }
    /**
     *    获取已更新的项目
     *
     *    @author    Garbin
     *    @return    array
     */
    function _get_updated_items($timeline = 0)
    {
        $timeline && $timeline -= date('Z');
        $limit = 5000;
        $result = array();
        /* 更新的店铺 */
        $model_store =& m('store');
        $updated_store = $model_store->find(array(
            'fields'    => 'store_id, add_time',
            'conditions' => "add_time >= {$timeline} AND state=" . STORE_OPEN,
            'limit'     => "0, {$limit}",
        ));
        if (!empty($updated_store))
        {
            foreach ($updated_store as $_store_id => $_v)
            {
                $result[] = array(
                    'url'       => SITE_URL . '/index.php?app=store&id=' . $_store_id,
                    'lastmod'   => date("Y-m-d", $_v['add_time']),
                    'changefreq'=> 'daily',
                    'priority'  => '1',
                );
            }
        }
        /* 更新的文章 */
        $model_article =& m('article');
        $updated_article = $model_article->find(array(
            'fields'    => 'article_id, add_time',
            'conditions'=> "add_time >= {$timeline} AND if_show=1",
            'limit'     => "0, {$limit}",
        ));
        if (!empty($updated_article))
        {
            foreach ($updated_article as $_article_id => $_v)
            {
                $result[] = array(
                    'url'       => SITE_URL . '/index.php?app=article&act=view&article_id=' . $_article_id,
                    'lastmod'   => date("Y-m-d", $_v['add_time']),
                    'changefreq'=> 'daily',
                    'priority'  => '0.8',
                );
            }
        }
        /* 更新的商品 */
        $model_goods =& m('goods');
        $updated_goods = $model_goods->find(array(
            'fields'        => 'goods_id, last_update',
            'conditions'    => "last_update >= {$timeline} AND if_show=1 AND closed=0",
            'limit'         => "0, {$limit}",
        ));
        if (!empty($updated_goods))
        {
            foreach ($updated_goods as $_goods_id => $_v)
            {
                $result[] = array(
                    'url'       => SITE_URL . '/index.php?app=goods&id=' . $_goods_id,
                    'lastmod'   => date("Y-m-d", $_v['last_update']),
                    'changefreq'=> 'daily',
                    'priority'  => '0.8',
                );
            }
        }
        return $result;
    }
    /**
     *    生成Google sitemap
     *
     *    @author    Garbin
     *    @param     array $items
     *    @return    string
     */
    function _build_google_sitemap($items)
    {
        $sitemap = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\r\n";
        $sitemap .= "    <url>\r\n        <loc>" . htmlentities(SITE_URL, ENT_QUOTES) . "</loc>\r\n        <lastmod>" . date('Y-m-d', gmtime()) . "</lastmod>\r\n        <changefreq>always</changefreq>\r\n        <priority>1</priority>\r\n    </url>";
        if (!empty($items))
        {
            foreach ($items as $item)
            {
                $sitemap .= "\r\n    <url>\r\n        <loc>" . htmlentities($item['url'], ENT_QUOTES) . "</loc>\r\n        <lastmod>{$item['lastmod']}</lastmod>\r\n        <changefreq>{$item['changefreq']}</changefreq>\r\n        <priority>{$item['priority']}</priority>\r\n    </url>";
            }
        }
        $sitemap .= "\r\n</urlset>";
        return $sitemap;
    }
    /**
     *    写入Google sitemap文件
     *
     *    @author    Garbin
     *    @param     string $sitemap
     *    @return    void
     */
    function _write_google_sitemap($sitemap)
    {
        file_put_contents($this->_google_sitemmap_file, $sitemap);
    }
}

?>
PHP 相关文章推荐
第十四节--命名空间
Nov 16 PHP
PHP下几种删除目录的方法总结
Aug 19 PHP
php实现mysql数据库备份类
Mar 20 PHP
追求程序速度,而不是编程的速度
Apr 23 PHP
php中随机显示图片的函数代码
Jun 23 PHP
一个简洁的PHP可逆加密函数(分享)
Jun 06 PHP
zf框架的校验器InArray使用示例
Mar 13 PHP
PHP输出英文时间日期的安全方法(RFC 1123格式)
Jun 13 PHP
PHP计算指定日期所在周的开始和结束日期的方法
Mar 24 PHP
php遍历CSV类实例
Apr 14 PHP
php二维数组按某个键值排序的实例讲解
Feb 15 PHP
ThinkPHP6.0如何利用自定义验证规则规范的实现登陆
Dec 16 PHP
使用PHP静态变量当缓存的方法
Nov 13 #PHP
使用phpQuery采集网页的方法
Nov 13 #PHP
phpQuery占用内存过多的处理方法
Nov 13 #PHP
PHP反射类ReflectionClass和ReflectionObject的使用方法
Nov 13 #PHP
php堆排序(heapsort)练习
Nov 13 #PHP
php生成EAN_13标准条形码实例
Nov 13 #PHP
使用php计算排列组合的方法
Nov 13 #PHP
You might like
据说是雅虎的一份PHP面试题附答案
2009/01/07 PHP
php生成静态页面的简单示例
2014/04/17 PHP
PHP实现合并discuz用户
2015/08/05 PHP
Thinkphp单字母函数使用指南
2016/05/08 PHP
PHP常用字符串函数小结(推荐)
2018/08/05 PHP
运用Windows XP附带的Msicuu.exe、Msizap.exe来彻底卸载顽固程序
2007/04/21 Javascript
浅谈JavaScript中定义变量时有无var声明的区别
2014/08/18 Javascript
微信小程序商城项目之侧栏分类效果(1)
2017/04/17 Javascript
微信小程序动态添加分享数据
2017/06/14 Javascript
JavaScript正则表达式简单实用实例
2017/06/23 Javascript
jquery插件开发之选项卡制作详解
2017/08/30 jQuery
JS实现按钮颜色切换效果
2020/09/05 Javascript
解决使用layui的时候form表单中的select等不能渲染的问题
2019/09/18 Javascript
Python入门篇之条件、循环
2014/10/17 Python
Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法
2015/04/24 Python
Python使用cx_Oracle调用Oracle存储过程的方法示例
2017/10/07 Python
Python入门之三角函数sin()函数实例详解
2017/11/08 Python
5款非常棒的Python工具
2018/01/05 Python
对python中矩阵相加函数sum()的使用详解
2019/01/28 Python
python3使用QQ邮箱发送邮件
2020/05/20 Python
Pytorch实现GoogLeNet的方法
2019/08/18 Python
自适应线性神经网络Adaline的python实现详解
2019/09/30 Python
python selenium自动化测试框架搭建的方法步骤
2020/06/14 Python
在Pycharm中安装Pandas库方法(简单易懂)
2021/02/20 Python
英国人最爱的饰品网站:Accessorize
2016/08/22 全球购物
Sneaker Studio乌克兰:购买运动鞋
2018/03/26 全球购物
No7 Beauty美国官网:英国国民护肤品牌
2019/10/31 全球购物
银行营业厅大堂经理岗位职责
2014/01/06 职场文书
党员创先争优公开承诺书
2014/03/28 职场文书
《鸿门宴》教学反思
2014/04/22 职场文书
营销与策划专业求职信
2014/06/20 职场文书
四风问题民主生活会对照检查材料思想汇报
2014/09/27 职场文书
水电工程师岗位职责
2015/02/13 职场文书
结婚典礼主持词
2015/06/29 职场文书
幼儿园毕业典礼家长致辞
2015/07/29 职场文书
MySQL系列之十四 MySQL的高可用实现
2021/07/02 MySQL