/concrete/jobs/generate_sitemap.php

https://github.com/rii-J/concrete5-de · PHP · 118 lines · 88 code · 25 blank · 5 comment · 17 complexity · 1a2ec1e0624f0f9c9b30485332317f64 MD5 · raw file

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