You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

71 lines
2.4 KiB

  1. <?php
  2. namespace dokuwiki\Sitemap;
  3. /**
  4. * An item of a sitemap.
  5. *
  6. * @author Michael Hamann
  7. */
  8. class Item
  9. {
  10. public $url;
  11. public $lastmod;
  12. public $changefreq;
  13. public $priority;
  14. /**
  15. * Create a new item.
  16. *
  17. * @param string $url The url of the item
  18. * @param int $lastmod Timestamp of the last modification
  19. * @param string $changefreq How frequently the item is likely to change.
  20. * Valid values: always, hourly, daily, weekly, monthly, yearly, never.
  21. * @param $priority float|string The priority of the item relative to other URLs on your site.
  22. * Valid values range from 0.0 to 1.0.
  23. */
  24. public function __construct($url, $lastmod, $changefreq = null, $priority = null)
  25. {
  26. $this->url = $url;
  27. $this->lastmod = $lastmod;
  28. $this->changefreq = $changefreq;
  29. $this->priority = $priority;
  30. }
  31. /**
  32. * Helper function for creating an item for a wikipage id.
  33. *
  34. * @param string $id A wikipage id.
  35. * @param string $changefreq How frequently the item is likely to change.
  36. * Valid values: always, hourly, daily, weekly, monthly, yearly, never.
  37. * @param float|string $priority The priority of the item relative to other URLs on your site.
  38. * Valid values range from 0.0 to 1.0.
  39. * @return Item The sitemap item.
  40. */
  41. public static function createFromID($id, $changefreq = null, $priority = null)
  42. {
  43. $id = trim($id);
  44. $date = @filemtime(wikiFN($id));
  45. if (!$date) return null;
  46. return new Item(wl($id, '', true), $date, $changefreq, $priority);
  47. }
  48. /**
  49. * Get the XML representation of the sitemap item.
  50. *
  51. * @return string The XML representation.
  52. */
  53. public function toXML()
  54. {
  55. $result = ' <url>' . NL
  56. . ' <loc>' . hsc($this->url) . '</loc>' . NL
  57. . ' <lastmod>' . date_iso8601($this->lastmod) . '</lastmod>' . NL;
  58. if ($this->changefreq !== null)
  59. $result .= ' <changefreq>' . hsc($this->changefreq) . '</changefreq>' . NL;
  60. if ($this->priority !== null)
  61. $result .= ' <priority>' . hsc($this->priority) . '</priority>' . NL;
  62. $result .= ' </url>' . NL;
  63. return $result;
  64. }
  65. }