問答百科站 70 萬問題,需生成符合百度規範的 Sitemap:每個 XML 含 5000 條 URL,共 140 個文件,外加一個 sitemapindex.xml 索引。為避免單次查詢過大影響線上性能,採用「每次查 1000 條、累積寫入」策略。
Index.txt 獲取上次進度(文件名、最大 ID、最小 ID、已寫入條數)。LIMIT 起始位置與數量。0.xml),更新索引文件進度。sitemapindex.xml(<sitemap><loc><lastmod>)。<?php
class SitemapAction {
private static $baseURL = 'https://www.xxx.cn';
private static $askMobileUrl = 'https://m.xxx.cn/ask/';
private static $askPcUrl = 'https://www.xxx.cn/ask/';
private $questionObj; // 模型對象
public function askSetMap() {
header('Content-Type: text/html; charset=utf-8');
$maxXml = 5000; // 單文件上限
$psize = 1000; // 每次查詢量
$indexFile = APP_PATH . 'setmapxml/Index.txt';
$indexXml = '../siteditu/ask/ask.xml'; // sitemapindex.xml
// 1. 讀取/初始化索引進度
$progress = $this->readProgress($indexFile, $maxXml, $psize);
// $progress = ['filename'=>0, 'maxid'=>0, 'minid'=>0, 'psize'=>1000, 'isNewFile'=>true, 'record'=>0]
// 2. 查詢數據
$list = self::$questionObj->getQuestionSetMap($where, $progress['maxid'], $progress['psize']);
if (!$list) { echo '無新數據'; exit; }
// 3. 生成 XML 內容
$xml = '';
foreach ($list as $k => $q) {
if ($k === 0) $progress['minid'] = $q['id'];
$q['lastmod'] = substr($q['lasttime'], 0, 10);
$q['mobileurl'] = self::$askMobileUrl . $q['id'] . '.html';
$q['pcurl'] = self::$askPcUrl . $q['id'] . '-p1.html';
$xml .= $this->buildMobileUrl($q);
$xml .= $this->buildPcUrl($q);
}
$progress['maxid'] = end($list)['id'];
$progress['record'] += count($list);
// 4. 寫入數據 XML
$this->writeDataXml($progress['filename'], $xml, $progress['isNewFile']);
// 5. 更新索引文件
$this->updateIndexTxt($indexFile, $progress);
// 6. 更新 sitemapindex.xml
if ($progress['isNewFile']) {
$this->updateIndexXml($indexXml, $progress['filename']);
}
}
private function buildMobileUrl($q) {
return "<url>\n<loc>{$q['mobileurl']}</loc>\n<mobile:mobile type=\"mobile\"/>\n<lastmod>{$q['lastmod']}</lastmod>\n<changefreq>daily</changefreq>\n<priority>0.8</priority>\n</url>\n";
}
private function buildPcUrl($q) {
return "<url>\n<loc>{$q['pcurl']}</loc>\n<lastmod>{$q['lastmod']}</lastmod>\n<changefreq>daily</changefreq>\n<priority>0.8</priority>\n</url>\n";
}
// ... 文件讀寫、進度管理等輔助方法省略 ...
}
?>
0,3146886,3145887,5000
1,3148886,3147887,1000
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex>
<sitemap>
<loc>https://www.xxx.cn/ask/setmapxml/0.xml</loc>
<lastmod>2014-05-12</lastmod>
</sitemap>
<sitemap>
<loc>https://www.xxx.cn/ask/setmapxml/1.xml</loc>
<lastmod>2014-05-12</lastmod>
</sitemap>
</sitemapindex>
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="https://www.baidu.com/schemas/sitemap-mobile/1/">
<url>
<loc>https://m.xxx.cn/ask/7460.html</loc>
<mobile:mobile type="mobile"/>
<lastmod>2013-01-11</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
...
</urlset>
LIMIT 1000 避免長事務鎖表、內存溢出。file_put_contents(..., FILE_APPEND) 或讀取修改重寫,避免佔用大量內存拼接巨大字符串。xmlns:mobile 命名空間及 <mobile:mobile type="mobile"/>,符合百度移動 Sitemap 規範。