xref: /dokuwiki/inc/Sitemap/Item.php (revision dccd6b2bba7367e4d1d2d7aa84c9f9d15584b593)
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    {
29        $this->url = $url;
30        $this->lastmod = $lastmod;
31        $this->changefreq = $changefreq;
32        $this->priority = $priority;
33    }
34
35    /**
36     * Helper function for creating an item for a wikipage id.
37     *
38     * @param string       $id         A wikipage id.
39     * @param string       $changefreq How frequently the item is likely to change.
40     *                                 Valid values: always, hourly, daily, weekly, monthly, yearly, never.
41     * @param float|string $priority   The priority of the item relative to other URLs on your site.
42     *                                 Valid values range from 0.0 to 1.0.
43     * @return Item The sitemap item.
44     */
45    public static function createFromID($id, $changefreq = null, $priority = null)
46    {
47        $id = trim($id);
48        $date = @filemtime(wikiFN($id));
49        if(!$date) return null;
50        return new Item(wl($id, '', true), $date, $changefreq, $priority);
51    }
52
53    /**
54     * Get the XML representation of the sitemap item.
55     *
56     * @return string The XML representation.
57     */
58    public function toXML()
59    {
60        $result = '  <url>'.NL
61            .'    <loc>'.hsc($this->url).'</loc>'.NL
62            .'    <lastmod>'.date_iso8601($this->lastmod).'</lastmod>'.NL;
63        if ($this->changefreq !== null)
64            $result .= '    <changefreq>'.hsc($this->changefreq).'</changefreq>'.NL;
65        if ($this->priority !== null)
66            $result .= '    <priority>'.hsc($this->priority).'</priority>'.NL;
67        $result .= '  </url>'.NL;
68        return $result;
69    }
70}
71