xref: /dokuwiki/vendor/openpsa/universalfeedcreator/lib/Creator/RSSCreator091.php (revision 3dc2d50c5fda9c4bf708ff4c26e266ba239af62c)
1<?php
2
3/**
4 * RSSCreator091 is a FeedCreator that implements RSS 0.91 Spec, revision 3.
5 *
6 * @see     http://my.netscape.com/publish/formats/rss-spec-0.91.html
7 * @since   1.3
8 * @author  Kai Blankenhorn <kaib@bitfolge.de>
9 * @package de.bitfolge.feedcreator
10 */
11class RSSCreator091 extends FeedCreator
12{
13
14    /** @var string Stores this RSS feed's version number. */
15    protected $RSSVersion;
16
17    /**
18     * RSSCreator091 constructor.
19     */
20    function __construct()
21    {
22        $this->_setRSSVersion("0.91");
23        $this->contentType = "application/rss+xml";
24    }
25
26    /**
27     * Sets this RSS feed's version number.
28     *
29     * @param string $version
30     */
31    protected function _setRSSVersion($version)
32    {
33        $this->RSSVersion = $version;
34    }
35
36    /** @inheritdoc */
37    public function createFeed()
38    {
39        $feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
40        $feed .= $this->_createGeneratorComment();
41        $feed .= $this->_createStylesheetReferences();
42        $feed .= "<rss version=\"".$this->RSSVersion."\"";
43
44        if (count($this->items) > 0
45            && !empty($this->items[0]->lat)
46        ) {
47            $feed .= "    xmlns:georss=\"http://www.georss.org/georss/\"\n";
48        }
49        if (count($this->items) > 0
50            && isset($this->items[0]->additionalElements['xcal:dtstart'])
51        ) {
52            $feed .= "    xmlns:xcal=\"urn:ietf:params:xml:ns:xcal\"\n";
53        }
54        $feed .= ">\n";
55        if ($this->format == 'BASE') {
56            $feed .= "    <channel xmlns:g=\"http://base.google.com/ns/1.0\">\n";
57        } else {
58            $feed .= "    <channel>\n";
59        }
60        $feed .= "        <title>".FeedCreator::iTrunc(htmlspecialchars($this->title), 100)."</title>\n";
61        $this->descriptionTruncSize = 500;
62        $feed .= "        <description>".$this->getDescription()."</description>\n";
63        $feed .= "        <link>".$this->link."</link>\n";
64        $now = new FeedDate();
65        $feed .= "        <lastBuildDate>".htmlspecialchars(
66                $this->lastBuildDate ?: $now->rfc822()
67            )."</lastBuildDate>\n";
68        $feed .= "        <generator>".FEEDCREATOR_VERSION."</generator>\n";
69
70        if ($this->image != null) {
71            $feed .= "        <image>\n";
72            $feed .= "            <url>".$this->image->url."</url>\n";
73            $feed .= "            <title>".FeedCreator::iTrunc(htmlspecialchars($this->image->title), 100)."</title>\n";
74            $feed .= "            <link>".$this->image->link."</link>\n";
75            if ($this->image->width != "") {
76                $feed .= "            <width>".$this->image->width."</width>\n";
77            }
78            if ($this->image->height != "") {
79                $feed .= "            <height>".$this->image->height."</height>\n";
80            }
81            if ($this->image->description != "") {
82                $feed .= "            <description>".htmlspecialchars($this->image->description)."</description>\n";
83            }
84            $feed .= "        </image>\n";
85        }
86        if ($this->language != "") {
87            $feed .= "        <language>".$this->language."</language>\n";
88        }
89        if ($this->copyright != "") {
90            $feed .= "        <copyright>".FeedCreator::iTrunc(
91                    htmlspecialchars($this->copyright),
92                    100
93                )."</copyright>\n";
94        }
95        if ($this->editor != "") {
96            $feed .= "        <managingEditor>".FeedCreator::iTrunc(
97                    htmlspecialchars($this->editor),
98                    100
99                )."</managingEditor>\n";
100        }
101        if ($this->webmaster != "") {
102            $feed .= "        <webMaster>".FeedCreator::iTrunc(
103                    htmlspecialchars($this->webmaster),
104                    100
105                )."</webMaster>\n";
106        }
107        if ($this->pubDate != "") {
108            $pubDate = new FeedDate($this->pubDate);
109            $feed .= "        <pubDate>".htmlspecialchars($pubDate->rfc822())."</pubDate>\n";
110        }
111        if ($this->category != "") {
112            $feed .= "        <category>".htmlspecialchars($this->category)."</category>\n";
113        }
114        if ($this->docs != "") {
115            $feed .= "        <docs>".FeedCreator::iTrunc(htmlspecialchars($this->docs), 500)."</docs>\n";
116        }
117        if ($this->ttl != "") {
118            $feed .= "        <ttl>".htmlspecialchars($this->ttl)."</ttl>\n";
119        }
120        if ($this->rating != "") {
121            $feed .= "        <rating>".FeedCreator::iTrunc(htmlspecialchars($this->rating), 500)."</rating>\n";
122        }
123        if ($this->skipHours != "") {
124            $feed .= "        <skipHours>".htmlspecialchars($this->skipHours)."</skipHours>\n";
125        }
126        if ($this->skipDays != "") {
127            $feed .= "        <skipDays>".htmlspecialchars($this->skipDays)."</skipDays>\n";
128        }
129        $feed .= $this->_createAdditionalElements($this->additionalElements, "    ");
130
131        for ($i = 0; $i < count($this->items); $i++) {
132            $feed .= "        <item>\n";
133            $feed .= "            <title>".FeedCreator::iTrunc(
134                    htmlspecialchars(strip_tags($this->items[$i]->title)),
135                    100
136                )."</title>\n";
137            $feed .= "            <link>".htmlspecialchars($this->items[$i]->link)."</link>\n";
138            $feed .= "            <description>".$this->items[$i]->getDescription()."</description>\n";
139
140            $creator = $this->getAuthor($this->items[$i]->author, $this->items[$i]->authorEmail);
141            if ($creator) {
142                $feed .= "            <author>".htmlspecialchars($creator)."</author>\n";
143            }
144
145            /*
146             // on hold
147            if ($this->items[$i]->source!="") {
148            $feed.= "            <source>".htmlspecialchars($this->items[$i]->source)."</source>\n";
149            }
150            */
151            if ($this->items[$i]->lat != "") {
152                $feed .= "            <georss:point>".$this->items[$i]->lat." ".$this->items[$i]->long."</georss:point>\n";
153            }
154            if (is_array($this->items[$i]->category)) {
155                foreach ($this->items[$i]->category as $cat) {
156                    $feed .= "        <category>".htmlspecialchars($cat)."</category>\n";
157                }
158            } else {
159                if ($this->items[$i]->category != "") {
160                    $feed .= "        <category>".htmlspecialchars($this->items[$i]->category)."</category>\n";
161                }
162            }
163            if ($this->items[$i]->comments != "") {
164                $feed .= "            <comments>".htmlspecialchars($this->items[$i]->comments)."</comments>\n";
165            }
166            if ($this->items[$i]->date != "") {
167                $itemDate = new FeedDate($this->items[$i]->date);
168                $feed .= "            <pubDate>".htmlspecialchars($itemDate->rfc822())."</pubDate>\n";
169            }
170            if ($this->items[$i]->guid != "") {
171                $feed .= "            <guid>".htmlspecialchars($this->items[$i]->guid)."</guid>\n";
172            }
173            if ($this->items[$i]->thumb != "") {
174                $feed .= "            <g:image_link>".htmlspecialchars($this->items[$i]->thumb)."</g:image_link>\n";
175            }
176            $feed .= $this->_createAdditionalElements($this->items[$i]->additionalElements, "            ");
177            $feed .= "        </item>\n";
178        }
179        $feed .= "    </channel>\n";
180        $feed .= "</rss>\n";
181
182        return $feed;
183    }
184
185    /**
186     * Compose the RSS-0.91 and RSS-2.0 author field.
187     *
188     * @author Joe Lapp <joe.lapp@pobox.com>
189     */
190    function getAuthor($author, $email)
191    {
192        if ($author && $email) {
193            return $email.' ('.$author.')';
194        }
195
196        return $email;
197    }
198}
199