xref: /dokuwiki/inc/Sitemap/Item.php (revision 8c7c53b0321a3cd3116b8d3b2ad27863a38dece7)
1432adb37SAndreas Gohr<?php
2432adb37SAndreas Gohr
3432adb37SAndreas Gohrnamespace dokuwiki\Sitemap;
4432adb37SAndreas Gohr
5432adb37SAndreas Gohr/**
6432adb37SAndreas Gohr * An item of a sitemap.
7432adb37SAndreas Gohr *
8432adb37SAndreas Gohr * @author Michael Hamann
9432adb37SAndreas Gohr */
10*8c7c53b0SAndreas Gohrclass Item
11*8c7c53b0SAndreas Gohr{
12432adb37SAndreas Gohr    public $url;
13432adb37SAndreas Gohr    public $lastmod;
14432adb37SAndreas Gohr    public $changefreq;
15432adb37SAndreas Gohr    public $priority;
16432adb37SAndreas Gohr
17432adb37SAndreas Gohr    /**
18432adb37SAndreas Gohr     * Create a new item.
19432adb37SAndreas Gohr     *
20432adb37SAndreas Gohr     * @param string $url        The url of the item
21432adb37SAndreas Gohr     * @param int    $lastmod    Timestamp of the last modification
22432adb37SAndreas Gohr     * @param string $changefreq How frequently the item is likely to change.
23432adb37SAndreas Gohr     *                           Valid values: always, hourly, daily, weekly, monthly, yearly, never.
24432adb37SAndreas Gohr     * @param $priority float|string The priority of the item relative to other URLs on your site.
25432adb37SAndreas Gohr     *                           Valid values range from 0.0 to 1.0.
26432adb37SAndreas Gohr     */
27432adb37SAndreas Gohr    public function __construct($url, $lastmod, $changefreq = null, $priority = null) {
28432adb37SAndreas Gohr        $this->url = $url;
29432adb37SAndreas Gohr        $this->lastmod = $lastmod;
30432adb37SAndreas Gohr        $this->changefreq = $changefreq;
31432adb37SAndreas Gohr        $this->priority = $priority;
32432adb37SAndreas Gohr    }
33432adb37SAndreas Gohr
34432adb37SAndreas Gohr    /**
35432adb37SAndreas Gohr     * Helper function for creating an item for a wikipage id.
36432adb37SAndreas Gohr     *
37432adb37SAndreas Gohr     * @param string       $id         A wikipage id.
38432adb37SAndreas Gohr     * @param string       $changefreq How frequently the item is likely to change.
39432adb37SAndreas Gohr     *                                 Valid values: always, hourly, daily, weekly, monthly, yearly, never.
40432adb37SAndreas Gohr     * @param float|string $priority   The priority of the item relative to other URLs on your site.
41432adb37SAndreas Gohr     *                                 Valid values range from 0.0 to 1.0.
42432adb37SAndreas Gohr     * @return Item The sitemap item.
43432adb37SAndreas Gohr     */
44432adb37SAndreas Gohr    public static function createFromID($id, $changefreq = null, $priority = null) {
45432adb37SAndreas Gohr        $id = trim($id);
46432adb37SAndreas Gohr        $date = @filemtime(wikiFN($id));
47432adb37SAndreas Gohr        if(!$date) return null;
48432adb37SAndreas Gohr        return new Item(wl($id, '', true), $date, $changefreq, $priority);
49432adb37SAndreas Gohr    }
50432adb37SAndreas Gohr
51432adb37SAndreas Gohr    /**
52432adb37SAndreas Gohr     * Get the XML representation of the sitemap item.
53432adb37SAndreas Gohr     *
54432adb37SAndreas Gohr     * @return string The XML representation.
55432adb37SAndreas Gohr     */
56432adb37SAndreas Gohr    public function toXML() {
57432adb37SAndreas Gohr        $result = '  <url>'.NL
58432adb37SAndreas Gohr            .'    <loc>'.hsc($this->url).'</loc>'.NL
59432adb37SAndreas Gohr            .'    <lastmod>'.date_iso8601($this->lastmod).'</lastmod>'.NL;
60432adb37SAndreas Gohr        if ($this->changefreq !== null)
61432adb37SAndreas Gohr            $result .= '    <changefreq>'.hsc($this->changefreq).'</changefreq>'.NL;
62432adb37SAndreas Gohr        if ($this->priority !== null)
63432adb37SAndreas Gohr            $result .= '    <priority>'.hsc($this->priority).'</priority>'.NL;
64432adb37SAndreas Gohr        $result .= '  </url>'.NL;
65432adb37SAndreas Gohr        return $result;
66432adb37SAndreas Gohr    }
67432adb37SAndreas Gohr}
68