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