xref: /dokuwiki/vendor/openpsa/universalfeedcreator/lib/Creator/AtomCreator10.php (revision 64d8abdb11b92482cec4f0ec0320b91fea20322f)
1572dd708SAndreas Gohr<?php
2572dd708SAndreas Gohr
3572dd708SAndreas Gohr/**
4572dd708SAndreas Gohr * AtomCreator10 is a FeedCreator that implements the atom specification,
5572dd708SAndreas Gohr * as in http://www.atomenabled.org/developers/syndication/atom-format-spec.php
6572dd708SAndreas Gohr * Please note that just by using AtomCreator10 you won't automatically
7572dd708SAndreas Gohr * produce valid atom files. For example, you have to specify either an editor
8572dd708SAndreas Gohr * for the feed or an author for every single feed item.
9572dd708SAndreas Gohr * Some elements have not been implemented yet. These are (incomplete list):
10572dd708SAndreas Gohr * author URL, item author's email and URL, item contents, alternate links,
11572dd708SAndreas Gohr * other link content types than text/html. Some of them may be created with
12572dd708SAndreas Gohr * AtomCreator10::additionalElements.
13572dd708SAndreas Gohr *
14572dd708SAndreas Gohr * @see     FeedCreator#additionalElements
15572dd708SAndreas Gohr * @since   1.7.2-mod (modified)
16572dd708SAndreas Gohr * @author  Mohammad Hafiz Ismail (mypapit@gmail.com)
17572dd708SAndreas Gohr */
18572dd708SAndreas Gohrclass AtomCreator10 extends FeedCreator
19572dd708SAndreas Gohr{
20572dd708SAndreas Gohr
21572dd708SAndreas Gohr    /**
22572dd708SAndreas Gohr     * AtomCreator10 constructor.
23572dd708SAndreas Gohr     */
24572dd708SAndreas Gohr    public function __construct()
25572dd708SAndreas Gohr    {
26572dd708SAndreas Gohr        $this->contentType = "application/atom+xml";
27572dd708SAndreas Gohr        $this->encoding = "utf-8";
28572dd708SAndreas Gohr    }
29572dd708SAndreas Gohr
30572dd708SAndreas Gohr    /** @inheritdoc */
31572dd708SAndreas Gohr    public function createFeed()
32572dd708SAndreas Gohr    {
33572dd708SAndreas Gohr        $feed = "<?xml version=\"1.0\" encoding=\"".$this->encoding."\"?>\n";
34572dd708SAndreas Gohr        $feed .= $this->_createGeneratorComment();
35572dd708SAndreas Gohr        $feed .= $this->_createStylesheetReferences();
36572dd708SAndreas Gohr        $feed .= "<feed xmlns=\"http://www.w3.org/2005/Atom\"";
37572dd708SAndreas Gohr        if (!empty($this->items[0]->lat)) {
38572dd708SAndreas Gohr            $feed .= " xmlns:georss=\"http://www.georss.org/georss\"\n";
39572dd708SAndreas Gohr        }
40572dd708SAndreas Gohr        if ($this->language != "") {
41572dd708SAndreas Gohr            $feed .= " xml:lang=\"".$this->language."\"";
42572dd708SAndreas Gohr        }
43572dd708SAndreas Gohr        $feed .= ">\n";
44*64d8abdbSAndreas Gohr        $feed .= "    <title>".htmlspecialchars((string) $this->title)."</title>\n";
45572dd708SAndreas Gohr        $feed .= "    <subtitle>".htmlspecialchars($this->description)."</subtitle>\n";
46*64d8abdbSAndreas Gohr        $feed .= "    <link rel=\"alternate\" type=\"text/html\" href=\"".htmlspecialchars((string) $this->link)."\"/>\n";
47*64d8abdbSAndreas Gohr        $feed .= "    <id>".htmlspecialchars((string) $this->link)."</id>\n";
48572dd708SAndreas Gohr        $now = new FeedDate();
49572dd708SAndreas Gohr        $feed .= "    <updated>".htmlspecialchars($now->iso8601())."</updated>\n";
50572dd708SAndreas Gohr        if ($this->editor != "") {
51572dd708SAndreas Gohr            $feed .= "    <author>\n";
52572dd708SAndreas Gohr            $feed .= "        <name>".$this->editor."</name>\n";
53572dd708SAndreas Gohr            if ($this->editorEmail != "") {
54572dd708SAndreas Gohr                $feed .= "        <email>".$this->editorEmail."</email>\n";
55572dd708SAndreas Gohr            }
56572dd708SAndreas Gohr            $feed .= "    </author>\n";
57572dd708SAndreas Gohr        }
58572dd708SAndreas Gohr        if ($this->category != "") {
59572dd708SAndreas Gohr
60572dd708SAndreas Gohr            $feed .= "    <category term=\"".htmlspecialchars($this->category)."\" />\n";
61572dd708SAndreas Gohr        }
62572dd708SAndreas Gohr        if ($this->copyright != "") {
63572dd708SAndreas Gohr            $feed .= "    <rights>".FeedCreator::iTrunc(htmlspecialchars($this->copyright), 100)."</rights>\n";
64572dd708SAndreas Gohr        }
65572dd708SAndreas Gohr        $feed .= "    <generator>".$this->version()."</generator>\n";
66572dd708SAndreas Gohr
67572dd708SAndreas Gohr        $feed .= "    <link rel=\"self\" type=\"application/atom+xml\" href=\"".htmlspecialchars(
68*64d8abdbSAndreas Gohr                (string) $this->syndicationURL
69572dd708SAndreas Gohr            )."\" />\n";
70572dd708SAndreas Gohr        $feed .= $this->_createAdditionalElements($this->additionalElements, "    ");
71572dd708SAndreas Gohr        for ($i = 0; $i < count($this->items); $i++) {
72572dd708SAndreas Gohr            $feed .= "    <entry>\n";
73*64d8abdbSAndreas Gohr            $feed .= "        <title>".htmlspecialchars(strip_tags((string) $this->items[$i]->title))."</title>\n";
74572dd708SAndreas Gohr            $feed .= "        <link rel=\"alternate\" type=\"text/html\" href=\"".htmlspecialchars(
75*64d8abdbSAndreas Gohr                    (string) $this->items[$i]->link
76572dd708SAndreas Gohr                )."\"/>\n";
77572dd708SAndreas Gohr            if ($this->items[$i]->date == "") {
78572dd708SAndreas Gohr                $this->items[$i]->date = time();
79572dd708SAndreas Gohr            }
80572dd708SAndreas Gohr            $itemDate = new FeedDate($this->items[$i]->date);
81572dd708SAndreas Gohr            $feed .= "        <published>".htmlspecialchars($itemDate->iso8601())."</published>\n";
82572dd708SAndreas Gohr            $feed .= "        <updated>".htmlspecialchars($itemDate->iso8601())."</updated>\n";
83572dd708SAndreas Gohr
84572dd708SAndreas Gohr            $tempguid = $this->items[$i]->link;
85572dd708SAndreas Gohr            if ($this->items[$i]->guid != "") {
86572dd708SAndreas Gohr                $tempguid = $this->items[$i]->guid;
87572dd708SAndreas Gohr            }
88572dd708SAndreas Gohr
89*64d8abdbSAndreas Gohr            $feed .= "        <id>".htmlspecialchars((string) $tempguid)."</id>\n";
90572dd708SAndreas Gohr            $feed .= $this->_createAdditionalElements($this->items[$i]->additionalElements, "        ");
91572dd708SAndreas Gohr            if ($this->items[$i]->author != "") {
92572dd708SAndreas Gohr                $feed .= "        <author>\n";
93572dd708SAndreas Gohr                $feed .= "            <name>".htmlspecialchars($this->items[$i]->author)."</name>\n";
94572dd708SAndreas Gohr                if ($this->items[$i]->authorEmail != "") {
95572dd708SAndreas Gohr                    $feed .= "            <email>".htmlspecialchars($this->items[$i]->authorEmail)."</email>\n";
96572dd708SAndreas Gohr                }
97572dd708SAndreas Gohr
98572dd708SAndreas Gohr                if ($this->items[$i]->authorURL != "") {
99572dd708SAndreas Gohr                    $feed .= "            <uri>".htmlspecialchars($this->items[$i]->authorURL)."</uri>\n";
100572dd708SAndreas Gohr                }
101572dd708SAndreas Gohr
102572dd708SAndreas Gohr                $feed .= "        </author>\n";
103572dd708SAndreas Gohr            }
104572dd708SAndreas Gohr
105e43cd7e1SAndreas Gohr            if (!empty($this->items[$i]->category)) {
106e43cd7e1SAndreas Gohr                foreach ((array) $this->items[$i]->category as $category) {
107572dd708SAndreas Gohr                    $feed .= "        <category ";
108572dd708SAndreas Gohr
109572dd708SAndreas Gohr                    if ($this->items[$i]->categoryScheme != "") {
110572dd708SAndreas Gohr                        $feed .= " scheme=\"".htmlspecialchars($this->items[$i]->categoryScheme)."\" ";
111572dd708SAndreas Gohr                    }
112572dd708SAndreas Gohr
113e43cd7e1SAndreas Gohr                    $feed .= " term=\"".htmlspecialchars($category)."\" />\n";
114e43cd7e1SAndreas Gohr                }
115572dd708SAndreas Gohr            }
116572dd708SAndreas Gohr
117572dd708SAndreas Gohr            if ($this->items[$i]->description != "") {
118572dd708SAndreas Gohr
119572dd708SAndreas Gohr                /*
120572dd708SAndreas Gohr                 * ATOM should have at least summary tag, however this implementation may be inaccurate
121572dd708SAndreas Gohr                */
122572dd708SAndreas Gohr                $tempdesc = $this->items[$i]->getDescription();
123572dd708SAndreas Gohr                $temptype = "";
124572dd708SAndreas Gohr
125572dd708SAndreas Gohr                if ($this->items[$i]->descriptionHtmlSyndicated) {
126572dd708SAndreas Gohr                    $temptype = " type=\"html\"";
127572dd708SAndreas Gohr                    $tempdesc = $this->items[$i]->getDescription();
128572dd708SAndreas Gohr
129572dd708SAndreas Gohr                }
130572dd708SAndreas Gohr
131572dd708SAndreas Gohr                if (empty($this->items[$i]->descriptionTruncSize)) {
132572dd708SAndreas Gohr                    $feed .= "        <content".$temptype.">".$tempdesc."</content>\n";
133572dd708SAndreas Gohr                }
134572dd708SAndreas Gohr
135572dd708SAndreas Gohr                $feed .= "        <summary".$temptype.">".$tempdesc."</summary>\n";
136572dd708SAndreas Gohr            } else {
137572dd708SAndreas Gohr
138572dd708SAndreas Gohr                $feed .= "     <summary>no summary</summary>\n";
139572dd708SAndreas Gohr
140572dd708SAndreas Gohr            }
141572dd708SAndreas Gohr
142572dd708SAndreas Gohr            if ($this->items[$i]->enclosure != null) {
143572dd708SAndreas Gohr                $feed .= "        <link rel=\"enclosure\" href=\"".$this->items[$i]->enclosure->url."\" type=\"".$this->items[$i]->enclosure->type."\"  length=\"".$this->items[$i]->enclosure->length."\"";
144572dd708SAndreas Gohr
145572dd708SAndreas Gohr                if ($this->items[$i]->enclosure->language != "") {
146572dd708SAndreas Gohr                    $feed .= " xml:lang=\"".$this->items[$i]->enclosure->language."\" ";
147572dd708SAndreas Gohr                }
148572dd708SAndreas Gohr
149572dd708SAndreas Gohr                if ($this->items[$i]->enclosure->title != "") {
150572dd708SAndreas Gohr                    $feed .= " title=\"".$this->items[$i]->enclosure->title."\" ";
151572dd708SAndreas Gohr                }
152572dd708SAndreas Gohr
153572dd708SAndreas Gohr                $feed .= " /> \n";
154572dd708SAndreas Gohr            }
155572dd708SAndreas Gohr            if ($this->items[$i]->lat != "") {
156572dd708SAndreas Gohr                $feed .= "        <georss:point>".$this->items[$i]->lat." ".$this->items[$i]->long."</georss:point>\n";
157572dd708SAndreas Gohr            }
158572dd708SAndreas Gohr            $feed .= "    </entry>\n";
159572dd708SAndreas Gohr        }
160572dd708SAndreas Gohr        $feed .= "</feed>\n";
161572dd708SAndreas Gohr
162572dd708SAndreas Gohr        return $feed;
163572dd708SAndreas Gohr    }
164572dd708SAndreas Gohr}
165