Sitemap_Page.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. require_once(PATH::PAGE . 'Page.php');
  3. /**
  4. * Sitemap.xml file page model.
  5. */
  6. class Sitemap_Page extends Page{
  7. /**
  8. * Sitemap contents
  9. */
  10. private $text = '';
  11. /**
  12. * Constructor.
  13. *
  14. * Retrieves the data and populates the text.
  15. */
  16. public function __construct(){
  17. parent::__construct();
  18. $this->set_view('sitemap.php');
  19. // TODO: Don't use static date.
  20. $lastmod = "2023-09-14";
  21. $this->text .= "<?xml version='1.0' encoding='UTF-8'?>
  22. <urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>
  23. <url>
  24. <loc>" . URL::BASE . "</loc>
  25. <lastmod>" . $lastmod ."</lastmod>
  26. <changefreq>monthly</changefreq>
  27. <priority>1.0</priority>
  28. </url>
  29. <url>
  30. <loc>" . URL::BASE . "profile/</loc>
  31. <lastmod>" . $lastmod ."</lastmod>
  32. <changefreq>monthly</changefreq>
  33. <priority>0.9</priority>
  34. </url>
  35. <url>
  36. <loc>" . URL::BASE . "projects/</loc>
  37. <lastmod>" . $lastmod ."</lastmod>
  38. <changefreq>monthly</changefreq>
  39. <priority>0.8</priority>
  40. </url>";
  41. $statement = get_context()->get_db()->prepare(
  42. 'SELECT permalink FROM project WHERE user = :user AND visible = 1 ORDER BY idx'
  43. );
  44. $statement->bindValue(':user', get_context()->get_user()->get_id(), PDO::PARAM_INT);
  45. $statement->execute();
  46. $priority = 0.7;
  47. $priority_dec = 0.3 / $statement->rowcount();
  48. while ($r = $statement->fetch(PDO::FETCH_ASSOC)){
  49. $this->text .= "
  50. <url>
  51. <loc>" . URL::BASE . "projects/" . $r["permalink"]. "</loc>
  52. <lastmod>" . $lastmod ."</lastmod>
  53. <changefreq>monthly</changefreq>
  54. <priority>" . $priority . "</priority>
  55. </url>";
  56. $priority -= $priority_dec;
  57. }
  58. $this->text .= "
  59. </urlset>";
  60. }
  61. /**
  62. * Retrieves the text for the sitemap XML file.
  63. *
  64. * @return string The text for the XML.
  65. */
  66. public function get_text(){return $this->text;}
  67. }