xref: /dokuwiki/vendor/openpsa/universalfeedcreator/lib/UniversalFeedCreator.php (revision 64d8abdb11b92482cec4f0ec0320b91fea20322f)
1572dd708SAndreas Gohr<?php
2572dd708SAndreas Gohr/**
3572dd708SAndreas Gohr * UniversalFeedCreator lets you choose during runtime which
4572dd708SAndreas Gohr * format to build.
5572dd708SAndreas Gohr * For general usage of a feed class, see the FeedCreator class
6572dd708SAndreas Gohr * below or the example above.
7572dd708SAndreas Gohr *
8572dd708SAndreas Gohr * @since   1.3
9572dd708SAndreas Gohr * @author  Kai Blankenhorn <kaib@bitfolge.de>
10572dd708SAndreas Gohr */
11572dd708SAndreas Gohrclass UniversalFeedCreator extends FeedCreator
12572dd708SAndreas Gohr{
13572dd708SAndreas Gohr    /** @var FeedCreator */
14572dd708SAndreas Gohr    protected $_feed;
15572dd708SAndreas Gohr
16572dd708SAndreas Gohr    /**
17572dd708SAndreas Gohr     * @param string $format
18572dd708SAndreas Gohr     */
19572dd708SAndreas Gohr    protected function _setFormat($format)
20572dd708SAndreas Gohr    {
21*64d8abdbSAndreas Gohr        switch (strtoupper((string) $format)) {
22572dd708SAndreas Gohr
23572dd708SAndreas Gohr            case "BASE":
24572dd708SAndreas Gohr                $this->format = $format;
25572dd708SAndreas Gohr            case "2.0":
26572dd708SAndreas Gohr                // fall through
27572dd708SAndreas Gohr            case "RSS2.0":
28572dd708SAndreas Gohr                $this->_feed = new RSSCreator20();
29572dd708SAndreas Gohr                break;
30572dd708SAndreas Gohr
31572dd708SAndreas Gohr            case "GEOPHOTORSS":
32572dd708SAndreas Gohr            case "PHOTORSS":
33572dd708SAndreas Gohr            case "GEORSS":
34572dd708SAndreas Gohr                $this->format = $format;
35572dd708SAndreas Gohr            case "1.0":
36572dd708SAndreas Gohr                // fall through
37572dd708SAndreas Gohr            case "RSS1.0":
38572dd708SAndreas Gohr                $this->_feed = new RSSCreator10();
39572dd708SAndreas Gohr                break;
40572dd708SAndreas Gohr
41572dd708SAndreas Gohr            case "0.91":
42572dd708SAndreas Gohr                // fall through
43572dd708SAndreas Gohr            case "RSS0.91":
44572dd708SAndreas Gohr                $this->_feed = new RSSCreator091();
45572dd708SAndreas Gohr                break;
46572dd708SAndreas Gohr
47572dd708SAndreas Gohr            case "PIE0.1":
48572dd708SAndreas Gohr                $this->_feed = new PIECreator01();
49572dd708SAndreas Gohr                break;
50572dd708SAndreas Gohr
51572dd708SAndreas Gohr            case "MBOX":
52572dd708SAndreas Gohr                $this->_feed = new MBOXCreator();
53572dd708SAndreas Gohr                break;
54572dd708SAndreas Gohr
55572dd708SAndreas Gohr            case "OPML":
56572dd708SAndreas Gohr                $this->_feed = new OPMLCreator();
57572dd708SAndreas Gohr                break;
58572dd708SAndreas Gohr
59572dd708SAndreas Gohr            case "TOOLBAR":
60572dd708SAndreas Gohr                $this->format = $format;
61572dd708SAndreas Gohr
62572dd708SAndreas Gohr            case "ATOM":
63572dd708SAndreas Gohr                // fall through: always the latest ATOM version
64572dd708SAndreas Gohr            case "ATOM1.0":
65572dd708SAndreas Gohr                $this->_feed = new AtomCreator10();
66572dd708SAndreas Gohr                break;
67572dd708SAndreas Gohr
68572dd708SAndreas Gohr            case "ATOM0.3":
69572dd708SAndreas Gohr                $this->_feed = new AtomCreator03();
70572dd708SAndreas Gohr                break;
71572dd708SAndreas Gohr
72572dd708SAndreas Gohr            case "HTML":
73572dd708SAndreas Gohr                $this->_feed = new HTMLCreator();
74572dd708SAndreas Gohr                break;
75572dd708SAndreas Gohr
76572dd708SAndreas Gohr            case "PHP":
77572dd708SAndreas Gohr                $this->_feed = new PHPCreator();
78572dd708SAndreas Gohr                break;
79572dd708SAndreas Gohr            case "GPX":
80572dd708SAndreas Gohr                $this->_feed = new GPXCreator();
81572dd708SAndreas Gohr                break;
82572dd708SAndreas Gohr            case "KML":
83572dd708SAndreas Gohr                $this->_feed = new KMLCreator();
84572dd708SAndreas Gohr                break;
85572dd708SAndreas Gohr            case "JS":
86572dd708SAndreas Gohr                // fall through
87572dd708SAndreas Gohr            case "JAVASCRIPT":
88572dd708SAndreas Gohr                $this->_feed = new JSCreator();
89572dd708SAndreas Gohr                break;
90572dd708SAndreas Gohr
91572dd708SAndreas Gohr            default:
92572dd708SAndreas Gohr                $this->_feed = new RSSCreator091();
93572dd708SAndreas Gohr                break;
94572dd708SAndreas Gohr        }
95572dd708SAndreas Gohr
96572dd708SAndreas Gohr        $vars = get_object_vars($this);
97572dd708SAndreas Gohr        foreach ($vars as $key => $value) {
98572dd708SAndreas Gohr            // prevent overwriting of properties "contentType", "encoding"; do not copy "_feed" itself
99572dd708SAndreas Gohr            if (!in_array($key, array("_feed", "contentType", "encoding"))) {
100572dd708SAndreas Gohr                $this->_feed->{$key} = $this->{$key};
101572dd708SAndreas Gohr            }
102572dd708SAndreas Gohr        }
103572dd708SAndreas Gohr    }
104572dd708SAndreas Gohr
105572dd708SAndreas Gohr    /**
106572dd708SAndreas Gohr     * Creates a syndication feed based on the items previously added.
107572dd708SAndreas Gohr     *
108572dd708SAndreas Gohr     * @see FeedCreator::addItem()
109572dd708SAndreas Gohr     * @param string $format format the feed should comply to. Valid values are:
110572dd708SAndreas Gohr     *                       "PIE0.1", "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3", "HTML", "JS"
111572dd708SAndreas Gohr     * @return string the contents of the feed.
112572dd708SAndreas Gohr     */
113572dd708SAndreas Gohr    public function createFeed($format = "RSS0.91")
114572dd708SAndreas Gohr    {
115572dd708SAndreas Gohr        $this->_setFormat($format);
116572dd708SAndreas Gohr
117572dd708SAndreas Gohr        return $this->_feed->createFeed();
118572dd708SAndreas Gohr    }
119572dd708SAndreas Gohr
120572dd708SAndreas Gohr    /**
121572dd708SAndreas Gohr     * Saves this feed as a file on the local disk. After the file is saved, an HTTP redirect
122572dd708SAndreas Gohr     * header may be sent to redirect the use to the newly created file.
123572dd708SAndreas Gohr     *
124572dd708SAndreas Gohr     * @since 1.4
125572dd708SAndreas Gohr     * @param string $format           format the feed should comply to. Valid values are:
126572dd708SAndreas Gohr     *                                 "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM",
127572dd708SAndreas Gohr     *                                 "ATOM0.3", "HTML", "JS"
128572dd708SAndreas Gohr     * @param string $filename         optional    the filename where a recent version of the feed is saved. If not
129d3233986SAndreas Gohr     *                                 specified, the filename is $_SERVER["SCRIPT_NAME"] with the extension changed to
130572dd708SAndreas Gohr     *                                 .xml (see _generateFilename()).
131572dd708SAndreas Gohr     * @param boolean $displayContents optional    send the content of the file or not. If true, the file will be sent
132572dd708SAndreas Gohr     *                                 in the body of the response.
133572dd708SAndreas Gohr     */
134572dd708SAndreas Gohr    public function saveFeed($format = "RSS0.91", $filename = "", $displayContents = true)
135572dd708SAndreas Gohr    {
136572dd708SAndreas Gohr        $this->_setFormat($format);
137572dd708SAndreas Gohr        $this->_feed->saveFeed($filename, $displayContents);
138572dd708SAndreas Gohr    }
139572dd708SAndreas Gohr
140572dd708SAndreas Gohr    /**
141572dd708SAndreas Gohr     * Turns on caching and checks if there is a recent version of this feed in the cache.
142572dd708SAndreas Gohr     * If there is, an HTTP redirect header is sent.
143572dd708SAndreas Gohr     * To effectively use caching, you should create the FeedCreator object and call this method
144572dd708SAndreas Gohr     * before anything else, especially before you do the time consuming task to build the feed
145572dd708SAndreas Gohr     * (web fetching, for example).
146572dd708SAndreas Gohr     *
147572dd708SAndreas Gohr     * @param string $format   format the feed should comply to. Valid values are:
148572dd708SAndreas Gohr     *                         "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3".
149572dd708SAndreas Gohr     * @param string $filename optional the filename where a recent version of the feed is saved. If not specified, the
150d3233986SAndreas Gohr     *                         filename is $_SERVER["SCRIPT_NAME"] with the extension changed to .xml (see
151572dd708SAndreas Gohr     *                         _generateFilename()).
152572dd708SAndreas Gohr     * @param int $timeout     optional the timeout in seconds before a cached version is refreshed (defaults to 3600 =
153572dd708SAndreas Gohr     *                         1 hour)
154572dd708SAndreas Gohr     */
155572dd708SAndreas Gohr    public function useCached($format = "RSS0.91", $filename = "", $timeout = 3600)
156572dd708SAndreas Gohr    {
157572dd708SAndreas Gohr        $this->_setFormat($format);
158572dd708SAndreas Gohr        $this->_feed->useCached($filename, $timeout);
159572dd708SAndreas Gohr    }
160572dd708SAndreas Gohr}
161