xref: /dokuwiki/vendor/openpsa/universalfeedcreator/lib/Creator/AtomCreator10.php (revision a087da71ea972d05ca659a1e9406c87aa7ae258b)
1<?php
2
3/**
4 * AtomCreator10 is a FeedCreator that implements the atom specification,
5 * as in http://www.atomenabled.org/developers/syndication/atom-format-spec.php
6 * Please note that just by using AtomCreator10 you won't automatically
7 * produce valid atom files. For example, you have to specify either an editor
8 * for the feed or an author for every single feed item.
9 * Some elements have not been implemented yet. These are (incomplete list):
10 * author URL, item author's email and URL, item contents, alternate links,
11 * other link content types than text/html. Some of them may be created with
12 * AtomCreator10::additionalElements.
13 *
14 * @see     FeedCreator#additionalElements
15 * @since   1.7.2-mod (modified)
16 * @author  Mohammad Hafiz Ismail (mypapit@gmail.com)
17 * @package de.bitfolge.feedcreator
18 */
19class AtomCreator10 extends FeedCreator
20{
21
22    /**
23     * AtomCreator10 constructor.
24     */
25    public function __construct()
26    {
27        $this->contentType = "application/atom+xml";
28        $this->encoding = "utf-8";
29    }
30
31    /** @inheritdoc */
32    public function createFeed()
33    {
34        $feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
35        $feed .= $this->_createGeneratorComment();
36        $feed .= $this->_createStylesheetReferences();
37        $feed .= "<feed xmlns=\"http://www.w3.org/2005/Atom\"";
38        if (!empty($this->items[0]->lat)) {
39            $feed .= " xmlns:georss=\"http://www.georss.org/georss\"\n";
40        }
41        if ($this->language != "") {
42            $feed .= " xml:lang=\"".$this->language."\"";
43        }
44        $feed .= ">\n";
45        $feed .= "    <title>".htmlspecialchars($this->title)."</title>\n";
46        $feed .= "    <subtitle>".htmlspecialchars($this->description)."</subtitle>\n";
47        $feed .= "    <link rel=\"alternate\" type=\"text/html\" href=\"".htmlspecialchars($this->link)."\"/>\n";
48        $feed .= "    <id>".htmlspecialchars($this->link)."</id>\n";
49        $now = new FeedDate();
50        $feed .= "    <updated>".htmlspecialchars($now->iso8601())."</updated>\n";
51        if ($this->editor != "") {
52            $feed .= "    <author>\n";
53            $feed .= "        <name>".$this->editor."</name>\n";
54            if ($this->editorEmail != "") {
55                $feed .= "        <email>".$this->editorEmail."</email>\n";
56            }
57            $feed .= "    </author>\n";
58        }
59        if ($this->category != "") {
60
61            $feed .= "    <category term=\"".htmlspecialchars($this->category)."\" />\n";
62        }
63        if ($this->copyright != "") {
64            $feed .= "    <rights>".FeedCreator::iTrunc(htmlspecialchars($this->copyright), 100)."</rights>\n";
65        }
66        $feed .= "    <generator>".$this->version()."</generator>\n";
67
68        $feed .= "    <link rel=\"self\" type=\"application/atom+xml\" href=\"".htmlspecialchars(
69                $this->syndicationURL
70            )."\" />\n";
71        $feed .= $this->_createAdditionalElements($this->additionalElements, "    ");
72        for ($i = 0; $i < count($this->items); $i++) {
73            $feed .= "    <entry>\n";
74            $feed .= "        <title>".htmlspecialchars(strip_tags($this->items[$i]->title))."</title>\n";
75            $feed .= "        <link rel=\"alternate\" type=\"text/html\" href=\"".htmlspecialchars(
76                    $this->items[$i]->link
77                )."\"/>\n";
78            if ($this->items[$i]->date == "") {
79                $this->items[$i]->date = time();
80            }
81            $itemDate = new FeedDate($this->items[$i]->date);
82            $feed .= "        <published>".htmlspecialchars($itemDate->iso8601())."</published>\n";
83            $feed .= "        <updated>".htmlspecialchars($itemDate->iso8601())."</updated>\n";
84
85            $tempguid = $this->items[$i]->link;
86            if ($this->items[$i]->guid != "") {
87                $tempguid = $this->items[$i]->guid;
88            }
89
90            $feed .= "        <id>".htmlspecialchars($tempguid)."</id>\n";
91            $feed .= $this->_createAdditionalElements($this->items[$i]->additionalElements, "        ");
92            if ($this->items[$i]->author != "") {
93                $feed .= "        <author>\n";
94                $feed .= "            <name>".htmlspecialchars($this->items[$i]->author)."</name>\n";
95                if ($this->items[$i]->authorEmail != "") {
96                    $feed .= "            <email>".htmlspecialchars($this->items[$i]->authorEmail)."</email>\n";
97                }
98
99                if ($this->items[$i]->authorURL != "") {
100                    $feed .= "            <uri>".htmlspecialchars($this->items[$i]->authorURL)."</uri>\n";
101                }
102
103                $feed .= "        </author>\n";
104            }
105
106            if ($this->items[$i]->category != "") {
107                $feed .= "        <category ";
108
109                if ($this->items[$i]->categoryScheme != "") {
110                    $feed .= " scheme=\"".htmlspecialchars($this->items[$i]->categoryScheme)."\" ";
111                }
112
113                $feed .= " term=\"".htmlspecialchars($this->items[$i]->category)."\" />\n";
114            }
115
116            if ($this->items[$i]->description != "") {
117
118                /*
119                 * ATOM should have at least summary tag, however this implementation may be inaccurate
120                */
121                $tempdesc = $this->items[$i]->getDescription();
122                $temptype = "";
123
124                if ($this->items[$i]->descriptionHtmlSyndicated) {
125                    $temptype = " type=\"html\"";
126                    $tempdesc = $this->items[$i]->getDescription();
127
128                }
129
130                if (empty($this->items[$i]->descriptionTruncSize)) {
131                    $feed .= "        <content".$temptype.">".$tempdesc."</content>\n";
132                }
133
134                $feed .= "        <summary".$temptype.">".$tempdesc."</summary>\n";
135            } else {
136
137                $feed .= "     <summary>no summary</summary>\n";
138
139            }
140
141            if ($this->items[$i]->enclosure != null) {
142                $feed .= "        <link rel=\"enclosure\" href=\"".$this->items[$i]->enclosure->url."\" type=\"".$this->items[$i]->enclosure->type."\"  length=\"".$this->items[$i]->enclosure->length."\"";
143
144                if ($this->items[$i]->enclosure->language != "") {
145                    $feed .= " xml:lang=\"".$this->items[$i]->enclosure->language."\" ";
146                }
147
148                if ($this->items[$i]->enclosure->title != "") {
149                    $feed .= " title=\"".$this->items[$i]->enclosure->title."\" ";
150                }
151
152                $feed .= " /> \n";
153            }
154            if ($this->items[$i]->lat != "") {
155                $feed .= "        <georss:point>".$this->items[$i]->lat." ".$this->items[$i]->long."</georss:point>\n";
156            }
157            $feed .= "    </entry>\n";
158        }
159        $feed .= "</feed>\n";
160
161        return $feed;
162    }
163}
164