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 相关文章推荐
php,ajax实现分页
Mar 27 PHP
使用PHP提取视频网站页面中的FLASH地址的代码
Apr 17 PHP
PHP中file_exists与is_file,is_dir的区别介绍
Sep 12 PHP
PHP读取大文件的类SplFileObject使用介绍
Apr 09 PHP
php判断页面是否是微信打开的示例(微信打开网页)
Apr 25 PHP
php格式化金额函数分享
Feb 02 PHP
ThinkPHP里用U方法调用js文件实例
Jun 18 PHP
smarty中改进truncate使其支持中文的方法
May 30 PHP
PHP中利用sleep函数实现定时执行功能实现代码
Aug 25 PHP
PHP封装的多文件上传类实例与用法详解
Feb 07 PHP
php生成随机数/生成随机字符串的方法小结【5种方法】
May 27 PHP
一文搞懂php的垃圾回收机制
Jun 18 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随机生成福彩双色球号码的2种方法
2013/02/04 PHP
PHP中使用TCPDF生成PDF文档实例
2014/07/01 PHP
浅谈php和.net的区别
2014/09/28 PHP
php合并数组中相同元素的方法
2014/11/13 PHP
php根据日期显示所在星座的方法
2015/07/13 PHP
很可爱的输入框
2008/08/03 Javascript
JS通过分析userAgent属性来判断浏览器的类型及版本
2014/03/28 Javascript
Javascript异步编程模型Promise模式详细介绍
2014/05/08 Javascript
nodejs教程之异步I/O
2014/11/21 NodeJs
使用VS开发 Node.js指南
2015/01/06 Javascript
简介JavaScript中toUpperCase()方法的使用
2015/06/06 Javascript
javascript实现一个简单的弹出窗
2016/02/22 Javascript
vue.js实现含搜索的多种复选框(附源码)
2017/03/23 Javascript
js实现移动端导航点击自动滑动效果
2017/07/18 Javascript
微信小程序开发之改变data中数组或对象的某一属性值
2018/07/05 Javascript
vue使用监听实现全选反选功能
2018/07/06 Javascript
详解vue中axios的使用与封装
2019/03/20 Javascript
layui table动态表头 改变表格头部 重新加载表格的方法
2019/09/21 Javascript
vue-cli3项目配置eslint代码规范的完整步骤
2020/09/10 Javascript
Python变量作用范围实例分析
2015/07/07 Python
Queue 实现生产者消费者模型(实例讲解)
2017/11/13 Python
Python+matplotlib实现计算两个信号的交叉谱密度实例
2018/01/08 Python
Python Django 页面上展示固定的页码数实现代码
2019/08/21 Python
Python一行代码解决矩阵旋转的问题
2019/11/30 Python
HTML5中外部浏览器唤起微信分享
2020/01/02 HTML / CSS
Html5跳转到APP指定页面的实现
2020/01/14 HTML / CSS
Electrolux伊莱克斯巴西商店:家用电器、小家电和配件
2018/05/23 全球购物
日本亚马逊官方网站:Amazon.co.jp
2020/04/14 全球购物
刚毕业大学生自荐信范文
2014/02/20 职场文书
环保倡议书怎么写
2014/05/16 职场文书
工商局所长四风自我剖析及整改措施
2014/10/26 职场文书
2014年仓库管理员工作总结
2014/11/18 职场文书
小学少先队活动总结
2015/05/08 职场文书
高中军训感想
2015/08/07 职场文书
pandas DataFrame.shift()函数的具体使用
2021/05/24 Python
vue 给数组添加新对象并赋值
2022/04/20 Vue.js