SitemapController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2018年7月15日
  7. * 生成sitemap文件
  8. */
  9. namespace app\home\controller;
  10. use core\basic\Controller;
  11. use app\home\model\SitemapModel;
  12. use core\basic\Url;
  13. class SitemapController extends Controller
  14. {
  15. protected $model;
  16. public function __construct()
  17. {
  18. $this->model = new SitemapModel();
  19. }
  20. public function index()
  21. {
  22. header("Content-type:text/xml;charset=utf-8");
  23. $str = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
  24. //$str .= '<urlset>' . "\n";
  25. $str .= '<urlset xmlns= "http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n" ;
  26. $str .= $this->makeNode('', date('Y-m-d'), '1.00', 'always'); // 根目录
  27. $sorts = $this->model->getSorts();
  28. $Parser = new ParserController();
  29. foreach ($sorts as $value) {
  30. if ($value->outlink) {
  31. continue;
  32. } elseif ($value->type == 1) {
  33. $link = $Parser->parserLink(1, $value->urlname, 'about', $value->scode, $value->filename);
  34. $str .= $this->makeNode($link, date('Y-m-d'), '0.80', 'daily');
  35. } else {
  36. $link = $Parser->parserLink(2, $value->urlname, 'list', $value->scode, $value->filename);
  37. $str .= $this->makeNode($link, date('Y-m-d'), '0.80', 'daily');
  38. $contents = $this->model->getSortContent($value->scode);
  39. foreach ($contents as $value2) {
  40. if ($value2->outlink) { // 外链
  41. continue;
  42. } else {
  43. $link = $Parser->parserLink(2, $value2->urlname, 'content', $value2->scode, $value2->sortfilename, $value2->id, $value2->filename);
  44. }
  45. $str .= $this->makeNode($link, date('Y-m-d', strtotime($value2->date)), '0.60', 'daily');
  46. }
  47. }
  48. }
  49. echo $str . "\n</urlset>";
  50. }
  51. // 生成结点信息
  52. private function makeNode($link, $date, $priority = 0.60, $changefreq = 'always')
  53. {
  54. $node = '
  55. <url>
  56. <loc>' . get_http_url() . $link . '</loc>
  57. <priority>' . $priority . '</priority>
  58. <lastmod>' . $date . '</lastmod>
  59. <changefreq>' . $changefreq . '</changefreq>
  60. </url>';
  61. return $node;
  62. }
  63. // 文本格式
  64. public function linkTxt()
  65. {
  66. header("Content-type:text/plain;charset=utf-8");
  67. $sorts = $this->model->getSorts();
  68. $Parser = new ParserController();
  69. $str = get_http_url() . "\n";
  70. foreach ($sorts as $value) {
  71. if ($value->outlink) {
  72. continue;
  73. } elseif ($value->type == 1) {
  74. $link = $Parser->parserLink(1, $value->urlname, 'about', $value->scode, $value->filename);
  75. $str .= get_http_url() . $link . "\n";
  76. } else {
  77. $link = $Parser->parserLink(2, $value->urlname, 'list', $value->scode, $value->filename);
  78. $str .= get_http_url() . $link . "\n";
  79. $contents = $this->model->getSortContent($value->scode);
  80. foreach ($contents as $value2) {
  81. if ($value2->outlink) { // 外链
  82. continue;
  83. } else {
  84. $link = $Parser->parserLink(2, $value2->urlname, 'content', $value2->scode, $value2->sortfilename, $value2->id, $value2->filename);
  85. }
  86. $str .= get_http_url() . $link . "\n";
  87. }
  88. }
  89. }
  90. echo $str;
  91. }
  92. }