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