<?php
/*
http://www.baidu.com/schemas/sitemap-mobile/1/
https://www.baidu.com/schemas/sitemap-mobile/1/sitemap-mobile.xsd
https://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd
*/
class Sitemap {
private static $baseURL = 'www.bugshoot.cn'; //URL地址
private static $subDir = '/sitemap/';
private static $mobile_type = 'htmladapt';//enum:mobile, pc, htmladapt
private static $mobileUrl = 'http://www.bugshoot.cn/thread-'; //移动版地址
private static $pcUrl = 'http://www.bugshoot.cn/thread-'; //pc地址
private static $zonePcUrl = 'http://www.bugshoot.cn/thread-'; //精选Pc链接
private static $zoneMobileUrl = 'http://www.bugshoot.cn/thread-'; //精选移动版链接
//sitemaps
public function mapCreate() {
header('Content-type:text/html;charset=utf-8');
//获取问题列表
$maxid = 0; //索引文件最大id
$minid = 0; //索引文件最小id
$psize = 1000; //数据库每次取数量
$maxXml = 2000; //xml写入记录数量
$where = array();
//读取索引文件
$index = 'index.txt';
//关联sitemaps路径
$askXml = "../sitemap.xml";
if (!file_exists($index)) {
$fp = fopen("$index", "w+");
if (!is_writable($index)) {
die("文件:" . $index . "不可写,请检查!");
}
fclose($fp);
} else {
//index.txt文件说明 0:xml文件名称(从1开始)、1:文件最大id、2:文件最小id、3:文件当前记录数
$fp = file($index);
$string = $fp[count($fp) - 1]; //显示最后一行
$arr = explode(',', $string);
}
//索引文件数量是否小于$maxXml
//如果为第一次运行
if (!$arr[1]) {
$bs = 1;
$filename = 0;
} else {
if ($arr && $arr[3] < $maxXml) {
$filename = $arr[0];
$psize = $maxXml - $arr[3] > $psize ? $psize : ($maxXml - $arr[3]);
$bs = 0;
} else {
$filename = $arr[0] + 1;
$bs = 1;
}
}
$maxid = empty($arr[1]) ? 0 : $arr[1];
$minid = empty($arr[2]) ? 0 : $arr[2];
echo "文件名称:" . $filename . ".xml" . "<br/ >";
echo "最大id:" . $maxid . "<br />";
echo "最小id:" . $minid . "<br />";
echo "xml写入最大记录:" . $maxXml . "<br />";
echo "数据库每次读取数量:" . $psize . "<br />";
$query = new queryObj($maxid, $psize);
$list = $query->getList();
if (count($list) <= 0) {
echo 1;
exit;
}
$record = intval($arr[3] + count($list)); //索引文件写入记录数
$indexArr = array('filename' => $filename, 'maxid' => $maxid, 'minid' => $minid, 'maxXml' => $record);
$start = '<?xml version="1.0" encoding="utf-8"?> ' . chr(10);
$start.= "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:mobile=\"http://www.baidu.com/schemas/sitemap-mobile/1/\">" . chr(10);
$start.= "</urlset>";
$xml = '';
foreach ($list as $k => $qinfo) {
#print_r($qinfo);
if ($k == 0) $indexArr['minid'] = $qinfo['tid'];
$qinfo['lastmod'] = substr($qinfo['last_date'], 0, 10);
$qinfo['mobielurl'] = self::$mobileUrl . $qinfo['tid'] . '.htm'; //移动版链接
$qinfo['pcurl'] = self::$pcUrl . $qinfo['tid'] . '.htm'; //pc版链接
$xml.= $this->sitemapMobileUrl($qinfo, 'weekly', self::$mobile_type); //移动版
$xml.= $this->sitemapPcUrl($qinfo, 'weekly'); //pc版
}
$maxid = end($list);
$indexArr['maxid'] = $maxid['tid'];
//更新索引文件
if ($bs == 0) {
//更新最后一行
$txt = file($index);
$txt[count($txt) - 1] = $indexArr['filename'] . ',' . $indexArr['maxid'] . ',' . $indexArr['minid'] . ',' . $indexArr['maxXml'] . "\r\n";
$str = join($txt);
if (is_writable($index)) {
if (!$handle = fopen($index, 'w')) {
echo "不能打开文件 $index";
exit;
exit;
}
if (fwrite($handle, $str) === FALSE) {
echo "不能写入到文件 $index";
fclose($handle);
exit;
exit;
}
echo "成功地写入文件$index";
fclose($handle);
} else {
echo "文件 $index 不可写";
exit;
}
#fclose($index);
} elseif ($bs == 1) {
//新加入一行
$fp = fopen($index, 'a');
$num = count($list);
$string = $indexArr['filename'] . ',' . $indexArr['maxid'] . ',' . $indexArr['minid'] . ',' . $num . "\r\n";
if (fwrite($fp, $string) === false) {
echo "追加新行失败。。。";
exit;
} else {
echo "追加成功<br />";
//更新sitemap索引文件
$xmlData = '<?xml version="1.0" encoding="utf-8"?>' . chr(10);
$xmlData.= "<sitemapindex>" . chr(10);
$xmlData.= "</sitemapindex>";
if (!file_exists($askXml)) file_put_contents($askXml, $xmlData);
$fileList = file($askXml);
$fileCount = count($fileList);
$sitemapxml = 'http://' . self::$baseURL . self::$subDir . "{$filename}.xml"; //正常sitemap链接
$txt = $this->siteMapIndex($sitemapxml);
$fileList[$fileCount - 1] = $txt . "</sitemapindex>";
$newContent = '';
foreach ($fileList as $v) {
$newContent.= $v;
}
if (!file_put_contents($askXml, $newContent)) exit('无法写入数据');
echo '已经写入文档' . $askXml;
}
fclose($fp);
}
$filename = $filename . '.xml';
//更新到xml文件中,增加结尾
if (!file_exists($filename)) file_put_contents($filename, $start);
$xmlList = file($filename);
$xmlCount = count($xmlList);
$xmlList[$xmlCount - 1] = $xml . "</urlset>";
$newXml = '';
foreach ($xmlList as $v) {
$newXml.= $v;
}
if (!file_put_contents($filename, $newXml)) exit("写入数据错误");
else echo "写入数据成功<br />";
}
//移动版xml
private function sitemapMobileUrl($data, $changefreq, $mobile_type) {
$xml = '';
if (is_array($data) && !empty($data)) {
$xml.= "<url>" . chr(10);
if ($data['tid']) $xml.= '<loc>' . $data['mobielurl'] . '</loc>' . chr(10); //移动版链接
$xml.= "<mobile:mobile type=\"". $mobile_type ."\"/>" . chr(10);
if ($data['lastmod']) $xml.= '<lastmod>' . $data['lastmod'] . '</lastmod>' . chr(10);
$xml.= '<changefreq>'. $changefreq .'</changefreq>' . chr(10);
$xml.= '<priority>0.8</priority>' . chr(10);
$xml.= "</url>" . chr(10);
return $xml;
}
}
//pc版xml
private function sitemapPcUrl($data, $changefreq) {
$xml = '';
if (is_array($data) && !empty($data)) {
$xml.= '<url>' . chr(10);
if ($data['tid']) $xml.= '<loc>' . $data['pcurl'] . '</loc>' . chr(10); //pc版链接
if ($data['lastmod']) $xml.= '<lastmod>' . $data['lastmod'] . '</lastmod>' . chr(10);
$xml.= '<changefreq>'. $changefreq .'</changefreq>' . chr(10);
$xml.= '<priority>0.8</priority>' . chr(10);
$xml.= '</url>' . chr(10);
return $xml;
}
}
//sitemaps索引文件
private function siteMapIndex($filename) {
$xml = '';
$xml.= "<sitemap>" . chr(10);
$xml.= "<loc>{$filename}</loc>" . chr(10);
$xml.= "<lastmod>" . date("Y-m-d", time()) . "</lastmod>" . chr(10);
$xml.= "</sitemap>" . chr(10);
return $xml;
}
}
class DB {
//pdo对象
public $con = NULL;
private $user;
private $pwd;
private $dbname;
function __construct($dbname='', $user='', $pwd='')
{ //初始化对象,将初始化值放在括号内
$this->user=$user;
$this->pwd=$pwd;
$this->dbname=$dbname;
$this->con = new PDO('mysql:host=127.0.0.1;dbname=' . $this->dbname . ';', $this->user, $this->pwd,
[PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES `utf8`',PDO::ATTR_PERSISTENT=> TRUE]);
$this->con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->con->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
}
public function query($sql, $para = NULL)
{
$sqlType = strtoupper(substr($sql, 0, 6));
$cmd = $this->con->prepare($sql);
if($para != NULL)
{
$cmd->execute($para);
}
else
{
$cmd->execute();
}
if($sqlType == 'SELECT')
{
return $cmd->fetchAll();
}
if($sqlType == 'INSERT')
{
return $this->con->lastInsertId();
}
return $cmd->rowCount();
}
}
class queryObj
{
private $maxid;
private $psize;
private $db;
function __construct($maxid = 0, $psize = 1000)
{
$this->maxid = $maxid;
$this->psize = $psize;
$this->db = new DB('bugshoot.cn', 'bugshoot.cn', 'bugshoot.cn');
}
public function getList()
{
echo '#####0####\n\r';
echo $this->maxid;
echo '\n\r#####1####';
$sql = 'SELECT tid,last_date FROM bbs_thread limit ' . ($this->maxid + 0) . ',' . $this->psize . ';';
echo $sql;
echo '\n\r#####2####\n\r';
$list = $this->db->query($sql);
return $list;
}
}
$s = new Sitemap();
$s->mapCreate();
php生成sitemapindex程序精较版
猜你喜欢
转载自blog.csdn.net/donglynn/article/details/78215902
今日推荐
周排行