xref: /dokuwiki/vendor/openpsa/universalfeedcreator/lib/UniversalFeedCreator.php (revision c13ef3ba1de57be630245f8ce5de354bdf7dc966)
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    {
2164d8abdbSAndreas 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
91*c13ef3baSAndreas Gohr            case "JSON":
92*c13ef3baSAndreas Gohr                $this->_feed = new JSONCreator();
93*c13ef3baSAndreas Gohr                break;
94*c13ef3baSAndreas Gohr
95572dd708SAndreas Gohr            default:
96572dd708SAndreas Gohr                $this->_feed = new RSSCreator091();
97572dd708SAndreas Gohr                break;
98572dd708SAndreas Gohr        }
99572dd708SAndreas Gohr
100572dd708SAndreas Gohr        $vars = get_object_vars($this);
101572dd708SAndreas Gohr        foreach ($vars as $key => $value) {
102572dd708SAndreas Gohr            // prevent overwriting of properties "contentType", "encoding"; do not copy "_feed" itself
103572dd708SAndreas Gohr            if (!in_array($key, array("_feed", "contentType", "encoding"))) {
104572dd708SAndreas Gohr                $this->_feed->{$key} = $this->{$key};
105572dd708SAndreas Gohr            }
106572dd708SAndreas Gohr        }
107572dd708SAndreas Gohr    }
108572dd708SAndreas Gohr
109572dd708SAndreas Gohr    /**
110572dd708SAndreas Gohr     * Creates a syndication feed based on the items previously added.
111572dd708SAndreas Gohr     *
112572dd708SAndreas Gohr     * @see FeedCreator::addItem()
113572dd708SAndreas Gohr     * @param string $format format the feed should comply to. Valid values are:
114572dd708SAndreas Gohr     *                       "PIE0.1", "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3", "HTML", "JS"
115572dd708SAndreas Gohr     * @return string the contents of the feed.
116572dd708SAndreas Gohr     */
117572dd708SAndreas Gohr    public function createFeed($format = "RSS0.91")
118572dd708SAndreas Gohr    {
119572dd708SAndreas Gohr        $this->_setFormat($format);
120572dd708SAndreas Gohr
121572dd708SAndreas Gohr        return $this->_feed->createFeed();
122572dd708SAndreas Gohr    }
123572dd708SAndreas Gohr
124572dd708SAndreas Gohr    /**
125572dd708SAndreas Gohr     * Saves this feed as a file on the local disk. After the file is saved, an HTTP redirect
126572dd708SAndreas Gohr     * header may be sent to redirect the use to the newly created file.
127572dd708SAndreas Gohr     *
128572dd708SAndreas Gohr     * @since 1.4
129572dd708SAndreas Gohr     * @param string $format           format the feed should comply to. Valid values are:
130572dd708SAndreas Gohr     *                                 "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM",
131572dd708SAndreas Gohr     *                                 "ATOM0.3", "HTML", "JS"
132572dd708SAndreas Gohr     * @param string $filename         optional    the filename where a recent version of the feed is saved. If not
133d3233986SAndreas Gohr     *                                 specified, the filename is $_SERVER["SCRIPT_NAME"] with the extension changed to
134572dd708SAndreas Gohr     *                                 .xml (see _generateFilename()).
135572dd708SAndreas Gohr     * @param boolean $displayContents optional    send the content of the file or not. If true, the file will be sent
136572dd708SAndreas Gohr     *                                 in the body of the response.
137572dd708SAndreas Gohr     */
138572dd708SAndreas Gohr    public function saveFeed($format = "RSS0.91", $filename = "", $displayContents = true)
139572dd708SAndreas Gohr    {
140572dd708SAndreas Gohr        $this->_setFormat($format);
141572dd708SAndreas Gohr        $this->_feed->saveFeed($filename, $displayContents);
142572dd708SAndreas Gohr    }
143572dd708SAndreas Gohr
144572dd708SAndreas Gohr    /**
145572dd708SAndreas Gohr     * Turns on caching and checks if there is a recent version of this feed in the cache.
146572dd708SAndreas Gohr     * If there is, an HTTP redirect header is sent.
147572dd708SAndreas Gohr     * To effectively use caching, you should create the FeedCreator object and call this method
148572dd708SAndreas Gohr     * before anything else, especially before you do the time consuming task to build the feed
149572dd708SAndreas Gohr     * (web fetching, for example).
150572dd708SAndreas Gohr     *
151572dd708SAndreas Gohr     * @param string $format   format the feed should comply to. Valid values are:
152572dd708SAndreas Gohr     *                         "PIE0.1" (deprecated), "mbox", "RSS0.91", "RSS1.0", "RSS2.0", "OPML", "ATOM0.3".
153572dd708SAndreas Gohr     * @param string $filename optional the filename where a recent version of the feed is saved. If not specified, the
154d3233986SAndreas Gohr     *                         filename is $_SERVER["SCRIPT_NAME"] with the extension changed to .xml (see
155572dd708SAndreas Gohr     *                         _generateFilename()).
156572dd708SAndreas Gohr     * @param int $timeout     optional the timeout in seconds before a cached version is refreshed (defaults to 3600 =
157572dd708SAndreas Gohr     *                         1 hour)
158572dd708SAndreas Gohr     */
159572dd708SAndreas Gohr    public function useCached($format = "RSS0.91", $filename = "", $timeout = 3600)
160572dd708SAndreas Gohr    {
161572dd708SAndreas Gohr        $this->_setFormat($format);
162572dd708SAndreas Gohr        $this->_feed->useCached($filename, $timeout);
163572dd708SAndreas Gohr    }
164572dd708SAndreas Gohr}
165