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 相关文章推荐
3
Oct 09 PHP
PHP通用分页类page.php[仿google分页]
Aug 31 PHP
php 正确解码javascript中通过escape编码后的字符
Jan 28 PHP
php笔记之:有规律大文件的读取与写入的分析
Apr 26 PHP
浅谈php正则表达式中的非贪婪模式匹配的使用
Nov 25 PHP
php通过Chianz.com获取IP地址与地区的方法
Jan 14 PHP
支持png透明图片的php生成缩略图类分享
Feb 08 PHP
php使用for语句输出三角形的方法
Jun 09 PHP
php将一维数组转换为每3个连续值组成的二维数组
May 06 PHP
PHP基于递归实现的约瑟夫环算法示例
Aug 27 PHP
Laravel 批量更新多条数据的示例
Nov 27 PHP
PHP设计模式之状态模式定义与用法详解
Apr 02 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数据库连接类~~做成一个分页类!
2006/11/25 PHP
PHP备份数据库生成SQL文件并下载的函数代码
2012/02/05 PHP
php使用高斯算法实现图片的模糊处理功能示例
2016/11/11 PHP
Fleaphp常见函数功能与用法示例
2016/11/15 PHP
PHP文件上传、客户端和服务器端加限制、抓取错误信息、完整步骤解析
2017/01/12 PHP
jscript之Open an Excel Spreadsheet
2007/06/13 Javascript
JavaScript几种形式的树结构菜单
2010/05/10 Javascript
JS图片根据鼠标滚动延时加载的实例代码
2013/07/13 Javascript
JavaScript获取多个数组的交集简单实例
2013/11/11 Javascript
jQuery学习笔记之 Ajax操作篇(一) - 数据加载
2014/06/23 Javascript
JavaScript的RequireJS库入门指南
2015/07/01 Javascript
jQuery拖动布局其结果保存到数据库
2015/10/09 Javascript
Js遍历键值对形式对象或Map形式的方法
2016/08/08 Javascript
基于BootStrap实现局部刷新分页实例代码
2016/08/08 Javascript
jQuery插件HighCharts实现的2D面积图效果示例【附demo源码下载】
2017/03/15 Javascript
React-router 4 按需加载的实现方式及原理详解
2017/05/25 Javascript
通过js控制时间,一秒一秒自己动的实例
2017/10/25 Javascript
Angular移动端页面input无法输入的解决方法
2017/11/14 Javascript
angularjs使用gulp-uglify压缩后执行报错的解决方法
2018/03/07 Javascript
js代码规范之Eslint安装与配置详解
2018/09/08 Javascript
vue动态改变背景图片demo分享
2018/09/13 Javascript
详解ES6 中的Object.assign()的用法实例代码
2021/01/11 Javascript
[53:03]Optic vs TNC 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/18 DOTA
Using Django with GAE Python 后台抓取多个网站的页面全文
2016/02/17 Python
解决python2.7用pip安装包时出现错误的问题
2017/01/23 Python
用Django实现一个可运行的区块链应用
2018/03/08 Python
Python Flask 搭建微信小程序后台详解
2019/05/06 Python
Python定义一个Actor任务
2020/07/29 Python
Ray-Ban雷朋美国官网:全球领先的太阳眼镜品牌
2016/07/20 全球购物
纬创Java面试题笔试题
2014/10/02 面试题
医生自荐信
2013/10/11 职场文书
歌唱比赛策划方案
2014/06/06 职场文书
酒店七夕情人节活动策划方案
2014/08/24 职场文书
2015教师年度工作总结范文
2015/04/07 职场文书
工作年限证明模板
2015/06/15 职场文书
小学中队委竞选稿
2015/11/20 职场文书