Sitemap.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services;
  10. use Yii;
  11. /**
  12. * Sitemap services.
  13. * @author Terry Zhao <2358269014@qq.com>
  14. * @since 1.0
  15. */
  16. class Sitemap extends Service
  17. {
  18. public $numPerPage = 100;
  19. public $sitemapConfig;
  20. protected $currentDate; // = date('Y-m-d');
  21. protected function initSiteMap()
  22. {
  23. $this->currentDate = date('Y-m-d');
  24. }
  25. /**
  26. * 在store的配置中,没一个store都有一个sitemap文件路径的配置项,譬如:
  27. * 'sitemapDir' => '@appfront/web/sitemap.xml',
  28. * 下面就是sitemap开始阶段,把格式头写入到对应的sitemap文件中
  29. * 对于sitemp.xml文件的访问,为了需要,您可以在nginx中做重新的指向。
  30. * sitemap的更多资料,您可以参看:http://www.fecshop.com/doc/fecshop-guide/instructions/cn-1.0/guide-fecshop_sitemap.html
  31. */
  32. protected function actionBeginSiteMap()
  33. {
  34. $this->initSiteMap();
  35. if (is_array($this->sitemapConfig) && !empty($this->sitemapConfig)) {
  36. foreach ($this->sitemapConfig as $appIn => $store) {
  37. if (is_array($store) && !empty($store)) {
  38. foreach ($store as $domain => $info) {
  39. $sitemapDir = (isset($info['sitemapDir']) && $info['sitemapDir']) ? $info['sitemapDir'] : '';
  40. if ($sitemapDir) {
  41. $sitemapAbsoluteDir = Yii::getAlias($sitemapDir);
  42. $xmlFile = fopen($sitemapAbsoluteDir, 'w') or die('Unable to open file!');
  43. if (file_exists($sitemapAbsoluteDir)) {
  44. $str = '<?xml version="1.0" encoding="UTF-8"?>
  45. <?xml-stylesheet type="text/xsl" href="gss.xsl"?>
  46. <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
  47. ';
  48. fwrite($xmlFile, $str);
  49. }
  50. fclose($xmlFile);
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }
  57. /**
  58. * sitemap 文件写入内容后的结束执行的函数。
  59. */
  60. protected function actionEndSiteMap()
  61. {
  62. $this->initSiteMap();
  63. if (is_array($this->sitemapConfig) && !empty($this->sitemapConfig)) {
  64. foreach ($this->sitemapConfig as $appIn => $store) {
  65. if (is_array($store) && !empty($store)) {
  66. foreach ($store as $domain => $info) {
  67. $sitemapDir = (isset($info['sitemapDir']) && $info['sitemapDir']) ? $info['sitemapDir'] : '';
  68. if ($sitemapDir) {
  69. $sitemapAbsoluteDir = Yii::getAlias($sitemapDir);
  70. $xmlFile = fopen($sitemapAbsoluteDir, 'a') or die('Unable to open file!');
  71. if (file_exists($sitemapAbsoluteDir)) {
  72. $str = '</urlset>';
  73. fwrite($xmlFile, $str);
  74. }
  75. fclose($xmlFile);
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
  82. /**
  83. * 在sitemap文件中写入home部分的链接
  84. */
  85. protected function actionHome()
  86. {
  87. $this->initSiteMap();
  88. if (is_array($this->sitemapConfig) && !empty($this->sitemapConfig)) {
  89. foreach ($this->sitemapConfig as $appIn => $store) {
  90. if (is_array($store) && !empty($store)) {
  91. foreach ($store as $domain => $info) {
  92. $https = $info['https'];
  93. $showScriptName = $info['showScriptName'];
  94. $sitemapDir = (isset($info['sitemapDir']) && $info['sitemapDir']) ? $info['sitemapDir'] : '';
  95. if ($sitemapDir) {
  96. $sitemapAbsoluteDir = Yii::getAlias($sitemapDir);
  97. $xmlFile = fopen($sitemapAbsoluteDir, 'a') or die('Unable to open file!');
  98. if (file_exists($sitemapAbsoluteDir)) {
  99. $home_url = Yii::$service->url->getUrlByDomain('', [], $https, $domain, $showScriptName, true);
  100. $str = '<url><loc>'.$home_url.'</loc><lastmod>'.$this->currentDate.'</lastmod></url>';
  101. fwrite($xmlFile, $str);
  102. }
  103. fclose($xmlFile);
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. /**
  111. * 得到分类的总个数
  112. */
  113. protected function actionCategorypagecount()
  114. {
  115. $this->initSiteMap();
  116. $coll = Yii::$service->category->coll();
  117. $count = $coll['count'];
  118. echo ceil($count / $this->numPerPage);
  119. }
  120. /**
  121. * 在sitemap文件中写入分类部分的链接
  122. */
  123. protected function actionCategory($pageNum)
  124. {
  125. $this->initSiteMap();
  126. if (is_array($this->sitemapConfig) && !empty($this->sitemapConfig)) {
  127. foreach ($this->sitemapConfig as $appIn => $store) {
  128. if (is_array($store) && !empty($store)) {
  129. foreach ($store as $domain => $info) {
  130. $https = $info['https'];
  131. $showScriptName = $info['showScriptName'];
  132. $sitemapDir = (isset($info['sitemapDir']) && $info['sitemapDir']) ? $info['sitemapDir'] : '';
  133. if ($sitemapDir) {
  134. $sitemapAbsoluteDir = Yii::getAlias($sitemapDir);
  135. $xmlFile = fopen($sitemapAbsoluteDir, 'a') or die('Unable to open file!');
  136. if (file_exists($sitemapAbsoluteDir)) {
  137. $filter = [
  138. 'numPerPage' => $this->numPerPage,
  139. 'pageNum' => $pageNum,
  140. 'asArray' => true,
  141. ];
  142. $coll = Yii::$service->category->coll($filter);
  143. $data = $coll['coll'];
  144. if (is_array($data) && !empty($data)) {
  145. foreach ($data as $one) {
  146. $category_url_key = $one['url_key'];
  147. $category_url = Yii::$service->url->getUrlByDomain($category_url_key, [], $https, $domain, $showScriptName, true);
  148. $str = '<url><loc>'.$category_url.'</loc><lastmod>'.$this->currentDate.'</lastmod></url>';
  149. fwrite($xmlFile, $str);
  150. }
  151. }
  152. }
  153. fclose($xmlFile);
  154. }
  155. }
  156. }
  157. }
  158. }
  159. }
  160. /**
  161. * 得到产品的总个数
  162. */
  163. protected function actionProductpagecount()
  164. {
  165. $this->initSiteMap();
  166. $coll = Yii::$service->product->coll();
  167. $count = $coll['count'];
  168. echo ceil($count / $this->numPerPage);
  169. }
  170. /**
  171. * 在sitemap文件中写入产品部分的链接
  172. */
  173. protected function actionProduct()
  174. {
  175. $this->initSiteMap();
  176. if (is_array($this->sitemapConfig) && !empty($this->sitemapConfig)) {
  177. foreach ($this->sitemapConfig as $appIn => $store) {
  178. if (is_array($store) && !empty($store)) {
  179. foreach ($store as $domain => $info) {
  180. $https = $info['https'];
  181. $showScriptName = $info['showScriptName'];
  182. $sitemapDir = (isset($info['sitemapDir']) && $info['sitemapDir']) ? $info['sitemapDir'] : '';
  183. if ($sitemapDir) {
  184. $sitemapAbsoluteDir = Yii::getAlias($sitemapDir);
  185. $xmlFile = fopen($sitemapAbsoluteDir, 'a') or die('Unable to open file!');
  186. if (file_exists($sitemapAbsoluteDir)) {
  187. $filter = [
  188. 'numPerPage' => $this->numPerPage,
  189. 'pageNum' => $pageNum,
  190. 'asArray' => true,
  191. ];
  192. $coll = Yii::$service->product->coll($filter);
  193. $data = $coll['coll'];
  194. if (is_array($data) && !empty($data)) {
  195. foreach ($data as $one) {
  196. $product_url_key = $one['url_key'];
  197. $product_url = Yii::$service->url->getUrlByDomain($product_url_key, [], $https, $domain, $showScriptName, true);
  198. $str = '<url><loc>'.$product_url.'</loc><lastmod>'.$this->currentDate.'</lastmod></url>';
  199. fwrite($xmlFile, $str);
  200. }
  201. }
  202. }
  203. fclose($xmlFile);
  204. }
  205. }
  206. }
  207. }
  208. }
  209. }
  210. /**
  211. * page页的总个数
  212. */
  213. protected function actionCmspagepagecount()
  214. {
  215. $this->initSiteMap();
  216. $coll = Yii::$service->cms->article->coll();
  217. $count = $coll['count'];
  218. echo ceil($count / $this->numPerPage);
  219. }
  220. /**
  221. * 在sitemap文件中写入page部分的链接
  222. */
  223. protected function actionCmspage()
  224. {
  225. $this->initSiteMap();
  226. if (is_array($this->sitemapConfig) && !empty($this->sitemapConfig)) {
  227. foreach ($this->sitemapConfig as $appIn => $store) {
  228. if (is_array($store) && !empty($store)) {
  229. foreach ($store as $domain => $info) {
  230. $https = $info['https'];
  231. $showScriptName = $info['showScriptName'];
  232. $sitemapDir = (isset($info['sitemapDir']) && $info['sitemapDir']) ? $info['sitemapDir'] : '';
  233. if ($sitemapDir) {
  234. $sitemapAbsoluteDir = Yii::getAlias($sitemapDir);
  235. $xmlFile = fopen($sitemapAbsoluteDir, 'a') or die('Unable to open file!');
  236. if (file_exists($sitemapAbsoluteDir)) {
  237. $filter = [
  238. 'numPerPage' => $this->numPerPage,
  239. 'pageNum' => $pageNum,
  240. 'asArray' => true,
  241. ];
  242. $coll = Yii::$service->cms->article->coll($filter);
  243. $data = $coll['coll'];
  244. if (is_array($data) && !empty($data)) {
  245. foreach ($data as $one) {
  246. $cms_page_url_key = $one['url_key'];
  247. $cms_page_url = Yii::$service->url->getUrlByDomain($cms_page_url_key, [], $https, $domain, $showScriptName, true);
  248. $str = '<url><loc>'.$cms_page_url.'</loc><lastmod>'.$this->currentDate.'</lastmod></url>';
  249. fwrite($xmlFile, $str);
  250. }
  251. }
  252. }
  253. fclose($xmlFile);
  254. }
  255. }
  256. }
  257. }
  258. }
  259. }
  260. }