По вопросам переделки, модернизации, создания
и поддержки сайтов звоните
+7 (495) 943-24-89
12.04.2011

Исправление ошибки в генерации sitemap.xml

При генерации файла карты сайта с помощью встроенного в Concrete5 задания в полученной карте, дата последней модификации всех страниц, кроме главной, выставляется некорректно. Это вызывает предупреждения при анализе файла карты сайта поисковыми системами.

Мы провели анализ данной проблемы и результатом явились правки в файле /concrete/jobs/generate_sitemap.php

  1. <?php
  2. /**
  3. *
  4. * Responsible for loading the indexed search class and initiating the reindex command.
  5. * @package Utilities
  6. */
  7.  
  8. defined('C5_EXECUTE') or die("Access Denied.");
  9. class GenerateSitemap extends Job {
  10.  
  11. public function getJobName() {
  12. return t('Generate Sitemap File');
  13. }
  14.  
  15. public function getJobDescription() {
  16. return t("Generate the sitemap.xml file that search engines use to crawl your site.");
  17. }
  18.  
  19. function run() {
  20.  
  21. $ni = Loader::helper('navigation');
  22.  
  23. $xmlFile = DIR_BASE.'/sitemap.xml';
  24. $xmlHead = "<" . "?" . "xml version=\"1.0\" encoding=\"" . APP_CHARSET . "\"?>\n"
  25. ."<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
  26. $home = '';
  27. $c = Page::getByID(1, "ACTIVE");
  28. $changefreq = $c->getAttribute('sitemap_changefreq');
  29. $priority = $c->getAttribute("sitemap_priority");
  30.  
  31. if ($changefreq == '') {
  32. $changefreq = 'weekly';
  33. }
  34. if ($priority == '') {
  35. $priority = '1.0';
  36. }
  37. $home .= "<url>\n";
  38. $home .= "<loc>". BASE_URL.DIR_REL."</loc>\n";
  39. $home .= " <lastmod> " . date('Y-m-d') . "</lastmod>\n";
  40. $home .= " <changefreq>" . $changefreq . "</changefreq>\n";
  41. $home .= " <priority>" . $priority . "</priority>\n";
  42. $home .= "</url>\n";
  43. $xmlFoot = "</urlset>\n";
  44.  
  45. if (!file_exists($xmlFile)) @touch($xmlFile);
  46.  
  47. if (is_writable($xmlFile)) {
  48. if (!$handle = fopen($xmlFile, 'w')) {
  49. throw new Exception(t("Cannot open file %s", $xmlFile));
  50. }
  51.  
  52. fwrite($handle, $xmlHead);
  53. fwrite($handle, $home);
  54. fflush($handle);
  55.  
  56. $db = Loader::db();
  57. $collection_attributes = Loader::model('collection_attributes');
  58. $r = $db->query("select cID from Pages where cID > 1 order by cID asc");
  59. $g = Group::getByID(GUEST_GROUP_ID);
  60. $nh = Loader::helper('navigation');
  61.  
  62. while ($row = $r->fetchRow()) {
  63. $c = Page::getByID($row['cID'], 'ACTIVE');
  64. $g->setPermissionsForObject($c);
  65. if ($c->isSystemPage()) {
  66. continue;
  67. }
  68.  
  69. if($c->getAttribute("exclude_sitemapxml")) {
  70. continue;
  71. }
  72.  
  73. if($c->isExternalLink()) {
  74. continue;
  75. }
  76.  
  77. if ($g->canRead()) {
  78.  
  79. $name = ($c->getCollectionName()) ? $c->getCollectionName() : '(No name)';
  80. $cPath = $ni->getCollectionURL($c);
  81. $changefreq = $c->getAttribute('sitemap_changefreq');
  82. $priority = $c->getAttribute("sitemap_priority");
  83.  
  84. if ($changefreq == '') {
  85. $changefreq = 'weekly';
  86. }
  87. if ($priority == '') {
  88. $priority = '0.' . round(rand(1, 5));
  89. }
  90.  
  91. $vo = $c->getVersionObject();
  92.  
  93. $node = "";
  94. $node .= "<url>\n";
  95. $node .= "<loc>" . $cPath . "</loc>\n";
  96. $node .= " <lastmod>". substr($vo->getVersionDateCreated(), 0, 10)."</lastmod>\n";
  97. $node .= " <changefreq>".$changefreq."</changefreq>\n";
  98. $node .= " <priority>".$priority."</priority>\n";
  99. $node .= "</url>\n";
  100.  
  101. fwrite($handle, $node);
  102. fflush($handle);
  103.  
  104. }
  105. }
  106.  
  107. fwrite($handle, $xmlFoot);
  108. fflush($handle);
  109. fclose($handle);
  110.  
  111. return t("Sitemap XML File Saved.");
  112.  
  113. } else {
  114. throw new Exception(t("The file %s is not writable", $xmlFile));
  115. }
  116. }
  117.  
  118. }
  119.  
  120. ?>

Исправленный файл задания вы можете скачать с нашего сайта. Разархивируйте его и поместите файл generate_sitemap.php в директорию /jobs вашего сайта и он автоматически будет использован при генерации файла карты сайта в слудующий раз.