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