Item.php 2.3 KB

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