Emlog Sitemap 插件修改版

作者:浮夸的晓编 分类:分享 发布于:2014-5-15
  前阵子在百度获取了本站的sitemap提交权限,但把奇遇sitemap插件生成的地图提交后,发现时间那一栏有些问题(时间过于精确<lastmod>2014-04-21T15:38:38+00:00</lastmod>都已经精确到秒了,而一般都只需精确到日期,不然就识别不了。如:<lastmod>2014-04-21</lastmod>),除了谷歌认识这外,其它好像都不认识。

  由于这时间是直接用的发帖时间,自己把这插件折腾了半天也没折腾出来,于是我去Emlog论坛发贴,希望奇遇能解决,也不知道这两天不在还是怎么的,一直没回复。

  最后我看了下百度sitemap的标准,发现这时间栏可要可不要,然后顺便看了下搜狗、360搜索及谷歌其实都对时间没啥要求,最主要的是网址这一行<loc>http://www.yoursite.com/</loc>。

  附百度单个xml数据格式:
<?xml version="1.0" encoding="utf-8"?>
<!-- XML文件需以utf-8编码-->
<urlset>
<!--必填标签-->
    <url>
        <!--必填标签,这是具体某一个链接的定义入口,每一条数据都要用<url>和</url>包含在里面,这是必须的 -->
        <loc>http://www.***.com/yoursite.html</loc>
        <!--必填,URL链接地址,长度不得超过256字节-->
        <lastmod>2009-12-14</lastmod>
        <!--可以不提交该标签,用来指定该链接的最后更新时间-->
        <changefreq>daily</changefreq>
        <!--可以不提交该标签,用这个标签告诉此链接可能会出现的更新频率 -->
        <priority>0.8</priority>
        <!--可以不提交该标签,用来指定此链接相对于其他链接的优先权比值,此值定于0.0-1.0之间-->
    </url>
    <url>
        <loc>http://www.***.com/yoursite2.html</loc>
        <lastmod>2010-05-01</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>
</urlset>

  其实只要标明时间这一项可以不要,这就好办找到class.sitemap.php文件把下面的代码直接替换即可:
<?php
/*
Plugin Name: sitemap
Version: 1.0
Plugin URL: http://www.qiyuuu.com/for-emlog/emlog-plugin-sitemap
Description: 生成sitemap,供搜索引擎抓取
Author: 奇遇
Author Email: qiyuuu@gmail.com
Author URL: http://www.qiyuuu.com
*/
!defined('EMLOG_ROOT') && exit('access deined!');
class sitemap {
	private $db;
	private $path;
	private $changefreq;
	private $priority;
	private $comment_time;
	private $data;
	function __construct() {
		extract(sitemap_config());
		$this->db = MySql::getInstance();
		$this->path = EMLOG_ROOT . '/' . $sitemap_name;
		$this->changefreq = $sitemap_changefreq;
		$this->priority = $sitemap_priority;
		$this->comment_time = $sitemap_comment_time;
	}
	function build() {
		$this->setData();
		$xml = $this->buildXML();
		return @file_put_contents($this->path,$xml);
	}
	private function setData() {
		$CACHE = Cache::getInstance();
		$data = array();
		$lastCommentTime = $this->getLastCommentTime();
		$data[] = array('url'=>BLOG_URL,'lastmod'=>time(),'changefreq'=>'always','priority'=>'1.0');
		//日志
		$query = $this->db->query("SELECT gid,date FROM " . DB_PREFIX . "blog WHERE type='blog' AND hide='n' ORDER BY date DESC");
		while($row = $this->db->fetch_array($query)) {
			$lastmod = $this->comment_time && isset($lastCommentTime[$row['gid']]) ? $lastCommentTime[$row['gid']] : $row['date'];
			$data[] = array('url'=>Url::log($row['gid']),'lastmod'=>$lastmod,'changefreq'=>$this->changefreq[0],'priority'=>$this->priority[0]);
		}
		//页面
		$query = $this->db->query("SELECT gid,date FROM " . DB_PREFIX . "blog WHERE type='page' AND hide='n' ORDER BY date DESC");
		while($row = $this->db->fetch_array($query)) {
			$lastmod = $this->comment_time && isset($lastCommentTime[$row['gid']]) ? $lastCommentTime[$row['gid']] : $row['date'];
			$data[] = array('url'=>Url::log($row['gid']),'lastmod'=>$lastmod,'changefreq'=>$this->changefreq[0],'priority'=>$this->priority[0]);
		}
		//分类
		foreach($CACHE->readCache('sort') as $value) {
			$data[] = array('url'=>Url::sort($value['sid']),'lastmod'=>time(),'changefreq'=>$this->changefreq[2],'priority'=>$this->priority[2]);	
		}
		//标签
		foreach($CACHE->readCache('tags') as $value) {
			$data[] = array('url'=>Url::tag($value['tagurl']),'lastmod'=>time(),'changefreq'=>$this->changefreq[3],'priority'=>$this->priority[3]);
		}
		//碎语
		if(Option::get('istwitter') == 'y') {
			$newtws_cache = $CACHE->readCache('newtw');
			$data[] = array('url'=>BLOG_URL . 't/','lastmod'=>$newtws_cache[0]['date'],'changefreq'=>$this->changefreq[4],'priority'=>$this->priority[4]);
		}
		//归档
		foreach($CACHE->readCache('record') as $value) {
			preg_match("/^([\d]{4})([\d]{2})$/", $value['date'], $match);
			$days = getMonthDayNum($match[2], $match[1]);
			$lastmod = emStrtotime($value['date'] . '01') + 3600 * 24 * $days;
			$data[] = array('url'=>Url::record($value['date']),'lastmod'=>$lastmod,'changefreq'=>$this->changefreq[5],'priority'=>$this->priority[5]);	
		}
		$this->data = $data;
	}
	private function generate($url, $lastmod, $changefreq, $priority) {
		$url = htmlspecialchars($url);
		$lastmod = gmdate('c',$lastmod);
		return "<url>\n<loc>$url</loc>\n<changefreq>$changefreq</changefreq>\n<priority>$priority</priority>\n</url>\n";
	}
	private function buildXML() {
		$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
		foreach($this->data as $value) {
			extract($value);
			$xml .= $this->generate($url, $lastmod, $changefreq, $priority);
		}
		$xml .= '</urlset>';
		return $xml;
	}
	private function getLastCommentTime() {
		$query = $this->db->query("SELECT gid,max(date) as date FROM " . DB_PREFIX . "comment WHERE hide='n' GROUP BY gid");
		$lastCommentTime = array();
		while($row = $this->db->fetch_array($query)) {
			$lastCommentTime[$row['gid']] = $row['date'];
		}
		return $lastCommentTime;
	}
}


  附站长网下载地址:http://down.chinaz.com/soft/36067.htm