SitemapController.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 .= $this->makeNode('', date('Y-m-d'), '1.00', 'always'); // 根目录
  26. $sorts = $this->model->getSorts();
  27. $Parser = new ParserController();
  28. foreach ($sorts as $value) {
  29. if ($value->outlink) {
  30. continue;
  31. } elseif ($value->type == 1) {
  32. $link = $Parser->parserLink(1, $value->urlname, 'about', $value->scode, $value->filename);
  33. $str .= $this->makeNode($link, date('Y-m-d'), '0.80', 'daily');
  34. } else {
  35. $link = $Parser->parserLink(2, $value->urlname, 'list', $value->scode, $value->filename);
  36. $str .= $this->makeNode($link, date('Y-m-d'), '0.80', 'daily');
  37. $contents = $this->model->getSortContent($value->scode);
  38. foreach ($contents as $value2) {
  39. if ($value2->outlink) { // 外链
  40. continue;
  41. } else {
  42. $link = $Parser->parserLink(2, $value2->urlname, 'content', $value2->scode, $value2->sortfilename, $value2->id, $value2->filename);
  43. }
  44. $str .= $this->makeNode($link, date('Y-m-d', strtotime($value2->date)), '0.60', 'daily');
  45. }
  46. }
  47. }
  48. echo $str . "\n</urlset>";
  49. }
  50. // 生成结点信息
  51. private function makeNode($link, $date, $priority = 0.60, $changefreq = 'always')
  52. {
  53. $node = '
  54. <url>
  55. <loc>' . get_http_url() . $link . '</loc>
  56. <priority>' . $priority . '</priority>
  57. <lastmod>' . $date . '</lastmod>
  58. <changefreq>' . $changefreq . '</changefreq>
  59. </url>';
  60. return $node;
  61. }
  62. // 文本格式
  63. public function linkTxt()
  64. {
  65. header("Content-type:text/plain;charset=utf-8");
  66. $sorts = $this->model->getSorts();
  67. $Parser = new ParserController();
  68. $str = get_http_url() . "\n";
  69. foreach ($sorts as $value) {
  70. if ($value->outlink) {
  71. continue;
  72. } elseif ($value->type == 1) {
  73. $link = $Parser->parserLink(1, $value->urlname, 'about', $value->scode, $value->filename);
  74. } else {
  75. $link = $Parser->parserLink(2, $value->urlname, 'list', $value->scode, $value->filename);
  76. $str .= get_http_url() . $link . "\n";
  77. $contents = $this->model->getSortContent($value->scode);
  78. foreach ($contents as $value2) {
  79. if ($value2->outlink) { // 外链
  80. continue;
  81. } else {
  82. $link = $Parser->parserLink(2, $value2->urlname, 'content', $value2->scode, $value2->sortfilename, $value2->id, $value2->filename);
  83. }
  84. $str .= get_http_url() . $link . "\n";
  85. }
  86. }
  87. }
  88. echo $str;
  89. }
  90. }