PHP生成sitemap.xml地图函数
发布时间:2024-06-05 09:23:16
标签:PHP,sitemap
<?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);
}
}
?>


猜你喜欢
- python标准库syssys模块包括了一组非常实用的服务,内含很多函数方法和变量,用来处理Python运行时配置以及资源,从而可以与前当程
- 通常大家想知道数据库是否增长了,增长了多少。大家可能想到用数据库的各个历史时期的大小来比较就可以了。 但怎么能得到这些历史的大小数据呢?好像
- 系统只要能从数据库连接池获取到一个数据库连接,就能执行CRUD。可通过数据库连接将待执行SQL发给MySQL。大部分 crud boy只知道
- 摘要:本文介绍了tensorflow的常用函数。1、tensorflow常用函数TensorFlow 将图形定义转换成分布式执行的操作, 以
- 在Python编程中,导入文本文件是常见的操作之一。Python提供了丰富的标准库,使得文件操作变得十分简单。那么,如何在Python中导入
- 说到排序,我想起一个故事,大意是说唐僧师徒西游美利坚,孙悟空买了本词典,开始逐条背诵单词。他们第一次下美国馆子的时候,不管服务员推荐什么,孙
- 在我们平时项目刚启动时,由于后台也是刚开始开发,我们前端往往在开发过程中没有数据和接口请求的,都要造一些假数据进去或者使用mock造一些数据
- 1. 为什么要使用正则表达式?首先,大家来看一个例子。一个文本文件里面存储了一些市场职位信息,格式如下所示:Python3 高级开发工程师
- 1 索引索引概念索引是一种特殊的文件,包含着对数据表里所有记录的引用指针。可以对表中的一列或多列创建索引,并指定索引的类型,各类索引有各自的
- 最近开发新业务,看到有些功能一样的表格,想着封装一个组件,记录一下:最终实现效果大概实现是:封装一个通用的表格接收两个数组, 一个控制行数,
- 与其他主流语言如 Javascript、Java 和 Python 相比,Golang 的错误处理方式可能和这些你熟悉的语言有所不同。所以才
- 本文实例讲述了Python使用pymongo模块操作MongoDB的方法。分享给大家供大家参考,具体如下:通过pymongo实现python
- 本文介绍了可以帮助简化 PHP 开发的10个项目,包括框架,类库,工具,代码。1.CakePHP Development Framework
- 原因Blog是一个更新并不很频繁的一套系统,但是每次刷新页面都要更新数据库反而很浪费资源,添加静态页面生成是一个解决办法,同时缓存是一个更好
- 这篇文章主要介绍了如何使用Python抓取网页tag操作,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- “/xxxxx”应用程序中的服务器错误。 -------------------------------------------------
- 1、如果之前已经安装我们先卸载一下yum -y remove php*2、由于linux的yum源不存在php7.x,所以我们要更改yum源
- PDOStatement::debugDumpParamsPDOStatement::debugDumpParams — 打印一条 SQL
- 前言CORS 即 Cross Origin Resource Sharing 跨域资源共享.跨域请求分两种:简单请求、复杂请求.简单请求简单
- 这篇文章主要介绍了简单了解python字符串前面加r,u的含义,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,